]> git.ipfire.org Git - thirdparty/dracut.git/blob - dracut-initramfs-restore.sh
fix(dracut-initramfs-restore.sh): shellcheck for dracut-initramfs-restore.sh
[thirdparty/dracut.git] / dracut-initramfs-restore.sh
1 #!/bin/bash
2
3 set -e
4
5 # do some sanity checks first
6 [ -e /run/initramfs/bin/sh ] && exit 0
7 [ -e /run/initramfs/.need_shutdown ] || exit 0
8
9 KERNEL_VERSION="$(uname -r)"
10
11 [[ $dracutbasedir ]] || dracutbasedir=/usr/lib/dracut
12 SKIP="$dracutbasedir/skipcpio"
13 [[ -x $SKIP ]] || SKIP="cat"
14
15 [[ -f /etc/machine-id ]] && read -r MACHINE_ID < /etc/machine-id
16
17 mount -o ro /boot &> /dev/null || true
18
19 if [[ -d /efi/loader/entries || -L /efi/loader/entries ]] \
20 && [[ $MACHINE_ID ]] \
21 && [[ -d /efi/${MACHINE_ID} || -L /efi/${MACHINE_ID} ]]; then
22 IMG="/efi/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
23 elif [[ -d /boot/loader/entries || -L /boot/loader/entries ]] \
24 && [[ $MACHINE_ID ]] \
25 && [[ -d /boot/${MACHINE_ID} || -L /boot/${MACHINE_ID} ]]; then
26 IMG="/boot/${MACHINE_ID}/${KERNEL_VERSION}/initrd"
27 else
28 IMG="/boot/initramfs-${KERNEL_VERSION}.img"
29 fi
30
31 cd /run/initramfs
32
33 [ -f .need_shutdown -a -f "$IMG" ] || exit 1
34
35 if $SKIP "$IMG" | zcat | cpio -id --no-absolute-filenames --quiet > /dev/null; then
36 rm -f -- .need_shutdown
37 elif $SKIP "$IMG" | xzcat | cpio -id --no-absolute-filenames --quiet > /dev/null; then
38 rm -f -- .need_shutdown
39 elif $SKIP "$IMG" | lz4 -d -c | cpio -id --no-absolute-filenames --quiet > /dev/null; then
40 rm -f -- .need_shutdown
41 elif $SKIP "$IMG" | zstd -d -c | cpio -id --no-absolute-filenames --quiet > /dev/null; then
42 rm -f -- .need_shutdown
43 else
44 # something failed, so we clean up
45 echo "Unpacking of $IMG to /run/initramfs failed" >&2
46 rm -f -- /run/initramfs/shutdown
47 exit 1
48 fi
49
50 if [[ -d squash ]]; then
51 if ! unsquashfs -no-xattrs -f -d . squash-root.img > /dev/null; then
52 echo "Squash module is enabled for this initramfs but failed to unpack squash-root.img" >&2
53 rm -f -- /run/initramfs/shutdown
54 exit 1
55 fi
56 fi
57
58 if [ -e /etc/selinux/config -a -x /usr/sbin/setfiles ]; then
59 . /etc/selinux/config
60 /usr/sbin/setfiles -v -r /run/initramfs /etc/selinux/"${SELINUXTYPE}"/contexts/files/file_contexts /run/initramfs > /dev/null
61 fi
62
63 exit 0