1#!/bin/sh
2# We are called with stdin/out/err = /dev/null
3
4resetgracetime=60
5
6logfile="/var/log/reboot/`date '+%Y%m%d%H%M%S'`.log"
7mkdir -p /var/log/reboot
8
9PATH=/sbin:/bin
10
11say() {
12	printf "\r%s\n\r" "$*"
13}
14
15# Since there is a potential for various fuckups during umount,
16# we start delayed hard reboot here which will forcibly
17# reboot hung box in a remote datacenter a thousand miles away ;)
18if test "$1" = "-r"; then
19	./hardshutdown -r "$resetgracetime" &
20fi
21
22# Now, (try to) switch away from X and open a console. I've seen reboots
23# hung on open("/dev/console"), therefore we do it _after_ hardshutdown
24exec >/dev/console 2>&1
25
26if test "$1" = "-r"; then
27	say "* `date '+%H:%M:%S'` Scheduled hard reboot in $resetgracetime seconds"
28fi
29
30say "* `date '+%H:%M:%S'` Stopping tasks (see /var/log/reboot/* files)"
31# log reboot event to file. %Y%m%d%H%M%S: YYYYMMDDHHMMSS
32./stop_tasks >"$logfile" 2>&1
33
34# Dying X tends to leave us at semi-random vt. Try to fix that,
35# but if it doesn't work, proceed anyway.
36exec >/dev/null 2>&1
37chvt 1 & sleep 1
38exec >/dev/console 2>&1
39
40command -v ctrlaltdel >/dev/null && {
41	say "* `date '+%H:%M:%S'` Setting Ctrl-Alt-Del to 'hard'"
42	ctrlaltdel hard
43}
44
45say "* `date '+%H:%M:%S'` Stopping storage devices"
46# we can't log this: we are about to unmount everything!
47./stop_storage "$@"
48
49# If we have cmdline params, start hardshutdown with them
50test "$*" && ./hardshutdown "$@"
51
52# Just sleep endlessly...
53say "* `date '+%H:%M:%S'` You may now power off or press Ctrl-Alt-Del to reboot"
54while true; do sleep 32000; done
55