xref: /openbmc/openbmc/meta-phosphor/recipes-phosphor/initrdscripts/files/obmc-shutdown.sh (revision 4a2d35f54b2e3fdd88a1f99254e293a0699fb21d)
1#!/bin/sh
2
3echo shutdown: "$@"
4
5export PS1="shutdown-sh# "
6# exec bin/sh
7
8cd /
9if [ ! -e /proc/mounts ]
10then
11	mkdir -p /proc
12	mount  proc /proc -tproc
13	umount_proc=1
14else
15	umount_proc=
16fi
17
18# Remove an empty oldroot, that means we are not invoked from systemd-shutdown
19rmdir /oldroot 2>/dev/null
20
21# Move /oldroot/run to /mnt in case it has the underlying rofs loop mounted.
22# Reverse sort order will ensure the overlay is unmounted before the loop mount
23mkdir -p /mnt
24mount --move /oldroot/run /mnt
25
26# 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 [/]
28awk '$2 ~ /^.oldroot|^.mnt|.r[ow]$/ { print $2 }' < /proc/mounts | sort -r | while IFS= read -r f
29do
30	echo "Unmounting $f"
31	umount "$f"
32done
33
34update=/run/initramfs/update
35image=/run/initramfs/image-
36
37wdt="-t 1 -T 5"
38wdrst="-T 15"
39
40if ls $image* > /dev/null 2>&1
41then
42	if test -x $update
43	then
44		if test -c /dev/watchdog
45		then
46			echo Pinging watchdog ${wdt+with args $wdt}
47			# shellcheck disable=SC2086
48			watchdog $wdt -F /dev/watchdog &
49			wd=$!
50		else
51			wd=
52		fi
53		$update --clean-saved-files
54		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
62
63		if test -n "$wd"
64		then
65			kill -9 $wd
66			if test -n "$wdrst"
67			then
68				echo "Resetting watchdog timeouts to $wdrst"
69				# shellcheck disable=SC2086
70				watchdog $wdrst -F /dev/watchdog &
71				sleep 1
72				# Kill the watchdog daemon, setting a timeout
73				# for the remaining shutdown work
74				kill -9 $!
75			fi
76		fi
77	else
78		echo 1>&2 "Flash update requested but $update program missing!"
79	fi
80fi
81
82echo Remaining mounts:
83cat /proc/mounts
84
85test "$umount_proc" && umount /proc && rmdir /proc
86
87# tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console
88test -t 1 && stty cooked 0<&1
89
90# Execute the command systemd told us to ...
91if test -d /oldroot  && test "$1"
92then
93	if test "$1" = kexec
94	then
95		$1 -f -e
96	else
97		$1 -f
98	fi
99fi
100
101
102echo "Execute ${1-reboot} -f if all unmounted ok, or exec /init"
103
104export PS1="shutdown-sh# "
105exec /bin/sh
106