* [PATCH v2 0/2] Significantly speedup image generation
@ 2025-04-23 12:43 'Felix Moessbauer' via isar-users
2025-04-23 12:43 ` [PATCH v2 1/2] delay creation of initrd until end of rootfs install 'Felix Moessbauer' via isar-users
2025-04-23 12:43 ` [PATCH v2 2/2] report approximate progress during initrd generation 'Felix Moessbauer' via isar-users
0 siblings, 2 replies; 3+ messages in thread
From: 'Felix Moessbauer' via isar-users @ 2025-04-23 12:43 UTC (permalink / raw)
To: isar-users; +Cc: adriaan.schmidt, cedric.hombourger, Felix Moessbauer
Especially on non native machines, the image generation can take a long time.
This is due to the fact that the initrd is generated during the rootfs
installation. By that, also the initrd is generated multiple times, despite
only one final version is needed. In case of a out-of-rootfs initrd, it is built
5 times (2x in rootfs, 2x in initramfs +1x manually in initramfs).
We optimize this by manually controlling when the initrd is built instead of
relying on the triggers from the initramfs-tools. As initramfs-tools does
not provide a conf.d style mechanism to completely disable the generation,
we now install this package during bootstrap and then stub the update-initramfs
calls during rootfs installation. In rootfs_postprocess, we restore the
original update-initramfs calls and build the initrd manually.
Changes since Patch v1:
- fix deployment of initrd for ubuntu (dracut) targets
- fix incorrect path-globbing in rootfs_quality_check
Changes since RFC v3:
- Use path-injection during do_packages_install to replace the update-initramfs script
- No need to add packages to bootstrap
- The initrd stub is deployed and removed in do_rootfs_install. By that, the clean rootfs
is deployed into the sstate cache
Changes since RFC v2:
- Complete re-design of approach to better integrate with sstate cache
- Unify logic in rootfs.bblcass and initramfs.bbclass
Best regards,
Felix
Felix Moessbauer (2):
delay creation of initrd until end of rootfs install
report approximate progress during initrd generation
meta/classes/image.bbclass | 15 +---
meta/classes/initramfs.bbclass | 39 +---------
meta/classes/rootfs.bbclass | 78 ++++++++++++++++++-
meta/lib/rootfs_progress.py | 33 ++++++--
.../sbuild-chroot/sbuild-chroot.inc | 1 +
5 files changed, 110 insertions(+), 56 deletions(-)
--
2.39.5
--
You received this message because you are subscribed to the Google Groups "isar-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to isar-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/isar-users/20250423124359.1081781-1-felix.moessbauer%40siemens.com.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 1/2] delay creation of initrd until end of rootfs install
2025-04-23 12:43 [PATCH v2 0/2] Significantly speedup image generation 'Felix Moessbauer' via isar-users
@ 2025-04-23 12:43 ` 'Felix Moessbauer' via isar-users
2025-04-23 12:43 ` [PATCH v2 2/2] report approximate progress during initrd generation 'Felix Moessbauer' via isar-users
1 sibling, 0 replies; 3+ messages in thread
From: 'Felix Moessbauer' via isar-users @ 2025-04-23 12:43 UTC (permalink / raw)
To: isar-users; +Cc: adriaan.schmidt, cedric.hombourger, Felix Moessbauer
This patch solves major performance issues around the initramfs
creation by ensuring that the initrd is only created once. This is
implemented by stubbing the update-initramfs call during the package
installing. After all apt operations are completed, we manually
trigger the initrd creation. In case a custom initramfs is used, the
creation is completely skipped in the image rootfs, as this would
anyways not be used. We further unify the initrd generation in the
rootfs and in the initramfs class.
Before that, each package install that made a initrd relevant change
triggered the update of the initrd. As we have multiple apt calls during
the build, this step was sometimes executed multiple times. In addition,
the apt install step is emulated, further slowing down the initrd
generation.
On a test build of the RPi4b target with a detached initramfs and a
distro kernel, this patch set reduced the build time form ~50min to
~15min.
Signed-off-by: Felix Moessbauer <felix.moessbauer@siemens.com>
---
meta/classes/image.bbclass | 15 +---
meta/classes/initramfs.bbclass | 39 +---------
meta/classes/rootfs.bbclass | 72 ++++++++++++++++++-
.../sbuild-chroot/sbuild-chroot.inc | 1 +
4 files changed, 76 insertions(+), 51 deletions(-)
diff --git a/meta/classes/image.bbclass b/meta/classes/image.bbclass
index ff3cd737..a766f7d5 100644
--- a/meta/classes/image.bbclass
+++ b/meta/classes/image.bbclass
@@ -67,6 +67,8 @@ inherit essential
ROOTFSDIR = "${IMAGE_ROOTFS}"
ROOTFS_FEATURES += "clean-package-cache clean-pycache generate-manifest export-dpkg-status clean-log-files clean-debconf-cache"
+# when using a custom initrd, do not generate one as part of the image rootfs
+ROOTFS_FEATURES += "${@ '' if d.getVar('INITRD_IMAGE') == '' else 'no-generate-initrd'}"
ROOTFS_PACKAGES += "${IMAGE_PREINSTALL} ${@isar_multiarch_packages('IMAGE_INSTALL', d)}"
ROOTFS_MANIFEST_DEPLOY_DIR ?= "${DEPLOY_DIR_IMAGE}"
ROOTFS_DPKGSTATUS_DEPLOY_DIR ?= "${DEPLOY_DIR_IMAGE}"
@@ -341,17 +343,6 @@ do_copy_boot_files() {
sudo cat "$kernel" > "${DEPLOYDIR}/${KERNEL_IMAGE}"
fi
- if [ -z "${INITRD_IMAGE}" ]; then
- # deploy default initrd if no custom one is build
- initrd="$(realpath -q '${IMAGE_ROOTFS}/initrd.img')"
- if [ ! -f "$initrd" ]; then
- initrd="$(realpath -q '${IMAGE_ROOTFS}/boot/initrd.img')"
- fi
- if [ -f "$initrd" ]; then
- cp -f "$initrd" '${DEPLOYDIR}/${INITRD_DEPLOY_FILE}'
- fi
- fi
-
for file in ${DTB_FILES}; do
dtb="$(find '${IMAGE_ROOTFS}/usr/lib' -type f \
-iwholename '*linux-image-*/'${file} | head -1)"
@@ -450,7 +441,7 @@ EOSUDO
-exec touch '{}' -h -d@${SOURCE_DATE_EPOCH} ';'
}
do_rootfs_finalize[network] = "${TASK_USE_SUDO}"
-addtask rootfs_finalize before do_rootfs after do_rootfs_postprocess
+addtask rootfs_finalize before do_rootfs after do_rootfs_postprocess do_generate_initramfs
ROOTFS_QA_FIND_ARGS ?= ""
diff --git a/meta/classes/initramfs.bbclass b/meta/classes/initramfs.bbclass
index 0197a60b..3a996f78 100644
--- a/meta/classes/initramfs.bbclass
+++ b/meta/classes/initramfs.bbclass
@@ -6,14 +6,11 @@ DEPLOYDIR = "${WORKDIR}/deploy"
STAMP = "${STAMPS_DIR}/${DISTRO}-${DISTRO_ARCH}/${PN}-${MACHINE}/${PV}-${PR}"
STAMPCLEAN = "${STAMPS_DIR}/${DISTRO}-${DISTRO_ARCH}/${PN}-${MACHINE}/*-*"
-# Sstate also needs to be machine-specific
-SSTATE_MANIFESTS = "${TMPDIR}/sstate-control/${MACHINE}-${DISTRO}-${DISTRO_ARCH}"
-SSTATETASKS += "do_generate_initramfs"
-
INITRAMFS_INSTALL ?= ""
INITRAMFS_PREINSTALL ?= ""
INITRAMFS_ROOTFS ?= "${WORKDIR}/rootfs"
INITRAMFS_IMAGE_NAME = "${INITRAMFS_FULLNAME}.initrd.img"
+INITRD_DEPLOY_FILE = "${INITRAMFS_IMAGE_NAME}"
# Install proper kernel
INITRAMFS_INSTALL += "${@ ("linux-image-" + d.getVar("KERNEL_NAME")) if d.getVar("KERNEL_NAME") else ""}"
@@ -28,37 +25,3 @@ ROOTFS_FEATURES = ""
ROOTFS_PACKAGES = "initramfs-tools ${INITRAMFS_PREINSTALL} ${INITRAMFS_INSTALL}"
inherit rootfs
-
-do_generate_initramfs[network] = "${TASK_USE_SUDO}"
-do_generate_initramfs[cleandirs] += "${DEPLOYDIR}"
-do_generate_initramfs[sstate-inputdirs] = "${DEPLOYDIR}"
-do_generate_initramfs[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
-do_generate_initramfs() {
- rootfs_do_mounts
-
- trap 'exit 1' INT HUP QUIT TERM ALRM USR1
- trap 'rootfs_do_umounts' EXIT
-
- rootfs_do_qemu
-
- sudo -E chroot "${INITRAMFS_ROOTFS}" sh -c '\
- export kernel_version=$(basename /boot/vmlinu[xz]* | cut -d'-' -f2-); \
- if [ -n "$kernel_version" ]; then \
- update-initramfs -u -v -k "$kernel_version"; \
- else \
- update-initramfs -u -v ; \
- fi'
-
- rootfs_do_umounts
-
- if [ ! -e "${INITRAMFS_ROOTFS}/initrd.img" ]; then
- bberror "No initramfs was found after generation!"
- fi
- cp ${INITRAMFS_ROOTFS}/initrd.img ${DEPLOYDIR}/${INITRAMFS_IMAGE_NAME}
-}
-addtask generate_initramfs after do_rootfs before do_build
-
-python do_generate_initramfs_setscene () {
- sstate_setscene(d)
-}
-addtask do_generate_initramfs_setscene
diff --git a/meta/classes/rootfs.bbclass b/meta/classes/rootfs.bbclass
index 446d92d6..fd6bb24a 100644
--- a/meta/classes/rootfs.bbclass
+++ b/meta/classes/rootfs.bbclass
@@ -14,6 +14,7 @@ ROOTFS_BASE_DISTRO ?= "${BASE_DISTRO}"
# 'generate-manifest' - generate a package manifest of the rootfs into ${ROOTFS_MANIFEST_DEPLOY_DIR}
# 'export-dpkg-status' - exports /var/lib/dpkg/status file to ${ROOTFS_DPKGSTATUS_DEPLOY_DIR}
# 'clean-log-files' - delete log files that are not owned by packages
+# 'no-generate-initrd' - do not generate debian default initrd
ROOTFS_FEATURES ?= ""
ROOTFS_APT_ARGS="install --yes -o Debug::pkgProblemResolver=yes"
@@ -22,6 +23,9 @@ ROOTFS_CLEAN_FILES="/etc/hostname /etc/resolv.conf"
ROOTFS_PACKAGE_SUFFIX ?= "${PN}-${DISTRO}-${DISTRO_ARCH}"
+# path to deploy stubbed versions of initrd update scripts during do_rootfs_install
+ROOTFS_STUBS_DIR = "/usr/local/isar-sbin"
+
# Useful environment variables:
export E = "${@ isar_export_proxies(d)}"
export DEBIAN_FRONTEND = "noninteractive"
@@ -165,6 +169,13 @@ rootfs_configure_apt() {
EOSUDO
}
+ROOTFS_CONFIGURE_COMMAND += "rootfs_disable_initrd_generation"
+rootfs_disable_initrd_generation[weight] = "1"
+rootfs_disable_initrd_generation() {
+ # fully disable initrd generation
+ sudo mkdir -p "${ROOTFSDIR}${ROOTFS_STUBS_DIR}"
+ sudo cp -a ${ROOTFSDIR}/usr/bin/true ${ROOTFSDIR}${ROOTFS_STUBS_DIR}/update-initramfs
+}
ROOTFS_INSTALL_COMMAND += "rootfs_install_pkgs_update"
rootfs_install_pkgs_update[weight] = "5"
@@ -227,7 +238,21 @@ rootfs_install_pkgs_install[progress] = "custom:rootfs_progress.PkgsInstallProgr
rootfs_install_pkgs_install[network] = "${TASK_USE_SUDO}"
rootfs_install_pkgs_install() {
sudo -E chroot "${ROOTFSDIR}" \
- /usr/bin/apt-get ${ROOTFS_APT_ARGS} ${ROOTFS_PACKAGES}
+ /usr/bin/apt-get ${ROOTFS_APT_ARGS} \
+ -o DPkg::Path='${ROOTFS_STUBS_DIR}:/usr/sbin:/usr/bin:/sbin:/bin' \
+ ${ROOTFS_PACKAGES}
+}
+
+ROOTFS_INSTALL_COMMAND += "rootfs_restore_initrd_tooling"
+rootfs_restore_initrd_tooling[weight] = "1"
+rootfs_restore_initrd_tooling() {
+ sudo rm -rf "${ROOTFSDIR}${ROOTFS_STUBS_DIR}"
+}
+
+ROOTFS_INSTALL_COMMAND += "${@bb.utils.contains('ROOTFS_FEATURES', 'no-generate-initrd', 'rootfs_clear_initrd_symlinks', '', d)}"
+rootfs_clear_initrd_symlinks() {
+ sudo rm -f ${ROOTFSDIR}/initrd.img
+ sudo rm -f ${ROOTFSDIR}/initrd.img.old
}
do_rootfs_install[root_cleandirs] = "${ROOTFSDIR}"
@@ -421,6 +446,51 @@ python do_rootfs_postprocess() {
}
addtask rootfs_postprocess before do_rootfs after do_unpack
+SSTATETASKS += "do_generate_initramfs"
+do_generate_initramfs[network] = "${TASK_USE_SUDO}"
+do_generate_initramfs[cleandirs] += "${DEPLOYDIR}"
+do_generate_initramfs[sstate-inputdirs] = "${DEPLOYDIR}"
+do_generate_initramfs[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
+python do_generate_initramfs() {
+ bb.build.exec_func('rootfs_do_mounts', d)
+ bb.build.exec_func('rootfs_do_qemu', d)
+ try:
+ bb.build.exec_func('rootfs_generate_initramfs', d)
+ finally:
+ bb.build.exec_func('rootfs_do_umounts', d)
+}
+
+python do_generate_initramfs_setscene () {
+ sstate_setscene(d)
+}
+
+rootfs_generate_initramfs[progress] = "custom:rootfs_progress.InitrdProgressHandler"
+rootfs_generate_initramfs() {
+ if [ -n "$(sudo find '${ROOTFSDIR}/boot' -type f -name 'vmlinu[xz]*')" ]; then
+ sudo -E chroot "${ROOTFSDIR}" sh -c '\
+ export kernel_version=$(basename /boot/vmlinu[xz]-* | cut -d'-' -f2-); \
+ echo "Generating initrd for kernel version: $kernel_version"; \
+ update-initramfs -u -v -k "$kernel_version";'
+ if [ -n "${INITRD_DEPLOY_FILE}" ]; then
+ if [ -f "${ROOTFSDIR}/initrd.img" ]; then
+ # debian (mkinitramfs)
+ cp ${ROOTFSDIR}/initrd.img ${DEPLOYDIR}/${INITRD_DEPLOY_FILE}
+ else
+ # ubuntu (dracut)
+ cp ${ROOTFSDIR}/boot/initrd.img ${DEPLOYDIR}/${INITRD_DEPLOY_FILE}
+ fi
+ fi
+ else
+ echo "no kernel in this rootfs, do not generate initrd"
+ fi
+}
+
+python() {
+ if 'no-generate-initrd' not in d.getVar('ROOTFS_FEATURES', True).split():
+ bb.build.addtask('do_generate_initramfs', 'do_rootfs', 'do_rootfs_postprocess', d)
+ bb.build.addtask('do_generate_initramfs_setscene', None, None, d)
+}
+
python do_rootfs() {
"""Virtual task"""
pass
diff --git a/meta/recipes-devtools/sbuild-chroot/sbuild-chroot.inc b/meta/recipes-devtools/sbuild-chroot/sbuild-chroot.inc
index 6478d3a6..98e427e5 100644
--- a/meta/recipes-devtools/sbuild-chroot/sbuild-chroot.inc
+++ b/meta/recipes-devtools/sbuild-chroot/sbuild-chroot.inc
@@ -55,6 +55,7 @@ SBUILD_CHROOT_PREINSTALL_COMMON = " \
SBUILD_CHROOT_DIR = "${WORKDIR}/rootfs"
ROOTFSDIR = "${SBUILD_CHROOT_DIR}"
ROOTFS_PACKAGES = "${SBUILD_CHROOT_PREINSTALL}"
+ROOTFS_FEATURES += "no-generate-initrd"
ROOTFS_POSTPROCESS_COMMAND:remove = "rootfs_cleanup_isar_apt"
--
2.39.5
--
You received this message because you are subscribed to the Google Groups "isar-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to isar-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/isar-users/20250423124359.1081781-2-felix.moessbauer%40siemens.com.
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2 2/2] report approximate progress during initrd generation
2025-04-23 12:43 [PATCH v2 0/2] Significantly speedup image generation 'Felix Moessbauer' via isar-users
2025-04-23 12:43 ` [PATCH v2 1/2] delay creation of initrd until end of rootfs install 'Felix Moessbauer' via isar-users
@ 2025-04-23 12:43 ` 'Felix Moessbauer' via isar-users
1 sibling, 0 replies; 3+ messages in thread
From: 'Felix Moessbauer' via isar-users @ 2025-04-23 12:43 UTC (permalink / raw)
To: isar-users; +Cc: adriaan.schmidt, cedric.hombourger, Felix Moessbauer
On non native architectures, the initrd generation has to be emulated.
In combination with kernels that provide many modules (like the distro
kernels), this leads to long initrd build times. To give a rough
estimation of the duration, we add progress reporting to that step.
As we always build the initrd with MODULES=most, we know from
experiments, that ~50% of the kernel modules of the rootfs are included
in the initrd. We use that number as a guesstimate, as we don't have
precise numbers when starting the task.
Signed-off-by: Felix Moessbauer <felix.moessbauer@siemens.com>
---
meta/classes/rootfs.bbclass | 6 ++++++
meta/lib/rootfs_progress.py | 33 ++++++++++++++++++++++++++++-----
2 files changed, 34 insertions(+), 5 deletions(-)
diff --git a/meta/classes/rootfs.bbclass b/meta/classes/rootfs.bbclass
index fd6bb24a..ab30ab91 100644
--- a/meta/classes/rootfs.bbclass
+++ b/meta/classes/rootfs.bbclass
@@ -454,6 +454,10 @@ do_generate_initramfs[sstate-outputdirs] = "${DEPLOY_DIR_IMAGE}"
python do_generate_initramfs() {
bb.build.exec_func('rootfs_do_mounts', d)
bb.build.exec_func('rootfs_do_qemu', d)
+
+ progress_reporter = bb.progress.ProgressHandler(d)
+ d.rootfs_progress = progress_reporter
+
try:
bb.build.exec_func('rootfs_generate_initramfs', d)
finally:
@@ -468,7 +472,9 @@ rootfs_generate_initramfs[progress] = "custom:rootfs_progress.InitrdProgressHand
rootfs_generate_initramfs() {
if [ -n "$(sudo find '${ROOTFSDIR}/boot' -type f -name 'vmlinu[xz]*')" ]; then
sudo -E chroot "${ROOTFSDIR}" sh -c '\
+ mods_total="$(find /usr/lib/modules -type f -name '*.ko*' | wc -l)"; \
export kernel_version=$(basename /boot/vmlinu[xz]-* | cut -d'-' -f2-); \
+ echo "Total number of modules: $mods_total"; \
echo "Generating initrd for kernel version: $kernel_version"; \
update-initramfs -u -v -k "$kernel_version";'
if [ -n "${INITRD_DEPLOY_FILE}" ]; then
diff --git a/meta/lib/rootfs_progress.py b/meta/lib/rootfs_progress.py
index f808852f..1cc70c87 100644
--- a/meta/lib/rootfs_progress.py
+++ b/meta/lib/rootfs_progress.py
@@ -28,14 +28,16 @@ class PkgsProgressHandler(bb.progress.ProgressHandler):
self._linebuffer = self._linebuffer[breakpos:]
if self._stage == 'prepare':
- match = re.search(
- r'^([0-9]+) upgraded, ([0-9]+) newly installed', line)
- if match:
- self._num_pkgs = int(match.group(1)) + int(match.group(2))
- self._stage = 'post-prepare'
+ self.process_total(line)
else:
self.process_line(line)
+ def process_total(self, line):
+ m = re.search(r'^([0-9]+) upgraded, ([0-9]+) newly installed', line)
+ if m:
+ self._num_pkgs = int(m.group(1)) + int(m.group(2))
+ self._stage = 'post-prepare'
+
def process_line(self, line):
return
@@ -65,3 +67,24 @@ class PkgsInstallProgressHandler(PkgsProgressHandler):
progress = 99 * (self._pkg + self._pkg_set_up) / (self._num_pkgs * 2)
self._progress.update(progress)
+
+
+class InitrdProgressHandler(PkgsProgressHandler):
+ def __init__(self, d, outfile, otherargs=None):
+ super().__init__(d, outfile)
+
+ def process_total(self, line):
+ m = re.search(r'^Total number of modules: ([0-9]+)', line)
+ if m:
+ # in MODULES=most mode, we install ~half of all modules
+ self._num_pkgs = int(m.group(1)) // 2
+ self._stage = 'post-prepare'
+
+ def process_line(self, line):
+ if line.startswith('Adding module'):
+ self._pkg += 1
+ elif line.startswith('(excluding'):
+ self._pkg += len(line.split(' ')) - 1
+ else:
+ return
+ self._progress.update(99 * self._pkg / self._num_pkgs)
--
2.39.5
--
You received this message because you are subscribed to the Google Groups "isar-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to isar-users+unsubscribe@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/isar-users/20250423124359.1081781-3-felix.moessbauer%40siemens.com.
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2025-04-23 12:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-04-23 12:43 [PATCH v2 0/2] Significantly speedup image generation 'Felix Moessbauer' via isar-users
2025-04-23 12:43 ` [PATCH v2 1/2] delay creation of initrd until end of rootfs install 'Felix Moessbauer' via isar-users
2025-04-23 12:43 ` [PATCH v2 2/2] report approximate progress during initrd generation 'Felix Moessbauer' via isar-users
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox