def append_initrd(initrd, files): """ Append files to an initrd. :param str initrd: Path to initrd :param list files: list of file paths to add :returns: Path to a new initrd :rtype: str The files are added to the initrd by creating a cpio image of the files (stored at /) and writing the cpio to the end of a copy of the initrd. The initrd is not changed, a copy is made before appending the cpio archive. """ qemu_initrd = tempfile.mktemp(prefix="lmc-initrd-", suffix=".img") shutil.copy2(initrd, qemu_initrd) ks_dir = tempfile.mkdtemp(prefix="lmc-ksdir-") for ks in files: shutil.copy2(ks, ks_dir) ks_initrd = tempfile.mktemp(prefix="lmc-ks-", suffix=".img") mkcpio(ks_dir, ks_initrd) shutil.rmtree(ks_dir) with open(qemu_initrd, "ab") as initrd_fp: with open(ks_initrd, "rb") as ks_fp: while True: data = ks_fp.read(1024**2) if not data: break initrd_fp.write(data) os.unlink(ks_initrd) return qemu_initrd
def mkcpio_test(self): """Test mkcpio function""" with tempfile.TemporaryDirectory(prefix="lorax.test.") as work_dir: with tempfile.NamedTemporaryFile(prefix="lorax.test.disk.") as disk_img: mkfakerootdir(work_dir) mkcpio(work_dir, disk_img.name, compression=None) self.assertTrue(os.path.exists(disk_img.name)) file_details = get_file_magic(disk_img.name) self.assertTrue("cpio" in file_details, file_details)
def installimg(self, srcdir, destfile): ''' installimg SRCDIR DESTFILE Create a compressed cpio archive of the contents of SRCDIR and place it in DESTFILE. If SRCDIR doesn't exist or is empty nothing is created. Examples: installimg ${LORAXDIR}/product/ images/product.img installimg ${LORAXDIR}/updates/ images/updates.img ''' if not os.path.isdir(self._in(srcdir)) or not os.listdir(self._in(srcdir)): return logger.info("Creating image file %s from contents of %s", self._out(destfile), self._in(srcdir)) mkcpio(self._in(srcdir), self._out(destfile))
def installimg(self, *args): ''' installimg [--xz|--gzip|--bzip2|--lzma] [-ARG|--ARG=OPTION] SRCDIR DESTFILE Create a compressed cpio archive of the contents of SRCDIR and place it in DESTFILE. If SRCDIR doesn't exist or is empty nothing is created. Examples: installimg ${LORAXDIR}/product/ images/product.img installimg ${LORAXDIR}/updates/ images/updates.img installimg --xz -6 ${LORAXDIR}/updates/ images/updates.img installimg --xz -9 --memlimit-compress=3700MiB ${LORAXDIR}/updates/ images/updates.img Optionally use a different compression type and override the default args passed to it. The default is xz -9 ''' COMPRESSORS = ("--xz", "--gzip", "--bzip2", "--lzma") if len(args) < 2: raise ValueError("Not enough args for installimg.") srcdir = args[-2] destfile = args[-1] if not os.path.isdir(self._in(srcdir)) or not os.listdir( self._in(srcdir)): return compression = "xz" compressargs = [] if args[0] in COMPRESSORS: compression = args[0][2:] for arg in args[1:-2]: if arg.startswith('-'): compressargs.append(arg) else: raise ValueError("Argument is missing -") logger.info("Creating image file %s from contents of %s", self._out(destfile), self._in(srcdir)) logger.debug("Using %s %s compression", compression, compressargs or "") mkcpio(self._in(srcdir), self._out(destfile), compression=compression, compressargs=compressargs)
def installimg(self, *args): ''' installimg [--xz|--gzip|--bzip2|--lzma] [-ARG|--ARG=OPTION] SRCDIR DESTFILE Create a compressed cpio archive of the contents of SRCDIR and place it in DESTFILE. If SRCDIR doesn't exist or is empty nothing is created. Examples: installimg ${LORAXDIR}/product/ images/product.img installimg ${LORAXDIR}/updates/ images/updates.img installimg --xz -6 ${LORAXDIR}/updates/ images/updates.img installimg --xz -9 --memlimit-compress=3700MiB ${LORAXDIR}/updates/ images/updates.img Optionally use a different compression type and override the default args passed to it. The default is xz -9 ''' COMPRESSORS = ("--xz", "--gzip", "--bzip2", "--lzma") if len(args) < 2: raise ValueError("Not enough args for installimg.") srcdir = args[-2] destfile = args[-1] if not os.path.isdir(self._in(srcdir)) or not os.listdir(self._in(srcdir)): return compression = "xz" compressargs = [] if args[0] in COMPRESSORS: compression = args[0][2:] for arg in args[1:-2]: if arg.startswith('-'): compressargs.append(arg) else: raise ValueError("Argument is missing -") logger.info("Creating image file %s from contents of %s", self._out(destfile), self._in(srcdir)) logger.debug("Using %s %s compression", compression, compressargs or "") mkcpio(self._in(srcdir), self._out(destfile), compression=compression, compressargs=compressargs)