From patchwork Fri Feb 26 01:18:42 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: sshang X-Patchwork-Id: 82237 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter.kernel.org (8.14.3/8.14.3) with ESMTP id o1Q1Dn3O014364 for ; Fri, 26 Feb 2010 01:13:49 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934848Ab0BZBNr (ORCPT ); Thu, 25 Feb 2010 20:13:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:49932 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934840Ab0BZBNq (ORCPT ); Thu, 25 Feb 2010 20:13:46 -0500 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.13.8/8.13.8) with ESMTP id o1Q1Dj3x028878 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Thu, 25 Feb 2010 20:13:46 -0500 Received: from localhost.localdomain ([10.66.91.194]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o1Q1DhTW019270; Thu, 25 Feb 2010 20:13:44 -0500 From: sshang To: lmr@redhat.com Cc: autotest@test.kernel.com, kvm@vger.kernel.org, sshang Subject: [PATCH] KVM-Test: Add private bridge to use for communication Date: Fri, 26 Feb 2010 09:18:42 +0800 Message-Id: <1267147122-733-1-git-send-email-sshang@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.3 (demeter.kernel.org [140.211.167.41]); Fri, 26 Feb 2010 01:13:49 +0000 (UTC) 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()