diff mbox

KVM-Test: Add private bridge to use for communication

Message ID 1266915370-10173-1-git-send-email-sshang@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

sshang Feb. 23, 2010, 8:56 a.m. UTC
None
diff mbox

Patch

diff --git a/client/tests/kvm/scripts/private_bridge_setup.py b/client/tests/kvm/scripts/private_bridge_setup.py
new file mode 100755
index 0000000..fe1b653
--- /dev/null
+++ b/client/tests/kvm/scripts/private_bridge_setup.py
@@ -0,0 +1,90 @@ 
+#!/usr/bin/python
+import os, sys, commands
+
+class SetupError(Exception):
+    """
+    Simple wrapper for the builtin Exception class.
+    """
+    pass
+
+
+class SetupPrivateBridge(object):
+    """
+    Setup private bridge and running dnsmasq for communication
+    between host and guest.
+    """
+    def __init__(self):
+        script_dir = os.path.dirname(sys.modules[__name__].__file__)
+        kvm_test_dir = os.path.abspath(os.path.join(script_dir, ".."))
+        self.private_bridge_name = os.environ.get(\
+                          'KVM_TEST_private_bridge_name','private')
+        self.private_bridge_addr = os.environ.get(\
+                          'KVM_TEST_private_bridge_addr','192.168.0.1')
+        self.private_bridge_mask = os.environ.get(\
+                          'KVM_TEST_private_bridge_mask','255.255.255.0')
+        self.dhcp_boot = os.environ.get(\
+                          'KVM_TEST_dhcp_boot','/pxelinux.0')
+        self.tftp_root = os.environ.get('KVM_TEST_tftp')
+        if self.tftp_root:
+            self.tftp_root = os.path.join(kvm_test_dir, self.tftp_root)
+        self.qemu_ifup_file = os.path.join(script_dir, "qemu-ifup-private")
+
+
+    def setup_private_bridge(self):
+        """
+        Setup private bridge.
+        """
+
+        text = """#!/bin/sh
+switch=%s
+/sbin/ifconfig $1 0.0.0.0 up
+/usr/sbin/brctl addif ${switch} $1
+"""
+        text = text % (self.private_bridge_name)
+        f = open(self.qemu_ifup_file,"w")
+        f.write(text)
+        f.close()
+        commands.getoutput('chmod a+x %s' % self.qemu_ifup_file)
+
+        check_br_cmd = "brctl show | grep %s" % self.private_bridge_name
+        s,o = commands.getstatusoutput(check_br_cmd)
+        if s:
+            add_br_cmd = "brctl addbr %s" % self.private_bridge_name
+            s,o = commands.getstatusoutput(add_br_cmd)
+            if s:
+                raise SetupError('Add bridge error: %s' % o)
+        else:
+            ifdown_cmd = "ifconfig %s down" % self.private_bridge_name
+            s,o = commands.getstatusoutput(ifdown_cmd)
+            if s:
+                raise SetupError('Down bridge error: %s' % o)
+
+        ifconfig_cmd = "ifconfig %s %s netmask %s up" % \
+                  (self.private_bridge_name,self.private_bridge_addr,\
+                      self.private_bridge_mask)
+        s,o = commands.getstatusoutput(ifconfig_cmd)
+        if s:
+            raise SetupError('Config bridge error: %s' % o)
+
+        commands.getoutput("pkill dnsmasq")
+
+        addr_list = self.private_bridge_addr.split('.')
+        addr_list[3] = str(int(addr_list[3]) + 1)
+        dhcp_addr_begin = '.'.join(addr_list)
+        addr_list[3] = '254'
+        dhcp_addr_end = '.'.join(addr_list)
+
+        dnsmasq_argument = " -u root --listen-address %s --dhcp-range %s,%s" %\
+                    (self.private_bridge_addr,dhcp_addr_begin,dhcp_addr_end)
+        if self.tftp_root:
+            dnsmasq_argument += " --dhcp-boot %s --enable-tftp --tftp-root %s"\
+                           % (self.dhcp_boot,self.tftp_root)
+        dnsmasq_cmd = "dnsmasq" + dnsmasq_argument
+        s,o = commands.getstatusoutput(dnsmasq_cmd)
+        if s:
+            raise SetupError("Running dnsmasq fail %s" % o)
+        print "Private bridge dnsmasq setup finished successfuly"
+
+if __name__ == "__main__":
+    setup = SetupPrivateBridge()
+    setup.setup_private_bridge()