]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/rc.shutdown
Add allowlogin, sys-chroots, cpusets
[projects/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 # $Id$
8
9 # Set the path.
10 PATH=/sbin:/bin:/usr/bin:/usr/sbin
11
12 # move to root dir
13 cd /
14
15 IN_SHUTDOWN=yes
16
17 . /etc/rc.d/init.d/functions
18
19 # avoid keyboard interruption
20 trap "echo" INT SEGV QUIT TERM
21 set +e
22
23 emit pld.shutdown-started
24
25 rc_splash "reboot"
26
27 # Kill all processes.
28 [ "${BASH+bash}" = bash ] && enable kill
29
30 runlevel=$1
31 previous=$2
32
33 # Stop blogd before umounting /var
34 if [ -x /sbin/blogd ]; then
35         killall -q -QUIT blogd
36 fi
37
38 # check for other processes.
39 # there could be none if all services were terminated properly
40 # pgrep -g 0 requires procps >= 3.2.6-1.1
41 # XXX: pgrep is on /usr!
42 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
43 if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
44         run_cmd "Sending all processes the TERM signal" killall5 -15
45 fi
46
47 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
48 if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
49         sleep 5
50         run_cmd "Sending all processes the KILL signal" killall5 -9
51 fi
52
53 # Write to wtmp file before unmounting /var
54 halt -w
55
56 if ! is_yes "$VSERVER"; then
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|sysfs|securityfs|squashfs)$/ \
80                         && $1 !~ /^(none|\/dev\/root)$/ \
81                         && $2 !~ ig {print $2}' /proc/mounts)
82         while [ -n "$remaining" -a "$retry" -gt 0 ]; do
83                 show "Unmounting file systems"; busy
84                 ERRORS=$(umount -a $force -t noproc 2>&1); rc=$?
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
94                 if [ $rc = 0 ]; then
95                         ok
96                 else
97                         fail
98                         [ -n "$ERRORS" ] && echo "$ERRORS"
99                 fi
100
101                 sleep 2
102                 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
103                                 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
104                                 && $1 !~ /^(none|\/dev\/root)$/ \
105                                 && $2 !~ ig {print $2}' /proc/mounts)
106                 [ -z "$remaining" ] && break
107                 fuser -k -m $sig $remaining > /dev/null
108                 sleep 5
109                 retry=$(($retry-1))
110                 sig=-9
111                 force="-f"
112         done
113
114         run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
115
116         if [ -x /sbin/vgchange ]; then
117                 lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
118                 if [ "$lvmversion" = "2" ]; then
119                         lvmignorelock="--ignorelockingfailure"
120                 else
121                         lvmignorelock=""
122                 fi
123                 run_cmd "Stopping LVM volume groups" /sbin/vgchange -a n $lvmignorelock
124         fi
125
126         goraidtab=1
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                         [ "$rc" -eq 0 ] && goraidtab=0
132                 fi
133         fi
134
135         # turn off raid
136         if [ -x /sbin/raidstop -a -f /etc/raidtab -a "$goraidtab" -eq 1 ]; then
137                 # we can not use raidstop -a here because this will only stop
138                 # devices listed in the default config file which is not always
139                 # the case. So we look only for the active raid devices
140                 if [ -f /proc/mdstat ] ; then
141                         mddevs=$(awk '/^md.* active/ {print $1}' /proc/mdstat)
142                         for mddev in $mddevs ; do
143                                 show "Turning off RAID for %s" "$mddev"
144                                 daemon /sbin/raidstop /dev/$mddev
145                         done
146                         unset mddev mddevs
147                 fi
148                 # runcmd "Turning off RAID" /sbin/raidstop -a
149         fi
150
151         show "Remounting remaining filesystems ro mode"; busy
152         if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
153                 ok
154         else
155                 fail
156         fi
157         _rebootwhat="system"
158 else
159         _rebootwhat="vserver"
160 fi
161
162 if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
163         . /etc/rc.d/init.d/cryptsetup
164
165         show "Stopping disk encryption"
166         halt_crypto && ok || fail
167 fi
168
169 if [ "$runlevel" = "0" ] ; then
170         show "The $_rebootwhat is halted"; ok
171         if [ -f /fastboot ]; then
172                 show "On the next boot fsck will be skipped."; ok
173         fi
174
175         if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
176                 . /etc/sysconfig/ups
177                 if is_yes "$POWEROFF_UPS"; then
178                         if [ -d /etc/ups.d ]; then
179                                 for i in /etc/ups.d/*.sh; do
180                                         [ ! -f $i -o ! -x $i ] && continue
181                                         $i poweroff
182                                 done
183                         fi
184                        [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups
185                fi
186         fi
187         [ "$previous" != "unknown" ] && eval halt -d -p -f
188 else
189         show "Please stand by while rebooting the $_rebootwhat"; ok
190         if [ -f /fastboot ]; then
191                 show "On the next boot fsck will be skipped."; ok
192         fi
193
194         if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then
195                 show "Will now restart with kexec"
196                 /sbin/kexec -e
197                 fail
198         fi
199
200         [ "$previous" != "unknown" ] && eval reboot -d -f
201 fi
This page took 0.035381 seconds and 3 git commands to generate.