From 57825c42663901f3c4f196943a2f7ed7288e289c Mon Sep 17 00:00:00 2001
From: Harald Hoyer <harald@redhat.com>
Date: Thu, 8 Dec 2016 17:53:40 +0100
Subject: [PATCH] dracut.sh: add default path for --uefi

The default output filename for --uefi is
<EFI>/EFI/Linux/linux-$kernel$-<MACHINE_ID>-<BUILD_ID>.efi.
<EFI> might be /efi, /boot or /boot/efi depending on where the ESP partition
is mounted. The <BUILD_ID> is taken from BUILD_ID in /usr/lib/os-release or
if it exists /etc/os-release and is left out, if BUILD_ID is non-existant or
empty.

Also a new option --no-machineid was added, which affects the default output
filename of --uefi and will discard the <MACHINE_ID> part.
---
 dracut.8.asc | 10 +++++++++-
 dracut.sh    | 43 +++++++++++++++++++++++++++++++++----------
 2 files changed, 42 insertions(+), 11 deletions(-)

diff --git a/dracut.8.asc b/dracut.8.asc
index 0e017070..4ab0872f 100644
--- a/dracut.8.asc
+++ b/dracut.8.asc
@@ -485,7 +485,15 @@ will not be able to boot.
 
 **--uefi**::
     Instead of creating an initramfs image, dracut will create an UEFI executable,
-    which can be executed by an UEFI BIOS.
+    which can be executed by an UEFI BIOS. The default output filename is
+    _<EFI>/EFI/Linux/linux-$kernel$-<MACHINE_ID>-<BUILD_ID>.efi_. <EFI> might be
+    _/efi_, _/boot_ or _/boot/efi_ depending on where the ESP partition is mounted.
+    The <BUILD_ID> is taken from BUILD_ID in _/usr/lib/os-release_ or if it exists
+    _/etc/os-release_ and is left out, if BUILD_ID is non-existant or empty.
+
+**--no-machineid**::
+    affects the default output filename of **--uefi** and will discard the <MACHINE_ID>
+    part.
 
 **--uefi-stub _<FILE>_**::
     Specifies the UEFI stub loader, which will load the attached kernel, initramfs and
diff --git a/dracut.sh b/dracut.sh
index 2d79bbcc..0886343f 100755
--- a/dracut.sh
+++ b/dracut.sh
@@ -371,6 +371,7 @@ rearrange_params()
         --long kernel-image: \
         --long no-hostonly-i18n \
         --long hostonly-i18n \
+        --long no-machineid \
         -- "$@")
 
     if (( $? != 0 )); then
@@ -566,6 +567,8 @@ while :; do
                        uefi_stub_l="$2";               PARMS_TO_STORE+=" '$2'"; shift;;
         --kernel-image)
                        kernel_image_l="$2";            PARMS_TO_STORE+=" '$2'"; shift;;
+        --no-machineid)
+                       machine_id_l="no";;
         --) shift; break;;
 
         *)  # should not even reach this point
@@ -624,16 +627,6 @@ if [[ $kernel ]]; then
     fi
 fi
 
-if ! [[ $outfile ]]; then
-    [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
-
-    if [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
-        outfile="/boot/${MACHINE_ID}/$kernel/initrd"
-    else
-        outfile="/boot/initramfs-$kernel.img"
-    fi
-fi
-
 unset LC_MESSAGES
 unset LC_CTYPE
 export LC_ALL=C
@@ -751,6 +744,36 @@ stdloglvl=$((stdloglvl + verbosity_mod_l))
 [[ $loginstall_l ]] && loginstall="$loginstall_l"
 [[ $uefi_stub_l ]] && uefi_stub="$uefi_stub_l"
 [[ $kernel_image_l ]] && kernel_image="$kernel_image_l"
+[[ $machine_id_l ]] && machine_id="$machine_id_l"
+
+if ! [[ $outfile ]]; then
+    if [[ $machine_id != "no" ]]; then
+        [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
+    fi
+
+    if [[ $uefi == "yes" ]]; then
+        BUILD_ID=$(cat /etc/os-release /usr/lib/os-release \
+                       | while read -r line || [[ $line ]]; do \
+                       [[ $line =~ BUILD_ID\=* ]] && eval "$line" && echo "$BUILD_ID" && break; \
+                   done)
+        if [[ -d /efi ]] && mountpoint -q /efi; then
+            efidir=/efi
+        else
+            efidir=/boot/EFI
+            if [[ -d /boot/efi/EFI ]] && mountpoint -q /boot/efi; then
+                efidir=/boot/efi/EFI
+            fi
+        fi
+        mkdir -p "$efidir/Linux"
+        outfile="$efidir/Linux/linux-$kernel${MACHINE_ID:+-${MACHINE_ID}}${BUILD_ID:+-${BUILD_ID}}.efi"
+    else
+        if [[ $MACHINE_ID ]] && ( [[ -d /boot/${MACHINE_ID} ]] || [[ -L /boot/${MACHINE_ID} ]] ); then
+            outfile="/boot/${MACHINE_ID}/$kernel/initrd"
+        else
+            outfile="/boot/initramfs-$kernel.img"
+        fi
+    fi
+fi
 
 # eliminate IFS hackery when messing with fw_dir
 export DRACUT_FIRMWARE_PATH=${fw_dir// /:}
