ab92d3
From 3b3dea36edc66300b84bb3887f8a5be971be959a Mon Sep 17 00:00:00 2001
ab92d3
From: Kairui Song <kasong@redhat.com>
ab92d3
Date: Mon, 15 Feb 2021 22:22:08 +0800
ab92d3
Subject: [PATCH] refactor(squash): move the post install scripts into the
ab92d3
 module-setup.sh
ab92d3
ab92d3
No function change, just move the post install code to 99squash to clean
ab92d3
up dracut.sh.
ab92d3
ab92d3
(cherry picked from commit 95ea16aa606912b7cc8f9942481c8a889b640c15)
ab92d3
ab92d3
Cherry-picked from: 95ea16aa
ab92d3
Resolves: #1959336
ab92d3
---
ab92d3
 dracut.sh                          | 44 ++-------------------------------
ab92d3
 modules.d/99squash/init.sh         |  2 +-
ab92d3
 modules.d/99squash/module-setup.sh | 50 +++++++++++++++++++++++++++++++++++---
ab92d3
 3 files changed, 50 insertions(+), 46 deletions(-)
ab92d3
ab92d3
diff --git a/dracut.sh b/dracut.sh
ab92d3
index 2090d89f..669bc6ce 100755
ab92d3
--- a/dracut.sh
ab92d3
+++ b/dracut.sh
ab92d3
@@ -1735,51 +1735,11 @@ if [[ $hostonly_cmdline == "yes" ]] ; then
ab92d3
 fi
ab92d3
 
ab92d3
 if dracut_module_included "squash"; then
ab92d3
-    dinfo "*** Install squash loader ***"
ab92d3
     readonly squash_dir="$initdir/squash/root"
ab92d3
     readonly squash_img="$initdir/squash/root.img"
ab92d3
-    readonly squash_candidate=( "usr" "etc" )
ab92d3
-
ab92d3
-    mkdir -m 0755 -p $squash_dir
ab92d3
-    for folder in "${squash_candidate[@]}"; do
ab92d3
-        mv $initdir/$folder $squash_dir/$folder
ab92d3
-    done
ab92d3
-
ab92d3
-    # Move some files out side of the squash image, including:
ab92d3
-    # - Files required to boot and mount the squashfs image
ab92d3
-    # - Files need to be accessible without mounting the squash image
ab92d3
-    # - Initramfs marker
ab92d3
-    for file in \
ab92d3
-        $squash_dir/usr/lib/modules/*/modules.* \
ab92d3
-        $squash_dir/usr/lib/dracut/* \
ab92d3
-        $squash_dir/etc/initrd-release
ab92d3
-    do
ab92d3
-        [[ -d $file ]] && continue
ab92d3
-        DRACUT_RESOLVE_DEPS=1 dracutsysrootdir=$squash_dir inst ${file#$squash_dir}
ab92d3
-        rm $file
ab92d3
-    done
ab92d3
 
ab92d3
-    mv $initdir/init $initdir/init.stock
ab92d3
-    ln -s squash/init.sh $initdir/init
ab92d3
-
ab92d3
-    # Reinstall required files for the squash image setup script.
ab92d3
-    # We have moved them inside the squashed image, but they need to be
ab92d3
-    # accessible before mounting the image.
ab92d3
-    inst_multiple "echo" "sh" "mount" "modprobe" "mkdir"
ab92d3
-    hostonly="" instmods "loop" "squashfs" "overlay"
ab92d3
-    # Only keep systemctl outsite if we need switch root
ab92d3
-    if [[ ! -f "$initdir/lib/dracut/no-switch-root" ]]; then
ab92d3
-      inst "systemctl"
ab92d3
-    fi
ab92d3
-
ab92d3
-    # Remove duplicated files
ab92d3
-    for folder in "${squash_candidate[@]}"; do
ab92d3
-        for file in $(find $initdir/$folder/ -not -type d); do
ab92d3
-            if [[ -e $squash_dir${file#$initdir} ]]; then
ab92d3
-                mv $squash_dir${file#$initdir} $file
ab92d3
-            fi
ab92d3
-        done
ab92d3
-    done
ab92d3
+    dinfo "*** Install squash loader ***"
ab92d3
+    DRACUT_SQUASH_POST_INST=1 module_install "squash"
ab92d3
 fi
ab92d3
 
ab92d3
 if [[ $kernel_only != yes ]]; then
ab92d3
diff --git a/modules.d/99squash/init.sh b/modules.d/99squash/init.sh
ab92d3
index bca49db5..d8b2cbba 100755
ab92d3
--- a/modules.d/99squash/init.sh
ab92d3
+++ b/modules.d/99squash/init.sh
ab92d3
@@ -1,7 +1,7 @@
ab92d3
 #!/bin/sh
ab92d3
 /squash/setup-squash.sh
ab92d3
 
ab92d3
-exec /init.stock
ab92d3
+exec /init.orig
ab92d3
 
ab92d3
 echo "Something went wrong when trying to start original init executable!"
ab92d3
 exit 1
ab92d3
diff --git a/modules.d/99squash/module-setup.sh b/modules.d/99squash/module-setup.sh
ab92d3
index ba9d52ff..0acf7c3a 100644
ab92d3
--- a/modules.d/99squash/module-setup.sh
ab92d3
+++ b/modules.d/99squash/module-setup.sh
ab92d3
@@ -30,11 +30,55 @@ installkernel() {
ab92d3
     hostonly="" instmods -c squashfs loop overlay
ab92d3
 }
ab92d3
 
ab92d3
-install() {
ab92d3
-    inst_multiple kmod modprobe mount mkdir ln echo
ab92d3
+installpost() {
ab92d3
+    local squash_candidate=( "usr" "etc" )
ab92d3
+
ab92d3
+    # shellcheck disable=SC2174
ab92d3
+    mkdir -m 0755 -p "$squash_dir"
ab92d3
+    for folder in "${squash_candidate[@]}"; do
ab92d3
+        mv "$initdir/$folder" "$squash_dir/$folder"
ab92d3
+    done
ab92d3
+
ab92d3
+    # Move some files out side of the squash image, including:
ab92d3
+    # - Files required to boot and mount the squashfs image
ab92d3
+    # - Files need to be accessible without mounting the squash image
ab92d3
+    # - Initramfs marker
ab92d3
+    for file in \
ab92d3
+        "$squash_dir"/usr/lib/modules/*/modules.* \
ab92d3
+        "$squash_dir"/usr/lib/dracut/* \
ab92d3
+        "$squash_dir"/etc/initrd-release
ab92d3
+    do
ab92d3
+        [[ -f $file ]] || continue
ab92d3
+        DRACUT_RESOLVE_DEPS=1 dracutsysrootdir="$squash_dir" inst "${file#$squash_dir}"
ab92d3
+        rm "$file"
ab92d3
+    done
ab92d3
+
ab92d3
+    # Install required files for the squash image setup script.
ab92d3
+    hostonly="" instmods "loop" "squashfs" "overlay"
ab92d3
+    inst_multiple modprobe mount mkdir ln echo
ab92d3
     inst "$moddir"/setup-squash.sh /squash/setup-squash.sh
ab92d3
     inst "$moddir"/clear-squash.sh /squash/clear-squash.sh
ab92d3
-    inst "$moddir"/init.sh /squash/init.sh
ab92d3
+
ab92d3
+    mv "$initdir"/init "$initdir"/init.orig
ab92d3
+    inst "$moddir"/init.sh "$initdir"/init
ab92d3
+
ab92d3
+    # Keep systemctl outsite if we need switch root
ab92d3
+    if [[ ! -f "$initdir/lib/dracut/no-switch-root" ]]; then
ab92d3
+      inst "systemctl"
ab92d3
+    fi
ab92d3
+
ab92d3
+    # Remove duplicated files
ab92d3
+    for folder in "${squash_candidate[@]}"; do
ab92d3
+        find "$initdir/$folder/" -not -type d \
ab92d3
+            -exec bash -c 'mv -f "$squash_dir${1#$initdir}" "$1"' -- "{}" \;
ab92d3
+    done
ab92d3
+}
ab92d3
+
ab92d3
+install() {
ab92d3
+    if [[ $DRACUT_SQUASH_POST_INST ]]; then
ab92d3
+        installpost
ab92d3
+        return
ab92d3
+    fi
ab92d3
 
ab92d3
     inst "$moddir/squash-mnt-clear.service" "$systemdsystemunitdir/squash-mnt-clear.service"
ab92d3
     systemctl -q --root "$initdir" add-wants initrd-switch-root.target squash-mnt-clear.service
ab92d3