From 2d851d7d1709f5a03d8dab847aa42770bff2644b Mon Sep 17 00:00:00 2001 From: Philipp Rudo Date: Mon, 22 Jul 2024 16:30:50 +0200 Subject: [PATCH 14/32] feat(squash): move mksquashfs to 99squash/modules-setup When using 99squash dracut actually builds two separat initrds. The "normal" one, that gets squashed into a squashfs image, and a "minimalistic" one, whose only task is to mount and switch_root to the squashfs image. For that 99squash currently requires a lot of special handling in dracut.sh. Move most of this special handling into 99squash itself. This requires a new approach when building the "minimalistic" initrd. The new approach works the following way 1. During the installation phase install the "normal" initrd into $initdir and the "minimalistic" initrd into $squashdir. 2. Strip the binaries in $initdir. 3. Trigger a special postinstall hook in 99squash that squashes the content of $initdir (excluding $squashdir) into the squashfs image, removes the content of $intidir (excluding $suqashdir) and, moves the content of $squashdir into $initdir. Signed-off-by: Philipp Rudo (cherry picked from commit 7a4dd89ca732329893628b886fe8e78337d896e8) Related: RHEL-43460 --- dracut.sh | 35 ++++-------------- modules.d/99squash/module-setup.sh | 58 +++++++++++++++++++++--------- 2 files changed, 49 insertions(+), 44 deletions(-) diff --git a/dracut.sh b/dracut.sh index 856b884e..4d2e3df2 100755 --- a/dracut.sh +++ b/dracut.sh @@ -1260,6 +1260,7 @@ trap ' trap 'exit 1;' SIGINT readonly initdir="${DRACUT_TMPDIR}/initramfs" +readonly squashdir="$initdir/squash_root" mkdir -p "$initdir" if [[ $early_microcode == yes ]] || { [[ $acpi_override == yes ]] && [[ -d $acpi_table_dir ]]; }; then @@ -1787,7 +1788,8 @@ export initdir dracutbasedir \ host_fs_types host_devs swap_devs sshkey add_fstab \ DRACUT_VERSION \ prefix filesystems drivers \ - hostonly_cmdline loginstall + hostonly_cmdline loginstall \ + squashdir squash_compress mods_to_load="" # check all our modules to see if they should be sourced. @@ -1892,6 +1894,8 @@ if [[ $kernel_only != yes ]]; then fi fi +dracut_module_included "squash" && mkdir -p "$squashdir" + _isize=0 #initramfs size modules_loaded=" " # source our modules. @@ -2243,14 +2247,6 @@ if [[ $kernel_only != yes ]]; then build_ld_cache fi -if dracut_module_included "squash"; then - readonly squash_dir="$initdir/squash/root" - readonly squash_img="$initdir/squash-root.img" - mkdir -p "$squash_dir" - dinfo "*** Install squash loader ***" - DRACUT_SQUASH_POST_INST=1 module_install "squash" -fi - if [[ $do_strip == yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then # stripping files negates (dedup) benefits of using reflink [[ -n $enhanced_cpio ]] && ddebug "strip is enabled alongside cpio reflink" @@ -2270,25 +2266,8 @@ fi if dracut_module_included "squash"; then dinfo "*** Squashing the files inside the initramfs ***" - declare squash_compress_arg - # shellcheck disable=SC2086 - if [[ $squash_compress ]]; then - if ! mksquashfs /dev/null "$DRACUT_TMPDIR"/.squash-test.img -no-progress -comp $squash_compress &> /dev/null; then - dwarn "mksquashfs doesn't support compressor '$squash_compress', failing back to default compressor." - else - squash_compress_arg="$squash_compress" - fi - fi - - # shellcheck disable=SC2086 - if ! mksquashfs "$squash_dir" "$squash_img" \ - -no-xattrs -no-exports -noappend -no-recovery -always-use-fragments \ - -no-progress ${squash_compress_arg:+-comp $squash_compress_arg} 1> /dev/null; then - dfatal "Failed making squash image" - exit 1 - fi - - rm -rf "$squash_dir" + DRACUT_SQUASH_POST_INST=1 module_install "squash" + rm -rf "$squashdir" dinfo "*** Squashing the files inside the initramfs done ***" # Skip initramfs compress diff --git a/modules.d/99squash/module-setup.sh b/modules.d/99squash/module-setup.sh index dc2e0a20..96d097af 100755 --- a/modules.d/99squash/module-setup.sh +++ b/modules.d/99squash/module-setup.sh @@ -12,26 +12,13 @@ depends() { return 0 } -installpost() { +squash_install() { local _busybox _busybox=$(find_binary busybox) - # Move everything under $initdir except $squash_dir - # itself into squash image - for i in "$initdir"/*; do - [[ $squash_dir == "$i"/* ]] || mv "$i" "$squash_dir"/ - done - # Create mount points for squash loader mkdir -p "$initdir"/squash/ - mkdir -p "$squash_dir"/squash/ - - # Copy dracut spec files out side of the squash image - # so dracut rebuild and lsinitrd can work - for file in "$squash_dir"/usr/lib/dracut/*; do - [[ -f $file ]] || continue - DRACUT_RESOLVE_DEPS=1 dracutsysrootdir="$squash_dir" inst "${file#"$squash_dir"}" - done + mkdir -p "$squashdir"/squash/ # Install required modules and binaries for the squash image init script. if [[ $_busybox ]]; then @@ -61,8 +48,47 @@ installpost() { build_ld_cache } +squash_installpost() { + local _img="$squashdir"/squash-root.img + local _comp _file + + # shellcheck disable=SC2086 + if [[ $squash_compress ]]; then + if ! mksquashfs /dev/null "$DRACUT_TMPDIR"/.squash-test.img -no-progress -comp $squash_compress &> /dev/null; then + dwarn "mksquashfs doesn't support compressor '$squash_compress', failing back to default compressor." + else + _comp="$squash_compress" + fi + fi + + # shellcheck disable=SC2086 + if ! mksquashfs "$initdir" "$_img" \ + -no-xattrs -no-exports -noappend -no-recovery -always-use-fragments \ + -no-progress ${_comp:+-comp $_comp} \ + -e "$squashdir" 1> /dev/null; then + dfatal "Failed making squash image" + exit 1 + fi + + # Rescue the dracut spec files so dracut rebuild and lsinitrd can work + for _file in "$initdir"/usr/lib/dracut/*; do + [[ -f $_file ]] || continue + DRACUT_RESOLVE_DEPS=1 dstdir=$squashdir inst "$_file" "${_file#"$initdir"}" + done + + # Remove everything that got squashed into the image + for _file in "$initdir"/*; do + [[ $_file == "$squashdir" ]] && continue + rm -rf "$_file" + done + mv "$squashdir"/* "$initdir" +} + install() { + if [[ $DRACUT_SQUASH_POST_INST ]]; then - installpost + squash_installpost + else + dstdir="$squashdir" squash_install fi } -- 2.42.0