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