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