]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/rc.shutdown
- emit pld.shutdown-started after blocking signals
[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 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
42 if [ -n "$pids" ]; then
43         run_cmd "Sending all processes the TERM signal" killall5 -15
44 fi
45
46 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
47 if [ -n "$pids" ]; then
48         sleep 5
49         run_cmd "Sending all processes the KILL signal" killall5 -9
50 fi
51
52 # Write to wtmp file before unmounting /var
53 halt -w
54
55 if ! is_yes "$VSERVER"; then
56         # Turn off swap, then unmount file systems.
57         run_cmd "Turning off swap" swapoff -a
58
59         [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop
60
61         if [ -x /sbin/quotaoff ]; then
62                 run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
63         fi
64
65         # grab kexec_loaded state before we umount /sys
66         kexec_loaded=`cat /sys/kernel/kexec_loaded 2>/dev/null`
67
68         # Unmount file systems, killing processes if we have to.
69         sig=-15
70         retry=3
71         force=
72         if [ -z "$UMOUNT_IGNORE" ]; then
73                 UMOUNT_IGNORE="/"
74         else
75                 UMOUNT_IGNORE="/ $UMOUNT_IGNORE"
76         fi
77         remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
78                         $3 !~ /^(proc|loopfs|devpts|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
79                         && $1 !~ /^(none|\/dev\/root)$/ \
80                         && $2 !~ ig {print $2}' /proc/mounts)
81         while [ -n "$remaining" -a "$retry" -gt 0 ]; do
82                 show "Unmounting file systems"; busy
83                 ERRORS=$(umount -a $force -t noproc 2>&1); rc=$?
84
85                 # we might had unmounted /usr, recheck $TPUT availability
86                 # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
87                 # TODO: look ahead the messages?
88                 if is_yes "$TPUT"; then
89                         TPUT=
90                         rc_gettext_init
91                 fi
92
93                 if [ $rc = 0 ]; then
94                         ok
95                 else
96                         fail
97                         [ -n "$ERRORS" ] && echo "$ERRORS"
98                 fi
99
100                 sleep 2
101                 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
102                                 $3 !~ /^(proc|loopfs|devpts|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
103                                 && $1 !~ /^(none|\/dev\/root)$/ \
104                                 && $2 !~ ig {print $2}' /proc/mounts)
105                 [ -z "$remaining" ] && break
106                 fuser -k -m $sig $remaining > /dev/null
107                 sleep 5
108                 retry=$(($retry-1))
109                 sig=-9
110                 force="-f"
111         done
112
113         run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
114
115         if [ -x /sbin/vgchange ]; then
116                 lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
117                 if [ "$lvmversion" = "2" ]; then
118                         lvmignorelock="--ignorelockingfailure"
119                 else
120                         lvmignorelock=""
121                 fi
122                 run_cmd "Stopping LVM volume groups" /sbin/vgchange -a n $lvmignorelock
123         fi
124
125         goraidtab=1
126         if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
127                 if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then
128                         /sbin/mdadm --stop --scan > /dev/null 2>&1
129                         rc=$?
130                         [ "$rc" -eq 0 ] && goraidtab=0
131                 fi
132         fi
133
134         # turn off raid
135         if [ -x /sbin/raidstop -a -f /etc/raidtab -a "$goraidtab" -eq 1 ]; then
136                 # we can not use raidstop -a here because this will only stop
137                 # devices listed in the default config file which is not always
138                 # the case. So we look only for the active raid devices
139                 if [ -f /proc/mdstat ] ; then
140                         mddevs=$(awk '/^md.* active/ {print $1}' /proc/mdstat)
141                         for mddev in $mddevs ; do
142                                 show "Turning off RAID for %s" "$mddev"
143                                 daemon /sbin/raidstop /dev/$mddev
144                         done
145                         unset mddev mddevs
146                 fi
147                 # runcmd "Turning off RAID" /sbin/raidstop -a
148         fi
149
150         show "Remounting remaining filesystems ro mode"; busy
151         if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
152                 ok
153         else
154                 fail
155         fi
156         _rebootwhat="system"
157 else
158         _rebootwhat="vserver"
159 fi
160
161 if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
162         . /etc/rc.d/init.d/cryptsetup
163
164         show "Stopping disk encryption"
165         halt_crypto && ok || fail
166 fi
167
168 if [ "$runlevel" = "0" ] ; then
169         show "The $_rebootwhat is halted"; ok
170         if [ -f /fastboot ]; then
171                 show "On the next boot fsck will be skipped."; ok
172         fi
173
174         if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
175                 . /etc/sysconfig/ups
176                 if is_yes "$POWEROFF_UPS"; then
177                         if [ -d /etc/ups.d ]; then
178                                 for i in /etc/ups.d/*.sh; do
179                                         [ ! -f $i -o ! -x $i ] && continue
180                                         $i poweroff
181                                 done
182                         fi
183                        [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups
184                fi
185         fi
186         [ "$previous" != "unknown" ] && eval halt -d -p -f
187 else
188         show "Please stand by while rebooting the $_rebootwhat"; ok
189         if [ -f /fastboot ]; then
190                 show "On the next boot fsck will be skipped."; ok
191         fi
192
193         if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then
194                 show "Will now restart with kexec"
195                 /sbin/kexec -e
196                 fail
197         fi
198
199         [ "$previous" != "unknown" ] && eval reboot -d -f
200 fi
This page took 0.040387 seconds and 4 git commands to generate.