blob: d3c8835959fdc533ec7a21e4dac344275cb8439b [file] [log] [blame]
Milton D. Miller II0e775142016-01-20 14:57:54 -06001#!/bin/sh
2
3echo shutdown: "$@"
4
5export PS1=shutdown-sh#\
6# exec bin/sh
7
Milton Miller1c0dd482016-02-05 11:06:39 -06008cd /
Milton D. Miller II0e775142016-01-20 14:57:54 -06009if [ ! -e /proc/mounts ]
10then
11 mkdir -p /proc
Milton Miller1c0dd482016-02-05 11:06:39 -060012 mount proc /proc -tproc
Milton D. Miller II0e775142016-01-20 14:57:54 -060013 umount_proc=1
14else
15 umount_proc=
16fi
17
Milton Miller7d9e2e42016-02-05 17:26:04 -060018# Remove an empty oldroot, that means we are not invoked from systemd-shutdown
Milton D. Miller II0e775142016-01-20 14:57:54 -060019rmdir /oldroot 2>/dev/null
20
Milton Miller7d9e2e42016-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 II0e775142016-01-20 14:57:54 -060026set -x
Milton Miller7d9e2e42016-02-05 17:26:04 -060027for f in $( awk '/oldroot|mnt/ { print $2 }' < /proc/mounts | sort -r )
Milton D. Miller II0e775142016-01-20 14:57:54 -060028do
29 umount $f
30done
31set +x
32
Milton D. Miller II648f0a82016-02-28 18:13:58 -060033update=/run/initramfs/update
Milton Miller36d336c2016-02-05 11:19:58 -060034image=/run/initramfs/image-
Milton D. Miller II648f0a82016-02-28 18:13:58 -060035
Milton Miller589ebdf2017-07-20 14:05:26 -050036wdt="-t 1 -T 5"
37wdrst="-T 15"
38
Milton D. Miller II042e2372016-02-28 18:06:46 -060039if ls $image* > /dev/null 2>&1
Milton D. Miller II0e775142016-01-20 14:57:54 -060040then
Milton D. Miller II648f0a82016-02-28 18:13:58 -060041 if test -x $update
Milton D. Miller II042e2372016-02-28 18:06:46 -060042 then
Milton Miller589ebdf2017-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 II32429c42016-02-29 11:22:06 -060051 $update --clean-saved-files
Milton Miller089d5df2016-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 Miller589ebdf2017-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 Mills2a85f602017-10-25 21:25:57 -050066 echo Resetting watchdog timeouts to $wdrst
Milton Miller589ebdf2017-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 II042e2372016-02-28 18:06:46 -060074 else
Milton D. Miller II648f0a82016-02-28 18:13:58 -060075 echo 1>&2 "Flash update requested but $update program missing!"
Milton D. Miller II042e2372016-02-28 18:06:46 -060076 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -060077fi
78
79echo Remaining mounts:
80cat /proc/mounts
81
Milton D. Miller II576ea812016-02-28 17:08:50 -060082test "$umount_proc" && umount /proc && rmdir /proc
Milton D. Miller II0e775142016-01-20 14:57:54 -060083
Milton D. Miller IIc67caac2016-02-28 17:18:44 -060084# tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console
Milton D. Miller IIe762a082016-02-24 12:04:05 -060085test -t 1 && stty cooked 0<&1
Milton D. Miller II0e775142016-01-20 14:57:54 -060086
Milton D. Miller II0e775142016-01-20 14:57:54 -060087# Execute the command systemd told us to ...
88if test -d /oldroot && test "$1"
89then
Milton D. Miller II35b92042016-02-28 17:23:04 -060090 if test "$1" = kexec
Milton D. Miller II0e775142016-01-20 14:57:54 -060091 then
92 $1 -f -e
93 else
94 $1 -f
95 fi
96fi
97
98
Andrew Jeffery6d1479b2016-11-22 14:33:23 +103099echo "Execute ${1-reboot} -f if all unmounted ok, or exec /init"
Milton D. Miller II0e775142016-01-20 14:57:54 -0600100
101export PS1=shutdown-sh#\
102exec /bin/sh