blob: 216e7e403f7c5b6c984da9f8835a827baa0e61dc [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 Millerd93b48e2016-02-05 11:19:58 -060040if test -x /update && ls $image* > /dev/null 2>&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060041then
Milton Miller39b6faa2016-02-05 13:56:18 -060042 /update ${1+"$@"}
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060043fi
44
45echo Remaining mounts:
46cat /proc/mounts
47
Milton D. Miller II61baed02016-02-28 17:08:50 -060048test "$umount_proc" && umount /proc && rmdir /proc
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060049
Milton D. Miller II12ee93b2016-02-24 12:04:05 -060050# ioctl(TIOC_DRAIN) to drain tty messages to console
51test -t 1 && stty cooked 0<&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060052
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060053# Execute the command systemd told us to ...
54if test -d /oldroot && test "$1"
55then
56 if test "$1" == kexec
57 then
58 $1 -f -e
59 else
60 $1 -f
61 fi
62fi
63
64
65echo "Execute ${1-reboot} -f if all unounted ok, or exec /init"
66
67export PS1=shutdown-sh#\
68exec /bin/sh