[RFC PATCH] classes: Add initramfs class

138 views
Skip to first unread message

Harald Seiler

unread,
Sep 21, 2020, 6:43:02 AM9/21/20
to isar-...@googlegroups.com, Harald Seiler
Add a new "image" class for generating a custom initramfs. It works
like this: A new minimal debian rootfs is bootstrapped and all
dependency packages for the new initramfs are installed. Then, an
initramfs is generated from this rootfs and deployed like usual.

This new initramfs.bbclass "image" class should be pulled in by an
"initramfs image" recipe. Said recipe then specifies all dependencies
of the initramfs via INITRAMFS_INSTALL and INITRAMFS_PREINSTALL (which
are analogous to the respective IMAGE_* variables).

initramfs.bbclass intentionally does _not_ expose a mechanism to change
/etc/initramfs-tools/initramfs.conf and /etc/initramfs-tools/modules.
Changes to their settings are better done via packages that deploy
conf-hooks to /usr/share/initramfs-tools/conf-hooks.d/ and module
fragment files to /usr/share/initramfs-tools/modules.d/.

Signed-off-by: Harald Seiler <h...@denx.de>
---

Notes:
I had this idea while searching for a way to build an initramfs that
uses dm-verity to assert integrity of the rootfs. To me, this feels
like a much cleaner solution than anything else I tried and I'm happy to
report that, using this approach, I got everything working nicely in the
original project.

In my opinion, this design has a number of advantages over the previous
solutions we have seen so far:

- It does not suffer any kind of initramfs pollution, caused by
packages installed into a rootfs. This is a big problem when trying
to generated an initramfs from e.g. `buildchroot-target` as many
unrelated packaged could be installed there which would all get
pulled into the initrd (if they install hooks/scripts).

This also means, with this new approach, the integrator has maximum
control over the contents of the initramfs.

- There are no needs to change the initramfs generation process in any
way, the debian tooling can be used exactly like its meant to.

- As most isar-generated images will never regenerate the initramfs
from the running system, all initramfs related packages are dead-weight
to the image. This is a problem when trying to generate the initramfs
from the actual image rootfs.

When it is necessary to rebuild the initramfs in a running system,
the packages designed for this new class could just be installed into
the rootfs, without any changes necessary. This means, any generic
initramfs module packages can be used both with the in-rootfs mechanism
and initramfs.bbclass.

- Because of this complete isolation and independence, implementation
of complex logic is much easier: For example dm-verity needs
a root-hash that is only available after the rootfs has been cast into
a filesystem image. With this new approach, this can be modelled with
a simple task dependency.

meta/classes/initramfs.bbclass | 41 ++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 meta/classes/initramfs.bbclass

diff --git a/meta/classes/initramfs.bbclass b/meta/classes/initramfs.bbclass
new file mode 100644
index 000000000000..8af9b4b379a5
--- /dev/null
+++ b/meta/classes/initramfs.bbclass
@@ -0,0 +1,41 @@
+# This software is a part of ISAR.
+
+# Make workdir and stamps machine-specific without changing common PN target
+WORKDIR = "${TMPDIR}/work/${DISTRO}-${DISTRO_ARCH}/${PN}-${MACHINE}/${PV}-${PR}"
+STAMP = "${STAMPS_DIR}/${DISTRO}-${DISTRO_ARCH}/${PN}-${MACHINE}/${PV}-${PR}"
+STAMPCLEAN = "${STAMPS_DIR}/${DISTRO}-${DISTRO_ARCH}/${PN}-${MACHINE}/*-*"
+
+INITRAMFS_INSTALL ?= ""
+INITRAMFS_PREINSTALL ?= ""
+INITRAMFS_ROOTFS ?= "${WORKDIR}/rootfs"
+INITRAMFS_IMAGE_FILE = "${DEPLOY_DIR_IMAGE}/${INITRAMFS_FULLNAME}.initrd.img"
+
+# Install proper kernel
+INITRAMFS_INSTALL += "${@ ("linux-image-" + d.getVar("KERNEL_NAME", True)) if d.getVar("KERNEL_NAME", True) else ""}"
+
+# Name of the initramfs including distro&machine names
+INITRAMFS_FULLNAME = "${PN}-${DISTRO}-${MACHINE}"
+
+DEPENDS += "${INITRAMFS_INSTALL}"
+
+ROOTFSDIR = "${INITRAMFS_ROOTFS}"
+ROOTFS_FEATURES = ""
+ROOTFS_PACKAGES = "initramfs-tools ${INITRAMFS_PREINSTALL} ${INITRAMFS_INSTALL}"
+
+inherit rootfs
+
+do_generate_initramfs() {
+ rootfs_do_mounts
+ rootfs_do_qemu
+
+ sudo -E chroot "${INITRAMFS_ROOTFS}" \
+ update-initramfs -u -v
+
+ if [ ! -e "${INITRAMFS_ROOTFS}/initrd.img" ]; then
+ die "No initramfs was found after generation!"
+ fi
+
+ rm -rf "${INITRAMFS_IMAGE_FILE}"
+ cp "${INITRAMFS_ROOTFS}/initrd.img" "${INITRAMFS_IMAGE_FILE}"
+}
+addtask generate_initramfs after do_rootfs before do_build
--
2.26.2

Jan Kiszka

unread,
Sep 21, 2020, 3:25:50 PM9/21/20
to Harald Seiler, isar-...@googlegroups.com, Quirin Gylstorff
I think the public would also benefit from a use / test case, i.e. some
recipe that inherits this and generates an own initramfs with
extensions. For merging this, that will be mandatory anyway.

Quirin, what is your impression of this approach?

Jan

--
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux

Harald Seiler

unread,
Sep 22, 2020, 6:34:31 AM9/22/20
to Jan Kiszka, isar-...@googlegroups.com, Quirin Gylstorff
Makes sense. What do you think would be a good and upstream-viable
example for this? I can't think of anything trivial _and_ generic that
would still be a sensible use-case - the whole reason for this bbclass is
cases where the a custom initramfs is needed, after all ...

One idea would be to get the remaining dm-verity recipes upstream as well.
This would definitely be quite a bit of work as the ones I have right now
are not entirely generic yet ... Though you mentioned that you're
interested in making them available at some point anyway, so maybe this is
the way to go?

Otherwise (or in addition), I could add a dummy recipe that e.g. just
prints something during boot to meta-isar, to show how it works ...

--
Harald

DENX Software Engineering GmbH, Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: +49-8142-66989-62 Fax: +49-8142-66989-80 Email: h...@denx.de

Jan Kiszka

unread,
Sep 22, 2020, 7:04:53 AM9/22/20
to Harald Seiler, isar-...@googlegroups.com, Quirin Gylstorff
That would have been exactly my suggestion as well. Can still be
replaced later on when we upstream things like dm-verity generation.

Gylstorff Quirin

unread,
Sep 22, 2020, 8:10:21 AM9/22/20
to Jan Kiszka, Harald Seiler, isar-...@googlegroups.com
From the first look of it, looks good to me.

Did you test what happens if you try to install a custom kernel module?

--
Quirin

Harald Seiler

unread,
Sep 22, 2020, 8:18:58 AM9/22/20
to Gylstorff Quirin, Jan Kiszka, isar-...@googlegroups.com
Hi,
You'll need to add the package for the custom module to INITRAMFS_INSTALL
or INITRAMFS_PREINSTALL as well of course. If you do that, I don't see
a reason why it shouldn't work (that would be a bug) but I did not test
this so far.

Harald Seiler

unread,
Sep 23, 2020, 12:21:53 PM9/23/20
to isar-...@googlegroups.com, Harald Seiler
Add the initramfs-example recipe/package which demonstrates how to write
initramfs modules. It demonstrates how to add hook scripts, boot scripts,
and conf-hooks.

Signed-off-by: Harald Seiler <h...@denx.de>
---
.../initramfs-example/files/example.conf-hook | 7 ++++
.../initramfs-example/files/example.hook | 19 +++++++++
.../initramfs-example/files/example.script | 21 ++++++++++
.../initramfs-example/initramfs-example.bb | 40 +++++++++++++++++++
4 files changed, 87 insertions(+)
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.hook
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.script
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/initramfs-example.bb

diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook b/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
new file mode 100644
index 000000000000..2a3cf7a84040
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
@@ -0,0 +1,7 @@
+# Example conf-hook.
+#
+# See "CONFIGURATION HOOK SCRIPTS" in initramfs-tools(7) for details.
+
+# Example: Use busybox instead of klibc-utils. The package must also add
+# `busybox` as a dependency when this is set.
+BUSYBOX=y
diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.hook b/meta-isar/recipes-initramfs/initramfs-example/files/example.hook
new file mode 100644
index 000000000000..0d84e7a97efd
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/files/example.hook
@@ -0,0 +1,19 @@
+#!/bin/sh
+# Example hook script.
+#
+# See "HOOK SCRIPTS" in initramfs-tools(7) for details.
+
+PREREQ=""
+prereqs()
+{
+ echo "$PREREQ"
+}
+case $1 in
+prereqs)
+ prereqs
+ exit 0
+ ;;
+esac
+
+. /usr/share/initramfs-tools/hook-functions
+# Begin real processing below this line
diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.script b/meta-isar/recipes-initramfs/initramfs-example/files/example.script
new file mode 100644
index 000000000000..784fad9c99bb
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/files/example.script
@@ -0,0 +1,21 @@
+#!/bin/sh
+# Example boot script.
+#
+# See "BOOT SCRIPTS" in initramfs-tools(7) for details.
+
+PREREQ=""
+prereqs()
+{
+ echo "$PREREQ"
+}
+case $1 in
+prereqs)
+ prereqs
+ exit 0
+ ;;
+esac
+
+. /scripts/functions
+# Begin real processing below this line
+
+log_success_msg "Hello from ISAR!"
diff --git a/meta-isar/recipes-initramfs/initramfs-example/initramfs-example.bb b/meta-isar/recipes-initramfs/initramfs-example/initramfs-example.bb
new file mode 100644
index 000000000000..c336dda92b5d
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/initramfs-example.bb
@@ -0,0 +1,40 @@
+# Example of a recipe containing an initramfs module. Packages like this can be
+# used with initramfs.bbclass or installed directly into a rootfs, depending on
+# the usecase.
+#
+# This software is a part of ISAR.
+
+DESCRIPTION = "Sample initramfs module for ISAR"
+MAINTAINER = "Your name here <y...@domain.com>"
+DEBIAN_DEPENDS = "initramfs-tools"
+
+# If the conf-hook enables BUSYBOX=y, busybox is needed:
+DEBIAN_DEPENDS .= ", busybox"
+
+SRC_URI = " \
+ file://example.conf-hook \
+ file://example.hook \
+ file://example.script \
+ "
+
+inherit dpkg-raw
+
+do_install[cleandirs] += " \
+ ${D}/usr/share/initramfs-tools/conf-hooks.d \
+ ${D}/usr/share/initramfs-tools/hooks \
+ ${D}/usr/share/initramfs-tools/scripts/local-top \
+ "
+do_install() {
+ # See "CONFIGURATION HOOK SCRIPTS" in initramfs-tools(7) for details.
+ install "${WORKDIR}/example.conf-hook" \
+ "${D}/usr/share/initramfs-tools/conf-hooks.d/isar-example"
+
+ # See "HOOK SCRIPTS" in initramfs-tools(7) for details.
+ install "${WORKDIR}/example.hook" \
+ "${D}/usr/share/initramfs-tools/hooks/isar-example"
+
+ # Note that there are other places where a boot script might be deployed to,
+ # apart from local-top. See "BOOT SCRIPTS" in initramfs-tools(7) for details.
+ install "${WORKDIR}/example.script" \
+ "${D}/usr/share/initramfs-tools/scripts/local-top/example.script"
+}
--
2.26.2

Harald Seiler

unread,
Sep 23, 2020, 12:21:54 PM9/23/20
to isar-...@googlegroups.com, Harald Seiler
isar-initramfs is a custom initramfs which additionally has the
initramfs-example module installed.

Signed-off-by: Harald Seiler <h...@denx.de>
---

Notes:
Maybe this initramfs should be tested in CI somewhere? I'm unsure what
makes sense, and how to "force" this custom initramfs into a CI target.

.../recipes-initramfs/images/isar-initramfs.bb | 18 ++++++++++++++++++
1 file changed, 18 insertions(+)
create mode 100644 meta-isar/recipes-initramfs/images/isar-initramfs.bb

diff --git a/meta-isar/recipes-initramfs/images/isar-initramfs.bb b/meta-isar/recipes-initramfs/images/isar-initramfs.bb
new file mode 100644
index 000000000000..aaa0350aab20
--- /dev/null
+++ b/meta-isar/recipes-initramfs/images/isar-initramfs.bb
@@ -0,0 +1,18 @@
+# Example of a custom initramfs image recipe. The image will be deployed to
+#
+# build/tmp/deploy/images/${MACHINE}/isar-initramfs-${DISTRO}-${MACHINE}.initrd.img
+#
+# This software is a part of ISAR.
+
+inherit initramfs
+
+# Debian packages that should be installed into the system for building the
+# initramfs. E.g. the cryptsetup package which contains initramfs scripts for
+# decrypting a root filesystem.
+INITRAMFS_PREINSTALL += " \
+ "
+
+# Recipes that should be installed into the initramfs build rootfs.
+INITRAMFS_INSTALL += " \
+ initramfs-example \
+ "
--
2.26.2

Harald Seiler

unread,
Sep 23, 2020, 12:22:00 PM9/23/20
to isar-...@googlegroups.com, Harald Seiler
Changes in v2:
- None (just added examples in new patches)

Jan Kiszka

unread,
Sep 25, 2020, 5:13:10 AM9/25/20
to Harald Seiler, isar-...@googlegroups.com
If this recipe is not pulled somewhere, it's dead. Some test-only
dependencies we had to local.conf in scripts/ci_build.sh. Or you add it
to meta-isar/conf/local.conf.sample.

If added, will it simply replace the default initramfs? Or is more needed?

Harald Seiler

unread,
Sep 25, 2020, 7:28:19 AM9/25/20
to Jan Kiszka, isar-...@googlegroups.com
Right, that's why I was asking where to put it.

> Some test-only dependencies we had to local.conf in scripts/ci_build.sh.
> Or you add it to meta-isar/conf/local.conf.sample.
>
> If added, will it simply replace the default initramfs? Or is more needed?

Well, there really isn't such a thing as the default initramfs right now.
`image.bbclass` deploys its own initramfs (the one debian installed to
/boot) to `${IMAGE_FULLNAME}-initrd.img` which further tasks could then
reference when building e.g. a fitImage or it could be added to
IMAGE_BOOT_FILES for WIC.

Similarly, the new `initramfs.bbclass` deploys the final artifact to
`${INITRAMFS_FULLNAME}.initrd.img`. What happens after that is entirely
up to the integrator. After all, this image-class is meant for cases
where customization beyond the abilities of the current in-rootfs
initramfs are needed. As an example, a fitImage recipe could use this
like

INITRAMFS_RECIPE = "isar-initramfs"

INITRD_IMG = "${PP_DEPLOY}/${INITRAMFS_RECIPE}-${DISTRO}-${MACHINE}.initrd.img"
do_fit_image[depends] += "${INITRAMFS_RECIPE}:do_build"

---

Now, for CI if we want to include this custom initramfs, we'd need to
either modify some image recipe to pull it in as shown above or build it
separately and then start qemu with

-initrd build/tmp/deploy/images/qemuamd64/isar-initramfs-debian-buster-qemuamd64.initrd.img

somewhere. But I'm not sure if that's easily integrated into the current
setup?

IMO the next best thing would be to just build test it. Would adding

mc:qemuamd64-buster:isar-initramfs

to TARGETS_SET in ci_build.sh work?

Henning Schild

unread,
Sep 25, 2020, 9:16:46 AM9/25/20
to Harald Seiler, isar-...@googlegroups.com
To me this sounds like a "buildchroot" specifically for the initramfs.
And the target would be to keep the stuff we need only in the initramfs
out of the "final" rootfs.

More inline.
I was about to ask why not just use that chroot ... got it.

> This also means, with this new approach, the integrator has
> maximum control over the contents of the initramfs.

And maximum responsibilty. Using an initrd that was not generated from
the "final" rootfs probably violates some assumptions or can at least
be seen as highly unusual. Not sure that is or could become a problem.

> - There are no needs to change the initramfs generation process
> in any way, the debian tooling can be used exactly like its meant to.
>
> - As most isar-generated images will never regenerate the
> initramfs from the running system, all initramfs related packages are
> dead-weight to the image. This is a problem when trying to generate
> the initramfs from the actual image rootfs.

Yes dead-weight, but is it heavy? In terms of disk-space we are
probably talking about really not much, if you compare to what
applications will pull in. But i do not know. I guess lvm, mdadm,
cryptsetup and friends could really add up ... but wic does not have
that anyways ;)

> When it is necessary to rebuild the initramfs in a running
> system, the packages designed for this new class could just be
> installed into the rootfs, without any changes necessary. This
> means, any generic initramfs module packages can be used both with
> the in-rootfs mechanism and initramfs.bbclass.
>
> - Because of this complete isolation and independence,
> implementation of complex logic is much easier: For example
> dm-verity needs a root-hash that is only available after the rootfs
> has been cast into a filesystem image. With this new approach, this
> can be modelled with a simple task dependency.

I guess one can pass that hash to the initrd with an argument from the
kernel command line. So you probably pass it into your wks, or whatever
imager controls the cmdline.

How much does the generation of the initrd impose on the "real" rootfs,
in terms of "dead" packages? And how polluted can an initrd become when
generated from a rootfs that "contains too much"? Maybe you have
numbers for your layer giving a perspective on the gain.

Generating the initramfs not from the rootfs means that the manifest
will not mention all packages shipped in the image, which could cause
legal issues when overlooked. Adding the manifests might result in
too much clearing effort, because not all that is in the
initramfs-buildchroot will be in the initramfs.

Henning

Harald Seiler

unread,
Oct 6, 2020, 8:24:17 AM10/6/20
to Henning Schild, isar-...@googlegroups.com
Hi,

On Fri, 2020-09-25 at 15:16 +0200, Henning Schild wrote:
> To me this sounds like a "buildchroot" specifically for the initramfs.

That's pretty much what it is. With the difference to the other
buildchroots that it is not shared when building multiple initramfs images
so each one has its own rootfs.

> And the target would be to keep the stuff we need only in the initramfs
> out of the "final" rootfs.

This is a nice side-effect but not the main reason I propose this. Just
to make it clear: I do _not_ want this to be a replacement for the current
way of building the initramfs from the image rootfs. This new bbclass is
meant for situations where in-image generation falls short or is otherwise
inappropriate. A few examples:

- As already mentioned, it makes dm-verity setups _much_ easier and a lot
cleaner because it avoids the dependency cycle that other approaches
have.

- This could be used to generate a completely custom initramfs-based
rescue system.
The vanilla initramfs from debian does not suffer any problems, as long as
you make sure the correct kernel modules are included. This should
usually just work(tm) because initramfs.bbclass does the exact same thing
as image.bbclass:

> > +# Install proper kernel
> > +INITRAMFS_INSTALL += "${@ ("linux-image-" + d.getVar("KERNEL_NAME",

With upstream initramfs modules, care has to be taken, of course. Let's
look at cryptroot from cryptsetup as an example: This module needs
a correct /etc/crypttab in the initramfs-chroot to properly unlock the
root filesystem. For any other partitions, crypttab needs to be in the
actual image rootfs.

You could now either have two separate crypttabs, one with just the rootfs
in initramfs-chroot and one for the rest in the actual image. Or, what
I'd find the cleaner solution: A config-package containing /etc/crypttab
that is deployed to both the image and the initramfs-chroot.

That said, I do not see why one would want the custom initramfs for this
scenario ... The in-image version will work just fine.

> > - There are no needs to change the initramfs generation process
> > in any way, the debian tooling can be used exactly like its meant to.
> >
> > - As most isar-generated images will never regenerate the
> > initramfs from the running system, all initramfs related packages are
> > dead-weight to the image. This is a problem when trying to generate
> > the initramfs from the actual image rootfs.
>
> Yes dead-weight, but is it heavy? In terms of disk-space we are
> probably talking about really not much, if you compare to what
> applications will pull in. But i do not know. I guess lvm, mdadm,
> cryptsetup and friends could really add up ... but wic does not have
> that anyways ;)

I don't think it would amount to much. If you're really short on space,
it could matter (as you said, one could pull any dm tooling out of the
rootfs), but apart from that I don't think it is that relevant.

> > When it is necessary to rebuild the initramfs in a running
> > system, the packages designed for this new class could just be
> > installed into the rootfs, without any changes necessary. This
> > means, any generic initramfs module packages can be used both with
> > the in-rootfs mechanism and initramfs.bbclass.
> >
> > - Because of this complete isolation and independence,
> > implementation of complex logic is much easier: For example
> > dm-verity needs a root-hash that is only available after the rootfs
> > has been cast into a filesystem image. With this new approach, this
> > can be modelled with a simple task dependency.
>
> I guess one can pass that hash to the initrd with an argument from the
> kernel command line. So you probably pass it into your wks, or whatever
> imager controls the cmdline.

I don't see this working without creating a dependency cycle somewhere ...
I did try to explore alternative solutions but it all ended up requiring
ugly hacks at some point or another.

> How much does the generation of the initrd impose on the "real" rootfs,
> in terms of "dead" packages? And how polluted can an initrd become when
> generated from a rootfs that "contains too much"? Maybe you have
> numbers for your layer giving a perspective on the gain.

The main motivation for me was a clean dm-verity setup so I do not have
numbers on this aspect. I don't expect it to be much though.

> Generating the initramfs not from the rootfs means that the manifest
> will not mention all packages shipped in the image, which could cause
> legal issues when overlooked. Adding the manifests might result in
> too much clearing effort, because not all that is in the
> initramfs-buildchroot will be in the initramfs.

Well you won't install anything into the initramfs-chroot that won't in
some form or another leave a trace in the initramfs so I'd say merging the
manifests does make sense here.

--
Harald
--

Jan Kiszka

unread,
Oct 13, 2020, 11:11:23 AM10/13/20
to Harald Seiler, isar-...@googlegroups.com
We already have fit/ubi example. Could that be expanded better to stress
this? Otherwise, I'm find with your suggestion above.

Jan

--
Siemens AG, T RDA IOT

Harald Seiler

unread,
Jan 14, 2021, 5:12:19 AM1/14/21
to isar-...@googlegroups.com, Harald Seiler, Jan Kiszka
Add a new "image" class for generating a custom initramfs. It works
like this: A new minimal debian rootfs is bootstrapped and all
dependency packages for the new initramfs are installed. Then, an
initramfs is generated from this rootfs and deployed like usual.

This new initramfs.bbclass "image" class should be pulled in by an
"initramfs image" recipe. Said recipe then specifies all dependencies
of the initramfs via INITRAMFS_INSTALL and INITRAMFS_PREINSTALL (which
are analogous to the respective IMAGE_* variables).

initramfs.bbclass intentionally does _not_ expose a mechanism to change
/etc/initramfs-tools/initramfs.conf and /etc/initramfs-tools/modules.
Changes to their settings are better done via packages that deploy
conf-hooks to /usr/share/initramfs-tools/conf-hooks.d/ and module
fragment files to /usr/share/initramfs-tools/modules.d/.

Signed-off-by: Harald Seiler <h...@denx.de>
---

Notes:
I had this idea while searching for a way to build an initramfs that
uses dm-verity to assert integrity of the rootfs. To me, this feels
like a much cleaner solution than anything else I tried and I'm happy to
report that, using this approach, I got everything working nicely in the
original project.

In my opinion, this design has a number of advantages over the previous
solutions we have seen so far:

- It does not suffer any kind of initramfs pollution, caused by
packages installed into a rootfs. This is a big problem when trying
to generated an initramfs from e.g. `buildchroot-target` as many
unrelated packaged could be installed there which would all get
pulled into the initrd (if they install hooks/scripts).

This also means, with this new approach, the integrator has maximum
control over the contents of the initramfs.

- There are no needs to change the initramfs generation process in any
way, the debian tooling can be used exactly like its meant to.

- As most isar-generated images will never regenerate the initramfs
from the running system, all initramfs related packages are dead-weight
to the image. This is a problem when trying to generate the initramfs
from the actual image rootfs.

When it is necessary to rebuild the initramfs in a running system,
the packages designed for this new class could just be installed into
the rootfs, without any changes necessary. This means, any generic
initramfs module packages can be used both with the in-rootfs mechanism
and initramfs.bbclass.

- Because of this complete isolation and independence, implementation
of complex logic is much easier: For example dm-verity needs
a root-hash that is only available after the rootfs has been cast into
a filesystem image. With this new approach, this can be modelled with
a simple task dependency.

Changes in v2:
- None (just added examples in new patches)

Changes in v3:
- None

meta/classes/initramfs.bbclass | 41 ++++++++++++++++++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 meta/classes/initramfs.bbclass

diff --git a/meta/classes/initramfs.bbclass b/meta/classes/initramfs.bbclass
new file mode 100644
index 000000000000..8af9b4b379a5
--- /dev/null
+++ b/meta/classes/initramfs.bbclass
@@ -0,0 +1,41 @@
+# This software is a part of ISAR.
+
2.29.2

Harald Seiler

unread,
Jan 14, 2021, 5:12:20 AM1/14/21
to isar-...@googlegroups.com, Harald Seiler, Jan Kiszka
Add the initramfs-example recipe/package which demonstrates how to write
initramfs modules. It demonstrates how to add hook scripts, boot scripts,
and conf-hooks.

Signed-off-by: Harald Seiler <h...@denx.de>
---

Notes:
Changes in v3:
- None

.../initramfs-example/files/example.conf-hook | 7 ++++
.../initramfs-example/files/example.hook | 19 +++++++++
.../initramfs-example/files/example.script | 21 ++++++++++
.../initramfs-example/initramfs-example.bb | 40 +++++++++++++++++++
4 files changed, 87 insertions(+)
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.hook
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.script
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/initramfs-example.bb

diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook b/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
new file mode 100644
index 000000000000..2a3cf7a84040
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
@@ -0,0 +1,7 @@
+# Example conf-hook.
+#
+# See "CONFIGURATION HOOK SCRIPTS" in initramfs-tools(7) for details.
+
+# Example: Use busybox instead of klibc-utils. The package must also add
+# `busybox` as a dependency when this is set.
+BUSYBOX=y
diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.hook b/meta-isar/recipes-initramfs/initramfs-example/files/example.hook
new file mode 100644
index 000000000000..0d84e7a97efd
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/files/example.hook
@@ -0,0 +1,19 @@
+#!/bin/sh
+# Example hook script.
+#
+# See "HOOK SCRIPTS" in initramfs-tools(7) for details.
+
+PREREQ=""
+prereqs()
+{
+ echo "$PREREQ"
+}
+case $1 in
+prereqs)
+ prereqs
+ exit 0
+ ;;
+esac
+
+. /usr/share/initramfs-tools/hook-functions
+# Begin real processing below this line
diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.script b/meta-isar/recipes-initramfs/initramfs-example/files/example.script
new file mode 100644
new file mode 100644
index 000000000000..c336dda92b5d
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/initramfs-example.bb
@@ -0,0 +1,40 @@
+# Example of a recipe containing an initramfs module. Packages like this can be
+# used with initramfs.bbclass or installed directly into a rootfs, depending on
+# the usecase.
+#
+# This software is a part of ISAR.
+
2.29.2

Harald Seiler

unread,
Jan 14, 2021, 5:12:21 AM1/14/21
to isar-...@googlegroups.com, Harald Seiler, Jan Kiszka
isar-initramfs is a custom initramfs which additionally has the
initramfs-example module installed. It is also built as part of
the CI.

Signed-off-by: Harald Seiler <h...@denx.de>
---

Notes:
Changes in v3:
- Add this target to ci_build.sh for CI inclusion.

.../recipes-initramfs/images/isar-initramfs.bb | 18 ++++++++++++++++++
scripts/ci_build.sh | 1 +
2 files changed, 19 insertions(+)
create mode 100644 meta-isar/recipes-initramfs/images/isar-initramfs.bb

diff --git a/meta-isar/recipes-initramfs/images/isar-initramfs.bb b/meta-isar/recipes-initramfs/images/isar-initramfs.bb
new file mode 100644
index 000000000000..aaa0350aab20
--- /dev/null
+++ b/meta-isar/recipes-initramfs/images/isar-initramfs.bb
@@ -0,0 +1,18 @@
+# Example of a custom initramfs image recipe. The image will be deployed to
+#
+# build/tmp/deploy/images/${MACHINE}/isar-initramfs-${DISTRO}-${MACHINE}.initrd.img
+#
+# This software is a part of ISAR.
+
+inherit initramfs
+
+# Debian packages that should be installed into the system for building the
+# initramfs. E.g. the cryptsetup package which contains initramfs scripts for
+# decrypting a root filesystem.
+INITRAMFS_PREINSTALL += " \
+ "
+
+# Recipes that should be installed into the initramfs build rootfs.
+INITRAMFS_INSTALL += " \
+ initramfs-example \
+ "
diff --git a/scripts/ci_build.sh b/scripts/ci_build.sh
index f4c33a37247e..f6fc5e54f7d3 100755
--- a/scripts/ci_build.sh
+++ b/scripts/ci_build.sh
@@ -28,6 +28,7 @@ TARGETS_SET="\
mc:qemuamd64-stretch:isar-image-base \
mc:qemuamd64-buster:isar-image-base \
mc:qemuamd64-buster-tgz:isar-image-base \
+ mc:qemuamd64-buster:isar-initramfs \
mc:qemumipsel-stretch:isar-image-base \
mc:qemumipsel-buster:isar-image-base \
mc:nand-ubi-demo-buster:isar-image-ubi \
--
2.29.2

florian...@siemens.com

unread,
Jan 18, 2021, 4:11:34 AM1/18/21
to isar-...@googlegroups.com
${INITRAMFS_IMAGE_FILE} references ${DEPLOY_DIR_IMAGE}, which may not
exist yet. So I guess we should add something like

do_generate_initramfs[dirs] = "${DEPLOY_DIR_IMAGE}"

otherwise you have fix the dependency with some kind of "intermediate
task" in the recipe using this class.

Harald Seiler

unread,
Jan 18, 2021, 5:00:42 AM1/18/21
to florian...@siemens.com, isar-...@googlegroups.com
Hi Florian,

On Mon, 2021-01-18 at 09:11 +0000, florian...@siemens.com wrote:
> On Thu, 2021-01-14 at 11:11 +0100, Harald Seiler wrote:
> > Add a new "image" class for generating a custom initramfs. It works
> > like this: A new minimal debian rootfs is bootstrapped and all
> > dependency packages for the new initramfs are installed. Then, an
> > initramfs is generated from this rootfs and deployed like usual.
> >
> > This new initramfs.bbclass "image" class should be pulled in by an
> > "initramfs image" recipe. Said recipe then specifies all dependencies
> > of the initramfs via INITRAMFS_INSTALL and INITRAMFS_PREINSTALL (which
> > are analogous to the respective IMAGE_* variables).
> >
> > initramfs.bbclass intentionally does _not_ expose a mechanism to change
> > /etc/initramfs-tools/initramfs.conf and /etc/initramfs-tools/modules.
> > Changes to their settings are better done via packages that deploy
> > conf-hooks to /usr/share/initramfs-tools/conf-hooks.d/ and module
> > fragment files to /usr/share/initramfs-tools/modules.d/.
> >
> > Signed-off-by: Harald Seiler <h...@denx.de>
> > ---
> >
[...]
> > +
> > +do_generate_initramfs() {
> > + rootfs_do_mounts
> > + rootfs_do_qemu
> > +
> > + sudo -E chroot "${INITRAMFS_ROOTFS}" \
> > + update-initramfs -u -v
> > +
> > + if [ ! -e "${INITRAMFS_ROOTFS}/initrd.img" ]; then
> > + die "No initramfs was found after generation!"
> > + fi
> > +
> > + rm -rf "${INITRAMFS_IMAGE_FILE}"
> > + cp "${INITRAMFS_ROOTFS}/initrd.img" "${INITRAMFS_IMAGE_FILE}"
>
> ${INITRAMFS_IMAGE_FILE} references ${DEPLOY_DIR_IMAGE}, which may not
> exist yet. So I guess we should add something like
>
> do_generate_initramfs[dirs] = "${DEPLOY_DIR_IMAGE}"
>
> otherwise you have fix the dependency with some kind of "intermediate
> task" in the recipe using this class.

Yeah, I actually did this exact change in a downstream version of the
recipe but somehow forgot to add it here ... Will fix!

Harald

> > +}
> > +addtask generate_initramfs after do_rootfs before do_build
> > --
> > 2.29.2
> >
>

Harald Seiler

unread,
Jan 18, 2021, 5:08:45 AM1/18/21
to isar-...@googlegroups.com, Harald Seiler, Jan Kiszka, florian . bezdeka @ siemens . com
Add a new "image" class for generating a custom initramfs. It works
like this: A new minimal debian rootfs is bootstrapped and all
dependency packages for the new initramfs are installed. Then, an
initramfs is generated from this rootfs and deployed like usual.

This new initramfs.bbclass "image" class should be pulled in by an
"initramfs image" recipe. Said recipe then specifies all dependencies
of the initramfs via INITRAMFS_INSTALL and INITRAMFS_PREINSTALL (which
are analogous to the respective IMAGE_* variables).

initramfs.bbclass intentionally does _not_ expose a mechanism to change
/etc/initramfs-tools/initramfs.conf and /etc/initramfs-tools/modules.
Changes to their settings are better done via packages that deploy
conf-hooks to /usr/share/initramfs-tools/conf-hooks.d/ and module
fragment files to /usr/share/initramfs-tools/modules.d/.

Signed-off-by: Harald Seiler <h...@denx.de>
---

Changes in v4:
- Add ${DEPLOY_DIR_IMAGE} to task [dirs] to ensure it is present.

meta/classes/initramfs.bbclass | 42 ++++++++++++++++++++++++++++++++++
1 file changed, 42 insertions(+)
create mode 100644 meta/classes/initramfs.bbclass

diff --git a/meta/classes/initramfs.bbclass b/meta/classes/initramfs.bbclass
new file mode 100644
index 000000000000..10a642b1a6be
--- /dev/null
+++ b/meta/classes/initramfs.bbclass
@@ -0,0 +1,42 @@
+do_generate_initramfs[dirs] = "${DEPLOY_DIR_IMAGE}"
+do_generate_initramfs() {
+ rootfs_do_mounts
+ rootfs_do_qemu
+
+ sudo -E chroot "${INITRAMFS_ROOTFS}" \
+ update-initramfs -u -v
+
+ if [ ! -e "${INITRAMFS_ROOTFS}/initrd.img" ]; then
+ die "No initramfs was found after generation!"
+ fi
+
+ rm -rf "${INITRAMFS_IMAGE_FILE}"
+ cp "${INITRAMFS_ROOTFS}/initrd.img" "${INITRAMFS_IMAGE_FILE}"

Harald Seiler

unread,
Jan 18, 2021, 5:08:47 AM1/18/21
to isar-...@googlegroups.com, Harald Seiler, Jan Kiszka, florian . bezdeka @ siemens . com
Add the initramfs-example recipe/package which demonstrates how to write
initramfs modules. It demonstrates how to add hook scripts, boot scripts,
and conf-hooks.

Signed-off-by: Harald Seiler <h...@denx.de>
---

Notes:
Changes in v3:
- None

Changes in v4:
- None

.../initramfs-example/files/example.conf-hook | 7 ++++
.../initramfs-example/files/example.hook | 19 +++++++++
.../initramfs-example/files/example.script | 21 ++++++++++
.../initramfs-example/initramfs-example.bb | 40 +++++++++++++++++++
4 files changed, 87 insertions(+)
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.hook
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/files/example.script
create mode 100644 meta-isar/recipes-initramfs/initramfs-example/initramfs-example.bb

diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook b/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
new file mode 100644
index 000000000000..2a3cf7a84040
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/files/example.conf-hook
@@ -0,0 +1,7 @@
+# Example conf-hook.
+#
+# See "CONFIGURATION HOOK SCRIPTS" in initramfs-tools(7) for details.
+
+# Example: Use busybox instead of klibc-utils. The package must also add
+# `busybox` as a dependency when this is set.
+BUSYBOX=y
diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.hook b/meta-isar/recipes-initramfs/initramfs-example/files/example.hook
new file mode 100644
index 000000000000..0d84e7a97efd
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/files/example.hook
@@ -0,0 +1,19 @@
+#!/bin/sh
+# Example hook script.
+#
+# See "HOOK SCRIPTS" in initramfs-tools(7) for details.
+
+PREREQ=""
+prereqs()
+{
+ echo "$PREREQ"
+}
+case $1 in
+prereqs)
+ prereqs
+ exit 0
+ ;;
+esac
+
+. /usr/share/initramfs-tools/hook-functions
+# Begin real processing below this line
diff --git a/meta-isar/recipes-initramfs/initramfs-example/files/example.script b/meta-isar/recipes-initramfs/initramfs-example/files/example.script
new file mode 100644
new file mode 100644
index 000000000000..c336dda92b5d
--- /dev/null
+++ b/meta-isar/recipes-initramfs/initramfs-example/initramfs-example.bb
@@ -0,0 +1,40 @@
+# Example of a recipe containing an initramfs module. Packages like this can be
+# used with initramfs.bbclass or installed directly into a rootfs, depending on
+# the usecase.
+#
+# This software is a part of ISAR.
+

Harald Seiler

unread,
Jan 18, 2021, 5:08:48 AM1/18/21
to isar-...@googlegroups.com, Harald Seiler, Jan Kiszka, florian . bezdeka @ siemens . com
isar-initramfs is a custom initramfs which additionally has the
initramfs-example module installed. It is also built as part of
the CI.

Signed-off-by: Harald Seiler <h...@denx.de>
---

Notes:
Changes in v3:
- Add this target to ci_build.sh for CI inclusion.

Changes in v4:
- None

.../recipes-initramfs/images/isar-initramfs.bb | 18 ++++++++++++++++++
scripts/ci_build.sh | 1 +
2 files changed, 19 insertions(+)
create mode 100644 meta-isar/recipes-initramfs/images/isar-initramfs.bb

diff --git a/meta-isar/recipes-initramfs/images/isar-initramfs.bb b/meta-isar/recipes-initramfs/images/isar-initramfs.bb
new file mode 100644
index 000000000000..aaa0350aab20
--- /dev/null
+++ b/meta-isar/recipes-initramfs/images/isar-initramfs.bb
@@ -0,0 +1,18 @@
+# Example of a custom initramfs image recipe. The image will be deployed to
+#
+# build/tmp/deploy/images/${MACHINE}/isar-initramfs-${DISTRO}-${MACHINE}.initrd.img
+#
+# This software is a part of ISAR.
+

Anton Mikanovich

unread,
Feb 10, 2021, 4:10:33 AM2/10/21
to Harald Seiler, isar-...@googlegroups.com, Jan Kiszka, florian . bezdeka @ siemens . com
18.01.2021 13:07, Harald Seiler wrote:
> Add a new "image" class for generating a custom initramfs. It works
> like this: A new minimal debian rootfs is bootstrapped and all
> dependency packages for the new initramfs are installed. Then, an
> initramfs is generated from this rootfs and deployed like usual.
>
> This new initramfs.bbclass "image" class should be pulled in by an
> "initramfs image" recipe. Said recipe then specifies all dependencies
> of the initramfs via INITRAMFS_INSTALL and INITRAMFS_PREINSTALL (which
> are analogous to the respective IMAGE_* variables).
>
> initramfs.bbclass intentionally does _not_ expose a mechanism to change
> /etc/initramfs-tools/initramfs.conf and /etc/initramfs-tools/modules.
> Changes to their settings are better done via packages that deploy
> conf-hooks to /usr/share/initramfs-tools/conf-hooks.d/ and module
> fragment files to /usr/share/initramfs-tools/modules.d/.
>
> Signed-off-by: Harald Seiler <h...@denx.de>

Patchset applied to next, thanks.

--
Anton Mikanovich
Promwad Ltd.
External service provider of ilbers GmbH
Maria-Merian-Str. 8
85521 Ottobrunn, Germany
+49 (89) 122 67 24-0
Commercial register Munich, HRB 214197
General Manager: Baurzhan Ismagulov

Reply all
Reply to author
Forward
0 new messages