source: projects/initscripts/tags/initscripts-8.91.3/rc.d/init.d/halt @ 1108

Revision 1108, 5.0 KB checked in by daisuke, 14 years ago (diff)

import initscripts-8.90.6 from internal cvs repository

Line 
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
11NOLOCALE=1
12. /etc/init.d/functions
13
14action() {
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
25halt_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.
41case "$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        ;;
55esac
56case "$1" in
57   *start)
58        ;;
59   *)
60        echo $"Usage: $0 {start}"
61        exit 1
62        ;;
63esac
64
65update_boot_stage kill
66
67# Kill all processes.
68[ "${BASH+bash}" = bash ] && enable kill
69
70action $"Sending all processes the TERM signal..." /sbin/killall5 -15
71sleep 2
72action $"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
78touch /var/lib/random-seed
79chmod 600 /var/lib/random-seed
80action $"Saving random seed: " dd if=/dev/urandom of=/var/lib/random-seed count=1 bs=512 2>/dev/null
81
82update_boot_stage clock
83[ -x /sbin/hwclock ] && action $"Syncing hardware clock to system time" /sbin/hwclock --systohc
84
85update_boot_stage umount
86
87# If noumount or readonlyroot is specified, skip umounting filesystems
88if ( ! egrep -q "(noumount|readonlyroot)" /proc/cmdline ); then
89
90# Try to unmount tmpfs filesystems to avoid swapping them in.  Ignore failures.
91tmpfs=$(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`
97if [ -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
109fi
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
124LANG=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
138rm -f /.autofsck
139
140update_boot_stage umount_force
141
142# Try all file systems other than root, essential filesystems and RAM disks,
143# one last time.
144awk '$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
148done
149
150if [ -x /sbin/halt.local ]; then
151   /sbin/halt.local
152fi
153
154update_boot_stage remount_ro
155
156# Remount read only anything that's left mounted.
157# echo $"Remounting remaining filesystems readonly"
158mount | awk '{ print $3 }' | while read line; do
159    fstab-decode mount -n -o ro,remount $line
160done
161
162fi # noumouont or readonlyroot
163
164update_boot_stage jalt
165
166# Now halt or reboot.
167echo $"$message"
168if [ -f /fastboot ]; then
169 echo $"On the next boot fsck will be skipped."
170elif [ -f /forcefsck ]; then
171 echo $"On the next boot fsck will be forced."
172fi
173
174# Shutdown UPS drivers
175if [ "$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
183fi
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
188HALTARGS="-d"
189[ -f /poweroff -o ! -f /halt ] && HALTARGS="$HALTARGS -p"
190
191exec $command $HALTARGS
Note: See TracBrowser for help on using the repository browser.