blob: d3c8835959fdc533ec7a21e4dac344275cb8439b [file] [log] [blame]
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06001#!/bin/sh
2
3echo shutdown: "$@"
4
5export PS1=shutdown-sh#\
6# exec bin/sh
7
Milton Millerfc6a3172016-02-05 11:06:39 -06008cd /
Milton D. Miller IId89d5e02016-01-20 14:57:54 -06009if [ ! -e /proc/mounts ]
10then
11 mkdir -p /proc
Milton Millerfc6a3172016-02-05 11:06:39 -060012 mount proc /proc -tproc
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060013 umount_proc=1
14else
15 umount_proc=
16fi
17
Milton Millerbf0f7952016-02-05 17:26:04 -060018# Remove an empty oldroot, that means we are not invoked from systemd-shutdown
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060019rmdir /oldroot 2>/dev/null
20
Milton Millerbf0f7952016-02-05 17:26:04 -060021# Move /oldroot/run to /mnt in case it has the underlying rofs loop mounted.
22# Ordered before /oldroot the overlay is unmounted before the loop mount
23mkdir -p /mnt
24mount --move /oldroot/run /mnt
25
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060026set -x
Milton Millerbf0f7952016-02-05 17:26:04 -060027for f in $( awk '/oldroot|mnt/ { print $2 }' < /proc/mounts | sort -r )
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060028do
29 umount $f
30done
31set +x
32
Milton D. Miller IIaa305252016-02-28 18:13:58 -060033update=/run/initramfs/update
Milton Millerd93b48e2016-02-05 11:19:58 -060034image=/run/initramfs/image-
Milton D. Miller IIaa305252016-02-28 18:13:58 -060035
Milton Millerac091c42017-07-20 14:05:26 -050036wdt="-t 1 -T 5"
37wdrst="-T 15"
38
Milton D. Miller II5b82a712016-02-28 18:06:46 -060039if ls $image* > /dev/null 2>&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060040then
Milton D. Miller IIaa305252016-02-28 18:13:58 -060041 if test -x $update
Milton D. Miller II5b82a712016-02-28 18:06:46 -060042 then
Milton Millerac091c42017-07-20 14:05:26 -050043 if test -c /dev/watchdog
44 then
45 echo Pinging watchdog ${wdt+with args $wdt}
46 watchdog $wdt -F /dev/watchdog &
47 wd=$!
48 else
49 wd=
50 fi
Milton D. Miller IIbaf190a2016-02-29 11:22:06 -060051 $update --clean-saved-files
Milton Millera52546e2016-05-23 18:44:30 -050052 remaining=$(ls $image*)
53 if test -n "$remaining"
54 then
55 echo 1>&2 "Flash update failed to flash these images:"
56 echo 1>&2 "$remaining"
57 else
58 echo "Flash update completed."
59 fi
Milton Millerac091c42017-07-20 14:05:26 -050060
61 if test -n "$wd"
62 then
63 kill -9 $wd
64 if test -n "$wdrst"
65 then
Gunnar Millsc4527442017-10-25 21:25:57 -050066 echo Resetting watchdog timeouts to $wdrst
Milton Millerac091c42017-07-20 14:05:26 -050067 watchdog $wdrst -F /dev/watchdog &
68 sleep 1
69 # Kill the watchdog daemon, setting a timeout
70 # for the remaining shutdown work
71 kill -9 $!
72 fi
73 fi
Milton D. Miller II5b82a712016-02-28 18:06:46 -060074 else
Milton D. Miller IIaa305252016-02-28 18:13:58 -060075 echo 1>&2 "Flash update requested but $update program missing!"
Milton D. Miller II5b82a712016-02-28 18:06:46 -060076 fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060077fi
78
79echo Remaining mounts:
80cat /proc/mounts
81
Milton D. Miller II61baed02016-02-28 17:08:50 -060082test "$umount_proc" && umount /proc && rmdir /proc
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060083
Milton D. Miller IIafb85252016-02-28 17:18:44 -060084# tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console
Milton D. Miller II12ee93b2016-02-24 12:04:05 -060085test -t 1 && stty cooked 0<&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060086
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060087# Execute the command systemd told us to ...
88if test -d /oldroot && test "$1"
89then
Milton D. Miller IIf4d9bc52016-02-28 17:23:04 -060090 if test "$1" = kexec
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060091 then
92 $1 -f -e
93 else
94 $1 -f
95 fi
96fi
97
98
Andrew Jefferyfc4c87d2016-11-22 14:33:23 +103099echo "Execute ${1-reboot} -f if all unmounted ok, or exec /init"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -0600100
101export PS1=shutdown-sh#\
102exec /bin/sh