-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkexec-load-grub
executable file
·198 lines (169 loc) · 4.28 KB
/
kexec-load-grub
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env bash
set -eu
PROGNAME=${0##*/}
PROGVERSION="1.0"
GRUBDATA=""
GRUBROOT=""
GRUBPREFIX=""
MENUENTRY=()
INITRDS=()
KERNELS=()
KARGS=()
# Mapping from grubs internal index to our menu index
GRUBINDEX=()
SELECTED=0
main() {
local showmenu=1
local args=$(getopt -o v -l version,root:,auto -n "$PROGNAME" -- "$@")
[ $? != 0 ] && exit 2
eval set -- $args
while true; do
case "$1" in
'--version'|'-v')
echo "$PROGNAME version $PROGVERSION"
exit 0
;;
'--root')
GRUBROOT="$2"
shift 2
continue
;;
'--auto')
showmenu=0
shift
continue
;;
'--')
shift
break
;;
*)
echo 'Internal error!' >&2
exit 1
esac
done
if [[ -z $GRUBROOT ]]; then
GRUBROOT=$(grub_root_probe)
fi
mountpoint -q /boot && GRUBPREFIX=/boot || GRUBPREFIX=
GRUBDATA=$(< "$GRUBROOT/grub.cfg")
grub_cfg_load
grub_default_load
[[ 1 = $showmenu ]] && menu_show
kexec_load
}
grub_root_probe() {
# Debian and friends
if [[ -f /boot/grub/grub.cfg ]]; then
echo "/boot/grub"
# RHEL and friends
elif [[ -f /boot/efi/EFI/redhat/grub.cfg ]]; then
echo "/boot/efi/EFI/redhat"
elif [[ -f /boot/grub2/grub.cfg ]]; then
echo "/boot/grub2"
else
echo "No grub.cfg could be located." 1>&2
exit 1
fi
}
grub_cfg_load() {
local offsets=($(echo "$GRUBDATA" | grep -n '^[[:space:]]*menuentry[[:space:]]' | cut -d: -f1))
local menuindex=0
local begin=0
local length=0
for grubindex in ${!offsets[@]}; do
begin=${offsets[$grubindex]}
length=$(echo "$GRUBDATA" | tail -n+$begin | grep -n -m 1 '^[[:space:]]*}[[:space:]]*' | cut -d: -f1)
grub_parse_entry "$(echo "$GRUBDATA" | tail -n+$begin | head -n$length)"
if [[ ! -z $entry_kernel_image ]]; then
MENUENTRY+=("$entry_name")
KERNELS+=("$entry_kernel_image")
INITRDS+=("$entry_initrd")
KARGS+=("$entry_append")
# grub_default_load needs to map from name to menu index; grub index to menu index
GRUBINDEX[$grubindex]=$menuindex
menuindex=$(($menuindex + 1))
fi
done
}
grub_default_load() {
# Find the default
local entry=$(echo "$GRUBDATA" | awk '/set default/ {print $2}' | cut -d'"' -f2 | tail -1)
if [[ "$entry" = '${saved_entry}' ]]; then
entry=$(sed -ne 's/^saved_entry=//p' "$GRUBROOT/grubenv")
fi
if [[ -z "$entry" ]]; then
entry=0
fi
if [[ $entry =~ ^[0-9]+$ ]]; then
if [[ "${GRUBINDEX[$entry]+isset}" ]]; then
SELECTED="${GRUBINDEX[$entry]}"
fi
else
for index in "${!MENUENTRY[@]}"; do
if [[ "${MENUENTRY[$index]}" = "$entry" ]]; then
SELECTED=$index
continue
fi
done
fi
}
grub_parse_entry() {
local data="$1"
entry_name=""
entry_kernel_image=""
entry_append=""
entry_initrd=""
while read command args; do
case "$command" in
'menuentry')
# filter args for $... this is not great.
eval set -- "$(printf '%s' "$args" | sed 's/\$[^ ]*//g')"
entry_name="$1"
;;
'linux'|'linux16'|'linuxefi')
while read image append; do
entry_kernel_image="${GRUBPREFIX}$image"
entry_append="$append"
done <<< "$args"
;;
'initrd'|'initrd16'|'initrdefi')
entry_initrd="${GRUBPREFIX}$args"
;;
esac
done <<< "$data"
}
menu_show() {
local wmenu=()
for index in "${!MENUENTRY[@]}"; do
wmenu+=("$index" "${MENUENTRY[$index]}")
done
SELECTED=$(whiptail \
--title 'KEXEC GRUB helper' \
--menu 'Select a GRUB entry' \
--notags \
--default-item "$SELECTED" \
25 85 16 -- \
"${wmenu[@]}" 3>&1 1>&2 2>&3)
}
kexec_load() {
local kernel_image="${KERNELS[$SELECTED]}"
local initrd="${INITRDS[$SELECTED]}"
local append="${KARGS[$SELECTED]}"
test -z "$append" && append="$(cat /proc/cmdline)"
printf 'Selected "%s"\n\n' "${MENUENTRY[$SELECTED]}"
if [[ -z "$kernel_image" ]]; then
echo "No kernel could be located." 1>&2
exit 1
fi
local kexec_args=('-l' "$kernel_image")
[[ -z "$initrd" ]] || kexec_args+=("--initrd=$initrd")
[[ -z "$append" ]] || kexec_args+=("--append=$append")
printf 'kernel %s\n' "$kernel_image"
printf 'initrd %s\n' "$initrd"
printf 'cmdline %s\n' "$append"
/sbin/kexec "${kexec_args[@]}"
echo
echo 'Ready to boot new kernel using "systemctl kexec"'
}
main "$@"