From patchwork Mon May 17 13:29:35 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Goldish X-Patchwork-Id: 100121 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 o4HDUwTr008505 for ; Mon, 17 May 2010 13:30:59 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755379Ab0EQNaZ (ORCPT ); Mon, 17 May 2010 09:30:25 -0400 Received: from mx1.redhat.com ([209.132.183.28]:52433 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1755346Ab0EQNaX (ORCPT ); Mon, 17 May 2010 09:30:23 -0400 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 o4HDULAD005782 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Mon, 17 May 2010 09:30:22 -0400 Received: from ns3.rdu.redhat.com (ns3.rdu.redhat.com [10.11.255.199]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id o4HDUL3k019642; Mon, 17 May 2010 09:30:21 -0400 Received: from localhost.localdomain (dhcp-1-188.tlv.redhat.com [10.35.1.188]) by ns3.rdu.redhat.com (8.13.8/8.13.8) with ESMTP id o4HDUGA4029225; Mon, 17 May 2010 09:30:20 -0400 From: Michael Goldish To: autotest@test.kernel.org, kvm@vger.kernel.org Cc: Michael Goldish Subject: [KVM-AUTOTEST PATCH] KVM test: use command line option wrapper functions Date: Mon, 17 May 2010 16:29:35 +0300 Message-Id: <1274102975-32453-3-git-send-email-mgoldish@redhat.com> In-Reply-To: <1274102975-32453-2-git-send-email-mgoldish@redhat.com> References: <1274102975-32453-1-git-send-email-mgoldish@redhat.com> <1274102975-32453-2-git-send-email-mgoldish@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]); Mon, 17 May 2010 13:30:59 +0000 (UTC) diff --git a/client/tests/kvm/kvm_vm.py b/client/tests/kvm/kvm_vm.py index 047505a..94bacdf 100755 --- a/client/tests/kvm/kvm_vm.py +++ b/client/tests/kvm/kvm_vm.py @@ -186,12 +186,100 @@ class VM: nic_model -- string to pass as 'model' parameter for this NIC (e.g. e1000) """ - if name is None: - name = self.name - if params is None: - params = self.params - if root_dir is None: - root_dir = self.root_dir + # Helper function for command line option wrappers + def has_option(help, option): + return bool(re.search(r"^-%s(\s|$)" % option, help, re.MULTILINE)) + + # Wrappers for all supported qemu command line parameters. + # This is meant to allow support for multiple qemu versions. + # Each of these functions receives the output of 'qemu -help' as a + # parameter, and should add the requested command line option + # accordingly. + + def add_name(help, name): + return " -name '%s'" % name + + def add_unix_socket_monitor(help, filename): + return " -monitor unix:%s,server,nowait" % filename + + def add_mem(help, mem): + return " -m %s" % mem + + def add_smp(help, smp): + return " -smp %s" % smp + + def add_cdrom(help, filename, index=2): + if has_option(help, "drive"): + return " -drive file=%s,index=%d,media=cdrom" % (filename, + index) + else: + return " -cdrom %s" % filename + + def add_drive(help, filename, format=None, cache=None, werror=None, + serial=None, snapshot=False, boot=False): + cmd = " -drive file=%s" % filename + if format: cmd += ",if=%s" % format + if cache: cmd += ",cache=%s" % cache + if werror: cmd += ",werror=%s" % werror + if serial: cmd += ",serial=%s" % serial + if snapshot: cmd += ",snapshot=on" + if boot: cmd += ",boot=on" + return cmd + + def add_nic(help, vlan, model=None, mac=None): + cmd = " -net nic,vlan=%d" % vlan + if model: cmd += ",model=%s" % model + if mac: cmd += ",macaddr=%s" % mac + return cmd + + def add_net(help, vlan, mode, ifname=None, script=None, + downscript=None): + cmd = " -net %s,vlan=%d" % (mode, vlan) + if mode == "tap": + if ifname: cmd += ",ifname=%s" % ifname + if script: cmd += ",script=%s" % script + cmd += ",downscript=%s" % (downscript or "no") + return cmd + + def add_floppy(help, filename): + return " -fda %s" % filename + + def add_tftp(help, filename): + return " -tftp %s" % filename + + def add_tcp_redir(help, host_port, guest_port): + return " -redir tcp:%s::%s" % (host_port, guest_port) + + def add_vnc(help, vnc_port): + return " -vnc :%d" % (vnc_port - 5900) + + def add_sdl(help): + if has_option(help, "sdl"): + return " -sdl" + else: + return "" + + def add_nographic(help): + return " -nographic" + + def add_uuid(help, uuid): + return " -uuid %s" % uuid + + def add_pcidevice(help, host): + return " -pcidevice host=%s" % host + + # End of command line option wrappers + + if name is None: name = self.name + if params is None: params = self.params + if root_dir is None: root_dir = self.root_dir + + qemu_binary = kvm_utils.get_path(root_dir, params.get("qemu_binary", + "qemu")) + # Get the output of 'qemu -help' (log a message in case this call never + # returns or causes some other kind of trouble) + logging.debug("Getting output of 'qemu -help'") + help = commands.getoutput("%s -help" % qemu_binary) # Start constructing the qemu command qemu_cmd = "" @@ -199,65 +287,49 @@ class VM: if params.get("x11_display"): qemu_cmd += "DISPLAY=%s " % params.get("x11_display") # Add the qemu binary - qemu_cmd += kvm_utils.get_path(root_dir, params.get("qemu_binary", - "qemu")) + qemu_cmd += qemu_binary # Add the VM's name - qemu_cmd += " -name '%s'" % name + qemu_cmd += add_name(help, name) # Add the monitor socket parameter - qemu_cmd += " -monitor unix:%s,server,nowait" % self.monitor_file_name + qemu_cmd += add_unix_socket_monitor(help, self.monitor_file_name) for image_name in kvm_utils.get_sub_dict_names(params, "images"): image_params = kvm_utils.get_sub_dict(params, image_name) if image_params.get("boot_drive") == "no": continue - qemu_cmd += " -drive file=%s" % get_image_filename(image_params, - root_dir) - if image_params.get("drive_format"): - qemu_cmd += ",if=%s" % image_params.get("drive_format") - if image_params.get("drive_cache"): - qemu_cmd += ",cache=%s" % image_params.get("drive_cache") - if image_params.get("drive_werror"): - qemu_cmd += ",werror=%s" % image_params.get("drive_werror") - if image_params.get("drive_serial"): - qemu_cmd += ",serial=%s" % image_params.get("drive_serial") - if image_params.get("image_snapshot") == "yes": - qemu_cmd += ",snapshot=on" - if image_params.get("image_boot") == "yes": - qemu_cmd += ",boot=on" + qemu_cmd += add_drive(help, + get_image_filename(image_params, root_dir), + image_params.get("drive_format"), + image_params.get("drive_cache"), + image_params.get("drive_werror"), + image_params.get("drive_serial"), + image_params.get("image_snapshot") == "yes", + image_params.get("image_boot") == "yes") vlan = 0 for nic_name in kvm_utils.get_sub_dict_names(params, "nics"): nic_params = kvm_utils.get_sub_dict(params, nic_name) # Handle the '-net nic' part - qemu_cmd += " -net nic,vlan=%d" % vlan - if nic_params.get("nic_model"): - qemu_cmd += ",model=%s" % nic_params.get("nic_model") - if nic_params.has_key("address_index"): - mac, ip = kvm_utils.get_mac_ip_pair_from_dict(nic_params) - if mac: - qemu_cmd += ",macaddr=%s" % mac + mac = None + if "address_index" in nic_params: + mac = kvm_utils.get_mac_ip_pair_from_dict(nic_params)[0] + qemu_cmd += add_nic(help, vlan, nic_params.get("nic_model"), mac) # Handle the '-net tap' or '-net user' part - mode = nic_params.get("nic_mode", "user") - qemu_cmd += " -net %s,vlan=%d" % (mode, vlan) - if mode == "tap": - if nic_params.get("nic_ifname"): - qemu_cmd += ",ifname=%s" % nic_params.get("nic_ifname") - script_path = nic_params.get("nic_script") - if script_path: - script_path = kvm_utils.get_path(root_dir, script_path) - qemu_cmd += ",script=%s" % script_path - script_path = nic_params.get("nic_downscript") - if script_path: - script_path = kvm_utils.get_path(root_dir, script_path) - qemu_cmd += ",downscript=%s" % script_path - else: - qemu_cmd += ",downscript=no" + script = nic_params.get("script") + downscript = nic_params.get("downscript") + if script: + script = kvm_utils.get_path(root_dir, script) + if downscript: + downscript = kvm_utils.get_path(root_dir, downscript) + qemu_cmd += add_net(help, vlan, nic_params.get("nic_mode", "user"), + nic_params.get("nic_ifname"), + script, downscript) # Proceed to next NIC vlan += 1 mem = params.get("mem") if mem: - qemu_cmd += " -m %s" % mem + qemu_cmd += add_mem(help, mem) smp = params.get("smp") if smp: @@ -266,7 +338,7 @@ class VM: iso = params.get("cdrom") if iso: iso = kvm_utils.get_path(root_dir, iso) - qemu_cmd += " -drive file=%s,index=2,media=cdrom" % iso + qemu_cmd += add_cdrom(help, iso) # Even though this is not a really scalable approach, # it doesn't seem like we are going to need more than @@ -274,47 +346,47 @@ class VM: iso_extra = params.get("cdrom_extra") if iso_extra: iso_extra = kvm_utils.get_path(root_dir, iso_extra) - qemu_cmd += " -drive file=%s,index=3,media=cdrom" % iso_extra + qemu_cmd += add_cdrom(help, iso_extra, 3) # We may want to add {floppy_otps} parameter for -fda - # {fat:floppy:}/path/. However vvfat is not usually recommended + # {fat:floppy:}/path/. However vvfat is not usually recommended. floppy = params.get("floppy") if floppy: floppy = kvm_utils.get_path(root_dir, floppy) - qemu_cmd += " -fda %s" % floppy + qemu_cmd += add_floppy(help, floppy) tftp = params.get("tftp") if tftp: tftp = kvm_utils.get_path(root_dir, tftp) - qemu_cmd += " -tftp %s" % tftp - - extra_params = params.get("extra_params") - if extra_params: - qemu_cmd += " %s" % extra_params + qemu_cmd += add_tftp(help, tftp) for redir_name in kvm_utils.get_sub_dict_names(params, "redirs"): redir_params = kvm_utils.get_sub_dict(params, redir_name) guest_port = int(redir_params.get("guest_port")) host_port = self.redirs.get(guest_port) - qemu_cmd += " -redir tcp:%s::%s" % (host_port, guest_port) + qemu_cmd += add_tcp_redir(help, host_port, guest_port) if params.get("display") == "vnc": - qemu_cmd += " -vnc :%d" % (self.vnc_port - 5900) + qemu_cmd += add_vnc(help, self.vnc_port) elif params.get("display") == "sdl": - qemu_cmd += " -sdl" + qemu_cmd += add_sdl(help) elif params.get("display") == "nographic": - qemu_cmd += " -nographic" + qemu_cmd += add_nographic(help) if params.get("uuid") == "random": - qemu_cmd += " -uuid %s" % self.uuid + qemu_cmd += add_uuid(help, self.uuid) elif params.get("uuid"): - qemu_cmd += " -uuid %s" % params.get("uuid") + qemu_cmd += add_uuid(help, params.get("uuid")) # If the PCI assignment step went OK, add each one of the PCI assigned # devices to the qemu command line. if self.pci_assignable: for pci_id in self.pa_pci_ids: - qemu_cmd += " -pcidevice host=%s" % pci_id + qemu_cmd += add_pcidevice(help, pci_id) + + extra_params = params.get("extra_params") + if extra_params: + qemu_cmd += " %s" % extra_params return qemu_cmd