#!/bin/sh
#
-# shutdwn Common script for system halt/reboot.
+# shutdown Common script for system halt/reboot.
#
-# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
-# Modified for PLD Linux by Grzegorz Stanislawski
-# Changes: Arkadiusz Mi¶kiewicz <misiek@pld.org.pl>
+# Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#
-# $Id: rc.shutdown,v 1.11 2000/06/23 09:11:10 wiget Exp $
# Set the path.
PATH=/sbin:/bin:/usr/bin:/usr/sbin
-# NLS
-NLS_DOMAIN="rc-scripts"
-
# move to root dir
cd /
+IN_SHUTDOWN=yes
+
. /etc/rc.d/init.d/functions
+# avoid keyboard interruption
+trap "echo" INT SEGV QUIT TERM
+set +e
+
+rc_splash "reboot"
+
# Kill all processes.
[ "${BASH+bash}" = bash ] && enable kill
-run_cmd "Sending all processes the TERM signal" killall5 -15
-sleep 5
-run_cmd "Sending all processes the KILL signal" killall5 -9
+runlevel=$1
+previous=$2
+
+# Stop blogd before umounting /var
+if [ -x /sbin/blogd ]; then
+ killall -q -QUIT blogd
+fi
+
+if [ -e /dev/rtc -o -e /dev/rtc0 ]; then
+ run_cmd "$(nls 'Syncing hardware clock to system time')" hwclock --systohc
+fi
+
+# check for other processes.
+# there could be none if all services were terminated properly
+# pgrep -g 0 requires procps >= 3.2.6-1.1
+# XXX: pgrep is on /usr!
+pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
+if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
+ run_cmd "Sending all processes the TERM signal" killall5 -15
+fi
+
+pids=$(pgrep -g 0 -l -v | grep -v '^1 ')
+if [ -n "$pids" ] || [ ! -x /usr/bin/pgrep ]; then
+ sleep 5
+ run_cmd "Sending all processes the KILL signal" killall5 -9
+fi
# Write to wtmp file before unmounting /var
halt -w
-# Turn off swap, then unmount file systems.
-run_cmd "Turning off swap and accounting" swapoff -a
-[ -x /sbin/accton ] && /sbin/accton
+if ! is_yes "$VSERVER"; then
+ # Turn off swap, then unmount file systems.
+ run_cmd "Turning off swap" swapoff -a
-if [ -x /sbin/quotaoff ]; then
- run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
-fi
+ [ -x /etc/rc.d/rc.acct ] && /etc/rc.d/rc.acct stop
+
+ if [ -x /sbin/quotaoff ]; then
+ run_cmd "Turning off quotas for local filesystems" /sbin/quotaoff -a
+ fi
-# Unmount file systems, killing processes if we have to.
-sig=-15
-retry=3
-force=
-remaining=`awk '!/(^#| proc | loopfs | devfs | devpts | shm |^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts`
-while [ -n "$remaining" -a "$retry" -gt 0 ]
-do
- show "Unmounting file systems" ; busy
- if ERRORS=`LC_ALL=C exec umount -a $FORCE -t noproc 2>&1` ; then
- deltext ; ok
+ # grab kexec_loaded state before we umount /sys
+ kexec_loaded=`cat /sys/kernel/kexec_loaded 2>/dev/null`
+
+ # Unmount file systems, killing processes if we have to.
+ sig=-15
+ retry=3
+ force=
+ if [ -z "$UMOUNT_IGNORE" ]; then
+ UMOUNT_IGNORE="/"
else
- deltext ; fail
- [ -n "$ERRORS" ] && echo "$ERRORS"
+ UMOUNT_IGNORE="/ $UMOUNT_IGNORE"
fi
-
- sleep 2
- remaining=`awk '!/(^#| proc | loopfs | devfs | devpts | shm |^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts`
- [ -z "$remaining" ] && break
- /sbin/fuser -k -m $sig $remaining > /dev/null
- sleep 5
- retry=$(($retry-1))
- sig=-9
- force="-f"
-done
-
-run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
-
-# turn off raid
-if [ -x /sbin/raidstop -a -f /etc/raidtab ]; then
- # we can not use raidstop -a here because this will only stop
- # devices listed in the default config file which is not always
- # the case. So we look only for the active raid devices
- if [ -f /proc/mdstat ] ; then
- mddevs=$(awk '/^md.* active/ {print $1}' /proc/mdstat)
- for mddev in $mddevs ; do
- MESSAGE="`nls "Turning off RAID for %s" "$mddev"`"
- run_cmd "$MESSAGE" raidstop /dev/$mddev
+ remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
+ $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|cgroup|cgroup2|sysfs|securityfs|squashfs)$/ \
+ && $1 !~ /^(none|\/dev\/root)$/ \
+ && $2 !~ ig {print $2}' /proc/mounts)
+ while [ -n "$remaining" -a "$retry" -gt 0 ]; do
+ for mnt in $remaining; do
+ run_cmd "$(nls 'Umounting file system: %s' $mnt)" umount $force -R $mnt
+
+ # we might had unmounted /usr, recheck $TPUT availability
+ # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
+ # TODO: look ahead the messages?
+ if is_yes "$TPUT"; then
+ TPUT=
+ rc_gettext_init
+ fi
done
- unset mddev mddevs
+
+ show "Unmounting remainging file systems"; busy
+ ERRORS=$(umount -a $force -t noproc,nosysfs,notmpfs,nodevpts,nodevtmpfs 2>&1); rc=$?
+
+ # we might had unmounted /usr, recheck $TPUT availability
+ # but well. we need tput only for show() and busy() (ok() and fail() messages are already cached)
+ # TODO: look ahead the messages?
+ if is_yes "$TPUT"; then
+ TPUT=
+ rc_gettext_init
+ fi
+
+ if [ $rc = 0 ]; then
+ ok
+ else
+ fail
+ if [ -n "$ERRORS" ]; then
+ nls "Umount errors:"
+ echo "$ERRORS"
+ fi
+ fi
+
+ sleep 2
+ remaining=$(awk -v ig="^($UMOUNT_IGNORE)$" 'BEGIN { gsub(/[\t ]+/, "|", ig); } \
+ $3 !~ /^(proc|loopfs|devpts|devtmpfs|shm|iso9660|ramfs|tmpfs|cgroup|cgroup2|sysfs|securityfs|squashfs)$/ \
+ && $1 !~ /^(none|\/dev\/root)$/ \
+ && $2 !~ ig {print $2}' /proc/mounts)
+ [ -z "$remaining" ] && break
+ fuser -k -m $sig $remaining > /dev/null
+ sleep 5
+ retry=$(($retry-1))
+ sig=-9
+ force="-f"
+ done
+
+ run_cmd "Remounting root filesystem in ro mode" mount -n -o remount,ro /
+
+ if [ -x /sbin/vgchange ]; then
+ lvmversion=$(LC_ALL=C /sbin/vgchange --version 2>/dev/null | awk '/LVM version:/{if ($3 >= 2) print "2"}')
+ if [ "$lvmversion" = "2" ]; then
+ lvmsysinit="--sysinit --noudevsync"
+ else
+ lvmsysinit=""
+ fi
+ /sbin/vgchange -a n $lvmsysinit > /dev/null 2>&1
+ fi
+
+ if [ -x /sbin/mdadm -a -f /etc/mdadm.conf ]; then
+ if grep -qE "^([[:blank:]]|)ARRAY[[:blank:]]" /etc/mdadm.conf 2>/dev/null; then
+ /sbin/mdadm --stop --scan > /dev/null 2>&1
+ rc=$?
+ fi
fi
- #runcmd "Turning off RAID" /sbin/raidstop -a
+
+ show "Remounting remaining filesystems ro mode"; busy
+ if mount | awk '/ext2|ext3|reiserfs|xfs|jfs/ { print $3 }' | while read line; do mount -n -o ro,remount $line; done; then
+ ok
+ else
+ fail
+ fi
+ _rebootwhat="system"
+else
+ _rebootwhat="vserver"
fi
-show "Remounting remaining filesystems ro mode"; busy
-if ( mount | awk '/ext2/ { print $3 }' | \
- while read line; do
- mount -n -o ro,remount $line; done ); then
- deltext; ok
+if [ -f /etc/crypttab ] && ! is_empty_file /etc/crypttab; then
+ . /etc/rc.d/init.d/cryptsetup
+
+ show "Stopping disk encryption"
+ halt_crypto && ok || fail
+fi
+
+if [ "$runlevel" = "0" ] ; then
+ show "The $_rebootwhat is halted"; ok
+ if [ -f /fastboot ]; then
+ show "On the next boot fsck will be skipped."; ok
+ fi
+
+ if [ -f /etc/killpower -a -f /etc/sysconfig/ups ] ; then
+ . /etc/sysconfig/ups
+ if is_yes "$POWEROFF_UPS"; then
+ if [ -d /etc/ups.d ]; then
+ for i in /etc/ups.d/*.sh; do
+ [ ! -f $i -o ! -x $i ] && continue
+ $i poweroff
+ done
+ fi
+ [ -x /sbin/poweroff-ups ] && /sbin/poweroff-ups
+ fi
+ fi
+ [ "$previous" != "unknown" ] && halt -d -p -f
else
- deltext; fail
+ show "Please stand by while rebooting the $_rebootwhat"; ok
+ if [ -f /fastboot ]; then
+ show "On the next boot fsck will be skipped."; ok
+ fi
+
+ if [ -x /sbin/kexec ] && [ "$kexec_loaded" = "1" ]; then
+ show "Will now restart with kexec"
+ /sbin/kexec -e
+ fail
+ fi
+
+ [ "$previous" != "unknown" ] && reboot -d -f
fi