]> TLD Linux GIT Repositories - rc-scripts.git/blob - rc.d/rc.shutdown
- disable lock checks (happens on LUKS2 and fails during system boot)
[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 if ! is_yes "$VSERVER"; then
58         # Turn off swap, then unmount file systems.
59         run_cmd "Turning off swap" swapoff -a
60
61         [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop
62
63         if [ -x /sbin/quotaoff ]; then
64                 run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
65         fi
66
67         # grab kexec_loaded state before we umount /sys
68         kexec_loaded=`cat /sys/kernel/kexec_loaded 2>/dev/null`
69
70         # Unmount file systems, killing processes if we have to.
71         sig=-15
72         retry=3
73         force=
74         if [ -z "$UMOUNT_IGNORE" ]; then
75                 UMOUNT_IGNORE="/"
76         else
77                 UMOUNT_IGNORE="/ $UMOUNT_IGNORE"
78         fi
79         remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
80                         $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
81                         && $1 !~ /^(none|\/dev\/root)$/ \
82                         && $2 !~ ig {print $2}' /proc/mounts)
83         while [ -n "$remaining" -a "$retry" -gt 0 ]; do
84                 show "Unmounting file systems"; busy
85                 ERRORS=$(umount -a $force -t noproc 2>&1); rc=$?
86
87                 # we might had unmounted /usr, recheck $TPUT availability
88                 # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
89                 # TODO: look ahead the messages?
90                 if is_yes "$TPUT"; then
91                         TPUT=
92                         rc_gettext_init
93                 fi
94
95                 if [ $rc = 0 ]; then
96                         ok
97                 else
98                         fail
99                         [ -n "$ERRORS" ] && echo "$ERRORS"
100                 fi
101
102                 sleep 2
103                 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
104                                 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
105                                 && $1 !~ /^(none|\/dev\/root)$/ \
106                                 && $2 !~ ig {print $2}' /proc/mounts)
107                 [ -z "$remaining" ] && break
108                 fuser -k -m $sig $remaining > /dev/null
109                 sleep 5
110                 retry=$(($retry-1))
111                 sig=-9
112                 force="-f"
113         done
114
115         run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
116
117         if [ -x /sbin/vgchange ]; then
118                 lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
119                 if [ "$lvmversion" = "2" ]; then
120                         lvmsysinit="--sysinit"
121                 else
122                         lvmsysinit=""
123                 fi
124                 /sbin/vgchange -a n $lvmsysinit > /dev/null 2>&1
125         fi
126
127         if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
128                 if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then
129                         /sbin/mdadm --stop --scan > /dev/null 2>&1
130                         rc=$?
131                 fi
132         fi
133
134         show "Remounting remaining filesystems ro mode"; busy
135         if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
136                 ok
137         else
138                 fail
139         fi
140         _rebootwhat="system"
141 else
142         _rebootwhat="vserver"
143 fi
144
145 if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
146         . /etc/rc.d/init.d/cryptsetup
147
148         show "Stopping disk encryption"
149         halt_crypto && ok || fail
150 fi
151
152 if [ "$runlevel" = "0" ] ; then
153         show "The $_rebootwhat is halted"; ok
154         if [ -f /fastboot ]; then
155                 show "On the next boot fsck will be skipped."; ok
156         fi
157
158         if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
159                 . /etc/sysconfig/ups
160                 if is_yes "$POWEROFF_UPS"; then
161                         if [ -d /etc/ups.d ]; then
162                                 for i in /etc/ups.d/*.sh; do
163                                         [ ! -f $i -o ! -x $i ] && continue
164                                         $i poweroff
165                                 done
166                         fi
167                        [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups
168                fi
169         fi
170         [ "$previous" != "unknown" ] && halt -d -p -f
171 else
172         show "Please stand by while rebooting the $_rebootwhat"; ok
173         if [ -f /fastboot ]; then
174                 show "On the next boot fsck will be skipped."; ok
175         fi
176
177         if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then
178                 show "Will now restart with kexec"
179                 /sbin/kexec -e
180                 fail
181         fi
182
183         [ "$previous" != "unknown" ] && reboot -d -f
184 fi