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