]> git.pld-linux.org Git - projects/rc-scripts.git/blob - rc.d/rc.shutdown
- cleanup vi:syntax, not needed and doesn't work anyway (should be vim:ft if any)
[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         remaining=$(awk '!/(^#| proc | loopfs | devpts | shm | iso9660 | ramfs | tmpfs | sysfs | securityfs | squashfs |^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts)
68         while [ -n "$remaining" -a "$retry" -gt 0 ]; do
69                 show "Unmounting file systems"; busy
70                 ERRORS=$(umount -a $force -t noproc 2>&1); rc=$?
71
72                 # we might had unmounted /usr, recheck $TPUT availability
73                 # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
74                 # TODO: look ahead the messages?
75                 if is_yes "$TPUT"; then
76                         TPUT=
77                         rc_gettext_init
78                 fi
79
80                 if [ $rc = 0 ]; then
81                         ok
82                 else
83                         fail
84                         [ -n "$ERRORS" ] && echo "$ERRORS"
85                 fi
86
87                 sleep 2
88                 remaining=$(awk '!/(^#| proc | loopfs | devpts | shm | iso9660 | ramfs | tmpfs | sysfs | securityfs | squashfs |^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts)
89                 [ -z "$remaining" ] && break
90                 fuser -k -m $sig $remaining > /dev/null
91                 sleep 5
92                 retry=$(($retry-1))
93                 sig=-9
94                 force="-f"
95         done
96
97         run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
98
99         if is_yes "$EVMS_LVM" || [ -x /sbin/vgscan -a -x /sbin/vgchange ]; then
100                 lvmversion=$(/sbin/vgchange --version 2>/dev/null | awk '{gsub("vgchange: Logical Volume Manager ",""); gsub("LVM version:     ",""); gsub(/\..*/,"");print $1; exit}')
101                 if [ "$lvmversion" = "2" ] ; then
102                         lvmignorelock="--ignorelockingfailure"
103                 else
104                         lvmignorelock=""
105                 fi
106                 run_cmd "Stopping LVM volume groups" /sbin/vgchange -a n $lvmignorelock
107         fi
108
109         goraidtab=1
110         if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
111                 if (grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null); then
112                         /sbin/mdadm --stop --scan > /dev/null 2>&1
113                         rc=$?
114                         [ "$rc" -eq 0 ] && goraidtab=0
115                 fi
116         fi
117
118         # turn off raid
119         if [ -x /sbin/raidstop -a -f /etc/raidtab -a "$goraidtab" -eq 1 ]; then
120                 # we can not use raidstop -a here because this will only stop
121                 # devices listed in the default config file which is not always
122                 # the case. So we look only for the active raid devices
123                 if [ -f /proc/mdstat ] ; then
124                         mddevs=$(awk '/^md.* active/ {print $1}' /proc/mdstat)
125                         for mddev in $mddevs ; do
126                                 show "Turning off RAID for %s" "$mddev"
127                                 daemon /sbin/raidstop /dev/$mddev
128                         done
129                         unset mddev mddevs
130                 fi
131                 # runcmd "Turning off RAID" /sbin/raidstop -a
132         fi
133
134         show "Remounting remaining filesystems ro mode"; busy
135         if ( mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | \
136                 while read line; do
137                 mount -n -o ro,remount $line; done ); then
138                 ok
139         else
140                 fail
141         fi
142         _rebootwhat="system"
143 else
144         _rebootwhat="vserver"
145 fi
146
147 if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
148         . /etc/rc.d/init.d/cryptsetup
149
150         show "Stopping disk encryption"
151         halt_crypto && ok || fail
152 fi
153
154 if [ "$runlevel" = "0" ] ; then
155         show "The $_rebootwhat is halted"; ok
156         [ -f /fastboot ] && (show "On the next boot fsck will be skipped."; ok)
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" ] && eval halt -d -p -f
171 else
172         show "Please stand by while rebooting the $_rebootwhat"; ok
173         [ -f /fastboot ] && (show "On the next boot fsck will be skipped."; ok)
174         [ -x /sbin/kexec ] && /sbin/kexec -e
175         [ "$previous" != "unknown" ] && eval reboot -d -f
176 fi
This page took 0.036944 seconds and 3 git commands to generate.