Package: libvirt
Version: 3.0.0-4
Severity: normal

Hi libvirt Maintainers,

Debian has been carrying the patch
"debian/Don-t-enable-default-network-on-boot.patch" for years now
and I agree that unconditionally enabling default networking
is too much messing with a user installation/network config.

We carry some [pre/post]inst code for quite a while now and it is
working fine. The code is actually from Serge and carried a long time
(since 2011, just as your patch is from 2008). Yet I didn't find a
bug/discussion to consider inclusion into Debian while I think it would
help there as well to fix the same issue.

Other than upstream which enables the default network unconditionally
this solution will essentially:
  - autostart default network if there is no conflicting network that
    will prevent it
  - on upgrades keep whatever the user set as autostart config

I don't like too much that to do so, it makes some assumptions on how
libvirt works internally. For example ln -s for autostart and depending
on output of other tools e.g. ip. But, as I mentioned, it is working fine
for years now and the convenience of just having your default network
around and running after installing is really great.

Attached is a patch on top of latest libvirt git in experimental, as a
change
like this would only ever be post strech anyway I guess experimental is the
place
to push it if you consider this an improvement for Debian as well.


-- 
Christian Ehrhardt
Software Engineer, Ubuntu Server
Canonical Ltd
From 841a7b3b46ab338a6ed62cda41ba4e2c164175cc Mon Sep 17 00:00:00 2001
From: Serge Hallyn <[email protected]>
Date: Wed, 14 Dec 2016 12:12:54 +0100
Subject: [PATCH] Autostart default network if not colliding

On top of "debian/Don-t-enable-default-network-on-boot.patch"
which makes sure not to unconditionally enable default networking
to avoid messing with a user installation/network config this change
provides collision detection to do so.

Other than upstream which enables the default network unconditionally
this solution will essentially:
  - autostart default network if there is no conflicting network that
    will prevent it
  - on upgrades keep whatever the user set as autostart config

The convenience of just having your default network around right after
install is really great.

Signed-off-by: Christian Ehrhardt <[email protected]>
---
 debian/libvirt-daemon-system.postinst | 70 +++++++++++++++++++++++++++++++++++
 debian/libvirt-daemon-system.preinst  |  6 +++
 2 files changed, 76 insertions(+)

diff --git a/debian/libvirt-daemon-system.postinst b/debian/libvirt-daemon-system.postinst
index b2e289c..fe196c6 100644
--- a/debian/libvirt-daemon-system.postinst
+++ b/debian/libvirt-daemon-system.postinst
@@ -67,6 +67,58 @@ add_users_groups()
     fi
 }
 
+includes_virbr0addr() {
+    viraddr="192.168.122.1"
+    a=$1
+    b=$2
+    for n in `seq 1 4`; do
+        aa=`echo $a | awk -F. '{ print $'$n' }'`
+        cc=`echo $viraddr | awk -F. '{ print $'$n' }'`
+        cmp=$((b/8))
+        if [ $cmp -ge $n ]; then
+            if [ $aa -ne $cc ]; then
+                echo "false"
+                return
+            fi
+        elif [ $((cmp+1)) -ge $n ]; then
+            # do we bother comparing partial (i.e. /25)?
+            :
+        else
+            break
+        fi
+    done
+    echo "true"
+    return
+}
+
+set_autostart()
+{
+    if [ ! -e /etc/libvirt/qemu/networks/autostart/default.xml ]; then
+        ln -s /etc/libvirt/qemu/networks/default.xml \
+              /etc/libvirt/qemu/networks/autostart/
+    fi
+}
+
+# on first install, don't set default network to autostart if we already
+# have a 192.168.122.0 network.  Good for instance for nested libvirt.
+maybe_set_autostart()
+{
+    # don't autostart virbr0 if 192.168.122.1/24 already exists
+    found=0
+    for pair in `ip addr show | grep "inet\>" |awk '{ print $2 }'`; do
+        a=`echo $pair | awk -F/ '{ print $1}'`
+        m=`echo $pair | awk -F/ '{ print $2}'`
+        res=`includes_virbr0addr $a $m`
+        if [ $res = "true" ]; then
+            found=1
+        fi
+    done
+    if [ $found -eq 1 ]; then
+        return
+    fi
+    set_autostart
+}
+
 
 add_statoverrides()
 {
@@ -134,6 +186,24 @@ case "$1" in
 
         # Force refresh of capabilties (#731815)
         rm -f /var/cache/libvirt/qemu/capabilities/*.xml
+
+        # 1. On an initial package install, create the default network autostart
+        #    symlink.
+        # 2. If the default.xml existed before upgrade, make sure it is
+        #    recreated.
+        #    This won't be a problem on most upgrades, but when upgrading from a
+        #    version where the symlink came with the package, it will.
+        # 3. If upgrading from one of the bad libvirt versions which deleted the
+        #    symlink wrongly, recreate it
+        EXISTED="/etc/libvirt/qemu/networks/autostart/TMP_defaultexisted"
+        if [ -z $2 ]; then
+            maybe_set_autostart
+        elif [ -e "$EXISTED" ]; then
+            # on upgrade, if default network was previously autostarted,
+            # continue to do so.
+            rm -f "$EXISTED"
+            set_autostart
+        fi
     ;;
 
     abort-upgrade|abort-remove|abort-deconfigure)
diff --git a/debian/libvirt-daemon-system.preinst b/debian/libvirt-daemon-system.preinst
index 8a06ff5..9752d7e 100644
--- a/debian/libvirt-daemon-system.preinst
+++ b/debian/libvirt-daemon-system.preinst
@@ -26,6 +26,12 @@ case "$1" in
 	    # Remove everything we know about libvirt-bin.service
 	    deb-systemd-helper purge libvirt-bin.service >/dev/null
 	    deb-systemd-helper unmask libvirt-bin.service >/dev/null
+        # If the default network autostart symlink existed, then note
+        # that here so we can recreate it at postinst.
+        EXISTED="/etc/libvirt/qemu/networks/autostart/TMP_defaultexisted"
+        if [ -e /etc/libvirt/qemu/networks/autostart/default.xml ]; then
+            touch "$EXISTED"
+        fi
 	fi
     ;;
 
-- 
2.7.4

Reply via email to