]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/rc.shutdown
- shorter lvm version detect
[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 rc_splash "reboot"
24
25 # Kill all processes.
26 [ "${BASH+bash}" = bash ] && enable kill
27
28 runlevel=$1
29 previous=$2
30
31 # Stop blogd before umounting /var
32 if [ -x /sbin/blogd ]; then
33         killall -q -QUIT blogd
34 fi
35
36 # check for other processes.
37 # there could be none if all services were terminated properly
38 # pgrep -g 0 requires procps >= 3.2.6-1.1
39 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
40 if [ -n "$pids" ]; then
41         run_cmd "Sending all processes the TERM signal" killall5 -15
42 fi
43
44 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
45 if [ -n "$pids" ]; then
46         sleep 5
47         run_cmd "Sending all processes the KILL signal" killall5 -9
48 fi
49
50 # Write to wtmp file before unmounting /var
51 halt -w
52
53 if ! is_yes "$VSERVER"; then
54         # Turn off swap, then unmount file systems.
55         run_cmd "Turning off swap" swapoff -a
56
57         [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop
58
59         if [ -x /sbin/quotaoff ]; then
60                 run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
61         fi
62
63         # Unmount file systems, killing processes if we have to.
64         sig=-15
65         retry=3
66         force=
67         if [ -z "$UMOUNT_IGNORE" ]; then
68                 UMOUNT_IGNORE="/"
69         else    
70                 UMOUNT_IGNORE="/ $UMOUNT_IGNORE"
71         fi      
72         remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
73                         $3 !~ /^(proc|loopfs|devpts|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
74                         && $1 !~ /^(none|\/dev\/root)$/ \
75                         && $2 !~ ig {print $2}' /proc/mounts)
76         while [ -n "$remaining" -a "$retry" -gt 0 ]; do
77                 show "Unmounting file systems"; busy
78                 ERRORS=$(umount -a $force -t noproc 2>&1); rc=$?
79
80                 # we might had unmounted /usr, recheck $TPUT availability
81                 # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
82                 # TODO: look ahead the messages?
83                 if is_yes "$TPUT"; then
84                         TPUT=
85                         rc_gettext_init
86                 fi
87
88                 if [ $rc = 0 ]; then
89                         ok
90                 else
91                         fail
92                         [ -n "$ERRORS" ] && echo "$ERRORS"
93                 fi
94
95                 sleep 2
96                 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
97                                 $3 !~ /^(proc|loopfs|devpts|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
98                                 && $1 !~ /^(none|\/dev\/root)$/ \
99                                 && $2 !~ ig {print $2}' /proc/mounts)
100                 [ -z "$remaining" ] && break
101                 fuser -k -m $sig $remaining > /dev/null
102                 sleep 5
103                 retry=$(($retry-1))
104                 sig=-9
105                 force="-f"
106         done
107
108         run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
109
110         if is_yes "$EVMS_LVM" || [ -x /sbin/vgchange ]; then
111                 lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
112                 if [ "$lvmversion" = "2" ] ; then
113                         lvmignorelock="--ignorelockingfailure"
114                 else
115                         lvmignorelock=""
116                 fi
117                 run_cmd "Stopping LVM volume groups" /sbin/vgchange -a n $lvmignorelock
118         fi
119
120         goraidtab=1
121         if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
122                 if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then
123                         /sbin/mdadm --stop --scan > /dev/null 2>&1
124                         rc=$?
125                         [ "$rc" -eq 0 ] && goraidtab=0
126                 fi
127         fi
128
129         # turn off raid
130         if [ -x /sbin/raidstop -a -f /etc/raidtab -a "$goraidtab" -eq 1 ]; then
131                 # we can not use raidstop -a here because this will only stop
132                 # devices listed in the default config file which is not always
133                 # the case. So we look only for the active raid devices
134                 if [ -f /proc/mdstat ] ; then
135                         mddevs=$(awk '/^md.* active/ {print $1}' /proc/mdstat)
136                         for mddev in $mddevs ; do
137                                 show "Turning off RAID for %s" "$mddev"
138                                 daemon /sbin/raidstop /dev/$mddev
139                         done
140                         unset mddev mddevs
141                 fi
142                 # runcmd "Turning off RAID" /sbin/raidstop -a
143         fi
144
145         show "Remounting remaining filesystems ro mode"; busy
146         if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
147                 ok
148         else
149                 fail
150         fi
151         _rebootwhat="system"
152 else
153         _rebootwhat="vserver"
154 fi
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" ] && eval 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         [ -x /sbin/kexec ] && /sbin/kexec -e
188         [ "$previous" != "unknown" ] && eval reboot -d -f
189 fi
This page took 0.045533 seconds and 4 git commands to generate.