blob: ddea48c7d7ea273d705dad65c1560c29f6576068 [file] [log] [blame]
Milton D. Miller II0e775142016-01-20 14:57:54 -06001#!/bin/sh
2
3echo shutdown: "$@"
4
Patrick Williams51d6af42023-04-14 14:19:55 -05005export PS1="shutdown-sh# "
Milton D. Miller II0e775142016-01-20 14:57:54 -06006# 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.
Milton Millerf53fdb02023-10-11 14:41:25 -050022# Reverse sort order will ensure the overlay is unmounted before the loop mount
Milton Miller7d9e2e42016-02-05 17:26:04 -060023mkdir -p /mnt
24mount --move /oldroot/run /mnt
25
Milton Millerf53fdb02023-10-11 14:41:25 -050026# Unmount paths with /oldroot /mnt under / and those ending with ro or rw
27# Use . to match any single character because busybox awk doesn't handle [/]
Milton Millerf53fdb02023-10-11 14:41:25 -050028awk '$2 ~ /^.oldroot|^.mnt|.r[ow]$/ { print $2 }' < /proc/mounts | sort -r | while IFS= read -r f
Milton D. Miller II0e775142016-01-20 14:57:54 -060029do
Zev Weiss31ffcfb2023-11-09 01:38:07 +000030 echo "Unmounting $f"
Patrick Williams51d6af42023-04-14 14:19:55 -050031 umount "$f"
Milton D. Miller II0e775142016-01-20 14:57:54 -060032done
Milton D. Miller II0e775142016-01-20 14:57:54 -060033
Milton D. Miller II648f0a82016-02-28 18:13:58 -060034update=/run/initramfs/update
Milton Miller36d336c2016-02-05 11:19:58 -060035image=/run/initramfs/image-
Milton D. Miller II648f0a82016-02-28 18:13:58 -060036
Milton Miller589ebdf2017-07-20 14:05:26 -050037wdt="-t 1 -T 5"
38wdrst="-T 15"
39
Milton D. Miller II042e2372016-02-28 18:06:46 -060040if ls $image* > /dev/null 2>&1
Milton D. Miller II0e775142016-01-20 14:57:54 -060041then
Milton D. Miller II648f0a82016-02-28 18:13:58 -060042 if test -x $update
Milton D. Miller II042e2372016-02-28 18:06:46 -060043 then
Milton Miller589ebdf2017-07-20 14:05:26 -050044 if test -c /dev/watchdog
45 then
46 echo Pinging watchdog ${wdt+with args $wdt}
Paul Fertser321a95f2023-04-25 07:51:27 +000047 # shellcheck disable=SC2086
48 watchdog $wdt -F /dev/watchdog &
Milton Miller589ebdf2017-07-20 14:05:26 -050049 wd=$!
50 else
51 wd=
52 fi
Milton D. Miller II32429c42016-02-29 11:22:06 -060053 $update --clean-saved-files
Milton Miller089d5df2016-05-23 18:44:30 -050054 remaining=$(ls $image*)
55 if test -n "$remaining"
56 then
57 echo 1>&2 "Flash update failed to flash these images:"
58 echo 1>&2 "$remaining"
59 else
60 echo "Flash update completed."
61 fi
Milton Miller589ebdf2017-07-20 14:05:26 -050062
63 if test -n "$wd"
64 then
65 kill -9 $wd
66 if test -n "$wdrst"
67 then
Patrick Williams51d6af42023-04-14 14:19:55 -050068 echo "Resetting watchdog timeouts to $wdrst"
Patrick Williams80f851c2023-08-13 19:29:56 -050069 # shellcheck disable=SC2086
70 watchdog $wdrst -F /dev/watchdog &
Milton Miller589ebdf2017-07-20 14:05:26 -050071 sleep 1
72 # Kill the watchdog daemon, setting a timeout
73 # for the remaining shutdown work
74 kill -9 $!
75 fi
76 fi
Milton D. Miller II042e2372016-02-28 18:06:46 -060077 else
Milton D. Miller II648f0a82016-02-28 18:13:58 -060078 echo 1>&2 "Flash update requested but $update program missing!"
Milton D. Miller II042e2372016-02-28 18:06:46 -060079 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -060080fi
81
82echo Remaining mounts:
83cat /proc/mounts
84
Milton D. Miller II576ea812016-02-28 17:08:50 -060085test "$umount_proc" && umount /proc && rmdir /proc
Milton D. Miller II0e775142016-01-20 14:57:54 -060086
Milton D. Miller IIc67caac2016-02-28 17:18:44 -060087# tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console
Milton D. Miller IIe762a082016-02-24 12:04:05 -060088test -t 1 && stty cooked 0<&1
Milton D. Miller II0e775142016-01-20 14:57:54 -060089
Milton D. Miller II0e775142016-01-20 14:57:54 -060090# Execute the command systemd told us to ...
91if test -d /oldroot && test "$1"
92then
Milton D. Miller II35b92042016-02-28 17:23:04 -060093 if test "$1" = kexec
Milton D. Miller II0e775142016-01-20 14:57:54 -060094 then
95 $1 -f -e
96 else
97 $1 -f
98 fi
99fi
100
101
Andrew Jeffery6d1479b2016-11-22 14:33:23 +1030102echo "Execute ${1-reboot} -f if all unmounted ok, or exec /init"
Milton D. Miller II0e775142016-01-20 14:57:54 -0600103
Zev Weiss4a2d35f2023-11-15 18:00:34 +0000104export PS1="shutdown-sh# "
Milton D. Miller II0e775142016-01-20 14:57:54 -0600105exec /bin/sh