[1108] | 1 | #!/bin/bash |
---|
| 2 | # |
---|
| 3 | # halt This file is executed by init when it goes into runlevel |
---|
| 4 | # 0 (halt) or runlevel 6 (reboot). It kills all processes, |
---|
| 5 | # unmounts file systems and then either halts or reboots. |
---|
| 6 | # |
---|
| 7 | # Author: Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org> |
---|
| 8 | # Modified for RHS Linux by Damien Neil |
---|
| 9 | # |
---|
| 10 | |
---|
| 11 | NOLOCALE=1 |
---|
| 12 | . /etc/init.d/functions |
---|
| 13 | |
---|
| 14 | action() { |
---|
| 15 | echo -n "$1 " |
---|
| 16 | shift |
---|
| 17 | if [ "$BOOTUP" = "color" ]; then |
---|
| 18 | "$@" && echo_success || echo_failure |
---|
| 19 | else |
---|
| 20 | "$@" |
---|
| 21 | fi |
---|
| 22 | echo |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | halt_crypto() { |
---|
| 26 | fnval=0 |
---|
| 27 | while read dst src key; do |
---|
| 28 | [ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue |
---|
| 29 | if [ -b "/dev/mapper/$dst" ]; then |
---|
| 30 | if /sbin/dmsetup info "$dst" | grep -q '^Open count: *0$'; then |
---|
| 31 | /sbin/cryptsetup remove "$dst" |
---|
| 32 | else |
---|
| 33 | fnval=1 |
---|
| 34 | fi |
---|
| 35 | fi |
---|
| 36 | done < /etc/crypttab |
---|
| 37 | return $fnval |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | # See how we were called. |
---|
| 41 | case "$0" in |
---|
| 42 | *halt) |
---|
| 43 | message=$"Halting system..." |
---|
| 44 | command="/sbin/halt" |
---|
| 45 | ;; |
---|
| 46 | *reboot) |
---|
| 47 | message=$"Please stand by while rebooting the system..." |
---|
| 48 | command="/sbin/reboot" |
---|
| 49 | kexec_command="/sbin/kexec" |
---|
| 50 | ;; |
---|
| 51 | *) |
---|
| 52 | echo $"$0: call me as 'halt' or 'reboot' please!" |
---|
| 53 | exit 1 |
---|
| 54 | ;; |
---|
| 55 | esac |
---|
| 56 | case "$1" in |
---|
| 57 | *start) |
---|
| 58 | ;; |
---|
| 59 | *) |
---|
| 60 | echo $"Usage: $0 {start}" |
---|
| 61 | exit 1 |
---|
| 62 | ;; |
---|
| 63 | esac |
---|
| 64 | |
---|
| 65 | update_boot_stage kill |
---|
| 66 | |
---|
| 67 | # Kill all processes. |
---|
| 68 | [ "${BASH+bash}" = bash ] && enable kill |
---|
| 69 | |
---|
| 70 | action $"Sending all processes the TERM signal..." /sbin/killall5 -15 |
---|
| 71 | sleep 2 |
---|
| 72 | action $"Sending all processes the KILL signal..." /sbin/killall5 -9 |
---|
| 73 | |
---|
| 74 | # Write to wtmp file before unmounting /var |
---|
| 75 | /sbin/halt -w |
---|
| 76 | |
---|
| 77 | # Save random seed |
---|
| 78 | touch /var/lib/random-seed |
---|
| 79 | chmod 600 /var/lib/random-seed |
---|
| 80 | action $"Saving random seed: " dd if=/dev/urandom of=/var/lib/random-seed count=1 bs=512 2>/dev/null |
---|
| 81 | |
---|
| 82 | update_boot_stage clock |
---|
| 83 | [ -x /sbin/hwclock ] && action $"Syncing hardware clock to system time" /sbin/hwclock --systohc |
---|
| 84 | |
---|
| 85 | update_boot_stage umount |
---|
| 86 | |
---|
| 87 | # If noumount or readonlyroot is specified, skip umounting filesystems |
---|
| 88 | if ( ! egrep -q "(noumount|readonlyroot)" /proc/cmdline ); then |
---|
| 89 | |
---|
| 90 | # Try to unmount tmpfs filesystems to avoid swapping them in. Ignore failures. |
---|
| 91 | tmpfs=$(awk '$2 ~ /^\/($|proc|dev)/ { next; } |
---|
| 92 | $3 == "tmpfs" { print $2; }' /proc/mounts | sort -r) |
---|
| 93 | [ -n "$tmpfs" ] && fstab-decode umount $tmpfs 2>/dev/null |
---|
| 94 | |
---|
| 95 | # Turn off swap, then unmount file systems. |
---|
| 96 | [ -f /proc/swaps ] && SWAPS=`awk '! /^Filename/ { print $1 }' /proc/swaps` |
---|
| 97 | if [ -n "$SWAPS" ]; then |
---|
| 98 | action $"Turning off swap: " swapoff $SWAPS |
---|
| 99 | for dst in $SWAPS; do |
---|
| 100 | if [[ "$dst" =~ ^/dev/mapper ]] \ |
---|
| 101 | && [ "$(dmsetup status "$dst" | cut -d ' ' -f 3)" = crypt ]; then |
---|
| 102 | backdev=$(/sbin/cryptsetup status "$dst" \ |
---|
| 103 | | awk '$1 == "device:" { print $2 }') |
---|
| 104 | /sbin/cryptsetup remove "$dst" |
---|
| 105 | # Leave partition with a blank plain-text swap |
---|
| 106 | mkswap "$backdev" > /dev/null |
---|
| 107 | fi |
---|
| 108 | done |
---|
| 109 | fi |
---|
| 110 | |
---|
| 111 | [ -x /sbin/quotaoff ] && action $"Turning off quotas: " /sbin/quotaoff -aug |
---|
| 112 | |
---|
| 113 | # Unmount file systems, killing processes if we have to. |
---|
| 114 | # Unmount loopback stuff first |
---|
| 115 | __umount_loopback_loop |
---|
| 116 | |
---|
| 117 | # Unmount RPC pipe file systems |
---|
| 118 | __umount_loop '$3 ~ /^rpc_pipefs$/ || $3 ~ /^rpc_svc_gss_pipefs$/ {print $2}' \ |
---|
| 119 | /proc/mounts \ |
---|
| 120 | $"Unmounting pipe file systems: " \ |
---|
| 121 | $"Unmounting pipe file systems (retry): " \ |
---|
| 122 | -f |
---|
| 123 | |
---|
| 124 | LANG=C __umount_loop '$2 ~ /^\/$|^\/proc|^\/dev/{next} |
---|
| 125 | $3 == "tmpfs" || $3 == "proc" {print $2 ; next} |
---|
| 126 | /(loopfs|autofs|nfs|cifs|smbfs|ncpfs|sysfs|^none|^\/dev\/ram|^\/dev\/root$)/ {next} |
---|
| 127 | {print $2}' /proc/mounts \ |
---|
| 128 | $"Unmounting file systems: " \ |
---|
| 129 | $"Unmounting file systems (retry): " \ |
---|
| 130 | -f |
---|
| 131 | |
---|
| 132 | [ -f /proc/bus/usb/devices ] && umount /proc/bus/usb |
---|
| 133 | |
---|
| 134 | [ -f /etc/crypttab ] && \ |
---|
| 135 | LANG=C action $"Stopping disk encryption: " halt_crypto |
---|
| 136 | |
---|
| 137 | # remove the crash indicator flag |
---|
| 138 | rm -f /.autofsck |
---|
| 139 | |
---|
| 140 | update_boot_stage umount_force |
---|
| 141 | |
---|
| 142 | # Try all file systems other than root, essential filesystems and RAM disks, |
---|
| 143 | # one last time. |
---|
| 144 | awk '$2 !~ /\/(|dev|proc|selinux|sys)$/ && $1 !~ /^\/dev\/ram/ { print $2 }' \ |
---|
| 145 | /proc/mounts | sort -r | \ |
---|
| 146 | while read line; do |
---|
| 147 | fstab-decode umount -f $line |
---|
| 148 | done |
---|
| 149 | |
---|
| 150 | if [ -x /sbin/halt.local ]; then |
---|
| 151 | /sbin/halt.local |
---|
| 152 | fi |
---|
| 153 | |
---|
| 154 | update_boot_stage remount_ro |
---|
| 155 | |
---|
| 156 | # Remount read only anything that's left mounted. |
---|
| 157 | # echo $"Remounting remaining filesystems readonly" |
---|
| 158 | mount | awk '{ print $3 }' | while read line; do |
---|
| 159 | fstab-decode mount -n -o ro,remount $line |
---|
| 160 | done |
---|
| 161 | |
---|
| 162 | fi # noumouont or readonlyroot |
---|
| 163 | |
---|
| 164 | update_boot_stage jalt |
---|
| 165 | |
---|
| 166 | # Now halt or reboot. |
---|
| 167 | echo $"$message" |
---|
| 168 | if [ -f /fastboot ]; then |
---|
| 169 | echo $"On the next boot fsck will be skipped." |
---|
| 170 | elif [ -f /forcefsck ]; then |
---|
| 171 | echo $"On the next boot fsck will be forced." |
---|
| 172 | fi |
---|
| 173 | |
---|
| 174 | # Shutdown UPS drivers |
---|
| 175 | if [ "$command" = /sbin/halt -a -f /etc/sysconfig/ups ]; then |
---|
| 176 | . /etc/sysconfig/ups |
---|
| 177 | if [ -z $POWERDOWNFLAG ]; then |
---|
| 178 | POWERDOWNFLAG=/etc/killpower |
---|
| 179 | fi |
---|
| 180 | if [ "$SERVER" = "yes" -a -f $POWERDOWNFLAG ]; then |
---|
| 181 | /sbin/upsdrvctl shutdown |
---|
| 182 | fi |
---|
| 183 | fi |
---|
| 184 | |
---|
| 185 | # First, try kexec. If that fails, fall back to rebooting the old way. |
---|
| 186 | [ -n "$kexec_command" ] && $kexec_command -e -x >& /dev/null |
---|
| 187 | |
---|
| 188 | HALTARGS="-d" |
---|
| 189 | [ -f /poweroff -o ! -f /halt ] && HALTARGS="$HALTARGS -p" |
---|
| 190 | |
---|
| 191 | exec $command $HALTARGS |
---|