Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cpio): improve initramfs image performance and efficiency via cpio reflinks #1531

Merged
merged 8 commits into from
Nov 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ ifeq ($(HAVE_SHFMT),yes)
shfmt -w -s .
endif

src/dracut-cpio/target/release/dracut-cpio: src/dracut-cpio/src/main.rs
cargo --offline build --release --manifest-path src/dracut-cpio/Cargo.toml

dracut-cpio: src/dracut-cpio/target/release/dracut-cpio
ln -fs $< $@

ifeq ($(enable_dracut_cpio),yes)
all: dracut-cpio
endif

doc: $(manpages) dracut.html

ifneq ($(enable_documentation),no)
Expand Down Expand Up @@ -196,6 +206,9 @@ endif
if [ -f dracut-util ]; then \
install -m 0755 dracut-util $(DESTDIR)$(pkglibdir)/dracut-util; \
fi
ifeq ($(enable_dracut_cpio),yes)
install -m 0755 dracut-cpio $(DESTDIR)$(pkglibdir)/dracut-cpio
endif
mkdir -p $(DESTDIR)${prefix}/lib/kernel/install.d
install -m 0755 install.d/50-dracut.install $(DESTDIR)${prefix}/lib/kernel/install.d/50-dracut.install
install -m 0755 install.d/51-dracut-rescue.install $(DESTDIR)${prefix}/lib/kernel/install.d/51-dracut-rescue.install
Expand All @@ -222,6 +235,7 @@ clean:
$(RM) dracut-util util/util $(UTIL_OBJECTS)
$(RM) $(manpages) dracut.html
$(RM) dracut.pc
$(RM) dracut-cpio src/dracut-cpio/target/release/dracut-cpio*
$(MAKE) -C test clean

dist: dracut-$(DRACUT_MAIN_VERSION).tar.xz
Expand Down
12 changes: 12 additions & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ echo \#buildapi-variable-no-builddir > /dev/null
prefix=/usr

enable_documentation=yes
enable_dracut_cpio=no

CC="${CC:-cc}"
PKG_CONFIG="${PKG_CONFIG:-pkg-config}"
Expand Down Expand Up @@ -48,6 +49,7 @@ while (($# > 0)); do
--infodir) read_arg infodir "$@" || shift ;;
--systemdsystemunitdir) read_arg systemdsystemunitdir "$@" || shift ;;
--bashcompletiondir) read_arg bashcompletiondir "$@" || shift ;;
--enable-dracut-cpio) enable_dracut_cpio=yes ;;
*) echo "Ignoring unknown option '$1'" ;;
esac
shift
Expand Down Expand Up @@ -102,6 +104,15 @@ if test $found = no; then
exit 1
fi

if test "$enable_dracut_cpio" = "yes"; then
cargo --version > /dev/null
ret=$?
if test $ret -ne 0; then
echo "dracut couldn't find cargo for dracut-cpio build"
exit 1
fi
fi

cat > Makefile.inc.$$ << EOF
prefix ?= ${prefix}
libdir ?= ${libdir:-${prefix}/lib}
Expand All @@ -110,6 +121,7 @@ sysconfdir ?= ${sysconfdir:-${prefix}/etc}
sbindir ?= ${sbindir:-${prefix}/sbin}
mandir ?= ${mandir:-${prefix}/share/man}
enable_documentation ?= ${enable_documentation:-yes}
enable_dracut_cpio ?= ${enable_dracut_cpio}
ddiss marked this conversation as resolved.
Show resolved Hide resolved
bindir ?= ${bindir:-${prefix}/bin}
KMOD_CFLAGS ?= $(${PKG_CONFIG} --cflags " libkmod >= 23 ")
KMOD_LIBS ?= $(${PKG_CONFIG} --libs " libkmod >= 23 ")
Expand Down
95 changes: 77 additions & 18 deletions dracut.sh
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Creates initial ramdisk images for preloading modules
otherwise you will not be able to boot.
--no-compress Do not compress the generated initramfs. This will
override any other compression options.
--enhanced-cpio Attempt to reflink cpio file data using dracut-cpio.
--list-modules List all available dracut modules.
-M, --show-modules Print included module's name to standard output during
build.
Expand Down Expand Up @@ -412,6 +413,7 @@ rearrange_params() {
--long zstd \
--long no-compress \
--long gzip \
--long enhanced-cpio \
--long list-modules \
--long show-modules \
--long keep \
Expand Down Expand Up @@ -770,6 +772,7 @@ while :; do
--zstd) compress_l="zstd" ;;
--no-compress) _no_compress_l="cat" ;;
--gzip) compress_l="gzip" ;;
--enhanced-cpio) enhanced_cpio_l="yes" ;;
--list-modules) do_list="yes" ;;
-M | --show-modules)
show_modules_l="yes"
Expand Down Expand Up @@ -982,6 +985,7 @@ stdloglvl=$((stdloglvl + verbosity_mod_l))
[[ $tmpdir ]] || tmpdir="$dracutsysrootdir"/var/tmp
[[ $INITRD_COMPRESS ]] && compress=$INITRD_COMPRESS
[[ $compress_l ]] && compress=$compress_l
[[ $enhanced_cpio_l ]] && enhanced_cpio=$enhanced_cpio_l
[[ $show_modules_l ]] && show_modules=$show_modules_l
[[ $nofscks_l ]] && nofscks="yes"
[[ $ro_mnt_l ]] && ro_mnt="yes"
Expand Down Expand Up @@ -1188,6 +1192,19 @@ else
exit 1
fi

if [[ $enhanced_cpio == "yes" ]]; then
enhanced_cpio="$dracutbasedir/dracut-cpio"
if [[ -x $enhanced_cpio ]]; then
# align based on statfs optimal transfer size
cpio_align=$(stat --file-system -c "%s" -- "$initdir")
else
dinfo "--enhanced-cpio ignored due to lack of dracut-cpio"
unset enhanced_cpio
fi
else
unset enhanced_cpio
fi

# shellcheck disable=SC2154
if [[ $no_kernel != yes ]] && ! [[ -d $srcmods ]]; then
printf "%s\n" "dracut: Cannot find module directory $srcmods" >&2
Expand Down Expand Up @@ -2252,6 +2269,8 @@ if dracut_module_included "squash"; then
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"
dinfo "*** Stripping files ***"
find "$initdir" -type f \
-executable -not -path '*/lib/modules/*.ko' -print0 \
Expand Down Expand Up @@ -2322,15 +2341,29 @@ if [[ $create_early_cpio == yes ]]; then
fi

# The microcode blob is _before_ the initramfs blob, not after
if ! (
umask 077
cd "$early_cpio_dir/d"
find . -print0 | sort -z \
| cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null \
${cpio_owner:+-R "$cpio_owner"} -H newc -o --quiet > "${DRACUT_TMPDIR}/initramfs.img"
); then
dfatal "dracut: creation of $outfile failed"
exit 1
if [[ -n $enhanced_cpio ]]; then
if ! (
umask 077
cd "$early_cpio_dir/d"
find . -print0 | sort -z \
| $enhanced_cpio --null ${cpio_owner:+--owner "$cpio_owner"} \
--mtime 0 --data-align "$cpio_align" --truncate-existing \
"${DRACUT_TMPDIR}/initramfs.img"
); then
dfatal "dracut-cpio: creation of $outfile failed"
exit 1
fi
else
if ! (
umask 077
cd "$early_cpio_dir/d"
find . -print0 | sort -z \
| cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null \
${cpio_owner:+-R "$cpio_owner"} -H newc -o --quiet > "${DRACUT_TMPDIR}/initramfs.img"
); then
dfatal "dracut: creation of $outfile failed"
exit 1
fi
fi
fi

Expand Down Expand Up @@ -2386,15 +2419,41 @@ if [[ $compress == $DRACUT_COMPRESS_ZSTD* ]] && ! check_kernel_config CONFIG_RD_
compress="cat"
fi

if ! (
umask 077
cd "$initdir"
find . -print0 | sort -z \
| cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null ${cpio_owner:+-R "$cpio_owner"} -H newc -o --quiet \
| $compress >> "${DRACUT_TMPDIR}/initramfs.img"
); then
dfatal "dracut: creation of $outfile failed"
exit 1
if [[ -n $enhanced_cpio ]]; then
if [[ $compress == "cat" ]]; then
# dracut-cpio appends by default, so any ucode remains
cpio_outfile="${DRACUT_TMPDIR}/initramfs.img"
else
ddebug "$compress compression enabled alongside cpio reflink"
# dracut-cpio doesn't output to stdout, so stage for compression
cpio_outfile="${DRACUT_TMPDIR}/initramfs.img.uncompressed"
fi

if ! (
umask 077
cd "$initdir"
find . -print0 | sort -z \
| $enhanced_cpio --null ${cpio_owner:+--owner "$cpio_owner"} \
--mtime 0 --data-align "$cpio_align" "$cpio_outfile" || exit 1
[[ $compress == "cat" ]] && exit 0
$compress < "$cpio_outfile" >> "${DRACUT_TMPDIR}/initramfs.img" \
&& rm "$cpio_outfile"
); then
dfatal "dracut-cpio: creation of $outfile failed"
exit 1
fi
unset cpio_outfile
else
if ! (
umask 077
cd "$initdir"
find . -print0 | sort -z \
| cpio ${CPIO_REPRODUCIBLE:+--reproducible} --null ${cpio_owner:+-R "$cpio_owner"} -H newc -o --quiet \
| $compress >> "${DRACUT_TMPDIR}/initramfs.img"
); then
dfatal "dracut: creation of $outfile failed"
exit 1
fi
fi

# shellcheck disable=SC2154
Expand Down
9 changes: 9 additions & 0 deletions man/dracut.8.asc
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,15 @@ will not be able to boot.
Specifies the kernel image, which to include in the UEFI executable. The default is
_/lib/modules/<KERNEL-VERSION>/vmlinuz_ or _/boot/vmlinuz-<KERNEL-VERSION>_

**--enhanced-cpio**::
Attempt to use the dracut-cpio binary, which optimizes archive creation for
copy-on-write filesystems by using the copy_file_range(2) syscall via Rust's
ddiss marked this conversation as resolved.
Show resolved Hide resolved
io::copy(). When specified, initramfs archives are also padded to ensure
optimal data alignment for extent sharing. To retain reflink data
deduplication benefits, this should be used alongside the **--no-compress**
and **--no-strip** parameters, with initramfs source files, **--tmpdir**
staging area and destination all on the same copy-on-write capable filesystem.

ENVIRONMENT
-----------

Expand Down
3 changes: 2 additions & 1 deletion shell-completion/bash/dracut
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ _dracut() {
--local --hostonly --no-hostonly --fstab --help --bzip2 --lzma
--xz --zstd --no-compress --gzip --list-modules --show-modules --keep
--printsize --regenerate-all --noimageifnotneeded --early-microcode
--no-early-microcode --print-cmdline --reproducible --uefi'
--no-early-microcode --print-cmdline --reproducible --uefi
--enhanced-cpio'
[ARG]='-a -m -o -d -I -k -c -L --kver --add --force-add --add-drivers
--omit-drivers --modules --omit --drivers --filesystems --install
--fwdir --libdirs --fscks --add-fstab --mount --device --nofscks
Expand Down
12 changes: 12 additions & 0 deletions src/dracut-cpio/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions src/dracut-cpio/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "dracut-cpio"
description = "cpio archive generator for Dracut"
authors = ["David Disseldorp"]
license = "GPL-2.0"
version = "0.1.0"
edition = "2018"

[dependencies]
crosvm = { path = "third_party/crosvm" }
# please avoid adding any more dependencies