def mkfsimage_from_disk_test(self): """Test creating a fsimage from the / partition of a disk image""" with tempfile.NamedTemporaryFile(prefix="lorax.test.disk.") as disk_img: self.assertTrue(mkfakediskimg(disk_img.name)) with tempfile.NamedTemporaryFile(prefix="lorax.test.disk.") as fs_img: mkfsimage_from_disk(disk_img.name, fs_img.name) self.assertTrue(os.path.exists(fs_img.name)) file_details = get_file_magic(fs_img.name) self.assertTrue("ext2 filesystem" in file_details, file_details)
def virt_install(opts, install_log, disk_img, disk_size, cancel_func=None): """ Use qemu to install to a disk image :param opts: options passed to livemedia-creator :type opts: argparse options :param str install_log: The path to write the log from qemu :param str disk_img: The full path to the disk image to be created :param int disk_size: The size of the disk_img in MiB :param cancel_func: Function that returns True to cancel build :type cancel_func: function This uses qemu with a boot.iso and a kickstart to create a disk image and then optionally, based on the opts passed, creates tarfile. """ iso_mount = IsoMountpoint(opts.iso, opts.location) if not iso_mount.stage2: iso_mount.umount() raise InstallError("ISO is missing stage2, cannot continue") log_monitor = LogMonitor(install_log, timeout=opts.timeout) cancel_funcs = [log_monitor.server.log_check] if cancel_func is not None: cancel_funcs.append(cancel_func) kernel_args = "" if opts.kernel_args: kernel_args += opts.kernel_args if opts.proxy: kernel_args += " proxy=" + opts.proxy if opts.image_type and not opts.make_fsimage: qemu_args = [] for arg in opts.qemu_args: qemu_args += arg.split(" ", 1) if "-f" not in qemu_args: qemu_args += ["-f", opts.image_type] mkqemu_img(disk_img, disk_size * 1024**2, qemu_args) if opts.make_fsimage or opts.make_tar or opts.make_oci: diskimg_path = tempfile.mktemp(prefix="lmc-disk-", suffix=".img") else: diskimg_path = disk_img try: QEMUInstall(opts, iso_mount, opts.ks, diskimg_path, disk_size, kernel_args, opts.ram, opts.vcpus, opts.vnc, opts.arch, cancel_func=lambda: any(f() for f in cancel_funcs), virtio_host=log_monitor.host, virtio_port=log_monitor.port, image_type=opts.image_type, boot_uefi=opts.virt_uefi, ovmf_path=opts.ovmf_path) log_monitor.shutdown() except InstallError as e: log.error("VirtualInstall failed: %s", e) raise finally: log.info("unmounting the iso") iso_mount.umount() if log_monitor.server.log_check(): if not log_monitor.server.error_line and opts.timeout: msg = "virt_install failed due to timeout" else: msg = "virt_install failed on line: %s" % log_monitor.server.error_line raise InstallError(msg) elif cancel_func and cancel_func(): raise InstallError("virt_install canceled by cancel_func") if opts.make_fsimage: mkfsimage_from_disk(diskimg_path, disk_img, disk_size, label=opts.fs_label) os.unlink(diskimg_path) elif opts.make_tar: compress_args = [] for arg in opts.compress_args: compress_args += arg.split(" ", 1) with PartitionMount(diskimg_path) as img_mount: if img_mount and img_mount.mount_dir: rc = mktar(img_mount.mount_dir, disk_img, opts.compression, compress_args) else: rc = 1 os.unlink(diskimg_path) if rc: raise InstallError("virt_install failed") elif opts.make_oci: # An OCI image places the filesystem under /rootfs/ and adds the json files at the top # And then creates a tar of the whole thing. compress_args = [] for arg in opts.compress_args: compress_args += arg.split(" ", 1) with PartitionMount(diskimg_path, submount="rootfs") as img_mount: if img_mount and img_mount.temp_dir: shutil.copy2(opts.oci_config, img_mount.temp_dir) shutil.copy2(opts.oci_runtime, img_mount.temp_dir) rc = mktar(img_mount.temp_dir, disk_img, opts.compression, compress_args) else: rc = 1 os.unlink(diskimg_path) if rc: raise InstallError("virt_install failed") elif opts.make_vagrant: compress_args = [] for arg in opts.compress_args: compress_args += arg.split(" ", 1) vagrant_dir = tempfile.mkdtemp(prefix="lmc-tmpdir-") metadata_path = joinpaths(vagrant_dir, "metadata.json") execWithRedirect( "mv", ["-f", disk_img, joinpaths(vagrant_dir, "box.img")], raise_err=True) if opts.vagrant_metadata: shutil.copy2(opts.vagrant_metadata, metadata_path) else: create_vagrant_metadata(metadata_path) update_vagrant_metadata(metadata_path, disk_size) if opts.vagrantfile: shutil.copy2(opts.vagrantfile, joinpaths(vagrant_dir, "vagrantfile")) rc = mktar(vagrant_dir, disk_img, opts.compression, compress_args, selinux=False) if rc: raise InstallError("virt_install failed") shutil.rmtree(vagrant_dir)