]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
feat(dracut.sh): add --aggresive-strip option
authorKairui Song <kasong@tencent.com>
Fri, 7 Jan 2022 06:03:20 +0000 (14:03 +0800)
committerJóhann B. Guðmundsson <johannbg@gmail.com>
Wed, 2 Feb 2022 22:59:27 +0000 (22:59 +0000)
Dracut currently calls `eu-strip` or `strip` with -g, which only strips
out .debug_* sections. symtab and strtab are kept, but are not required
for runtime, and people will rarely need to do binary level debugging
work in initramfs.

So introduce a --aggresive-strip options, try strip out all sections
that are not required for runtime. This can help reduce the binary size
by a lot.

For example, the size of libc.so is reduced by a lot when stripped
with no option than with -g.

    3014184 libc-2.28.orig.so
    2970920 libc-2.28.strip-g.so
    1460904 libc-2.28.strip.so

Signed-off-by: Kairui Song <kasong@tencent.com>
dracut.sh

index 20556762c4c360b70d753dadefd8cbf5e27ff3be..58d51d715241b529d42561db14591fd62bdfc8e4 100755 (executable)
--- a/dracut.sh
+++ b/dracut.sh
@@ -111,6 +111,8 @@ Creates initial ramdisk images for preloading modules
   --no-early-microcode  Do not combine early microcode with ramdisk
   --kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
   --strip               Strip binaries in the initramfs
+  --aggresive-strip     Strip more than just debug symbol and sections,
+                        for a smaller initramfs build.
   --nostrip             Do not strip binaries in the initramfs
   --hardlink            Hardlink files in the initramfs
   --nohardlink          Do not hardlink files in the initramfs
@@ -379,6 +381,7 @@ rearrange_params() {
             --long print-cmdline \
             --long kernel-cmdline: \
             --long strip \
+            --long aggresive-strip \
             --long nostrip \
             --long hardlink \
             --long nohardlink \
@@ -697,6 +700,7 @@ while :; do
             early_microcode_l="no"
             ;;
         --strip) do_strip_l="yes" ;;
+        --aggresive-strip) aggresive_strip_l="yes" ;;
         --nostrip) do_strip_l="no" ;;
         --hardlink) do_hardlink_l="yes" ;;
         --nohardlink) do_hardlink_l="no" ;;
@@ -969,6 +973,7 @@ stdloglvl=$((stdloglvl + verbosity_mod_l))
 [[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
 [[ $do_strip_l ]] && do_strip=$do_strip_l
 [[ $do_strip ]] || do_strip=yes
+[[ $aggresive_strip_l ]] && aggresive_strip=$aggresive_strip_l
 [[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
 [[ $do_hardlink ]] || do_hardlink=yes
 [[ $prefix_l ]] && prefix=$prefix_l
@@ -2118,6 +2123,13 @@ if [[ $do_strip == yes ]]; then
             do_strip=no
         fi
     done
+
+    if [[ $aggresive_strip ]]; then
+        # `eu-strip` and `strip` both strips all unneeded parts by default
+        strip_args=(-p)
+    else
+        strip_args=(-g -p)
+    fi
 fi
 
 # cleanup empty ldconfig_paths directories
@@ -2263,14 +2275,14 @@ if [[ $do_strip == yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
     dinfo "*** Stripping files ***"
     find "$initdir" -type f \
         -executable -not -path '*/lib/modules/*.ko' -print0 \
-        | xargs -r -0 $strip_cmd -g -p 2> /dev/null
+        | xargs -r -0 $strip_cmd "${strip_args[@]}" 2> /dev/null
 
     # strip kernel modules, but do not touch signed modules
     find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
         | while read -r -d $'\0' f || [ -n "$f" ]; do
             SIG=$(tail -c 28 "$f" | tr -d '\000')
             [[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
-        done | xargs -r -0 $strip_cmd -g -p
+        done | xargs -r -0 $strip_cmd "${strip_args[@]}"
     dinfo "*** Stripping files done ***"
 fi