def create_pxe_config(template, images_dir, live_image_name, add_args=None): """ Create template for pxe to live configuration :param str images_dir: Path of directory with images to be used :param str live_image_name: Name of live rootfs image file :param list add_args: Arguments to be added to initrd= pxe config """ add_args = add_args or [] kernels = [ kernel for kernel in findkernels(images_dir, kdir="") if hasattr(kernel, "initrd") ] if not kernels: return kernel = kernels[0] add_args_str = " ".join(add_args) try: result = Template(filename=template).render(kernel=kernel.path, initrd=kernel.initrd.path, liveimg=live_image_name, addargs=add_args_str) except Exception: log.error(text_error_template().render()) raise with open(joinpaths(images_dir, "PXE_CONFIG"), "w") as f: f.write(result)
def rebuild_initrds_for_live(opts, sys_root_dir, results_dir): """ Rebuild intrds for pxe live image (root=live:http://) :param opts: options passed to livemedia-creator :type opts: argparse options :param str sys_root_dir: Path to root of the system :param str results_dir: Path of directory for storing results """ # cmdline dracut args override the defaults, but need to be parsed log.info("dracut args = %s", dracut_args(opts)) dracut = ["dracut", "--nomdadmconf", "--nolvmconf"] + dracut_args(opts) kdir = "boot" if opts.ostree: kernels_dir = glob.glob(joinpaths(sys_root_dir, "boot/ostree/*")) if kernels_dir: kdir = os.path.relpath(kernels_dir[0], sys_root_dir) kernels = [kernel for kernel in findkernels(sys_root_dir, kdir)] if not kernels: raise Exception("No initrds found, cannot rebuild_initrds") if opts.ostree: # Dracut assumes to have some dirs in disk image # /var/tmp for temp files vartmp_dir = joinpaths(sys_root_dir, "var/tmp") if not os.path.isdir(vartmp_dir): os.mkdir(vartmp_dir) # /root (maybe not fatal) root_dir = joinpaths(sys_root_dir, "var/roothome") if not os.path.isdir(root_dir): os.mkdir(root_dir) # /tmp (maybe not fatal) tmp_dir = joinpaths(sys_root_dir, "sysroot/tmp") if not os.path.isdir(tmp_dir): os.mkdir(tmp_dir) # Write the new initramfs directly to the results directory os.mkdir(joinpaths(sys_root_dir, "results")) mount(results_dir, opts="bind", mnt=joinpaths(sys_root_dir, "results")) # Dracut runs out of space inside the minimal rootfs image mount("/var/tmp", opts="bind", mnt=joinpaths(sys_root_dir, "var/tmp")) for kernel in kernels: if hasattr(kernel, "initrd"): outfile = os.path.basename(kernel.initrd.path) else: # Construct an initrd from the kernel name outfile = os.path.basename(kernel.path.replace("vmlinuz-", "initrd-") + ".img") log.info("rebuilding %s", outfile) log.info("dracut warnings about /proc are safe to ignore") kver = kernel.version cmd = dracut + ["/results/"+outfile, kver] runcmd(cmd, root=sys_root_dir) shutil.copy2(joinpaths(sys_root_dir, kernel.path), results_dir) umount(joinpaths(sys_root_dir, "var/tmp"), delete=False) umount(joinpaths(sys_root_dir, "results"), delete=False)
def get_arch(mount_dir): """ Get the kernel arch :returns: Arch of first kernel found at mount_dir/boot/ or i386 :rtype: str """ kernels = findkernels(mount_dir) if not kernels: return "i386" return kernels[0].arch