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