From 39a765debe212407cce28e6d3a84a65e4efc1c6e Mon Sep 17 00:00:00 2001 From: Antonio Alvarez Feijoo Date: Thu, 13 Mar 2025 17:03:21 +0100 Subject: [PATCH] fix(dracut): protect existing output file against build errors If dracut fails to build the initrd image or EFI binary for any reason (e.g., if `cp` fails because there is no space left on the device), it removes the existing output file before exiting, which may result in an unbootable system. Instead of copying the initrd image directly to the output, copy it alongside it to the same output directory, and if the copy succeeds, replace it. --- dracut.sh | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/dracut.sh b/dracut.sh index 4cae0cdd9..2e1cc1050 100755 --- a/dracut.sh +++ b/dracut.sh @@ -2373,6 +2373,12 @@ if dracut_module_included "squash-lib"; then compress="cat" fi +# protect existing output file against build errors +if [[ -e $outfile ]]; then + outfile_final="$outfile" + outfile="${outfile}.tmp" +fi + dinfo "*** Creating image file '$outfile' ***" if [[ $uefi == yes ]]; then @@ -2708,6 +2714,18 @@ else fi fi +if [[ $outfile_final ]]; then + dinfo "*** Moving image file '$outfile' to '$outfile_final' ***" + if mv -f "$outfile" "$outfile_final"; then + dinfo "*** Moving image file '$outfile' to '$outfile_final' done ***" + outfile="$outfile_final" + else + rm -f -- "$outfile_final" + dfatal "Move of $outfile_final failed" + exit 1 + fi +fi + btrfs_uuid() { btrfs filesystem show "$1" | sed -n '1s/^.*uuid: //p' } -- 2.47.3