forked from avagin/kexec-reboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast-reboot
executable file
·132 lines (118 loc) · 3.09 KB
/
fast-reboot
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
#!/bin/sh
set -e
[ -n "$BASH" ] && set -o pipefail
set -u
# defaults
wait=10
detach_wait=3
nodetach=
usage() {
cat << EOF
Usage: fast-reboot [option ...] [kernel|boot-index]
Options are:
--wait, -w N Wait N seconds before reboot (default $wait)
--nowait Don't wait, reboot immediately
--nodetach, -n Do not detach. By default it runs --detach $detach_wait
--detach, -d N Schedule reboot in N seconds and return
--help, -h Show this help message
--kargs, k Additional kernel arguments
Example:
$0 0 Boot kernel with index 0
EOF
exit $1
}
uerr() {
echo "ERROR: $*" 2>&1
echo
usage 1
}
# Check that all programs we need are available, exit if not
# (relying on 'set -e' and hash returning non-zero for errors)
hash grubby kexec systemctl
# Process options and arguments
target=''
kargs=''
while test $# -gt 0; do
case $1 in
-w|--wait)
if [ "$#" -lt 2 ]; then
uerr "Option $1 requires an argument"
fi
wait=$2
if ! printf "%f" $wait > /dev/null 2>&1; then
uerr "Option $1 argument not a number"
fi
shift
;;
--nowait)
wait=0
;;
-n|--nodetach)
nodetach=yes
;;
-d|--detach)
if [ "$#" -lt 2 ]; then
uerr "Option $1 requires an argument"
fi
detach_wait=$2
if ! printf "%f" $detach_wait > /dev/null 2>&1; then
uerr "Option $1 argument not a number"
fi
shift
;;
-h|--help)
usage 0
;;
-k|--kargs)
kargs=$2
shift
;;
-*)
uerr "Unrecognized option: $1"
;;
*)
if [ -n "$target" ]; then
uerr "Extra arguments: $*"
fi
target=$1
;;
esac
shift
done
if [ -z "$target" ]; then
if ! target=$(grubby --default-kernel); then
# grubby prints error to stdout
echo "grubby: $target" 1>&2
exit 1
fi
fi
if ! info=$(grubby --info=$target); then
# grubby prints error to stdout
echo "grubby: $info" 1>&2
exit 1
fi
eval $(echo "$info" | sed "s|^\(\S*\)=\([^'\"].*\)$|\1='\2'|")
kernel=${kernel##*@} # retain the part after the last @ in case of ZFS where the target is indicated
initrd=${initrd##*@} # retain the part after the last @ in case of ZFS where the target is indicated
printf "\nBooting $title.... "
if [ -z "$nodetach" ]; then
printf "detached\n"
else
printf "not detaching\n"
fi
if [ "$wait" -ne 0 ]; then
printf "\nPress Ctrl-C within $wait seconds to cancel reboot\n\n"
count=0
while [ $count -lt $wait ]; do
printf $count
sleep 1
count=$(( $count + 1 ))
done
printf "\n"
fi
kexec -l "$kernel" --initrd="$initrd" --command-line="root=$root $args $kargs"
if [ -z "$nodetach" ]; then
setsid bash -c "sleep $detach_wait; systemctl kexec" &>/dev/null < /dev/null &
else
systemctl kexec
fi