]> TLD Linux GIT Repositories - rc-scripts.git/blob - rc.d/rc.shutdown
- skip efivarfs when unmounting file systems
[rc-scripts.git] / rc.d / rc.shutdown
1 #!/bin/sh
2 #
3 # shutdown      Common script for system halt/reboot.
4 #
5 # Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
6 #
7
8 # Set the path.
9 PATH=/sbin:/bin:/usr/bin:/usr/sbin
10
11 # move to root dir
12 cd /
13
14 IN_SHUTDOWN=yes
15
16 . /etc/rc.d/init.d/functions
17
18 # avoid keyboard interruption
19 trap "echo" INT SEGV QUIT TERM
20 set +e
21
22 rc_splash "reboot"
23
24 # Kill all processes.
25 [ "${BASH+bash}" = bash ] && enable kill
26
27 runlevel=$1
28 previous=$2
29
30 # Stop blogd before umounting /var
31 if [ -x /sbin/blogd ]; then
32         killall -q -QUIT blogd
33 fi
34
35 if [ -e /dev/rtc -o -e /dev/rtc0 ]; then
36         run_cmd "$(nls 'Syncing hardware clock to system time')" hwclock --systohc
37 fi
38
39 # check for other processes.
40 # there could be none if all services were terminated properly
41 # pgrep -g 0 requires procps >= 3.2.6-1.1
42 # XXX: pgrep is on /usr!
43 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
44 if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
45         run_cmd "Sending all processes the TERM signal" killall5 -15
46 fi
47
48 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
49 if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
50         sleep 5
51         run_cmd "Sending all processes the KILL signal" killall5 -9
52 fi
53
54 # Write to wtmp file before unmounting /var
55 halt -w
56
57 # Turn off swap, then unmount file systems.
58 run_cmd "Turning off swap" swapoff -a
59
60 [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop
61
62 if [ -x /sbin/quotaoff ]; then
63         run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
64 fi
65
66 # grab kexec_loaded state before we umount /sys
67 kexec_loaded=`cat /sys/kernel/kexec_loaded 2>/dev/null`
68
69 # Unmount file systems, killing processes if we have to.
70 sig=-15
71 retry=3
72 force=
73 if [ -z "$UMOUNT_IGNORE" ]; then
74         UMOUNT_IGNORE="/"
75 else
76         UMOUNT_IGNORE="/ $UMOUNT_IGNORE"
77 fi
78 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
79                 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|cgroup|cgroup2|sysfs|securityfs|efivarfs|squashfs)$/ \
80                 && $1 !~ /^(none|\/dev\/root)$/ \
81                 && $2 !~ ig {print $2}' /proc/mounts)
82 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
83         for mnt in $remaining; do
84                 run_cmd "$(nls 'Umounting file system: %s' $mnt)" umount $force -R $mnt
85
86                 # we might had unmounted /usr, recheck $TPUT availability
87                 # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
88                 # TODO: look ahead the messages?
89                 if is_yes "$TPUT"; then
90                     TPUT=
91                     rc_gettext_init
92                 fi
93         done
94
95         show "Unmounting remainging file systems"; busy
96         ERRORS=$(umount -a $force -t noproc,nosysfs,notmpfs,nodevpts,nodevtmpfs 2>&1); rc=$
97
98         # we might had unmounted /usr, recheck $TPUT availability
99         # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
100         # TODO: look ahead the messages?
101         if is_yes "$TPUT"; then
102                 TPUT=
103                 rc_gettext_init
104         fi
105
106         if [ $rc = 0 ]; then
107                 ok
108         else
109                 fail
110                 if [ -n "$ERRORS" ]; then
111                        nls "Umount errors:"
112                        echo "$ERRORS"
113                 fi
114         fi
115
116         sleep 2
117         remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
118                         $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|cgroup|cgroup2|sysfs|securityfs|efivarfs|squashfs)$/ \
119                         && $1 !~ /^(none|\/dev\/root)$/ \
120                         && $2 !~ ig {print $2}' /proc/mounts)
121         [ -z "$remaining" ] && break
122         fuser -k -m $sig $remaining > /dev/null
123         sleep 5
124         retry=$(($retry-1))
125         sig=-9
126         force="-f"
127 done
128
129 run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
130
131 if [ -x /sbin/vgchange ]; then
132         lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
133         if [ "$lvmversion" = "2" ]; then
134                 lvmsysinit="--sysinit --noudevsync"
135         else
136                 lvmsysinit=""
137         fi
138         /sbin/vgchange -a n $lvmsysinit > /dev/null 2>&1
139 fi
140
141 if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
142         if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then
143                 /sbin/mdadm --stop --scan > /dev/null 2>&1
144                 rc=$?
145         fi
146 fi
147
148 show "Remounting remaining filesystems ro mode"; busy
149 if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
150         ok
151 else
152         fail
153 fi
154 _rebootwhat="system"
155
156 if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
157         . /etc/rc.d/init.d/cryptsetup
158
159         show "Stopping disk encryption"
160         halt_crypto && ok || fail
161 fi
162
163 if [ "$runlevel" = "0" ] ; then
164         show "The $_rebootwhat is halted"; ok
165         if [ -f /fastboot ]; then
166                 show "On the next boot fsck will be skipped."; ok
167         fi
168
169         if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
170                 . /etc/sysconfig/ups
171                 if is_yes "$POWEROFF_UPS"; then
172                         if [ -d /etc/ups.d ]; then
173                                 for i in /etc/ups.d/*.sh; do
174                                         [ ! -f $i -o ! -x $i ] && continue
175                                         $i poweroff
176                                 done
177                         fi
178                        [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups
179                fi
180         fi
181         [ "$previous" != "unknown" ] && halt -d -p -f
182 else
183         show "Please stand by while rebooting the $_rebootwhat"; ok
184         if [ -f /fastboot ]; then
185                 show "On the next boot fsck will be skipped."; ok
186         fi
187
188         if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then
189                 show "Will now restart with kexec"
190                 /sbin/kexec -e
191                 fail
192         fi
193
194         [ "$previous" != "unknown" ] && reboot -d -f
195 fi