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