3 # shutdown Common script for system halt/reboot.
5 # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
9 PATH=/sbin:/bin:/usr/bin:/usr/sbin
16 . /etc/rc.d/init.d/functions
18 # avoid keyboard interruption
19 trap "echo" INT SEGV QUIT TERM
25 [ "${BASH+bash}" = bash ] && enable kill
30 # Stop blogd before umounting /var
31 if [ -x /sbin/blogd ]; then
32 killall -q -QUIT blogd
35 if [ -e /dev/rtc -o -e /dev/rtc0 ]; then
36 run_cmd "$(nls 'Syncing hardware clock to system time')" hwclock --systohc
39 # check for other processes.
40 # there could be none if all services were terminated properly
41 # pgrep -g 0 requires procps >= 3.2.6-1.1
42 # XXX: pgrep is on /usr!
43 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
44 if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
45 run_cmd "Sending all processes the TERM signal" killall5 -15
48 pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
49 if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
51 run_cmd "Sending all processes the KILL signal" killall5 -9
54 # Write to wtmp file before unmounting /var
57 # Turn off swap, then unmount file systems.
58 run_cmd "Turning off swap" swapoff -a
60 [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop
62 if [ -x /sbin/quotaoff ]; then
63 run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
66 # grab kexec_loaded state before we umount /sys
67 kexec_loaded=`cat /sys/kernel/kexec_loaded 2>/dev/null`
69 # Unmount file systems, killing processes if we have to.
73 if [ -z "$UMOUNT_IGNORE" ]; then
76 UMOUNT_IGNORE="/ $UMOUNT_IGNORE"
78 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
79 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
80 && $1 !~ /^(none|\/dev\/root)$/ \
81 && $2 !~ ig {print $2}' /proc/mounts)
82 while [ -n "$remaining" -a "$retry" -gt 0 ]; do
83 show "Unmounting file systems"; busy
84 ERRORS=$(umount -a $force -t noproc,nosysfs 2>&1); rc=$?
86 # we might had unmounted /usr, recheck $TPUT availability
87 # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
88 # TODO: look ahead the messages?
89 if is_yes "$TPUT"; then
98 [ -n "$ERRORS" ] && echo "$ERRORS"
102 remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
103 $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|sysfs|securityfs|squashfs)$/ \
104 && $1 !~ /^(none|\/dev\/root)$/ \
105 && $2 !~ ig {print $2}' /proc/mounts)
106 [ -z "$remaining" ] && break
107 fuser -k -m $sig $remaining > /dev/null
114 run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
116 if [ -x /sbin/vgchange ]; then
117 lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
118 if [ "$lvmversion" = "2" ]; then
119 lvmsysinit="--sysinit --noudevsync"
123 /sbin/vgchange -a n $lvmsysinit > /dev/null 2>&1
126 if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
127 if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then
128 /sbin/mdadm --stop --scan > /dev/null 2>&1
133 show "Remounting remaining filesystems ro mode"; busy
134 if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
141 if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
142 . /etc/rc.d/init.d/cryptsetup
144 show "Stopping disk encryption"
145 halt_crypto && ok || fail
148 if [ "$runlevel" = "0" ] ; then
149 show "The $_rebootwhat is halted"; ok
150 if [ -f /fastboot ]; then
151 show "On the next boot fsck will be skipped."; ok
154 if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
156 if is_yes "$POWEROFF_UPS"; then
157 if [ -d /etc/ups.d ]; then
158 for i in /etc/ups.d/*.sh; do
159 [ ! -f $i -o ! -x $i ] && continue
163 [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups
166 [ "$previous" != "unknown" ] && halt -d -p -f
168 show "Please stand by while rebooting the $_rebootwhat"; ok
169 if [ -f /fastboot ]; then
170 show "On the next boot fsck will be skipped."; ok
173 if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then
174 show "Will now restart with kexec"
179 [ "$previous" != "unknown" ] && reboot -d -f