diff mbox series

[v6,2/2] image-bootfiles.bbclass: new class, copy boot files to root filesystem

Message ID 20240710085310.147425-3-marcus.folkesson@gmail.com
State New
Headers show
Series image-bootfiles: new class | expand

Commit Message

Marcus Folkesson July 10, 2024, 8:53 a.m. UTC
image-bootfiles class copy files listed in IMAGE_BOOT_FILES
to the IMAGE_BOOT_FILES_DIR directory of the root filesystem.

This is useful when there is no explicit boot partition but all boot
files should instead reside inside the root filesystem.

Signed-off-by: Marcus Folkesson <marcus.folkesson@gmail.com>
Reviewed-by: Quentin Schulz <quentin.schulz@cherry.de>
Reviewed-by: Konrad Weihmann <kweihmann@outlook.com>
---

Notes:
    v3:
            - Skip the intermediate bootfiles() function
            - Rename variable names to be consistent
            - Various python optimizations
    v4:
            - Correct usage description
            - Create image_dst before copy
    v5:
            - Check if install_files is [] rather than None
            - Print warning if overwriting files
            - run as ROOTFS_POSTPROCESS_COMMAND instead
    v6:
            - Collect tags to make it easier to apply

 meta/classes/image-bootfiles.bbclass | 41 ++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 meta/classes/image-bootfiles.bbclass
diff mbox series

Patch

diff --git a/meta/classes/image-bootfiles.bbclass b/meta/classes/image-bootfiles.bbclass
new file mode 100644
index 0000000000..a17c92f66b
--- /dev/null
+++ b/meta/classes/image-bootfiles.bbclass
@@ -0,0 +1,41 @@ 
+#
+# SPDX-License-Identifier: MIT
+#
+# Copyright (C) 2024 Marcus Folkesson
+# Author: Marcus Folkesson <marcus.folkesson@gmail.com>
+#
+# Writes IMAGE_BOOT_FILES to the IMAGE_BOOT_FILES_DIR directory.
+#
+# Usage: add "inherit image-bootfiles" to your image.
+#
+
+IMAGE_BOOT_FILES_DIR ?= "boot"
+
+python bootfiles_populate() {
+    import shutil
+    from oe.bootfiles import get_boot_files
+
+    deploy_image_dir = d.getVar("DEPLOY_DIR_IMAGE")
+    boot_dir = os.path.join(d.getVar("IMAGE_ROOTFS"), d.getVar("IMAGE_BOOT_FILES_DIR"))
+
+    boot_files = d.getVar("IMAGE_BOOT_FILES")
+    if boot_files is None:
+        return
+
+    install_files = get_boot_files(deploy_image_dir, boot_files)
+    if not install_files:
+        bb.warn("Could not find any boot files to install even though IMAGE_BOOT_FILES is not empty")
+        return
+
+    os.makedirs(boot_dir, exist_ok=True)
+    for src, dst  in install_files:
+        image_src = os.path.join(deploy_image_dir, src)
+        image_dst = os.path.join(boot_dir, dst)
+        if os.path.exists(image_dst):
+            bb.warn("%s does already exist and will be overwritten" % image_dst)
+
+        os.makedirs(os.path.dirname(image_dst), exist_ok=True)
+        shutil.copyfile(image_src, image_dst)
+}
+
+ROOTFS_POSTPROCESS_COMMAND += "bootfiles_populate;"