github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/network/devices.go (about) 1 package network 2 3 import ( 4 "fmt" 5 "os/exec" 6 7 "github.com/containers/libpod/pkg/util" 8 "github.com/containers/libpod/utils" 9 "github.com/sirupsen/logrus" 10 ) 11 12 // GetFreeDeviceName returns a device name that is unused; used when no network 13 // name is provided by user 14 func GetFreeDeviceName() (string, error) { 15 var ( 16 deviceNum uint 17 deviceName string 18 ) 19 networkNames, err := GetNetworkNamesFromFileSystem() 20 if err != nil { 21 return "", err 22 } 23 liveNetworksNames, err := GetLiveNetworkNames() 24 if err != nil { 25 return "", err 26 } 27 bridgeNames, err := GetBridgeNamesFromFileSystem() 28 if err != nil { 29 return "", err 30 } 31 for { 32 deviceName = fmt.Sprintf("%s%d", CNIDeviceName, deviceNum) 33 logrus.Debugf("checking if device name %q exists in other cni networks", deviceName) 34 if util.StringInSlice(deviceName, networkNames) { 35 deviceNum++ 36 continue 37 } 38 logrus.Debugf("checking if device name %q exists in live networks", deviceName) 39 if util.StringInSlice(deviceName, liveNetworksNames) { 40 deviceNum++ 41 continue 42 } 43 logrus.Debugf("checking if device name %q already exists as a bridge name ", deviceName) 44 if !util.StringInSlice(deviceName, bridgeNames) { 45 break 46 } 47 deviceNum++ 48 } 49 return deviceName, nil 50 } 51 52 // RemoveInterface removes an interface by the given name 53 func RemoveInterface(interfaceName string) error { 54 // Make sure we have the ip command on the system 55 ipPath, err := exec.LookPath("ip") 56 if err != nil { 57 return err 58 } 59 // Delete the network interface 60 _, err = utils.ExecCmd(ipPath, []string{"link", "del", interfaceName}...) 61 return err 62 }