File 0001-Add-support-for-mkosi-initrd.patch of Package transactional-update

From 10ebb61dcf8ee859030efb83940d12bdee352134 Mon Sep 17 00:00:00 2001
From: Antonio Alvarez Feijoo <antonio.feijoo@suse.com>
Date: Wed, 29 Jan 2025 12:32:00 +0100
Subject: [PATCH] Add support for mkosi-initrd

`mkosi` provides the `mkosi-initrd` script [1], which allows to generate initrds
locally on the system. Although it is not equivalent in features to `dracut`,
it's a valid alternative with its own benefits in some use cases:
- Clear ownership of bugs: no 3rd party binary (e.g. dracut) deciding what is
installed from each package or even modifying them.
- Any change on a package configured to be included would automatically apply to
the initrd (if the package triggers initrd rebuild).
- It attempts to shift the maintenance effort from the initrd generator to the
responsible package.

To choose which initrd generator will be used, use a new `INITRD_GENERATOR`
option in `/etc/sysconfig/bootloader`. If not defined, the default will still be
dracut.

[1] https://github.com/systemd/mkosi/commit/99ca14cffb13309a0e6865b2724cc199916c753b
---
 sbin/transactional-update.in | 61 +++++++++++++++++++++++++++++++++---
 1 file changed, 56 insertions(+), 5 deletions(-)

diff --git a/sbin/transactional-update.in b/sbin/transactional-update.in
index 8fbbb91..40e2cf2 100755
--- a/sbin/transactional-update.in
+++ b/sbin/transactional-update.in
@@ -57,6 +57,7 @@ NON_ROOTFS_WHITELIST=("/var/lib/YaST2/cookies" "/var/lib/rpm" "/var/lib/systemd/
 OCI_RSYNC_ARGS="-a --hard-links --xattrs --acls --inplace"
 OCI_RSYNC_EXCLUDES="/etc /var /usr/local /tmp /root /home /srv /opt /sys /dev /proc /run"
 OCI_TARGET=""
+INITRD_GENERATOR=""
 DRACUT_OPTS=""
 TUKIT_OPTS=""
 
@@ -249,6 +250,41 @@ is_bls() {
     [ -e "/usr/bin/sdbootutil" ] && /usr/bin/sdbootutil is-installed
 }
 
+find_initrd_generator() {
+    INITRD_GENERATOR="$(. /etc/sysconfig/bootloader 2>/dev/null && echo "$INITRD_GENERATOR")"
+
+    # dracut is the default initrd generator
+    if [ -z "$INITRD_GENERATOR" ]; then
+        INITRD_GENERATOR="dracut"
+    elif [ "$INITRD_GENERATOR" = "mkosi" ]; then
+        INITRD_GENERATOR="mkosi-initrd"
+    fi
+
+    [ "$INITRD_GENERATOR" = "dracut" ] || [ "$INITRD_GENERATOR" = "mkosi-initrd" ] || {
+        log_error "ERROR: the initrd generator \"$INITRD_GENERATOR\" configured in /etc/sysconfig/bootloader is not supported."
+        return 1
+    }
+
+    return 0
+}
+
+# FIXME: mkosi-initrd does not provide anything like --regenerate-all yet
+mkosi_initrd_regenerate_all() {
+    local snapshot_id="$1"
+    local snapshot_dir="$2"
+    local kver
+    local ret=0
+
+    for d in "${snapshot_dir}"/usr/lib/modules/*; do
+        [ -d "$d" ] || continue
+        kver=${d##*/}
+        tukit ${TUKIT_OPTS} call "${snapshot_id}" mkosi-initrd --kernel-version "${kver}" -O "/boot" -o "initrd-${kver}" |& tee -a ${LOGFILE} 1>&${origstdout}
+        ((ret += $?))
+    done
+
+    return "${ret}"
+}
+
 rebuild_kdump_initrd() {
     if tukit -q call "$1" systemctl is-enabled --quiet kdump.service; then
         tukit ${TUKIT_OPTS} call "$1" /sbin/mkdumprd |& tee -a ${LOGFILE} 1>&${origstdout}
@@ -1305,12 +1341,27 @@ if [ -n "${ZYPPER_ARG}" ] || [ ${REWRITE_GRUB_CFG} -eq 1 ] \
     fi
 
     if [ ${REWRITE_INITRD} -eq 1 ]; then
-        log_info "Creating new initrd"
-        if ! tukit ${TUKIT_OPTS} call "${SNAPSHOT_ID}" dracut ${DRACUT_OPTS} --force --regenerate-all |& tee -a ${LOGFILE} 1>&${origstdout}; then
-            log_error "ERROR: initrd creation failed!"
-            EXITCODE=1
+        if find_initrd_generator; then
+            log_info "Creating new initrd using $INITRD_GENERATOR"
+            case "$INITRD_GENERATOR" in
+                "dracut")
+                    if ! tukit ${TUKIT_OPTS} call "${SNAPSHOT_ID}" dracut ${DRACUT_OPTS} --force --regenerate-all |& tee -a ${LOGFILE} 1>&${origstdout}; then
+                        EXITCODE=1
+                    fi
+                    ;;
+                "mkosi-initrd")
+                    if ! mkosi_initrd_regenerate_all "${SNAPSHOT_ID}" "${SNAPSHOT_DIR}"; then
+                        EXITCODE=1
+                    fi
+                    ;;
+            esac
+            if [ ${EXITCODE} -ne 0 ]; then
+                log_error "ERROR: initrd creation failed!"
+            else
+                REBUILD_KDUMP_INITRD=1
+            fi
         else
-            REBUILD_KDUMP_INITRD=1
+            EXITCODE=1
         fi
         set_reboot_level "kexec"
     fi
-- 
2.43.0

openSUSE Build Service is sponsored by