blob: 90e5dfbeb4588ba2e69d17906fcd2a03da3620c5 [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 D. Miller II5b82a712016-02-28 18:06:46 -060036if ls $image* > /dev/null 2>&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060037then
Milton D. Miller IIaa305252016-02-28 18:13:58 -060038 if test -x $update
Milton D. Miller II5b82a712016-02-28 18:06:46 -060039 then
Milton D. Miller IIbaf190a2016-02-29 11:22:06 -060040 $update --clean-saved-files
Milton Millera52546e2016-05-23 18:44:30 -050041 remaining=$(ls $image*)
42 if test -n "$remaining"
43 then
44 echo 1>&2 "Flash update failed to flash these images:"
45 echo 1>&2 "$remaining"
46 else
47 echo "Flash update completed."
48 fi
Milton D. Miller II5b82a712016-02-28 18:06:46 -060049 else
Milton D. Miller IIaa305252016-02-28 18:13:58 -060050 echo 1>&2 "Flash update requested but $update program missing!"
Milton D. Miller II5b82a712016-02-28 18:06:46 -060051 fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060052fi
53
54echo Remaining mounts:
55cat /proc/mounts
56
Milton D. Miller II61baed02016-02-28 17:08:50 -060057test "$umount_proc" && umount /proc && rmdir /proc
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060058
Milton D. Miller IIafb85252016-02-28 17:18:44 -060059# tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console
Milton D. Miller II12ee93b2016-02-24 12:04:05 -060060test -t 1 && stty cooked 0<&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060061
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060062# Execute the command systemd told us to ...
63if test -d /oldroot && test "$1"
64then
Milton D. Miller IIf4d9bc52016-02-28 17:23:04 -060065 if test "$1" = kexec
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060066 then
67 $1 -f -e
68 else
69 $1 -f
70 fi
71fi
72
73
Andrew Jefferyfc4c87d2016-11-22 14:33:23 +103074echo "Execute ${1-reboot} -f if all unmounted ok, or exec /init"
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060075
76export PS1=shutdown-sh#\
77exec /bin/sh