def add_disk(self, disk_type, device_type, disk, qemu_type="raw"): """ Add a disk to the libvirt file :param disk_type: A string containing the type of disk (file, block) :param device_type: A string containing the type of device (disk, cdrom) :param disk: The path to the disk :param qemu_type: A string containing the format of disk (e.g., raw) :return: """ xml_type = "%s_%s" % (disk_type, device_type) if device_type in self.disk_ids: self.disk_ids[device_type] += 1 else: self.disk_ids[device_type] = ord('a') f = os.path.join( self.config_dir, "%s-%s-%s" % (disk_type, device_type, LibvirtFile.TEMPLATE_FILE)) if not os.path.exists(f): cziso.abort("Unable to find libvirt file %s" % f) values = { 'id': chr(self.disk_ids[device_type]), xml_type: os.path.realpath(disk), 'qemu_type': qemu_type } self.disk_xmls.append(cziso.fill_template(f, **values))
def set_interface(self, iface): """ Add an interface to the libvirt file :param iface: The local interface that you want this vM to be bridged to """ f = os.path.join(self.config_dir, "iface-%s" % LibvirtFile.TEMPLATE_FILE) self.iface_xml = cziso.fill_template(f, iface=iface)
def get_xml(self): """ Return the virt file XML :return: A string containing the libvirt file """ return cziso.fill_template(self.libvirt, vm_name=self.name, disks="\n".join(self.disk_xmls), interface=self.iface_xml)
def restore_clonezilla_iso(self, iso_file, image): """ Restpre a Clonezilla VM ISO file :param iso_file: Path to Clonezilla ISO file to restore :param image: Destination for restored image of type Image :return: Returns if successful; otherwise aborts """ self.logger.info("Restoring image %s to image %s" % (iso_file, image)) if not os.path.exists(iso_file): cziso.abort("ISO file %s does not exist" % iso_file) # launch Clonezilla libvirt_file = cziso.virtualmachine.LibvirtFile(self.config.config_dir) libvirt_file.add_disk("file", "cdrom", iso_file) image.add_to_libvirt(libvirt_file) vm = cziso.virtualmachine.VM() status = vm.launch(libvirt_file.get_xml()) if status != 0: cziso.abort("Unable to launch Clonezilla Live VM") # run restore iso script expect_path = cziso.fill_template(self.restore_expect, tmp_dir=self.temp_dir, vm_name=libvirt_file.get_name()) self.logger.info("""Running restore expect script -- it may take a few mins to boot the Clonezilla Live VM before you see any output""") subprocess.call("expect %s" % expect_path, shell=True) # cleanup vm.clean() image.unmount() os.remove(expect_path) self.logger.info("Restored image %s is now ready" % image)
def convert_to_clonezilla_iso(self, image, out_dir, network): """ Create a Clonezilla ISO file from specified image :param image: Path to raw image file to convert :param ip: IP address to use for Clonezilla VM (default: from Rocks) :param netmask: Netmask to use for Clonezilla VM (default: from Rocks) :return: Returns if successful; otherwise aborts """ if not image.exists(): cziso.abort("Image file %s does not exist" % image) if out_dir is not None and not os.path.exists(out_dir): cziso.abort("Output directory %s does not exist" % out_dir) self.logger.info("Converting image %s to iso" % image) # mount raw image and check it if not image.mount(): cziso.abort("Unable to mount input image %s" % image) image.fsck() image.unmount() # mount temp directory to place iso when complete tmp = self.create_temp_directory() ip, netmask = None, None if network is None: ip, netmask = cziso.get_free_ip(self.priv_interface) else: ip, netmask = network.split(":") if netmask is None or ip is None: cziso.abort("Unable to create a NFS export. No ip or netmask") cziso.create_nfs_export(tmp, ip) # check that we don't overwrite an existing ISO file generated_iso_filename = Clonezilla.get_cz_restore_iso_filename(image) generated_iso_path = os.path.join(tmp, generated_iso_filename) # insert the disk size into the file name new_iso_filename = Clonezilla.get_cziso_restore_iso_filename(image) candidate_dst_file = os.path.join(self.temp_dir, new_iso_filename) if out_dir is not None: candidate_dst_file = os.path.join(out_dir, new_iso_filename) dst_file = cziso.increment_filename(candidate_dst_file) # launch Clonezilla libvirt_file = cziso.virtualmachine.LibvirtFile(self.config.config_dir) libvirt_file.add_disk("file", "cdrom", self.clonezilla_custom.get_or_download()) image.add_to_libvirt(libvirt_file) libvirt_file.set_interface(self.priv_interface) vm = cziso.virtualmachine.VM() status = vm.launch(libvirt_file.get_xml()) if status != 0: cziso.abort("Unable to launch Clonezilla Live VM") # run create iso script expect_path = cziso.fill_template(self.create_expect, tmp_dir=tmp, temp_dir=tmp, vm_name=libvirt_file.get_name(), ip=ip, netmask=netmask, vm_id=image.get_image_id()) self.logger.info( """Running expect script to execute gen-rec-iso script -- it may take a few mins to boot the Clonezilla Live VM before you see any output""") subprocess.call("expect %s" % expect_path, shell=True) if os.path.exists(generated_iso_path): self.logger.debug("Moving ISO file %s to %s" % (generated_iso_path, dst_file)) os.rename(generated_iso_path, dst_file) self.logger.info("Clonezilla restore ISO file is now ready at %s" % dst_file) else: self.logger.error("Clonezilla did not generate ISO file") # cleanup vm.clean() cziso.remove_nfs_export(tmp, ip) shutil.rmtree(tmp) image.unmount()