[libvirt] [PATCH] network: avoid existing bridges during rpm install of default network

Laine Stump laine at laine.org
Tue Sep 29 17:11:07 UTC 2015

When we install the libvirt-daemon-config-network package from an rpm,
the specfile checks that the subnet used for the default network isn't
already used, but blindly assumes that virbr0 is available. This is
almost always the case, but there could be a situation where someone
was already using virbr0 for their own network, then decided to
install libvirt-daemon-config-network, leading to a failure when they
tried to start the default network.

This patch adds a bit to the %post script for the
daemon-network-config package (in the specfile, used only for .rpm
packages) that checks "ip link show" and the existing libvirt network
xml files in a loop to find the lowest numbered virbrN that is
currently unused by either.

(note that we already check for in-use bridge devices when defining a
network in libvirt's network driver, but the rpm install bypasses
libvirt (which may not yet be fully functional) and creates the xml
file itself).

I found this sitting in a source tree, written a few months ago and
forgotten. It is a followup to commit 37b8bc6f, which added a similar
check in the network driver during virNetworkDefineXML(). I vaguely
recall someone reporting this problem on IRC or maybe on one of the
mailing lists (they had created a bridge manually using the name
virbr0, then when they installed libvirt, it happily made a network
definition using virbr0). Yes, not very common, but possible.

 libvirt.spec.in | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/libvirt.spec.in b/libvirt.spec.in
index 78a4cc3..931c5b9 100644
--- a/libvirt.spec.in
+++ b/libvirt.spec.in
@@ -1793,8 +1793,24 @@ if test $1 -eq 1 && test ! -f %{_sysconfdir}/libvirt/qemu/networks/default.xml ;
+    # find an unused bridge device named virbrN (this assumes that the
+    # original name in default.xml is exactly "virbr0", so do not
+    # change it there without also changing orig_br).
+    orig_br=0
+    br=${orig_br}
+    while ip link show virbr${br} >/dev/null 2>&1 ||\
+          (grep '<bridge ' %{_sysconfdir}/libvirt/qemu/networks/*.xml |\
+           grep virbr${br} >/dev/null 2>&1); do
+      br=$(expr ${br} + 1)
+      # safety to prevent an endless loop
+      if test ${br} -gt 256; then
+          break;
+      fi
+    done
     sed -e "s/${orig_sub}/${sub}/g" \
+        -e "s/virbr${orig_br}/virbr${br}/" \
         -e "s,</name>,</name>\n  <uuid>$UUID</uuid>," \
          < %{_datadir}/libvirt/networks/default.xml \
          > %{_sysconfdir}/libvirt/qemu/networks/default.xml

More information about the libvir-list mailing list