blob: 8d5c6726c9ba20576b40a84f63b4d0e2df1bc697 [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 D. Miller IIaa305252016-02-28 18:13:58 -060033update=/run/initramfs/update
Milton Millerd93b48e2016-02-05 11:19:58 -060034image=/run/initramfs/image-
Milton D. Miller IIaa305252016-02-28 18:13:58 -060035
Milton Millerd93b48e2016-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 IIbf7bfd42016-01-27 20:18:16 -060037 ! cmp /run/mtd:u-boot-env /run/fw_env
38then
Milton Millerd93b48e2016-02-05 11:19:58 -060039 ln -sn /run/fw_env ${image}u-boot-env
Milton D. Miller IIbf7bfd42016-01-27 20:18:16 -060040fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060041
Milton D. Miller II5b82a712016-02-28 18:06:46 -060042if ls $image* > /dev/null 2>&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060043then
Milton D. Miller IIaa305252016-02-28 18:13:58 -060044 if test -x $update
Milton D. Miller II5b82a712016-02-28 18:06:46 -060045 then
Milton D. Miller IIbaf190a2016-02-29 11:22:06 -060046 $update --clean-saved-files
Milton Millera52546e2016-05-23 18:44:30 -050047 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 II5b82a712016-02-28 18:06:46 -060055 else
Milton D. Miller IIaa305252016-02-28 18:13:58 -060056 echo 1>&2 "Flash update requested but $update program missing!"
Milton D. Miller II5b82a712016-02-28 18:06:46 -060057 fi
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060058fi
59
60echo Remaining mounts:
61cat /proc/mounts
62
Milton D. Miller II61baed02016-02-28 17:08:50 -060063test "$umount_proc" && umount /proc && rmdir /proc
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060064
Milton D. Miller IIafb85252016-02-28 17:18:44 -060065# tcsattr(tty, TIOCDRAIN, mode) to drain tty messages to console
Milton D. Miller II12ee93b2016-02-24 12:04:05 -060066test -t 1 && stty cooked 0<&1
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060067
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060068# Execute the command systemd told us to ...
69if test -d /oldroot && test "$1"
70then
Milton D. Miller IIf4d9bc52016-02-28 17:23:04 -060071 if test "$1" = kexec
Milton D. Miller IId89d5e02016-01-20 14:57:54 -060072 then
73 $1 -f -e
74 else
75 $1 -f
76 fi
77fi
78
79
80echo "Execute ${1-reboot} -f if all unounted ok, or exec /init"
81
82export PS1=shutdown-sh#\
83exec /bin/sh