public inbox for isar-users@googlegroups.com
 help / color / mirror / Atom feed
From: Henning Schild <henning.schild@siemens.com>
To: isar-users@googlegroups.com
Cc: Henning Schild <henning.schild@siemens.com>
Subject: [PATCH 3/9] wic: add a bootimg-efi-isar plugin outside the wic tree
Date: Wed, 31 Jan 2018 10:41:54 +0100	[thread overview]
Message-ID: <5813b0e31a65669b6e27294b70050e3e70626b57.1517390790.git.henning.schild@siemens.com> (raw)
In-Reply-To: <cover.1517390790.git.henning.schild@siemens.com>
In-Reply-To: <cover.1517390790.git.henning.schild@siemens.com>

The added plugin is a copy of the modified version in our wic. Move it
out and rename it so we can get back to a clean wic.

Issues:
 - wic in Isar has been messed with

Impact:
 This patch prepares for the Issue beeing solved, because now the plugin
 is outside the wic-tree. Nothing changes for users.

Signed-off-by: Henning Schild <henning.schild@siemens.com>
---
 .../scripts/lib/wic/canned-wks/sdimage-efi.wks     |   2 +-
 .../lib/wic/plugins/source/bootimg-efi-isar.py     | 307 +++++++++++++++++++++
 2 files changed, 308 insertions(+), 1 deletion(-)
 create mode 100644 meta-isar/scripts/lib/wic/plugins/source/bootimg-efi-isar.py

diff --git a/meta-isar/scripts/lib/wic/canned-wks/sdimage-efi.wks b/meta-isar/scripts/lib/wic/canned-wks/sdimage-efi.wks
index 1171a26..580ad21 100644
--- a/meta-isar/scripts/lib/wic/canned-wks/sdimage-efi.wks
+++ b/meta-isar/scripts/lib/wic/canned-wks/sdimage-efi.wks
@@ -2,7 +2,7 @@
 # long-description: Creates a partitioned EFI disk image without any swap that
 # the user can directly dd to boot media.
 
-part /boot --source bootimg-efi --sourceparams="loader=grub-efi" --ondisk sda --label msdos --active --align 1024
+part /boot --source bootimg-efi-isar --sourceparams="loader=grub-efi" --ondisk sda --label msdos --active --align 1024
 
 part / --source rootfs --ondisk sda --fstype=ext4 --label platform --align 1024 --use-uuid
 
diff --git a/meta-isar/scripts/lib/wic/plugins/source/bootimg-efi-isar.py b/meta-isar/scripts/lib/wic/plugins/source/bootimg-efi-isar.py
new file mode 100644
index 0000000..68ff67b
--- /dev/null
+++ b/meta-isar/scripts/lib/wic/plugins/source/bootimg-efi-isar.py
@@ -0,0 +1,307 @@
+# ex:ts=4:sw=4:sts=4:et
+# -*- tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*-
+#
+# Copyright (c) 2014, Intel Corporation.
+# All rights reserved.
+#
+# 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-efi-isar' source plugin class for 'wic'
+#
+# AUTHORS
+# Tom Zanussi <tom.zanussi (at] linux.intel.com>
+#
+
+import logging
+import os
+import shutil
+
+from wic import WicError
+from wic.engine import get_custom_config
+from wic.pluginbase import SourcePlugin
+from wic.utils.misc import (exec_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-isar'
+
+    @classmethod
+    def do_configure_grubefi(cls, creator, cr_workdir):
+        """
+        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)
+
+        if not custom_cfg:
+            # Create grub configuration using parameters from wks file
+            bootloader = creator.ks.bootloader
+
+            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 += "set root='hd0,gpt2'\n"
+            grubefi_conf += "menuentry 'boot'{\n"
+
+            kernel_image = get_bitbake_var("KERNEL_IMAGE")
+            kernel = "/boot/%s" % kernel_image
+
+            grubefi_conf += "linux %s root=/dev/sda2 rootwait %s\n" \
+                            % (kernel, bootloader.append)
+
+            initrd_image = get_bitbake_var("INITRD_IMAGE")
+            initrd = "/boot/%s" % initrd_image
+
+            grubefi_conf += "initrd %s\n" % initrd
+
+            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()
+
+        cfg = open("%s/hdd/boot/EFI/BOOT/grub-mkimage.cfg" % cr_workdir, "w")
+        mkimage_conf = "set root='hd0,gpt1'\n"
+        mkimage_conf += "set prefix=($root)/EFI/BOOT\n"
+        cfg.write(mkimage_conf)
+        cfg.close()
+
+    @classmethod
+    def do_configure_systemdboot(cls, hdddir, creator, cr_workdir, source_params):
+        """
+        Create loader-specific systemd-boot/gummiboot config
+        """
+        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 = ""
+        loader_conf += "default boot\n"
+        loader_conf += "timeout %d\n" % bootloader.timeout
+
+        initrd = source_params.get('initrd')
+
+        if initrd:
+            # obviously we need to have a common common deploy var
+            bootimg_dir = get_bitbake_var("DEPLOY_DIR_IMAGE")
+            if not bootimg_dir:
+                raise WicError("Couldn't find DEPLOY_DIR_IMAGE, exiting")
+
+            cp_cmd = "cp %s/%s %s" % (bootimg_dir, initrd, hdddir)
+            exec_cmd(cp_cmd, True)
+        else:
+            logger.debug("Ignoring missing initrd")
+
+        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)
+        cfg.close()
+
+        configfile = creator.ks.bootloader.configfile
+        custom_cfg = None
+        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)
+
+        if not custom_cfg:
+            # Create systemd-boot configuration using parameters from wks file
+            kernel_name = get_bitbake_var("KERNEL_IMAGE")
+            kernel = "/%s" % kernel_name
+
+            boot_conf = ""
+            boot_conf += "title boot\n"
+            boot_conf += "linux %s\n" % kernel
+            boot_conf += "options LABEL=Boot root=%s %s\n" % \
+                             (creator.rootdev, bootloader.append)
+
+            if initrd:
+                boot_conf += "initrd /%s\n" % initrd
+
+        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)
+        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(creator, cr_workdir)
+            elif source_params['loader'] == 'systemd-boot':
+                cls.do_configure_systemdboot(hdddir, creator, cr_workdir, source_params)
+            else:
+                raise WicError("unrecognized bootimg-efi-isar loader: %s" % source_params['loader'])
+        except KeyError:
+            raise WicError("bootimg-efi-isar requires a loader, none specified")
+
+
+    @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_name = get_bitbake_var("KERNEL_IMAGE")
+        install_cmd = "install -m 0644 %s/%s %s/%s" % \
+                      (staging_kernel_dir, kernel_name, hdddir, kernel_name)
+        exec_cmd(install_cmd)
+
+
+        try:
+            if source_params['loader'] == 'grub-efi':
+                shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub.cfg" % cr_workdir,
+                                "%s/grub.cfg" % cr_workdir)
+                shutil.copyfile("%s/hdd/boot/EFI/BOOT/grub-mkimage.cfg" % cr_workdir,
+                                "%s/grub-mkimage.cfg" % cr_workdir)
+                for mod in [x for x in os.listdir(kernel_dir) if x.startswith("grub-efi-")]:
+                    cp_cmd = "cp %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)
+
+                distro_arch = get_bitbake_var("DISTRO_ARCH")
+                if not distro_arch:
+                    raise WicError("Couldn't find target architecture")
+
+                if distro_arch == "amd64":
+                    grub_target = 'x86_64-efi'
+                    grub_image = "bootx64.efi"
+                elif distro_arch == "i386":
+                    grub_target = 'i386-efi'
+                    grub_image = "bootia32.efi"
+                else:
+                    raise WicError("grub-efi is incompatible with target %s" %
+                                   distro_arch)
+
+                bootimg_dir = "%s/hdd/boot" % cr_workdir
+                if not os.path.isfile("%s/EFI/BOOT/%s" \
+                                      % (bootimg_dir, grub_image)):
+
+                    # TODO: check that grub-mkimage is available
+                    grub_cmd = "grub-mkimage -p /EFI/BOOT "
+                    grub_cmd += "-c %s/grub-mkimage.cfg " % cr_workdir
+                    grub_cmd += "-O %s -o %s/EFI/BOOT/%s " \
+                                % (grub_target, bootimg_dir, grub_image)
+                    grub_cmd += "part_gpt part_msdos ntfs ntfscomp fat ext2 "
+                    grub_cmd += "normal chain boot configfile linux multiboot "
+                    grub_cmd += "search efi_gop efi_uga font gfxterm gfxmenu "
+                    grub_cmd += "terminal minicmd test iorw loadenv echo help "
+                    grub_cmd += "reboot serial terminfo iso9660 loopback tar "
+                    grub_cmd += "memdisk ls search_fs_uuid udf btrfs xfs lvm "
+                    grub_cmd += "reiserfs ata "
+                    exec_cmd(grub_cmd)
+            elif source_params['loader'] == 'systemd-boot':
+                for mod in [x for x in os.listdir(kernel_dir) if x.startswith("systemd-")]:
+                    cp_cmd = "cp %s/%s %s/EFI/BOOT/%s" % (kernel_dir, mod, hdddir, mod[8:])
+                    exec_cmd(cp_cmd, True)
+            else:
+                raise WicError("unrecognized bootimg-efi-isar loader: %s" %
+                               source_params['loader'])
+        except KeyError:
+            raise WicError("bootimg-efi-isar requires a loader, none specified")
+
+        startup = os.path.join(kernel_dir, "startup.nsh")
+        if os.path.exists(startup):
+            cp_cmd = "cp %s %s/" % (startup, hdddir)
+            exec_cmd(cp_cmd, True)
+
+        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.img" % cr_workdir
+
+        dosfs_cmd = "mkdosfs -n efi -C %s %d" % (bootimg, blocks)
+        exec_cmd(dosfs_cmd)
+
+        mcopy_cmd = "env MTOOLS_SKIP_CHECK=1 mcopy -i %s -s %s/* ::/" % \
+                    (bootimg, hdddir)
+        exec_cmd(mcopy_cmd, True)
+
+        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
-- 
2.13.6


  parent reply	other threads:[~2018-01-31  9:42 UTC|newest]

Thread overview: 51+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-31  9:41 [PATCH 0/9] first wic integration Henning Schild
2018-01-31  9:41 ` [PATCH 1/9] classes: image: introduce size measuring function, for before do_*_image Henning Schild
2018-01-31  9:41 ` [PATCH 2/9] images: new class wic-img for wic intregration Henning Schild
2018-02-13 14:44   ` Alexander Smirnov
2018-02-13 16:06     ` Henning Schild
2018-01-31  9:41 ` Henning Schild [this message]
2018-02-12 17:48   ` [PATCH 3/9] wic: add a bootimg-efi-isar plugin outside the wic tree Jan Kiszka
2018-01-31  9:41 ` [PATCH 4/9] Revert "wic: Make the bootimg-efi plugin generate usable images" Henning Schild
2018-01-31  9:41 ` [PATCH 5/9] Revert "wic: Introduce the `WicExecError` exception class" Henning Schild
2018-01-31  9:41 ` [PATCH 6/9] Revert "wic: Work around mcopy error" Henning Schild
2018-01-31  9:41 ` [PATCH 7/9] Revert "wic: Use sudo instead of pseudo" Henning Schild
2018-01-31  9:41 ` [PATCH 8/9] Revert "wic: Remove sysroot support" Henning Schild
2018-01-31  9:42 ` [PATCH 9/9] wic: now truly go for the wic version we claim to have Henning Schild
2018-01-31 10:11   ` Alexander Smirnov
2018-01-31 10:55     ` Jan Kiszka
2018-01-31 11:11       ` Alexander Smirnov
2018-01-31 11:43         ` Jan Kiszka
2018-01-31 11:53           ` Baurzhan Ismagulov
2018-01-31 12:01             ` Jan Kiszka
2018-01-31 12:28               ` Baurzhan Ismagulov
2018-01-31 13:53                 ` Henning Schild
2018-01-31 14:01                   ` Baurzhan Ismagulov
2018-01-31 14:21                     ` Henning Schild
2018-01-31 10:02 ` [PATCH 0/9] first wic integration Alexander Smirnov
2018-01-31 10:12   ` Henning Schild
2018-01-31 11:24     ` Baurzhan Ismagulov
2018-01-31 11:47       ` Jan Kiszka
2018-01-31 12:02         ` Baurzhan Ismagulov
2018-01-31 12:15           ` Jan Kiszka
2018-01-31 13:30             ` Jan Kiszka
2018-01-31 13:41               ` Baurzhan Ismagulov
2018-01-31 14:01                 ` Jan Kiszka
2018-01-31 15:21                   ` Baurzhan Ismagulov
2018-01-31 15:46                     ` Henning Schild
2018-01-31 16:13                     ` Jan Kiszka
2018-01-31 13:35             ` Baurzhan Ismagulov
2018-01-31 13:47               ` Henning Schild
2018-01-31 14:00                 ` Baurzhan Ismagulov
2018-01-31 13:46             ` Henning Schild
2018-01-31 13:36           ` Henning Schild
2018-01-31 13:40             ` Baurzhan Ismagulov
2018-01-31 13:05       ` Henning Schild
2018-02-01 12:41 ` [PATCH] images: wic: limit use of sudo and enable manual call again Henning Schild
2018-02-01 12:44   ` Henning Schild
2018-02-01 16:09     ` Baurzhan Ismagulov
2018-02-01 18:10       ` Henning Schild
2018-02-01 18:55         ` Henning Schild
2018-02-12 19:07   ` Henning Schild
2018-02-12 17:27 ` [PATCH 0/9] first wic integration Henning Schild
2018-02-12 18:21   ` Alexander Smirnov
2018-02-12 18:30     ` Henning Schild

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5813b0e31a65669b6e27294b70050e3e70626b57.1517390790.git.henning.schild@siemens.com \
    --to=henning.schild@siemens.com \
    --cc=isar-users@googlegroups.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox