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