Debian Codegraph @

setup/files/RH/halt.default-RH.drbl

raw
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, <[email protected]>
8 # Modified for RHS Linux by Damien Neil
9 #
10 # This file is modified by Steven Shiau <steven _at_ clonezilla org> to be used for DRBL on Fedora 14 on Nov/21/2010
11
12 NOLOCALE=1
13 . /etc/init.d/functions
14
15 UMOUNT="umount"
16 [ ! -w /etc ] && UMOUNT="umount -n"
17
18 halt_crypto() {
19 fnval=0
20 while read dst src key; do
21 [ -z "$dst" -o "${dst#\#}" != "$dst" ] && continue
22 if [ -b "/dev/mapper/$dst" ]; then
23 if /sbin/dmsetup info "$dst" | grep -q '^Open count: *0$'; then
24 action $"Stopping disk encryption for $dst" /sbin/cryptsetup remove "$dst"
25 else
26 fnval=1
27 fi
28 fi
29 done < /etc/crypttab
30 return $fnval
31 }
32
33 kill_all() {
34 local STRING rc
35
36 STRING=$1
37 echo -n "$STRING "
38 shift
39 /sbin/killall5 "$@"
40 rc=$?
41 # Retval: 0: success 1: error 2: no processes found to kill
42 if [ "$rc" == 1 ]; then
43 failure $"$STRING"
44 else
45 success $"$STRING"
46 fi
47 echo
48 return $rc
49 }
50
51 # See how we were called.
52 case "$0" in
53 *halt)
54 message=$"Halting system..."
55 command="/sbin/halt"
56 ;;
57 *reboot)
58 message=$"Please stand by while rebooting the system..."
59 command="/sbin/reboot"
60 kexec_command="/sbin/kexec"
61 ;;
62 *)
63 echo $"$0: call me as 'halt' or 'reboot' please!"
64 exit 1
65 ;;
66 esac
67 case "$1" in
68 *start)
69 ;;
70 *)
71 echo $"Usage: $0 {start}"
72 exit 1
73 ;;
74 esac
75
76 # Kill all processes.
77 [ "${BASH+bash}" = bash ] && enable kill
78
79 # Find mdmon pid's and omit killing them
80 OMITARGS=
81 for i in /dev/md/*.pid; do
82 if [ ! -r "$i" ]; then
83 continue
84 fi
85 OMITARGS="$OMITARGS -o $(cat $i)"
86 done
87
88 kill_all $"Sending all processes the TERM signal..." -15 $OMITARGS
89 # No need to sleep and kill -9 if no processes to kill were found
90 if [ "$?" == 0 ]; then
91 # DRBL, because it's DRBL, no waiting for sleep... comment sleep
92 #sleep 2
93 kill_all $"Sending all processes the KILL signal..." -9 $OMITARGS
94 fi
95
96 # Write to wtmp file before unmounting /var
97 /sbin/halt -w
98
99 # Save mixer settings, here for lack of a better place.
100 if [ -s /etc/alsa/alsactl.conf ] && [ -x /sbin/alsactl ] && [ -w /etc/asound.state ]; then
101 action $"Saving mixer settings" /sbin/alsactl -E ALSA_CONFIG_PATH=/etc/alsa/alsactl.conf --ignore store
102 fi
103
104 # Save random seed
105 touch /var/lib/random-seed
106 chmod 600 /var/lib/random-seed
107 action $"Saving random seed: " dd if=/dev/urandom of=/var/lib/random-seed count=1 bs=512 2>/dev/null
108
109 [ -x /sbin/hwclock ] && action $"Syncing hardware clock to system time" /sbin/hwclock --systohc
110
111 # Try to unmount tmpfs filesystems to avoid swapping them in. Ignore failures.
112 tmpfs=$(awk '$2 ~ /^\/($|proc|dev)/ { next; }
113 $3 == "tmpfs" { print $2; }' /proc/mounts | sort -r)
114 [ -n "$tmpfs" ] && fstab-decode $UMOUNT $tmpfs 2>/dev/null
115
116 # Turn off swap, then unmount file systems.
117 [ -f /proc/swaps ] && SWAPS=$(awk '! /^Filename/ { print $1 }' /proc/swaps)
118 if [ -n "$SWAPS" ]; then
119 action $"Turning off swap: " swapoff $SWAPS
120 for dst in $SWAPS; do
121 if [[ "$dst" == /dev/mapper* ]] \
122 && [ "$(dmsetup status "$dst" | cut -d ' ' -f 3)" = crypt ]; then
123 backdev=$(/sbin/cryptsetup status "$dst" \
124 | awk '$1 == "device:" { print $2 }')
125 /sbin/cryptsetup remove "$dst"
126 fi
127 done
128 fi
129
130 [ -x /sbin/quotaoff ] && action $"Turning off quotas: " /sbin/quotaoff -aug
131
132 # Unmount file systems, killing processes if we have to.
133 # Unmount loopback stuff first
134 __umount_loopback_loop
135
136 # Unmount RPC pipe file systems
137 __umount_loop '$3 ~ /^rpc_pipefs$/ || $3 ~ /^rpc_svc_gss_pipefs$/ {print $2}' \
138 /proc/mounts \
139 $"Unmounting pipe file systems: " \
140 $"Unmounting pipe file systems (retry): " \
141 -f
142
143 # DRBL add nfs and /dev/shm (tmpfs) to be excluded.
144 # DRBL comment the tmpfs, which we use to replace devfs in DRBL clients.
145 #LANG=C __umount_loop '$2 ~ /^\/$|^\/proc|^\/cgroup|^\/sys\/fs\/cgroup|^\/dev/{next}
146 # $3 == "tmpfs" || $3 == "proc" {print $2 ; next}
147 # /(loopfs|autofs|nfs|cifs|smbfs|ncpfs|sysfs|^none|^\/dev\/ram|^\/dev\/root$)/ {next}
148 LANG=C __umount_loop '$2 ~ /^\/$|^\/proc|^\/cgroup|^\/sys\/fs\/cgroup|^\/dev/{next}
149 $3 == "proc" {print $2 ; next}
150 /(loopfs|autofs|nfs|cifs|smbfs|ncpfs|sysfs|^none|^\/dev\/ram|^\/dev\/root$|^\/dev\/shm)/ {next}
151 {print $2}' /proc/mounts \
152 $"Unmounting file systems: " \
153 $"Unmounting file systems (retry): " \
154 -f
155
156 [ -f /proc/bus/usb/devices ] && $UMOUNT /proc/bus/usb
157
158 [ -f /etc/crypttab ] && halt_crypto
159
160 # remove the crash indicator flag
161 rm -f /.autofsck
162
163 # Try all file systems other than root, essential filesystems and RAM disks,
164 # one last time.
165 # DRBL, add /etc, /var, /usr and tmpfs (for /dev/shm) so they will not be unmounted...
166 #awk '$2 !~ /\/(|dev|proc|selinux|cgroup|sys)$/ && $1 !~ /(^\/dev\/ram|cgroup)/ { print $2 }' \
167 awk '$2 !~ /\/(|dev|proc|selinux|cgroup|sys|etc|var|usr)$/ && $1 !~ /(^\/dev\/ram|cgroup|^tmpfs)/ { print $2 }' \
168 /proc/mounts | sort -r | \
169 while read line; do
170 fstab-decode $UMOUNT -f $line
171 done
172
173 if [ -x /sbin/halt.local ]; then
174 /sbin/halt.local
175 fi
176
177 # Tell init to re-exec itself.
178 kill -TERM 1
179
180 # Remount read only anything that's left mounted.
181 # echo $"Remounting remaining filesystems readonly"
182 # DRBL added codes to exclude nfs and tmpfs for being remounted again.
183 #mount | awk '{ print $1,$3 }' | while read dev dir; do
184 mount | awk '$5 !~ /(nfs|tmpfs)/ { print $1,$3 }' | while read dev dir; do
185 fstab-decode mount -n -o ro,remount $dev $dir
186 done
187
188 # If we left mdmon's running wait for the raidsets to become clean
189 if [ -n "$OMITARGS" ]; then
190 mdadm --wait-clean --scan
191 fi
192
193 # Now halt or reboot.
194 echo $"$message"
195 if [ -f /fastboot ]; then
196 echo $"On the next boot fsck will be skipped."
197 elif [ -f /forcefsck ]; then
198 echo $"On the next boot fsck will be forced."
199 fi
200
201 # Shutdown UPS drivers
202 if [ "$command" = /sbin/halt ] && [ -f /etc/sysconfig/ups ]; then
203 . /etc/sysconfig/ups
204 if [ -z $POWERDOWNFLAG ]; then
205 POWERDOWNFLAG=/etc/killpower
206 fi
207 if [ "$SERVER" = "yes" ] && [ -f $POWERDOWNFLAG ]; then
208 /sbin/upsdrvctl shutdown
209 fi
210 fi
211
212 # First, try kexec. If that fails, fall back to rebooting the old way.
213 [ -n "$kexec_command" ] && $kexec_command -e -x >& /dev/null
214
215 HALTARGS="-d"
216 [ "$INIT_HALT" != "HALT" ] && HALTARGS="$HALTARGS -p"
217
218 exec $command $HALTARGS