]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
fix(dracut): protect existing output file against build errors 694/head
authorAntonio Alvarez Feijoo <antonio.feijoo@suse.com>
Thu, 13 Mar 2025 16:03:21 +0000 (17:03 +0100)
committerNeal Gompa (ニール・ゴンパ) <ngompa13@gmail.com>
Sun, 20 Apr 2025 10:43:51 +0000 (06:43 -0400)
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

index 4cae0cdd95eac0092d9f91c464560dc426e82330..2e1cc105046ed12a6e947b2dd49acd535a9c6681 100755 (executable)
--- 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'
 }