BUGFIX stopJail: Destroy interfaces once, whatever the current configuration

This commit is contained in:
yo 2024-09-05 10:05:21 +02:00
parent 199b3b1f7f
commit 7ca633d946

View File

@ -7,8 +7,8 @@ import (
"sync" "sync"
"errors" "errors"
"regexp" "regexp"
"slices"
"os/exec" "os/exec"
//"reflect"
"strconv" "strconv"
"strings" "strings"
@ -83,26 +83,39 @@ func umountAndUnjailZFS(jail *Jail) error {
} }
func destroyVNetInterfaces(jail *Jail) error { func destroyVNetInterfaces(jail *Jail) error {
if !strings.EqualFold(jail.Config.Ip4_addr, "none") { // Wherever ipv6/ipv4 is enabled, if interface exist we destroy it
for _, i := range strings.Split(jail.Config.Ip4_addr, ",") { var vnetnames []string
for _, i := range strings.Split(jail.Config.Ip4_addr, ",") {
if len(strings.Split(i, "|")) == 2 {
iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID) iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID)
fmt.Printf("%s: ", iname) if !slices.Contains(vnetnames, iname) {
_, err := executeCommand(fmt.Sprintf("ifconfig %s destroy", iname)) vnetnames = append(vnetnames, iname)
//_, err := executeScript(fmt.Sprintf("ifconfig %s destroy >/dev/null 2>&1", iname))
if err != nil {
return err
} else {
fmt.Printf("OK\n")
} }
} }
} }
if !strings.EqualFold(jail.Config.Ip6_addr, "none") { for _, i := range strings.Split(jail.Config.Ip6_addr, ",") {
for _, i := range strings.Split(jail.Config.Ip6_addr, ",") { if len(strings.Split(i, "|")) == 2 {
iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID) iname := fmt.Sprintf("%s.%d", strings.Split(i, "|")[0], jail.JID)
fmt.Printf("%s: ", iname) if !slices.Contains(vnetnames, iname) {
vnetnames = append(vnetnames, iname)
}
}
}
for _, iname := range vnetnames {
fmt.Printf(" >%s: ", iname)
_, err := executeCommand(fmt.Sprintf("ifconfig %s", iname))
if err != nil {
if strings.Contains(err.Error(), "does not exist") {
fmt.Printf("OK\n")
} else {
fmt.Printf("ERR: %v\n", err)
return err
}
} else {
_, err := executeCommand(fmt.Sprintf("ifconfig %s destroy", iname)) _, err := executeCommand(fmt.Sprintf("ifconfig %s destroy", iname))
//_, err := executeScript(fmt.Sprintf("ifconfig %s destroy >/dev/null 2>&1", iname))
if err != nil { if err != nil {
fmt.Printf("ERR: %v\n", err)
return err return err
} else { } else {
fmt.Printf("OK\n") fmt.Printf("OK\n")