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