Exemplo n.º 1
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(
                                description="Fix broken GPT",
                                parents=[log_parser])
    parser.add_argument("image",
                        help="Image file or block device to fix")
    parser.add_argument("--write", action="store_true",
                        help="Write changes without asking")
    parser.add_argument("--dry-run", action="store_true",
                        help="Do not change anything")
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)

    if args.dry_run:
        mode = "rb"
    else:
        mode = "r+b"
    
    with open(args.image, mode) as image_f:
        image_st = os.fstat(image_f.fileno())
        if stat.S_ISBLK(image_st.st_mode):
            logger.debug("{} is a block device, reading its properties"
                                                        .format(args.image))
            uint64_buf = ctypes.c_uint64()
            if fcntl.ioctl(image_f.fileno(), BLKGETSIZE64, uint64_buf) < 0:
                raise IOError("ioctl BLKGETSIZE64 failed")
            image_size = uint64_buf.value
            logger.debug("  device size: {}".format(image_size))
            int_buf = ctypes.c_size_t()
            if fcntl.ioctl(image_f.fileno(), BLKSSZGET, int_buf) < 0:
                raise IOError("ioctl BLKSSZGET failed")
            logger.debug("  block size: {}".format(int_buf.value))
            if int_buf.value != 512:
                logger.warning("{} block size is {}, but this utility"
                        " currently works on 512-bytes blocks only."
                                        .format(args.image, int_buf.value))
        elif stat.S_ISREG(image_st.st_mode):
            image_size = os.fstat(image_f.fileno()).st_size
            logger.debug("image size: {}".format(image_size))
        else:
            logger.error("{} not a block device nor a regular file"
                                                        .format(args.image))
            sys.exit(1)
        if image_size & 0x1ff:
            logger.error("Image size not a multiply of 512!")
            sys.exit(1)
        try:
            primary_gpt = GPT(image_f,
                              image_size=image_size,
                              lba_size=512)
        except GPTError as err:
            logger.error("Could not read GPT at the second 512-bytes sector:"
                            " {}. Other sector sizes not supported yet."
                            .format(err))
            sys.exit(1)

        try:
            backup_gpt = primary_gpt.load_backup()
        except GPTError as err:
            logger.warning(err)

        if (primary_gpt.something_wrong or not backup_gpt
                                            or backup_gpt.something_wrong):
            logger.info("Problems found, will fix that.")
        elif primary_gpt.part_array_size != 128:
            logger.info("Strange partition array size ({}), will fix that.")
        else:
            logger.info("Everything seems OK. Nothing to do.")
            return

        primary_gpt.trim_partition_array()
        backup_gpt = primary_gpt.make_backup()
        
        logger.debug("New primary GPT:\n{}".format(primary_gpt))
        logger.debug("New backup GPT:\n{}".format(backup_gpt))

        if args.dry_run:
            logger.info("Skipping write.")
            return

        if not args.write:
            ans = input("Modify the image [y/N]?")
            if ans.lower() not in ("y", "yes"):
                return
        
        primary_gpt.write()
        backup_gpt.write()
Exemplo n.º 2
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(description="Install packages",
                                     parents=[log_parser])
    parser.add_argument("--no-clean",
                        action="store_true",
                        help="Do not clean up after failed install")
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)

    config = pld_nr_buildconf.Config.get_config()

    lst_files = []

    installer = PackageInstaller(config)
    try:
        installer.init_rpm_db()
        installer.poldek("--upa", ignore_errors=True)
        prev_files = set()
        installer.poldek("--install", "filesystem")
        installer.setup_chroot()
        package_modules = {}
        for module in config.modules:
            if module == "base":
                lst_fn = "base.full-lst"
            else:
                lst_fn = "{0}.lst".format(module)
            lst_files.append(lst_fn)
            logger.debug("Checking if {0!r} already exists".format(lst_fn))
            if os.path.exists(lst_fn):
                files = [l.strip() for l in open(lst_fn, "rt").readlines()]
                prev_files.update(files)
                logger.info("'{0}' packages already installed".format(module))
                open(lst_fn, "a").close()  # update mtime
                continue
            script_fn = "../modules/{0}/pre-install.sh".format(module)
            if os.path.exists(script_fn):
                config.run_script(script_fn, sudo=True)
            pset_fn = "../modules/{0}/deps_workaround.pset".format(module)
            if os.path.exists(pset_fn):
                logger.debug(
                    "Installing deps_workaround packages for {0}".format(
                        module))
                installer.poldek("--install", "--pset", pset_fn, "--nofollow",
                                 "--nodeps", "--pmopt", "noscripts")
            pset_fn = "../modules/{0}/packages.pset".format(module)
            if os.path.exists(pset_fn):
                logger.debug("Installing packages for {0}".format(module))
                installer.poldek("--install", "--pset", pset_fn)
            script_fn = "../modules/{0}/post-install.sh".format(module)
            if os.path.exists(script_fn):
                logger.debug("Running the 'post-install' script")
                config.run_script(script_fn, sudo=True)
            logger.debug("Getting list of installed files")
            files = set(installer.get_file_list())
            module_files = files - prev_files
            prev_files = files
            logger.debug("Writing {0!r}".format(lst_fn))
            with open(lst_fn, "wt") as lst_f:
                for path in sorted(module_files):
                    print(path, file=lst_f)
            module_pkgs = installer.get_installed_pkg_list()
            for pkg in module_pkgs:
                if pkg not in package_modules:
                    package_modules[pkg] = module
        write_package_list("../pld-nr-{}.packages".format(config.bits),
                           installer, config.modules, package_modules)
    except:
        if not args.no_clean:
            installer.cleanup(True)
            for lst_fn in lst_files:
                if os.path.exists(lst_fn):
                    try:
                        os.unlink(lst_fn)
                    except OSError as err:
                        logger.warning(str(err))
        raise
    else:
        installer.cleanup(False)
Exemplo n.º 3
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(
                                description="Make hybrid ISO/GPT boot image",
                                parents=[log_parser])
    parser.add_argument("destination",
                        help="Destination image file name")
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)

    config = pld_nr_buildconf.Config.get_config()

    root_dir = os.path.abspath("root")
    tmp_img_dir = os.path.abspath("tmp_img")
    templ_dir = os.path.abspath("../iso_templ")
    netenv_fn = os.path.abspath("pld-nr-net.env")
    net_files = []
    vmlinuz_fn = os.path.join(root_dir, "boot/vmlinuz")
    while os.path.islink(vmlinuz_fn):
        link_target = os.readlink(vmlinuz_fn)
        link_target = os.path.join("/boot", link_target)
        vmlinuz_fn = os.path.join(root_dir, link_target.lstrip("/"))

    if os.path.exists(args.destination):
        os.unlink(args.destination)

    if os.path.exists(tmp_img_dir):
        shutil.rmtree(tmp_img_dir)

    os.makedirs(tmp_img_dir)
    try:
        logger.debug("Copying ISO contents template")
        config.copy_template_dir(templ_dir, tmp_img_dir)

        logger.debug("Creating the ISO image")
        command = ["xorriso",
                "-report_about", "ALL",
                "-dev", args.destination,
                "-in_charset", "utf-8",
                "-out_charset", "utf-8",
                "-hardlinks", "on",
                "-acl", "off",
                "-xattr", "off",
                "-pathspecs", "on",
                "-uid", "0",
                "-gid", "0",

                "-joliet", "on",
                "-rockridge", "on",
                "-volid", "PLD_NR",
                "-volume_date", "uuid", config.cd_vol_id.replace("-", ""),
                ]
        if config.bios and "i386-pc" in config.grub_platforms:
            # CD boot
            command += ["-add", "/boot/boot.img=boot.img", "--"]
            command += ["-boot_image", "grub", "bin_path=/boot/boot.img"]
            command += ["-boot_image", "grub", "next"]
            
            # HDD boot
            command += ["-boot_image", "any",
                                    "system_area=/lib/grub/i386-pc/boot.img"]
            command += ["-boot_image", "any", "next"]
            # MBR and the image will be patched later

        if config.efi:
            command += ["-add", "/boot/efi.img=efi.img", "--"]
            command += ["-boot_image", "any", "efi_path=/boot/efi.img"]
            command += ["-boot_image", "any", "next"]
            command += ["-boot_image", "any", "efi_boot_part=--efi-boot-image"]
            command += ["-boot_image", "any", "next"]

        command.append("-add")
        for plat in config.grub_platforms:
            src_dir = os.path.join("/lib/grub", plat)
            for path in glob(os.path.join(src_dir, "*")):
                if path.endswith(".module"):
                    continue
                dst_path = "/boot/grub" + path[len("/lib/grub"):]
                command.append("{}={}".format(dst_path, path))
        pld_nr_prefix = "pld-nr-{}".format(config.bits)
        for name in config.initramfs_files:
            command.append("/{0}/{1}={1}".format(pld_nr_prefix, name))
            net_files.append("{}/{}".format(pld_nr_prefix, name))
        for mod in config.modules:
            command.append("/{0}/{1}.cpi={1}.cpi".format(pld_nr_prefix, mod))
            net_files.append("{}/{}.cpi".format(pld_nr_prefix, mod))
        command.append("/{}/vmlinuz={}".format(pld_nr_prefix, vmlinuz_fn))
        net_files.append("{}/vmlinuz".format(pld_nr_prefix))
        if config.efi:
            command.append("/boot/grub/font.pf2=font.pf2")
        command.append("/={}".format(tmp_img_dir))
        if config.memtest86:
            command.append("/boot/memtest86=/boot/memtest86")
            net_files.append("boot/memtest86")
        if config.memtest86_plus:
            command.append("/boot/memtest86_p=/boot/memtest86+")
            net_files.append("boot/memtest86_p")
        for img in config.net_grub_images:
            command.append("/boot/{0}={0}".format(img))
        if config.net_grub_images:
            write_netenv_file(netenv_fn, net_files)
            command.append("/boot/pld-nr-net.env={}".format(netenv_fn))
        command.append("--")

        subprocess.check_call(command)
        
        if config.bios and "i386-pc" in config.grub_platforms:
            patch_image_mbr(config, args.destination)
    finally:
        shutil.rmtree(tmp_img_dir)
Exemplo n.º 4
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(description="Make initramfs",
                                     parents=[log_parser])
    parser.add_argument("--out-list", metavar="FILE",
                        help="Save initramfs contents list to FILE")
    parser.add_argument("--substract-contents", metavar=("INFILE", "OUTFILE"),
                        nargs=2,
                        help="Read file list from INFILE, exclude contents"
                              " of this initramfs module and write to OUTFILE"),
    parser.add_argument("--exclude", metavar="FILE",
                        help="Do not include any files listed in FILE")
    parser.add_argument("name", metavar="NAME",
                        help="Name of the initramfs module."
                            " _NAME.cpi and _NAME.lst files will be written.")
    args = parser.parse_args()
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)
    
    config = pld_nr_buildconf.Config.get_config()

    skel_dir = os.path.abspath("../initramfs/{}.skel".format(args.name))
    root_dir = os.path.abspath("root")
    modules_dir = os.path.abspath("../modules")
    built_skel_dir = os.path.abspath("initramfs")
    out_cpio_fn = os.path.abspath("_{}.cpi".format(args.name))
    files_list_fn = os.path.abspath("../initramfs/{}.files".format(args.name))
    gic_list_fn = os.path.abspath("_{}.gen_init_cpio.list".format(args.name))
    out_lst_fn = os.path.abspath("_{0}.lst".format(args.name))
    if args.substract_contents:
        base_full_lst_fn = os.path.abspath(args.substract_contents[0])
        base_lst_fn = os.path.abspath(args.substract_contents[1])
    else:
        base_full_lst_fn = None
        base_lst_fn = None

    os.chdir(root_dir)

    extra_files = []
    if args.name == "init":
        logger.debug("Looking for module scripts")
        for module in config.modules:
            module_init_fn = os.path.join(modules_dir, module, "init.sh")
            logger.debug("  {}".format(module_init_fn))
            if os.path.exists(module_init_fn):
                logger.debug("    got it")
                extra_files.append("file /.rcd/modules/{}.init {} 0644 0 0"
                                                .format(module, module_init_fn))

    logger.debug("Completing file list")
    files, globs = process_files_list(config, files_list_fn, gic_list_fn,
                                                        root_dir, extra_files)
    subprocess.check_call(["gen_init_cpio", gic_list_fn],
                          stdout=open(out_cpio_fn, "wb"))

    paths = expand_globs(config, globs)
    files += paths

    find_deps(config, paths, files, root_dir)

    paths.sort()

    cpio_append(out_cpio_fn, paths)

    if os.path.exists(built_skel_dir):
        shutil.rmtree(built_skel_dir)
    os.makedirs(built_skel_dir)
    try:
        config.copy_template_dir(skel_dir, built_skel_dir)
        os.chdir(built_skel_dir)
        built_paths = []
        for dirpath, dirnames, filenames in os.walk("."):
            dirpath = dirpath[2:] # strip "./"
            for dirname in dirnames:
                path = os.path.join(dirpath, dirname)
                built_paths.append(path)
            for filename in filenames:
                path = os.path.join(dirpath, filename)
                built_paths.append(path)
        if os.path.exists("init"):
            os.chmod("init", 0o755)
        cpio_append(out_cpio_fn, built_paths)
    finally:
        os.chdir(root_dir)
        shutil.rmtree(built_skel_dir)

    with open(out_lst_fn, "wt") as init_lst:
        for path in sorted(set(paths) | set(files) | set(built_paths)):
            print(path, file=init_lst)

    os.chdir(os.path.dirname(out_lst_fn))
    cpio_append(out_cpio_fn, [os.path.basename(out_lst_fn)])

    if args.substract_contents:
        base_all_paths = set(l.rstrip() for l in
                                    open(base_full_lst_fn, "rt").readlines())
        base_paths = set(base_all_paths) - set(paths)

        with open(base_lst_fn, "wt") as base_lst:
            for path in base_paths:
                print(path, file=base_lst)

    logger.debug("compressing {0!r}".format(out_cpio_fn))
    subprocess.check_call(config.compress_cmd + ["-f", out_cpio_fn])
    compressed_fn = out_cpio_fn + config.compressed_ext
    logger.debug("renaming {0!r} to {1!r}".format(compressed_fn, out_cpio_fn))
    os.rename(compressed_fn, out_cpio_fn)
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(description="Make initramfs",
                                     parents=[log_parser])
    parser.add_argument("--out-list",
                        metavar="FILE",
                        help="Save initramfs contents list to FILE")
    parser.add_argument("--substract-contents",
                        metavar=("INFILE", "OUTFILE"),
                        nargs=2,
                        help="Read file list from INFILE, exclude contents"
                        " of this initramfs module and write to OUTFILE"),
    parser.add_argument("--exclude",
                        metavar="FILE",
                        help="Do not include any files listed in FILE")
    parser.add_argument("name",
                        metavar="NAME",
                        help="Name of the initramfs module."
                        " _NAME.cpi and _NAME.lst files will be written.")
    args = parser.parse_args()
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)

    config = pld_nr_buildconf.Config.get_config()

    skel_dir = os.path.abspath("../initramfs/{}.skel".format(args.name))
    root_dir = os.path.abspath("root")
    modules_dir = os.path.abspath("../modules")
    built_skel_dir = os.path.abspath("initramfs")
    out_cpio_fn = os.path.abspath("_{}.cpi".format(args.name))
    files_list_fn = os.path.abspath("../initramfs/{}.files".format(args.name))
    gic_list_fn = os.path.abspath("_{}.gen_init_cpio.list".format(args.name))
    out_lst_fn = os.path.abspath("_{0}.lst".format(args.name))
    if args.substract_contents:
        base_full_lst_fn = os.path.abspath(args.substract_contents[0])
        base_lst_fn = os.path.abspath(args.substract_contents[1])
    else:
        base_full_lst_fn = None
        base_lst_fn = None

    os.chdir(root_dir)

    extra_files = []
    if args.name == "init":
        logger.debug("Looking for module scripts")
        for module in config.modules:
            module_init_fn = os.path.join(modules_dir, module, "init.sh")
            logger.debug("  {}".format(module_init_fn))
            if os.path.exists(module_init_fn):
                logger.debug("    got it")
                extra_files.append(
                    "file /.rcd/modules/{}.init {} 0644 0 0".format(
                        module, module_init_fn))

    logger.debug("Completing file list")
    files, globs = process_files_list(config, files_list_fn, gic_list_fn,
                                      root_dir, extra_files)
    subprocess.check_call(["gen_init_cpio", gic_list_fn],
                          stdout=open(out_cpio_fn, "wb"))

    paths = expand_globs(config, globs)
    files += paths

    find_deps(config, paths, files, root_dir)

    paths.sort()

    cpio_append(out_cpio_fn, paths)

    if os.path.exists(built_skel_dir):
        shutil.rmtree(built_skel_dir)
    os.makedirs(built_skel_dir)
    try:
        config.copy_template_dir(skel_dir, built_skel_dir)
        os.chdir(built_skel_dir)
        built_paths = []
        for dirpath, dirnames, filenames in os.walk("."):
            dirpath = dirpath[2:]  # strip "./"
            for dirname in dirnames:
                path = os.path.join(dirpath, dirname)
                built_paths.append(path)
            for filename in filenames:
                path = os.path.join(dirpath, filename)
                built_paths.append(path)
        if os.path.exists("init"):
            os.chmod("init", 0o755)
        cpio_append(out_cpio_fn, built_paths)
    finally:
        os.chdir(root_dir)
        shutil.rmtree(built_skel_dir)

    with open(out_lst_fn, "wt") as init_lst:
        for path in sorted(set(paths) | set(files) | set(built_paths)):
            print(path, file=init_lst)

    os.chdir(os.path.dirname(out_lst_fn))
    cpio_append(out_cpio_fn, [os.path.basename(out_lst_fn)])

    if args.substract_contents:
        base_all_paths = set(l.rstrip()
                             for l in open(base_full_lst_fn, "rt").readlines())
        base_paths = set(base_all_paths) - set(paths)

        with open(base_lst_fn, "wt") as base_lst:
            for path in base_paths:
                print(path, file=base_lst)

    logger.debug("compressing {0!r}".format(out_cpio_fn))
    subprocess.check_call(config.compress_cmd + ["-f", out_cpio_fn])
    compressed_fn = out_cpio_fn + config.compressed_ext
    logger.debug("renaming {0!r} to {1!r}".format(compressed_fn, out_cpio_fn))
    os.rename(compressed_fn, out_cpio_fn)
Exemplo n.º 6
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(description="Install packages",
                                     parents=[log_parser])
    parser.add_argument("--no-clean", action="store_true",
                        help="Do not clean up after failed install")
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)

    config = pld_nr_buildconf.Config.get_config()

    lst_files = []

    installer = PackageInstaller(config)
    try:
        installer.init_rpm_db()
        installer.poldek("--upa", ignore_errors=True)
        prev_files = set()
        installer.poldek("--install", "filesystem")
        installer.setup_chroot()
        package_modules = {}
        for module in config.modules:
            if module == "base":
                lst_fn = "base.full-lst"
            else:
                lst_fn = "{0}.lst".format(module)
            lst_files.append(lst_fn)
            logger.debug("Checking if {0!r} already exists".format(lst_fn))
            if os.path.exists(lst_fn):
                files = [l.strip() for l in open(lst_fn, "rt").readlines()]
                prev_files.update(files)
                logger.info("'{0}' packages already installed".format(module))
                open(lst_fn, "a").close() # update mtime
                continue
            script_fn = "../modules/{0}/pre-install.sh".format(module)
            if os.path.exists(script_fn):
                config.run_script(script_fn, sudo=True)
            pset_fn = "../modules/{0}/conds_workaround.pset".format(module)
            if os.path.exists(pset_fn):
                logger.debug("Installing conds_workaround packages for {0}"
                                    .format(module))
                installer.poldek("--install", "--pset", pset_fn,
                            "--nofollow", "--nodeps", "--pmopt", "noscripts")
            pset_fn = "../modules/{0}/packages.pset".format(module)
            if os.path.exists(pset_fn):
                logger.debug("Installing packages for {0}".format(module))
                installer.poldek("--install", "--pset", pset_fn)
            script_fn = "../modules/{0}/post-install.sh".format(module)
            if os.path.exists(script_fn):
                logger.debug("Running the 'post-install' script")
                config.run_script(script_fn, sudo=True)
            logger.debug("Getting list of installed files")
            files = set(installer.get_file_list())
            module_files = files - prev_files
            prev_files = files
            logger.debug("Writing {0!r}".format(lst_fn))
            with open(lst_fn, "wt") as lst_f:
                for path in sorted(module_files):
                    print(path, file=lst_f)
            module_pkgs = installer.get_installed_pkg_list()
            for pkg in module_pkgs:
                if pkg not in package_modules:
                    package_modules[pkg] = module
        write_package_list("../pld-nr-{}.packages".format(config.bits),
                            installer, config.modules, package_modules)
    except:
        if not args.no_clean:
            installer.cleanup(True)
            for lst_fn in lst_files:
                if os.path.exists(lst_fn):
                    try:
                        os.unlink(lst_fn)
                    except OSError as err:
                        logger.warning(str(err))
        raise
    else:
        installer.cleanup(False)
Exemplo n.º 7
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(description="Fix broken GPT",
                                     parents=[log_parser])
    parser.add_argument("image", help="Image file or block device to fix")
    parser.add_argument("--write",
                        action="store_true",
                        help="Write changes without asking")
    parser.add_argument("--dry-run",
                        action="store_true",
                        help="Do not change anything")
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)

    if args.dry_run:
        mode = "rb"
    else:
        mode = "r+b"

    with open(args.image, mode) as image_f:
        image_st = os.fstat(image_f.fileno())
        if stat.S_ISBLK(image_st.st_mode):
            logger.debug("{} is a block device, reading its properties".format(
                args.image))
            uint64_buf = ctypes.c_uint64()
            if fcntl.ioctl(image_f.fileno(), BLKGETSIZE64, uint64_buf) < 0:
                raise IOError("ioctl BLKGETSIZE64 failed")
            image_size = uint64_buf.value
            logger.debug("  device size: {}".format(image_size))
            int_buf = ctypes.c_size_t()
            if fcntl.ioctl(image_f.fileno(), BLKSSZGET, int_buf) < 0:
                raise IOError("ioctl BLKSSZGET failed")
            logger.debug("  block size: {}".format(int_buf.value))
            if int_buf.value != 512:
                logger.warning(
                    "{} block size is {}, but this utility"
                    " currently works on 512-bytes blocks only.".format(
                        args.image, int_buf.value))
        elif stat.S_ISREG(image_st.st_mode):
            image_size = os.fstat(image_f.fileno()).st_size
            logger.debug("image size: {}".format(image_size))
        else:
            logger.error("{} not a block device nor a regular file".format(
                args.image))
            sys.exit(1)
        if image_size & 0x1ff:
            logger.error("Image size not a multiply of 512!")
            sys.exit(1)
        try:
            primary_gpt = GPT(image_f, image_size=image_size, lba_size=512)
        except GPTError as err:
            logger.error(
                "Could not read GPT at the second 512-bytes sector:"
                " {}. Other sector sizes not supported yet.".format(err))
            sys.exit(1)

        try:
            backup_gpt = primary_gpt.load_backup()
        except GPTError as err:
            logger.warning(err)

        if (primary_gpt.something_wrong or not backup_gpt
                or backup_gpt.something_wrong):
            logger.info("Problems found, will fix that.")
        elif primary_gpt.part_array_size != 128:
            logger.info("Strange partition array size ({}), will fix that.")
        else:
            logger.info("Everything seems OK. Nothing to do.")
            return

        primary_gpt.trim_partition_array()
        backup_gpt = primary_gpt.make_backup()

        logger.debug("New primary GPT:\n{}".format(primary_gpt))
        logger.debug("New backup GPT:\n{}".format(backup_gpt))

        if args.dry_run:
            logger.info("Skipping write.")
            return

        if not args.write:
            ans = input("Modify the image [y/N]?")
            if ans.lower() not in ("y", "yes"):
                return

        primary_gpt.write()
        backup_gpt.write()
Exemplo n.º 8
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(description="Make EFI partition image",
                                     parents=[log_parser])
    parser.add_argument("destination",
                        nargs="?",
                        default="efi.img",
                        help="Destination file name")
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)
    
    config = pld_nr_buildconf.Config.get_config()

    efi_img_fn = os.path.abspath(args.destination)
    efi_templ_dir = os.path.abspath("../efi_templ")
    efi_mnt_dir = os.path.abspath("efi_mnt")

    grub_files = {}
    for plat in config.grub_platforms:
        if not plat.endswith("-efi"):
            continue
        img_fn = os.path.abspath("grub-{}.img".format(plat))
        if plat.startswith("i386-"):
            grub_files[img_fn] = "IA32"
        elif plat.startswith("x86_64"):
            grub_files[img_fn] = "X64"
        else:
            logger.warning("Unuspported GRUB EFI platform: {}".format(plat))

    logger.info("Computing required image size")
    extra_files = list(grub_files)

    if config.efi_shell:
        efi_shell_path = "/lib/efi/{}/Shell.efi".format(config.efi_arch)
        extra_files.append(efi_shell_path)

    safe_env = dict(os.environ)
    safe_env["LC_ALL"] = "C"
    du_output = subprocess.check_output(["du", "-sbcD",
                                            "../efi_templ",
                                            ] + extra_files,
                                            env=safe_env)
    match = DU_OUTPUT_RE.search(du_output.decode("utf-8"))
    bytes_needed = int(int(match.group(1)) * 1.2)
    logger.debug("bytes needed: {0!r}".format(bytes_needed))
    blocks_needed = max(bytes_needed // 1024, 256)

    if blocks_needed & 0x0f:
        # so mtools doesn't complain that:
        # Total number of sectors not a multiple of sectors per track (32)!
        blocks_needed = (blocks_needed & 0xfffffff0) + 0x10

    logger.info("Creating the image")
    subprocess.check_call(["dd", "if=/dev/zero", "of=" + efi_img_fn,
                            "bs=1024", "count={0}".format(blocks_needed)])
    try:
        subprocess.check_call(["mkdosfs", "-I",
                                "-i", config.efi_vol_id.replace("-", ""),
                                efi_img_fn])
        if not os.path.exists(efi_mnt_dir):
            os.makedirs(efi_mnt_dir)
        logger.info("Installing PLD NR EFI files")
        subprocess.check_call(["mmd", "-i", efi_img_fn,
                                                "::/EFI", "::/EFI/BOOT"])
        if config.efi_shell:
            subprocess.check_call(["mcopy", "-i", efi_img_fn,
                                    efi_shell_path,
                                    "::/EFI/SHELL{}.EFI".format(
                                                    config.efi_arch.upper())])
        config.mcopy_template_dir(efi_templ_dir, "::/", image=efi_img_fn)
        for source, efi_arch in grub_files.items():
            dst = "::/EFI/BOOT/BOOT{}.EFI".format(efi_arch)
            subprocess.check_call(["mcopy", "-i", efi_img_fn, source, dst])
    except:
        os.unlink(efi_img_fn)
        raise
Exemplo n.º 9
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(
        description="Make hybrid ISO/GPT boot image", parents=[log_parser])
    parser.add_argument("destination", help="Destination image file name")
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)

    config = pld_nr_buildconf.Config.get_config()

    root_dir = os.path.abspath("root")
    tmp_img_dir = os.path.abspath("tmp_img")
    templ_dir = os.path.abspath("../iso_templ")
    netenv_fn = os.path.abspath("pld-nr-net.env")
    net_files = []
    vmlinuz_fn = os.path.join(root_dir, "boot/vmlinuz")
    while os.path.islink(vmlinuz_fn):
        link_target = os.readlink(vmlinuz_fn)
        link_target = os.path.join("/boot", link_target)
        vmlinuz_fn = os.path.join(root_dir, link_target.lstrip("/"))

    if os.path.exists(args.destination):
        os.unlink(args.destination)

    if os.path.exists(tmp_img_dir):
        shutil.rmtree(tmp_img_dir)

    os.makedirs(tmp_img_dir)
    try:
        logger.debug("Copying ISO contents template")
        config.copy_template_dir(templ_dir, tmp_img_dir)

        logger.debug("Creating the ISO image")
        command = [
            "xorriso",
            "-report_about",
            "ALL",
            "-dev",
            args.destination,
            "-in_charset",
            "utf-8",
            "-out_charset",
            "utf-8",
            "-hardlinks",
            "on",
            "-acl",
            "off",
            "-xattr",
            "off",
            "-pathspecs",
            "on",
            "-uid",
            "0",
            "-gid",
            "0",
            "-joliet",
            "on",
            "-rockridge",
            "on",
            "-volid",
            "PLD_NR",
            "-volume_date",
            "uuid",
            config.cd_vol_id.replace("-", ""),
        ]
        if config.bios and "i386-pc" in config.grub_platforms:
            # CD boot
            command += ["-add", "/boot/boot.img=boot.img", "--"]
            command += ["-boot_image", "grub", "bin_path=/boot/boot.img"]
            command += ["-boot_image", "grub", "next"]

            # HDD boot
            command += [
                "-boot_image", "any", "system_area=/lib/grub/i386-pc/boot.img"
            ]
            command += ["-boot_image", "any", "next"]
            # MBR and the image will be patched later

        if config.efi:
            command += ["-add", "/boot/efi.img=efi.img", "--"]
            command += ["-boot_image", "any", "efi_path=/boot/efi.img"]
            command += ["-boot_image", "any", "next"]
            command += ["-boot_image", "any", "efi_boot_part=--efi-boot-image"]
            command += ["-boot_image", "any", "next"]

        command.append("-add")
        for plat in config.grub_platforms:
            src_dir = os.path.join("/lib/grub", plat)
            for path in glob(os.path.join(src_dir, "*")):
                if path.endswith(".module"):
                    continue
                dst_path = "/boot/grub" + path[len("/lib/grub"):]
                command.append("{}={}".format(dst_path, path))
        pld_nr_prefix = "pld-nr-{}".format(config.bits)
        for name in config.initramfs_files:
            command.append("/{0}/{1}={1}".format(pld_nr_prefix, name))
            net_files.append("{}/{}".format(pld_nr_prefix, name))
        for mod in config.modules:
            command.append("/{0}/{1}.cpi={1}.cpi".format(pld_nr_prefix, mod))
            net_files.append("{}/{}.cpi".format(pld_nr_prefix, mod))
        command.append("/{}/vmlinuz={}".format(pld_nr_prefix, vmlinuz_fn))
        net_files.append("{}/vmlinuz".format(pld_nr_prefix))
        if config.efi:
            command.append("/boot/grub/font.pf2=font.pf2")
        command.append("/={}".format(tmp_img_dir))
        if config.memtest86:
            command.append("/boot/memtest86=/boot/memtest86")
            net_files.append("boot/memtest86")
        if config.memtest86_plus:
            command.append("/boot/memtest86_p=/boot/memtest86+")
            net_files.append("boot/memtest86_p")
        for img in config.net_grub_images:
            command.append("/boot/{0}={0}".format(img))
        if config.net_grub_images:
            write_netenv_file(netenv_fn, net_files)
            command.append("/boot/pld-nr-net.env={}".format(netenv_fn))
        command.append("--")

        subprocess.check_call(command)

        if config.bios and "i386-pc" in config.grub_platforms:
            patch_image_mbr(config, args.destination)
    finally:
        shutil.rmtree(tmp_img_dir)
Exemplo n.º 10
0
def main():
    log_parser = pld_nr_buildconf.get_logging_args_parser()
    parser = argparse.ArgumentParser(description="Make boot image",
                                     parents=[log_parser])
    parser.add_argument("platform",
                        default="i386-pc", 
                        help="Grub platform name")
    parser.add_argument("--pxe", action="store_true",
                        help="Build image for PXE boot")
    parser.add_argument("destination",
                        help="Destination file name")
    args = parser.parse_args()
    pld_nr_buildconf.setup_logging(args)
    
    config = pld_nr_buildconf.Config.get_config()

    if args.platform == "i386-efi":
        efi_arch = "ia32"
    elif args.platform == "x86_64-efi":
        efi_arch = "x64"
    else:
        efi_arch = ""

    with tempfile.NamedTemporaryFile(mode="w+t") as grub_early:
        if args.pxe:
# net_get_dhcp_option net_default_server pxe:dhcp 66 string
            grub_early.write("""
set pldnr_prefix=/pld-nr
set netboot=yes
set prefix=($root)$pldnr_prefix/boot/grub
load_env -f ($root)/pld-nr-net.env
echo "using pldnr_prefix: $pldnr_prefix prefix: $prefix"
""")
        else:
            grub_early.write("""
echo "starting grub ({platform})"
search.fs_uuid {vol_id} root
search.fs_uuid {efi_id} efi_part
set pldnr_prefix=
set prefix=($root)/boot/grub
set efi_suffix={efi_suffix}
echo "using prefix: $prefix efi_part: $efi_part $efi_suffix"
"""
                     .format(platform=args.platform,
                             vol_id=config.cd_vol_id,
                             efi_id=config.efi_vol_id,
                             efi_suffix=efi_arch.upper()))
        grub_early.flush()
        platform = args.platform
        grub_core_modules = ["minicmd", "echo"]
        if args.pxe:
            if args.platform.endswith("-pc"):
                platform += "-pxe"
                grub_core_modules += ["pxe"]
            else:
                grub_core_modules += ["efinet"]
            grub_core_modules += ["tftp", "loadenv"]
            prefix = "/pld-nr/boot/grub"
        else:
            if args.platform.endswith("-pc"):
                grub_core_modules += ["biosdisk"]
            grub_core_modules += ["iso9660", "search", "search_label",
                                    "fat", "part_gpt", "iso9660"]
            prefix = "/boot/grub"
        logger.debug("Making {} grub image for {} with modules: {!r}"
                        .format(args.destination, args.platform,
                                                    grub_core_modules))
        subprocess.check_call(["grub-mkimage",
                                "--output", args.destination,
                                "--format", platform,
                                "--prefix", prefix,
                                "--config", grub_early.name,
                                ] + grub_core_modules)