-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbootloop
More file actions
executable file
·263 lines (225 loc) · 6.01 KB
/
bootloop
File metadata and controls
executable file
·263 lines (225 loc) · 6.01 KB
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#!/bin/bash
#
# Need path for script to run in cron
#
PATH=$PATH:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
countdown=20
mindelay=20
maxdelay=600
action="$@" # action consists of all the parameters
stat=1
bootloopfile="/tmp/bootloop.log"
rebootlogfile="/root/reboot.log"
stopfile="/root/stop"
crashcountfile="/tmp/crashcount.log"
crashcount=$(ls /var/crash/* 2> /dev/null | wc -l)
usagestr=$(
cat <<EOF
$(basename $0) [script]
"script" is an optional script to be executed on every loop.
The bootloop is accomplished by creating a cron job that calls
this script and the optional script, if present, on every boot.
The number of boots is printed on the serial console along
with other messages, e.g. the seconds remaining to reboot.
The boot loop is terminated when any one of the following
conditions is met.
1. bootloop finds finds a file named "stop" in /root. The root
user can simply type "touch stop" in the root directory to
do this.
2. bootloop detects a new crash dump under /var/crash.
3. The optional script returns with a zero status.
When the boot loop is terminated, its temp files are deleted,
the cron job is deleted from crontab, leaving any other cron
jobs intact, and a log file is left in "$rebootlogfile".
The default timeout before the next reboot is 20 seconds. You
will be given an opportunity to change this before starting,
but the minimum delay is 20 seconds, to allow enough time to
manually terminate the bootloop.
\0
EOF
)
userstr="
Must be root to invoke $(basename $0)
"
# putcon() - ouput to serial console
#
# Write strings to the console and optionally write the same
# string to a file.
#
# $1 - the string
# $2 - optional file
# $3 - new, any string in this argument will cause the file to be
# opened for writing, which deletes the previous contents.
#
putcon()
{
echo -e "$1" > $console
if [[ $2 ]]; then
if [[ $3 ]]; then
echo -e "$1" > $2
else
echo -e "$1" >> $2
fi
fi
}
# getchar()
#
# Returns character in $1
#
getchar() {
local char
read -n1 char
echo
[ $# -gt 0 ] && eval $1=$char
}
# promptgetchar()
#
# Prints a prompt passed in $1 and returns the char in $2
#
promptgetchar() {
local prompt=$1
echo -ne "$prompt"
getchar $2
}
function getuser {
local promptstring="$1"
local var
echo -ne "$promptstring"
read var
eval $2=$var
}
# loop_yn()
#
# Loop until user gives either a y or n answer.
#
# $1 - prompt string
# $2 - the character to be returned
#
loop_yn() {
local ans
local x
while true; do
promptgetchar "$1" ans
for x in {y,Y,n,N}; do
[[ $x == $ans ]] && eval $2=$ans && return
done
echo -e "Please enter 'y' or 'n'."
done
}
# loop_range()
#
# Loop until user gives a number in the range defined by the args
#
# $1 - prompt string
# $2 - the value to be returned
# $3 - minimum
# $4 - maximum
#
loop_range() {
local number
local min=$3
local max=$4
while true; do
getuser "$1" number
if [[ $number =~ ^-?[0-9]+$ ]] && \
[[ $number -ge $min ]] && \
[[ $number -le $max ]]; then
eval $2=$number
return 0
fi
echo -e "Please enter a number between $min and $max."
done
}
# start_bootloop() - query the user.
#
# If user wants to continue, present option to change default timeout
# between boots.
#
start_bootloop() {
local answer
echo -e "$usagestr"
loop_yn "Do you want to continue? [y/n]: " answer
if [[ $answer == "n" ]] || [[ $answer == "N" ]]; then
exit_bootloop 0
fi
echo "Current delay between reboots is $countdown seconds."
loop_yn "Do you want to change it? [y/n] : " answer
if [[ $answer == "y" ]] || [[ $answer == "Y" ]]; then
loop_range "Seconds delay between reboots: " countdown $mindelay $maxdelay
fi
}
exit_bootloop() {
[[ -e "$bootloopfile" ]] && mv "$bootloopfile" "$rebootlogfile"
[[ -e "$stopfile" ]] && rm -f "$stopfile"
[[ -e "$crashcountfile" ]] && rm -f "$crashcountfile"
crontab -l | grep -v "@reboot /root/bin/bootloop "$action"" | crontab -
exit $1
}
#####################################
# Start of Execution
####################################
[ $(id -u) -eq 0 ] || { echo -e "$userstr"; exit 1; }
[ "$1" == "-h" ] && { echo -e "$usagestr"; exit 1; }
# Find the serial console
# If we can't find an active console, direct output to /dev/null
#
console=$(cat /proc/tty/driver/serial | grep RTS | grep CTS | grep DTR)
if [[ $console ]]; then
console="/dev/ttyS"$(echo $console | cut -d':' -f1)
else
console="/dev/null"
fi
putcon "bootloop pid: "$$""
# If the bootloopfile exists, then update it.
# Else, create the cron job for the bootloop. initialize the bootcount,
# and create the bootloop file and the crashcountfile, and query the
# user.
#
if [ -f $bootloopfile ]; then
bootcount=$(( $(cat $bootloopfile | cut -d':' -f2) + 1 ))
else
echo "@reboot /root/bin/bootloop $action" | crontab
bootcount=1
> $bootloopfile
echo $crashcount > $crashcountfile
start_bootloop
fi
putcon "boot count: "$bootcount"" $bootloopfile new
# If there's been a crash, then exit with nonzero status.
# We can tell there's been a crash if there are any files under
# /var/crash.
#
oldcount=$(cat $crashcountfile)
if [[ $crashcount != $oldcount ]]; then
putcon "New core dump detected. Exiting bootloop." $bootloopfile
exit_bootloop 1
fi
if [ "$action" ]; then
# wall -n "Executing: "$action""
putcon "Executing: "$action""
$action
stat=$?
fi
# If "action" returns nonzero status, or if there was no "action"
# passed, then reboot. Default value of stat is 1, so it can
# only be 0 if an "action" was executed and it returned a 0.
#
if [ $stat -ne 0 ]; then
putcon "Starting countdown to next reboot ..."
for (( i=$countdown; i > 0; --i )); do
# (( $i % 20 )) || wall -n " $i"
(( $i % 20 )) || putcon " $i"
sleep 1
if [[ -e $HOME/stop ]]; then
putcon "\"stop\" file detected.\nbootloop exiting ...\n" $bootloopfile
exit_bootloop 0
fi
done
putcon " $i\nRebooting now ..."
shutdown -r now
else
putcon ""$action": returned zero status." $bootloopfile
putcon "Boot loop halted." $bootloopfile
putcon "Number of boot loops: "$bootcount"\n" $rbootloopfile
exit_bootloop 0
fi