]>
Commit | Line | Data |
---|---|---|
2fa6fd3a | 1 | #!/bin/bash |
d837ac39 HH |
2 | |
3 | export LANG=C | |
4 | ||
5 | COMMAND="$1" | |
6 | KERNEL_VERSION="$2" | |
8652d549 HH |
7 | BOOT_DIR_ABS="${3%/*}/0-rescue" |
8 | KERNEL_IMAGE="$4" | |
9 | ||
2fa6fd3a HH |
10 | |
11 | dropindirs_sort() | |
12 | { | |
13 | suffix=$1; shift | |
14 | args=("$@") | |
15 | files=$( | |
16 | while (( $# > 0 )); do | |
17 | for i in ${1}/*${suffix}; do | |
18 | [[ -f $i ]] && echo ${i##*/} | |
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 HH |
35 | |
36 | if [[ ! -f /etc/machine-id ]] || [[ ! -s /etc/machine-id ]]; then | |
37 | systemd-machine-id-setup | |
38 | fi | |
39 | ||
d837ac39 | 40 | [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id |
478aa7da | 41 | |
d837ac39 | 42 | if [[ -f /etc/kernel/cmdline ]]; then |
a6018700 | 43 | read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline |
a7d3ad67 | 44 | elif [[ -f /usr/lib/kernel/cmdline ]]; then |
a6018700 | 45 | read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline |
d837ac39 | 46 | fi |
10e59202 | 47 | |
d837ac39 | 48 | if ! [[ "${BOOT_OPTIONS[@]}" ]]; then |
10e59202 HH |
49 | read -r -d '' -a line < /proc/cmdline |
50 | for i in "${line[@]}"; do | |
51 | [[ "${i#initrd=*}" != "$i" ]] && continue | |
52 | BOOT_OPTIONS+=("$i") | |
53 | done | |
d837ac39 | 54 | fi |
10e59202 HH |
55 | |
56 | if ! [[ ${BOOT_OPTIONS[*]} ]]; then | |
57 | echo "Could not determine the kernel command line parameters." >&2 | |
58 | echo "Please specify the kernel command line in /etc/kernel/cmdline!" >&2 | |
d837ac39 HH |
59 | exit 1 |
60 | fi | |
61 | ||
2fa6fd3a HH |
62 | LOADER_ENTRY="/boot/loader/entries/${MACHINE_ID}-0-rescue.conf" |
63 | BOOT_DIR="/${MACHINE_ID}/0-rescue" | |
d837ac39 HH |
64 | |
65 | ret=0 | |
66 | ||
67 | case "$COMMAND" in | |
68 | add) | |
2fa6fd3a | 69 | for i in "/boot/loader/entries/${MACHINE_ID}-0-rescue.conf"; do |
d837ac39 HH |
70 | [[ -f $i ]] && exit 0 |
71 | done | |
72 | ||
2fa6fd3a HH |
73 | # source our config dir |
74 | for f in $(dropindirs_sort ".conf" "/etc/dracut.conf.d" "/usr/lib/dracut/dracut.conf.d"); do | |
75 | [[ -e $f ]] && . "$f" | |
76 | done | |
77 | ||
78 | [[ $dracut_rescue_image != "yes" ]] && exit 0 | |
79 | ||
c0c6c74d HH |
80 | [[ -d "$BOOT_DIR_ABS" ]] || mkdir -p "$BOOT_DIR_ABS" |
81 | ||
8cd88205 | 82 | if ! cp --reflink=auto "$KERNEL_IMAGE" "$BOOT_DIR_ABS"/linux; then |
2fa6fd3a HH |
83 | echo "Can't copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'!" >&2 |
84 | fi | |
85 | ||
95957248 | 86 | dracut --no-hostonly -a "rescue" "$BOOT_DIR_ABS"/initrd "$KERNEL_VERSION" |
d837ac39 HH |
87 | ((ret+=$?)) |
88 | ||
89 | { | |
90 | echo "title $PRETTY_NAME - Rescue Image" | |
91 | echo "version $KERNEL_VERSION" | |
92 | echo "machine-id $MACHINE_ID" | |
93 | echo "options ${BOOT_OPTIONS[@]} rd.auto=1" | |
94 | echo "linux $BOOT_DIR/linux" | |
2fa6fd3a | 95 | echo "initrd $BOOT_DIR/initrd" |
d837ac39 HH |
96 | } > $LOADER_ENTRY |
97 | ((ret+=$?)) | |
d837ac39 HH |
98 | ;; |
99 | ||
100 | remove) | |
2fa6fd3a | 101 | exit 0 |
d837ac39 HH |
102 | ;; |
103 | ||
104 | *) | |
105 | usage | |
106 | ret=1;; | |
107 | esac | |
108 | ||
109 | ((ret+=$?)) | |
110 | ||
111 | exit $ret |