于 2011年11月18日 21:34, Lucas Meneghel Rodrigues 写道: > On 11/18/2011 05:04 AM, Xu He Jie wrote: >> the different with last version: >> * Move url argument to last position for virsh_*. >> * Add driver_type 'default', and 'default' as default value for >> driver_type. >> * When VM.remove() failed, it will raise an exception. And add new >> exception 'VMRemoveError'. >> * Other small fix. >> >> When I run libvirt test, the driver of libvirt will decide >> by virsh. >> >> So I add option of 'driver_type' that can specify which driver >> libvirt will connect to. >> >> but now just can specify two type of driver 'qemu and xen', >> if specify other driver will use default driver that >> detected by virsh. > > Looks good, you may prepare v3
Thanks for your review! But I found VM.create() will use virt-install to create VM, so I guess we need pass url to virt-install too. And there is an option 'use_connect_uri = yes' in tests_base.cfg.sample, but I didn't find somewhere using that. So I just pass uri to virt-install will be OK? > >> Signed-off-by: Xu He Jie<[email protected]> >> --- >> client/tests/libvirt/tests_base.cfg.sample | 3 + >> client/virt/libvirt_vm.py | 160 +++++++++++++++++----------- >> client/virt/virt_env_process.py | 8 +- >> client/virt/virt_vm.py | 2 + >> 4 files changed, 106 insertions(+), 67 deletions(-) >> >> diff --git a/client/tests/libvirt/tests_base.cfg.sample >> b/client/tests/libvirt/tests_base.cfg.sample >> index 409fe80..5096bdd 100644 >> --- a/client/tests/libvirt/tests_base.cfg.sample >> +++ b/client/tests/libvirt/tests_base.cfg.sample >> @@ -80,6 +80,9 @@ image_raw_device = no >> # NFS directory of guests' images >> images_good = 0.0.0.0:/autotest/images_good >> >> +# libvirt >> +driver_type = default >> + >> # libvirt (virt-install optional arguments) >> use_connect_uri = yes >> use_autostart = no >> diff --git a/client/virt/libvirt_vm.py b/client/virt/libvirt_vm.py >> index 0c32551..41b0e14 100644 >> --- a/client/virt/libvirt_vm.py >> +++ b/client/virt/libvirt_vm.py >> @@ -30,88 +30,94 @@ def libvirtd_restart(): >> return False >> >> >> -def virsh_cmd(cmd): >> +def virsh_cmd(cmd, url = ""): >> if VIRSH_EXEC is None: >> raise ValueError('Missing command: virsh') >> - cmd_result = utils.run("%s %s" % (VIRSH_EXEC, cmd), >> ignore_status=True, >> + >> + url_arg = "" >> + if url: >> + url_arg = "-c " + url >> + >> + cmd_result = utils.run("%s %s %s" % (VIRSH_EXEC, url_arg, cmd), >> ignore_status=True, >> verbose=DEBUG) >> if DEBUG: >> if cmd_result.stdout.strip(): >> logging.debug("stdout: %s", cmd_result.stdout.strip()) >> if cmd_result.stderr.strip(): >> logging.debug("stderr: %s", cmd_result.stderr.strip()) >> + >> return cmd_result.stdout.strip() >> >> >> -def virsh_uri(): >> +def virsh_uri(url = ""): >> """ >> Return the hypervisor canonical URI. >> """ >> - return virsh_cmd("uri") >> + return virsh_cmd("uri", url) >> >> >> -def virsh_hostname(): >> +def virsh_hostname(url = ""): >> """ >> Return the hypervisor hostname. >> """ >> - return virsh_cmd("hostname") >> + return virsh_cmd("hostname", url) >> >> >> -def virsh_domstate(name): >> +def virsh_domstate(name, url = ""): >> """ >> Return the state about a running domain. >> >> @param name: VM name >> """ >> - return virsh_cmd("domstate %s" % name) >> + return virsh_cmd("domstate %s" % name, url) >> >> >> -def virsh_uuid(name): >> +def virsh_uuid(name, url = ""): >> """ >> Return the Converted domain name or id to the domain UUID. >> >> @param name: VM name >> """ >> - return virsh_cmd("domuuid %s" % name) >> + return virsh_cmd("domuuid %s" % name, url) >> >> >> -def virsh_screenshot(name, filename): >> - virsh_cmd("screenshot %s %s" % (name, filename)) >> +def virsh_screenshot(name, filename, url = ""): >> + virsh_cmd("screenshot %s %s" % (name, filename), url) >> return filename >> >> >> -def virsh_dumpxml(name): >> +def virsh_dumpxml(name, url = ""): >> """ >> Return the domain information as an XML dump. >> >> @param name: VM name >> """ >> - return virsh_cmd("dumpxml %s" % name) >> + return virsh_cmd("dumpxml %s" % name, url) >> >> >> -def virsh_is_alive(name): >> +def virsh_is_alive(name, url = ""): >> """ >> Return True if the domain is started/alive. >> >> @param name: VM name >> """ >> - return not virsh_is_dead(name) >> + return not virsh_is_dead(name, url) >> >> >> -def virsh_is_dead(name): >> +def virsh_is_dead(name, url = ""): >> """ >> Return True if the domain is not started/dead. >> >> @param name: VM name >> """ >> - state = virsh_domstate(name) >> + state = virsh_domstate(name, url) >> if state in ('running', 'idle', 'no state'): >> return False >> else: >> return True >> >> >> -def virsh_suspend(name): >> +def virsh_suspend(name, url = ""): >> """ >> Return True on successful domain suspention of VM. >> >> @@ -120,7 +126,7 @@ def virsh_suspend(name): >> @param name: VM name >> """ >> try: >> - utils.run("virsh suspend %s" % (name)) >> + virsh_cmd("suspend %s" % (name), url) >> if virsh_domstate(name) == 'paused': >> logging.debug("Suspended VM %s", name) >> return True >> @@ -131,7 +137,7 @@ def virsh_suspend(name): >> return False >> >> >> -def virsh_resume(name): >> +def virsh_resume(name, url = ""): >> """ >> Return True on successful domain resumption of VM. >> >> @@ -140,8 +146,8 @@ def virsh_resume(name): >> @param name: VM name >> """ >> try: >> - utils.run("virsh resume %s" % (name)) >> - if virsh_is_alive(name): >> + virsh_cmd("resume %s" % (name), url) >> + if virsh_is_alive(name, url): >> logging.debug("Resumed VM %s", name) >> return True >> else: >> @@ -151,7 +157,7 @@ def virsh_resume(name): >> return False >> >> >> -def virsh_start(name, vm): >> +def virsh_start(name, url = ""): >> """ >> Return True on successful domain start. >> >> @@ -159,17 +165,17 @@ def virsh_start(name, vm): >> >> @param name: VM name >> """ >> - if virsh_is_alive(name): >> + if virsh_is_alive(name, url): >> return >> try: >> - utils.run("virsh start %s" % (name)) >> + virsh_cmd("start %s" % (name), url) >> return True >> except error.CmdError: >> logging.error("Start VM %s failed", name) >> return False >> >> >> -def virsh_shutdown(name): >> +def virsh_shutdown(name, url = ""): >> """ >> Return True on successful domain shutdown. >> >> @@ -177,17 +183,17 @@ def virsh_shutdown(name): >> >> @param name: VM name >> """ >> - if virsh_domstate(name) == 'shut off': >> + if virsh_domstate(name, url) == 'shut off': >> return True >> try: >> - utils.run("virsh shutdown %s" % (name)) >> + virsh_cmd("virsh shutdown %s" % (name), url) >> return True >> except error.CmdError: >> logging.error("Shutdown VM %s failed", name) >> return False >> >> >> -def virsh_destroy(name): >> +def virsh_destroy(name, url = ""): >> """ >> Return True on successful domain destroy. >> >> @@ -196,17 +202,17 @@ def virsh_destroy(name): >> >> @param name: VM name >> """ >> - if virsh_domstate(name) == 'shut off': >> + if virsh_domstate(name, url) == 'shut off': >> return True >> try: >> - utils.run("virsh destroy %s" % (name)) >> + virsh_cmd("virsh destroy %s" % (name), url) >> return True >> except error.CmdError: >> logging.error("Destroy VM %s failed", name) >> return False >> >> >> -def virsh_undefine(name): >> +def virsh_undefine(name, url = ""): >> """ >> Return True on successful domain undefine. >> >> @@ -216,7 +222,7 @@ def virsh_undefine(name): >> @param name: VM name >> """ >> try: >> - utils.run("virsh undefine %s" % (name)) >> + virsh_cmd("undefine %s" % (name), url) >> logging.debug("undefined VM %s", name) >> return True >> except error.CmdError: >> @@ -224,46 +230,45 @@ def virsh_undefine(name): >> return False >> >> >> -def virsh_remove_domain(name): >> +def virsh_remove_domain(name, url = ""): >> """ >> Return True after forcefully removing a domain if it exists. >> >> @param name: VM name >> """ >> - if virsh_domain_exists(name): >> - if virsh_is_alive(name): >> - virsh_destroy(name) >> - virsh_undefine(name) >> + if virsh_domain_exists(name, url): >> + if virsh_is_alive(name, url): >> + virsh_destroy(name, url) >> + virsh_undefine(name, url) >> return True >> >> >> -def virsh_domain_exists(name): >> +def virsh_domain_exists(name, url = ""): >> """ >> Return True if a domain exits. >> >> @param name: VM name >> """ >> try: >> - utils.run("virsh domstate %s" % name) >> + virsh_cmd("domstate %s" % name, url) >> return True >> except error.CmdError: >> logging.warning("VM %s does not exist", name) >> return False >> >> -VIRSH_DEFAULT_URI = virsh_uri() >> -LIBVIRT_QEMU = False >> -LIBVIRT_XEN = False >> - >> -if VIRSH_DEFAULT_URI == 'qemu:///system': >> - LIBVIRT_QEMU = True >> -elif VIRSH_DEFAULT_URI == 'xen:///': >> - LIBVIRT_XEN = True >> - >> >> class VM(virt_vm.BaseVM): >> """ >> This class handles all basic VM operations for libvirt. >> """ >> + >> + >> + # constant for libirt driver type, >> + # now it only supports default, qemu and xen. >> + LIBVIRT_DEFAULT = "default" >> + LIBVIRT_QEMU = "qemu" >> + LIBVIRT_XEN = "xen" >> + >> def __init__(self, name, params, root_dir, address_cache, state=None): >> """ >> Initialize the object and set a few attributes. >> @@ -297,7 +302,27 @@ class VM(virt_vm.BaseVM): >> self.address_cache = address_cache >> # For now, libvirt does not have a monitor property. >> self.monitor = None >> + self.driver_type = params.get("driver_type", self.LIBVIRT_DEFAULT) >> + >> + default_url = virsh_uri() >> + if not self.driver_type: >> + if default_url == "qemu:///system": >> + self.driver_type = self.LIBVIRT_QEMU >> + elif default_url == "xen:///": >> + self.driver_type = self.LIBVIRT_XEN >> + else: >> + self.driver_type = self.LIBVIRT_DEFAULT >> + >> + # if driver_type is not supported, just use the default url that >> + # was detected by virsh >> + if self.driver_type == self.LIBVIRT_QEMU: >> + self.connect_url = "qemu:///system" >> + elif self.driver_type == self.LIBVIRT_XEN: >> + self.connect_url = "xen:///" >> + else: >> + self.connect_url = default_url >> >> + logging.info("VM '%s' with url '%s'" % (name, self.connect_url)) >> >> def verify_alive(self): >> """ >> @@ -307,21 +332,21 @@ class VM(virt_vm.BaseVM): >> """ >> if not self.is_alive(): >> raise virt_vm.VMDeadError("Domain %s is inactive" % self.name, >> - virsh_domstate(self.name)) >> + virsh_domstate(self.name, self.connect_url)) >> >> >> def is_alive(self): >> """ >> Return True if VM is alive. >> """ >> - return virsh_is_alive(self.name) >> + return virsh_is_alive(self.name, self.connect_url) >> >> >> def is_dead(self): >> """ >> Return True if VM is dead. >> """ >> - return virsh_is_dead(self.name) >> + return virsh_is_dead(self.name, self.connect_url) >> >> >> def clone(self, name=None, params=None, root_dir=None, >> address_cache=None, >> @@ -647,7 +672,7 @@ class VM(virt_vm.BaseVM): >> image_params.get("drive_cache"), >> image_params.get("image_format")) >> >> - if LIBVIRT_QEMU: >> + if self.driver_type == self.LIBVIRT_QEMU: >> for cdrom in params.objects("cdroms"): >> cdrom_params = params.object_params(cdrom) >> iso = cdrom_params.get("cdrom") >> @@ -699,9 +724,9 @@ class VM(virt_vm.BaseVM): >> virt_install_cmd += " --mac %s" % mac >> self.nic_mac = mac >> >> - if LIBVIRT_XEN: >> + if self.driver_type == self.LIBVIRT_XEN: >> virt_install_cmd += (" --network=%s" % params.get("virsh_network")) >> - elif LIBVIRT_QEMU: >> + elif self.driver_type == self.LIBVIRT_QEMU: >> virt_install_cmd += (" --network=%s,model=%s" % >> (params.get("virsh_network"), >> params.get("nic_model"))) >> @@ -918,7 +943,7 @@ class VM(virt_vm.BaseVM): >> finally: >> session.close() >> >> - virsh_destroy(self.name) >> + virsh_destroy(self.name, self.connect_url) >> >> finally: >> if self.serial_console: >> @@ -939,6 +964,15 @@ class VM(virt_vm.BaseVM): >> for vlan in range(num_nics): >> self.free_mac_address(vlan) >> >> + def remove(self): >> + if self.is_alive(): >> + if not virsh_destroy(self.name, self.connect_url): >> + raise virt_vm.VMRemoveError("VM can not be destroy") >> + >> + if not virsh_undefine(self.name, self.connect_url): >> + raise virt_vm.VMRemoveError("VM removed fault") >> + >> + logging.debug("VM '%s' is removed", self.name) >> >> def get_address(self, index=0): >> """ >> @@ -1018,7 +1052,7 @@ class VM(virt_vm.BaseVM): >> @raise VMMACAddressMissingError: If no MAC address is defined for the >> requested NIC >> """ >> - thexml = virsh_dumpxml(self.name) >> + thexml = virsh_dumpxml(self.name, self.connect_url) >> dom = minidom.parseString(thexml) >> count = 0 >> for node in dom.getElementsByTagName('interface'): >> @@ -1151,7 +1185,7 @@ class VM(virt_vm.BaseVM): >> def screendump(self, filename, debug=False): >> if debug: >> logging.debug("Requesting screenshot %s" % filename) >> - return virsh_screenshot(self.name, filename) >> + return virsh_screenshot(self.name, filename, self.connect_url) >> >> >> def wait_for_start(self, count=60): >> @@ -1168,7 +1202,7 @@ class VM(virt_vm.BaseVM): >> while count> 0: >> # check every 5 seconds >> if count % 5 == 0: >> - if virsh_is_alive(self.name): >> + if virsh_is_alive(self.name, self.connect_url): >> session = self.wait_for_login(timeout=60) >> session.close() >> logging.debug("Start took %d seconds", timeout - count) >> @@ -1183,7 +1217,7 @@ class VM(virt_vm.BaseVM): >> """ >> Starts this VM. >> """ >> - if virsh_start(self.name): >> + if virsh_start(self.name, self.connect_url): >> if self.wait_for_start(): >> logging.debug("Started VM %s", self.name) >> return True >> @@ -1209,7 +1243,7 @@ class VM(virt_vm.BaseVM): >> while count> 0: >> # check every 5 seconds >> if count % 5 == 0: >> - if virsh_is_dead(self.name): >> + if virsh_is_dead(self.name, self.connect_url): >> logging.debug("Shutdown took %d seconds", timeout - count) >> return True >> count -= 1 >> @@ -1222,7 +1256,7 @@ class VM(virt_vm.BaseVM): >> """ >> Shuts down this VM. >> """ >> - if virsh_shutdown(self.name): >> + if virsh_shutdown(self.name, self.connect_url): >> if self.wait_for_shutdown(): >> logging.debug("VM %s shut down", self.name) >> return True >> diff --git a/client/virt/virt_env_process.py >> b/client/virt/virt_env_process.py >> index 1e7ea57..bc72796 100644 >> --- a/client/virt/virt_env_process.py >> +++ b/client/virt/virt_env_process.py >> @@ -65,8 +65,8 @@ def preprocess_vm(test, params, env, name): >> logging.debug("'force_remove_vm' specified; removing VM...") >> remove_vm = True >> >> - if remove_vm and not libvirt_vm.virsh_remove_domain(name): >> - raise error.TestError("Could not remove VM") >> + if remove_vm: >> + vm.remove() >> >> start_vm = False >> >> @@ -80,7 +80,7 @@ def preprocess_vm(test, params, env, name): >> elif params.get("start_vm") == "yes": >> # need to deal with libvirt VM differently than qemu >> if vm_type == 'libvirt': >> - if not libvirt_vm.virsh_is_alive(name): >> + if not vm.is_alive(): >> logging.debug("VM is not alive; starting it...") >> start_vm = True >> else: >> @@ -95,7 +95,7 @@ def preprocess_vm(test, params, env, name): >> if start_vm: >> if vm_type == "libvirt" and params.get("type") != "unattended_install": >> vm.params = params >> - libvirt_vm.virsh_start(name, vm) >> + vm.start() >> # Wait for the domain to be created >> virt_utils.wait_for(func=vm.is_alive, timeout=60, >> text=("waiting for domain %s to start" % >> diff --git a/client/virt/virt_vm.py b/client/virt/virt_vm.py >> index 4a20ff4..60536e5 100644 >> --- a/client/virt/virt_vm.py >> +++ b/client/virt/virt_vm.py >> @@ -204,6 +204,8 @@ class VMRebootError(VMError): >> class VMStatusError(VMError): >> pass >> >> +class VMRemoveError(VMError): >> + pass >> >> def get_image_blkdebug_filename(params, root_dir): >> """ > _______________________________________________ Autotest mailing list [email protected] http://test.kernel.org/cgi-bin/mailman/listinfo/autotest
