def create_ext4_runtime(self, outfile="/var/tmp/squashfs.img", compression="xz", compressargs=None, size=2): """Create a squashfs compressed ext4 runtime""" # make live rootfs image - must be named "LiveOS/rootfs.img" for dracut compressargs = compressargs or [] workdir = joinpaths(os.path.dirname(outfile), "runtime-workdir") os.makedirs(joinpaths(workdir, "LiveOS")) # Catch problems with the rootfs being too small and clearly log them try: imgutils.mkrootfsimg(self.vars.root, joinpaths(workdir, "LiveOS/rootfs.img"), "Anaconda", size=size) except CalledProcessError as e: if e.stdout and "No space left on device" in e.stdout: logger.error("The rootfs ran out of space with size=%d", size) raise # squash the live rootfs and clean up workdir imgutils.mksquashfs(workdir, outfile, compression, compressargs) remove(workdir)
def create_squashfs_runtime(self, outfile="/var/tmp/squashfs.img", compression="xz", compressargs=None, size=2): """Create a plain squashfs runtime""" compressargs = compressargs or [] os.makedirs(os.path.dirname(outfile)) # squash the rootfs imgutils.mksquashfs(self.vars.root, outfile, compression, compressargs)
def mksquashfs_test(self): """Test mksquashfs function""" with tempfile.TemporaryDirectory(prefix="lorax.test.") as work_dir: with tempfile.NamedTemporaryFile(prefix="lorax.test.disk.") as disk_img: mkfakerootdir(work_dir) disk_img.close() mksquashfs(work_dir, disk_img.name) self.assertTrue(os.path.exists(disk_img.name)) file_details = get_file_magic(disk_img.name) self.assertTrue("Squashfs" in file_details, file_details)
def create_runtime(self, outfile="/var/tmp/squashfs.img", compression="xz", compressargs=None, size=2): # make live rootfs image - must be named "LiveOS/rootfs.img" for dracut compressargs = compressargs or [] workdir = joinpaths(os.path.dirname(outfile), "runtime-workdir") os.makedirs(joinpaths(workdir, "LiveOS")) imgutils.mkrootfsimg(self.vars.root, joinpaths(workdir, "LiveOS/rootfs.img"), "Anaconda", size=size) # squash the live rootfs and clean up workdir imgutils.mksquashfs(workdir, outfile, compression, compressargs) remove(workdir)
def make_pxe_live(image, image_root, out_dir): """ Use pylorax to generate a SquashFS image with an embedded ext4 rootfs.img used for PXE booting. """ logging.debug('Building PXE Live image...') squashfs_path = out_dir + '/' + image + '-squashfs.img' # Remove any existing squashfs images unlink(squashfs_path) with tempfile.TemporaryDirectory() as tmpdir: os.makedirs(tmpdir + '/LiveOS', mode=0o700, exist_ok=True) imgutils.mkrootfsimg(image_root, tmpdir + '/LiveOS/rootfs.img', size=None, label="LiveOS") imgutils.mksquashfs(tmpdir, squashfs_path, 'xz', []) chown_file(squashfs_path)
def make_squashfs(opts, disk_img, work_dir): """ Create a squashfs image of an unpartitioned filesystem disk image :param str disk_img: Path to the unpartitioned filesystem disk image :param str work_dir: Output compressed image to work_dir+images/install.img :param str compression: Compression type to use :returns: True if squashfs creation was successful. False if there was an error. :rtype: bool Take disk_img and put it into LiveOS/rootfs.img and squashfs this tree into work_dir+images/install.img fsck.ext4 is run on the disk image to make sure there are no errors and to zero out any deleted blocks to make it compress better. If this fails for any reason it will return False and log the error. """ # Make sure free blocks are actually zeroed so it will compress rc = execWithRedirect("/usr/sbin/fsck.ext4", ["-y", "-f", "-E", "discard", disk_img]) if rc != 0: log.error("Problem zeroing free blocks of %s", disk_img) return False liveos_dir = joinpaths(work_dir, "runtime/LiveOS") os.makedirs(liveos_dir) os.makedirs(os.path.dirname(joinpaths(work_dir, RUNTIME))) rc = execWithRedirect( "/bin/ln", [disk_img, joinpaths(liveos_dir, "rootfs.img")]) if rc != 0: shutil.copy2(disk_img, joinpaths(liveos_dir, "rootfs.img")) compression, compressargs = squashfs_args(opts) mksquashfs(joinpaths(work_dir, "runtime"), joinpaths(work_dir, RUNTIME), compression, compressargs) remove(joinpaths(work_dir, "runtime")) return True
def make_live_images(opts, work_dir, disk_img): """ Create live images from direcory or rootfs image :param opts: options passed to livemedia-creator :type opts: argparse options :param str work_dir: Directory for storing results :param str disk_img: Path to disk image (fsimage or partitioned) :returns: Path of directory with created images or None :rtype: str fsck.ext4 is run on the rootfs_image to make sure there are no errors and to zero out any deleted blocks to make it compress better. If this fails for any reason it will return None and log the error. """ sys_root = "" squashfs_root_dir = joinpaths(work_dir, "squashfs_root") liveos_dir = joinpaths(squashfs_root_dir, "LiveOS") os.makedirs(liveos_dir) rootfs_img = joinpaths(liveos_dir, "rootfs.img") if opts.fs_image or opts.no_virt: # Find the ostree root in the fsimage if opts.ostree: with Mount(disk_img, opts="loop") as mnt_dir: sys_root = find_ostree_root(mnt_dir) # Try to hardlink the image, if that fails, copy it rc = execWithRedirect("/bin/ln", [disk_img, rootfs_img]) if rc != 0: shutil.copy2(disk_img, rootfs_img) else: is_root_part = None if opts.ostree: is_root_part = lambda dir: os.path.exists(dir + "/ostree/deploy") with PartitionMount(disk_img, mount_ok=is_root_part) as img_mount: if img_mount and img_mount.mount_dir: try: mounted_sysroot_boot_dir = None if opts.ostree: sys_root = find_ostree_root(img_mount.mount_dir) mounted_sysroot_boot_dir = mount_boot_part_over_root( img_mount) if opts.live_rootfs_keep_size: size = img_mount.mount_size / 1024**3 else: size = opts.live_rootfs_size or None log.info("Creating live rootfs image") mkrootfsimg(img_mount.mount_dir, rootfs_img, "LiveOS", size=size, sysroot=sys_root) finally: if mounted_sysroot_boot_dir: umount(mounted_sysroot_boot_dir) log.debug("sys_root = %s", sys_root) # Make sure free blocks are actually zeroed so it will compress rc = execWithRedirect("/usr/sbin/fsck.ext4", ["-y", "-f", "-E", "discard", rootfs_img]) if rc != 0: log.error("Problem zeroing free blocks of %s", disk_img) return None log.info("Packing live rootfs image") add_pxe_args = [] live_image_name = "live-rootfs.squashfs.img" compression, compressargs = squashfs_args(opts) mksquashfs(squashfs_root_dir, joinpaths(work_dir, live_image_name), compression, compressargs) log.info("Rebuilding initramfs for live") with Mount(rootfs_img, opts="loop") as mnt_dir: try: mount(joinpaths(mnt_dir, "boot"), opts="bind", mnt=joinpaths(mnt_dir, sys_root, "boot")) rebuild_initrds_for_live(opts, joinpaths(mnt_dir, sys_root), work_dir) finally: umount(joinpaths(mnt_dir, sys_root, "boot"), delete=False) remove(squashfs_root_dir) if opts.ostree: add_pxe_args.append("ostree=/%s" % sys_root) template = joinpaths(opts.lorax_templates, "pxe-live/pxe-config.tmpl") create_pxe_config(template, work_dir, live_image_name, add_pxe_args) return work_dir