blob: 8d5d0c983c175fa357fb9d271dae957be7e4d2fb [file] [log] [blame]
Milton D. Miller II0e775142016-01-20 14:57:54 -06001#!/bin/sh
2
3echo shutdown: "$@"
4
5export PS1=shutdown-sh#\
6# 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.
22# Ordered before /oldroot the overlay is unmounted before the loop mount
23mkdir -p /mnt
24mount --move /oldroot/run /mnt
25
Milton D. Miller II0e775142016-01-20 14:57:54 -060026set -x
Milton Miller7d9e2e42016-02-05 17:26:04 -060027for f in $( awk '/oldroot|mnt/ { print $2 }' < /proc/mounts | sort -r )
Milton D. Miller II0e775142016-01-20 14:57:54 -060028do
29 umount $f
30done
31set +x
32
Milton D. Miller II648f0a82016-02-28 18:13:58 -060033update=/run/initramfs/update
Milton Miller36d336c2016-02-05 11:19:58 -060034image=/run/initramfs/image-
Milton D. Miller II648f0a82016-02-28 18:13:58 -060035
Milton Miller36d336c2016-02-05 11:19:58 -060036if test -s /run/fw_env -a -c /run/mtd:u-boot-env -a ! -e ${image}u-boot-env &&
Milton D. Miller II8084d742016-01-27 20:18:16 -060037 ! cmp /run/mtd:u-boot-env /run/fw_env
38then
Milton Miller36d336c2016-02-05 11:19:58 -060039 ln -sn /run/fw_env ${image}u-boot-env
Milton D. Miller II8084d742016-01-27 20:18:16 -060040fi
Milton D. Miller II0e775142016-01-20 14:57:54 -060041
Milton D. Miller II042e2372016-02-28 18:06:46 -060042if ls $image* > /dev/null 2>&1
Milton D. Miller II0e775142016-01-20 14:57:54 -060043then
Milton D. Miller II648f0a82016-02-28 18:13:58 -060044 if test -x $update
Milton D. Miller II042e2372016-02-28 18:06:46 -060045 then
Milton D. Miller II32429c42016-02-29 11:22:06 -060046 $update --clean-saved-files
Milton D. Miller II042e2372016-02-28 18:06:46 -060047 else
Milton D. Miller II648f0a82016-02-28 18:13:58 -060048 echo 1>&2 "Flash update requested but $update program missing!"
Milton D. Miller II042e2372016-02-28 18:06:46 -060049 fi
Milton D. Miller II0e775142016-01-20 14:57:54 -060050fi
51
52echo Remaining mounts:
53cat /proc/mounts
54
Milton D. Miller II576ea812016-02-28 17:08:50 -060055test "$umount_proc" && umount /proc && rmdir /proc
Milton D. Miller II0e775142016-01-20 14:57:54 -060056
Milton D. Miller IIc67caac2016-02-28 17:18:44 -060057# tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console
Milton D. Miller IIe762a082016-02-24 12:04:05 -060058test -t 1 && stty cooked 0<&1
Milton D. Miller II0e775142016-01-20 14:57:54 -060059
Milton D. Miller II0e775142016-01-20 14:57:54 -060060# Execute the command systemd told us to ...
61if test -d /oldroot && test "$1"
62then
Milton D. Miller II35b92042016-02-28 17:23:04 -060063 if test "$1" = kexec
Milton D. Miller II0e775142016-01-20 14:57:54 -060064 then
65 $1 -f -e
66 else
67 $1 -f
68 fi
69fi
70
71
72echo "Execute ${1-reboot} -f if all unounted ok, or exec /init"
73
74export PS1=shutdown-sh#\
75exec /bin/sh