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