From: Antonio Alvarez Feijoo Date: Thu, 13 Mar 2025 16:03:21 +0000 (+0100) Subject: fix(dracut): protect existing output file against build errors X-Git-Tag: 107~20 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=refs%2Fpull%2F694%2Fhead;p=thirdparty%2Fdracut-ng.git 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. --- 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' }