3 # shutdown Common script for system halt/reboot.
5 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
10 PATH=/sbin:/bin:/usr/bin:/usr/sbin
17 . /etc/rc.d/init.d/functions
19 # avoid keyboard interruption
20 trap "echo" INT SEGV QUIT TERM
23 emit pld.shutdown-started
24 emit starting JOB=shutdown
25 emit started JOB=shutdown
30 [ "${BASH+bash}" = bash ] && enable kill
35 # Stop blogd before umounting /var
36 if [ -x /sbin/blogd ]; then
37 killall -q -QUIT blogd
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
43 # XXX: pgrep is on /usr!
44 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
45 if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
46 run_cmd "Sending all processes the TERM signal" killall5 -15
49 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
50 if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
52 run_cmd "Sending all processes the KILL signal" killall5 -9
55 # Write to wtmp file before unmounting /var
58 if ! is_yes "$VSERVER"; then
59 # Turn off swap, then unmount file systems.
60 run_cmd "Turning off swap" swapoff -a
62 [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop
64 if [ -x /sbin/quotaoff ]; then
65 run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
68 # grab kexec_loaded state before we umount /sys
69 kexec_loaded=`cat /sys/kernel/kexec_loaded 2>/dev/null`
71 # Unmount file systems, killing processes if we have to.
75 if [ -z "$UMOUNT_IGNORE" ]; then
78 UMOUNT_IGNORE="/ $UMOUNT_IGNORE"
80 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
81 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
82 && $1 !~ /^(none|\/dev\/root)$/ \
83 && $2 !~ ig {print $2}' /proc/mounts)
84 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
85 show "Unmounting file systems"; busy
86 ERRORS=$(umount -a $force -t noproc 2>&1); rc=$?
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
100 [ -n "$ERRORS" ] && echo "$ERRORS"
104 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
105 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
106 && $1 !~ /^(none|\/dev\/root)$/ \
107 && $2 !~ ig {print $2}' /proc/mounts)
108 [ -z "$remaining" ] && break
109 fuser -k -m $sig $remaining > /dev/null
116 run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
118 if [ -x /sbin/vgchange ]; then
119 lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
120 if [ "$lvmversion" = "2" ]; then
121 lvmsysinit="--sysinit"
125 /sbin/vgchange -a n $lvmsysinit > /dev/null 2>&1
129 if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
130 if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then
131 /sbin/mdadm --stop --scan > /dev/null 2>&1
133 [ "$rc" -eq 0 ] && goraidtab=0
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
150 # runcmd "Turning off RAID" /sbin/raidstop -a
153 show "Remounting remaining filesystems ro mode"; busy
154 if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
161 _rebootwhat="vserver"
164 if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
165 . /etc/rc.d/init.d/cryptsetup
167 show "Stopping disk encryption"
168 halt_crypto && ok || fail
171 if [ "$runlevel" = "0" ] ; then
172 show "The $_rebootwhat is halted"; ok
173 if [ -f /fastboot ]; then
174 show "On the next boot fsck will be skipped."; ok
177 if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
179 if is_yes "$POWEROFF_UPS"; then
180 if [ -d /etc/ups.d ]; then
181 for i in /etc/ups.d/*.sh; do
182 [ ! -f $i -o ! -x $i ] && continue
186 [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups
189 [ "$previous" != "unknown" ] && eval halt -d -p -f
191 show "Please stand by while rebooting the $_rebootwhat"; ok
192 if [ -f /fastboot ]; then
193 show "On the next boot fsck will be skipped."; ok
196 if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then
197 show "Will now restart with kexec"
202 [ "$previous" != "unknown" ] && eval reboot -d -f