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