]> git.ipfire.org Git - thirdparty/dracut.git/blame - install.d/51-dracut-rescue.install
chore: remove unnecessary shellcheck disable for SC2154
[thirdparty/dracut.git] / install.d / 51-dracut-rescue.install
CommitLineData
2fa6fd3a 1#!/bin/bash
d837ac39
HH
2
3export LANG=C
4
5COMMAND="$1"
6KERNEL_VERSION="$2"
8652d549
HH
7BOOT_DIR_ABS="${3%/*}/0-rescue"
8KERNEL_IMAGE="$4"
9
17b8649e
AA
10dropindirs_sort() {
11 suffix=$1
12 shift
2fa6fd3a
HH
13 args=("$@")
14 files=$(
17b8649e 15 while (($# > 0)); do
7356c828
HH
16 for i in "${1}"/*"${suffix}"; do
17 [[ -f $i ]] && echo "${i##*/}"
2fa6fd3a
HH
18 done
19 shift
20 done | sort -Vu
21 )
22
23 for f in $files; do
24 for d in "${args[@]}"; do
25 if [[ -f "$d/$f" ]]; then
26 echo "$d/$f"
27 continue 2
28 fi
29 done
30 done
31}
d837ac39 32
17b8649e
AA
33if [[ -f /etc/os-release ]]; then
34 . /etc/os-release
35elif [[ -f /usr/lib/os-release ]]; then
36 . /usr/lib/os-release
37fi
38
39[[ -n $PRETTY_NAME ]] || PRETTY_NAME="Linux $KERNEL_VERSION"
478aa7da 40
b71d162a
YW
41if [[ ${KERNEL_INSTALL_MACHINE_ID+x} ]]; then
42 MACHINE_ID=$KERNEL_INSTALL_MACHINE_ID
17b8649e 43elif [[ -f /etc/machine-id ]]; then
7356c828 44 read -r MACHINE_ID < /etc/machine-id
478aa7da
HH
45fi
46
b71d162a
YW
47if ! [[ $MACHINE_ID ]]; then
48 exit 0
49fi
478aa7da 50
17b8649e
AA
51if [ -n "$KERNEL_INSTALL_CONF_ROOT" ]; then
52 if [ -f "$KERNEL_INSTALL_CONF_ROOT/cmdline" ]; then
53 read -r -d '' -a BOOT_OPTIONS < "$KERNEL_INSTALL_CONF_ROOT/cmdline"
54 fi
55elif [[ -f /etc/kernel/cmdline ]]; then
a6018700 56 read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
a7d3ad67 57elif [[ -f /usr/lib/kernel/cmdline ]]; then
a6018700 58 read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
38b4f4b2 59else
b1c5cae6
ZJS
60 declare -a BOOT_OPTIONS
61
10e59202
HH
62 read -r -d '' -a line < /proc/cmdline
63 for i in "${line[@]}"; do
17b8649e 64 [[ ${i#initrd=*} != "$i" ]] && continue
10e59202
HH
65 BOOT_OPTIONS+=("$i")
66 done
d837ac39 67fi
10e59202 68
17b8649e 69if [[ -d ${BOOT_DIR_ABS%/*} ]]; then
b39b8452 70 BOOT_DIR="/${MACHINE_ID}/0-rescue"
17b8649e 71 BOOT_ROOT=${BOOT_DIR_ABS%"$BOOT_DIR"}
b39b8452
JMC
72 LOADER_ENTRY="$BOOT_ROOT/loader/entries/${MACHINE_ID}-0-rescue.conf"
73 KERNEL="linux"
74 INITRD="initrd"
75else
76 BLS_DIR="/boot/loader/entries"
b39b8452
JMC
77 BOOT_DIR_ABS="/boot"
78 LOADER_ENTRY="$BLS_DIR/${MACHINE_ID}-0-rescue.conf"
79 KERNEL="vmlinuz-0-rescue-${MACHINE_ID}"
80 INITRD="initramfs-0-rescue-${MACHINE_ID}.img"
81fi
d837ac39
HH
82
83ret=0
84
85case "$COMMAND" in
86 add)
17b8649e
AA
87 if [[ -f $LOADER_ENTRY ]] && [[ -f "$BOOT_DIR_ABS/$KERNEL" ]] \
88 && [[ -f "$BOOT_DIR_ABS/$INITRD" ]]; then
89 [[ $KERNEL_INSTALL_VERBOSE == 1 ]] \
90 && echo "Skipping, there is already a rescue image generated with the same input parameters"
91 exit 0
92 fi
d837ac39 93
2fa6fd3a
HH
94 # source our config dir
95 for f in $(dropindirs_sort ".conf" "/etc/dracut.conf.d" "/usr/lib/dracut/dracut.conf.d"); do
7356c828
HH
96 if [[ -e $f ]]; then
97 # shellcheck disable=SC1090
98 . "$f"
99 fi
2fa6fd3a
HH
100 done
101
17b8649e
AA
102 if [[ $dracut_rescue_image != "yes" ]]; then
103 [[ $KERNEL_INSTALL_VERBOSE == 1 ]] \
104 && echo "Skipping, 'dracut_rescue_image' not set to 'yes' in any dracut configuration file"
105 exit 0
106 fi
2fa6fd3a 107
17b8649e 108 [[ -d $BOOT_DIR_ABS ]] || mkdir -p "$BOOT_DIR_ABS"
c0c6c74d 109
b39b8452
JMC
110 if ! cp --reflink=auto "$KERNEL_IMAGE" "$BOOT_DIR_ABS/$KERNEL"; then
111 echo "Can't copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/$KERNEL'!" >&2
2fa6fd3a
HH
112 fi
113
b39b8452 114 if [[ ! -f "$BOOT_DIR_ABS/$INITRD" ]]; then
17b8649e 115 # shellcheck disable=SC2046
a037634a 116 dracut -f --no-hostonly --no-uefi \
17b8649e
AA
117 -a "rescue" \
118 $([[ $KERNEL_INSTALL_VERBOSE == 1 ]] && echo --verbose) \
119 --kver "$KERNEL_VERSION" \
120 "$BOOT_DIR_ABS/$INITRD"
121 ((ret += $?))
b39b8452
JMC
122 fi
123
17b8649e
AA
124 [[ $KERNEL_INSTALL_VERBOSE == 1 ]] && echo "Creating $LOADER_ENTRY"
125 if [[ ${BOOT_DIR_ABS} != "/boot" ]]; then
b39b8452
JMC
126 {
127 echo "title $PRETTY_NAME - Rescue Image"
128 echo "version $KERNEL_VERSION"
129 echo "machine-id $MACHINE_ID"
7356c828 130 echo "options ${BOOT_OPTIONS[*]} rd.auto=1"
b39b8452
JMC
131 echo "linux $BOOT_DIR/linux"
132 echo "initrd $BOOT_DIR/initrd"
7356c828 133 } > "$LOADER_ENTRY"
b39b8452 134 else
ff366790 135 if [[ -e "${BLS_DIR}/${MACHINE_ID}-${KERNEL_VERSION}.conf" ]]; then
7356c828 136 cp -aT "${BLS_DIR}/${MACHINE_ID}-${KERNEL_VERSION}.conf" "$LOADER_ENTRY"
ff366790 137 else
7356c828 138 cp -aT "${KERNEL_IMAGE%/*}/bls.conf" "$LOADER_ENTRY"
ff366790 139 fi
7356c828 140 sed -i "s/${KERNEL_VERSION}/0-rescue-${MACHINE_ID}/" "$LOADER_ENTRY"
b39b8452 141 fi
d837ac39 142
17b8649e 143 ((ret += $?))
d837ac39
HH
144 ;;
145
146 remove)
2fa6fd3a 147 exit 0
d837ac39
HH
148 ;;
149
d837ac39
HH
150esac
151
d837ac39 152exit $ret