source: projects/initscripts/trunk/rc.d/init.d/halt @ 8038

Revision 8038, 5.1 KB checked in by daisuke, 10 years ago (diff)

add detect_container()

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