def preprocess_image(test, params, image_name): """ Preprocess a single QEMU image according to the instructions in params. @param test: Autotest test object. @param params: A dict containing image preprocessing parameters. @note: Currently this function just creates an image if requested. """ base_dir = data_dir.get_data_dir() if params.get("storage_type") == "iscsi": iscsidev = qemu_storage.Iscsidev(params, base_dir, image_name) params["image_name"] = iscsidev.setup() else: image_filename = storage.get_image_filename(params, base_dir) create_image = False if params.get("force_create_image") == "yes": create_image = True elif (params.get("create_image") == "yes" and not os.path.exists(image_filename)): create_image = True if create_image: image = qemu_storage.QemuImg(params, base_dir, image_name) if not image.create(params): raise error.TestError("Could not create image")
def preprocess_image(test, params, image_name): """ Preprocess a single QEMU image according to the instructions in params. :param test: Autotest test object. :param params: A dict containing image preprocessing parameters. :note: Currently this function just creates an image if requested. """ base_dir = params.get("images_base_dir", data_dir.get_data_dir()) if not storage.preprocess_image_backend(base_dir, params, image_name): logging.error("Backend can't be prepared correctly.") image_filename = storage.get_image_filename(params, base_dir) create_image = False if params.get("force_create_image") == "yes": create_image = True elif (params.get("create_image") == "yes" and not storage.file_exists(params, image_filename)): create_image = True if params.get("backup_image_before_testing", "no") == "yes": image = qemu_storage.QemuImg(params, base_dir, image_name) image.backup_image(params, base_dir, "backup", True, True) if create_image: image = qemu_storage.QemuImg(params, base_dir, image_name) image.create(params)
def print_guest_list(options): """ Helper function to pretty print the guest list. This function uses a paginator, if possible (inspired on git). @param options: OptParse object with cmdline options. @param cartesian_parser: Cartesian parser object with test options. """ cfg = os.path.join(data_dir.get_root_dir(), options.type, "cfg", "guest-os.cfg") cartesian_parser = cartesian_config.Parser() cartesian_parser.parse_file(cfg) pipe = get_paginator() index = 0 pipe.write("Searched %s for guest images\n" % os.path.join(data_dir.get_data_dir(), 'images')) pipe.write("Available guests:") pipe.write("\n\n") for params in cartesian_parser.get_dicts(): index += 1 image_name = storage.get_image_filename(params, data_dir.get_data_dir()) shortname = ".".join(params['name'].split(".")[1:]) if os.path.isfile(image_name): out = (bcolors.blue + str(index) + bcolors.end + " " + shortname + "\n") else: out = (bcolors.blue + str(index) + bcolors.end + " " + shortname + " " + bcolors.yellow + "(missing %s)" % os.path.basename(image_name) + bcolors.end + "\n") pipe.write(out)
def print_guest_list(options): """ Helper function to pretty print the guest list. This function uses a paginator, if possible (inspired on git). :param options: OptParse object with cmdline options. :param cartesian_parser: Cartesian parser object with test options. """ pipe = get_paginator() # lvsb testing has no concept of guests if options.vt_type == 'lvsb': pipe.write("No guest types available for lvsb testing") return index = 0 pipe.write("Searched %s for guest images\n" % os.path.join(data_dir.get_data_dir(), 'images')) pipe.write("Available guests:") pipe.write("\n\n") for params in get_guest_name_parser(options).get_dicts(): index += 1 base_dir = params.get("images_base_dir", data_dir.get_data_dir()) image_name = storage.get_image_filename(params, base_dir) name = params['name'] if os.path.isfile(image_name): out = (bcolors.blue + str(index) + bcolors.end + " " + name + "\n") else: out = (bcolors.blue + str(index) + bcolors.end + " " + name + " " + bcolors.yellow + "(missing %s)" % os.path.basename(image_name) + bcolors.end + "\n") pipe.write(out)
def preprocess_image(test, params, image_name): """ Preprocess a single QEMU image according to the instructions in params. @param test: Autotest test object. @param params: A dict containing image preprocessing parameters. @note: Currently this function just creates an image if requested. """ if params.get("storage_type") == "iscsi": iscsidev = kvm_storage.Iscsidev(params, test.bindir, image_name) params["image_name"] = iscsidev.setup() else: image_filename = storage.get_image_filename(params, test.bindir) create_image = False if params.get("force_create_image") == "yes": create_image = True elif (params.get("create_image") == "yes" and not os.path.exists(image_filename)): create_image = True if create_image: image = kvm_storage.QemuImg(params, test.bindir, image_name) if not image.create(params): raise error.TestError("Could not create image")
def preprocess_image(test, params, image_name, vm_process_status=None): """ Preprocess a single QEMU image according to the instructions in params. :param test: Autotest test object. :param params: A dict containing image preprocessing parameters. :param vm_process_status: This is needed in postprocess_image. Add it here only for keep it work with process_images() :note: Currently this function just creates an image if requested. """ base_dir = params.get("images_base_dir", data_dir.get_data_dir()) if not storage.preprocess_image_backend(base_dir, params, image_name): logging.error("Backend can't be prepared correctly.") image_filename = storage.get_image_filename(params, base_dir) create_image = False if params.get("force_create_image") == "yes": create_image = True elif (params.get("create_image") == "yes" and not storage.file_exists(params, image_filename)): create_image = True if params.get("backup_image_before_testing", "no") == "yes": image = qemu_storage.QemuImg(params, base_dir, image_name) image.backup_image(params, base_dir, "backup", True, True) if create_image: image = qemu_storage.QemuImg(params, base_dir, image_name) image.create(params)
def print_guest_list(options): """ Helper function to pretty print the guest list. This function uses a paginator, if possible (inspired on git). @param options: OptParse object with cmdline options. @param cartesian_parser: Cartesian parser object with test options. """ pipe = get_paginator() index = 0 pipe.write("Searched %s for guest images\n" % os.path.join(data_dir.get_data_dir(), 'images')) pipe.write("Available guests:") pipe.write("\n\n") for params in get_guest_name_parser(options).get_dicts(): index += 1 image_name = storage.get_image_filename(params, data_dir.get_data_dir()) shortname = params['shortname'] if os.path.isfile(image_name): out = (bcolors.blue + str(index) + bcolors.end + " " + shortname + "\n") else: out = (bcolors.blue + str(index) + bcolors.end + " " + shortname + " " + bcolors.yellow + "(missing %s)" % os.path.basename(image_name) + bcolors.end + "\n") pipe.write(out)
def preprocess_image(test, params, image_name): """ Preprocess a single QEMU image according to the instructions in params. :param test: Autotest test object. :param params: A dict containing image preprocessing parameters. :note: Currently this function just creates an image if requested. """ base_dir = params.get("images_base_dir", data_dir.get_data_dir()) image_filename = storage.get_image_filename(params, base_dir) create_image = False if params.get("force_create_image") == "yes": create_image = True elif (params.get("create_image") == "yes" and not os.path.exists(image_filename)): create_image = True if params.get("backup_image_before_testing", "no") == "yes": image = qemu_storage.QemuImg(params, base_dir, image_name) image.backup_image(params, base_dir, "backup", True, True) if create_image: image = qemu_storage.QemuImg(params, base_dir, image_name) image.create(params)
def convert(self, params, root_dir, cache_mode=None): """ Convert image :param params: dictionary containing the test parameters :param root_dir: dir for save the convert image :param cache_mode: The cache mode used to write the output disk image. Valid options are: ``none``, ``writeback`` (default), ``writethrough``, ``directsync`` and ``unsafe``. :note: params should contain: convert_image_tag the image name of the convert image convert_filename the name of the image after convert convert_fmt the format after convert compressed indicates that target image must be compressed encrypted there are two value "off" and "on", default value is "off" """ convert_image_tag = params["image_convert"] convert_image = params["convert_name_%s" % convert_image_tag] convert_compressed = params.get("convert_compressed") convert_encrypted = params.get("convert_encrypted", "off") convert_format = params["convert_format_%s" % convert_image_tag] params_convert = { "image_name": convert_image, "image_format": convert_format } convert_image_filename = storage.get_image_filename( params_convert, root_dir) cmd = self.image_cmd cmd += " convert" if convert_compressed == "yes": cmd += " -c" if convert_encrypted != "off": cmd += " -o encryption=%s" % convert_encrypted if self.image_format: cmd += " -f %s" % self.image_format cmd += " -O %s" % convert_format if cache_mode: cmd += " -t %s" % cache_mode cmd += " %s %s" % (self.image_filename, convert_image_filename) logging.info("Convert image %s from %s to %s", self.image_filename, self.image_format, convert_format) utils.system(cmd) return convert_image_tag
def convert(self, params, root_dir, cache_mode=None): """ Convert image :param params: dictionary containing the test parameters :param root_dir: dir for save the convert image :param cache_mode: The cache mode used to write the output disk image. Valid options are: ``none``, ``writeback`` (default), ``writethrough``, ``directsync`` and ``unsafe``. :note: params should contain: convert_image_tag the image name of the convert image convert_filename the name of the image after convert convert_fmt the format after convert compressed indicates that target image must be compressed encrypted there are two value "off" and "on", default value is "off" """ convert_image_tag = params["image_convert"] convert_image = params["convert_name_%s" % convert_image_tag] convert_compressed = params.get("convert_compressed") convert_encrypted = params.get("convert_encrypted", "off") convert_format = params["convert_format_%s" % convert_image_tag] params_convert = {"image_name": convert_image, "image_format": convert_format} convert_image_filename = storage.get_image_filename(params_convert, root_dir) cmd = self.image_cmd cmd += " convert" if convert_compressed == "yes": cmd += " -c" if convert_encrypted != "off": cmd += " -o encryption=%s" % convert_encrypted if self.image_format: cmd += " -f %s" % self.image_format cmd += " -O %s" % convert_format if cache_mode: cmd += " -t %s" % cache_mode cmd += " %s %s" % (self.image_filename, convert_image_filename) logging.info("Convert image %s from %s to %s", self.image_filename, self.image_format, convert_format) utils.system(cmd) return convert_image_tag
def check_disk_params(self, params, root_dir=''): """ Check gathered info from qtree/block with params @param params: autotest params @param root_dir: root_dir of images. If all images use absolute path it's safe to omit this param. @return: number of errors """ err = 0 disks = {} for disk in self.disks: if isinstance(disk, QtreeDisk): disks[disk.get_qname()] = disk.get_params().copy() # We don't have the params name so we need to map file_names instead qname = None for name in params.objects('images'): current = None image_params = params.object_params(name) image_name = os.path.realpath( storage.get_image_filename(image_params, root_dir)) for (qname, disk) in disks.iteritems(): if disk.get('image_name') == image_name: current = disk # autotest params might use relative path current['image_name'] = image_params.get('image_name') break if not current: logging.error("Disk %s is not in qtree but is in params.", name) err += 1 continue for prop in current.iterkeys(): handled = False if prop == "drive_format": # HOOK: params to qemu translation if current.get(prop).startswith(image_params.get(prop)): handled = True elif (image_params.get(prop) and image_params.get(prop) == current.get(prop)): handled = True if not handled: logging.error( "Disk %s property %s=%s doesn't match params" " %s", qname, prop, current.get(prop), image_params.get(prop)) err += 1 disks.pop(qname) if disks: logging.error( 'Some disks were in qtree but not in autotest params' ': %s', disks) err += 1 return err
def check_disk_params(self, params, root_dir=''): """ Check gathered info from qtree/block with params @param params: autotest params @param root_dir: root_dir of images. If all images use absolute path it's safe to omit this param. @return: number of errors """ err = 0 disks = {} for disk in self.disks: if isinstance(disk, QtreeDisk): disks[disk.get_qname()] = disk.get_params().copy() # We don't have the params name so we need to map file_names instead qname = None for name in params.objects('images'): current = None image_params = params.object_params(name) image_name = os.path.realpath( storage.get_image_filename(image_params, root_dir)) for (qname, disk) in disks.iteritems(): if disk.get('image_name') == image_name: current = disk # autotest params might use relative path current['image_name'] = image_params.get('image_name') break if not current: logging.error("Disk %s is not in qtree but is in params.", name) err += 1 continue for prop in current.iterkeys(): handled = False if prop == "drive_format": # HOOK: params to qemu translation if current.get(prop).startswith(image_params.get(prop)): handled = True elif (image_params.get(prop) and image_params.get(prop) == current.get(prop)): handled = True if not handled: logging.error("Disk %s property %s=%s doesn't match params" " %s", qname, prop, current.get(prop), image_params.get(prop)) err += 1 disks.pop(qname) if disks: logging.error('Some disks were in qtree but not in autotest params' ': %s', disks) err += 1 return err
def convert(self, params, root_dir): """ Convert image @param params: dictionary containing the test parameters @param root_dir: dir for save the convert image @note: params should contain: convert_image_tag -- the image name of the convert image convert_filename -- the name of the image after convert convert_fmt -- the format after convert compressed -- indicates that target image must be compressed encrypted -- there are two value "off" and "on", default value is "off" """ convert_image_tag = params.get("image_convert") convert_image = params.get("image_name_%s" % convert_image_tag) convert_compressed = params.get("convert_compressed") convert_encrypted = params.get("convert_encrypted", "off") convert_format = params.get("image_format_%s" % convert_image_tag) params_convert = { "image_name": convert_image, "image_format": convert_format } convert_image_filename = storage.get_image_filename( params_convert, root_dir) cmd = self.image_cmd cmd += " convert" if convert_compressed == "yes": cmd += " -c" if convert_encrypted != "off": cmd += " -o encryption=%s" % convert_encrypted if self.image_format: cmd += " -f %s" % self.image_format cmd += " -O %s" % convert_format cmd += " %s %s" % (self.image_filename, convert_image_filename) logging.info("Convert image %s from %s to %s", self.image_filename, self.image_format, convert_format) utils.system(cmd) return convert_image_tag
def convert(self, params, root_dir): """ Convert image @param params: dictionary containing the test parameters @param root_dir: dir for save the convert image @note: params should contain: convert_image_tag -- the image name of the convert image convert_filename -- the name of the image after convert convert_fmt -- the format after convert compressed -- indicates that target image must be compressed encrypted -- there are two value "off" and "on", default value is "off" """ convert_image_tag = params.get("image_convert") convert_image = params.get("image_name_%s" % convert_image_tag) convert_compressed = params.get("convert_compressed") convert_encrypted = params.get("convert_encrypted", "off") convert_format = params.get("image_format_%s" % convert_image_tag) params_convert = {"image_name": convert_image, "image_format": convert_format} convert_image_filename = storage.get_image_filename(params_convert, root_dir) cmd = self.image_cmd cmd += " convert" if convert_compressed == "yes": cmd += " -c" if convert_encrypted != "off": cmd += " -o encryption=%s" % convert_encrypted if self.image_format: cmd += " -f %s" % self.image_format cmd += " -O %s" % convert_format cmd += " %s %s" % (self.image_filename, convert_image_filename) logging.info("Convert image %s from %s to %s", self.image_filename, self.image_format,convert_format) utils.system(cmd) return convert_image_tag
def check_disk_params(self, params): """ Check gathered info from qtree/block with params @param params: autotest params @return: number of errors """ err = 0 disks = {} for disk in self.disks: if isinstance(disk, QtreeDisk): disks[disk.get_qname()] = disk.get_params().copy() # We don't have the params name so we need to map file_names instead qname = None for name in params.objects('cdroms'): image_name = utils_misc.get_path( data_dir.get_data_dir(), params.object_params(name).get('cdrom', '')) image_name = os.path.realpath(image_name) for (qname, disk) in disks.iteritems(): if disk.get('image_name') == image_name: break else: continue # Not /proc/scsi cdrom device disks.pop(qname) for name in params.objects('images'): current = None image_params = params.object_params(name) image_name = os.path.realpath( storage.get_image_filename(image_params, data_dir.get_data_dir())) for (qname, disk) in disks.iteritems(): if disk.get('image_name') == image_name: current = disk # autotest params might use relative path current['image_name'] = image_params.get('image_name') break if not current: logging.error("Disk %s is not in qtree but is in params.", name) err += 1 continue for prop in current.iterkeys(): handled = False if prop == "drive_format": # HOOK: ahci disk is ide-* disk if (image_params.get(prop) == 'ahci' and current.get(prop).startswith('ide-')): handled = True # HOOK: params to qemu translation elif current.get(prop).startswith(image_params.get(prop)): handled = True elif (image_params.get(prop) and image_params.get(prop) == current.get(prop)): handled = True if not handled: logging.error( "Disk %s property %s=%s doesn't match params" " %s", qname, prop, current.get(prop), image_params.get(prop)) err += 1 disks.pop(qname) if disks: logging.error( 'Some disks were in qtree but not in autotest params' ': %s', disks) err += 1 return err
def preprocess(test, params, env): """ Preprocess all VMs and images according to the instructions in params. Also, collect some host information, such as the KVM version. :param test: An Autotest test object. :param params: A dict containing all VM and image parameters. :param env: The environment (a dict-like object). """ error.context("preprocessing") # First, let's verify if this test does require root or not. If it # does and the test suite is running as a regular user, we shall just # throw a TestNAError exception, which will skip the test. if params.get('requires_root', 'no') == 'yes': utils_misc.verify_running_as_root() port = params.get('shell_port') prompt = params.get('shell_prompt') address = params.get('ovirt_node_address') username = params.get('ovirt_node_user') password = params.get('ovirt_node_password') setup_pb = False for nic in params.get('nics', "").split(): nic_params = params.object_params(nic) if nic_params.get('netdst') == 'private': setup_pb = True params_pb = nic_params params['netdst_%s' % nic] = nic_params.get("priv_brname", 'atbr0') if setup_pb: brcfg = test_setup.PrivateBridgeConfig(params_pb) brcfg.setup() base_dir = data_dir.get_data_dir() if params.get("storage_type") == "iscsi": iscsidev = qemu_storage.Iscsidev(params, base_dir, "iscsi") params["image_name"] = iscsidev.setup() params["image_raw_device"] = "yes" if params.get("storage_type") == "lvm": lvmdev = qemu_storage.LVMdev(params, base_dir, "lvm") params["image_name"] = lvmdev.setup() params["image_raw_device"] = "yes" env.register_lvmdev("lvm_%s" % params["main_vm"], lvmdev) if params.get("storage_type") == "nfs": image_nfs = nfs.Nfs(params) image_nfs.setup() image_name_only = os.path.basename(params["image_name"]) params['image_name'] = os.path.join(image_nfs.mount_dir, image_name_only) for image_name in params.objects("images"): name_tag = "image_name_%s" % image_name if params.get(name_tag): image_name_only = os.path.basename(params[name_tag]) params[name_tag] = os.path.join(image_nfs.mount_dir, image_name_only) # Start tcpdump if it isn't already running # The fact it has to be started here is so that the test params # have to be honored. env.start_tcpdump(params) # Destroy and remove VMs that are no longer needed in the environment requested_vms = params.objects("vms") for key in env.keys(): vm = env[key] if not isinstance(vm, virt_vm.BaseVM): continue if vm.name not in requested_vms: vm.destroy() del env[key] if (params.get("auto_cpu_model") == "yes" and params.get("vm_type") == "qemu"): if not env.get("cpu_model"): env["cpu_model"] = utils_misc.get_qemu_best_cpu_model(params) params["cpu_model"] = env.get("cpu_model") kvm_ver_cmd = params.get("kvm_ver_cmd", "") if kvm_ver_cmd: try: cmd_result = utils.run(kvm_ver_cmd) kvm_version = cmd_result.stdout.strip() except error.CmdError: kvm_version = "Unknown" else: # Get the KVM kernel module version and write it as a keyval if os.path.exists("/dev/kvm"): try: kvm_version = open("/sys/module/kvm/version").read().strip() except Exception: kvm_version = os.uname()[2] else: logging.warning("KVM module not loaded") kvm_version = "Unknown" logging.debug("KVM version: %s" % kvm_version) test.write_test_keyval({"kvm_version": kvm_version}) # Get the KVM userspace version and write it as a keyval kvm_userspace_ver_cmd = params.get("kvm_userspace_ver_cmd", "") if kvm_userspace_ver_cmd: try: cmd_result = utils.run(kvm_userspace_ver_cmd) kvm_userspace_version = cmd_result.stdout.strip() except error.CmdError: kvm_userspace_version = "Unknown" else: qemu_path = utils_misc.get_qemu_binary(params) version_line = commands.getoutput("%s -help | head -n 1" % qemu_path) matches = re.findall("[Vv]ersion .*?,", version_line) if matches: kvm_userspace_version = " ".join(matches[0].split()[1:]).strip(",") else: kvm_userspace_version = "Unknown" logging.debug("KVM userspace version: %s" % kvm_userspace_version) test.write_test_keyval({"kvm_userspace_version": kvm_userspace_version}) if params.get("setup_hugepages") == "yes": h = test_setup.HugePageConfig(params) suggest_mem = h.setup() if suggest_mem is not None: params['mem'] = suggest_mem if params.get("vm_type") == "libvirt": utils_libvirtd.libvirtd_restart() if params.get("setup_thp") == "yes": thp = test_setup.TransparentHugePageConfig(test, params) thp.setup() if params.get("setup_ksm") == "yes": ksm = test_setup.KSMConfig(params, env) ksm.setup(env) # Execute any pre_commands if params.get("pre_command"): process_command(test, params, env, params.get("pre_command"), int(params.get("pre_command_timeout", "600")), params.get("pre_command_noncritical") == "yes") # if you want set "pci=nomsi" before test, set "disable_pci_msi = yes" # and pci_msi_sensitive = "yes" if params.get("pci_msi_sensitive", "no") == "yes": disable_pci_msi = params.get("disable_pci_msi", "no") image_filename = storage.get_image_filename(params, data_dir.get_data_dir()) grub_file = params.get("grub_file", "/boot/grub2/grub.cfg") kernel_cfg_pos_reg = params.get("kernel_cfg_pos_reg", r".*vmlinuz-\d+.*") msi_keyword = params.get("msi_keyword", " pci=nomsi") disk_obj = utils_disk.GuestFSModiDisk(image_filename) kernel_config_ori = disk_obj.read_file(grub_file) kernel_config = re.findall(kernel_cfg_pos_reg, kernel_config_ori) if not kernel_config: raise error.TestError("Cannot find the kernel config, reg is %s" % kernel_cfg_pos_reg) kernel_config_line = kernel_config[0] kernel_need_modify = False if disable_pci_msi == "yes": if not re.findall(msi_keyword, kernel_config_line): kernel_config_set = kernel_config_line + msi_keyword kernel_need_modify = True else: if re.findall(msi_keyword, kernel_config_line): kernel_config_set = re.sub(msi_keyword, "", kernel_config_line) kernel_need_modify = True if kernel_need_modify: for vm in env.get_all_vms(): if vm: vm.destroy() env.unregister_vm(vm.name) disk_obj.replace_image_file_content(grub_file, kernel_config_line, kernel_config_set) logging.debug("Guest cmdline 'pci=nomsi' setting is: [ %s ]" % disable_pci_msi) kernel_extra_params = params.get("kernel_extra_params") if kernel_extra_params: image_filename = storage.get_image_filename(params, data_dir.get_data_dir()) grub_file = params.get("grub_file", "/boot/grub2/grub.cfg") kernel_cfg_pos_reg = params.get("kernel_cfg_pos_reg", r".*vmlinuz-\d+.*") disk_obj = utils_disk.GuestFSModiDisk(image_filename) kernel_config_ori = disk_obj.read_file(grub_file) kernel_config = re.findall(kernel_cfg_pos_reg, kernel_config_ori) if not kernel_config: raise error.TestError("Cannot find the kernel config, reg is %s" % kernel_cfg_pos_reg) kernel_config_line = kernel_config[0] kernel_need_modify = False if not re.findall(kernel_extra_params, kernel_config_line): kernel_config_set = kernel_config_line + kernel_extra_params kernel_need_modify = True if kernel_need_modify: for vm in env.get_all_vms(): if vm: vm.destroy() env.unregister_vm(vm.name) disk_obj.replace_image_file_content(grub_file, kernel_config_line, kernel_config_set) logging.debug("Guest cmdline extra_params setting is: [ %s ]" % kernel_extra_params) # Clone master image from vms. base_dir = data_dir.get_data_dir() if params.get("master_images_clone"): for vm_name in params.get("vms").split(): vm = env.get_vm(vm_name) if vm: vm.destroy() env.unregister_vm(vm_name) vm_params = params.object_params(vm_name) for image in vm_params.get("master_images_clone").split(): image_obj = qemu_storage.QemuImg(params, base_dir, image) image_obj.clone_image(params, vm_name, image, base_dir) # Preprocess all VMs and images if params.get("not_preprocess", "no") == "no": process(test, params, env, preprocess_image, preprocess_vm) # Start the screendump thread if params.get("take_regular_screendumps") == "yes": global _screendump_thread, _screendump_thread_termination_event _screendump_thread_termination_event = threading.Event() _screendump_thread = threading.Thread(target=_take_screendumps, name='ScreenDump', args=(test, params, env)) _screendump_thread.start() # Start the register query thread if params.get("store_vm_register") == "yes": global _vm_register_thread, _vm_register_thread_termination_event _vm_register_thread_termination_event = threading.Event() _vm_register_thread = threading.Thread(target=_store_vm_register, name='VmRegister', args=(test, params, env)) _vm_register_thread.start() return params
def make_create_command(self, name=None, params=None, root_dir=None): """ Generate a libvirt command line. All parameters are optional. If a parameter is not supplied, the corresponding value stored in the class attributes is used. @param name: The name of the object @param params: A dict containing VM params @param root_dir: Base directory for relative filenames @note: The params dict should contain: mem -- memory size in MBs cdrom -- ISO filename to use with the qemu -cdrom parameter extra_params -- a string to append to the qemu command shell_port -- port of the remote shell daemon on the guest (SSH, Telnet or the home-made Remote Shell Server) shell_client -- client program to use for connecting to the remote shell daemon on the guest (ssh, telnet or nc) x11_display -- if specified, the DISPLAY environment variable will be be set to this value for the qemu process (useful for SDL rendering) images -- a list of image object names, separated by spaces nics -- a list of NIC object names, separated by spaces For each image in images: drive_format -- string to pass as 'if' parameter for this image (e.g. ide, scsi) image_snapshot -- if yes, pass 'snapshot=on' to qemu for this image image_boot -- if yes, pass 'boot=on' to qemu for this image In addition, all parameters required by get_image_filename. For each NIC in nics: nic_model -- string to pass as 'model' parameter for this NIC (e.g. e1000) """ # helper function for command line option wrappers def has_option(help, option): return bool(re.search(r"--%s" % option, help, re.MULTILINE)) # Wrappers for all supported libvirt command line parameters. # This is meant to allow support for multiple libvirt versions. # Each of these functions receives the output of 'libvirt --help' as a # parameter, and should add the requested command line option # accordingly. def add_name(help, name): return " --name '%s'" % name def add_machine_type(help, machine_type): if has_option(help, "machine"): return " --machine %s" % machine_type else: return "" def add_hvm_or_pv(help, hvm_or_pv): if hvm_or_pv == "hvm": return " --hvm --accelerate" elif hvm_or_pv == "pv": return " --paravirt" else: logging.warning("Unknown virt type hvm_or_pv, using default.") return "" def add_mem(help, mem): return " --ram=%s" % mem def add_check_cpu(help): if has_option(help, "check-cpu"): return " --check-cpu" else: return "" def add_smp(help, smp): return " --vcpu=%s" % smp def add_location(help, location): if has_option(help, "location"): return " --location %s" % location else: return "" def add_cdrom(help, filename, index=None): if has_option(help, "cdrom"): return " --cdrom %s" % filename else: return "" def add_pxe(help): if has_option(help, "pxe"): return " --pxe" else: return "" def add_import(help): if has_option(help, "import"): return " --import" else: return "" def add_drive(help, filename, pool=None, vol=None, device=None, bus=None, perms=None, size=None, sparse=False, cache=None, format=None): cmd = " --disk" if filename: cmd += " path=%s" % filename elif pool: if vol: cmd += " vol=%s/%s" % (pool, vol) else: cmd += " pool=%s" % pool if device: cmd += ",device=%s" % device if bus: cmd += ",bus=%s" % bus if perms: cmd += ",%s" % perms if size: cmd += ",size=%s" % size.rstrip("Gg") if sparse: cmd += ",sparse=false" if format: cmd += ",format=%s" % format if cache: cmd += ",cache=%s" % cache return cmd def add_floppy(help, filename): return " --disk path=%s,device=floppy,ro" % filename def add_vnc(help, vnc_port=None): if vnc_port: return " --vnc --vncport=%d" % (vnc_port) else: return " --vnc" def add_vnclisten(help, vnclisten): if has_option(help, "vnclisten"): return " --vnclisten=%s" % (vnclisten) else: return "" def add_sdl(help): if has_option(help, "sdl"): return " --sdl" else: return "" def add_nographic(help): return " --nographics" def add_video(help, video_device): if has_option(help, "video"): return " --video=%s" % (video_device) else: return "" def add_uuid(help, uuid): if has_option(help, "uuid"): return " --uuid %s" % uuid else: return "" def add_os_type(help, os_type): if has_option(help, "os-type"): return " --os-type %s" % os_type else: return "" def add_os_variant(help, os_variant): if has_option(help, "os-variant"): return " --os-variant %s" % os_variant else: return "" def add_pcidevice(help, pci_device): if has_option(help, "host-device"): return " --host-device %s" % pci_device else: return "" def add_soundhw(help, sound_device): if has_option(help, "soundhw"): return " --soundhw %s" % sound_device else: return "" def add_serial(help, filename): if has_option(help, "serial"): return " --serial file,path=%s --serial pty" % filename else: self.only_pty = True return "" def add_kernel_cmdline(help, cmdline): return " -append %s" % cmdline def add_connect_uri(help, uri): if uri and has_option(help, "connect"): return " --connect=%s" % uri else: return "" def add_nic(help, nic_params): """ Return additional command line params based on dict-like nic_params """ mac = nic_params.get('mac') nettype = nic_params.get('nettype') netdst = nic_params.get('netdst') nic_model = nic_params.get('nic_model') if nettype: result = " --network=%s" % nettype else: result = "" if has_option(help, "bridge"): # older libvirt (--network=NATdev --bridge=bridgename --mac=mac) if nettype != 'user': result += ':%s' % netdst if mac: # possible to specify --mac w/o --network result += " --mac=%s" % mac else: # newer libvirt (--network=mynet,model=virtio,mac=00:11) if nettype != 'user': result += '=%s' % netdst if nettype and nic_model: # only supported along with nettype result += ",model=%s" % nic_model if nettype and mac: result += ',mac=%s' % mac elif mac: # possible to specify --mac w/o --network result += " --mac=%s" % mac logging.debug("vm.make_create_command.add_nic returning: %s" % result) return result # 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 # Clone this VM using the new params vm = self.clone(name, params, root_dir, copy_state=True) virt_install_binary = utils_misc.get_path( root_dir, params.get("virt_install_binary", "virt-install")) help = utils.system_output("%s --help" % virt_install_binary) # Find all supported machine types, so we can rule out an unsupported # machine type option passed in the configuration. hvm_or_pv = params.get("hvm_or_pv", "hvm") # default to 'uname -m' output arch_name = params.get("vm_arch_name", utils.get_current_kernel_arch()) capabs = libvirt_xml.LibvirtXML() support_machine_type = capabs.os_arch_machine_map[hvm_or_pv][arch_name] logging.debug("Machine types supported for %s\%s: %s" % (hvm_or_pv, arch_name, support_machine_type)) # Start constructing the qemu command virt_install_cmd = "" # Set the X11 display parameter if requested if params.get("x11_display"): virt_install_cmd += "DISPLAY=%s " % params.get("x11_display") # Add the qemu binary virt_install_cmd += virt_install_binary # set connect uri virt_install_cmd += add_connect_uri(help, self.connect_uri) # hvm or pv specificed by libvirt switch (pv used by Xen only) if hvm_or_pv: virt_install_cmd += add_hvm_or_pv(help, hvm_or_pv) # Add the VM's name virt_install_cmd += add_name(help, name) machine_type = params.get("machine_type") if machine_type: if machine_type in support_machine_type: virt_install_cmd += add_machine_type(help, machine_type) else: raise error.TestNAError("Unsupported machine type %s." % (machine_type)) mem = params.get("mem") if mem: virt_install_cmd += add_mem(help, mem) # TODO: should we do the check before we call ? negative case ? check_cpu = params.get("use_check_cpu") if check_cpu: virt_install_cmd += add_check_cpu(help) smp = params.get("smp") if smp: virt_install_cmd += add_smp(help, smp) # TODO: directory location for vmlinuz/kernel for cdrom install ? location = None if params.get("medium") == 'url': location = params.get('url') elif params.get("medium") == 'kernel_initrd': # directory location of kernel/initrd pair (directory layout must # be in format libvirt will recognize) location = params.get("image_dir") elif params.get("medium") == 'nfs': location = "nfs:%s:%s" % (params.get("nfs_server"), params.get("nfs_dir")) elif params.get("medium") == 'cdrom': if params.get("use_libvirt_cdrom_switch") == 'yes': virt_install_cmd += add_cdrom(help, params.get("cdrom_cd1")) elif params.get("unattended_delivery_method") == "integrated": virt_install_cmd += add_cdrom(help, params.get("cdrom_unattended")) else: location = params.get("image_dir") kernel_dir = os.path.dirname(params.get("kernel")) kernel_parent_dir = os.path.dirname(kernel_dir) pxeboot_link = os.path.join(kernel_parent_dir, "pxeboot") if os.path.islink(pxeboot_link): os.unlink(pxeboot_link) if os.path.isdir(pxeboot_link): logging.info("Removed old %s leftover directory", pxeboot_link) shutil.rmtree(pxeboot_link) os.symlink(kernel_dir, pxeboot_link) elif params.get("medium") == "import": virt_install_cmd += add_import(help) if location: virt_install_cmd += add_location(help, location) if params.get("display") == "vnc": if params.get("vnc_autoport") == "yes": vm.vnc_autoport = True else: vm.vnc_autoport = False if not vm.vnc_autoport and params.get("vnc_port"): vm.vnc_port = int(params.get("vnc_port")) virt_install_cmd += add_vnc(help, vm.vnc_port) if params.get("vnclisten"): vm.vnclisten = params.get("vnclisten") virt_install_cmd += add_vnclisten(help, vm.vnclisten) elif params.get("display") == "sdl": virt_install_cmd += add_sdl(help) elif params.get("display") == "nographic": virt_install_cmd += add_nographic(help) video_device = params.get("video_device") if video_device: virt_install_cmd += add_video(help, video_device) sound_device = params.get("sound_device") if sound_device: virt_install_cmd += add_soundhw(help, sound_device) # if none is given a random UUID will be generated by libvirt if params.get("uuid"): virt_install_cmd += add_uuid(help, params.get("uuid")) # selectable OS type if params.get("use_os_type") == "yes": virt_install_cmd += add_os_type(help, params.get("os_type")) # selectable OS variant if params.get("use_os_variant") == "yes": virt_install_cmd += add_os_variant(help, params.get("os_variant")) # Add serial console virt_install_cmd += add_serial(help, self.get_serial_console_filename()) # If the PCI assignment step went OK, add each one of the PCI assigned # devices to the command line. if self.pci_devices: for pci_id in self.pci_devices: virt_install_cmd += add_pcidevice(help, pci_id) for image_name in params.objects("images"): image_params = params.object_params(image_name) filename = storage.get_image_filename(image_params, data_dir.get_data_dir()) if image_params.get("use_storage_pool") == "yes": filename = None virt_install_cmd += add_drive(help, filename, image_params.get("image_pool"), image_params.get("image_vol"), image_params.get("image_device"), image_params.get("image_bus"), image_params.get("image_perms"), image_params.get("image_size"), image_params.get("drive_sparse"), image_params.get("drive_cache"), image_params.get("image_format")) if image_params.get("boot_drive") == "no": continue if filename: virt_install_cmd += add_drive(help, filename, None, None, None, image_params.get("drive_format"), None, image_params.get("image_size"), image_params.get("drive_sparse"), image_params.get("drive_cache"), image_params.get("image_format")) if (params.get('unattended_delivery_method') != 'integrated' and not (self.driver_type == 'xen' and params.get('hvm_or_pv') == 'pv')): for cdrom in params.objects("cdroms"): cdrom_params = params.object_params(cdrom) iso = cdrom_params.get("cdrom") if params.get("use_libvirt_cdrom_switch") == 'yes': # we don't want to skip the winutils iso if not cdrom == 'winutils': logging.debug("Using --cdrom instead of --disk for install") logging.debug("Skipping CDROM:%s:%s", cdrom, iso) continue if params.get("medium") == 'cdrom_no_kernel_initrd': if iso == params.get("cdrom_cd1"): logging.debug("Using cdrom or url for install") logging.debug("Skipping CDROM: %s", iso) continue if iso: virt_install_cmd += add_drive(help, utils_misc.get_path(root_dir, iso), image_params.get("iso_image_pool"), image_params.get("iso_image_vol"), 'cdrom', None, None, None, None, None, None) # We may want to add {floppy_otps} parameter for -fda # {fat:floppy:}/path/. However vvfat is not usually recommended. # Only support to add the main floppy if you want to add the second # one please modify this part. floppy = params.get("floppy_name") if floppy: floppy = utils_misc.get_path(data_dir.get_data_dir(), floppy) virt_install_cmd += add_drive(help, floppy, None, None, 'floppy', None, None, None, None, None, None) # setup networking parameters for nic in vm.virtnet: # make_create_command can be called w/o vm.create() nic = vm.add_nic(**dict(nic)) logging.debug("make_create_command() setting up command for" " nic: %s" % str(nic)) virt_install_cmd += add_nic(help,nic) if params.get("use_no_reboot") == "yes": virt_install_cmd += " --noreboot" if params.get("use_autostart") == "yes": virt_install_cmd += " --autostart" if params.get("virt_install_debug") == "yes": virt_install_cmd += " --debug" # bz still open, not fully functional yet if params.get("use_virt_install_wait") == "yes": virt_install_cmd += (" --wait %s" % params.get("virt_install_wait_time")) kernel_params = params.get("kernel_params") if kernel_params: virt_install_cmd += " --extra-args '%s'" % kernel_params virt_install_cmd += " --noautoconsole" return virt_install_cmd
def check_disk_params(self, params): """ Check gathered info from qtree/block with params @param params: autotest params @return: number of errors """ err = 0 disks = {} for disk in self.disks: if isinstance(disk, QtreeDisk): disks[disk.get_qname()] = disk.get_params().copy() # We don't have the params name so we need to map file_names instead qname = None for name in params.objects('cdroms'): image_name = utils_misc.get_path(data_dir.get_data_dir(), params.object_params(name).get('cdrom', '')) image_name = os.path.realpath(image_name) for (qname, disk) in disks.iteritems(): if disk.get('image_name') == image_name: break else: continue # Not /proc/scsi cdrom device disks.pop(qname) for name in params.objects('images'): current = None image_params = params.object_params(name) base_dir = image_params.get("images_base_dir", data_dir.get_data_dir()) image_name = os.path.realpath( storage.get_image_filename(image_params, base_dir)) for (qname, disk) in disks.iteritems(): if disk.get('image_name') == image_name: current = disk # autotest params might use relative path current['image_name'] = image_params.get('image_name') break if not current: logging.error("Disk %s is not in qtree but is in params.", name) err += 1 continue for prop in current.iterkeys(): handled = False if prop == "drive_format": # HOOK: ahci disk is ide-* disk if (image_params.get(prop) == 'ahci' and current.get(prop).startswith('ide-')): handled = True # HOOK: params to qemu translation elif current.get(prop).startswith(image_params.get(prop)): handled = True elif (image_params.get(prop) and image_params.get(prop) == current.get(prop)): handled = True if not handled: logging.error("Disk %s property %s=%s doesn't match params" " %s", qname, prop, current.get(prop), image_params.get(prop)) err += 1 disks.pop(qname) if disks: logging.error('Some disks were in qtree but not in autotest params' ': %s', disks) err += 1 return err
def check_disk_params(self, params): """ Check gathered info from qtree/block with params :param params: autotest params :return: number of errors """ def check_drive_format(node, params): """ checks the drive format according to qtree info """ expected = params.get('drive_format') if expected == 'scsi': if arch.ARCH == 'ppc64': expected = 'spapr-vscsi' else: expected = 'lsi53c895a' elif expected.startswith('scsi'): expected = params.get('scsi_hba', 'virtio-scsi-pci') elif expected.startswith('usb'): expected = 'usb-storage' try: if expected == 'virtio': actual = node.qtree['type'] else: actual = node.parent.parent.qtree.get('type') except AttributeError: logging.error("Failed to check drive format, can't get parent" "of:\n%s", node) if actual == 'virtio-scsi-device': # new name for virtio-scsi actual = 'virtio-scsi-pci' if expected not in actual: return ("drive format in qemu is %s, in autotest %s" % (actual, expected)) err = 0 disks = {} for disk in self.disks: if isinstance(disk, QtreeDisk): disks[disk.get_qname()] = (disk.get_params().copy(), disk) # We don't have the params name so we need to map file_names instead qname = None for name in params.objects('cdroms'): image_name = utils_misc.get_path(data_dir.get_data_dir(), params.object_params(name).get('cdrom', '')) image_name = os.path.realpath(image_name) for (qname, disk) in disks.iteritems(): if disk[0].get('image_name') == image_name: break else: continue # Not /proc/scsi cdrom device disks.pop(qname) for name in params.objects('images'): current = None image_params = params.object_params(name) base_dir = image_params.get("images_base_dir", data_dir.get_data_dir()) image_name = os.path.realpath( storage.get_image_filename(image_params, base_dir)) for (qname, disk) in disks.iteritems(): if disk[0].get('image_name') == image_name: current = disk[0] current_node = disk[1] # autotest params might use relative path current['image_name'] = image_params.get('image_name') break if not current: logging.error("Disk %s is not in qtree but is in params.", name) err += 1 continue for prop in current.iterkeys(): handled = False if prop == "drive_format": out = check_drive_format(current_node, image_params) if out: logging.error("Disk %s %s", qname, out) err += 1 handled = True elif (image_params.get(prop) and image_params.get(prop) == current.get(prop)): handled = True if not handled: logging.error("Disk %s property %s=%s doesn't match params" " %s", qname, prop, current.get(prop), image_params.get(prop)) err += 1 disks.pop(qname) if disks: logging.error('Some disks were in qtree but not in autotest params' ': %s', disks) err += 1 return err
def preprocess(test, params, env): """ Preprocess all VMs and images according to the instructions in params. Also, collect some host information, such as the KVM version. :param test: An Autotest test object. :param params: A dict containing all VM and image parameters. :param env: The environment (a dict-like object). """ error.context("preprocessing") # First, let's verify if this test does require root or not. If it # does and the test suite is running as a regular user, we shall just # throw a TestNAError exception, which will skip the test. if params.get('requires_root', 'no') == 'yes': utils_misc.verify_running_as_root() port = params.get('shell_port') prompt = params.get('shell_prompt') address = params.get('ovirt_node_address') username = params.get('ovirt_node_user') password = params.get('ovirt_node_password') setup_pb = False for nic in params.get('nics', "").split(): nic_params = params.object_params(nic) if nic_params.get('netdst') == 'private': setup_pb = True params_pb = nic_params params['netdst_%s' % nic] = nic_params.get("priv_brname", 'atbr0') if setup_pb: brcfg = test_setup.PrivateBridgeConfig(params_pb) brcfg.setup() base_dir = data_dir.get_data_dir() if params.get("storage_type") == "iscsi": iscsidev = qemu_storage.Iscsidev(params, base_dir, "iscsi") params["image_name"] = iscsidev.setup() params["image_raw_device"] = "yes" if params.get("storage_type") == "lvm": lvmdev = qemu_storage.LVMdev(params, base_dir, "lvm") params["image_name"] = lvmdev.setup() params["image_raw_device"] = "yes" env.register_lvmdev("lvm_%s" % params["main_vm"], lvmdev) if params.get("storage_type") == "nfs": image_nfs = nfs.Nfs(params) image_nfs.setup() image_name_only = os.path.basename(params["image_name"]) params['image_name'] = os.path.join(image_nfs.mount_dir, image_name_only) for image_name in params.objects("images"): name_tag = "image_name_%s" % image_name if params.get(name_tag): image_name_only = os.path.basename(params[name_tag]) params[name_tag] = os.path.join(image_nfs.mount_dir, image_name_only) # Start tcpdump if it isn't already running # The fact it has to be started here is so that the test params # have to be honored. env.start_tcpdump(params) # Destroy and remove VMs that are no longer needed in the environment requested_vms = params.objects("vms") for key in env.keys(): vm = env[key] if not isinstance(vm, virt_vm.BaseVM): continue if not vm.name in requested_vms: vm.destroy() del env[key] if (params.get("auto_cpu_model") == "yes" and params.get("vm_type") == "qemu"): if not env.get("cpu_model"): env["cpu_model"] = utils_misc.get_qemu_best_cpu_model(params) params["cpu_model"] = env.get("cpu_model") kvm_ver_cmd = params.get("kvm_ver_cmd", "") if kvm_ver_cmd: try: cmd_result = utils.run(kvm_ver_cmd) kvm_version = cmd_result.stdout.strip() except error.CmdError: kvm_version = "Unknown" else: # Get the KVM kernel module version and write it as a keyval if os.path.exists("/dev/kvm"): try: kvm_version = open("/sys/module/kvm/version").read().strip() except Exception: kvm_version = os.uname()[2] else: logging.warning("KVM module not loaded") kvm_version = "Unknown" logging.debug("KVM version: %s" % kvm_version) test.write_test_keyval({"kvm_version": kvm_version}) # Get the KVM userspace version and write it as a keyval kvm_userspace_ver_cmd = params.get("kvm_userspace_ver_cmd", "") if kvm_userspace_ver_cmd: try: cmd_result = utils.run(kvm_userspace_ver_cmd) kvm_userspace_version = cmd_result.stdout.strip() except error.CmdError: kvm_userspace_version = "Unknown" else: qemu_path = utils_misc.get_qemu_binary(params) version_line = commands.getoutput("%s -help | head -n 1" % qemu_path) matches = re.findall("[Vv]ersion .*?,", version_line) if matches: kvm_userspace_version = " ".join(matches[0].split()[1:]).strip(",") else: kvm_userspace_version = "Unknown" logging.debug("KVM userspace version: %s" % kvm_userspace_version) test.write_test_keyval({"kvm_userspace_version": kvm_userspace_version}) if params.get("setup_hugepages") == "yes": h = test_setup.HugePageConfig(params) suggest_mem = h.setup() if suggest_mem is not None: params['mem'] = suggest_mem if params.get("vm_type") == "libvirt": utils_libvirtd.libvirtd_restart() if params.get("setup_thp") == "yes": thp = test_setup.TransparentHugePageConfig(test, params) thp.setup() if params.get("setup_ksm") == "yes": ksm = test_setup.KSMConfig(params, env) ksm.setup(env) # Execute any pre_commands if params.get("pre_command"): process_command(test, params, env, params.get("pre_command"), int(params.get("pre_command_timeout", "600")), params.get("pre_command_noncritical") == "yes") # if you want set "pci=nomsi" before test, set "disable_pci_msi = yes" # and pci_msi_sensitive = "yes" if params.get("pci_msi_sensitive", "no") == "yes": disable_pci_msi = params.get("disable_pci_msi", "no") image_filename = storage.get_image_filename(params, data_dir.get_data_dir()) grub_file = params.get("grub_file", "/boot/grub2/grub.cfg") kernel_cfg_pos_reg = params.get("kernel_cfg_pos_reg", r".*vmlinuz-\d+.*") msi_keyword = params.get("msi_keyword", " pci=nomsi") disk_obj = utils_disk.GuestFSModiDisk(image_filename) kernel_config_ori = disk_obj.read_file(grub_file) kernel_config = re.findall(kernel_cfg_pos_reg, kernel_config_ori) if not kernel_config: raise error.TestError("Cannot find the kernel config, reg is %s" % kernel_cfg_pos_reg) kernel_config_line = kernel_config[0] kernel_need_modify = False if disable_pci_msi == "yes": if not re.findall(msi_keyword, kernel_config_line): kernel_config_set = kernel_config_line + msi_keyword kernel_need_modify = True else: if re.findall(msi_keyword, kernel_config_line): kernel_config_set = re.sub(msi_keyword, "", kernel_config_line) kernel_need_modify = True if kernel_need_modify: for vm in env.get_all_vms(): if vm: vm.destroy() env.unregister_vm(vm.name) disk_obj.replace_image_file_content(grub_file, kernel_config_line, kernel_config_set) logging.debug("Guest cmdline 'pci=nomsi' setting is: [ %s ]" % disable_pci_msi) kernel_extra_params = params.get("kernel_extra_params") if kernel_extra_params: image_filename = storage.get_image_filename(params, data_dir.get_data_dir()) grub_file = params.get("grub_file", "/boot/grub2/grub.cfg") kernel_cfg_pos_reg = params.get("kernel_cfg_pos_reg", r".*vmlinuz-\d+.*") disk_obj = utils_disk.GuestFSModiDisk(image_filename) kernel_config_ori = disk_obj.read_file(grub_file) kernel_config = re.findall(kernel_cfg_pos_reg, kernel_config_ori) if not kernel_config: raise error.TestError("Cannot find the kernel config, reg is %s" % kernel_cfg_pos_reg) kernel_config_line = kernel_config[0] kernel_need_modify = False if not re.findall(kernel_extra_params, kernel_config_line): kernel_config_set = kernel_config_line + kernel_extra_params kernel_need_modify = True if kernel_need_modify: for vm in env.get_all_vms(): if vm: vm.destroy() env.unregister_vm(vm.name) disk_obj.replace_image_file_content(grub_file, kernel_config_line, kernel_config_set) logging.debug("Guest cmdline extra_params setting is: [ %s ]" % kernel_extra_params) # Clone master image from vms. base_dir = data_dir.get_data_dir() if params.get("master_images_clone"): for vm_name in params.get("vms").split(): vm = env.get_vm(vm_name) if vm: vm.destroy(free_mac_addresses=False) env.unregister_vm(vm_name) vm_params = params.object_params(vm_name) for image in vm_params.get("master_images_clone").split(): image_obj = qemu_storage.QemuImg(params, base_dir, image) image_obj.clone_image(params, vm_name, image, base_dir) # Preprocess all VMs and images if params.get("not_preprocess", "no") == "no": process(test, params, env, preprocess_image, preprocess_vm) # Start the screendump thread if params.get("take_regular_screendumps") == "yes": global _screendump_thread, _screendump_thread_termination_event _screendump_thread_termination_event = threading.Event() _screendump_thread = threading.Thread(target=_take_screendumps, name='ScreenDump', args=(test, params, env)) _screendump_thread.start() return params
kernel_extra_params_remove = params.get("kernel_extra_params_remove") if params.get("disable_pci_msi"): disable_pci_msi = params.get("disable-pci_msi") if disable_pci_msi == "yes": if "pci=" in kernel_extra_params_add: kernel_extra_params_add = re.sub("pci=.*?\s+", "pci=nomsi ", kernel_extra_params_add) else: kernel_extra_params_add += " pci=nomsi" params["ker_remove_similar_pci"] = "yes" else: kernel_extra_params_remove += " pci=nomsi" if kernel_extra_params_add or kernel_extra_params_remove: global kernel_cmdline, kernel_modified image_filename = storage.get_image_filename(params, data_dir.get_data_dir()) grub_file = params.get("grub_file", "/boot/grub2/grub.cfg") kernel_cfg_pos_reg = params.get("kernel_cfg_pos_reg", r".*vmlinuz-\d+.*") disk_obj = utils_disk.GuestFSModiDisk(image_filename) kernel_config_ori = disk_obj.read_file(grub_file) kernel_config = re.findall(kernel_cfg_pos_reg, kernel_config_ori) if not kernel_config: raise error.TestError("Cannot find the kernel config, reg is %s" % kernel_cfg_pos_reg) kernel_config = kernel_config[0] kernel_cmdline = kernel_config kernel_need_modify = False kernel_config_set = kernel_config
def check_disk_params(self, params): """ Check gathered info from qtree/block with params :param params: autotest params :return: number of errors """ def check_drive_format(node, params): """ checks the drive format according to qtree info """ expected = params.get('drive_format') if expected == 'scsi': if arch.ARCH == 'ppc64': expected = 'spapr-vscsi' else: expected = 'lsi53c895a' elif expected.startswith('scsi'): expected = params.get('scsi_hba', 'virtio-scsi-pci') elif expected.startswith('usb'): expected = 'usb-storage' try: if expected == 'virtio': actual = node.qtree['type'] else: actual = node.parent.parent.qtree.get('type') except AttributeError: logging.error( "Failed to check drive format, can't get parent" "of:\n%s", node) if actual == 'virtio-scsi-device': # new name for virtio-scsi actual = 'virtio-scsi-pci' if expected not in actual: return ("drive format in qemu is %s, in autotest %s" % (actual, expected)) err = 0 disks = {} for disk in self.disks: if isinstance(disk, QtreeDisk): disks[disk.get_qname()] = (disk.get_params().copy(), disk) # We don't have the params name so we need to map file_names instead qname = None for name in params.objects('cdroms'): image_name = utils_misc.get_path( data_dir.get_data_dir(), params.object_params(name).get('cdrom', '')) image_name = os.path.realpath(image_name) for (qname, disk) in disks.iteritems(): if disk[0].get('image_name') == image_name: break else: continue # Not /proc/scsi cdrom device disks.pop(qname) for name in params.objects('images'): current = None image_params = params.object_params(name) base_dir = image_params.get("images_base_dir", data_dir.get_data_dir()) image_name = os.path.realpath( storage.get_image_filename(image_params, base_dir)) for (qname, disk) in disks.iteritems(): if disk[0].get('image_name') == image_name: current = disk[0] current_node = disk[1] # autotest params might use relative path current['image_name'] = image_params.get('image_name') break if not current: logging.error("Disk %s is not in qtree but is in params.", name) err += 1 continue for prop in current.iterkeys(): handled = False if prop == "drive_format": out = check_drive_format(current_node, image_params) if out: logging.error("Disk %s %s", qname, out) err += 1 handled = True elif (image_params.get(prop) and image_params.get(prop) == current.get(prop)): handled = True if not handled: logging.error( "Disk %s property %s=%s doesn't match params" " %s", qname, prop, current.get(prop), image_params.get(prop)) err += 1 disks.pop(qname) if disks: logging.error( 'Some disks were in qtree but not in autotest params' ': %s', disks) err += 1 return err
def make_create_command(self, name=None, params=None, root_dir=None): """ Generate a libvirt command line. All parameters are optional. If a parameter is not supplied, the corresponding value stored in the class attributes is used. @param name: The name of the object @param params: A dict containing VM params @param root_dir: Base directory for relative filenames @note: The params dict should contain: mem -- memory size in MBs cdrom -- ISO filename to use with the qemu -cdrom parameter extra_params -- a string to append to the qemu command shell_port -- port of the remote shell daemon on the guest (SSH, Telnet or the home-made Remote Shell Server) shell_client -- client program to use for connecting to the remote shell daemon on the guest (ssh, telnet or nc) x11_display -- if specified, the DISPLAY environment variable will be be set to this value for the qemu process (useful for SDL rendering) images -- a list of image object names, separated by spaces nics -- a list of NIC object names, separated by spaces For each image in images: drive_format -- string to pass as 'if' parameter for this image (e.g. ide, scsi) image_snapshot -- if yes, pass 'snapshot=on' to qemu for this image image_boot -- if yes, pass 'boot=on' to qemu for this image In addition, all parameters required by get_image_filename. For each NIC in nics: nic_model -- string to pass as 'model' parameter for this NIC (e.g. e1000) """ # helper function for command line option wrappers def has_option(help_text, option): return bool(re.search(r"--%s" % option, help_text, re.MULTILINE)) # Wrappers for all supported libvirt command line parameters. # This is meant to allow support for multiple libvirt versions. # Each of these functions receives the output of 'libvirt --help' as a # parameter, and should add the requested command line option # accordingly. def add_name(help_text, name): return " --name '%s'" % name def add_machine_type(help_text, machine_type): if has_option(help_text, "machine"): return " --machine %s" % machine_type else: return "" def add_hvm_or_pv(help_text, hvm_or_pv): if hvm_or_pv == "hvm": return " --hvm --accelerate" elif hvm_or_pv == "pv": return " --paravirt" else: logging.warning("Unknown virt type hvm_or_pv, using default.") return "" def add_mem(help_text, mem): return " --ram=%s" % mem def add_check_cpu(help_text): if has_option(help_text, "check-cpu"): return " --check-cpu" else: return "" def add_smp(help_text, smp): return " --vcpu=%s" % smp def add_location(help_text, location): if has_option(help_text, "location"): return " --location %s" % location else: return "" def add_cdrom(help_text, filename, index=None): if has_option(help_text, "cdrom"): return " --cdrom %s" % filename else: return "" def add_pxe(help_text): if has_option(help_text, "pxe"): return " --pxe" else: return "" def add_import(help_text): if has_option(help_text, "import"): return " --import" else: return "" def add_drive( help_text, filename, pool=None, vol=None, device=None, bus=None, perms=None, size=None, sparse=False, cache=None, fmt=None, ): cmd = " --disk" if filename: cmd += " path=%s" % filename elif pool: if vol: cmd += " vol=%s/%s" % (pool, vol) else: cmd += " pool=%s" % pool if device: cmd += ",device=%s" % device if bus: cmd += ",bus=%s" % bus if perms: cmd += ",%s" % perms if size: cmd += ",size=%s" % size.rstrip("Gg") if sparse: cmd += ",sparse=false" if fmt: cmd += ",format=%s" % fmt if cache: cmd += ",cache=%s" % cache return cmd def add_floppy(help_text, filename): return " --disk path=%s,device=floppy,ro" % filename def add_vnc(help_text, vnc_port=None): if vnc_port: return " --vnc --vncport=%d" % (vnc_port) else: return " --vnc" def add_vnclisten(help_text, vnclisten): if has_option(help_text, "vnclisten"): return " --vnclisten=%s" % (vnclisten) else: return "" def add_sdl(help_text): if has_option(help_text, "sdl"): return " --sdl" else: return "" def add_nographic(help_text): return " --nographics" def add_video(help_text, video_device): if has_option(help_text, "video"): return " --video=%s" % (video_device) else: return "" def add_uuid(help_text, uuid): if has_option(help_text, "uuid"): return " --uuid %s" % uuid else: return "" def add_os_type(help_text, os_type): if has_option(help_text, "os-type"): return " --os-type %s" % os_type else: return "" def add_os_variant(help_text, os_variant): if has_option(help_text, "os-variant"): return " --os-variant %s" % os_variant else: return "" def add_pcidevice(help_text, pci_device): if has_option(help_text, "host-device"): return " --host-device %s" % pci_device else: return "" def add_soundhw(help_text, sound_device): if has_option(help_text, "soundhw"): return " --soundhw %s" % sound_device else: return "" def add_serial(help_text, filename): if has_option(help_text, "serial"): return " --serial file,path=%s --serial pty" % filename else: self.only_pty = True return "" def add_kernel_cmdline(help_text, cmdline): return " -append %s" % cmdline def add_connect_uri(help_text, uri): if uri and has_option(help_text, "connect"): return " --connect=%s" % uri else: return "" def add_nic(help_text, nic_params): """ Return additional command line params based on dict-like nic_params """ mac = nic_params.get("mac") nettype = nic_params.get("nettype") netdst = nic_params.get("netdst") nic_model = nic_params.get("nic_model") if nettype: result = " --network=%s" % nettype else: result = "" if has_option(help_text, "bridge"): # older libvirt (--network=NATdev --bridge=bridgename --mac=mac) if nettype != "user": result += ":%s" % netdst if mac: # possible to specify --mac w/o --network result += " --mac=%s" % mac else: # newer libvirt (--network=mynet,model=virtio,mac=00:11) if nettype != "user": result += "=%s" % netdst if nettype and nic_model: # only supported along with nettype result += ",model=%s" % nic_model if nettype and mac: result += ",mac=%s" % mac elif mac: # possible to specify --mac w/o --network result += " --mac=%s" % mac logging.debug("vm.make_create_command.add_nic returning: %s" % result) return result # 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 # Clone this VM using the new params vm = self.clone(name, params, root_dir, copy_state=True) virt_install_binary = utils_misc.get_path(root_dir, params.get("virt_install_binary", "virt-install")) help_text = utils.system_output("%s --help" % virt_install_binary) # Find all supported machine types, so we can rule out an unsupported # machine type option passed in the configuration. hvm_or_pv = params.get("hvm_or_pv", "hvm") # default to 'uname -m' output arch_name = params.get("vm_arch_name", utils.get_current_kernel_arch()) capabs = libvirt_xml.LibvirtXML() support_machine_type = capabs.os_arch_machine_map[hvm_or_pv][arch_name] logging.debug("Machine types supported for %s\%s: %s" % (hvm_or_pv, arch_name, support_machine_type)) # Start constructing the qemu command virt_install_cmd = "" # Set the X11 display parameter if requested if params.get("x11_display"): virt_install_cmd += "DISPLAY=%s " % params.get("x11_display") # Add the qemu binary virt_install_cmd += virt_install_binary # set connect uri virt_install_cmd += add_connect_uri(help_text, self.connect_uri) # hvm or pv specificed by libvirt switch (pv used by Xen only) if hvm_or_pv: virt_install_cmd += add_hvm_or_pv(help_text, hvm_or_pv) # Add the VM's name virt_install_cmd += add_name(help_text, name) machine_type = params.get("machine_type") if machine_type: if machine_type in support_machine_type: virt_install_cmd += add_machine_type(help_text, machine_type) else: raise error.TestNAError("Unsupported machine type %s." % (machine_type)) mem = params.get("mem") if mem: virt_install_cmd += add_mem(help_text, mem) # TODO: should we do the check before we call ? negative case ? check_cpu = params.get("use_check_cpu") if check_cpu: virt_install_cmd += add_check_cpu(help_text) smp = params.get("smp") if smp: virt_install_cmd += add_smp(help_text, smp) # TODO: directory location for vmlinuz/kernel for cdrom install ? location = None if params.get("medium") == "url": location = params.get("url") elif params.get("medium") == "kernel_initrd": # directory location of kernel/initrd pair (directory layout must # be in format libvirt will recognize) location = params.get("image_dir") elif params.get("medium") == "nfs": location = "nfs:%s:%s" % (params.get("nfs_server"), params.get("nfs_dir")) elif params.get("medium") == "cdrom": if params.get("use_libvirt_cdrom_switch") == "yes": virt_install_cmd += add_cdrom(help_text, params.get("cdrom_cd1")) elif params.get("unattended_delivery_method") == "integrated": virt_install_cmd += add_cdrom( help_text, os.path.join(data_dir.get_data_dir(), params.get("cdrom_unattended")) ) else: location = params.get("image_dir") kernel_dir = os.path.dirname(params.get("kernel")) kernel_parent_dir = os.path.dirname(kernel_dir) pxeboot_link = os.path.join(kernel_parent_dir, "pxeboot") if os.path.islink(pxeboot_link): os.unlink(pxeboot_link) if os.path.isdir(pxeboot_link): logging.info("Removed old %s leftover directory", pxeboot_link) shutil.rmtree(pxeboot_link) os.symlink(kernel_dir, pxeboot_link) elif params.get("medium") == "import": virt_install_cmd += add_import(help_text) if location: virt_install_cmd += add_location(help_text, location) if params.get("display") == "vnc": if params.get("vnc_autoport") == "yes": vm.vnc_autoport = True else: vm.vnc_autoport = False if not vm.vnc_autoport and params.get("vnc_port"): vm.vnc_port = int(params.get("vnc_port")) virt_install_cmd += add_vnc(help_text, vm.vnc_port) if params.get("vnclisten"): vm.vnclisten = params.get("vnclisten") virt_install_cmd += add_vnclisten(help_text, vm.vnclisten) elif params.get("display") == "sdl": virt_install_cmd += add_sdl(help_text) elif params.get("display") == "nographic": virt_install_cmd += add_nographic(help_text) video_device = params.get("video_device") if video_device: virt_install_cmd += add_video(help_text, video_device) sound_device = params.get("sound_device") if sound_device: virt_install_cmd += add_soundhw(help_text, sound_device) # if none is given a random UUID will be generated by libvirt if params.get("uuid"): virt_install_cmd += add_uuid(help_text, params.get("uuid")) # selectable OS type if params.get("use_os_type") == "yes": virt_install_cmd += add_os_type(help_text, params.get("os_type")) # selectable OS variant if params.get("use_os_variant") == "yes": virt_install_cmd += add_os_variant(help_text, params.get("os_variant")) # Add serial console virt_install_cmd += add_serial(help_text, self.get_serial_console_filename()) # If the PCI assignment step went OK, add each one of the PCI assigned # devices to the command line. if self.pci_devices: for pci_id in self.pci_devices: virt_install_cmd += add_pcidevice(help_text, pci_id) for image_name in params.objects("images"): image_params = params.object_params(image_name) filename = storage.get_image_filename(image_params, data_dir.get_data_dir()) if image_params.get("use_storage_pool") == "yes": filename = None virt_install_cmd += add_drive( help_text, filename, image_params.get("image_pool"), image_params.get("image_vol"), image_params.get("image_device"), image_params.get("image_bus"), image_params.get("image_perms"), image_params.get("image_size"), image_params.get("drive_sparse"), image_params.get("drive_cache"), image_params.get("image_format"), ) if image_params.get("boot_drive") == "no": continue if filename: virt_install_cmd += add_drive( help_text, filename, None, None, None, image_params.get("drive_format"), None, image_params.get("image_size"), image_params.get("drive_sparse"), image_params.get("drive_cache"), image_params.get("image_format"), ) if params.get("unattended_delivery_method") != "integrated" and not ( self.driver_type == "xen" and params.get("hvm_or_pv") == "pv" ): for cdrom in params.objects("cdroms"): cdrom_params = params.object_params(cdrom) iso = cdrom_params.get("cdrom") if params.get("use_libvirt_cdrom_switch") == "yes": # we don't want to skip the winutils iso if not cdrom == "winutils": logging.debug("Using --cdrom instead of --disk for install") logging.debug("Skipping CDROM:%s:%s", cdrom, iso) continue if params.get("medium") == "cdrom_no_kernel_initrd": if iso == params.get("cdrom_cd1"): logging.debug("Using cdrom or url for install") logging.debug("Skipping CDROM: %s", iso) continue if iso: virt_install_cmd += add_drive( help_text, utils_misc.get_path(root_dir, iso), image_params.get("iso_image_pool"), image_params.get("iso_image_vol"), "cdrom", None, None, None, None, None, None, ) # We may want to add {floppy_otps} parameter for -fda # {fat:floppy:}/path/. However vvfat is not usually recommended. # Only support to add the main floppy if you want to add the second # one please modify this part. floppy = params.get("floppy_name") if floppy: floppy = utils_misc.get_path(data_dir.get_data_dir(), floppy) virt_install_cmd += add_drive(help_text, floppy, None, None, "floppy", None, None, None, None, None, None) # setup networking parameters for nic in vm.virtnet: # make_create_command can be called w/o vm.create() nic = vm.add_nic(**dict(nic)) logging.debug("make_create_command() setting up command for" " nic: %s" % str(nic)) virt_install_cmd += add_nic(help_text, nic) if params.get("use_no_reboot") == "yes": virt_install_cmd += " --noreboot" if params.get("use_autostart") == "yes": virt_install_cmd += " --autostart" if params.get("virt_install_debug") == "yes": virt_install_cmd += " --debug" # bz still open, not fully functional yet if params.get("use_virt_install_wait") == "yes": virt_install_cmd += " --wait %s" % params.get("virt_install_wait_time") kernel_params = params.get("kernel_params") if kernel_params: virt_install_cmd += " --extra-args '%s'" % kernel_params virt_install_cmd += " --noautoconsole" return virt_install_cmd
logging.error("e") except Exception, e: logging.error("Unexpected error:" % e) libvirtd_inst.restart() # Execute any pre_commands if params.get("pre_command"): process_command(test, params, env, params.get("pre_command"), int(params.get("pre_command_timeout", "600")), params.get("pre_command_noncritical") == "yes") # if you want set "pci=nomsi" before test, set "disable_pci_msi = yes" # and pci_msi_sensitive = "yes" if params.get("pci_msi_sensitive", "no") == "yes": disable_pci_msi = params.get("disable_pci_msi", "no") image_filename = storage.get_image_filename(params, data_dir.get_data_dir()) grub_file = params.get("grub_file", "/boot/grub2/grub.cfg") kernel_cfg_pos_reg = params.get("kernel_cfg_pos_reg", r".*vmlinuz-\d+.*") msi_keyword = params.get("msi_keyword", " pci=nomsi") disk_obj = utils_disk.GuestFSModiDisk(image_filename) kernel_config_ori = disk_obj.read_file(grub_file) kernel_config = re.findall(kernel_cfg_pos_reg, kernel_config_ori) if not kernel_config: raise error.TestError("Cannot find the kernel config, reg is %s" % kernel_cfg_pos_reg) kernel_config_line = kernel_config[0] kernel_need_modify = False if disable_pci_msi == "yes":
def create(self, params, ignore_errors=False): """ Create an image using qemu_img or dd. :param params: Dictionary containing the test parameters. :param ignore_errors: Whether to ignore errors on the image creation cmd. :note: params should contain: image_name name of the image file, without extension image_format format of the image (qcow2, raw etc) image_cluster_size (optional) cluster size for the image image_size requested size of the image (a string qemu-img can understand, such as '10G') create_with_dd use dd to create the image (raw format only) base_image(optional) the base image name when create snapshot base_format(optional) the format of base image encrypted(optional) if the image is encrypted, allowed values: on and off. Default is "off" preallocated(optional) if preallocation when create image, allowed values: off, metadata. Default is "off" :return: tuple (path to the image created, utils.CmdResult object containing the result of the creation command). """ if params.get("create_with_dd") == "yes" and self.image_format == "raw": # maps K,M,G,T => (count, bs) human = {'K': (1, 1), 'M': (1, 1024), 'G': (1024, 1024), 'T': (1024, 1048576), } if human.has_key(self.size[-1]): block_size = human[self.size[-1]][1] size = int(self.size[:-1]) * human[self.size[-1]][0] qemu_img_cmd = ("dd if=/dev/zero of=%s count=%s bs=%sK" % (self.image_filename, size, block_size)) else: qemu_img_cmd = self.image_cmd qemu_img_cmd += " create" qemu_img_cmd += " -f %s" % self.image_format image_cluster_size = params.get("image_cluster_size", None) preallocated = params.get("preallocated", "off") encrypted = params.get("encrypted", "off") image_extra_params = params.get("image_extra_params", "") has_backing_file = params.get('has_backing_file') qemu_img_cmd += " -o " if preallocated != "off": qemu_img_cmd += "preallocation=%s," % preallocated if encrypted != "off": qemu_img_cmd += "encrypted=%s," % encrypted if image_cluster_size is not None: qemu_img_cmd += "cluster_size=%s," % image_cluster_size if has_backing_file == "yes": backing_param = params.object_params("backing_file") backing_file = storage.get_image_filename(backing_param, self.root_dir) backing_fmt = backing_param.get("image_format") qemu_img_cmd += "backing_file=%s," % backing_file qemu_img_cmd += "backing_fmt=%s," % backing_fmt if image_extra_params: qemu_img_cmd += "%s," % image_extra_params qemu_img_cmd = qemu_img_cmd.rstrip(" -o") qemu_img_cmd = qemu_img_cmd.rstrip(",") if self.base_tag: qemu_img_cmd += " -b %s" % self.base_image_filename if self.base_format: qemu_img_cmd += " -F %s" % self.base_format qemu_img_cmd += " %s" % self.image_filename qemu_img_cmd += " %s" % self.size if (params.get("image_backend", "filesystem") == "filesystem"): image_dirname = os.path.dirname(self.image_filename) if image_dirname and not os.path.isdir(image_dirname): e_msg = ("Parent directory of the image file %s does " "not exist" % self.image_filename) logging.error(e_msg) logging.error("This usually means a serious setup error.") logging.error("Please verify if your data dir contains the " "expected directory structure") logging.error("Backing data dir: %s", data_dir.get_backing_data_dir()) logging.error("Directory structure:") for root, _, _ in os.walk(data_dir.get_backing_data_dir()): logging.error(root) logging.warning("We'll try to proceed by creating the dir. " "Other errors may ensue") os.makedirs(image_dirname) msg = "Create image by command: %s" % qemu_img_cmd error.context(msg, logging.info) cmd_result = utils.run(qemu_img_cmd, verbose=False, ignore_status=True) if cmd_result.exit_status != 0 and not ignore_errors: raise error.TestError("Failed to create image %s" % self.image_filename) return self.image_filename, cmd_result
def preprocess(test, params, env): """ Preprocess all VMs and images according to the instructions in params. Also, collect some host information, such as the KVM version. @param test: An Autotest test object. @param params: A dict containing all VM and image parameters. @param env: The environment (a dict-like object). """ error.context("preprocessing") # First, let's verify if this test does require root or not. If it # does and the test suite is running as a regular user, we shall just # throw a TestNAError exception, which will skip the test. if params.get('requires_root', 'no') == 'yes': utils_misc.verify_running_as_root() port = params.get('shell_port') prompt = params.get('shell_prompt') address = params.get('ovirt_node_address') username = params.get('ovirt_node_user') password = params.get('ovirt_node_password') setup_pb = False for nic in params.get('nics', "").split(): nic_params = params.object_params(nic) if nic_params.get('netdst') == 'private': setup_pb = True params_pb = nic_params params['netdst_%s' % nic] = nic_params.get("priv_brname", 'atbr0') if setup_pb: brcfg = test_setup.PrivateBridgeConfig(params_pb) brcfg.setup() base_dir = data_dir.get_data_dir() if params.get("storage_type") == "iscsi": iscsidev = qemu_storage.Iscsidev(params, base_dir, "iscsi") params["image_name"] = iscsidev.setup() params["image_raw_device"] = "yes" # Start tcpdump if it isn't already running if "address_cache" not in env: env["address_cache"] = {} if "tcpdump" in env and not env["tcpdump"].is_alive(): env["tcpdump"].close() del env["tcpdump"] if "tcpdump" not in env and params.get("run_tcpdump", "yes") == "yes": cmd = "%s -npvi any 'port 68'" % utils_misc.find_command("tcpdump") if params.get("remote_preprocess") == "yes": login_cmd = ("ssh -o UserKnownHostsFile=/dev/null -o \ PreferredAuthentications=password -p %s %s@%s" % (port, username, address)) env["tcpdump"] = aexpect.ShellSession( login_cmd, output_func=_update_address_cache, output_params=(env["address_cache"],)) remote.handle_prompts(env["tcpdump"], username, password, prompt) env["tcpdump"].sendline(cmd) else: env["tcpdump"] = aexpect.Tail( command=cmd, output_func=_tcpdump_handler, output_params=(env["address_cache"], "tcpdump.log",)) if utils_misc.wait_for(lambda: not env["tcpdump"].is_alive(), 0.1, 0.1, 1.0): logging.warn("Could not start tcpdump") logging.warn("Status: %s" % env["tcpdump"].get_status()) logging.warn("Output:" + utils_misc.format_str_for_message( env["tcpdump"].get_output())) # Destroy and remove VMs that are no longer needed in the environment requested_vms = params.objects("vms") for key in env.keys(): vm = env[key] if not isinstance(vm, virt_vm.BaseVM): continue if not vm.name in requested_vms: vm.destroy() del env[key] if (params.get("auto_cpu_model") == "yes" and params.get("vm_type") == "qemu"): if not env.get("cpu_model"): env["cpu_model"] = utils_misc.get_qemu_best_cpu_model(params) params["cpu_model"] = env.get("cpu_model") kvm_ver_cmd = params.get("kvm_ver_cmd", "") if kvm_ver_cmd: try: cmd_result = utils.run(kvm_ver_cmd) kvm_version = cmd_result.stdout.strip() except error.CmdError: kvm_version = "Unknown" else: # Get the KVM kernel module version and write it as a keyval if os.path.exists("/dev/kvm"): try: kvm_version = open("/sys/module/kvm/version").read().strip() except Exception: kvm_version = os.uname()[2] else: logging.warning("KVM module not loaded") kvm_version = "Unknown" logging.debug("KVM version: %s" % kvm_version) test.write_test_keyval({"kvm_version": kvm_version}) # Get the KVM userspace version and write it as a keyval kvm_userspace_ver_cmd = params.get("kvm_userspace_ver_cmd", "") if kvm_userspace_ver_cmd: try: cmd_result = utils.run(kvm_userspace_ver_cmd) kvm_userspace_version = cmd_result.stdout.strip() except error.CmdError: kvm_userspace_version = "Unknown" else: qemu_path = utils_misc.get_qemu_binary(params) version_line = commands.getoutput("%s -help | head -n 1" % qemu_path) matches = re.findall("[Vv]ersion .*?,", version_line) if matches: kvm_userspace_version = " ".join(matches[0].split()[1:]).strip(",") else: kvm_userspace_version = "Unknown" logging.debug("KVM userspace version: %s" % kvm_userspace_version) test.write_test_keyval({"kvm_userspace_version": kvm_userspace_version}) if params.get("setup_hugepages") == "yes": h = test_setup.HugePageConfig(params) h.setup() if params.get("vm_type") == "libvirt": utils_libvirtd.libvirtd_restart() if params.get("setup_thp") == "yes": thp = test_setup.TransparentHugePageConfig(test, params) thp.setup() if params.get("setup_ksm") == "yes": ksm = test_setup.KSMConfig(params, env) ksm.setup(env) # Execute any pre_commands if params.get("pre_command"): process_command(test, params, env, params.get("pre_command"), int(params.get("pre_command_timeout", "600")), params.get("pre_command_noncritical") == "yes") # if you want set "pci=nomsi" before test, set "disable_pci_msi = yes" # and pci_msi_sensitive = "yes" if params.get("pci_msi_sensitive", "no") == "yes": disable_pci_msi = params.get("disable_pci_msi", "no") image_filename = storage.get_image_filename(params, data_dir.get_data_dir()) grub_file = params.get("grub_file", "/boot/grub2/grub.cfg") kernel_cfg_pos_reg = params.get("kernel_cfg_pos_reg", r".*vmlinuz-\d+.*") msi_keyword = params.get("msi_keyword", " pci=nomsi") disk_obj = utils_disk.GuestFSModiDisk(image_filename) kernel_config_ori = disk_obj.read_file(grub_file) kernel_config = re.findall(kernel_cfg_pos_reg, kernel_config_ori) if not kernel_config: raise error.TestError("Cannot find the kernel config, reg is %s" % kernel_cfg_pos_reg) kernel_config_line = kernel_config[0] kernel_need_modify = False if disable_pci_msi == "yes": if not re.findall(msi_keyword, kernel_config_line): kernel_config_set = kernel_config_line + msi_keyword kernel_need_modify = True else: if re.findall(msi_keyword, kernel_config_line): kernel_config_set = re.sub(msi_keyword, "", kernel_config_line) kernel_need_modify = True if kernel_need_modify: for vm in env.get_all_vms(): if vm: vm.destroy() env.unregister_vm(vm.name) disk_obj.replace_image_file_content(grub_file, kernel_config_line, kernel_config_set) logging.debug("Guest cmdline 'pci=nomsi' setting is: [ %s ]" % disable_pci_msi) # Clone master image from vms. base_dir = data_dir.get_data_dir() if params.get("master_images_clone"): for vm_name in params.get("vms").split(): vm = env.get_vm(vm_name) if vm: vm.destroy(free_mac_addresses=False) env.unregister_vm(vm_name) vm_params = params.object_params(vm_name) for image in vm_params.get("master_images_clone").split(): image_obj = qemu_storage.QemuImg(params, base_dir, image) image_obj.clone_image(params, vm_name, image, base_dir) # Preprocess all VMs and images if params.get("not_preprocess", "no") == "no": process(test, params, env, preprocess_image, preprocess_vm) # Start the screendump thread if params.get("take_regular_screendumps") == "yes": global _screendump_thread, _screendump_thread_termination_event _screendump_thread_termination_event = threading.Event() _screendump_thread = threading.Thread(target=_take_screendumps, name='ScreenDump', args=(test, params, env)) _screendump_thread.start() return params