]> git.pld-linux.org Git - projects/rc-scripts.git/blame - rc.d/rc.shutdown
localize $cmdline in parse_cmdnline
[projects/rc-scripts.git] / rc.d / rc.shutdown
CommitLineData
791f7cd2
JR
1#!/bin/sh
2#
af55fe6b 3# shutdown Common script for system halt/reboot.
791f7cd2 4#
5e6dfc29 5# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
791f7cd2 6#
ec8b15cb 7# $Id$
791f7cd2
JR
8
9# Set the path.
10PATH=/sbin:/bin:/usr/bin:/usr/sbin
11
de160ea3
AM
12# move to root dir
13cd /
14
d84e0c78
JR
15IN_SHUTDOWN=yes
16
791f7cd2
JR
17. /etc/rc.d/init.d/functions
18
b6509675
AM
19# avoid keyboard interruption
20trap "echo" INT SEGV QUIT TERM
21set +e
22
f8fa316b 23emit pld.shutdown-started
e995eca6
ER
24emit starting JOB=shutdown
25emit started JOB=shutdown
f8fa316b 26
b6509675
AM
27rc_splash "reboot"
28
791f7cd2
JR
29# Kill all processes.
30[ "${BASH+bash}" = bash ] && enable kill
31
d0f42664 32runlevel=$1
cc10e704 33previous=$2
d0f42664 34
0868c9a6 35# Stop blogd before umounting /var
b171122f
ER
36if [ -x /sbin/blogd ]; then
37 killall -q -QUIT blogd
0868c9a6
ER
38fi
39
cf360554
ER
40if [ -e /dev/rtc -o -e /dev/rtc0 ]; then
41 run_cmd "$(nls 'Syncing hardware clock to system time')" hwclock --systohc
42fi
43
74c57003
AM
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
bcff88d5 47# XXX: pgrep is on /usr!
74c57003 48pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
bcff88d5 49if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
74c57003
AM
50 run_cmd "Sending all processes the TERM signal" killall5 -15
51fi
52
53pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
bcff88d5 54if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
74c57003
AM
55 sleep 5
56 run_cmd "Sending all processes the KILL signal" killall5 -9
57fi
791f7cd2
JR
58
59# Write to wtmp file before unmounting /var
1822a5a6 60halt -w
791f7cd2 61
1aaf8c92
JR
62if ! is_yes "$VSERVER"; then
63 # Turn off swap, then unmount file systems.
64 run_cmd "Turning off swap" swapoff -a
7d7fd6c1 65
1aaf8c92 66 [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop
791f7cd2 67
1aaf8c92
JR
68 if [ -x /sbin/quotaoff ]; then
69 run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
70 fi
2eec3807 71
6b30816c
ER
72 # grab kexec_loaded state before we umount /sys
73 kexec_loaded=`cat /sys/kernel/kexec_loaded 2>/dev/null`
74
cc10e704
JR
75 # Unmount file systems, killing processes if we have to.
76 sig=-15
77 retry=3
78 force=
4cc198ff
JR
79 if [ -z "$UMOUNT_IGNORE" ]; then
80 UMOUNT_IGNORE="/"
df82ecd4 81 else
4cc198ff 82 UMOUNT_IGNORE="/ $UMOUNT_IGNORE"
df82ecd4 83 fi
4cc198ff 84 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
a0bbd9e1 85 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
2850ca3d 86 && $1 !~ /^(none|\/dev\/root)$/ \
4cc198ff 87 && $2 !~ ig {print $2}' /proc/mounts)
2a57d1ce
ER
88 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
89 show "Unmounting file systems"; busy
34c624e7 90 ERRORS=$(umount -a $force -t noproc 2>&1); rc=$?
2a57d1ce
ER
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
cc10e704
JR
101 ok
102 else
103 fail
104 [ -n "$ERRORS" ] && echo "$ERRORS"
105 fi
5e6dfc29 106
cc10e704 107 sleep 2
4cc198ff 108 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
a0bbd9e1 109 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
2850ca3d 110 && $1 !~ /^(none|\/dev\/root)$/ \
4cc198ff 111 && $2 !~ ig {print $2}' /proc/mounts)
cc10e704 112 [ -z "$remaining" ] && break
4b0d64e2 113 fuser -k -m $sig $remaining > /dev/null
cc10e704
JR
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
9bec7e9f 122 if [ -x /sbin/vgchange ]; then
0174ae89 123 lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
9bec7e9f 124 if [ "$lvmversion" = "2" ]; then
ffa93fa2 125 lvmsysinit="--sysinit"
cc10e704 126 else
ffa93fa2 127 lvmsysinit=""
cc10e704 128 fi
ffa93fa2 129 /sbin/vgchange -a n $lvmsysinit > /dev/null 2>&1
41781ded 130 fi
41781ded 131
cc10e704 132 if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
62119804 133 if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then
9045fdf6 134 /sbin/mdadm --stop --scan > /dev/null 2>&1
5e6dfc29 135 rc=$?
cc10e704 136 fi
8fb3abd8 137 fi
5e6dfc29 138
cc10e704 139 show "Remounting remaining filesystems ro mode"; busy
62119804 140 if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
cc10e704
JR
141 ok
142 else
143 fail
144 fi
2e33f3ce
JR
145 _rebootwhat="system"
146else
147 _rebootwhat="vserver"
148fi
149
6b496866 150if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
8710c4b5
ER
151 . /etc/rc.d/init.d/cryptsetup
152
153 show "Stopping disk encryption"
878e1414
ER
154 halt_crypto && ok || fail
155fi
156
d0f42664 157if [ "$runlevel" = "0" ] ; then
2e33f3ce 158 show "The $_rebootwhat is halted"; ok
62119804
ER
159 if [ -f /fastboot ]; then
160 show "On the next boot fsck will be skipped."; ok
161 fi
bc451b91 162
d0b6372d 163 if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
5e6dfc29 164 . /etc/sysconfig/ups
d0b6372d
AM
165 if is_yes "$POWEROFF_UPS"; then
166 if [ -d /etc/ups.d ]; then
03ef5439 167 for i in /etc/ups.d/*.sh; do
d0b6372d
AM
168 [ ! -f $i -o ! -x $i ] && continue
169 $i poweroff
170 done
171 fi
172 [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups
173 fi
cc10e704 174 fi
554bf775 175 [ "$previous" != "unknown" ] && halt -d -p -f
d0f42664 176else
2e33f3ce 177 show "Please stand by while rebooting the $_rebootwhat"; ok
62119804
ER
178 if [ -f /fastboot ]; then
179 show "On the next boot fsck will be skipped."; ok
180 fi
69d7c00f 181
413ed86d
ER
182 # not really sure where thse events should go
183 emit stopped JOB=shutdown
184 emit stopping JOB=shutdown
185
6b30816c
ER
186 if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then
187 show "Will now restart with kexec"
69d7c00f 188 /sbin/kexec -e
6b30816c 189 fail
69d7c00f
ER
190 fi
191
554bf775 192 [ "$previous" != "unknown" ] && reboot -d -f
d0f42664 193fi
This page took 0.132197 seconds and 4 git commands to generate.