Javier Martinez Canillas ef023b
From 5e574046e76eae2af5563f48b3ab910b3553ba56 Mon Sep 17 00:00:00 2001
Javier Martinez Canillas ef023b
From: Javier Martinez Canillas <javierm@redhat.com>
Javier Martinez Canillas ef023b
Date: Wed, 28 Feb 2018 09:39:12 +0100
Javier Martinez Canillas ef023b
Subject: [PATCH 1/1] 5?-dracut*.install: Allow scripts to install the
Javier Martinez Canillas ef023b
 initramfs in /boot dir
Javier Martinez Canillas ef023b
Javier Martinez Canillas ef023b
The GRUB 2 bootloaders expect the initrd to be installed in /boot instead
Javier Martinez Canillas ef023b
of /boot/$MACHINE_ID/$KERNEL_VERSION/{linux,initrd}, so if that directory
Javier Martinez Canillas ef023b
doesn't exists, install the initramfs images on the /boot directory.
Javier Martinez Canillas ef023b
Javier Martinez Canillas ef023b
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
Javier Martinez Canillas ef023b
---
Javier Martinez Canillas ef023b
 50-dracut.install        | 17 ++++++++++-----
Javier Martinez Canillas ef023b
 51-dracut-rescue.install | 55 ++++++++++++++++++++++++++++++++++--------------
Javier Martinez Canillas ef023b
 2 files changed, 51 insertions(+), 21 deletions(-)
Javier Martinez Canillas ef023b
Javier Martinez Canillas ef023b
diff --git a/50-dracut.install b/50-dracut.install
Javier Martinez Canillas ef023b
index f1314d0222c8..64e3549f2992 100755
Javier Martinez Canillas ef023b
--- a/50-dracut.install
Javier Martinez Canillas ef023b
+++ b/50-dracut.install
Javier Martinez Canillas ef023b
@@ -11,6 +11,13 @@ if ! [[ ${KERNEL_INSTALL_MACHINE_ID-x} ]]; then
Javier Martinez Canillas ef023b
     exit 0
Javier Martinez Canillas ef023b
 fi
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
+if [[ -d "$BOOT_DIR_ABS" ]]; then
Javier Martinez Canillas ef023b
+    INITRD="initrd"
Javier Martinez Canillas ef023b
+else
Javier Martinez Canillas ef023b
+    BOOT_DIR_ABS="/boot"
Javier Martinez Canillas ef023b
+    INITRD="initramfs-${KERNEL_VERSION}.img"
Javier Martinez Canillas ef023b
+fi
Javier Martinez Canillas ef023b
+
Javier Martinez Canillas ef023b
 ret=0
Javier Martinez Canillas ef023b
 case "$COMMAND" in
Javier Martinez Canillas ef023b
     add)
Javier Martinez Canillas ef023b
@@ -18,9 +25,9 @@ case "$COMMAND" in
Javier Martinez Canillas ef023b
         if [[ -f ${INITRD_IMAGE_PREGENERATED} ]]; then
Javier Martinez Canillas ef023b
             # we found an initrd at the same place as the kernel
Javier Martinez Canillas ef023b
             # use this and don't generate a new one
Javier Martinez Canillas ef023b
-            cp --reflink=auto "$INITRD_IMAGE_PREGENERATED" "$BOOT_DIR_ABS/initrd" \
Javier Martinez Canillas ef023b
-                && chown root:root "$BOOT_DIR_ABS/initrd" \
Javier Martinez Canillas ef023b
-                && chmod 0600 "$BOOT_DIR_ABS/initrd" \
Javier Martinez Canillas ef023b
+            cp --reflink=auto "$INITRD_IMAGE_PREGENERATED" "$BOOT_DIR_ABS/$INITRD" \
Javier Martinez Canillas ef023b
+                && chown root:root "$BOOT_DIR_ABS/$INITRD" \
Javier Martinez Canillas ef023b
+                && chmod 0600 "$BOOT_DIR_ABS/$INITRD" \
Javier Martinez Canillas ef023b
                 && exit 0
Javier Martinez Canillas ef023b
         fi
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
@@ -42,11 +49,11 @@ case "$COMMAND" in
Javier Martinez Canillas ef023b
                 break
Javier Martinez Canillas ef023b
             fi
Javier Martinez Canillas ef023b
         done
Javier Martinez Canillas ef023b
-	dracut ${noimageifnotneeded:+--noimageifnotneeded} "$BOOT_DIR_ABS"/initrd "$KERNEL_VERSION"
Javier Martinez Canillas ef023b
+	dracut ${noimageifnotneeded:+--noimageifnotneeded} "$BOOT_DIR_ABS/$INITRD" "$KERNEL_VERSION"
Javier Martinez Canillas ef023b
         ret=$?
Javier Martinez Canillas ef023b
 	;;
Javier Martinez Canillas ef023b
     remove)
Javier Martinez Canillas ef023b
-        rm -f -- "$BOOT_DIR_ABS"/initrd
Javier Martinez Canillas ef023b
+        rm -f -- "$BOOT_DIR_ABS/$INITRD"
Javier Martinez Canillas ef023b
         ret=$?
Javier Martinez Canillas ef023b
 	;;
Javier Martinez Canillas ef023b
 esac
Javier Martinez Canillas ef023b
diff --git a/51-dracut-rescue.install b/51-dracut-rescue.install
Javier Martinez Canillas ef023b
index 7fbeac1d4ce6..3f07a698f2f9 100755
Javier Martinez Canillas ef023b
--- a/51-dracut-rescue.install
Javier Martinez Canillas ef023b
+++ b/51-dracut-rescue.install
Javier Martinez Canillas ef023b
@@ -63,15 +63,30 @@ if ! [[ ${BOOT_OPTIONS[*]} ]]; then
Javier Martinez Canillas ef023b
     exit 1
Javier Martinez Canillas ef023b
 fi
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
-BOOT_DIR="/${MACHINE_ID}/0-rescue"
Javier Martinez Canillas ef023b
-BOOT_ROOT=${BOOT_DIR_ABS%$BOOT_DIR}
Javier Martinez Canillas ef023b
-LOADER_ENTRY="$BOOT_ROOT/loader/entries/${MACHINE_ID}-0-rescue.conf"
Javier Martinez Canillas ef023b
+if [[ -d "$BOOT_DIR_ABS" ]]; then
Javier Martinez Canillas ef023b
+    BOOT_DIR="/${MACHINE_ID}/0-rescue"
Javier Martinez Canillas ef023b
+    BOOT_ROOT=${BOOT_DIR_ABS%$BOOT_DIR}
Javier Martinez Canillas ef023b
+    LOADER_ENTRY="$BOOT_ROOT/loader/entries/${MACHINE_ID}-0-rescue.conf"
Javier Martinez Canillas ef023b
+    KERNEL="linux"
Javier Martinez Canillas ef023b
+    INITRD="initrd"
Javier Martinez Canillas ef023b
+else
Javier Martinez Canillas ef023b
+    BLS_DIR="/boot/loader/entries"
Javier Martinez Canillas ef023b
+    if [ -d /sys/firmware/efi ]; then
Javier Martinez Canillas ef023b
+	BLS_DIR="/boot/efi/EFI/${ID}/loader/entries"
Javier Martinez Canillas ef023b
+    fi
Javier Martinez Canillas ef023b
+
Javier Martinez Canillas ef023b
+    BOOT_DIR_ABS="/boot"
Javier Martinez Canillas ef023b
+    LOADER_ENTRY="$BLS_DIR/${MACHINE_ID}-0-rescue.conf"
Javier Martinez Canillas ef023b
+    KERNEL="vmlinuz-0-rescue-${MACHINE_ID}"
Javier Martinez Canillas ef023b
+    INITRD="initramfs-0-rescue-${MACHINE_ID}.img"
Javier Martinez Canillas ef023b
+fi
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
 ret=0
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
 case "$COMMAND" in
Javier Martinez Canillas ef023b
     add)
Javier Martinez Canillas ef023b
-        [[ -f "$LOADER_ENTRY" ]] && exit 0
Javier Martinez Canillas ef023b
+        [[ -f "$LOADER_ENTRY" ]] && [[ -f "$BOOT_DIR_ABS/$KERNEL" ]] \
Javier Martinez Canillas ef023b
+            && [[ -f "$BOOT_DIR_ABS/$INITRD" ]] && exit 0
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
         # source our config dir
Javier Martinez Canillas ef023b
         for f in $(dropindirs_sort ".conf" "/etc/dracut.conf.d" "/usr/lib/dracut/dracut.conf.d"); do
Javier Martinez Canillas ef023b
@@ -82,21 +97,29 @@ case "$COMMAND" in
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
         [[ -d "$BOOT_DIR_ABS" ]] || mkdir -p "$BOOT_DIR_ABS"
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
-        if ! cp --reflink=auto "$KERNEL_IMAGE" "$BOOT_DIR_ABS"/linux; then
Javier Martinez Canillas ef023b
-            echo "Can't copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'!" >&2
Javier Martinez Canillas ef023b
+        if ! cp --reflink=auto "$KERNEL_IMAGE" "$BOOT_DIR_ABS/$KERNEL"; then
Javier Martinez Canillas ef023b
+            echo "Can't copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/$KERNEL'!" >&2
Javier Martinez Canillas ef023b
         fi
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
-        dracut --no-hostonly -a "rescue" "$BOOT_DIR_ABS"/initrd "$KERNEL_VERSION"
Javier Martinez Canillas ef023b
-        ((ret+=$?))
Javier Martinez Canillas ef023b
+        if [[ ! -f "$BOOT_DIR_ABS/$INITRD" ]]; then
Javier Martinez Canillas ef023b
+            dracut --no-hostonly -a "rescue" "$BOOT_DIR_ABS/$INITRD" "$KERNEL_VERSION"
Javier Martinez Canillas ef023b
+            ((ret+=$?))
Javier Martinez Canillas ef023b
+        fi
Javier Martinez Canillas ef023b
+
Javier Martinez Canillas ef023b
+        if [[ -d "$BOOT_DIR" ]]; then
Javier Martinez Canillas ef023b
+            {
Javier Martinez Canillas ef023b
+                echo "title      $PRETTY_NAME - Rescue Image"
Javier Martinez Canillas ef023b
+                echo "version    $KERNEL_VERSION"
Javier Martinez Canillas ef023b
+                echo "machine-id $MACHINE_ID"
Javier Martinez Canillas ef023b
+                echo "options    ${BOOT_OPTIONS[@]} rd.auto=1"
Javier Martinez Canillas ef023b
+                echo "linux      $BOOT_DIR/linux"
Javier Martinez Canillas ef023b
+                echo "initrd     $BOOT_DIR/initrd"
Javier Martinez Canillas ef023b
+            } > $LOADER_ENTRY
Javier Martinez Canillas ef023b
+        else
Javier Martinez Canillas ef023b
+            cp -aT "${KERNEL_IMAGE%/*}/bls.conf" $LOADER_ENTRY
Javier Martinez Canillas ef023b
+            sed -i 's/'$KERNEL_VERSION'/0-rescue-'${MACHINE_ID}'/' $LOADER_ENTRY
Javier Martinez Canillas ef023b
+        fi
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
-        {
Javier Martinez Canillas ef023b
-            echo "title      $PRETTY_NAME - Rescue Image"
Javier Martinez Canillas ef023b
-            echo "version    $KERNEL_VERSION"
Javier Martinez Canillas ef023b
-            echo "machine-id $MACHINE_ID"
Javier Martinez Canillas ef023b
-            echo "options    ${BOOT_OPTIONS[@]} rd.auto=1"
Javier Martinez Canillas ef023b
-            echo "linux      $BOOT_DIR/linux"
Javier Martinez Canillas ef023b
-            echo "initrd     $BOOT_DIR/initrd"
Javier Martinez Canillas ef023b
-        } > $LOADER_ENTRY
Javier Martinez Canillas ef023b
         ((ret+=$?))
Javier Martinez Canillas ef023b
         ;;
Javier Martinez Canillas ef023b
 
Javier Martinez Canillas ef023b
-- 
Javier Martinez Canillas ef023b
2.14.3
Javier Martinez Canillas ef023b