def preprocess_image(test, params): """ 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. """ image_filename = virt_vm.get_image_filename(params, test.bindir) create_image = False if params.get("force_create_image") == "yes": logging.debug("Param 'force_create_image' specified, creating image") create_image = True elif params.get("create_image") == "yes" and not os.path.exists(image_filename): create_image = True if create_image and not virt_vm.create_image(params, test.bindir): raise error.TestError("Could not create image")
def preprocess_image(test, params): """ 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. """ image_filename = virt_vm.get_image_filename(params, test.bindir) create_image = False if params.get("force_create_image") == "yes": logging.debug("Param 'force_create_image' specified, creating image") create_image = True elif (params.get("create_image") == "yes" and not os.path.exists(image_filename)): create_image = True if create_image and not virt_vm.create_image(params, test.bindir): raise error.TestError("Could not create image")
def __make_libvirt_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_hvm_or_pv(help, hvm_or_pv): return " %s" % hvm_or_pv 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_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 return cmd def add_floppy(help, filename): return " --disk path=%s,device=floppy,ro" % filename def add_vnc(help, vnc_port): return " --vnc --vncport=%d" % (vnc_port) 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: return "" def add_kernel_cmdline(help, cmdline): return " -append %s" % cmdline def add_connect_uri(help, uri): if has_option(help, "connect"): return " --connect=%s" % uri else: return "" # 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 = virt_utils.get_path( root_dir, params.get("virt_install_binary", "virt-install")) help = utils.system_output("%s --help" % virt_install_binary) # 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) hvm_or_pv = params.get("hvm_or_pv") 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) 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': if params.get("url") == 'auto': location = params.get('auto_content_url') else: 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")) else: location = params.get("image_dir") if location: virt_install_cmd += add_location(help, location) if params.get("display") == "vnc": if params.get("vnc_port"): vm.vnc_port = int(params.get("vnc_port")) virt_install_cmd += add_vnc(help, vm.vnc_port) 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")) # 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 = virt_vm.get_image_filename(image_params, root_dir) if image_params.get("use_storage_pool") == "yes": filename = None if image_params.get("boot_drive") == "no": continue 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 self.driver_type == self.LIBVIRT_QEMU: 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, virt_utils.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. floppy = params.get("floppy") if floppy: floppy = virt_utils.get_path(root_dir, floppy) virt_install_cmd += add_drive(help, floppy, None, None, 'floppy', None, None, None, None, None, None) # FIXME: for now in the pilot always add mac address to virt-install vlan = 0 mac = vm.get_mac_address(vlan) if mac: virt_install_cmd += " --mac %s" % mac self.nic_mac = mac if self.driver_type == self.LIBVIRT_XEN: virt_install_cmd += (" --network=%s" % params.get("virsh_network")) elif self.driver_type == self.LIBVIRT_QEMU: virt_install_cmd += (" --network=%s,model=%s" % (params.get("virsh_network"), params.get("nic_model"))) 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")) extra_params = params.get("extra_params") if extra_params: virt_install_cmd += " --extra-args '%s'" % extra_params virt_install_cmd += " --noautoconsole" return virt_install_cmd
def __make_libvirt_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_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_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 return cmd def add_floppy(help, filename): return " --disk path=%s,device=floppy,ro" % filename def add_vnc(help, vnc_port): return " --vnc --vncport=%d" % (vnc_port) def add_vnclisten(help, vnclisten): return " --vnclisten=%s " % (vnclisten) 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: return "" def add_kernel_cmdline(help, cmdline): return " -append %s" % cmdline def add_connect_uri(help, uri): if has_option(help, "connect"): return " --connect=%s" % uri else: return "" # 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 = virt_utils.get_path( root_dir, params.get("virt_install_binary", "virt-install")) help = utils.system_output("%s --help" % virt_install_binary) # 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) hvm_or_pv = params.get("hvm_or_pv") 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) 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) # libvirt expects --location <path>/images/pxeboot/<vmlinuz|initrd> 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 ((self.driver_type == self.LIBVIRT_XEN) and (params.get('hvm_or_pv') == 'hvm')): virt_install_cmd += add_cdrom(help, params.get("cdrom_unattended")) else: # Fake images/pxeboot using relative symlinks # Assumes kernel and initrd were copied to same dir # TODO: This and cooresponding add_cdrom() in unattended_install test # should be much cleaner. location = os.path.dirname(params.get("kernel")) try: os.symlink(".", os.path.join(location, "images")) os.symlink(".", os.path.join(location, "pxeboot")) except OSError: pass # ignore if already exists if location: virt_install_cmd += add_location(help, location) if params.get("display") == "vnc": if 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")) # 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 = virt_vm.get_image_filename(image_params, root_dir) if image_params.get("use_storage_pool") == "yes": filename = None if image_params.get("boot_drive") == "no": continue 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 self.driver_type == self.LIBVIRT_QEMU: 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, virt_utils.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. floppy = params.get("floppy") if floppy: floppy = virt_utils.get_path(root_dir, floppy) virt_install_cmd += add_drive(help, floppy, None, None, 'floppy', None, None, None, None, None, None) # FIXME: for now in the pilot always add mac address to virt-install vlan = 0 mac = vm.get_mac_address(vlan) if mac: virt_install_cmd += " --mac %s" % mac self.nic_mac = mac if self.driver_type == self.LIBVIRT_XEN: virt_install_cmd += (" --network=%s" % params.get("virsh_network")) elif self.driver_type == self.LIBVIRT_QEMU: virt_install_cmd += ( " --network=%s,model=%s" % (params.get("virsh_network"), params.get("nic_model"))) 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