mirror of
https://github.com/containers/podman.git
synced 2026-04-03 06:26:14 -04:00
when removing a podman network, we need to make sure we delete the network interface if one was ever created (by running a container). also, when removing networks, we check if any containers are using the network. if they are, we error out unless the user provides a 'force' option which will remove the containers in question. Signed-off-by: baude <bbaude@redhat.com>
56 lines
1.4 KiB
Go
56 lines
1.4 KiB
Go
package network
|
|
|
|
import (
|
|
"fmt"
|
|
"os/exec"
|
|
|
|
"github.com/containers/libpod/pkg/util"
|
|
"github.com/containers/libpod/utils"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
// GetFreeDeviceName returns a device name that is unused; used when no network
|
|
// name is provided by user
|
|
func GetFreeDeviceName() (string, error) {
|
|
var (
|
|
deviceNum uint
|
|
deviceName string
|
|
)
|
|
networkNames, err := GetNetworkNamesFromFileSystem()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
liveNetworksNames, err := GetLiveNetworkNames()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
for {
|
|
deviceName = fmt.Sprintf("%s%d", CNIDeviceName, deviceNum)
|
|
logrus.Debugf("checking if device name %s exists in other cni networks", deviceName)
|
|
if util.StringInSlice(deviceName, networkNames) {
|
|
deviceNum++
|
|
continue
|
|
}
|
|
logrus.Debugf("checking if device name %s exists in live networks", deviceName)
|
|
if !util.StringInSlice(deviceName, liveNetworksNames) {
|
|
break
|
|
}
|
|
// TODO Still need to check the bridge names for a conflict but I dont know
|
|
// how to get them yet!
|
|
deviceNum++
|
|
}
|
|
return deviceName, nil
|
|
}
|
|
|
|
// RemoveInterface removes an interface by the given name
|
|
func RemoveInterface(interfaceName string) error {
|
|
// Make sure we have the ip command on the system
|
|
ipPath, err := exec.LookPath("ip")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
// Delete the network interface
|
|
_, err = utils.ExecCmd(ipPath, []string{"link", "del", interfaceName}...)
|
|
return err
|
|
}
|