5?-dracut*.install: better parsing of /proc/cmdline
[dracut.git] / 51-dracut-rescue.install
blob0f262d58a88471cba2c8f2f25f3f17be777ca48c
1 #!/bin/bash
3 export LANG=C
5 COMMAND="$1"
6 KERNEL_VERSION="$2"
7 BOOT_DIR_ABS="${3%/*}/0-rescue"
8 KERNEL_IMAGE="$4"
11 dropindirs_sort()
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
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
30 done
31 done
34 [[ -f /etc/os-release ]] && . /etc/os-release
36 if [[ ! -f /etc/machine-id ]] || [[ ! -s /etc/machine-id ]]; then
37 systemd-machine-id-setup
40 [[ -f /etc/machine-id ]] && read MACHINE_ID < /etc/machine-id
42 if [[ -f /etc/kernel/cmdline ]]; then
43 read -r -d '' -a BOOT_OPTIONS < /etc/kernel/cmdline
44 elif [[ -f /usr/lib/kernel/cmdline ]]; then
45 read -r -d '' -a BOOT_OPTIONS < /usr/lib/kernel/cmdline
47 if ! [[ "${BOOT_OPTIONS[@]}" ]]; then
48 read -r -d '' -a BOOT_OPTIONS < /proc/cmdline
50 if ! [[ $BOOT_OPTIONS ]]; then
51 exit 1
54 LOADER_ENTRY="/boot/loader/entries/${MACHINE_ID}-0-rescue.conf"
55 BOOT_DIR="/${MACHINE_ID}/0-rescue"
57 ret=0
59 case "$COMMAND" in
60 add)
61 for i in "/boot/loader/entries/${MACHINE_ID}-0-rescue.conf"; do
62 [[ -f $i ]] && exit 0
63 done
65 # source our config dir
66 for f in $(dropindirs_sort ".conf" "/etc/dracut.conf.d" "/usr/lib/dracut/dracut.conf.d"); do
67 [[ -e $f ]] && . "$f"
68 done
70 [[ $dracut_rescue_image != "yes" ]] && exit 0
72 [[ -d "$BOOT_DIR_ABS" ]] || mkdir -p "$BOOT_DIR_ABS"
74 if ! cp --reflink=auto "$KERNEL_IMAGE" "$BOOT_DIR_ABS"/linux; then
75 echo "Can't copy '$KERNEL_IMAGE to '$BOOT_DIR_ABS/linux'!" >&2
78 dracut --no-hostonly -a "rescue" "$BOOT_DIR_ABS"/initrd "$KERNEL_VERSION"
79 ((ret+=$?))
82 echo "title $PRETTY_NAME - Rescue Image"
83 echo "version $KERNEL_VERSION"
84 echo "machine-id $MACHINE_ID"
85 echo "options ${BOOT_OPTIONS[@]} rd.auto=1"
86 echo "linux $BOOT_DIR/linux"
87 echo "initrd $BOOT_DIR/initrd"
88 } > $LOADER_ENTRY
89 ((ret+=$?))
92 remove)
93 exit 0
97 usage
98 ret=1;;
99 esac
101 ((ret+=$?))
103 exit $ret