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