]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
refactor: introduce find_initrd_for_kernel_version
authorBenjamin Drung <benjamin.drung@canonical.com>
Tue, 2 Jul 2024 20:14:40 +0000 (22:14 +0200)
committerLaszlo Gombos <laszlo.gombos@gmail.com>
Sun, 7 Jul 2024 20:52:40 +0000 (16:52 -0400)
Refactor `dracut-initramfs-restore.sh` and `lsinitrd.sh` to introduce a
`find_initrd_for_kernel_version` function to abstract the code. This
commit does not change the logic of the search order.

dracut-initramfs-restore.sh
lsinitrd.sh

index fd4c3c0b7896c80d94c4b0be329d1b01d0a41102..105987ea433e792ecd188323a216210ef337de40 100755 (executable)
@@ -17,39 +17,49 @@ KERNEL_VERSION="$(uname -r)"
 SKIP="$dracutbasedir/skipcpio"
 [[ -x $SKIP ]] || SKIP="cat"
 
-if [[ -d /efi/Default ]] || [[ -d /boot/Default ]] || [[ -d /boot/efi/Default ]]; then
-    MACHINE_ID="Default"
-elif [[ -s /etc/machine-id ]]; then
-    read -r MACHINE_ID < /etc/machine-id
-    [[ $MACHINE_ID == "uninitialized" ]] && MACHINE_ID="Default"
-else
-    MACHINE_ID="Default"
-fi
+find_initrd_for_kernel_version() {
+    local kernel_version="$1"
+    local files machine_id
+
+    if [[ -d /efi/Default ]] || [[ -d /boot/Default ]] || [[ -d /boot/efi/Default ]]; then
+        machine_id="Default"
+    elif [[ -s /etc/machine-id ]]; then
+        read -r machine_id < /etc/machine-id
+        [[ $machine_id == "uninitialized" ]] && machine_id="Default"
+    else
+        machine_id="Default"
+    fi
+
+    if [[ -d /efi/loader/entries || -L /efi/loader/entries ]] \
+        && [[ -d /efi/$machine_id || -L /efi/$machine_id ]]; then
+        echo "/efi/${machine_id}/${kernel_version}/initrd"
+    elif [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
+        && [[ -d /boot/$machine_id || -L /boot/$machine_id ]]; then
+        echo "/boot/${machine_id}/${kernel_version}/initrd"
+    elif [[ -d /boot/efi/loader/entries || -L /boot/efi/loader/entries ]] \
+        && [[ -d /boot/efi/$machine_id || -L /boot/efi/$machine_id ]]; then
+        echo "/boot/efi/$machine_id/$kernel_version/initrd"
+    elif [[ -f /lib/modules/${kernel_version}/initrd ]]; then
+        echo "/lib/modules/${kernel_version}/initrd"
+    elif [[ -f /boot/initramfs-${kernel_version}.img ]]; then
+        echo "/boot/initramfs-${kernel_version}.img"
+    elif mountpoint -q /efi; then
+        echo "/efi/$machine_id/$kernel_version/initrd"
+    elif mountpoint -q /boot/efi; then
+        echo "/boot/efi/$machine_id/$kernel_version/initrd"
+    else
+        files=(/boot/initr*"${kernel_version}"*)
+        if [ "${#files[@]}" -ge 1 ] && [ -e "${files[0]}" ]; then
+            echo "${files[0]}"
+        fi
+    fi
+}
 
 mount -o ro /boot &> /dev/null || true
 
-if [[ -d /efi/loader/entries || -L /efi/loader/entries ]] \
-    && [[ -d /efi/$MACHINE_ID || -L /efi/$MACHINE_ID ]]; then
-    IMG="/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
-elif [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
-    && [[ -d /boot/$MACHINE_ID || -L /boot/$MACHINE_ID ]]; then
-    IMG="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
-elif [[ -d /boot/efi/loader/entries || -L /boot/efi/loader/entries ]] \
-    && [[ -d /boot/efi/$MACHINE_ID || -L /boot/efi/$MACHINE_ID ]]; then
-    IMG="/boot/efi/$MACHINE_ID/$KERNEL_VERSION/initrd"
-elif [[ -f /lib/modules/${KERNEL_VERSION}/initrd ]]; then
-    IMG="/lib/modules/${KERNEL_VERSION}/initrd"
-elif [[ -f /boot/initramfs-${KERNEL_VERSION}.img ]]; then
-    IMG="/boot/initramfs-${KERNEL_VERSION}.img"
-elif mountpoint -q /efi; then
-    IMG="/efi/$MACHINE_ID/$KERNEL_VERSION/initrd"
-elif mountpoint -q /boot/efi; then
-    IMG="/boot/efi/$MACHINE_ID/$KERNEL_VERSION/initrd"
-else
-    files=(/boot/initr*"${KERNEL_VERSION}"*)
-    if [ "${#files[@]}" -ge 1 ] && [ -e "${files[0]}" ]; then
-        IMG="${files[0]}"
-    elif [[ -f /boot/initramfs-linux.img ]]; then
+IMG=$(find_initrd_for_kernel_version "$KERNEL_VERSION")
+if [ -z "$IMG" ]; then
+    if [[ -f /boot/initramfs-linux.img ]]; then
         IMG="/boot/initramfs-linux.img"
     else
         echo "No initramfs image found to restore!"
index f17523cf586525351192647cb70073d80df169bb..15bf4ac572fe658695dc672bab7019b82f125222 100755 (executable)
@@ -98,53 +98,58 @@ done
 
 [[ $KERNEL_VERSION ]] || KERNEL_VERSION="$(uname -r)"
 
-if [[ $1 ]]; then
-    image="$1"
-    if ! [[ -f $image ]]; then
-        {
-            echo "$image does not exist"
-            echo
-        } >&2
-        usage
-        exit 1
-    fi
-else
+find_initrd_for_kernel_version() {
+    local kernel_version="$1"
+    local machine_id
+
     if [[ -d /efi/Default ]] || [[ -d /boot/Default ]] || [[ -d /boot/efi/Default ]]; then
-        MACHINE_ID="Default"
+        machine_id="Default"
     elif [[ -s /etc/machine-id ]]; then
-        read -r MACHINE_ID < /etc/machine-id
-        [[ $MACHINE_ID == "uninitialized" ]] && MACHINE_ID="Default"
+        read -r machine_id < /etc/machine-id
+        [[ $machine_id == "uninitialized" ]] && machine_id="Default"
     else
-        MACHINE_ID="Default"
+        machine_id="Default"
     fi
 
     if [[ -d /efi/loader/entries || -L /efi/loader/entries ]] \
-        && [[ $MACHINE_ID ]] \
-        && [[ -d /efi/${MACHINE_ID} || -L /efi/${MACHINE_ID} ]]; then
-        image="/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
+        && [[ $machine_id ]] \
+        && [[ -d /efi/${machine_id} || -L /efi/${machine_id} ]]; then
+        echo "/efi/${machine_id}/${kernel_version}/initrd"
     elif [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
-        && [[ $MACHINE_ID ]] \
-        && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]]; then
-        image="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
+        && [[ $machine_id ]] \
+        && [[ -d /boot/${machine_id} || -L /boot/${machine_id} ]]; then
+        echo "/boot/${machine_id}/${kernel_version}/initrd"
     elif [[ -d /boot/efi/loader/entries || -L /boot/efi/loader/entries ]] \
-        && [[ $MACHINE_ID ]] \
-        && [[ -d /boot/efi/${MACHINE_ID} || -L /boot/efi/${MACHINE_ID} ]]; then
-        image="/boot/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
-    elif [[ -f /lib/modules/${KERNEL_VERSION}/initrd ]]; then
-        image="/lib/modules/${KERNEL_VERSION}/initrd"
-    elif [[ -f /lib/modules/${KERNEL_VERSION}/initramfs.img ]]; then
-        image="/lib/modules/${KERNEL_VERSION}/initramfs.img"
-    elif [[ -f /boot/initramfs-${KERNEL_VERSION}.img ]]; then
-        image="/boot/initramfs-${KERNEL_VERSION}.img"
-    elif [[ $MACHINE_ID ]] \
+        && [[ $machine_id ]] \
+        && [[ -d /boot/efi/${machine_id} || -L /boot/efi/${machine_id} ]]; then
+        echo "/boot/efi/${machine_id}/${kernel_version}/initrd"
+    elif [[ -f /lib/modules/${kernel_version}/initrd ]]; then
+        echo "/lib/modules/${kernel_version}/initrd"
+    elif [[ -f /lib/modules/${kernel_version}/initramfs.img ]]; then
+        echo "/lib/modules/${kernel_version}/initramfs.img"
+    elif [[ -f /boot/initramfs-${kernel_version}.img ]]; then
+        echo "/boot/initramfs-${kernel_version}.img"
+    elif [[ $machine_id ]] \
         && mountpoint -q /efi; then
-        image="/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
-    elif [[ $MACHINE_ID ]] \
+        echo "/efi/${machine_id}/${kernel_version}/initrd"
+    elif [[ $machine_id ]] \
         && mountpoint -q /boot/efi; then
-        image="/boot/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
-    else
-        image=""
+        echo "/boot/efi/${machine_id}/${kernel_version}/initrd"
     fi
+}
+
+if [[ $1 ]]; then
+    image="$1"
+    if ! [[ -f $image ]]; then
+        {
+            echo "$image does not exist"
+            echo
+        } >&2
+        usage
+        exit 1
+    fi
+else
+    image=$(find_initrd_for_kernel_version "$KERNEL_VERSION")
 fi
 
 shift