]> git.ipfire.org Git - thirdparty/dracut.git/blobdiff - dracut-initramfs-restore.sh
fix(nvmf): install 8021q module unconditionally
[thirdparty/dracut.git] / dracut-initramfs-restore.sh
old mode 100644 (file)
new mode 100755 (executable)
index 872a5fb..412c3a8
@@ -6,39 +6,59 @@ set -e
 [ -e /run/initramfs/bin/sh ] && exit 0
 [ -e /run/initramfs/.need_shutdown ] || exit 0
 
+# SIGTERM signal is received upon forced shutdown: ignore the signal
+# We want to remain alive to be able to trap unpacking errors to avoid
+# switching root to an incompletely unpacked initramfs
+trap 'echo "Received SIGTERM signal, ignoring!" >&2' TERM
+
 KERNEL_VERSION="$(uname -r)"
 
 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
 SKIP="$dracutbasedir/skipcpio"
-[[ -x $SKIP ]] || SKIP=cat
+[[ -x $SKIP ]] || SKIP="cat"
 
-[[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/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
 
 mount -o ro /boot &> /dev/null || true
 
 if [[ -d /efi/loader/entries || -L /efi/loader/entries ]] \
-    && [[ $MACHINE_ID ]] \
-    && [[ -d /efi/${MACHINE_ID} || -L /efi/${MACHINE_ID} ]]; then
+    && [[ -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 ]] \
-    && [[ $MACHINE_ID ]] \
-    && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]]; then
+    && [[ -d /boot/$MACHINE_ID || -L /boot/$MACHINE_ID ]]; then
     IMG="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
-else
+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
+    echo "No initramfs image found to restore!"
+    exit 1
 fi
 
 cd /run/initramfs
 
-[ -f .need_shutdown -a -f "$IMG" ] || exit 1
-
-if $SKIP "$IMG" | zcat | cpio -id --no-absolute-filenames --quiet > /dev/null; then
-    rm -f -- .need_shutdown
-elif $SKIP "$IMG" | xzcat | cpio -id --no-absolute-filenames --quiet > /dev/null; then
-    rm -f -- .need_shutdown
-elif $SKIP "$IMG" | lz4 -d -c | cpio -id --no-absolute-filenames --quiet > /dev/null; then
-    rm -f -- .need_shutdown
-elif $SKIP "$IMG" | zstd -d -c | cpio -id --no-absolute-filenames --quiet > /dev/null; then
+if (command -v zcat > /dev/null && $SKIP "$IMG" 2> /dev/null | zcat 2> /dev/null | cpio -id --no-absolute-filenames --quiet > /dev/null 2>&1) \
+    || (command -v bzcat > /dev/null && $SKIP "$IMG" 2> /dev/null | bzcat 2> /dev/null | cpio -id --no-absolute-filenames --quiet > /dev/null 2>&1) \
+    || (command -v xzcat > /dev/null && $SKIP "$IMG" 2> /dev/null | xzcat 2> /dev/null | cpio -id --no-absolute-filenames --quiet > /dev/null 2>&1) \
+    || (command -v lz4 > /dev/null && $SKIP "$IMG" 2> /dev/null | lz4 -d -c 2> /dev/null | cpio -id --no-absolute-filenames --quiet > /dev/null 2>&1) \
+    || (command -v lzop > /dev/null && $SKIP "$IMG" 2> /dev/null | lzop -d -c 2> /dev/null | cpio -id --no-absolute-filenames --quiet > /dev/null 2>&1) \
+    || (command -v zstd > /dev/null && $SKIP "$IMG" 2> /dev/null | zstd -d -c 2> /dev/null | cpio -id --no-absolute-filenames --quiet > /dev/null 2>&1) \
+    || ($SKIP "$IMG" 2> /dev/null | cpio -id --no-absolute-filenames --quiet > /dev/null 2>&1); then
     rm -f -- .need_shutdown
 else
     # something failed, so we clean up
@@ -48,8 +68,7 @@ else
 fi
 
 if [[ -d squash ]]; then
-    unsquashfs -no-xattrs -f -d . squash-root.img > /dev/null
-    if [ $? -ne 0 ]; then
+    if ! unsquashfs -no-xattrs -f -d . squash-root.img > /dev/null; then
         echo "Squash module is enabled for this initramfs but failed to unpack squash-root.img" >&2
         rm -f -- /run/initramfs/shutdown
         exit 1
@@ -58,7 +77,7 @@ fi
 
 if [ -e /etc/selinux/config -a -x /usr/sbin/setfiles ]; then
     . /etc/selinux/config
-    /usr/sbin/setfiles -v -r /run/initramfs /etc/selinux/${SELINUXTYPE}/contexts/files/file_contexts /run/initramfs > /dev/null
+    [ -n "${SELINUXTYPE}" ] && /usr/sbin/setfiles -v -r /run/initramfs /etc/selinux/"${SELINUXTYPE}"/contexts/files/file_contexts /run/initramfs > /dev/null
 fi
 
 exit 0