9f65cc
From f462176a19f463861fea7a26af6288403785eb9b Mon Sep 17 00:00:00 2001
9f65cc
From: Kairui Song <kasong@redhat.com>
9f65cc
Date: Mon, 15 Feb 2021 14:04:05 +0800
9f65cc
Subject: [PATCH] feat(squash): use busybox for early setup if available
9f65cc
9f65cc
Use busybox can help reduce the size of early setup environment.
9f65cc
9f65cc
With this change, everything is packed in the squash image, and
9f65cc
the setup files will be dropped once squash image setup is done,
9f65cc
so initramfs stage memory usage is reduced to the minimun,
9f65cc
and initramfs decompress is also faster.
9f65cc
9f65cc
File layout of a squash initramfs looks like this:
9f65cc
9f65cc
========================================================================
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 .
9f65cc
-rwxr-xr-x   1 root     root          946 Feb 15 14:07 init
9f65cc
lrwxrwxrwx   1 root     root            7 Feb 15 14:07 lib -> usr/lib
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 squash
9f65cc
-rw-r--r--   1 root     root     91000832 Feb 15 14:07 squash-root.img
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 usr
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 usr/bin
9f65cc
-rwxr-xr-x   1 root     root      1293688 Jul 27  2020 usr/bin/busybox
9f65cc
lrwxrwxrwx   1 root     root            7 Feb 15 14:07 usr/bin/echo -> busybox
9f65cc
lrwxrwxrwx   1 root     root            7 Feb 15 14:07 usr/bin/mkdir -> busybox
9f65cc
lrwxrwxrwx   1 root     root            7 Feb 15 14:07 usr/bin/modprobe -> busybox
9f65cc
lrwxrwxrwx   1 root     root            7 Feb 15 14:07 usr/bin/mount -> busybox
9f65cc
lrwxrwxrwx   1 root     root            7 Feb 15 14:07 usr/bin/sh -> busybox
9f65cc
lrwxrwxrwx   1 root     root            7 Feb 15 14:07 usr/bin/switch_root -> busybox
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 usr/lib
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 usr/lib/dracut
9f65cc
-rw-r--r--   1 root     root           23 Feb 15 14:07 usr/lib/dracut/build-parameter.txt
9f65cc
-rw-r--r--   1 root     root           31 Feb 15 14:07 usr/lib/dracut/dracut-051-93.git20210215.fc33
9f65cc
-rw-r--r--   1 root     root          358 Feb 15 14:07 usr/lib/dracut/modules.txt
9f65cc
-rw-r--r--   1 root     root            0 Feb 15 14:07 usr/lib/dracut/need-initqueue
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 usr/lib/modules
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 usr/lib/modules/5.10.11-200.fc33.x86_64
9f65cc
drwxr-xr-x   1 root     root            0 Feb 15 14:07 usr/lib/modules/5.10.11-200.fc33.x86_64/kernel
9f65cc
<... kernel module misc files skipped ... >
9f65cc
========================================================================
9f65cc
9f65cc
(cherry picked from commit 90f269f6afe409925bad86f0bd7e9322ad9b4fb0)
9f65cc
9f65cc
Resolves: #1959336
9f65cc
---
9f65cc
 modules.d/99squash/module-setup.sh | 13 ++++++++++++-
9f65cc
 1 file changed, 12 insertions(+), 1 deletion(-)
9f65cc
9f65cc
diff --git a/modules.d/99squash/module-setup.sh b/modules.d/99squash/module-setup.sh
9f65cc
index 50c92c31..72cc83ad 100644
9f65cc
--- a/modules.d/99squash/module-setup.sh
9f65cc
+++ b/modules.d/99squash/module-setup.sh
9f65cc
@@ -19,6 +19,9 @@ depends() {
9f65cc
 }
9f65cc
 
9f65cc
 installpost() {
9f65cc
+    local _busybox
9f65cc
+    _busybox=$(find_binary busybox)
9f65cc
+
9f65cc
     # Move everything under $initdir except $squash_dir
9f65cc
     # itself into squash image
9f65cc
     for i in "$initdir"/*; do
9f65cc
@@ -37,7 +40,15 @@ installpost() {
9f65cc
     done
9f65cc
 
9f65cc
     # Install required modules and binaries for the squash image init script.
9f65cc
-    DRACUT_RESOLVE_DEPS=1 inst_multiple sh mount modprobe mkdir switch_root
9f65cc
+    if [[ $_busybox ]]; then
9f65cc
+        inst "$_busybox" /usr/bin/busybox
9f65cc
+        for _i in sh echo mount modprobe mkdir switch_root; do
9f65cc
+            ln_r /usr/bin/busybox /usr/bin/$_i
9f65cc
+        done
9f65cc
+    else
9f65cc
+        DRACUT_RESOLVE_DEPS=1 inst_multiple sh mount modprobe mkdir switch_root
9f65cc
+    fi
9f65cc
+
9f65cc
     hostonly="" instmods "loop" "squashfs" "overlay"
9f65cc
     dracut_kernel_post
9f65cc
 
9f65cc