blob: bae572b71d0da93a84440e6ae92ae5b47bdf6c4b [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 Millerd93b48e2016-02-05 11:19:58 -060033image=/run/initramfs/image-
34if test -s /run/fw_env -a -c /run/mtd:u-boot-env -a ! -e ${image}u-boot-env &&
Milton D. Miller IIbf7bfd42016-01-27 20:18:16 -060035 ! cmp /run/mtd:u-boot-env /run/fw_env
36then
Milton Millerd93b48e2016-02-05 11:19:58 -060037 ln -sn /run/fw_env ${image}u-boot-env
Milton D. Miller IIbf7bfd42016-01-27 20:18:16 -060038fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060039
Milton D. Miller II5b82a712016-02-28 18:06:46 -060040if ls $image* > /dev/null 2>&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060041then
Milton D. Miller II5b82a712016-02-28 18:06:46 -060042 if test -x /update
43 then
44 /update
45 else
46 echo 1>&2 "Flash update requested but /update program missing!"
47 fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060048fi
49
50echo Remaining mounts:
51cat /proc/mounts
52
Milton D. Miller II61baed02016-02-28 17:08:50 -060053test "$umount_proc" && umount /proc && rmdir /proc
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060054
Milton D. Miller IIafb85252016-02-28 17:18:44 -060055# tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console
Milton D. Miller II12ee93b2016-02-24 12:04:05 -060056test -t 1 && stty cooked 0<&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060057
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060058# Execute the command systemd told us to ...
59if test -d /oldroot && test "$1"
60then
Milton D. Miller IIf4d9bc52016-02-28 17:23:04 -060061 if test "$1" = kexec
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060062 then
63 $1 -f -e
64 else
65 $1 -f
66 fi
67fi
68
69
70echo "Execute ${1-reboot} -f if all unounted ok, or exec /init"
71
72export PS1=shutdown-sh#\
73exec /bin/sh