1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2026-12-03 01:16:10 +00:00

fix(interfaces): an interface a VM or a network uses is not deleted

A stopped bridge was deleted while a shut-off VM's network card or a
libvirt network named it; that VM or network then did not start.
Deleting an interface is now refused while any definition of a VM,
running or not, or a network names it, with their names.

A new bridge was offered a device a VM's macvtap card or a network
forwarding to it used; bridging it cut that VM off. Such devices are no
longer offered. A NAT or routed network's outgoing device counts as
used too.
This commit is contained in:
catborise 2026-11-12 15:53:37 +03:00
parent edc165cfc4
commit de249ebad7
7 changed files with 97 additions and 27 deletions

View file

@ -80,13 +80,14 @@ def interface_info(iface, tree):
def networks_by_device(conn):
"""The host's libvirt networks by the devices they use: their bridge, or
every interface or physical function they forward to. One listing, one
"""The host's libvirt networks by the devices they use: their bridge, the
device NAT or routing goes out by, or every interface or physical
function they forward to. One listing, one
XML read per network."""
by_device = {}
for net in conn.listAllNetworks(0):
tree = ElementTree.fromstring(net.XMLDesc(0))
devices = {attribute(tree, "bridge", "name")}
devices = {attribute(tree, "bridge", "name"), attribute(tree, "forward", "dev")}
for forward in tree.findall("forward/interface") + tree.findall("forward/pf"):
devices.add(forward.get("dev"))
devices.discard(None)
@ -109,15 +110,18 @@ class wvmInterfaces(wvmConnect):
def get_member_candidates(self, netdevs):
"""The devices of netdevs a new bridge can take: not the loopback, a
bridge, bond or VLAN of the host nor a device in one as configured, at
any depth (a bridge's bond's devices), not the bridge of a libvirt
network and not a VM's tap (libvirt names them vnetN and macvtapN)."""
any depth (a bridge's bond's devices), not a device a libvirt network
or a VM's macvtap card uses and not a VM's tap (libvirt names them
vnetN and macvtapN)."""
taken = {"lo"}
for iface in self.wvm.listAllInterfaces(0):
tree = ElementTree.fromstring(iface.XMLDesc(VIR_INTERFACE_XML_INACTIVE))
if tree.get("type") in ("bridge", "bond", "vlan"):
taken.update(element.get("name") for element in tree.iter("interface"))
for net in self.wvm.listAllNetworks(0):
taken.add(util.get_xml_path(net.XMLDesc(0), "/network/bridge/@name"))
taken.update(networks_by_device(self.wvm))
for _name, trees in self.domain_trees():
for tree in trees:
taken.update(tree.xpath("devices/interface[@type='direct']/source/@dev"))
return [dev for dev in netdevs if dev not in taken and not dev.startswith(("vnet", "macvtap"))]
def define_iface(self, xml, flag=0):
@ -264,6 +268,24 @@ class wvmInterface(wvmConnect):
def start_iface(self):
self.iface.create()
def check_unused(self):
"""Refused while a VM's network card (in any of its definitions, the
VM running or not) or a libvirt network names the interface: that VM
or network would no longer start."""
name = self.iface.name()
networks = networks_by_device(self.wvm).get(name, [])
vms = self.domains_using(lambda tree: any(
name in (source.get("bridge"), source.get("dev")) or source.get("network") in networks
for source in tree.xpath("devices/interface/source")))
named = []
if vms:
named.append(util.message(gettext_noop("VMs %(names)s"), names=", ".join(vms)))
if networks:
named.append(util.message(gettext_noop("networks %(names)s"), names=", ".join(networks)))
if named:
raise util.OperationError(gettext_noop("Interface %(iface)s is used by %(named)s; remove it from them first"),
iface=name, named=named)
def delete_iface(self):
"""Undefine the interface, stopped first. netcf removes a bridge's
members with it, so each member is defined again on its own, as virsh
@ -273,6 +295,7 @@ class wvmInterface(wvmConnect):
All of it is one change of the host's configuration."""
if self.iface.isActive():
raise util.OperationError(gettext_noop("Stop interface %(iface)s before deleting it"), iface=self.iface.name())
self.check_unused()
tree = ElementTree.fromstring(self.iface.XMLDesc(VIR_INTERFACE_XML_INACTIVE))
members = tree.findall("bridge/interface")
if len(members) > 1 and tree.findall("protocol"):

View file

@ -64,6 +64,22 @@ class DeleteInterfaceTestCase(SimpleTestCase):
conn.delete_iface()
conn.iface.undefine.assert_not_called()
def test_an_interface_a_vm_or_a_network_names_is_refused(self):
# the VM is shut off: its saved definition names the bridge, and it would not start
vm = MagicMock(**{"name.return_value": "web-1", "isPersistent.return_value": True,
"XMLDesc.return_value": "<domain><devices><interface type='bridge'><source bridge='br1'/>"
"</interface></devices></domain>"})
lab = MagicMock(**{"name.return_value": "lab",
"XMLDesc.return_value": "<network><forward mode='bridge'/><bridge name='br1'/></network>"})
for domains, networks, why in (([vm], [], "used by VMs web-1;"), ([], [lab], "used by networks lab;"),
([vm], [lab], "used by VMs web-1; networks lab;")):
conn = interface("<interface type='bridge' name='br1'><bridge/></interface>")
conn.wvm.listAllDomains.return_value = domains
conn.wvm.listAllNetworks.return_value = networks
with self.subTest(why), self.assertRaisesRegex(OperationError, why):
conn.delete_iface()
conn.iface.undefine.assert_not_called()
def test_a_member_libvirt_refuses_rolls_the_whole_change_back(self):
conn = interface(f"<interface type='bridge' name='br1'><bridge>{BOND}</bridge></interface>")
conn.wvm.interfaceDefineXML.side_effect = libvirt.libvirtError("netcf: cannot write")

View file

@ -63,6 +63,16 @@ class ReadTests(SimpleTestCase):
for listed in conn.wvm.listAllInterfaces.return_value:
listed.XMLDesc.assert_called_with(libvirt.VIR_INTERFACE_XML_INACTIVE)
def test_a_device_a_vm_or_a_network_uses_is_taken(self):
# a VM's macvtap card, running or not, and a network forwarding to it would be cut off
conn = self.interfaces(networks=["<network><forward mode='bridge'><interface dev='eno6'/></forward></network>"])
conn.wvm.listAllNetworks.return_value[0].name.return_value = "direct"
conn.wvm.listAllDomains.return_value = [MagicMock(**{
"name.return_value": "web-1", "isPersistent.return_value": False,
"XMLDesc.return_value": "<domain><devices><interface type='direct'><source dev='eno5' mode='bridge'/>"
"</interface></devices></domain>"})]
self.assertEqual(conn.get_member_candidates(["eno5", "eno6", "eno7"]), ["eno7"])
def test_a_device_in_a_bond_of_a_bridge_is_taken(self):
nested = ("<interface type='bridge' name='br1'><mac address='52:54:00:aa:01:05'/><bridge><interface type='bond' name='bond0'><bond>"
"<interface type='ethernet' name='eno3'/><interface type='ethernet' name='eno4'/></bond></interface>"
@ -97,12 +107,12 @@ class BridgeUseTests(SimpleTestCase):
from vrtManager.interface import networks_by_device
nets = [MagicMock(**{"name.return_value": name, "XMLDesc.return_value": xml}) for name, xml in (
("default", "<network><bridge name='virbr0'/></network>"),
("default", "<network><forward mode='nat' dev='eth3'/><bridge name='virbr0'/></network>"),
("lan", "<network><forward mode='bridge'/><bridge name='br0'/></network>"),
("direct", "<network><forward mode='bridge'><interface dev='eth0'/><interface dev='eth1'/></forward></network>"),
("sriov", "<network><forward mode='hostdev'><pf dev='eth2'/></forward></network>"))]
self.assertEqual(networks_by_device(MagicMock(**{"listAllNetworks.return_value": nets})),
{"virbr0": ["default"], "br0": ["lan"], "eth0": ["direct"], "eth1": ["direct"], "eth2": ["sriov"]})
{"virbr0": ["default"], "eth3": ["default"], "br0": ["lan"], "eth0": ["direct"], "eth1": ["direct"], "eth2": ["sriov"]})
doms = [MagicMock(**{"name.return_value": name, "XMLDesc.return_value": xml}) for name, xml in (
("web", "<domain><devices><interface type='bridge'><mac address='52:54:00:00:00:01'/><source bridge='br0'/>"
"<target dev='vnet3'/></interface></devices></domain>"),