Skip to content

Commit

Permalink
refactor(99squash): gather code into the squash module
Browse files Browse the repository at this point in the history
Move squash-module-specific code from dracut.sh to
/99squash/module-setup.sh.
Use dracut's new module_postprocess() feature.

Make shell installation explicit, preferring busybox, if available.

Clarify a comment about the dracut directory files.

Also, make new shellcheck adjustments.
  • Loading branch information
FGrose committed Jul 29, 2023
1 parent c647a71 commit eeae801
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 44 deletions.
37 changes: 1 addition & 36 deletions dracut.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2243,14 +2243,6 @@ for _d_mod in $mods_to_postprocess; do
}
done
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"
Expand All @@ -2270,36 +2262,9 @@ fi
for _d_mod in $mods_to_postprocess; do
_d_mod=${_d_mod%%@*}
module_postprocess "${_d_mod%:*}" "${_d_mod#*:}"
squash_compress=$squash_compress module_postprocess "${_d_mod%:*}" "${_d_mod#*:}"
done
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"
dinfo "*** Squashing the files inside the initramfs done ***"
# Skip initramfs compress
compress="cat"
fi
dinfo "*** Creating image file '$outfile' ***"
if [[ $uefi == yes ]]; then
Expand Down
60 changes: 52 additions & 8 deletions modules.d/99squash/module-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,28 @@ check() {
}

depends() {

if find_binary busybox &> /dev/null; then
echo "busybox"
fi
echo "systemd-initrd"
return 0
}

install() {

# Enroll module for postprocessing.
# shellcheck disable=SC2154
mods_to_postprocess+=" squash:$moddir@installpost@ "

}

installpost() {
local _busybox
_busybox=$(find_binary busybox)
# shellcheck disable=SC2154
readonly squash_dir="$initdir/squash/root"
readonly squash_img="$initdir/squash-root.img"
mkdir -p "$squash_dir"
dinfo "*** Install squash loader ***"

# Move everything under $initdir except $squash_dir
# itself into squash image
Expand All @@ -26,16 +41,16 @@ installpost() {
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
# Copy /dracut/ directory files out of the squash image directory
# 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

# Install required modules and binaries for the squash image init script.
if [[ $_busybox ]]; then
inst "$_busybox" /usr/bin/busybox
if find_binary busybox; then
inst busybox /usr/bin/busybox
for _i in sh echo mount modprobe mkdir switch_root grep umount; do
ln_r /usr/bin/busybox /usr/bin/$_i
done
Expand All @@ -61,8 +76,37 @@ installpost() {
build_ld_cache
}

install() {
if [[ $DRACUT_SQUASH_POST_INST ]]; then
postprocess() {

# shellcheck disable=SC2154
[[ $action == installpost ]] && {
installpost
return 0
}

dinfo "*** Squashing the files inside the initramfs ***"
declare squash_compress_arg
if [[ $squash_compress ]]; then
# shellcheck disable=SC2086
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', falling 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"
dinfo "*** Squashing the files inside the initramfs done ***"

# Skip initramfs compress
export compress="cat"

}

0 comments on commit eeae801

Please sign in to comment.