BUGFIXes on dhcp & multi net handling

This commit is contained in:
yo 2023-08-05 19:49:59 +02:00
parent fe4192da2d
commit 534deb371c

View File

@ -330,8 +330,10 @@ func configureDhcpOrAcceptRtadv(jail *Jail, ipproto int, enable bool) error {
for _, n := range nics { for _, n := range nics {
// vnet0 is epair0b inside jail // vnet0 is epair0b inside jail
if strings.Contains(n, "vnet") { //if strings.Contains(n, "vnet") {
n = fmt.Sprintf("%sb", strings.Replace(n, "vnet", "epair", 1)) if strings.HasPrefix(n, "vnet") {
splitd := strings.Split(n, "|")
n = fmt.Sprintf("%sb", strings.Replace(splitd[0], "vnet", "epair", 1))
} }
key := fmt.Sprintf("ifconfig_%s", n) key := fmt.Sprintf("ifconfig_%s", n)
value := "SYNCDHCP" value := "SYNCDHCP"
@ -342,12 +344,12 @@ func configureDhcpOrAcceptRtadv(jail *Jail, ipproto int, enable bool) error {
} }
if enable == true { if enable == true {
err := enableRcKeyValue(jail.ConfigPath, key, value) err := enableRcKeyValue(fmt.Sprintf("%s/etc/rc.conf", jail.RootPath), key, value)
if err != nil { if err != nil {
return fmt.Errorf("ERROR setting %s=%s with sysrc for jail %s: %s\n", key, value, jail.Name, err) return fmt.Errorf("ERROR setting %s=%s with sysrc for jail %s: %s\n", key, value, jail.Name, err)
} }
} else { } else {
err := disableRcKey(jail.ConfigPath, key) err := disableRcKey(fmt.Sprintf("%s/etc/rc.conf", jail.RootPath), key)
if err != nil { if err != nil {
return fmt.Errorf("ERROR deleting %s with sysrc for jail %s: %v\n", key, jail.Name, err) return fmt.Errorf("ERROR deleting %s with sysrc for jail %s: %v\n", key, jail.Name, err)
} }
@ -508,7 +510,7 @@ func buildDevfsRuleSet(jail *Jail, m *sync.Mutex) (error, int) {
} }
} }
log.Debug("buildDevfsRuleSet: Build ruleset %d\n", ruleset) log.Debugf("buildDevfsRuleSet: Build ruleset %d\n", ruleset)
// Get default devfs_ruleset for the datastore // Get default devfs_ruleset for the datastore
// UPDATE: We don't need this as every jail have a default Devfs_ruleset value // UPDATE: We don't need this as every jail have a default Devfs_ruleset value
@ -893,11 +895,11 @@ func setupVnetInterfaceHostSide(jail *Jail) ([]string, error) {
} }
epairs = append(epairs, hsepair) epairs = append(epairs, hsepair)
} }
log.Debugf("setupVnetInterfaceHostSide: returning %v\n", epairs)
return epairs, nil return epairs, nil
} }
func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error { func setupVnetInterfaceJailSide(jail *Jail) error {
var jsmac []byte var jsmac []byte
var err error var err error
@ -946,10 +948,7 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
jsmac = val.Bytes() jsmac = val.Bytes()
} }
lasta := strings.LastIndex(hsepair, "a") cmd := fmt.Sprintf("/sbin/ifconfig %s vnet %s", jnic, jail.InternalName)
jsepair := hsepair[:lasta] + strings.Replace(hsepair[lasta:], "a", "b", 1)
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)
@ -961,14 +960,14 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) 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, jsepair, mtu) cmd = fmt.Sprintf("/usr/sbin/jexec %d ifconfig %s mtu %d", jail.JID, jnic, 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, jsepair, jnic) cmd = fmt.Sprintf("/usr/sbin/setfib %s jexec %d ifconfig %s name %s", jail.Config.Exec_fib, jail.JID, jnic, 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)
@ -1010,6 +1009,8 @@ func setupVnetInterfaceJailSide(jail *Jail, hsepair string) error {
} }
} }
log.Debugf("setupVnetInterfaceJailSide: return with success\n")
return nil return nil
} }
@ -1165,7 +1166,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, gJails) cj, err = getJailFromArray(a, []string{"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
@ -1415,21 +1416,20 @@ func StartJail(args []string) {
} }
fmt.Printf(" > Setup VNet network:\n") fmt.Printf(" > Setup VNet network:\n")
hsepairs, err := setupVnetInterfaceHostSide(cj); _, 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
} }
for _, ep := range hsepairs { if err = setupVnetInterfaceJailSide(cj); err != nil {
if err = setupVnetInterfaceJailSide(cj, ep); 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
} }
}
fmt.Printf(" > Setup VNet network: OK\n") fmt.Printf(" > Setup VNet network: OK\n")
// TODO: Handle DHCP // Set default route, unless main network is dhcp
if ! cj.isFirstNetDhcp() {
fmt.Printf(" > Setup default ipv4 gateway:\n") fmt.Printf(" > Setup default ipv4 gateway:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter) cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d route add default %s", cj.Config.Exec_fib, cj.JID, cj.Config.Defaultrouter)
out, err := executeCommand(cmd) out, err := executeCommand(cmd)
@ -1438,6 +1438,7 @@ func StartJail(args []string) {
} else { } else {
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")
@ -1477,7 +1478,7 @@ func StartJail(args []string) {
fmt.Printf(" > Start services:\n") fmt.Printf(" > Start services:\n")
cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d %s", cj.Config.Exec_fib, cj.JID, cj.Config.Exec_start) cmd := fmt.Sprintf("/usr/sbin/setfib %s /usr/sbin/jexec %d %s", cj.Config.Exec_fib, cj.JID, cj.Config.Exec_start)
err := executeCommandNonBlocking(cmd) err := executeCommandNonBlocking(cmd)
if err != nil && len(out) > 0 { if err != nil {
fmt.Printf("Error: %v\n", err) fmt.Printf("Error: %v\n", err)
} else { } else {
fmt.Printf(" > Start services: OK\n") fmt.Printf(" > Start services: OK\n")