Milton D. Miller II | d89d5e0 | 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 | fc6a317 | 2016-02-05 11:06:39 -0600 | [diff] [blame] | 8 | cd / |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 9 | if [ ! -e /proc/mounts ] |
| 10 | then |
| 11 | mkdir -p /proc |
Milton Miller | fc6a317 | 2016-02-05 11:06:39 -0600 | [diff] [blame] | 12 | mount proc /proc -tproc |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 13 | umount_proc=1 |
| 14 | else |
| 15 | umount_proc= |
| 16 | fi |
| 17 | |
Milton Miller | bf0f795 | 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 | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 19 | rmdir /oldroot 2>/dev/null |
| 20 | |
Milton Miller | bf0f795 | 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 | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 26 | set -x |
Milton Miller | bf0f795 | 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 | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 28 | do |
| 29 | umount $f |
| 30 | done |
| 31 | set +x |
| 32 | |
Milton D. Miller II | aa30525 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 33 | update=/run/initramfs/update |
Milton Miller | d93b48e | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 34 | image=/run/initramfs/image- |
Milton D. Miller II | aa30525 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 35 | |
Milton D. Miller II | 5b82a71 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 36 | if ls $image* > /dev/null 2>&1 |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 37 | then |
Milton D. Miller II | aa30525 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 38 | if test -x $update |
Milton D. Miller II | 5b82a71 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 39 | then |
Milton D. Miller II | baf190a | 2016-02-29 11:22:06 -0600 | [diff] [blame] | 40 | $update --clean-saved-files |
Milton Miller | a52546e | 2016-05-23 18:44:30 -0500 | [diff] [blame] | 41 | 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 II | 5b82a71 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 49 | else |
Milton D. Miller II | aa30525 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 50 | echo 1>&2 "Flash update requested but $update program missing!" |
Milton D. Miller II | 5b82a71 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 51 | fi |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 52 | fi |
| 53 | |
| 54 | echo Remaining mounts: |
| 55 | cat /proc/mounts |
| 56 | |
Milton D. Miller II | 61baed0 | 2016-02-28 17:08:50 -0600 | [diff] [blame] | 57 | test "$umount_proc" && umount /proc && rmdir /proc |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 58 | |
Milton D. Miller II | afb8525 | 2016-02-28 17:18:44 -0600 | [diff] [blame] | 59 | # tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console |
Milton D. Miller II | 12ee93b | 2016-02-24 12:04:05 -0600 | [diff] [blame] | 60 | test -t 1 && stty cooked 0<&1 |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 61 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 62 | # Execute the command systemd told us to ... |
| 63 | if test -d /oldroot && test "$1" |
| 64 | then |
Milton D. Miller II | f4d9bc5 | 2016-02-28 17:23:04 -0600 | [diff] [blame] | 65 | if test "$1" = kexec |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 66 | then |
| 67 | $1 -f -e |
| 68 | else |
| 69 | $1 -f |
| 70 | fi |
| 71 | fi |
| 72 | |
| 73 | |
Andrew Jeffery | fc4c87d | 2016-11-22 14:33:23 +1030 | [diff] [blame] | 74 | echo "Execute ${1-reboot} -f if all unmounted ok, or exec /init" |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 75 | |
| 76 | export PS1=shutdown-sh#\ |
| 77 | exec /bin/sh |