BUGFIX setupVnetInterfaceJailSide

This commit is contained in:
yo 2023-08-06 14:50:32 +02:00
parent 14984f417c
commit 549d517cf9

View File

@ -899,7 +899,7 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) {
return epairs, nil
}
func setupVnetInterfaceJailSide(jail *Jail) error {
func setupVnetInterfaceJailSide(jail *Jail, hostepairs []string) error {
var jsmac []byte
var err error
@ -919,7 +919,7 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
}
// Loop through configured interfaces
for _, nicCnf := range strings.Split(jail.Config.Interfaces, ",") {
for i, nicCnf := range strings.Split(jail.Config.Interfaces, ",") {
v := strings.Split(nicCnf, ":")
if len(v) != 2 {
return fmt.Errorf("Invalid value for Interfaces: %s\n", nicCnf)
@ -930,6 +930,8 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
// inside jail final nic name
jnic := strings.Replace(v[0], "vnet", "epair", 1)
jnic = jnic + "b"
// host side associated jail nic name
jsepair := fmt.Sprintf("%sb", strings.TrimSuffix(hostepairs[i], "a"))
// Get jail side MAC
pname := fmt.Sprintf("Config.%s_mac", nic)
@ -948,7 +950,7 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
jsmac = val.Bytes()
}
cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jnic, jail.InternalName)
cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jsepair, jail.InternalName)
_, err := executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error linking interface to jail: %v\n", err)
@ -960,14 +962,14 @@ func setupVnetInterfaceJailSide(jail *Jail) error {
return fmt.Errorf("Error getting bridge %s mtu: %v\n", bridge, err)
}
cmd = fmt.Sprintf("/usr/sbin/jexec %d ifconfig %s mtu %d", jail.JID, jnic, mtu)
cmd = fmt.Sprintf("/usr/sbin/jexec %d ifconfig %s mtu %d", jail.JID, jsepair, mtu)
_, err = executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error setting mtu: %v\n", err)
}
// rename epairXXb to epair0b (or opair1b, ...)
cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, jnic, jnic)
cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, jsepair, jnic)
_, err = executeCommand(cmd)
if err != nil {
return fmt.Errorf("Error linking interface to jail: %v\n", err)
@ -1070,9 +1072,9 @@ func StartJailsAtBoot() {
var curThNb int
var curPri int
// Get boot enabled jails
// Get boot enabled non-template jails
for _, j := range gJails {
if j.Config.Boot > 0 {
if j.Config.Boot > 0 && !strings.EqualFold(j.Config.Jailtype, "template") {
startList = append(startList, j)
}
}
@ -1166,7 +1168,7 @@ func StartJail(args []string) {
for _, a := range args {
// Check if jail exist and is distinctly named
cj, err = getJailFromArray(a, []string{"jail"}, gJails)
cj, err = getJailFromArray(a, []string{"basejail", "jail"}, gJails)
if err != nil {
fmt.Printf("Error getting jail: %s\n", err)
continue
@ -1178,7 +1180,7 @@ func StartJail(args []string) {
}
fmt.Printf("> Starting jail %s\n", cj.Name)
// Set InternalName as it is used by some of these
cj.InternalName = fmt.Sprintf("ioc-%s", cj.Name)
@ -1340,34 +1342,34 @@ func StartJail(args []string) {
cj.Config.Defaultrouter = ip4[0]
}
}
// See https://github.com/iocage/iocage/blob/e94863d4c54f02523fb09e62e48be7db9ac92eda/iocage_lib/ioc_start.py:401
if cj.Config.Vnet == 0 {
// Not supported
fmt.Printf("Only VNet jails supported\n")
return
}
var net []string
if false == strings.EqualFold(cj.Config.Vnet_interfaces, "none") {
net = append(net, strings.Split(cj.Config.Vnet_interfaces, " ")...)
}
err, dynrs := buildDevfsRuleSet(cj, &gMdevfs)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
err = buildJailParameters(cj, dynrs)
if err != nil {
fmt.Printf("%s\n", err.Error())
return
}
// Synchronize jail config to disk
writeConfigToDisk(cj, false)
cj.WriteConfigToDisk(false)
start_cmd := fmt.Sprintf("/usr/sbin/jail -f /var/run/jail.%s.conf -c", cj.InternalName)
//TODO: handle start_env & prestart_env, could be used by iocage plugins
@ -1377,17 +1379,17 @@ func StartJail(args []string) {
fmt.Printf("Aborting jail start\n")
return
}
fmt.Printf(" > Start jail:\n")
_, err = executeCommand(start_cmd)
if err != nil {
fmt.Printf("Error starting jail %s: %v\n", cj.Name, err)
return
}
fmt.Printf(" > Start jail: OK\n")
fmt.Printf(" > With devfs ruleset %d\n", dynrs)
// Update running state, JID and Devfs_ruleset
cj.Running = true
cj.Devfs_ruleset = dynrs
@ -1401,13 +1403,13 @@ func StartJail(args []string) {
break
}
}
hostInt, err := gJailHost.GetInterfaces()
if err != nil {
fmt.Printf("Error listing jail host interfaces: %v\n", err)
return
}
if false == strings.EqualFold(cj.Config.Vnet_default_interface, "auto") &&
false == strings.EqualFold(cj.Config.Vnet_default_interface, "none") &&
false == isStringInArray(hostInt, cj.Config.Vnet_default_interface) {
@ -1416,13 +1418,13 @@ func StartJail(args []string) {
}
fmt.Printf(" > Setup VNet network:\n")
_, err = setupVnetInterfaceHostSide(cj);
hsepairs, err := setupVnetInterfaceHostSide(cj);
if err != nil {
fmt.Printf("Error setting VNet interface host side: %v\n", err)
return
}
if err = setupVnetInterfaceJailSide(cj); err != nil {
if err = setupVnetInterfaceJailSide(cj, hsepairs); err != nil {
fmt.Printf("Error setting VNet interface jail side: %v\n", err)
return
}
@ -1439,7 +1441,7 @@ func StartJail(args []string) {
fmt.Printf(" > Setup default ipv4 gateway: OK\n")
}
}
if cj.Config.Ip6_addr != "none" {
fmt.Printf(" > Setup default ipv6 gateway:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add -6 default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter6)
@ -1450,7 +1452,7 @@ func StartJail(args []string) {
fmt.Printf(" > Setup default ipv6 gateway: OK\n")
}
}
if cj.Config.Jail_zfs > 0 {
fmt.Printf(" > Jail ZFS datasets:\n")
err = jailZfsDatasets(cj)
@ -1465,14 +1467,14 @@ func StartJail(args []string) {
if err != nil {
fmt.Printf("%s\n", err)
}
if cj.Config.Host_time > 0 {
err = copyLocalTime(cj)
if err != nil {
fmt.Printf("%s\n", err)
}
}
// Start services
if len(cj.Config.Exec_start) > 0 {
fmt.Printf(" > Start services:\n")
@ -1484,7 +1486,7 @@ func StartJail(args []string) {
fmt.Printf(" > Start services: OK\n")
}
}
if cj.Config.Rtsold > 0 || strings.EqualFold(cj.Config.Ip6_addr, "accept_rtadv") {
fmt.Printf(" > Start rtsold:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d service rtsold start", cj.Config.Exec_fib, cj.JID)
@ -1495,7 +1497,7 @@ func StartJail(args []string) {
fmt.Printf(" > Start rtsold: OK\n")
}
}
// TODO: Execute Exec_poststart
if len(cj.Config.Exec_poststart) > 0 {
fmt.Printf(" > Execute post-start:\n")
@ -1507,19 +1509,18 @@ func StartJail(args []string) {
fmt.Printf(" > Execute post-start: OK\n")
}
}
// WIP 10/07/2022 : https://github.com/iocage/iocage/blob/master/iocage_lib/ioc_start.py#L891
// TODO: Handle dhcp
// TODO: Apply rctl
// Update last_started
// 23/07/2023 : This is not working, when writing to disk the old value is used
dt := time.Now()
curDate := fmt.Sprintf("%s", dt.Format("2006-01-02 15:04:05"))
cj.Config.Last_started = curDate
writeConfigToDisk(cj, false)
/*
out, err := executeCommand(fmt.Sprintf("rctl jail:%s", cj.InternalName))
if err == nil && len(out) > 0 {