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 Miller | d93b48e | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 36 | if test -s /run/fw_env -a -c /run/mtd:u-boot-env -a ! -e ${image}u-boot-env && |
Milton D. Miller II | bf7bfd4 | 2016-01-27 20:18:16 -0600 | [diff] [blame] | 37 | ! cmp /run/mtd:u-boot-env /run/fw_env |
| 38 | then |
Milton Miller | d93b48e | 2016-02-05 11:19:58 -0600 | [diff] [blame] | 39 | ln -sn /run/fw_env ${image}u-boot-env |
Milton D. Miller II | bf7bfd4 | 2016-01-27 20:18:16 -0600 | [diff] [blame] | 40 | fi |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 41 | |
Milton D. Miller II | 5b82a71 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 42 | if ls $image* > /dev/null 2>&1 |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 43 | then |
Milton D. Miller II | aa30525 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 44 | if test -x $update |
Milton D. Miller II | 5b82a71 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 45 | then |
Milton D. Miller II | baf190a | 2016-02-29 11:22:06 -0600 | [diff] [blame] | 46 | $update --clean-saved-files |
Milton Miller | a52546e | 2016-05-23 18:44:30 -0500 | [diff] [blame] | 47 | remaining=$(ls $image*) |
| 48 | if test -n "$remaining" |
| 49 | then |
| 50 | echo 1>&2 "Flash update failed to flash these images:" |
| 51 | echo 1>&2 "$remaining" |
| 52 | else |
| 53 | echo "Flash update completed." |
| 54 | fi |
Milton D. Miller II | 5b82a71 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 55 | else |
Milton D. Miller II | aa30525 | 2016-02-28 18:13:58 -0600 | [diff] [blame] | 56 | echo 1>&2 "Flash update requested but $update program missing!" |
Milton D. Miller II | 5b82a71 | 2016-02-28 18:06:46 -0600 | [diff] [blame] | 57 | fi |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 58 | fi |
| 59 | |
| 60 | echo Remaining mounts: |
| 61 | cat /proc/mounts |
| 62 | |
Milton D. Miller II | 61baed0 | 2016-02-28 17:08:50 -0600 | [diff] [blame] | 63 | test "$umount_proc" && umount /proc && rmdir /proc |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 64 | |
Milton D. Miller II | afb8525 | 2016-02-28 17:18:44 -0600 | [diff] [blame] | 65 | # tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console |
Milton D. Miller II | 12ee93b | 2016-02-24 12:04:05 -0600 | [diff] [blame] | 66 | test -t 1 && stty cooked 0<&1 |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 67 | |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 68 | # Execute the command systemd told us to ... |
| 69 | if test -d /oldroot && test "$1" |
| 70 | then |
Milton D. Miller II | f4d9bc5 | 2016-02-28 17:23:04 -0600 | [diff] [blame] | 71 | if test "$1" = kexec |
Milton D. Miller II | d89d5e0 | 2016-01-20 14:57:54 -0600 | [diff] [blame] | 72 | then |
| 73 | $1 -f -e |
| 74 | else |
| 75 | $1 -f |
| 76 | fi |
| 77 | fi |
| 78 | |
| 79 | |
Andrew Jeffery | fc4c87d | 2016-11-22 14:33:23 +1030 | [diff] [blame] | 80 | 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] | 81 | |
| 82 | export PS1=shutdown-sh#\ |
| 83 | exec /bin/sh |