Skip to content

Commit 67fc670

Browse files
ryncsnjohannbg
authored andcommitted
feat(dracut.sh): add --aggresive-strip option
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>
1 parent 22e6830 commit 67fc670

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

dracut.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ Creates initial ramdisk images for preloading modules
111111
--no-early-microcode Do not combine early microcode with ramdisk
112112
--kernel-cmdline [PARAMETERS] Specify default kernel command line parameters
113113
--strip Strip binaries in the initramfs
114+
--aggresive-strip Strip more than just debug symbol and sections,
115+
for a smaller initramfs build.
114116
--nostrip Do not strip binaries in the initramfs
115117
--hardlink Hardlink files in the initramfs
116118
--nohardlink Do not hardlink files in the initramfs
@@ -379,6 +381,7 @@ rearrange_params() {
379381
--long print-cmdline \
380382
--long kernel-cmdline: \
381383
--long strip \
384+
--long aggresive-strip \
382385
--long nostrip \
383386
--long hardlink \
384387
--long nohardlink \
@@ -697,6 +700,7 @@ while :; do
697700
early_microcode_l="no"
698701
;;
699702
--strip) do_strip_l="yes" ;;
703+
--aggresive-strip) aggresive_strip_l="yes" ;;
700704
--nostrip) do_strip_l="no" ;;
701705
--hardlink) do_hardlink_l="yes" ;;
702706
--nohardlink) do_hardlink_l="no" ;;
@@ -969,6 +973,7 @@ stdloglvl=$((stdloglvl + verbosity_mod_l))
969973
[[ $drivers_dir_l ]] && drivers_dir=$drivers_dir_l
970974
[[ $do_strip_l ]] && do_strip=$do_strip_l
971975
[[ $do_strip ]] || do_strip=yes
976+
[[ $aggresive_strip_l ]] && aggresive_strip=$aggresive_strip_l
972977
[[ $do_hardlink_l ]] && do_hardlink=$do_hardlink_l
973978
[[ $do_hardlink ]] || do_hardlink=yes
974979
[[ $prefix_l ]] && prefix=$prefix_l
@@ -2118,6 +2123,13 @@ if [[ $do_strip == yes ]]; then
21182123
do_strip=no
21192124
fi
21202125
done
2126+
2127+
if [[ $aggresive_strip ]]; then
2128+
# `eu-strip` and `strip` both strips all unneeded parts by default
2129+
strip_args=(-p)
2130+
else
2131+
strip_args=(-g -p)
2132+
fi
21212133
fi
21222134
21232135
# cleanup empty ldconfig_paths directories
@@ -2263,14 +2275,14 @@ if [[ $do_strip == yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
22632275
dinfo "*** Stripping files ***"
22642276
find "$initdir" -type f \
22652277
-executable -not -path '*/lib/modules/*.ko' -print0 \
2266-
| xargs -r -0 $strip_cmd -g -p 2> /dev/null
2278+
| xargs -r -0 $strip_cmd "${strip_args[@]}" 2> /dev/null
22672279
22682280
# strip kernel modules, but do not touch signed modules
22692281
find "$initdir" -type f -path '*/lib/modules/*.ko' -print0 \
22702282
| while read -r -d $'\0' f || [ -n "$f" ]; do
22712283
SIG=$(tail -c 28 "$f" | tr -d '\000')
22722284
[[ $SIG == '~Module signature appended~' ]] || { printf "%s\000" "$f"; }
2273-
done | xargs -r -0 $strip_cmd -g -p
2285+
done | xargs -r -0 $strip_cmd "${strip_args[@]}"
22742286
dinfo "*** Stripping files done ***"
22752287
fi
22762288

0 commit comments

Comments
 (0)