]> git.ipfire.org Git - thirdparty/dracut.git/blame - 51-dracut-rescue.install
fix: shellcheck for 51-dracut-rescue.install
[thirdparty/dracut.git] / 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
2fa6fd3a
HH
10
11dropindirs_sort()
12{
13 suffix=$1; shift
14 args=("$@")
15 files=$(
16 while (( $# > 0 )); do
7356c828
HH
17 for i in "${1}"/*"${suffix}"; do
18 [[ -f $i ]] && echo "${i##*/}"
2fa6fd3a
HH
19 done
20 shift
21 done | sort -Vu
22 )
23
24 for f in $files; do
25 for d in "${args[@]}"; do
26 if [[ -f "$d/$f" ]]; then
27 echo "$d/$f"
28 continue 2
29 fi
30 done
31 done
32}
d837ac39
HH
33
34[[ -f /etc/os-release ]] && . /etc/os-release
478aa7da 35
b71d162a
YW
36if [[ ${KERNEL_INSTALL_MACHINE_ID+x} ]]; then
37 MACHINE_ID=$KERNEL_INSTALL_MACHINE_ID
38elif [[ -f /etc/machine-id ]] ; then
7356c828 39 read -r MACHINE_ID < /etc/machine-id
478aa7da
HH
40fi
41
b71d162a
YW
42if ! [[ $MACHINE_ID ]]; then
43 exit 0
44fi
478aa7da 45
d837ac39 46if [[ -f /etc/kernel/cmdline ]]; then
a6018700 47 read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
a7d3ad67 48elif [[ -f /usr/lib/kernel/cmdline ]]; then
a6018700 49 read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
38b4f4b2 50else
b1c5cae6
ZJS
51 declare -a BOOT_OPTIONS
52
10e59202
HH
53 read -r -d '' -a line < /proc/cmdline
54 for i in "${line[@]}"; do
55 [[ "${i#initrd=*}" != "$i" ]] && continue
56 BOOT_OPTIONS+=("$i")
57 done
d837ac39 58fi
10e59202 59
f8c24964 60if [[ -d "${BOOT_DIR_ABS%/*}" ]]; then
b39b8452
JMC
61 BOOT_DIR="/${MACHINE_ID}/0-rescue"
62 BOOT_ROOT=${BOOT_DIR_ABS%$BOOT_DIR}
63 LOADER_ENTRY="$BOOT_ROOT/loader/entries/${MACHINE_ID}-0-rescue.conf"
64 KERNEL="linux"
65 INITRD="initrd"
66else
67 BLS_DIR="/boot/loader/entries"
b39b8452
JMC
68 BOOT_DIR_ABS="/boot"
69 LOADER_ENTRY="$BLS_DIR/${MACHINE_ID}-0-rescue.conf"
70 KERNEL="vmlinuz-0-rescue-${MACHINE_ID}"
71 INITRD="initramfs-0-rescue-${MACHINE_ID}.img"
72fi
d837ac39
HH
73
74ret=0
75
76case "$COMMAND" in
77 add)
b39b8452
JMC
78 [[ -f "$LOADER_ENTRY" ]] && [[ -f "$BOOT_DIR_ABS/$KERNEL" ]] \
79 && [[ -f "$BOOT_DIR_ABS/$INITRD" ]] && exit 0
d837ac39 80
2fa6fd3a
HH
81 # source our config dir
82 for f in $(dropindirs_sort ".conf" "/etc/dracut.conf.d" "/usr/lib/dracut/dracut.conf.d"); do
7356c828
HH
83 if [[ -e $f ]]; then
84 # shellcheck disable=SC1090
85 . "$f"
86 fi
2fa6fd3a
HH
87 done
88
7356c828 89 # shellcheck disable=SC2154
2fa6fd3a
HH
90 [[ $dracut_rescue_image != "yes" ]] && exit 0
91
c0c6c74d
HH
92 [[ -d "$BOOT_DIR_ABS" ]] || mkdir -p "$BOOT_DIR_ABS"
93
b39b8452
JMC
94 if ! cp --reflink=auto "$KERNEL_IMAGE" "$BOOT_DIR_ABS/$KERNEL"; then
95 echo "Can't copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/$KERNEL'!" >&2
2fa6fd3a
HH
96 fi
97
b39b8452 98 if [[ ! -f "$BOOT_DIR_ABS/$INITRD" ]]; then
48c283a2 99 dracut -f --no-hostonly -a "rescue" "$BOOT_DIR_ABS/$INITRD" "$KERNEL_VERSION"
b39b8452
JMC
100 ((ret+=$?))
101 fi
102
f8c24964 103 if [[ "${BOOT_DIR_ABS}" != "/boot" ]]; then
b39b8452
JMC
104 {
105 echo "title $PRETTY_NAME - Rescue Image"
106 echo "version $KERNEL_VERSION"
107 echo "machine-id $MACHINE_ID"
7356c828 108 echo "options ${BOOT_OPTIONS[*]} rd.auto=1"
b39b8452
JMC
109 echo "linux $BOOT_DIR/linux"
110 echo "initrd $BOOT_DIR/initrd"
7356c828 111 } > "$LOADER_ENTRY"
b39b8452 112 else
ff366790 113 if [[ -e "${BLS_DIR}/${MACHINE_ID}-${KERNEL_VERSION}.conf" ]]; then
7356c828 114 cp -aT "${BLS_DIR}/${MACHINE_ID}-${KERNEL_VERSION}.conf" "$LOADER_ENTRY"
ff366790 115 else
7356c828 116 cp -aT "${KERNEL_IMAGE%/*}/bls.conf" "$LOADER_ENTRY"
ff366790 117 fi
7356c828 118 sed -i "s/${KERNEL_VERSION}/0-rescue-${MACHINE_ID}/" "$LOADER_ENTRY"
b39b8452 119 fi
d837ac39 120
d837ac39 121 ((ret+=$?))
d837ac39
HH
122 ;;
123
124 remove)
2fa6fd3a 125 exit 0
d837ac39
HH
126 ;;
127
128 *)
129 usage
130 ret=1;;
131esac
132
d837ac39 133exit $ret