Skip to contentSkip to conversation

Use net-tools as a fallback for iproute2 for bridge configuration

PATCH In review Carlo Marcelo Arenas Belon 2 participants 2 messages Sep 25, 2007 KVM

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 Use net-tools as a fallback for iproute2 for bridge configuration

The following patch uses net-tools (ifconfig and netstat) if /sbin/ip is not available while trying to configure a bridge with the kvm wrapper and qemu-ifup.

It also adds an option to the kvm wrapper to define which bridge device to use instead of the hardcoded eth0 value (which is still the default)

Carlo

---
 kvm               |   33 ++++++++++++++++++++++++++++-----
 scripts/qemu-ifup |   17 +++++++++++++++--
 2 files changed, 43 insertions(+), 7 deletions(-)
kvm +28 -5
index d091c86..2f81ae4 100755
@@ -55,6 +55,12 @@ optparser.add_option('--no-tap',
                      default = not privileged,
                      )
 
+optparser.add_option('--bridge',
+                     help = 'use this device to build the bridge',
+		     dest = 'bridge',
+		     default = None,
+		     )
+
 optparser.add_option('--mac',
                      help = 'use this specific mac addr',
                      dest = 'mac',
@@ -191,6 +197,7 @@ bootdisk = 'c'
 if options.install:
     bootdisk = 'd'
 
+# kvm always compiles for the x86_64 target
 arch = 'x86_64'
 
 if arch == 'x86_64':
@@ -226,15 +233,31 @@ if options.debugger:
 if not options.irqchip:
     qemu_args += ('-no-kvm-irqchip',)
 
+def getmac(interface):
+    if os.access('/sbin/ip', os.F_OK):
+        for line in commands.getoutput('/sbin/ip link show ' + interface).splitlines():
+            m = re.match(r'.*link/ether (..:..:..:..:..:..).*', line)
+	    if m:
+	        mac = m.group(1)
+		return mac
+    else:
+        for line in commands.getoutput('/sbin/ifconfig ' + interface).splitlines():
+            m = re.match(r'.*HWaddr (..:..:..:..:..:..)', line)
+	    if m:
+	        mac = m.group(1)
+		return mac
+    return False
+
 if not options.notap:
+    bridge = options.bridge
+    if not bridge:
+        bridge = 'eth0'
+
     mac = options.mac
     if not mac:
-        for line in commands.getoutput('/sbin/ip link show eth0').splitlines():
-            m = re.match(r'.*link/ether (..:..:..:..:..:..).*', line)
-            if m:
-                mac = m.group(1)
+        mac = getmac(bridge) 
         if not mac:
-            raise Exception, 'Unable to determine eth0 mac address'
+            raise Exception, 'Unable to determine ' + bridge + ' mac address'
         mac_components = mac.split(':')
         mac_components[0] = 'a0'
         mac = ':'.join(mac_components)
scripts/qemu-ifup +15 -2
index 3bf8801..989fe9a 100755
@@ -1,5 +1,18 @@
 #!/bin/sh
 
-switch=$(/sbin/ip route list | awk '/^default / { print $NF }')
+if [ -x /sbin/brctl ]; then
+  BRCTL="/sbin/brctl"
+elif [ -x /usr/sbin/brctl ]; then
+  BRCTL="/usr/sbin/brctl"
+else
+  echo "no bridge utils installed"
+  exit 1
+fi
+
+if [ -x /sbin/ip ]; then
+  switch=$(/sbin/ip route list | awk '/^default / { print $NF }')
+else
+  switch=$(/bin/netstat -rn | awk '/^0\.0\.0\.0/ { print $NF }')
+fi
 /sbin/ifconfig $1 0.0.0.0 up
-/usr/sbin/brctl addif ${switch} $1
+${BRCTL} addif ${switch} $1
Signature · 7 lines
1.5.1.6


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
#1 lore raw
Quoted 9 lines · Carlo Marcelo Arenas Belon
scripts/qemu-ifup +1 -1
index 3bf8801..989fe9a 100755
@@ -1,5 +1,18 @@
 #!/bin/sh
 
-switch=$(/sbin/ip route list | awk '/^default / { print $NF }')
+if [ -x /sbin/brctl ]; then

scripts/qemu-ifup line 13

Quoted 10 lines
scripts/qemu-ifup +10 -0
+  BRCTL="/sbin/brctl"
+elif [ -x /usr/sbin/brctl ]; then
+  BRCTL="/usr/sbin/brctl"
+else
+  echo "no bridge utils installed"
+  exit 1
+fi
+
+if [ -x /sbin/ip ]; then
+  switch=$(/sbin/ip route list | awk '/^default / { print $NF }')

NAK on this piece. This won't work with non-trivial setups (yes, I now that the original script is broken too, but since you are touching it...).
See:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=417151

IMHO it's better to rely on distro-specific scripts to setup the networking.

Luca

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/