]> git.ipfire.org Git - thirdparty/dracut-ng.git/commitdiff
fix: check for searched initrds to be present
authorBenjamin Drung <benjamin.drung@canonical.com>
Tue, 2 Jul 2024 20:15:41 +0000 (22:15 +0200)
committerLaszlo Gombos <laszlo.gombos@gmail.com>
Sun, 7 Jul 2024 20:52:40 +0000 (16:52 -0400)
`find_initrd_for_kernel_version` uses the same search order than
`kernel-install` from systemd v249 [1]. When
`dracut-initramfs-restore.sh` or `lsinitrd.sh` searches for initrds,
they should check the presence of the files instead of assuming them to
be present. Otherwise the search might be stopped before an existing
initrd is found. On Ubuntu 24.10 (oracular) with a mounted `/boot`:

```
$ ls /boot/initr* -alh
lrwxrwxrwx 1 root root  27 Jun 20 15:31 /boot/initrd.img -> initrd.img-6.8.0-31-generic
-rw-r--r-- 1 root root 22M Jun 20 16:27 /boot/initrd.img-6.8.0-31-generic
lrwxrwxrwx 1 root root  27 Jun 20 15:31 /boot/initrd.img.old -> initrd.img-6.8.0-31-generic
$ lsinitrd 2>&1 | head -n1
No <initramfs file> specified and the default image '/boot/efi/6491959c70e6481eae6406d25e250d76/6.8.0-31-generic/initrd' cannot be accessed!
```

[1] https://github.com/systemd/systemd/blob/v249/src/kernel-install/kernel-install

dracut-initramfs-restore.sh
lsinitrd.sh

index 105987ea433e792ecd188323a216210ef337de40..747253081eaca7ca5a66de05bc899c0a029cc65f 100755 (executable)
@@ -19,7 +19,7 @@ SKIP="$dracutbasedir/skipcpio"
 
 find_initrd_for_kernel_version() {
     local kernel_version="$1"
-    local files machine_id
+    local base_path files initrd machine_id
 
     if [[ -d /efi/Default ]] || [[ -d /boot/Default ]] || [[ -d /boot/efi/Default ]]; then
         machine_id="Default"
@@ -30,23 +30,20 @@ find_initrd_for_kernel_version() {
         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
+    if [ -n "$machine_id" ]; then
+        for base_path in /efi /boot /boot/efi; do
+            initrd="${base_path}/${machine_id}/${kernel_version}/initrd"
+            if [ -f "$initrd" ]; then
+                echo "$initrd"
+                return
+            fi
+        done
+    fi
+
+    if [[ -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
index 15bf4ac572fe658695dc672bab7019b82f125222..39c6aa922662a85749df4d69c115c9d6317d75b2 100755 (executable)
@@ -100,7 +100,7 @@ done
 
 find_initrd_for_kernel_version() {
     local kernel_version="$1"
-    local machine_id
+    local base_path initrd machine_id
 
     if [[ -d /efi/Default ]] || [[ -d /boot/Default ]] || [[ -d /boot/efi/Default ]]; then
         machine_id="Default"
@@ -111,30 +111,22 @@ find_initrd_for_kernel_version() {
         machine_id="Default"
     fi
 
-    if [[ -d /efi/loader/entries || -L /efi/loader/entries ]] \
-        && [[ $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
-        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
-        echo "/boot/efi/${machine_id}/${kernel_version}/initrd"
-    elif [[ -f /lib/modules/${kernel_version}/initrd ]]; then
+    if [ -n "$machine_id" ]; then
+        for base_path in /efi /boot /boot/efi; do
+            initrd="${base_path}/${machine_id}/${kernel_version}/initrd"
+            if [ -f "$initrd" ]; then
+                echo "$initrd"
+                return
+            fi
+        done
+    fi
+
+    if [[ -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
-        echo "/efi/${machine_id}/${kernel_version}/initrd"
-    elif [[ $machine_id ]] \
-        && mountpoint -q /boot/efi; then
-        echo "/boot/efi/${machine_id}/${kernel_version}/initrd"
     fi
 }