From 26694f2009f5df3a740c128fa55f3ed08769ff1e Mon Sep 17 00:00:00 2001 From: Anibal Limon Date: Sat, 23 Mar 2024 13:53:04 -0600 Subject: wic: plugins source bootimage/isoimage rename to allow be imported Python not support importing modules with - so change to _. (From OE-Core rev: 2de444fc3ef450f45f8f93403544e8f7461657b0) Signed-off-by: Anibal Limon Signed-off-by: Richard Purdie --- .../lib/wic/plugins/source/bootimg-biosplusefi.py | 213 ---------- scripts/lib/wic/plugins/source/bootimg-efi.py | 435 ------------------- .../lib/wic/plugins/source/bootimg-partition.py | 162 ------- scripts/lib/wic/plugins/source/bootimg-pcbios.py | 209 ---------- .../lib/wic/plugins/source/bootimg_biosplusefi.py | 213 ++++++++++ scripts/lib/wic/plugins/source/bootimg_efi.py | 435 +++++++++++++++++++ .../lib/wic/plugins/source/bootimg_partition.py | 162 +++++++ scripts/lib/wic/plugins/source/bootimg_pcbios.py | 209 ++++++++++ .../lib/wic/plugins/source/isoimage-isohybrid.py | 463 --------------------- .../lib/wic/plugins/source/isoimage_isohybrid.py | 463 +++++++++++++++++++++ 10 files changed, 1482 insertions(+), 1482 deletions(-) delete mode 100644 scripts/lib/wic/plugins/source/bootimg-biosplusefi.py delete mode 100644 scripts/lib/wic/plugins/source/bootimg-efi.py delete mode 100644 scripts/lib/wic/plugins/source/bootimg-partition.py delete mode 100644 scripts/lib/wic/plugins/source/bootimg-pcbios.py create mode 100644 scripts/lib/wic/plugins/source/bootimg_biosplusefi.py create mode 100644 scripts/lib/wic/plugins/source/bootimg_efi.py create mode 100644 scripts/lib/wic/plugins/source/bootimg_partition.py create mode 100644 scripts/lib/wic/plugins/source/bootimg_pcbios.py delete mode 100644 scripts/lib/wic/plugins/source/isoimage-isohybrid.py create mode 100644 scripts/lib/wic/plugins/source/isoimage_isohybrid.py (limited to 'scripts/lib') diff --git a/scripts/lib/wic/plugins/source/bootimg-biosplusefi.py b/scripts/lib/wic/plugins/source/bootimg-biosplusefi.py deleted file mode 100644 index 5bd7390680..0000000000 --- a/scripts/lib/wic/plugins/source/bootimg-biosplusefi.py +++ /dev/null @@ -1,213 +0,0 @@ -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License version 2 as -# published by the Free Software Foundation. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License along -# with this program; if not, write to the Free Software Foundation, Inc., -# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. -# -# DESCRIPTION -# This implements the 'bootimg-biosplusefi' source plugin class for 'wic' -# -# AUTHORS -# William Bourque - -import types - -from wic.pluginbase import SourcePlugin -from importlib.machinery import SourceFileLoader - -class BootimgBiosPlusEFIPlugin(SourcePlugin): - """ - Create MBR + EFI boot partition - - This plugin creates a boot partition that contains both - legacy BIOS and EFI content. It will be able to boot from both. - This is useful when managing PC fleet with some older machines - without EFI support. - - Note it is possible to create an image that can boot from both - legacy BIOS and EFI by defining two partitions : one with arg - --source bootimg-efi and another one with --source bootimg-pcbios. - However, this method has the obvious downside that it requires TWO - partitions to be created on the storage device. - Both partitions will also be marked as "bootable" which does not work on - most BIOS, has BIOS often uses the "bootable" flag to determine - what to boot. If you have such a BIOS, you need to manually remove the - "bootable" flag from the EFI partition for the drive to be bootable. - Having two partitions also seems to confuse wic : the content of - the first partition will be duplicated into the second, even though it - will not be used at all. - - Also, unlike "isoimage-isohybrid" that also does BIOS and EFI, this plugin - allows you to have more than only a single rootfs partitions and does - not turn the rootfs into an initramfs RAM image. - - This plugin is made to put everything into a single /boot partition so it - does not have the limitations listed above. - - The plugin is made so it does tries not to reimplement what's already - been done in other plugins; as such it imports "bootimg-pcbios" - and "bootimg-efi". - Plugin "bootimg-pcbios" is used to generate legacy BIOS boot. - Plugin "bootimg-efi" is used to generate the UEFI boot. Note that it - requires a --sourceparams argument to know which loader to use; refer - to "bootimg-efi" code/documentation for the list of loader. - - Imports are handled with "SourceFileLoader" from importlib as it is - otherwise very difficult to import module that has hyphen "-" in their - filename. - The SourcePlugin() methods used in the plugins (do_install_disk, - do_configure_partition, do_prepare_partition) are then called on both, - beginning by "bootimg-efi". - - Plugin options, such as "--sourceparams" can still be passed to a - plugin, as long they does not cause issue in the other plugin. - - Example wic configuration: - part /boot --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\\ - --ondisk sda --label os_boot --active --align 1024 --use-uuid - """ - - name = 'bootimg-biosplusefi' - - __PCBIOS_MODULE_NAME = "bootimg-pcbios" - __EFI_MODULE_NAME = "bootimg-efi" - - __imgEFIObj = None - __imgBiosObj = None - - @classmethod - def __init__(cls): - """ - Constructor (init) - """ - - # XXX - # For some reasons, __init__ constructor is never called. - # Something to do with how pluginbase works? - cls.__instanciateSubClasses() - - @classmethod - def __instanciateSubClasses(cls): - """ - - """ - - # Import bootimg-pcbios (class name "BootimgPcbiosPlugin") - modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), - cls.__PCBIOS_MODULE_NAME + ".py") - loader = SourceFileLoader(cls.__PCBIOS_MODULE_NAME, modulePath) - mod = types.ModuleType(loader.name) - loader.exec_module(mod) - cls.__imgBiosObj = mod.BootimgPcbiosPlugin() - - # Import bootimg-efi (class name "BootimgEFIPlugin") - modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), - cls.__EFI_MODULE_NAME + ".py") - loader = SourceFileLoader(cls.__EFI_MODULE_NAME, modulePath) - mod = types.ModuleType(loader.name) - loader.exec_module(mod) - cls.__imgEFIObj = mod.BootimgEFIPlugin() - - @classmethod - def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, - bootimg_dir, kernel_dir, native_sysroot): - """ - Called after all partitions have been prepared and assembled into a - disk image. - """ - - if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): - cls.__instanciateSubClasses() - - cls.__imgEFIObj.do_install_disk( - disk, - disk_name, - creator, - workdir, - oe_builddir, - bootimg_dir, - kernel_dir, - native_sysroot) - - cls.__imgBiosObj.do_install_disk( - disk, - disk_name, - creator, - workdir, - oe_builddir, - bootimg_dir, - kernel_dir, - native_sysroot) - - @classmethod - def do_configure_partition(cls, part, source_params, creator, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - native_sysroot): - """ - Called before do_prepare_partition() - """ - - if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): - cls.__instanciateSubClasses() - - cls.__imgEFIObj.do_configure_partition( - part, - source_params, - creator, - cr_workdir, - oe_builddir, - bootimg_dir, - kernel_dir, - native_sysroot) - - cls.__imgBiosObj.do_configure_partition( - part, - source_params, - creator, - cr_workdir, - oe_builddir, - bootimg_dir, - kernel_dir, - native_sysroot) - - @classmethod - def do_prepare_partition(cls, part, source_params, creator, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - rootfs_dir, native_sysroot): - """ - Called to do the actual content population for a partition i.e. it - 'prepares' the partition to be incorporated into the image. - """ - - if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): - cls.__instanciateSubClasses() - - cls.__imgEFIObj.do_prepare_partition( - part, - source_params, - creator, - cr_workdir, - oe_builddir, - bootimg_dir, - kernel_dir, - rootfs_dir, - native_sysroot) - - cls.__imgBiosObj.do_prepare_partition( - part, - source_params, - creator, - cr_workdir, - oe_builddir, - bootimg_dir, - kernel_dir, - rootfs_dir, - native_sysroot) diff --git a/scripts/lib/wic/plugins/source/bootimg-efi.py b/scripts/lib/wic/plugins/source/bootimg-efi.py deleted file mode 100644 index 38da5080fb..0000000000 --- a/scripts/lib/wic/plugins/source/bootimg-efi.py +++ /dev/null @@ -1,435 +0,0 @@ -# -# Copyright (c) 2014, Intel Corporation. -# -# SPDX-License-Identifier: GPL-2.0-only -# -# DESCRIPTION -# This implements the 'bootimg-efi' source plugin class for 'wic' -# -# AUTHORS -# Tom Zanussi -# - -import logging -import os -import tempfile -import shutil -import re - -from glob import glob - -from wic import WicError -from wic.engine import get_custom_config -from wic.pluginbase import SourcePlugin -from wic.misc import (exec_cmd, exec_native_cmd, - get_bitbake_var, BOOTDD_EXTRA_SPACE) - -logger = logging.getLogger('wic') - -class BootimgEFIPlugin(SourcePlugin): - """ - Create EFI boot partition. - This plugin supports GRUB 2 and systemd-boot bootloaders. - """ - - name = 'bootimg-efi' - - @classmethod - def _copy_additional_files(cls, hdddir, initrd, dtb): - bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") - if not bootimg_dir: - raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") - - if initrd: - initrds = initrd.split(';') - for rd in initrds: - cp_cmd = "cp -v -p %s/%s %s" % (bootimg_dir, rd, hdddir) - out = exec_cmd(cp_cmd, True) - logger.debug("initrd files:\n%s" % (out)) - else: - logger.debug("Ignoring missing initrd") - - if dtb: - if ';' in dtb: - raise WicError("Only one DTB supported, exiting") - cp_cmd = "cp -v -p %s/%s %s" % (bootimg_dir, dtb, hdddir) - out = exec_cmd(cp_cmd, True) - logger.debug("dtb files:\n%s" % (out)) - - @classmethod - def do_configure_grubefi(cls, hdddir, creator, cr_workdir, source_params): - """ - Create loader-specific (grub-efi) config - """ - configfile = creator.ks.bootloader.configfile - custom_cfg = None - if configfile: - custom_cfg = get_custom_config(configfile) - if custom_cfg: - # Use a custom configuration for grub - grubefi_conf = custom_cfg - logger.debug("Using custom configuration file " - "%s for grub.cfg", configfile) - else: - raise WicError("configfile is specified but failed to " - "get it from %s." % configfile) - - initrd = source_params.get('initrd') - dtb = source_params.get('dtb') - - cls._copy_additional_files(hdddir, initrd, dtb) - - if not custom_cfg: - # Create grub configuration using parameters from wks file - bootloader = creator.ks.bootloader - title = source_params.get('title') - - grubefi_conf = "" - grubefi_conf += "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n" - grubefi_conf += "default=boot\n" - grubefi_conf += "timeout=%s\n" % bootloader.timeout - grubefi_conf += "menuentry '%s'{\n" % (title if title else "boot") - - kernel = get_bitbake_var("KERNEL_IMAGETYPE") - if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": - if get_bitbake_var("INITRAMFS_IMAGE"): - kernel = "%s-%s.bin" % \ - (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) - - label = source_params.get('label') - label_conf = "root=%s" % creator.rootdev - if label: - label_conf = "LABEL=%s" % label - - grubefi_conf += "linux /%s %s rootwait %s\n" \ - % (kernel, label_conf, bootloader.append) - - if initrd: - initrds = initrd.split(';') - grubefi_conf += "initrd" - for rd in initrds: - grubefi_conf += " /%s" % rd - grubefi_conf += "\n" - - if dtb: - grubefi_conf += "devicetree /%s\n" % dtb - - grubefi_conf += "}\n" - - logger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg", - cr_workdir) - cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w") - cfg.write(grubefi_conf) - cfg.close() - - @classmethod - def do_configure_systemdboot(cls, hdddir, creator, cr_workdir, source_params): - """ - Create loader-specific systemd-boot/gummiboot config. Unified Kernel Image (uki) - support is done in image recipe with uki.bbclass and only systemd-boot loader config - and ESP partition structure is created here. - """ - # detect uki.bbclass usage - image_classes = get_bitbake_var("IMAGE_CLASSES").split() - unified_image = False - if "uki" in image_classes: - unified_image = True - - install_cmd = "install -d %s/loader" % hdddir - exec_cmd(install_cmd) - - install_cmd = "install -d %s/loader/entries" % hdddir - exec_cmd(install_cmd) - - bootloader = creator.ks.bootloader - loader_conf = "" - - # 5 seconds is a sensible default timeout - loader_conf += "timeout %d\n" % (bootloader.timeout or 5) - - logger.debug("Writing systemd-boot config " - "%s/hdd/boot/loader/loader.conf", cr_workdir) - cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w") - cfg.write(loader_conf) - logger.debug("loader.conf:\n%s" % (loader_conf)) - cfg.close() - - initrd = source_params.get('initrd') - dtb = source_params.get('dtb') - if not unified_image: - cls._copy_additional_files(hdddir, initrd, dtb) - - configfile = creator.ks.bootloader.configfile - custom_cfg = None - boot_conf = "" - if configfile: - custom_cfg = get_custom_config(configfile) - if custom_cfg: - # Use a custom configuration for systemd-boot - boot_conf = custom_cfg - logger.debug("Using custom configuration file " - "%s for systemd-boots's boot.conf", configfile) - else: - raise WicError("configfile is specified but failed to " - "get it from %s.", configfile) - else: - # Create systemd-boot configuration using parameters from wks file - kernel = get_bitbake_var("KERNEL_IMAGETYPE") - if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": - if get_bitbake_var("INITRAMFS_IMAGE"): - kernel = "%s-%s.bin" % \ - (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) - - title = source_params.get('title') - - boot_conf += "title %s\n" % (title if title else "boot") - boot_conf += "linux /%s\n" % kernel - - label = source_params.get('label') - label_conf = "LABEL=Boot root=%s" % creator.rootdev - if label: - label_conf = "LABEL=%s" % label - - boot_conf += "options %s %s\n" % \ - (label_conf, bootloader.append) - - if initrd: - initrds = initrd.split(';') - for rd in initrds: - boot_conf += "initrd /%s\n" % rd - - if dtb: - boot_conf += "devicetree /%s\n" % dtb - - if not unified_image: - logger.debug("Writing systemd-boot config " - "%s/hdd/boot/loader/entries/boot.conf", cr_workdir) - cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w") - cfg.write(boot_conf) - logger.debug("boot.conf:\n%s" % (boot_conf)) - cfg.close() - - - @classmethod - def do_configure_partition(cls, part, source_params, creator, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - native_sysroot): - """ - Called before do_prepare_partition(), creates loader-specific config - """ - hdddir = "%s/hdd/boot" % cr_workdir - - install_cmd = "install -d %s/EFI/BOOT" % hdddir - exec_cmd(install_cmd) - - try: - if source_params['loader'] == 'grub-efi': - cls.do_configure_grubefi(hdddir, creator, cr_workdir, source_params) - elif source_params['loader'] == 'systemd-boot': - cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params) - elif source_params['loader'] == 'uefi-kernel': - pass - else: - raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader']) - except KeyError: - raise WicError("bootimg-efi requires a loader, none specified") - - if get_bitbake_var("IMAGE_EFI_BOOT_FILES") is None: - logger.debug('No boot files defined in IMAGE_EFI_BOOT_FILES') - else: - boot_files = None - for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)): - if fmt: - var = fmt % id - else: - var = "" - - boot_files = get_bitbake_var("IMAGE_EFI_BOOT_FILES" + var) - if boot_files: - break - - logger.debug('Boot files: %s', boot_files) - - # list of tuples (src_name, dst_name) - deploy_files = [] - for src_entry in re.findall(r'[\w;\-\.\+/\*]+', boot_files): - if ';' in src_entry: - dst_entry = tuple(src_entry.split(';')) - if not dst_entry[0] or not dst_entry[1]: - raise WicError('Malformed boot file entry: %s' % src_entry) - else: - dst_entry = (src_entry, src_entry) - - logger.debug('Destination entry: %r', dst_entry) - deploy_files.append(dst_entry) - - cls.install_task = []; - for deploy_entry in deploy_files: - src, dst = deploy_entry - if '*' in src: - # by default install files under their basename - entry_name_fn = os.path.basename - if dst != src: - # unless a target name was given, then treat name - # as a directory and append a basename - entry_name_fn = lambda name: \ - os.path.join(dst, - os.path.basename(name)) - - srcs = glob(os.path.join(kernel_dir, src)) - - logger.debug('Globbed sources: %s', ', '.join(srcs)) - for entry in srcs: - src = os.path.relpath(entry, kernel_dir) - entry_dst_name = entry_name_fn(entry) - cls.install_task.append((src, entry_dst_name)) - else: - cls.install_task.append((src, dst)) - - @classmethod - def do_prepare_partition(cls, part, source_params, creator, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - rootfs_dir, native_sysroot): - """ - Called to do the actual content population for a partition i.e. it - 'prepares' the partition to be incorporated into the image. - In this case, prepare content for an EFI (grub) boot partition. - """ - if not kernel_dir: - kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") - if not kernel_dir: - raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") - - staging_kernel_dir = kernel_dir - - hdddir = "%s/hdd/boot" % cr_workdir - - kernel = get_bitbake_var("KERNEL_IMAGETYPE") - if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": - if get_bitbake_var("INITRAMFS_IMAGE"): - kernel = "%s-%s.bin" % \ - (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) - - if source_params.get('create-unified-kernel-image') == "true": - raise WicError("create-unified-kernel-image is no longer supported. Please use uki.bbclass.") - - if source_params.get('install-kernel-into-boot-dir') != 'false': - install_cmd = "install -v -p -m 0644 %s/%s %s/%s" % \ - (staging_kernel_dir, kernel, hdddir, kernel) - out = exec_cmd(install_cmd) - logger.debug("Installed kernel files:\n%s" % out) - - if get_bitbake_var("IMAGE_EFI_BOOT_FILES"): - for src_path, dst_path in cls.install_task: - install_cmd = "install -v -p -m 0644 -D %s %s" \ - % (os.path.join(kernel_dir, src_path), - os.path.join(hdddir, dst_path)) - out = exec_cmd(install_cmd) - logger.debug("Installed IMAGE_EFI_BOOT_FILES:\n%s" % out) - - try: - if source_params['loader'] == 'grub-efi': - shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, - "%s/grub.cfg" % cr_workdir) - for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]: - cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:]) - exec_cmd(cp_cmd, True) - shutil.move("%s/grub.cfg" % cr_workdir, - "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir) - elif source_params['loader'] == 'systemd-boot': - for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]: - cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:]) - out = exec_cmd(cp_cmd, True) - logger.debug("systemd-boot files:\n%s" % out) - elif source_params['loader'] == 'uefi-kernel': - kernel = get_bitbake_var("KERNEL_IMAGETYPE") - if not kernel: - raise WicError("Empty KERNEL_IMAGETYPE") - target = get_bitbake_var("TARGET_SYS") - if not target: - raise WicError("Empty TARGET_SYS") - - if re.match("x86_64", target): - kernel_efi_image = "bootx64.efi" - elif re.match('i.86', target): - kernel_efi_image = "bootia32.efi" - elif re.match('aarch64', target): - kernel_efi_image = "bootaa64.efi" - elif re.match('arm', target): - kernel_efi_image = "bootarm.efi" - else: - raise WicError("UEFI stub kernel is incompatible with target %s" % target) - - for mod in [x for x in os.listdir(kernel_dir) if x.startswith(kernel)]: - cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, kernel_efi_image) - out = exec_cmd(cp_cmd, True) - logger.debug("uefi-kernel files:\n%s" % out) - else: - raise WicError("unrecognized bootimg-efi loader: %s" % - source_params['loader']) - - # must have installed at least one EFI bootloader - out = glob(os.path.join(hdddir, 'EFI', 'BOOT', 'boot*.efi')) - logger.debug("Installed EFI loader files:\n%s" % out) - if not out: - raise WicError("No EFI loaders installed to ESP partition. Check that grub-efi, systemd-boot or similar is installed.") - - except KeyError: - raise WicError("bootimg-efi requires a loader, none specified") - - startup = os.path.join(kernel_dir, "startup.nsh") - if os.path.exists(startup): - cp_cmd = "cp -v -p %s %s/" % (startup, hdddir) - out = exec_cmd(cp_cmd, True) - logger.debug("startup files:\n%s" % out) - - for paths in part.include_path or []: - for path in paths: - cp_cmd = "cp -v -p -r %s %s/" % (path, hdddir) - exec_cmd(cp_cmd, True) - logger.debug("include_path files:\n%s" % out) - - du_cmd = "du -bks %s" % hdddir - out = exec_cmd(du_cmd) - blocks = int(out.split()[0]) - - extra_blocks = part.get_extra_block_count(blocks) - - if extra_blocks < BOOTDD_EXTRA_SPACE: - extra_blocks = BOOTDD_EXTRA_SPACE - - blocks += extra_blocks - - logger.debug("Added %d extra blocks to %s to get to %d total blocks", - extra_blocks, part.mountpoint, blocks) - - # required for compatibility with certain devices expecting file system - # block count to be equal to partition block count - if blocks < part.fixed_size: - blocks = part.fixed_size - logger.debug("Overriding %s to %d total blocks for compatibility", - part.mountpoint, blocks) - - # dosfs image, created by mkdosfs - bootimg = "%s/boot.img" % cr_workdir - - label = part.label if part.label else "ESP" - - dosfs_cmd = "mkdosfs -v -n %s -i %s -C %s %d" % \ - (label, part.fsuuid, bootimg, blocks) - exec_native_cmd(dosfs_cmd, native_sysroot) - logger.debug("mkdosfs:\n%s" % (str(out))) - - mcopy_cmd = "mcopy -v -p -i %s -s %s/* ::/" % (bootimg, hdddir) - out = exec_native_cmd(mcopy_cmd, native_sysroot) - logger.debug("mcopy:\n%s" % (str(out))) - - chmod_cmd = "chmod 644 %s" % bootimg - exec_cmd(chmod_cmd) - - du_cmd = "du -Lbks %s" % bootimg - out = exec_cmd(du_cmd) - bootimg_size = out.split()[0] - - part.size = int(bootimg_size) - part.source_file = bootimg diff --git a/scripts/lib/wic/plugins/source/bootimg-partition.py b/scripts/lib/wic/plugins/source/bootimg-partition.py deleted file mode 100644 index 589853a439..0000000000 --- a/scripts/lib/wic/plugins/source/bootimg-partition.py +++ /dev/null @@ -1,162 +0,0 @@ -# -# Copyright OpenEmbedded Contributors -# -# SPDX-License-Identifier: GPL-2.0-only -# -# DESCRIPTION -# This implements the 'bootimg-partition' source plugin class for -# 'wic'. The plugin creates an image of boot partition, copying over -# files listed in IMAGE_BOOT_FILES bitbake variable. -# -# AUTHORS -# Maciej Borzecki -# - -import logging -import os -import re - -from oe.bootfiles import get_boot_files - -from wic import WicError -from wic.engine import get_custom_config -from wic.pluginbase import SourcePlugin -from wic.misc import exec_cmd, get_bitbake_var - -logger = logging.getLogger('wic') - -class BootimgPartitionPlugin(SourcePlugin): - """ - Create an image of boot partition, copying over files - listed in IMAGE_BOOT_FILES bitbake variable. - """ - - name = 'bootimg-partition' - image_boot_files_var_name = 'IMAGE_BOOT_FILES' - - @classmethod - def do_configure_partition(cls, part, source_params, cr, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - native_sysroot): - """ - Called before do_prepare_partition(), create u-boot specific boot config - """ - hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) - install_cmd = "install -d %s" % hdddir - exec_cmd(install_cmd) - - if not kernel_dir: - kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") - if not kernel_dir: - raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") - - boot_files = None - for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)): - if fmt: - var = fmt % id - else: - var = "" - - boot_files = get_bitbake_var(cls.image_boot_files_var_name + var) - if boot_files is not None: - break - - if boot_files is None: - raise WicError('No boot files defined, %s unset for entry #%d' % (cls.image_boot_files_var_name, part.lineno)) - - logger.debug('Boot files: %s', boot_files) - - cls.install_task = get_boot_files(kernel_dir, boot_files) - if source_params.get('loader') != "u-boot": - return - - configfile = cr.ks.bootloader.configfile - custom_cfg = None - if configfile: - custom_cfg = get_custom_config(configfile) - if custom_cfg: - # Use a custom configuration for extlinux.conf - extlinux_conf = custom_cfg - logger.debug("Using custom configuration file " - "%s for extlinux.conf", configfile) - else: - raise WicError("configfile is specified but failed to " - "get it from %s." % configfile) - - if not custom_cfg: - # The kernel types supported by the sysboot of u-boot - kernel_types = ["zImage", "Image", "fitImage", "uImage", "vmlinux"] - has_dtb = False - fdt_dir = '/' - kernel_name = None - - # Find the kernel image name, from the highest precedence to lowest - for image in kernel_types: - for task in cls.install_task: - src, dst = task - if re.match(image, src): - kernel_name = os.path.join('/', dst) - break - if kernel_name: - break - - for task in cls.install_task: - src, dst = task - # We suppose that all the dtb are in the same directory - if re.search(r'\.dtb', src) and fdt_dir == '/': - has_dtb = True - fdt_dir = os.path.join(fdt_dir, os.path.dirname(dst)) - break - - if not kernel_name: - raise WicError('No kernel file found') - - # Compose the extlinux.conf - extlinux_conf = "default Yocto\n" - extlinux_conf += "label Yocto\n" - extlinux_conf += " kernel %s\n" % kernel_name - if has_dtb: - extlinux_conf += " fdtdir %s\n" % fdt_dir - bootloader = cr.ks.bootloader - extlinux_conf += "append root=%s rootwait %s\n" \ - % (cr.rootdev, bootloader.append if bootloader.append else '') - - install_cmd = "install -d %s/extlinux/" % hdddir - exec_cmd(install_cmd) - cfg = open("%s/extlinux/extlinux.conf" % hdddir, "w") - cfg.write(extlinux_conf) - cfg.close() - - - @classmethod - def do_prepare_partition(cls, part, source_params, cr, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - rootfs_dir, native_sysroot): - """ - Called to do the actual content population for a partition i.e. it - 'prepares' the partition to be incorporated into the image. - In this case, does the following: - - sets up a vfat partition - - copies all files listed in IMAGE_BOOT_FILES variable - """ - hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) - - if not kernel_dir: - kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") - if not kernel_dir: - raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") - - logger.debug('Kernel dir: %s', bootimg_dir) - - - for task in cls.install_task: - src_path, dst_path = task - logger.debug('Install %s as %s', src_path, dst_path) - install_cmd = "install -m 0644 -D %s %s" \ - % (os.path.join(kernel_dir, src_path), - os.path.join(hdddir, dst_path)) - exec_cmd(install_cmd) - - logger.debug('Prepare boot partition using rootfs in %s', hdddir) - part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, - native_sysroot, False) diff --git a/scripts/lib/wic/plugins/source/bootimg-pcbios.py b/scripts/lib/wic/plugins/source/bootimg-pcbios.py deleted file mode 100644 index a207a83530..0000000000 --- a/scripts/lib/wic/plugins/source/bootimg-pcbios.py +++ /dev/null @@ -1,209 +0,0 @@ -# -# Copyright (c) 2014, Intel Corporation. -# -# SPDX-License-Identifier: GPL-2.0-only -# -# DESCRIPTION -# This implements the 'bootimg-pcbios' source plugin class for 'wic' -# -# AUTHORS -# Tom Zanussi -# - -import logging -import os -import re - -from wic import WicError -from wic.engine import get_custom_config -from wic.pluginbase import SourcePlugin -from wic.misc import (exec_cmd, exec_native_cmd, - get_bitbake_var, BOOTDD_EXTRA_SPACE) - -logger = logging.getLogger('wic') - -class BootimgPcbiosPlugin(SourcePlugin): - """ - Create MBR boot partition and install syslinux on it. - """ - - name = 'bootimg-pcbios' - - @classmethod - def _get_bootimg_dir(cls, bootimg_dir, dirname): - """ - Check if dirname exists in default bootimg_dir or in STAGING_DIR. - """ - staging_datadir = get_bitbake_var("STAGING_DATADIR") - for result in (bootimg_dir, staging_datadir): - if os.path.exists("%s/%s" % (result, dirname)): - return result - - # STAGING_DATADIR is expanded with MLPREFIX if multilib is enabled - # but dependency syslinux is still populated to original STAGING_DATADIR - nonarch_datadir = re.sub('/[^/]*recipe-sysroot', '/recipe-sysroot', staging_datadir) - if os.path.exists(os.path.join(nonarch_datadir, dirname)): - return nonarch_datadir - - raise WicError("Couldn't find correct bootimg_dir, exiting") - - @classmethod - def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, - bootimg_dir, kernel_dir, native_sysroot): - """ - Called after all partitions have been prepared and assembled into a - disk image. In this case, we install the MBR. - """ - bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') - mbrfile = "%s/syslinux/" % bootimg_dir - if creator.ptable_format == 'msdos': - mbrfile += "mbr.bin" - elif creator.ptable_format == 'gpt': - mbrfile += "gptmbr.bin" - else: - raise WicError("Unsupported partition table: %s" % - creator.ptable_format) - - if not os.path.exists(mbrfile): - raise WicError("Couldn't find %s. If using the -e option, do you " - "have the right MACHINE set in local.conf? If not, " - "is the bootimg_dir path correct?" % mbrfile) - - full_path = creator._full_path(workdir, disk_name, "direct") - logger.debug("Installing MBR on disk %s as %s with size %s bytes", - disk_name, full_path, disk.min_size) - - dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path) - exec_cmd(dd_cmd, native_sysroot) - - @classmethod - def do_configure_partition(cls, part, source_params, creator, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - native_sysroot): - """ - Called before do_prepare_partition(), creates syslinux config - """ - hdddir = "%s/hdd/boot" % cr_workdir - - install_cmd = "install -d %s" % hdddir - exec_cmd(install_cmd) - - bootloader = creator.ks.bootloader - - custom_cfg = None - if bootloader.configfile: - custom_cfg = get_custom_config(bootloader.configfile) - if custom_cfg: - # Use a custom configuration for grub - syslinux_conf = custom_cfg - logger.debug("Using custom configuration file %s " - "for syslinux.cfg", bootloader.configfile) - else: - raise WicError("configfile is specified but failed to " - "get it from %s." % bootloader.configfile) - - if not custom_cfg: - # Create syslinux configuration using parameters from wks file - splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg") - if os.path.exists(splash): - splashline = "menu background splash.jpg" - else: - splashline = "" - - syslinux_conf = "" - syslinux_conf += "PROMPT 0\n" - syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n" - syslinux_conf += "\n" - syslinux_conf += "ALLOWOPTIONS 1\n" - syslinux_conf += "SERIAL 0 115200\n" - syslinux_conf += "\n" - if splashline: - syslinux_conf += "%s\n" % splashline - syslinux_conf += "DEFAULT boot\n" - syslinux_conf += "LABEL boot\n" - - kernel = "/" + get_bitbake_var("KERNEL_IMAGETYPE") - syslinux_conf += "KERNEL " + kernel + "\n" - - syslinux_conf += "APPEND label=boot root=%s %s\n" % \ - (creator.rootdev, bootloader.append) - - logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg", - cr_workdir) - cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w") - cfg.write(syslinux_conf) - cfg.close() - - @classmethod - def do_prepare_partition(cls, part, source_params, creator, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - rootfs_dir, native_sysroot): - """ - Called to do the actual content population for a partition i.e. it - 'prepares' the partition to be incorporated into the image. - In this case, prepare content for legacy bios boot partition. - """ - bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') - - staging_kernel_dir = kernel_dir - - hdddir = "%s/hdd/boot" % cr_workdir - - kernel = get_bitbake_var("KERNEL_IMAGETYPE") - if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": - if get_bitbake_var("INITRAMFS_IMAGE"): - kernel = "%s-%s.bin" % \ - (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) - - cmds = ("install -m 0644 %s/%s %s/%s" % - (staging_kernel_dir, kernel, hdddir, get_bitbake_var("KERNEL_IMAGETYPE")), - "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" % - (bootimg_dir, hdddir), - "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" % - (bootimg_dir, hdddir), - "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" % - (bootimg_dir, hdddir), - "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" % - (bootimg_dir, hdddir)) - - for install_cmd in cmds: - exec_cmd(install_cmd) - - du_cmd = "du -bks %s" % hdddir - out = exec_cmd(du_cmd) - blocks = int(out.split()[0]) - - extra_blocks = part.get_extra_block_count(blocks) - - if extra_blocks < BOOTDD_EXTRA_SPACE: - extra_blocks = BOOTDD_EXTRA_SPACE - - blocks += extra_blocks - - logger.debug("Added %d extra blocks to %s to get to %d total blocks", - extra_blocks, part.mountpoint, blocks) - - # dosfs image, created by mkdosfs - bootimg = "%s/boot%s.img" % (cr_workdir, part.lineno) - - label = part.label if part.label else "boot" - - dosfs_cmd = "mkdosfs -n %s -i %s -S 512 -C %s %d" % \ - (label, part.fsuuid, bootimg, blocks) - exec_native_cmd(dosfs_cmd, native_sysroot) - - mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) - exec_native_cmd(mcopy_cmd, native_sysroot) - - syslinux_cmd = "syslinux %s" % bootimg - exec_native_cmd(syslinux_cmd, native_sysroot) - - chmod_cmd = "chmod 644 %s" % bootimg - exec_cmd(chmod_cmd) - - du_cmd = "du -Lbks %s" % bootimg - out = exec_cmd(du_cmd) - bootimg_size = out.split()[0] - - part.size = int(bootimg_size) - part.source_file = bootimg diff --git a/scripts/lib/wic/plugins/source/bootimg_biosplusefi.py b/scripts/lib/wic/plugins/source/bootimg_biosplusefi.py new file mode 100644 index 0000000000..5bd7390680 --- /dev/null +++ b/scripts/lib/wic/plugins/source/bootimg_biosplusefi.py @@ -0,0 +1,213 @@ +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License version 2 as +# published by the Free Software Foundation. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License along +# with this program; if not, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +# +# DESCRIPTION +# This implements the 'bootimg-biosplusefi' source plugin class for 'wic' +# +# AUTHORS +# William Bourque + +import types + +from wic.pluginbase import SourcePlugin +from importlib.machinery import SourceFileLoader + +class BootimgBiosPlusEFIPlugin(SourcePlugin): + """ + Create MBR + EFI boot partition + + This plugin creates a boot partition that contains both + legacy BIOS and EFI content. It will be able to boot from both. + This is useful when managing PC fleet with some older machines + without EFI support. + + Note it is possible to create an image that can boot from both + legacy BIOS and EFI by defining two partitions : one with arg + --source bootimg-efi and another one with --source bootimg-pcbios. + However, this method has the obvious downside that it requires TWO + partitions to be created on the storage device. + Both partitions will also be marked as "bootable" which does not work on + most BIOS, has BIOS often uses the "bootable" flag to determine + what to boot. If you have such a BIOS, you need to manually remove the + "bootable" flag from the EFI partition for the drive to be bootable. + Having two partitions also seems to confuse wic : the content of + the first partition will be duplicated into the second, even though it + will not be used at all. + + Also, unlike "isoimage-isohybrid" that also does BIOS and EFI, this plugin + allows you to have more than only a single rootfs partitions and does + not turn the rootfs into an initramfs RAM image. + + This plugin is made to put everything into a single /boot partition so it + does not have the limitations listed above. + + The plugin is made so it does tries not to reimplement what's already + been done in other plugins; as such it imports "bootimg-pcbios" + and "bootimg-efi". + Plugin "bootimg-pcbios" is used to generate legacy BIOS boot. + Plugin "bootimg-efi" is used to generate the UEFI boot. Note that it + requires a --sourceparams argument to know which loader to use; refer + to "bootimg-efi" code/documentation for the list of loader. + + Imports are handled with "SourceFileLoader" from importlib as it is + otherwise very difficult to import module that has hyphen "-" in their + filename. + The SourcePlugin() methods used in the plugins (do_install_disk, + do_configure_partition, do_prepare_partition) are then called on both, + beginning by "bootimg-efi". + + Plugin options, such as "--sourceparams" can still be passed to a + plugin, as long they does not cause issue in the other plugin. + + Example wic configuration: + part /boot --source bootimg-biosplusefi --sourceparams="loader=grub-efi"\\ + --ondisk sda --label os_boot --active --align 1024 --use-uuid + """ + + name = 'bootimg-biosplusefi' + + __PCBIOS_MODULE_NAME = "bootimg-pcbios" + __EFI_MODULE_NAME = "bootimg-efi" + + __imgEFIObj = None + __imgBiosObj = None + + @classmethod + def __init__(cls): + """ + Constructor (init) + """ + + # XXX + # For some reasons, __init__ constructor is never called. + # Something to do with how pluginbase works? + cls.__instanciateSubClasses() + + @classmethod + def __instanciateSubClasses(cls): + """ + + """ + + # Import bootimg-pcbios (class name "BootimgPcbiosPlugin") + modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), + cls.__PCBIOS_MODULE_NAME + ".py") + loader = SourceFileLoader(cls.__PCBIOS_MODULE_NAME, modulePath) + mod = types.ModuleType(loader.name) + loader.exec_module(mod) + cls.__imgBiosObj = mod.BootimgPcbiosPlugin() + + # Import bootimg-efi (class name "BootimgEFIPlugin") + modulePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), + cls.__EFI_MODULE_NAME + ".py") + loader = SourceFileLoader(cls.__EFI_MODULE_NAME, modulePath) + mod = types.ModuleType(loader.name) + loader.exec_module(mod) + cls.__imgEFIObj = mod.BootimgEFIPlugin() + + @classmethod + def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, + bootimg_dir, kernel_dir, native_sysroot): + """ + Called after all partitions have been prepared and assembled into a + disk image. + """ + + if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): + cls.__instanciateSubClasses() + + cls.__imgEFIObj.do_install_disk( + disk, + disk_name, + creator, + workdir, + oe_builddir, + bootimg_dir, + kernel_dir, + native_sysroot) + + cls.__imgBiosObj.do_install_disk( + disk, + disk_name, + creator, + workdir, + oe_builddir, + bootimg_dir, + kernel_dir, + native_sysroot) + + @classmethod + def do_configure_partition(cls, part, source_params, creator, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + native_sysroot): + """ + Called before do_prepare_partition() + """ + + if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): + cls.__instanciateSubClasses() + + cls.__imgEFIObj.do_configure_partition( + part, + source_params, + creator, + cr_workdir, + oe_builddir, + bootimg_dir, + kernel_dir, + native_sysroot) + + cls.__imgBiosObj.do_configure_partition( + part, + source_params, + creator, + cr_workdir, + oe_builddir, + bootimg_dir, + kernel_dir, + native_sysroot) + + @classmethod + def do_prepare_partition(cls, part, source_params, creator, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + rootfs_dir, native_sysroot): + """ + Called to do the actual content population for a partition i.e. it + 'prepares' the partition to be incorporated into the image. + """ + + if ( (not cls.__imgEFIObj) or (not cls.__imgBiosObj) ): + cls.__instanciateSubClasses() + + cls.__imgEFIObj.do_prepare_partition( + part, + source_params, + creator, + cr_workdir, + oe_builddir, + bootimg_dir, + kernel_dir, + rootfs_dir, + native_sysroot) + + cls.__imgBiosObj.do_prepare_partition( + part, + source_params, + creator, + cr_workdir, + oe_builddir, + bootimg_dir, + kernel_dir, + rootfs_dir, + native_sysroot) diff --git a/scripts/lib/wic/plugins/source/bootimg_efi.py b/scripts/lib/wic/plugins/source/bootimg_efi.py new file mode 100644 index 0000000000..38da5080fb --- /dev/null +++ b/scripts/lib/wic/plugins/source/bootimg_efi.py @@ -0,0 +1,435 @@ +# +# Copyright (c) 2014, Intel Corporation. +# +# SPDX-License-Identifier: GPL-2.0-only +# +# DESCRIPTION +# This implements the 'bootimg-efi' source plugin class for 'wic' +# +# AUTHORS +# Tom Zanussi +# + +import logging +import os +import tempfile +import shutil +import re + +from glob import glob + +from wic import WicError +from wic.engine import get_custom_config +from wic.pluginbase import SourcePlugin +from wic.misc import (exec_cmd, exec_native_cmd, + get_bitbake_var, BOOTDD_EXTRA_SPACE) + +logger = logging.getLogger('wic') + +class BootimgEFIPlugin(SourcePlugin): + """ + Create EFI boot partition. + This plugin supports GRUB 2 and systemd-boot bootloaders. + """ + + name = 'bootimg-efi' + + @classmethod + def _copy_additional_files(cls, hdddir, initrd, dtb): + bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") + if not bootimg_dir: + raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") + + if initrd: + initrds = initrd.split(';') + for rd in initrds: + cp_cmd = "cp -v -p %s/%s %s" % (bootimg_dir, rd, hdddir) + out = exec_cmd(cp_cmd, True) + logger.debug("initrd files:\n%s" % (out)) + else: + logger.debug("Ignoring missing initrd") + + if dtb: + if ';' in dtb: + raise WicError("Only one DTB supported, exiting") + cp_cmd = "cp -v -p %s/%s %s" % (bootimg_dir, dtb, hdddir) + out = exec_cmd(cp_cmd, True) + logger.debug("dtb files:\n%s" % (out)) + + @classmethod + def do_configure_grubefi(cls, hdddir, creator, cr_workdir, source_params): + """ + Create loader-specific (grub-efi) config + """ + configfile = creator.ks.bootloader.configfile + custom_cfg = None + if configfile: + custom_cfg = get_custom_config(configfile) + if custom_cfg: + # Use a custom configuration for grub + grubefi_conf = custom_cfg + logger.debug("Using custom configuration file " + "%s for grub.cfg", configfile) + else: + raise WicError("configfile is specified but failed to " + "get it from %s." % configfile) + + initrd = source_params.get('initrd') + dtb = source_params.get('dtb') + + cls._copy_additional_files(hdddir, initrd, dtb) + + if not custom_cfg: + # Create grub configuration using parameters from wks file + bootloader = creator.ks.bootloader + title = source_params.get('title') + + grubefi_conf = "" + grubefi_conf += "serial --unit=0 --speed=115200 --word=8 --parity=no --stop=1\n" + grubefi_conf += "default=boot\n" + grubefi_conf += "timeout=%s\n" % bootloader.timeout + grubefi_conf += "menuentry '%s'{\n" % (title if title else "boot") + + kernel = get_bitbake_var("KERNEL_IMAGETYPE") + if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": + if get_bitbake_var("INITRAMFS_IMAGE"): + kernel = "%s-%s.bin" % \ + (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) + + label = source_params.get('label') + label_conf = "root=%s" % creator.rootdev + if label: + label_conf = "LABEL=%s" % label + + grubefi_conf += "linux /%s %s rootwait %s\n" \ + % (kernel, label_conf, bootloader.append) + + if initrd: + initrds = initrd.split(';') + grubefi_conf += "initrd" + for rd in initrds: + grubefi_conf += " /%s" % rd + grubefi_conf += "\n" + + if dtb: + grubefi_conf += "devicetree /%s\n" % dtb + + grubefi_conf += "}\n" + + logger.debug("Writing grubefi config %s/hdd/boot/EFI/BOOT/grub.cfg", + cr_workdir) + cfg = open("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, "w") + cfg.write(grubefi_conf) + cfg.close() + + @classmethod + def do_configure_systemdboot(cls, hdddir, creator, cr_workdir, source_params): + """ + Create loader-specific systemd-boot/gummiboot config. Unified Kernel Image (uki) + support is done in image recipe with uki.bbclass and only systemd-boot loader config + and ESP partition structure is created here. + """ + # detect uki.bbclass usage + image_classes = get_bitbake_var("IMAGE_CLASSES").split() + unified_image = False + if "uki" in image_classes: + unified_image = True + + install_cmd = "install -d %s/loader" % hdddir + exec_cmd(install_cmd) + + install_cmd = "install -d %s/loader/entries" % hdddir + exec_cmd(install_cmd) + + bootloader = creator.ks.bootloader + loader_conf = "" + + # 5 seconds is a sensible default timeout + loader_conf += "timeout %d\n" % (bootloader.timeout or 5) + + logger.debug("Writing systemd-boot config " + "%s/hdd/boot/loader/loader.conf", cr_workdir) + cfg = open("%s/hdd/boot/loader/loader.conf" % cr_workdir, "w") + cfg.write(loader_conf) + logger.debug("loader.conf:\n%s" % (loader_conf)) + cfg.close() + + initrd = source_params.get('initrd') + dtb = source_params.get('dtb') + if not unified_image: + cls._copy_additional_files(hdddir, initrd, dtb) + + configfile = creator.ks.bootloader.configfile + custom_cfg = None + boot_conf = "" + if configfile: + custom_cfg = get_custom_config(configfile) + if custom_cfg: + # Use a custom configuration for systemd-boot + boot_conf = custom_cfg + logger.debug("Using custom configuration file " + "%s for systemd-boots's boot.conf", configfile) + else: + raise WicError("configfile is specified but failed to " + "get it from %s.", configfile) + else: + # Create systemd-boot configuration using parameters from wks file + kernel = get_bitbake_var("KERNEL_IMAGETYPE") + if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": + if get_bitbake_var("INITRAMFS_IMAGE"): + kernel = "%s-%s.bin" % \ + (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) + + title = source_params.get('title') + + boot_conf += "title %s\n" % (title if title else "boot") + boot_conf += "linux /%s\n" % kernel + + label = source_params.get('label') + label_conf = "LABEL=Boot root=%s" % creator.rootdev + if label: + label_conf = "LABEL=%s" % label + + boot_conf += "options %s %s\n" % \ + (label_conf, bootloader.append) + + if initrd: + initrds = initrd.split(';') + for rd in initrds: + boot_conf += "initrd /%s\n" % rd + + if dtb: + boot_conf += "devicetree /%s\n" % dtb + + if not unified_image: + logger.debug("Writing systemd-boot config " + "%s/hdd/boot/loader/entries/boot.conf", cr_workdir) + cfg = open("%s/hdd/boot/loader/entries/boot.conf" % cr_workdir, "w") + cfg.write(boot_conf) + logger.debug("boot.conf:\n%s" % (boot_conf)) + cfg.close() + + + @classmethod + def do_configure_partition(cls, part, source_params, creator, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + native_sysroot): + """ + Called before do_prepare_partition(), creates loader-specific config + """ + hdddir = "%s/hdd/boot" % cr_workdir + + install_cmd = "install -d %s/EFI/BOOT" % hdddir + exec_cmd(install_cmd) + + try: + if source_params['loader'] == 'grub-efi': + cls.do_configure_grubefi(hdddir, creator, cr_workdir, source_params) + elif source_params['loader'] == 'systemd-boot': + cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params) + elif source_params['loader'] == 'uefi-kernel': + pass + else: + raise WicError("unrecognized bootimg-efi loader: %s" % source_params['loader']) + except KeyError: + raise WicError("bootimg-efi requires a loader, none specified") + + if get_bitbake_var("IMAGE_EFI_BOOT_FILES") is None: + logger.debug('No boot files defined in IMAGE_EFI_BOOT_FILES') + else: + boot_files = None + for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)): + if fmt: + var = fmt % id + else: + var = "" + + boot_files = get_bitbake_var("IMAGE_EFI_BOOT_FILES" + var) + if boot_files: + break + + logger.debug('Boot files: %s', boot_files) + + # list of tuples (src_name, dst_name) + deploy_files = [] + for src_entry in re.findall(r'[\w;\-\.\+/\*]+', boot_files): + if ';' in src_entry: + dst_entry = tuple(src_entry.split(';')) + if not dst_entry[0] or not dst_entry[1]: + raise WicError('Malformed boot file entry: %s' % src_entry) + else: + dst_entry = (src_entry, src_entry) + + logger.debug('Destination entry: %r', dst_entry) + deploy_files.append(dst_entry) + + cls.install_task = []; + for deploy_entry in deploy_files: + src, dst = deploy_entry + if '*' in src: + # by default install files under their basename + entry_name_fn = os.path.basename + if dst != src: + # unless a target name was given, then treat name + # as a directory and append a basename + entry_name_fn = lambda name: \ + os.path.join(dst, + os.path.basename(name)) + + srcs = glob(os.path.join(kernel_dir, src)) + + logger.debug('Globbed sources: %s', ', '.join(srcs)) + for entry in srcs: + src = os.path.relpath(entry, kernel_dir) + entry_dst_name = entry_name_fn(entry) + cls.install_task.append((src, entry_dst_name)) + else: + cls.install_task.append((src, dst)) + + @classmethod + def do_prepare_partition(cls, part, source_params, creator, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + rootfs_dir, native_sysroot): + """ + Called to do the actual content population for a partition i.e. it + 'prepares' the partition to be incorporated into the image. + In this case, prepare content for an EFI (grub) boot partition. + """ + if not kernel_dir: + kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") + if not kernel_dir: + raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") + + staging_kernel_dir = kernel_dir + + hdddir = "%s/hdd/boot" % cr_workdir + + kernel = get_bitbake_var("KERNEL_IMAGETYPE") + if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": + if get_bitbake_var("INITRAMFS_IMAGE"): + kernel = "%s-%s.bin" % \ + (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) + + if source_params.get('create-unified-kernel-image') == "true": + raise WicError("create-unified-kernel-image is no longer supported. Please use uki.bbclass.") + + if source_params.get('install-kernel-into-boot-dir') != 'false': + install_cmd = "install -v -p -m 0644 %s/%s %s/%s" % \ + (staging_kernel_dir, kernel, hdddir, kernel) + out = exec_cmd(install_cmd) + logger.debug("Installed kernel files:\n%s" % out) + + if get_bitbake_var("IMAGE_EFI_BOOT_FILES"): + for src_path, dst_path in cls.install_task: + install_cmd = "install -v -p -m 0644 -D %s %s" \ + % (os.path.join(kernel_dir, src_path), + os.path.join(hdddir, dst_path)) + out = exec_cmd(install_cmd) + logger.debug("Installed IMAGE_EFI_BOOT_FILES:\n%s" % out) + + try: + if source_params['loader'] == 'grub-efi': + shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir, + "%s/grub.cfg" % cr_workdir) + for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]: + cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[9:]) + exec_cmd(cp_cmd, True) + shutil.move("%s/grub.cfg" % cr_workdir, + "%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir) + elif source_params['loader'] == 'systemd-boot': + for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]: + cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:]) + out = exec_cmd(cp_cmd, True) + logger.debug("systemd-boot files:\n%s" % out) + elif source_params['loader'] == 'uefi-kernel': + kernel = get_bitbake_var("KERNEL_IMAGETYPE") + if not kernel: + raise WicError("Empty KERNEL_IMAGETYPE") + target = get_bitbake_var("TARGET_SYS") + if not target: + raise WicError("Empty TARGET_SYS") + + if re.match("x86_64", target): + kernel_efi_image = "bootx64.efi" + elif re.match('i.86', target): + kernel_efi_image = "bootia32.efi" + elif re.match('aarch64', target): + kernel_efi_image = "bootaa64.efi" + elif re.match('arm', target): + kernel_efi_image = "bootarm.efi" + else: + raise WicError("UEFI stub kernel is incompatible with target %s" % target) + + for mod in [x for x in os.listdir(kernel_dir) if x.startswith(kernel)]: + cp_cmd = "cp -v -p %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, kernel_efi_image) + out = exec_cmd(cp_cmd, True) + logger.debug("uefi-kernel files:\n%s" % out) + else: + raise WicError("unrecognized bootimg-efi loader: %s" % + source_params['loader']) + + # must have installed at least one EFI bootloader + out = glob(os.path.join(hdddir, 'EFI', 'BOOT', 'boot*.efi')) + logger.debug("Installed EFI loader files:\n%s" % out) + if not out: + raise WicError("No EFI loaders installed to ESP partition. Check that grub-efi, systemd-boot or similar is installed.") + + except KeyError: + raise WicError("bootimg-efi requires a loader, none specified") + + startup = os.path.join(kernel_dir, "startup.nsh") + if os.path.exists(startup): + cp_cmd = "cp -v -p %s %s/" % (startup, hdddir) + out = exec_cmd(cp_cmd, True) + logger.debug("startup files:\n%s" % out) + + for paths in part.include_path or []: + for path in paths: + cp_cmd = "cp -v -p -r %s %s/" % (path, hdddir) + exec_cmd(cp_cmd, True) + logger.debug("include_path files:\n%s" % out) + + du_cmd = "du -bks %s" % hdddir + out = exec_cmd(du_cmd) + blocks = int(out.split()[0]) + + extra_blocks = part.get_extra_block_count(blocks) + + if extra_blocks < BOOTDD_EXTRA_SPACE: + extra_blocks = BOOTDD_EXTRA_SPACE + + blocks += extra_blocks + + logger.debug("Added %d extra blocks to %s to get to %d total blocks", + extra_blocks, part.mountpoint, blocks) + + # required for compatibility with certain devices expecting file system + # block count to be equal to partition block count + if blocks < part.fixed_size: + blocks = part.fixed_size + logger.debug("Overriding %s to %d total blocks for compatibility", + part.mountpoint, blocks) + + # dosfs image, created by mkdosfs + bootimg = "%s/boot.img" % cr_workdir + + label = part.label if part.label else "ESP" + + dosfs_cmd = "mkdosfs -v -n %s -i %s -C %s %d" % \ + (label, part.fsuuid, bootimg, blocks) + exec_native_cmd(dosfs_cmd, native_sysroot) + logger.debug("mkdosfs:\n%s" % (str(out))) + + mcopy_cmd = "mcopy -v -p -i %s -s %s/* ::/" % (bootimg, hdddir) + out = exec_native_cmd(mcopy_cmd, native_sysroot) + logger.debug("mcopy:\n%s" % (str(out))) + + chmod_cmd = "chmod 644 %s" % bootimg + exec_cmd(chmod_cmd) + + du_cmd = "du -Lbks %s" % bootimg + out = exec_cmd(du_cmd) + bootimg_size = out.split()[0] + + part.size = int(bootimg_size) + part.source_file = bootimg diff --git a/scripts/lib/wic/plugins/source/bootimg_partition.py b/scripts/lib/wic/plugins/source/bootimg_partition.py new file mode 100644 index 0000000000..589853a439 --- /dev/null +++ b/scripts/lib/wic/plugins/source/bootimg_partition.py @@ -0,0 +1,162 @@ +# +# Copyright OpenEmbedded Contributors +# +# SPDX-License-Identifier: GPL-2.0-only +# +# DESCRIPTION +# This implements the 'bootimg-partition' source plugin class for +# 'wic'. The plugin creates an image of boot partition, copying over +# files listed in IMAGE_BOOT_FILES bitbake variable. +# +# AUTHORS +# Maciej Borzecki +# + +import logging +import os +import re + +from oe.bootfiles import get_boot_files + +from wic import WicError +from wic.engine import get_custom_config +from wic.pluginbase import SourcePlugin +from wic.misc import exec_cmd, get_bitbake_var + +logger = logging.getLogger('wic') + +class BootimgPartitionPlugin(SourcePlugin): + """ + Create an image of boot partition, copying over files + listed in IMAGE_BOOT_FILES bitbake variable. + """ + + name = 'bootimg-partition' + image_boot_files_var_name = 'IMAGE_BOOT_FILES' + + @classmethod + def do_configure_partition(cls, part, source_params, cr, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + native_sysroot): + """ + Called before do_prepare_partition(), create u-boot specific boot config + """ + hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) + install_cmd = "install -d %s" % hdddir + exec_cmd(install_cmd) + + if not kernel_dir: + kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") + if not kernel_dir: + raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") + + boot_files = None + for (fmt, id) in (("_uuid-%s", part.uuid), ("_label-%s", part.label), (None, None)): + if fmt: + var = fmt % id + else: + var = "" + + boot_files = get_bitbake_var(cls.image_boot_files_var_name + var) + if boot_files is not None: + break + + if boot_files is None: + raise WicError('No boot files defined, %s unset for entry #%d' % (cls.image_boot_files_var_name, part.lineno)) + + logger.debug('Boot files: %s', boot_files) + + cls.install_task = get_boot_files(kernel_dir, boot_files) + if source_params.get('loader') != "u-boot": + return + + configfile = cr.ks.bootloader.configfile + custom_cfg = None + if configfile: + custom_cfg = get_custom_config(configfile) + if custom_cfg: + # Use a custom configuration for extlinux.conf + extlinux_conf = custom_cfg + logger.debug("Using custom configuration file " + "%s for extlinux.conf", configfile) + else: + raise WicError("configfile is specified but failed to " + "get it from %s." % configfile) + + if not custom_cfg: + # The kernel types supported by the sysboot of u-boot + kernel_types = ["zImage", "Image", "fitImage", "uImage", "vmlinux"] + has_dtb = False + fdt_dir = '/' + kernel_name = None + + # Find the kernel image name, from the highest precedence to lowest + for image in kernel_types: + for task in cls.install_task: + src, dst = task + if re.match(image, src): + kernel_name = os.path.join('/', dst) + break + if kernel_name: + break + + for task in cls.install_task: + src, dst = task + # We suppose that all the dtb are in the same directory + if re.search(r'\.dtb', src) and fdt_dir == '/': + has_dtb = True + fdt_dir = os.path.join(fdt_dir, os.path.dirname(dst)) + break + + if not kernel_name: + raise WicError('No kernel file found') + + # Compose the extlinux.conf + extlinux_conf = "default Yocto\n" + extlinux_conf += "label Yocto\n" + extlinux_conf += " kernel %s\n" % kernel_name + if has_dtb: + extlinux_conf += " fdtdir %s\n" % fdt_dir + bootloader = cr.ks.bootloader + extlinux_conf += "append root=%s rootwait %s\n" \ + % (cr.rootdev, bootloader.append if bootloader.append else '') + + install_cmd = "install -d %s/extlinux/" % hdddir + exec_cmd(install_cmd) + cfg = open("%s/extlinux/extlinux.conf" % hdddir, "w") + cfg.write(extlinux_conf) + cfg.close() + + + @classmethod + def do_prepare_partition(cls, part, source_params, cr, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + rootfs_dir, native_sysroot): + """ + Called to do the actual content population for a partition i.e. it + 'prepares' the partition to be incorporated into the image. + In this case, does the following: + - sets up a vfat partition + - copies all files listed in IMAGE_BOOT_FILES variable + """ + hdddir = "%s/boot.%d" % (cr_workdir, part.lineno) + + if not kernel_dir: + kernel_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") + if not kernel_dir: + raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") + + logger.debug('Kernel dir: %s', bootimg_dir) + + + for task in cls.install_task: + src_path, dst_path = task + logger.debug('Install %s as %s', src_path, dst_path) + install_cmd = "install -m 0644 -D %s %s" \ + % (os.path.join(kernel_dir, src_path), + os.path.join(hdddir, dst_path)) + exec_cmd(install_cmd) + + logger.debug('Prepare boot partition using rootfs in %s', hdddir) + part.prepare_rootfs(cr_workdir, oe_builddir, hdddir, + native_sysroot, False) diff --git a/scripts/lib/wic/plugins/source/bootimg_pcbios.py b/scripts/lib/wic/plugins/source/bootimg_pcbios.py new file mode 100644 index 0000000000..a207a83530 --- /dev/null +++ b/scripts/lib/wic/plugins/source/bootimg_pcbios.py @@ -0,0 +1,209 @@ +# +# Copyright (c) 2014, Intel Corporation. +# +# SPDX-License-Identifier: GPL-2.0-only +# +# DESCRIPTION +# This implements the 'bootimg-pcbios' source plugin class for 'wic' +# +# AUTHORS +# Tom Zanussi +# + +import logging +import os +import re + +from wic import WicError +from wic.engine import get_custom_config +from wic.pluginbase import SourcePlugin +from wic.misc import (exec_cmd, exec_native_cmd, + get_bitbake_var, BOOTDD_EXTRA_SPACE) + +logger = logging.getLogger('wic') + +class BootimgPcbiosPlugin(SourcePlugin): + """ + Create MBR boot partition and install syslinux on it. + """ + + name = 'bootimg-pcbios' + + @classmethod + def _get_bootimg_dir(cls, bootimg_dir, dirname): + """ + Check if dirname exists in default bootimg_dir or in STAGING_DIR. + """ + staging_datadir = get_bitbake_var("STAGING_DATADIR") + for result in (bootimg_dir, staging_datadir): + if os.path.exists("%s/%s" % (result, dirname)): + return result + + # STAGING_DATADIR is expanded with MLPREFIX if multilib is enabled + # but dependency syslinux is still populated to original STAGING_DATADIR + nonarch_datadir = re.sub('/[^/]*recipe-sysroot', '/recipe-sysroot', staging_datadir) + if os.path.exists(os.path.join(nonarch_datadir, dirname)): + return nonarch_datadir + + raise WicError("Couldn't find correct bootimg_dir, exiting") + + @classmethod + def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, + bootimg_dir, kernel_dir, native_sysroot): + """ + Called after all partitions have been prepared and assembled into a + disk image. In this case, we install the MBR. + """ + bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') + mbrfile = "%s/syslinux/" % bootimg_dir + if creator.ptable_format == 'msdos': + mbrfile += "mbr.bin" + elif creator.ptable_format == 'gpt': + mbrfile += "gptmbr.bin" + else: + raise WicError("Unsupported partition table: %s" % + creator.ptable_format) + + if not os.path.exists(mbrfile): + raise WicError("Couldn't find %s. If using the -e option, do you " + "have the right MACHINE set in local.conf? If not, " + "is the bootimg_dir path correct?" % mbrfile) + + full_path = creator._full_path(workdir, disk_name, "direct") + logger.debug("Installing MBR on disk %s as %s with size %s bytes", + disk_name, full_path, disk.min_size) + + dd_cmd = "dd if=%s of=%s conv=notrunc" % (mbrfile, full_path) + exec_cmd(dd_cmd, native_sysroot) + + @classmethod + def do_configure_partition(cls, part, source_params, creator, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + native_sysroot): + """ + Called before do_prepare_partition(), creates syslinux config + """ + hdddir = "%s/hdd/boot" % cr_workdir + + install_cmd = "install -d %s" % hdddir + exec_cmd(install_cmd) + + bootloader = creator.ks.bootloader + + custom_cfg = None + if bootloader.configfile: + custom_cfg = get_custom_config(bootloader.configfile) + if custom_cfg: + # Use a custom configuration for grub + syslinux_conf = custom_cfg + logger.debug("Using custom configuration file %s " + "for syslinux.cfg", bootloader.configfile) + else: + raise WicError("configfile is specified but failed to " + "get it from %s." % bootloader.configfile) + + if not custom_cfg: + # Create syslinux configuration using parameters from wks file + splash = os.path.join(cr_workdir, "/hdd/boot/splash.jpg") + if os.path.exists(splash): + splashline = "menu background splash.jpg" + else: + splashline = "" + + syslinux_conf = "" + syslinux_conf += "PROMPT 0\n" + syslinux_conf += "TIMEOUT " + str(bootloader.timeout) + "\n" + syslinux_conf += "\n" + syslinux_conf += "ALLOWOPTIONS 1\n" + syslinux_conf += "SERIAL 0 115200\n" + syslinux_conf += "\n" + if splashline: + syslinux_conf += "%s\n" % splashline + syslinux_conf += "DEFAULT boot\n" + syslinux_conf += "LABEL boot\n" + + kernel = "/" + get_bitbake_var("KERNEL_IMAGETYPE") + syslinux_conf += "KERNEL " + kernel + "\n" + + syslinux_conf += "APPEND label=boot root=%s %s\n" % \ + (creator.rootdev, bootloader.append) + + logger.debug("Writing syslinux config %s/hdd/boot/syslinux.cfg", + cr_workdir) + cfg = open("%s/hdd/boot/syslinux.cfg" % cr_workdir, "w") + cfg.write(syslinux_conf) + cfg.close() + + @classmethod + def do_prepare_partition(cls, part, source_params, creator, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + rootfs_dir, native_sysroot): + """ + Called to do the actual content population for a partition i.e. it + 'prepares' the partition to be incorporated into the image. + In this case, prepare content for legacy bios boot partition. + """ + bootimg_dir = cls._get_bootimg_dir(bootimg_dir, 'syslinux') + + staging_kernel_dir = kernel_dir + + hdddir = "%s/hdd/boot" % cr_workdir + + kernel = get_bitbake_var("KERNEL_IMAGETYPE") + if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": + if get_bitbake_var("INITRAMFS_IMAGE"): + kernel = "%s-%s.bin" % \ + (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) + + cmds = ("install -m 0644 %s/%s %s/%s" % + (staging_kernel_dir, kernel, hdddir, get_bitbake_var("KERNEL_IMAGETYPE")), + "install -m 444 %s/syslinux/ldlinux.sys %s/ldlinux.sys" % + (bootimg_dir, hdddir), + "install -m 0644 %s/syslinux/vesamenu.c32 %s/vesamenu.c32" % + (bootimg_dir, hdddir), + "install -m 444 %s/syslinux/libcom32.c32 %s/libcom32.c32" % + (bootimg_dir, hdddir), + "install -m 444 %s/syslinux/libutil.c32 %s/libutil.c32" % + (bootimg_dir, hdddir)) + + for install_cmd in cmds: + exec_cmd(install_cmd) + + du_cmd = "du -bks %s" % hdddir + out = exec_cmd(du_cmd) + blocks = int(out.split()[0]) + + extra_blocks = part.get_extra_block_count(blocks) + + if extra_blocks < BOOTDD_EXTRA_SPACE: + extra_blocks = BOOTDD_EXTRA_SPACE + + blocks += extra_blocks + + logger.debug("Added %d extra blocks to %s to get to %d total blocks", + extra_blocks, part.mountpoint, blocks) + + # dosfs image, created by mkdosfs + bootimg = "%s/boot%s.img" % (cr_workdir, part.lineno) + + label = part.label if part.label else "boot" + + dosfs_cmd = "mkdosfs -n %s -i %s -S 512 -C %s %d" % \ + (label, part.fsuuid, bootimg, blocks) + exec_native_cmd(dosfs_cmd, native_sysroot) + + mcopy_cmd = "mcopy -i %s -s %s/* ::/" % (bootimg, hdddir) + exec_native_cmd(mcopy_cmd, native_sysroot) + + syslinux_cmd = "syslinux %s" % bootimg + exec_native_cmd(syslinux_cmd, native_sysroot) + + chmod_cmd = "chmod 644 %s" % bootimg + exec_cmd(chmod_cmd) + + du_cmd = "du -Lbks %s" % bootimg + out = exec_cmd(du_cmd) + bootimg_size = out.split()[0] + + part.size = int(bootimg_size) + part.source_file = bootimg diff --git a/scripts/lib/wic/plugins/source/isoimage-isohybrid.py b/scripts/lib/wic/plugins/source/isoimage-isohybrid.py deleted file mode 100644 index 607356ad13..0000000000 --- a/scripts/lib/wic/plugins/source/isoimage-isohybrid.py +++ /dev/null @@ -1,463 +0,0 @@ -# -# Copyright OpenEmbedded Contributors -# -# SPDX-License-Identifier: GPL-2.0-only -# -# DESCRIPTION -# This implements the 'isoimage-isohybrid' source plugin class for 'wic' -# -# AUTHORS -# Mihaly Varga - -import glob -import logging -import os -import re -import shutil - -from wic import WicError -from wic.engine import get_custom_config -from wic.pluginbase import SourcePlugin -from wic.misc import exec_cmd, exec_native_cmd, get_bitbake_var - -logger = logging.getLogger('wic') - -class IsoImagePlugin(SourcePlugin): - """ - Create a bootable ISO image - - This plugin creates a hybrid, legacy and EFI bootable ISO image. The - generated image can be used on optical media as well as USB media. - - Legacy boot uses syslinux and EFI boot uses grub or gummiboot (not - implemented yet) as bootloader. The plugin creates the directories required - by bootloaders and populates them by creating and configuring the - bootloader files. - - Example kickstart file: - part /boot --source isoimage-isohybrid --sourceparams="loader=grub-efi, \\ - image_name= IsoImage" --ondisk cd --label LIVECD - bootloader --timeout=10 --append=" " - - In --sourceparams "loader" specifies the bootloader used for booting in EFI - mode, while "image_name" specifies the name of the generated image. In the - example above, wic creates an ISO image named IsoImage-cd.direct (default - extension added by direct imeger plugin) and a file named IsoImage-cd.iso - """ - - name = 'isoimage-isohybrid' - - @classmethod - def do_configure_syslinux(cls, creator, cr_workdir): - """ - Create loader-specific (syslinux) config - """ - splash = os.path.join(cr_workdir, "ISO/boot/splash.jpg") - if os.path.exists(splash): - splashline = "menu background splash.jpg" - else: - splashline = "" - - bootloader = creator.ks.bootloader - - syslinux_conf = "" - syslinux_conf += "PROMPT 0\n" - syslinux_conf += "TIMEOUT %s \n" % (bootloader.timeout or 10) - syslinux_conf += "\n" - syslinux_conf += "ALLOWOPTIONS 1\n" - syslinux_conf += "SERIAL 0 115200\n" - syslinux_conf += "\n" - if splashline: - syslinux_conf += "%s\n" % splashline - syslinux_conf += "DEFAULT boot\n" - syslinux_conf += "LABEL boot\n" - - kernel = get_bitbake_var("KERNEL_IMAGETYPE") - if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": - if get_bitbake_var("INITRAMFS_IMAGE"): - kernel = "%s-%s.bin" % \ - (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) - - syslinux_conf += "KERNEL /" + kernel + "\n" - syslinux_conf += "APPEND initrd=/initrd LABEL=boot %s\n" \ - % bootloader.append - - logger.debug("Writing syslinux config %s/ISO/isolinux/isolinux.cfg", - cr_workdir) - - with open("%s/ISO/isolinux/isolinux.cfg" % cr_workdir, "w") as cfg: - cfg.write(syslinux_conf) - - @classmethod - def do_configure_grubefi(cls, part, creator, target_dir): - """ - Create loader-specific (grub-efi) config - """ - configfile = creator.ks.bootloader.configfile - if configfile: - grubefi_conf = get_custom_config(configfile) - if grubefi_conf: - logger.debug("Using custom configuration file %s for grub.cfg", - configfile) - else: - raise WicError("configfile is specified " - "but failed to get it from %s", configfile) - else: - splash = os.path.join(target_dir, "splash.jpg") - if os.path.exists(splash): - splashline = "menu background splash.jpg" - else: - splashline = "" - - bootloader = creator.ks.bootloader - - grubefi_conf = "" - grubefi_conf += "serial --unit=0 --speed=115200 --word=8 " - grubefi_conf += "--parity=no --stop=1\n" - grubefi_conf += "default=boot\n" - grubefi_conf += "timeout=%s\n" % (bootloader.timeout or 10) - grubefi_conf += "\n" - grubefi_conf += "search --set=root --label %s " % part.label - grubefi_conf += "\n" - grubefi_conf += "menuentry 'boot'{\n" - - kernel = get_bitbake_var("KERNEL_IMAGETYPE") - if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": - if get_bitbake_var("INITRAMFS_IMAGE"): - kernel = "%s-%s.bin" % \ - (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) - - grubefi_conf += "linux /%s rootwait %s\n" \ - % (kernel, bootloader.append) - grubefi_conf += "initrd /initrd \n" - grubefi_conf += "}\n" - - if splashline: - grubefi_conf += "%s\n" % splashline - - cfg_path = os.path.join(target_dir, "grub.cfg") - logger.debug("Writing grubefi config %s", cfg_path) - - with open(cfg_path, "w") as cfg: - cfg.write(grubefi_conf) - - @staticmethod - def _build_initramfs_path(rootfs_dir, cr_workdir): - """ - Create path for initramfs image - """ - - initrd = get_bitbake_var("INITRD_LIVE") or get_bitbake_var("INITRD") - if not initrd: - initrd_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") - if not initrd_dir: - raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting.") - - image_name = get_bitbake_var("IMAGE_BASENAME") - if not image_name: - raise WicError("Couldn't find IMAGE_BASENAME, exiting.") - - image_type = get_bitbake_var("INITRAMFS_FSTYPES") - if not image_type: - raise WicError("Couldn't find INITRAMFS_FSTYPES, exiting.") - - machine = os.path.basename(initrd_dir) - - pattern = '%s/%s*%s.%s' % (initrd_dir, image_name, machine, image_type) - files = glob.glob(pattern) - if files: - initrd = files[0] - - if not initrd or not os.path.exists(initrd): - # Create initrd from rootfs directory - initrd = "%s/initrd.cpio.gz" % cr_workdir - initrd_dir = "%s/INITRD" % cr_workdir - shutil.copytree("%s" % rootfs_dir, \ - "%s" % initrd_dir, symlinks=True) - - if os.path.isfile("%s/init" % rootfs_dir): - shutil.copy2("%s/init" % rootfs_dir, "%s/init" % initrd_dir) - elif os.path.lexists("%s/init" % rootfs_dir): - os.symlink(os.readlink("%s/init" % rootfs_dir), \ - "%s/init" % initrd_dir) - elif os.path.isfile("%s/sbin/init" % rootfs_dir): - shutil.copy2("%s/sbin/init" % rootfs_dir, \ - "%s" % initrd_dir) - elif os.path.lexists("%s/sbin/init" % rootfs_dir): - os.symlink(os.readlink("%s/sbin/init" % rootfs_dir), \ - "%s/init" % initrd_dir) - else: - raise WicError("Couldn't find or build initrd, exiting.") - - exec_cmd("cd %s && find . | cpio -o -H newc -R root:root >%s/initrd.cpio " \ - % (initrd_dir, cr_workdir), as_shell=True) - exec_cmd("gzip -f -9 %s/initrd.cpio" % cr_workdir, as_shell=True) - shutil.rmtree(initrd_dir) - - return initrd - - @classmethod - def do_configure_partition(cls, part, source_params, creator, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - native_sysroot): - """ - Called before do_prepare_partition(), creates loader-specific config - """ - isodir = "%s/ISO/" % cr_workdir - - if os.path.exists(isodir): - shutil.rmtree(isodir) - - install_cmd = "install -d %s " % isodir - exec_cmd(install_cmd) - - # Overwrite the name of the created image - logger.debug(source_params) - if 'image_name' in source_params and \ - source_params['image_name'].strip(): - creator.name = source_params['image_name'].strip() - logger.debug("The name of the image is: %s", creator.name) - - @staticmethod - def _install_payload(source_params, iso_dir): - """ - Copies contents of payload directory (as specified in 'payload_dir' param) into iso_dir - """ - - if source_params.get('payload_dir'): - payload_dir = source_params['payload_dir'] - - logger.debug("Payload directory: %s", payload_dir) - shutil.copytree(payload_dir, iso_dir, symlinks=True, dirs_exist_ok=True) - - @classmethod - def do_prepare_partition(cls, part, source_params, creator, cr_workdir, - oe_builddir, bootimg_dir, kernel_dir, - rootfs_dir, native_sysroot): - """ - Called to do the actual content population for a partition i.e. it - 'prepares' the partition to be incorporated into the image. - In this case, prepare content for a bootable ISO image. - """ - - isodir = "%s/ISO" % cr_workdir - - cls._install_payload(source_params, isodir) - - if part.rootfs_dir is None: - if not 'ROOTFS_DIR' in rootfs_dir: - raise WicError("Couldn't find --rootfs-dir, exiting.") - rootfs_dir = rootfs_dir['ROOTFS_DIR'] - else: - if part.rootfs_dir in rootfs_dir: - rootfs_dir = rootfs_dir[part.rootfs_dir] - elif part.rootfs_dir: - rootfs_dir = part.rootfs_dir - else: - raise WicError("Couldn't find --rootfs-dir=%s connection " - "or it is not a valid path, exiting." % - part.rootfs_dir) - - if not os.path.isdir(rootfs_dir): - rootfs_dir = get_bitbake_var("IMAGE_ROOTFS") - if not os.path.isdir(rootfs_dir): - raise WicError("Couldn't find IMAGE_ROOTFS, exiting.") - - part.rootfs_dir = rootfs_dir - deploy_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") - img_iso_dir = get_bitbake_var("ISODIR") - - # Remove the temporary file created by part.prepare_rootfs() - if os.path.isfile(part.source_file): - os.remove(part.source_file) - - # Support using a different initrd other than default - if source_params.get('initrd'): - initrd = source_params['initrd'] - if not deploy_dir: - raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") - cp_cmd = "cp %s/%s %s" % (deploy_dir, initrd, cr_workdir) - exec_cmd(cp_cmd) - else: - # Prepare initial ramdisk - initrd = "%s/initrd" % deploy_dir - if not os.path.isfile(initrd): - initrd = "%s/initrd" % img_iso_dir - if not os.path.isfile(initrd): - initrd = cls._build_initramfs_path(rootfs_dir, cr_workdir) - - install_cmd = "install -m 0644 %s %s/initrd" % (initrd, isodir) - exec_cmd(install_cmd) - - # Remove the temporary file created by _build_initramfs_path function - if os.path.isfile("%s/initrd.cpio.gz" % cr_workdir): - os.remove("%s/initrd.cpio.gz" % cr_workdir) - - kernel = get_bitbake_var("KERNEL_IMAGETYPE") - if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": - if get_bitbake_var("INITRAMFS_IMAGE"): - kernel = "%s-%s.bin" % \ - (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) - - install_cmd = "install -m 0644 %s/%s %s/%s" % \ - (kernel_dir, kernel, isodir, kernel) - exec_cmd(install_cmd) - - #Create bootloader for efi boot - try: - target_dir = "%s/EFI/BOOT" % isodir - if os.path.exists(target_dir): - shutil.rmtree(target_dir) - - os.makedirs(target_dir) - - if source_params['loader'] == 'grub-efi': - # Builds bootx64.efi/bootia32.efi if ISODIR didn't exist or - # didn't contains it - target_arch = get_bitbake_var("TARGET_SYS") - if not target_arch: - raise WicError("Coludn't find target architecture") - - if re.match("x86_64", target_arch): - grub_src_image = "grub-efi-bootx64.efi" - grub_dest_image = "bootx64.efi" - elif re.match('i.86', target_arch): - grub_src_image = "grub-efi-bootia32.efi" - grub_dest_image = "bootia32.efi" - else: - raise WicError("grub-efi is incompatible with target %s" % - target_arch) - - grub_target = os.path.join(target_dir, grub_dest_image) - if not os.path.isfile(grub_target): - grub_src = os.path.join(deploy_dir, grub_src_image) - if not os.path.exists(grub_src): - raise WicError("Grub loader %s is not found in %s. " - "Please build grub-efi first" % (grub_src_image, deploy_dir)) - shutil.copy(grub_src, grub_target) - - if not os.path.isfile(os.path.join(target_dir, "boot.cfg")): - cls.do_configure_grubefi(part, creator, target_dir) - - else: - raise WicError("unrecognized bootimg-efi loader: %s" % - source_params['loader']) - except KeyError: - raise WicError("bootimg-efi requires a loader, none specified") - - # Create efi.img that contains bootloader files for EFI booting - # if ISODIR didn't exist or didn't contains it - if os.path.isfile("%s/efi.img" % img_iso_dir): - install_cmd = "install -m 0644 %s/efi.img %s/efi.img" % \ - (img_iso_dir, isodir) - exec_cmd(install_cmd) - else: - # Default to 100 blocks of extra space for file system overhead - esp_extra_blocks = int(source_params.get('esp_extra_blocks', '100')) - - du_cmd = "du -bks %s/EFI" % isodir - out = exec_cmd(du_cmd) - blocks = int(out.split()[0]) - blocks += esp_extra_blocks - logger.debug("Added 100 extra blocks to %s to get to %d " - "total blocks", part.mountpoint, blocks) - - # dosfs image for EFI boot - bootimg = "%s/efi.img" % isodir - - esp_label = source_params.get('esp_label', 'EFIimg') - - dosfs_cmd = 'mkfs.vfat -n \'%s\' -S 512 -C %s %d' \ - % (esp_label, bootimg, blocks) - exec_native_cmd(dosfs_cmd, native_sysroot) - - mmd_cmd = "mmd -i %s ::/EFI" % bootimg - exec_native_cmd(mmd_cmd, native_sysroot) - - mcopy_cmd = "mcopy -i %s -s %s/EFI/* ::/EFI/" \ - % (bootimg, isodir) - exec_native_cmd(mcopy_cmd, native_sysroot) - - chmod_cmd = "chmod 644 %s" % bootimg - exec_cmd(chmod_cmd) - - # Prepare files for legacy boot - syslinux_dir = get_bitbake_var("STAGING_DATADIR") - if not syslinux_dir: - raise WicError("Couldn't find STAGING_DATADIR, exiting.") - - if os.path.exists("%s/isolinux" % isodir): - shutil.rmtree("%s/isolinux" % isodir) - - install_cmd = "install -d %s/isolinux" % isodir - exec_cmd(install_cmd) - - cls.do_configure_syslinux(creator, cr_workdir) - - install_cmd = "install -m 444 %s/syslinux/ldlinux.sys " % syslinux_dir - install_cmd += "%s/isolinux/ldlinux.sys" % isodir - exec_cmd(install_cmd) - - install_cmd = "install -m 444 %s/syslinux/isohdpfx.bin " % syslinux_dir - install_cmd += "%s/isolinux/isohdpfx.bin" % isodir - exec_cmd(install_cmd) - - install_cmd = "install -m 644 %s/syslinux/isolinux.bin " % syslinux_dir - install_cmd += "%s/isolinux/isolinux.bin" % isodir - exec_cmd(install_cmd) - - install_cmd = "install -m 644 %s/syslinux/ldlinux.c32 " % syslinux_dir - install_cmd += "%s/isolinux/ldlinux.c32" % isodir - exec_cmd(install_cmd) - - #create ISO image - iso_img = "%s/tempiso_img.iso" % cr_workdir - iso_bootimg = "isolinux/isolinux.bin" - iso_bootcat = "isolinux/boot.cat" - efi_img = "efi.img" - - mkisofs_cmd = "mkisofs -V %s " % part.label - mkisofs_cmd += "-o %s -U " % iso_img - mkisofs_cmd += "-J -joliet-long -r -iso-level 2 -b %s " % iso_bootimg - mkisofs_cmd += "-c %s -no-emul-boot -boot-load-size 4 " % iso_bootcat - mkisofs_cmd += "-boot-info-table -eltorito-alt-boot " - mkisofs_cmd += "-eltorito-platform 0xEF -eltorito-boot %s " % efi_img - mkisofs_cmd += "-no-emul-boot %s " % isodir - - logger.debug("running command: %s", mkisofs_cmd) - exec_native_cmd(mkisofs_cmd, native_sysroot) - - shutil.rmtree(isodir) - - du_cmd = "du -Lbks %s" % iso_img - out = exec_cmd(du_cmd) - isoimg_size = int(out.split()[0]) - - part.size = isoimg_size - part.source_file = iso_img - - @classmethod - def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, - bootimg_dir, kernel_dir, native_sysroot): - """ - Called after all partitions have been prepared and assembled into a - disk image. In this case, we insert/modify the MBR using isohybrid - utility for booting via BIOS from disk storage devices. - """ - - iso_img = "%s.p1" % disk.path - full_path = creator._full_path(workdir, disk_name, "direct") - full_path_iso = creator._full_path(workdir, disk_name, "iso") - - isohybrid_cmd = "isohybrid -u %s" % iso_img - logger.debug("running command: %s", isohybrid_cmd) - exec_native_cmd(isohybrid_cmd, native_sysroot) - - # Replace the image created by direct plugin with the one created by - # mkisofs command. This is necessary because the iso image created by - # mkisofs has a very specific MBR is system area of the ISO image, and - # direct plugin adds and configures an another MBR. - logger.debug("Replaceing the image created by direct plugin\n") - os.remove(disk.path) - shutil.copy2(iso_img, full_path_iso) - shutil.copy2(full_path_iso, full_path) diff --git a/scripts/lib/wic/plugins/source/isoimage_isohybrid.py b/scripts/lib/wic/plugins/source/isoimage_isohybrid.py new file mode 100644 index 0000000000..607356ad13 --- /dev/null +++ b/scripts/lib/wic/plugins/source/isoimage_isohybrid.py @@ -0,0 +1,463 @@ +# +# Copyright OpenEmbedded Contributors +# +# SPDX-License-Identifier: GPL-2.0-only +# +# DESCRIPTION +# This implements the 'isoimage-isohybrid' source plugin class for 'wic' +# +# AUTHORS +# Mihaly Varga + +import glob +import logging +import os +import re +import shutil + +from wic import WicError +from wic.engine import get_custom_config +from wic.pluginbase import SourcePlugin +from wic.misc import exec_cmd, exec_native_cmd, get_bitbake_var + +logger = logging.getLogger('wic') + +class IsoImagePlugin(SourcePlugin): + """ + Create a bootable ISO image + + This plugin creates a hybrid, legacy and EFI bootable ISO image. The + generated image can be used on optical media as well as USB media. + + Legacy boot uses syslinux and EFI boot uses grub or gummiboot (not + implemented yet) as bootloader. The plugin creates the directories required + by bootloaders and populates them by creating and configuring the + bootloader files. + + Example kickstart file: + part /boot --source isoimage-isohybrid --sourceparams="loader=grub-efi, \\ + image_name= IsoImage" --ondisk cd --label LIVECD + bootloader --timeout=10 --append=" " + + In --sourceparams "loader" specifies the bootloader used for booting in EFI + mode, while "image_name" specifies the name of the generated image. In the + example above, wic creates an ISO image named IsoImage-cd.direct (default + extension added by direct imeger plugin) and a file named IsoImage-cd.iso + """ + + name = 'isoimage-isohybrid' + + @classmethod + def do_configure_syslinux(cls, creator, cr_workdir): + """ + Create loader-specific (syslinux) config + """ + splash = os.path.join(cr_workdir, "ISO/boot/splash.jpg") + if os.path.exists(splash): + splashline = "menu background splash.jpg" + else: + splashline = "" + + bootloader = creator.ks.bootloader + + syslinux_conf = "" + syslinux_conf += "PROMPT 0\n" + syslinux_conf += "TIMEOUT %s \n" % (bootloader.timeout or 10) + syslinux_conf += "\n" + syslinux_conf += "ALLOWOPTIONS 1\n" + syslinux_conf += "SERIAL 0 115200\n" + syslinux_conf += "\n" + if splashline: + syslinux_conf += "%s\n" % splashline + syslinux_conf += "DEFAULT boot\n" + syslinux_conf += "LABEL boot\n" + + kernel = get_bitbake_var("KERNEL_IMAGETYPE") + if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": + if get_bitbake_var("INITRAMFS_IMAGE"): + kernel = "%s-%s.bin" % \ + (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) + + syslinux_conf += "KERNEL /" + kernel + "\n" + syslinux_conf += "APPEND initrd=/initrd LABEL=boot %s\n" \ + % bootloader.append + + logger.debug("Writing syslinux config %s/ISO/isolinux/isolinux.cfg", + cr_workdir) + + with open("%s/ISO/isolinux/isolinux.cfg" % cr_workdir, "w") as cfg: + cfg.write(syslinux_conf) + + @classmethod + def do_configure_grubefi(cls, part, creator, target_dir): + """ + Create loader-specific (grub-efi) config + """ + configfile = creator.ks.bootloader.configfile + if configfile: + grubefi_conf = get_custom_config(configfile) + if grubefi_conf: + logger.debug("Using custom configuration file %s for grub.cfg", + configfile) + else: + raise WicError("configfile is specified " + "but failed to get it from %s", configfile) + else: + splash = os.path.join(target_dir, "splash.jpg") + if os.path.exists(splash): + splashline = "menu background splash.jpg" + else: + splashline = "" + + bootloader = creator.ks.bootloader + + grubefi_conf = "" + grubefi_conf += "serial --unit=0 --speed=115200 --word=8 " + grubefi_conf += "--parity=no --stop=1\n" + grubefi_conf += "default=boot\n" + grubefi_conf += "timeout=%s\n" % (bootloader.timeout or 10) + grubefi_conf += "\n" + grubefi_conf += "search --set=root --label %s " % part.label + grubefi_conf += "\n" + grubefi_conf += "menuentry 'boot'{\n" + + kernel = get_bitbake_var("KERNEL_IMAGETYPE") + if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": + if get_bitbake_var("INITRAMFS_IMAGE"): + kernel = "%s-%s.bin" % \ + (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) + + grubefi_conf += "linux /%s rootwait %s\n" \ + % (kernel, bootloader.append) + grubefi_conf += "initrd /initrd \n" + grubefi_conf += "}\n" + + if splashline: + grubefi_conf += "%s\n" % splashline + + cfg_path = os.path.join(target_dir, "grub.cfg") + logger.debug("Writing grubefi config %s", cfg_path) + + with open(cfg_path, "w") as cfg: + cfg.write(grubefi_conf) + + @staticmethod + def _build_initramfs_path(rootfs_dir, cr_workdir): + """ + Create path for initramfs image + """ + + initrd = get_bitbake_var("INITRD_LIVE") or get_bitbake_var("INITRD") + if not initrd: + initrd_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") + if not initrd_dir: + raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting.") + + image_name = get_bitbake_var("IMAGE_BASENAME") + if not image_name: + raise WicError("Couldn't find IMAGE_BASENAME, exiting.") + + image_type = get_bitbake_var("INITRAMFS_FSTYPES") + if not image_type: + raise WicError("Couldn't find INITRAMFS_FSTYPES, exiting.") + + machine = os.path.basename(initrd_dir) + + pattern = '%s/%s*%s.%s' % (initrd_dir, image_name, machine, image_type) + files = glob.glob(pattern) + if files: + initrd = files[0] + + if not initrd or not os.path.exists(initrd): + # Create initrd from rootfs directory + initrd = "%s/initrd.cpio.gz" % cr_workdir + initrd_dir = "%s/INITRD" % cr_workdir + shutil.copytree("%s" % rootfs_dir, \ + "%s" % initrd_dir, symlinks=True) + + if os.path.isfile("%s/init" % rootfs_dir): + shutil.copy2("%s/init" % rootfs_dir, "%s/init" % initrd_dir) + elif os.path.lexists("%s/init" % rootfs_dir): + os.symlink(os.readlink("%s/init" % rootfs_dir), \ + "%s/init" % initrd_dir) + elif os.path.isfile("%s/sbin/init" % rootfs_dir): + shutil.copy2("%s/sbin/init" % rootfs_dir, \ + "%s" % initrd_dir) + elif os.path.lexists("%s/sbin/init" % rootfs_dir): + os.symlink(os.readlink("%s/sbin/init" % rootfs_dir), \ + "%s/init" % initrd_dir) + else: + raise WicError("Couldn't find or build initrd, exiting.") + + exec_cmd("cd %s && find . | cpio -o -H newc -R root:root >%s/initrd.cpio " \ + % (initrd_dir, cr_workdir), as_shell=True) + exec_cmd("gzip -f -9 %s/initrd.cpio" % cr_workdir, as_shell=True) + shutil.rmtree(initrd_dir) + + return initrd + + @classmethod + def do_configure_partition(cls, part, source_params, creator, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + native_sysroot): + """ + Called before do_prepare_partition(), creates loader-specific config + """ + isodir = "%s/ISO/" % cr_workdir + + if os.path.exists(isodir): + shutil.rmtree(isodir) + + install_cmd = "install -d %s " % isodir + exec_cmd(install_cmd) + + # Overwrite the name of the created image + logger.debug(source_params) + if 'image_name' in source_params and \ + source_params['image_name'].strip(): + creator.name = source_params['image_name'].strip() + logger.debug("The name of the image is: %s", creator.name) + + @staticmethod + def _install_payload(source_params, iso_dir): + """ + Copies contents of payload directory (as specified in 'payload_dir' param) into iso_dir + """ + + if source_params.get('payload_dir'): + payload_dir = source_params['payload_dir'] + + logger.debug("Payload directory: %s", payload_dir) + shutil.copytree(payload_dir, iso_dir, symlinks=True, dirs_exist_ok=True) + + @classmethod + def do_prepare_partition(cls, part, source_params, creator, cr_workdir, + oe_builddir, bootimg_dir, kernel_dir, + rootfs_dir, native_sysroot): + """ + Called to do the actual content population for a partition i.e. it + 'prepares' the partition to be incorporated into the image. + In this case, prepare content for a bootable ISO image. + """ + + isodir = "%s/ISO" % cr_workdir + + cls._install_payload(source_params, isodir) + + if part.rootfs_dir is None: + if not 'ROOTFS_DIR' in rootfs_dir: + raise WicError("Couldn't find --rootfs-dir, exiting.") + rootfs_dir = rootfs_dir['ROOTFS_DIR'] + else: + if part.rootfs_dir in rootfs_dir: + rootfs_dir = rootfs_dir[part.rootfs_dir] + elif part.rootfs_dir: + rootfs_dir = part.rootfs_dir + else: + raise WicError("Couldn't find --rootfs-dir=%s connection " + "or it is not a valid path, exiting." % + part.rootfs_dir) + + if not os.path.isdir(rootfs_dir): + rootfs_dir = get_bitbake_var("IMAGE_ROOTFS") + if not os.path.isdir(rootfs_dir): + raise WicError("Couldn't find IMAGE_ROOTFS, exiting.") + + part.rootfs_dir = rootfs_dir + deploy_dir = get_bitbake_var("DEPLOY_DIR_IMAGE") + img_iso_dir = get_bitbake_var("ISODIR") + + # Remove the temporary file created by part.prepare_rootfs() + if os.path.isfile(part.source_file): + os.remove(part.source_file) + + # Support using a different initrd other than default + if source_params.get('initrd'): + initrd = source_params['initrd'] + if not deploy_dir: + raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting") + cp_cmd = "cp %s/%s %s" % (deploy_dir, initrd, cr_workdir) + exec_cmd(cp_cmd) + else: + # Prepare initial ramdisk + initrd = "%s/initrd" % deploy_dir + if not os.path.isfile(initrd): + initrd = "%s/initrd" % img_iso_dir + if not os.path.isfile(initrd): + initrd = cls._build_initramfs_path(rootfs_dir, cr_workdir) + + install_cmd = "install -m 0644 %s %s/initrd" % (initrd, isodir) + exec_cmd(install_cmd) + + # Remove the temporary file created by _build_initramfs_path function + if os.path.isfile("%s/initrd.cpio.gz" % cr_workdir): + os.remove("%s/initrd.cpio.gz" % cr_workdir) + + kernel = get_bitbake_var("KERNEL_IMAGETYPE") + if get_bitbake_var("INITRAMFS_IMAGE_BUNDLE") == "1": + if get_bitbake_var("INITRAMFS_IMAGE"): + kernel = "%s-%s.bin" % \ + (get_bitbake_var("KERNEL_IMAGETYPE"), get_bitbake_var("INITRAMFS_LINK_NAME")) + + install_cmd = "install -m 0644 %s/%s %s/%s" % \ + (kernel_dir, kernel, isodir, kernel) + exec_cmd(install_cmd) + + #Create bootloader for efi boot + try: + target_dir = "%s/EFI/BOOT" % isodir + if os.path.exists(target_dir): + shutil.rmtree(target_dir) + + os.makedirs(target_dir) + + if source_params['loader'] == 'grub-efi': + # Builds bootx64.efi/bootia32.efi if ISODIR didn't exist or + # didn't contains it + target_arch = get_bitbake_var("TARGET_SYS") + if not target_arch: + raise WicError("Coludn't find target architecture") + + if re.match("x86_64", target_arch): + grub_src_image = "grub-efi-bootx64.efi" + grub_dest_image = "bootx64.efi" + elif re.match('i.86', target_arch): + grub_src_image = "grub-efi-bootia32.efi" + grub_dest_image = "bootia32.efi" + else: + raise WicError("grub-efi is incompatible with target %s" % + target_arch) + + grub_target = os.path.join(target_dir, grub_dest_image) + if not os.path.isfile(grub_target): + grub_src = os.path.join(deploy_dir, grub_src_image) + if not os.path.exists(grub_src): + raise WicError("Grub loader %s is not found in %s. " + "Please build grub-efi first" % (grub_src_image, deploy_dir)) + shutil.copy(grub_src, grub_target) + + if not os.path.isfile(os.path.join(target_dir, "boot.cfg")): + cls.do_configure_grubefi(part, creator, target_dir) + + else: + raise WicError("unrecognized bootimg-efi loader: %s" % + source_params['loader']) + except KeyError: + raise WicError("bootimg-efi requires a loader, none specified") + + # Create efi.img that contains bootloader files for EFI booting + # if ISODIR didn't exist or didn't contains it + if os.path.isfile("%s/efi.img" % img_iso_dir): + install_cmd = "install -m 0644 %s/efi.img %s/efi.img" % \ + (img_iso_dir, isodir) + exec_cmd(install_cmd) + else: + # Default to 100 blocks of extra space for file system overhead + esp_extra_blocks = int(source_params.get('esp_extra_blocks', '100')) + + du_cmd = "du -bks %s/EFI" % isodir + out = exec_cmd(du_cmd) + blocks = int(out.split()[0]) + blocks += esp_extra_blocks + logger.debug("Added 100 extra blocks to %s to get to %d " + "total blocks", part.mountpoint, blocks) + + # dosfs image for EFI boot + bootimg = "%s/efi.img" % isodir + + esp_label = source_params.get('esp_label', 'EFIimg') + + dosfs_cmd = 'mkfs.vfat -n \'%s\' -S 512 -C %s %d' \ + % (esp_label, bootimg, blocks) + exec_native_cmd(dosfs_cmd, native_sysroot) + + mmd_cmd = "mmd -i %s ::/EFI" % bootimg + exec_native_cmd(mmd_cmd, native_sysroot) + + mcopy_cmd = "mcopy -i %s -s %s/EFI/* ::/EFI/" \ + % (bootimg, isodir) + exec_native_cmd(mcopy_cmd, native_sysroot) + + chmod_cmd = "chmod 644 %s" % bootimg + exec_cmd(chmod_cmd) + + # Prepare files for legacy boot + syslinux_dir = get_bitbake_var("STAGING_DATADIR") + if not syslinux_dir: + raise WicError("Couldn't find STAGING_DATADIR, exiting.") + + if os.path.exists("%s/isolinux" % isodir): + shutil.rmtree("%s/isolinux" % isodir) + + install_cmd = "install -d %s/isolinux" % isodir + exec_cmd(install_cmd) + + cls.do_configure_syslinux(creator, cr_workdir) + + install_cmd = "install -m 444 %s/syslinux/ldlinux.sys " % syslinux_dir + install_cmd += "%s/isolinux/ldlinux.sys" % isodir + exec_cmd(install_cmd) + + install_cmd = "install -m 444 %s/syslinux/isohdpfx.bin " % syslinux_dir + install_cmd += "%s/isolinux/isohdpfx.bin" % isodir + exec_cmd(install_cmd) + + install_cmd = "install -m 644 %s/syslinux/isolinux.bin " % syslinux_dir + install_cmd += "%s/isolinux/isolinux.bin" % isodir + exec_cmd(install_cmd) + + install_cmd = "install -m 644 %s/syslinux/ldlinux.c32 " % syslinux_dir + install_cmd += "%s/isolinux/ldlinux.c32" % isodir + exec_cmd(install_cmd) + + #create ISO image + iso_img = "%s/tempiso_img.iso" % cr_workdir + iso_bootimg = "isolinux/isolinux.bin" + iso_bootcat = "isolinux/boot.cat" + efi_img = "efi.img" + + mkisofs_cmd = "mkisofs -V %s " % part.label + mkisofs_cmd += "-o %s -U " % iso_img + mkisofs_cmd += "-J -joliet-long -r -iso-level 2 -b %s " % iso_bootimg + mkisofs_cmd += "-c %s -no-emul-boot -boot-load-size 4 " % iso_bootcat + mkisofs_cmd += "-boot-info-table -eltorito-alt-boot " + mkisofs_cmd += "-eltorito-platform 0xEF -eltorito-boot %s " % efi_img + mkisofs_cmd += "-no-emul-boot %s " % isodir + + logger.debug("running command: %s", mkisofs_cmd) + exec_native_cmd(mkisofs_cmd, native_sysroot) + + shutil.rmtree(isodir) + + du_cmd = "du -Lbks %s" % iso_img + out = exec_cmd(du_cmd) + isoimg_size = int(out.split()[0]) + + part.size = isoimg_size + part.source_file = iso_img + + @classmethod + def do_install_disk(cls, disk, disk_name, creator, workdir, oe_builddir, + bootimg_dir, kernel_dir, native_sysroot): + """ + Called after all partitions have been prepared and assembled into a + disk image. In this case, we insert/modify the MBR using isohybrid + utility for booting via BIOS from disk storage devices. + """ + + iso_img = "%s.p1" % disk.path + full_path = creator._full_path(workdir, disk_name, "direct") + full_path_iso = creator._full_path(workdir, disk_name, "iso") + + isohybrid_cmd = "isohybrid -u %s" % iso_img + logger.debug("running command: %s", isohybrid_cmd) + exec_native_cmd(isohybrid_cmd, native_sysroot) + + # Replace the image created by direct plugin with the one created by + # mkisofs command. This is necessary because the iso image created by + # mkisofs has a very specific MBR is system area of the ISO image, and + # direct plugin adds and configures an another MBR. + logger.debug("Replaceing the image created by direct plugin\n") + os.remove(disk.path) + shutil.copy2(iso_img, full_path_iso) + shutil.copy2(full_path_iso, full_path) -- cgit v1.2.3-54-g00ecf