a0a3b4
From b4ad021d34975f25f50024e3601354f75593837f Mon Sep 17 00:00:00 2001
a0a3b4
From: Harald Hoyer <harald@redhat.com>
a0a3b4
Date: Wed, 29 Jun 2016 11:25:12 +0200
a0a3b4
Subject: [PATCH] dracut: only use one tmpdir
a0a3b4
a0a3b4
also remove any partial written initramfs images
a0a3b4
---
a0a3b4
 dracut-logger.sh |  3 +--
49e6a8
 dracut.sh        | 66 +++++++++++++++++++++++++++++++++---------------
a0a3b4
 2 files changed, 47 insertions(+), 22 deletions(-)
a0a3b4
a0a3b4
diff --git a/dracut-logger.sh b/dracut-logger.sh
1755ca
index ffa56167..53f74625 100755
a0a3b4
--- a/dracut-logger.sh
a0a3b4
+++ b/dracut-logger.sh
a0a3b4
@@ -150,8 +150,7 @@ dlog_init() {
a0a3b4
             && type -P systemd-cat &>/dev/null \
a0a3b4
             && systemctl --quiet is-active systemd-journald.socket &>/dev/null \
a0a3b4
             && { echo "dracut-$DRACUT_VERSION" | systemd-cat -t 'dracut' &>/dev/null; } ; then
a0a3b4
-            readonly _dlogdir="$(mktemp --tmpdir="$TMPDIR/" -d -t dracut-log.XXXXXX)"
a0a3b4
-            readonly _systemdcatfile="$_dlogdir/systemd-cat"
a0a3b4
+            readonly _systemdcatfile="$DRACUT_TMPDIR/systemd-cat"
a0a3b4
             mkfifo "$_systemdcatfile"
a0a3b4
             readonly _dlogfd=15
a0a3b4
             systemd-cat -t 'dracut' --level-prefix=true <"$_systemdcatfile" &
a0a3b4
diff --git a/dracut.sh b/dracut.sh
1755ca
index b85d42dd..78976792 100755
a0a3b4
--- a/dracut.sh
a0a3b4
+++ b/dracut.sh
a0a3b4
@@ -813,24 +813,25 @@ fi
a0a3b4
 [[ $hostonly != "-h" ]] && unset hostonly
a0a3b4
 
a0a3b4
 readonly TMPDIR="$tmpdir"
a0a3b4
-readonly initdir="$(mktemp --tmpdir="$TMPDIR/" -d -t initramfs.XXXXXX)"
a0a3b4
-[ -d "$initdir" ] || {
a0a3b4
-    printf "%s\n" "dracut: mktemp --tmpdir=\"$TMPDIR/\" -d -t initramfs.XXXXXX failed." >&2
a0a3b4
+readonly DRACUT_TMPDIR="$(mktemp -p "$TMPDIR/" -d -t dracut.XXXXXX)"
a0a3b4
+[ -d "$DRACUT_TMPDIR" ] || {
a0a3b4
+    printf "%s\n" "dracut: mktemp -p '$TMPDIR/' -d -t dracut.XXXXXX failed." >&2
a0a3b4
     exit 1
a0a3b4
 }
a0a3b4
 
a0a3b4
 # clean up after ourselves no matter how we die.
a0a3b4
 trap '
a0a3b4
     ret=$?;
a0a3b4
-    [[ $keep ]] && echo "Not removing $initdir." >&2 || { [[ $initdir ]] && rm -rf -- "$initdir"; };
a0a3b4
-    [[ $keep ]] && echo "Not removing $early_cpio_dir." >&2 || { [[ $early_cpio_dir ]] && rm -Rf -- "$early_cpio_dir"; };
a0a3b4
-    [[ $_dlogdir ]] && rm -Rf -- "$_dlogdir";
a0a3b4
+    [[ $keep ]] && echo "Not removing $DRACUT_TMPDIR." >&2 || { [[ $DRACUT_TMPDIR ]] && rm -rf -- "$DRACUT_TMPDIR"; };
a0a3b4
     exit $ret;
a0a3b4
     ' EXIT
a0a3b4
 
a0a3b4
 # clean up after ourselves no matter how we die.
a0a3b4
 trap 'exit 1;' SIGINT
a0a3b4
 
a0a3b4
+readonly initdir="${DRACUT_TMPDIR}/initramfs"
a0a3b4
+mkdir "$initdir"
a0a3b4
+
a0a3b4
 export DRACUT_KERNEL_LAZY="1"
a0a3b4
 export DRACUT_RESOLVE_LAZY="1"
a0a3b4
 
a0a3b4
@@ -878,20 +879,18 @@ case "$(arch)" in
a0a3b4
     i686|x86_64)
a0a3b4
         ;;
a0a3b4
     *)
a0a3b4
-        early_microcode=no
a0a3b4
-        dinfo "Disabling early microcode for $(arch)"
a0a3b4
+        if [[ $early_microcode = yes ]]; then
a0a3b4
+            early_microcode=no
a0a3b4
+            dinfo "Disabling early microcode for $(arch)"
a0a3b4
+        fi
a0a3b4
         ;;
a0a3b4
 esac
a0a3b4
 
a0a3b4
 if [[ $early_microcode = yes ]] || ( [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]] ); then
a0a3b4
-    readonly early_cpio_dir="$(mktemp --tmpdir="$TMPDIR/" -d -t early_cpio.XXXXXX)"
a0a3b4
-    [ -d "$early_cpio_dir" ] || {
a0a3b4
-        printf "%s\n" "dracut: mktemp --tmpdir=\"$TMPDIR/\" -d -t early_cpio.XXXXXX failed." >&2
a0a3b4
-        exit 1
a0a3b4
-    }
a0a3b4
+    readonly early_cpio_dir="${DRACUT_TMPDIR}/earlycpio"
a0a3b4
+    mkdir "$early_cpio_dir"
a0a3b4
 fi
a0a3b4
 
a0a3b4
-
a0a3b4
 if (( ${#drivers_l[@]} )); then
a0a3b4
     drivers=''
a0a3b4
     while pop drivers_l val; do
a0a3b4
@@ -1485,7 +1484,7 @@ if [[ $do_strip = yes ]] && ! [[ $DRACUT_FIPS_MODE ]]; then
a0a3b4
 fi
a0a3b4
 
a0a3b4
 if [[ $early_microcode = yes ]]; then
a0a3b4
-    dinfo "*** Generating early-microcode cpio image ***"
a0a3b4
+    dinfo "*** Generating early-microcode cpio image contents ***"
a0a3b4
     ucode_dir=(amd-ucode intel-ucode)
a0a3b4
     ucode_dest=(AuthenticAMD.bin GenuineIntel.bin)
a0a3b4
     _dest_dir="$early_cpio_dir/d/kernel/x86/microcode"
a0a3b4
@@ -1521,6 +1520,9 @@ if [[ $early_microcode = yes ]]; then
a0a3b4
             fi
a0a3b4
         done
a0a3b4
     done
a0a3b4
+    if ! [[ $create_early_cpio = yes ]]; then
a0a3b4
+        dinfo "*** No early-microcode cpio image needed ***"
a0a3b4
+    fi
a0a3b4
 fi
a0a3b4
 
a0a3b4
 if [[ $acpi_override = yes ]] && [[ -d $acpi_table_dir ]]; then
a0a3b4
@@ -1546,23 +1548,47 @@ dinfo "*** Creating image file ***"
a0a3b4
 [[ "$UID" != 0 ]] && cpio_owner_root="-R 0:0"
a0a3b4
 
a0a3b4
 if [[ $create_early_cpio = yes ]]; then
a0a3b4
+    dinfo "*** Creating microcode section ***"
a0a3b4
     echo 1 > "$early_cpio_dir/d/early_cpio"
a0a3b4
     # The microcode blob is _before_ the initramfs blob, not after
a0a3b4
-    (cd "$early_cpio_dir/d";     find . -print0 | cpio --null $cpio_owner_root -H newc -o --quiet > $outfile)
a0a3b4
+    if ! (
a0a3b4
+            umask 077
a0a3b4
+            cd "$early_cpio_dir/d"
a0a3b4
+            find . -print0 | cpio --null $cpio_owner_root -H newc -o --quiet > "${DRACUT_TMPDIR}/initramfs.img"
a0a3b4
+        ); then
a0a3b4
+        dfatal "dracut: creation of $outfile failed"
a0a3b4
+        exit 1
a0a3b4
+    else
a0a3b4
+        dinfo "*** Created microcode section ***"
a0a3b4
+    fi
a0a3b4
 fi
a0a3b4
-if ! ( umask 077; cd "$initdir"; find . -print0 | cpio --null $cpio_owner_root -H newc -o --quiet | \
a0a3b4
-    $compress >> "$outfile"; ); then
a0a3b4
+
a0a3b4
+if ! (
a0a3b4
+        umask 077
a0a3b4
+        cd "$initdir"
a0a3b4
+        find . -print0 | cpio --null $cpio_owner_root -H newc -o --quiet | \
a0a3b4
+            $compress >> "${DRACUT_TMPDIR}/initramfs.img";
a0a3b4
+    ); then
a0a3b4
     dfatal "dracut: creation of $outfile failed"
a0a3b4
     exit 1
a0a3b4
 fi
a0a3b4
+
a0a3b4
 dinfo "*** Creating image file done ***"
a0a3b4
 
a0a3b4
 if (( maxloglvl >= 5 )); then
a0a3b4
     if [[ $allowlocal ]]; then
a0a3b4
-	"$dracutbasedir/lsinitrd.sh" "$outfile"| ddebug
a0a3b4
+	"$dracutbasedir/lsinitrd.sh" "${DRACUT_TMPDIR}/initramfs.img" | ddebug
a0a3b4
     else
a0a3b4
-        lsinitrd "$outfile"| ddebug
a0a3b4
+        lsinitrd "${DRACUT_TMPDIR}/initramfs.img" | ddebug
a0a3b4
     fi
a0a3b4
 fi
a0a3b4
 
a0a3b4
+if cp --reflink=auto "${DRACUT_TMPDIR}/initramfs.img" "$outfile" |& derror ; then
a0a3b4
+    dinfo "*** Creating initramfs image file '$outfile' done ***"
a0a3b4
+else
a0a3b4
+    rm -f -- "$outfile"
a0a3b4
+    dfatal "dracut: creation of $outfile failed"
a0a3b4
+    exit 1
a0a3b4
+fi
a0a3b4
+
a0a3b4
 exit 0