/[vps]/baselayout-vserver/trunk/init.d/halt.sh
Gentoo

Contents of /baselayout-vserver/trunk/init.d/halt.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 3 - (show annotations) (download) (as text)
Sat Sep 3 16:10:27 2005 UTC (8 years, 7 months ago) by hollow
File MIME type: text/x-sh
File size: 6892 byte(s)
import initial baselayout sources (1.12.0_pre8)
1 # Copyright 1999-2005 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # Check to see if this is a livecd, if it is read the commandline
5 # this mainly makes sure $CDBOOT is defined if it's a livecd
6 [[ -f /sbin/livecd-functions.sh ]] && \
7 source /sbin/livecd-functions.sh && \
8 livecd_read_commandline
9
10 # Reset pam_console permissions if we are actually using it
11 if [[ -x /sbin/pam_console_apply && ! -c /dev/.devfsd && \
12 -n $(grep -v -e '^[[:space:]]*#' /etc/pam.d/* | grep 'pam_console') ]]; then
13 /sbin/pam_console_apply -r
14 fi
15
16 # We need to properly terminate devfsd to save the permissions
17 if [[ -n $(ps --no-heading -C 'devfsd') ]]; then
18 ebegin "Stopping devfsd"
19 killall -15 devfsd &>/dev/null
20 eend $?
21 elif [[ ! -e /dev/.devfsd && -e /dev/.udev && -z ${CDBOOT} && \
22 ${RC_DEVICE_TARBALL} == "yes" ]] && \
23 touch /lib/udev-state/devices.tar.bz2 2>/dev/null
24 then
25 ebegin "Saving device nodes"
26 # Handle our temp files
27 devices_udev=$(mktemp /tmp/devices.udev.XXXXXX)
28 devices_real=$(mktemp /tmp/devices.real.XXXXXX)
29 devices_totar=$(mktemp /tmp/devices.totar.XXXXXX)
30 device_tarball=$(mktemp /tmp/devices-XXXXXX)
31
32 if [[ -z ${devices_udev} || -z ${devices_real} || \
33 -z ${device_tarball} ]]; then
34 eend 1 "Could not create temporary files!"
35 else
36 cd /dev
37 # Find all devices
38 find . -xdev -type b -or -type c -or -type l | cut -d/ -f2- > \
39 "${devices_real}"
40 # Figure out what udev created
41 eval $(grep '^[[:space:]]*udev_db=' /etc/udev/udev.conf)
42 if [[ -d ${udev_db} ]]; then
43 # New udev_db is clear text ...
44 udevinfo=$(cat "${udev_db}"/*)
45 else
46 # Old one is not ...
47 udevinfo=$(udevinfo -d)
48 fi
49 # This basically strips 'S:' and 'N:' from the db output, and then
50 # print all the nodes/symlinks udev created ...
51 echo "${udevinfo}" | gawk '
52 /^(N|S):.+/ {
53 sub(/^(N|S):/, "")
54 split($0, nodes)
55 for (x in nodes)
56 print nodes[x]
57 }' > "${devices_udev}"
58 # These ones we also do not want in there
59 for x in MAKEDEV core fd initctl pts shm stderr stdin stdout; do
60 echo "${x}" >> "${devices_udev}"
61 done
62 fgrep -x -v -f "${devices_udev}" < "${devices_real}" > "${devices_totar}"
63 # Now only tarball those not created by udev if we have any
64 if [[ -s ${devices_totar} ]]; then
65 try tar --one-file-system -jcpf "${device_tarball}" -T "${devices_totar}"
66 try mv -f "${device_tarball}" /lib/udev-state/devices.tar.bz2
67 try rm -f "${devices_udev}" "${devices_real}"
68 else
69 rm -f /lib/udev-state/devices.tar.bz2
70 fi
71 eend 0
72 fi
73 fi
74
75 # Try to unmount all tmpfs filesystems not in use, else a deadlock may
76 # occure, bug #13599.
77 umount -at tmpfs &>/dev/null
78
79 if [[ -n $(swapon -s 2>/dev/null) ]]; then
80 ebegin "Deactivating swap"
81 swapoff -a
82 eend $?
83 fi
84
85 # Write a reboot record to /var/log/wtmp before unmounting
86
87 halt -w &>/dev/null
88
89 # Unmounting should use /proc/mounts and work with/without devfsd running
90
91 # Credits for next function to unmount loop devices, goes to:
92 #
93 # Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
94 # Modified for RHS Linux by Damien Neil
95 #
96 #
97 # Unmount file systems, killing processes if we have to.
98 # Unmount loopback stuff first
99 # Use `umount -d` to detach the loopback device
100
101 # Remove loopback devices started by dm-crypt
102
103 remaining=$(awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts | \
104 sort -r | grep -v '/newroot' | grep -v '/mnt/livecd')
105 [[ -n ${remaining} ]] && {
106 sig=
107 retry=3
108
109 while [[ -n ${remaining} && ${retry} -gt 0 ]]; do
110 if [[ ${retry} -lt 3 ]]; then
111 ebegin "Unmounting loopback filesystems (retry)"
112 umount -d ${remaining} &>/dev/null
113 eend $? "Failed to unmount filesystems this retry"
114 else
115 ebegin "Unmounting loopback filesystems"
116 umount -d ${remaining} &>/dev/null
117 eend $? "Failed to unmount filesystems"
118 fi
119
120 remaining=$(awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts | \
121 sort -r | grep -v '/newroot' | grep -v '/mnt/livecd')
122 [[ -z ${remaining} ]] && break
123
124 /bin/fuser -k -m ${sig} ${remaining} &>/dev/null
125 sleep 5
126 retry=$((${retry} - 1))
127 sig=-9
128 done
129 }
130
131 # Try to unmount all filesystems (no /proc,tmpfs,devfs,etc).
132 # This is needed to make sure we dont have a mounted filesystem
133 # on a LVM volume when shutting LVM down ...
134 ebegin "Unmounting filesystems"
135 unmounts=$( \
136 awk '{ \
137 if (($3 !~ /^(proc|devpts|sysfs|devfs|tmpfs|usb(dev)?fs)$/) && \
138 ($1 != "none") && \
139 ($1 !~ /^(rootfs|\/dev\/root)$/) && \
140 ($2 != "/")) \
141 print $2 }' /proc/mounts | sort -ur)
142 for x in ${unmounts}; do
143 # Do not umount these if we are booting off a livecd
144 if [[ -n ${CDBOOT} ]] && \
145 [[ ${x} == "/mnt/cdrom" || ${x} == "/mnt/livecd" ]] ; then
146 continue
147 fi
148
149 x=${x//\\040/ }
150 if ! umount "${x}" &>/dev/null; then
151 # Kill processes still using this mount
152 /bin/fuser -k -m -9 "${x}" &>/dev/null
153 sleep 2
154 # Now try to unmount it again ...
155 umount -f -r "${x}" &>/dev/null
156 fi
157 done
158 eend 0
159
160 # Try to remove any dm-crypt mappings
161 stop_addon dm-crypt
162
163 # Stop LVM, etc
164 stop_volumes
165
166 # This is a function because its used twice below
167 ups_kill_power() {
168 local UPS_CTL UPS_POWERDOWN
169 if [[ -f /etc/killpower ]] ; then
170 UPS_CTL=/sbin/upsdrvctl
171 UPS_POWERDOWN="${UPS_CTL} shutdown"
172 elif [[ -f /etc/apcupsd/powerfail ]] ; then
173 UPS_CTL=/etc/apcupsd/apccontrol
174 UPS_POWERDOWN="${UPS_CTL} killpower"
175 else
176 return 0
177 fi
178 if [[ -x ${UPS_CTL} ]] ; then
179 ewarn "Signalling ups driver(s) to kill the load!"
180 ${UPS_POWERDOWN}
181 ewarn "Halt system and wait for the UPS to kill our power"
182 /sbin/halt -id
183 while [ 1 ]; do sleep 60; done
184 fi
185 }
186
187 mount_readonly() {
188 local x=
189 local retval=0
190 local cmd=$1
191
192 # Get better results with a sync and sleep
193 sync; sync
194 sleep 1
195
196 for x in $(awk '$1 != "none" { print $2 }' /proc/mounts | sort -ur) ; do
197 x=${x//\\040/ }
198 if [[ ${cmd} == "u" ]]; then
199 umount -n -r "${x}"
200 else
201 mount -n -o remount,ro "${x}" &>/dev/null
202 fi
203 retval=$((${retval} + $?))
204 done
205 [[ ${retval} -ne 0 ]] && killall5 -9 &>/dev/null
206
207 return ${retval}
208 }
209
210 # Since we use `mount` in mount_readonly(), but we parse /proc/mounts, we
211 # have to make sure our /etc/mtab and /proc/mounts agree
212 cp /proc/mounts /etc/mtab &>/dev/null
213 ebegin "Remounting remaining filesystems readonly"
214 mount_worked=0
215 if ! mount_readonly ; then
216 if ! mount_readonly ; then
217 # If these things really don't want to remount ro, then
218 # let's try to force them to unmount
219 if ! mount_readonly u ; then
220 mount_worked=1
221 fi
222 fi
223 fi
224 eend ${mount_worked}
225 if [[ ${mount_worked} -eq 1 ]]; then
226 ups_kill_power
227 /sbin/sulogin -t 10 /dev/console
228 fi
229
230 # Inform if there is a forced or skipped fsck
231 if [[ -f /fastboot ]]; then
232 echo
233 ewarn "Fsck will be skipped on next startup"
234 elif [[ -f /forcefsck ]]; then
235 echo
236 ewarn "A full fsck will be forced on next startup"
237 fi
238
239 ups_kill_power
240
241
242 # vim:ts=4

Properties

Name Value
svn:executable *

  ViewVC Help
Powered by ViewVC 1.1.20