ab92d3
From 9a6b40f023db3763694fb99a820f11017cc56811 Mon Sep 17 00:00:00 2001
ab92d3
From: Kairui Song <kasong@redhat.com>
ab92d3
Date: Tue, 9 Jun 2020 00:41:24 +0800
ab92d3
Subject: [PATCH] 99squash: simplify the code
ab92d3
ab92d3
The new dracutsysrootdir could be used to replace the shell function
ab92d3
required_in_root, so drop it and also simplify the code.
ab92d3
ab92d3
Signed-off-by: Kairui Song <kasong@redhat.com>
ab92d3
(cherry picked from commit 4159819fbb20fca8c0a80ddb17e211f481ec7717)
ab92d3
ab92d3
Resolves: #1959336
ab92d3
---
ab92d3
 dracut.sh | 89 ++++++++++++++-------------------------------------------------
ab92d3
 1 file changed, 20 insertions(+), 69 deletions(-)
ab92d3
ab92d3
diff --git a/dracut.sh b/dracut.sh
ab92d3
index b9657dc6..176b2259 100755
ab92d3
--- a/dracut.sh
ab92d3
+++ b/dracut.sh
ab92d3
@@ -1736,23 +1736,19 @@ fi
ab92d3
 
ab92d3
 if dracut_module_included "squash"; then
ab92d3
     dinfo "*** Install squash loader ***"
ab92d3
-    if ! check_kernel_config CONFIG_SQUASHFS; then
ab92d3
-        dfatal "CONFIG_SQUASHFS have to be enabled for dracut squash module to work"
ab92d3
-        exit 1
ab92d3
-    fi
ab92d3
-    if ! check_kernel_config CONFIG_OVERLAY_FS; then
ab92d3
-        dfatal "CONFIG_OVERLAY_FS have to be enabled for dracut squash module to work"
ab92d3
-        exit 1
ab92d3
-    fi
ab92d3
-    if ! check_kernel_config CONFIG_DEVTMPFS; then
ab92d3
-        dfatal "CONFIG_DEVTMPFS have to be enabled for dracut squash module to work"
ab92d3
+    for config in \
ab92d3
+      CONFIG_SQUASHFS \
ab92d3
+      CONFIG_OVERLAY_FS \
ab92d3
+      CONFIG_DEVTMPFS;
ab92d3
+    do
ab92d3
+      if ! check_kernel_config $config; then
ab92d3
+        dfatal "$config have to be enabled for dracut squash module to work"
ab92d3
         exit 1
ab92d3
-    fi
ab92d3
+      fi
ab92d3
+    done
ab92d3
 
ab92d3
     readonly squash_dir="$initdir/squash/root"
ab92d3
-    readonly squash_img=$initdir/squash/root.img
ab92d3
-
ab92d3
-    # Currently only move "usr" "etc" to squashdir
ab92d3
+    readonly squash_img="$initdir/squash/root.img"
ab92d3
     readonly squash_candidate=( "usr" "etc" )
ab92d3
 
ab92d3
     mkdir -m 0755 -p $squash_dir
ab92d3
@@ -1763,57 +1759,15 @@ if dracut_module_included "squash"; then
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
-    required_in_root() {
ab92d3
-        local file=$1
ab92d3
-        local _sqsh_file=$squash_dir/$file
ab92d3
-        local _init_file=$initdir/$file
ab92d3
-
ab92d3
-        if [[ -e $_init_file ]]; then
ab92d3
-            return
ab92d3
-        fi
ab92d3
-
ab92d3
-        if [[ ! -e $_sqsh_file ]] && [[ ! -L $_sqsh_file ]]; then
ab92d3
-            derror "$file is required to boot a squashed initramfs but it's not installed!"
ab92d3
-            return
ab92d3
-        fi
ab92d3
-
ab92d3
-        if [[ ! -d $(dirname $_init_file) ]]; then
ab92d3
-            required_in_root $(dirname $file)
ab92d3
-        fi
ab92d3
-
ab92d3
-        if [[ -L $_sqsh_file ]]; then
ab92d3
-          cp --preserve=all -P $_sqsh_file $_init_file
ab92d3
-          _sqsh_file=$(realpath $_sqsh_file 2>/dev/null)
ab92d3
-          if [[ -e $_sqsh_file ]] && [[ "$_sqsh_file" == "$squash_dir"* ]]; then
ab92d3
-            # Relative symlink
ab92d3
-            required_in_root ${_sqsh_file#$squash_dir/}
ab92d3
-            return
ab92d3
-          fi
ab92d3
-          if [[ -e $squash_dir$_sqsh_file ]]; then
ab92d3
-            # Absolute symlink
ab92d3
-            required_in_root ${_sqsh_file#/}
ab92d3
-            return
ab92d3
-          fi
ab92d3
-          required_in_root ${module_spec#$squash_dir/}
ab92d3
-        else
ab92d3
-          if [[ -d $_sqsh_file ]]; then
ab92d3
-            mkdir $_init_file
ab92d3
-          else
ab92d3
-            mv $_sqsh_file $_init_file
ab92d3
-          fi
ab92d3
-        fi
ab92d3
-    }
ab92d3
-
ab92d3
-    required_in_root etc/initrd-release
ab92d3
-
ab92d3
-    for module_spec in $squash_dir/usr/lib/modules/*/modules.*;
ab92d3
-    do
ab92d3
-        required_in_root ${module_spec#$squash_dir/}
ab92d3
-    done
ab92d3
-
ab92d3
-    for dracut_spec in $squash_dir/usr/lib/dracut/*;
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
-        required_in_root ${dracut_spec#$squash_dir/}
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
@@ -1824,17 +1778,14 @@ if dracut_module_included "squash"; then
ab92d3
     # accessible before mounting the image.
ab92d3
     inst_multiple "echo" "sh" "mount" "modprobe" "mkdir"
ab92d3
     hostonly="" instmods "loop" "squashfs" "overlay"
ab92d3
-
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
-        # Remove duplicated files in squashfs image, save some more space
ab92d3
-        [[ ! -d $initdir/$folder/ ]] && continue
ab92d3
-        for file in $(find $initdir/$folder/ -not -type d);
ab92d3
-        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