121229
From 829ed8ef5cca250fac475ad6b0265c792c3af015 Mon Sep 17 00:00:00 2001
121229
From: Kairui Song <kasong@redhat.com>
121229
Date: Wed, 13 Mar 2019 17:36:53 +0800
121229
Subject: [PATCH] squash: also squash systemctl if switch-root is not needed
121229
121229
systemctl need to be accessible on switch-root, but we unmount the
121229
squash image on switch-root, so it will fail. systemctl depends on a lot
121229
of libraries, squash them can save more RAM. So allow modules
121229
(eg. kdump) to tell dracut that switch-root will be intercepted,
121229
then we don't need to take care of that.
121229
121229
Signed-off-by: Kairui Song <kasong@redhat.com>
121229
(cherry picked from commit 3ee0ca5eb74be5d1fbd0e6d643f6fff06234177f)
121229
121229
Resolves: #1691705
121229
---
121229
 dracut-init.sh |  4 ++++
121229
 dracut.sh      | 40 ++++++++++++++++++++++------------------
121229
 2 files changed, 26 insertions(+), 18 deletions(-)
121229
121229
diff --git a/dracut-init.sh b/dracut-init.sh
121229
index 50d23e2d..b1ac9966 100644
121229
--- a/dracut-init.sh
121229
+++ b/dracut-init.sh
121229
@@ -150,6 +150,10 @@ dracut_module_included() {
121229
     [[ " $mods_to_load $modules_loaded " == *\ $*\ * ]]
121229
 }
121229
 
121229
+dracut_no_switch_root() {
121229
+    >"$initdir/lib/dracut/no-switch-root"
121229
+}
121229
+
121229
 if ! [[ $DRACUT_INSTALL ]]; then
121229
     DRACUT_INSTALL=$(find_binary dracut-install)
121229
 fi
121229
diff --git a/dracut.sh b/dracut.sh
121229
index 6de88b3e..8144a16e 100755
121229
--- a/dracut.sh
121229
+++ b/dracut.sh
121229
@@ -1772,24 +1772,6 @@ if dracut_module_included "squash"; then
121229
         mv $initdir/$folder $squash_dir/$folder
121229
     done
121229
 
121229
-    # Reinstall required files for the squash image setup script.
121229
-    # We have moved them inside the squashed image, but they need to be
121229
-    # accessible before mounting the image. Also install systemctl,
121229
-    # it's requires for switch-root, but we will umount the image before switch-root
121229
-    inst_multiple "echo" "sh" "mount" "modprobe" "mkdir" "systemctl"
121229
-    hostonly="" instmods "loop" "squashfs" "overlay"
121229
-
121229
-    for folder in "${squash_candidate[@]}"; do
121229
-        # Remove duplicated files in squashfs image, save some more space
121229
-        [[ ! -d $initdir/$folder/ ]] && continue
121229
-        for file in $(find $initdir/$folder/ -not -type d);
121229
-        do
121229
-            if [[ -e $squash_dir${file#$initdir} ]]; then
121229
-                mv $squash_dir${file#$initdir} $file
121229
-            fi
121229
-        done
121229
-    done
121229
-
121229
     # Move some files out side of the squash image, including:
121229
     # - Files required to boot and mount the squashfs image
121229
     # - Files need to be accessable without mounting the squash image
121229
@@ -1851,6 +1833,28 @@ if dracut_module_included "squash"; then
121229
     ln -s squash/init.sh $initdir/init
121229
     ln -s squash/shutdown.sh $initdir/shutdown
121229
 
121229
+    # Reinstall required files for the squash image setup script.
121229
+    # We have moved them inside the squashed image, but they need to be
121229
+    # accessible before mounting the image.
121229
+    inst_multiple "echo" "sh" "mount" "modprobe" "mkdir"
121229
+    hostonly="" instmods "loop" "squashfs" "overlay"
121229
+
121229
+    # Only keep systemctl outsite if we need switch root
121229
+    if [[ ! -f "$initdir/lib/dracut/no-switch-root" ]]; then
121229
+      inst "systemctl"
121229
+    fi
121229
+
121229
+    for folder in "${squash_candidate[@]}"; do
121229
+        # Remove duplicated files in squashfs image, save some more space
121229
+        [[ ! -d $initdir/$folder/ ]] && continue
121229
+        for file in $(find $initdir/$folder/ -not -type d);
121229
+        do
121229
+            if [[ -e $squash_dir${file#$initdir} ]]; then
121229
+                mv $squash_dir${file#$initdir} $file
121229
+            fi
121229
+        done
121229
+    done
121229
+
121229
     mksquashfs $squash_dir $squash_img -comp xz -b 64K -Xdict-size 100% &> /dev/null
121229
 
121229
     if [[ $? != 0 ]]; then
121229