Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 1 | #!/bin/sh |
| 2 | |
| 3 | echo shutdown: "$@" |
| 4 | |
| 5 | export PS1=shutdown-sh#\ |
| 6 | # exec bin/sh |
| 7 | |
Milton Miller | 1c0dd48 | 2016-02-05 11:06:39 -0600 | [diff] [blame] | 8 | cd / |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 9 | if [ ! -e /proc/mounts ] |
| 10 | then |
| 11 | mkdir -p /proc |
Milton Miller | 1c0dd48 | 2016-02-05 11:06:39 -0600 | [diff] [blame] | 12 | mount proc /proc -tproc |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 13 | umount_proc=1 |
| 14 | else |
| 15 | umount_proc= |
| 16 | fi |
| 17 | |
Milton Miller | 7d9e2e4 | 2016-02-05 17:26:04 -0600 | [diff] [blame] | 18 | # Remove an empty oldroot, that means we are not invoked from systemd-shutdown |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 19 | rmdir /oldroot 2>/dev/null |
| 20 | |
Milton Miller | 7d9e2e4 | 2016-02-05 17:26:04 -0600 | [diff] [blame] | 21 | # 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 |
| 23 | mkdir -p /mnt |
| 24 | mount --move /oldroot/run /mnt |
| 25 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 26 | set -x |
Milton Miller | 7d9e2e4 | 2016-02-05 17:26:04 -0600 | [diff] [blame] | 27 | for f in $( awk '/oldroot|mnt/ { print $2 }' < /proc/mounts | sort -r ) |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 28 | do |
| 29 | umount $f |
| 30 | done |
| 31 | set +x |
| 32 | |
Milton D. Miller II | 648f0a8 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 33 | update=/run/initramfs/update |
Milton Miller | 36d336c | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 34 | image=/run/initramfs/image- |
Milton D. Miller II | 648f0a8 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 35 | |
Milton Miller | 589ebdf | 2017-07-20 14:05:26 -0500 | [diff] [blame] | 36 | wdt="-t 1 -T 5" |
| 37 | wdrst="-T 15" |
| 38 | |
Milton D. Miller II | 042e237 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 39 | if ls $image* > /dev/null 2>&1 |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 40 | then |
Milton D. Miller II | 648f0a8 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 41 | if test -x $update |
Milton D. Miller II | 042e237 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 42 | then |
Milton Miller | 589ebdf | 2017-07-20 14:05:26 -0500 | [diff] [blame] | 43 | 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 II | 32429c4 | 2016-02-29 11:22:06 -0600 | [diff] [blame] | 51 | $update --clean-saved-files |
Milton Miller | 089d5df | 2016-05-23 18:44:30 -0500 | [diff] [blame] | 52 | 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 Miller | 589ebdf | 2017-07-20 14:05:26 -0500 | [diff] [blame] | 60 | |
| 61 | if test -n "$wd" |
| 62 | then |
| 63 | kill -9 $wd |
| 64 | if test -n "$wdrst" |
| 65 | then |
Gunnar Mills | 2a85f60 | 2017-10-25 21:25:57 -0500 | [diff] [blame] | 66 | echo Resetting watchdog timeouts to $wdrst |
Milton Miller | 589ebdf | 2017-07-20 14:05:26 -0500 | [diff] [blame] | 67 | 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 II | 042e237 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 74 | else |
Milton D. Miller II | 648f0a8 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 75 | echo 1>&2 "Flash update requested but $update program missing!" |
Milton D. Miller II | 042e237 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 76 | fi |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 77 | fi |
| 78 | |
| 79 | echo Remaining mounts: |
| 80 | cat /proc/mounts |
| 81 | |
Milton D. Miller II | 576ea81 | 2016-02-28 17:08:50 -0600 | [diff] [blame] | 82 | test "$umount_proc" && umount /proc && rmdir /proc |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 83 | |
Milton D. Miller II | c67caac | 2016-02-28 17:18:44 -0600 | [diff] [blame] | 84 | # tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console |
Milton D. Miller II | e762a08 | 2016-02-24 12:04:05 -0600 | [diff] [blame] | 85 | test -t 1 && stty cooked 0<&1 |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 86 | |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 87 | # Execute the command systemd told us to ... |
| 88 | if test -d /oldroot && test "$1" |
| 89 | then |
Milton D. Miller II | 35b9204 | 2016-02-28 17:23:04 -0600 | [diff] [blame] | 90 | if test "$1" = kexec |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 91 | then |
| 92 | $1 -f -e |
| 93 | else |
| 94 | $1 -f |
| 95 | fi |
| 96 | fi |
| 97 | |
| 98 | |
Andrew Jeffery | 6d1479b | 2016-11-22 14:33:23 +1030 | [diff] [blame] | 99 | echo "Execute ${1-reboot} -f if all unmounted ok, or exec /init" |
Milton D. Miller II | 0e77514 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 100 | |
| 101 | export PS1=shutdown-sh#\ |
| 102 | exec /bin/sh |