/[baselayout]/branches/bsd-porting/init.d/halt.sh
Gentoo

Contents of /branches/bsd-porting/init.d/halt.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2016 - (show annotations) (download) (as text)
Mon Apr 24 13:27:31 2006 UTC (8 years, 8 months ago) by uberlord
File MIME type: text/x-sh
File size: 6908 byte(s)
Fixed everyones borked sort command in halt.sh
1 # Copyright 1999-2006 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 else
68 rm -f /lib/udev-state/devices.tar.bz2
69 fi
70 eend 0
71 fi
72
73 rm -f "${devices_udev}" "${devices_real}" "${devices_totar}" "${device_tarball}"
74 fi
75
76 # Try to unmount all tmpfs filesystems not in use, else a deadlock may
77 # occure, bug #13599.
78 umount -at tmpfs &>/dev/null
79
80 if [[ -n $(swapon -s 2>/dev/null) ]]; then
81 ebegin "Deactivating swap"
82 swapoff -a
83 eend $?
84 fi
85
86 # Write a reboot record to /var/log/wtmp before unmounting
87
88 halt -w &>/dev/null
89
90 # Unmounting should use /proc/mounts and work with/without devfsd running
91
92 # Credits for next function to unmount loop devices, goes to:
93 #
94 # Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
95 # Modified for RHS Linux by Damien Neil
96 #
97 #
98 # Unmount file systems, killing processes if we have to.
99 # Unmount loopback stuff first
100 # Use `umount -d` to detach the loopback device
101
102 # Remove loopback devices started by dm-crypt
103
104 remaining=$(awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts | \
105 sort -r | grep -v '/newroot' | grep -v '/mnt/livecd')
106 [[ -n ${remaining} ]] && {
107 sig=
108 retry=3
109
110 while [[ -n ${remaining} && ${retry} -gt 0 ]]; do
111 if [[ ${retry} -lt 3 ]]; then
112 ebegin "Unmounting loopback filesystems (retry)"
113 umount -d ${remaining} &>/dev/null
114 eend $? "Failed to unmount filesystems this retry"
115 else
116 ebegin "Unmounting loopback filesystems"
117 umount -d ${remaining} &>/dev/null
118 eend $? "Failed to unmount filesystems"
119 fi
120
121 remaining=$(awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts | \
122 sort -r | grep -v '/newroot' | grep -v '/mnt/livecd')
123 [[ -z ${remaining} ]] && break
124
125 /bin/fuser -s -k ${sig} -m ${remaining}
126 sleep 5
127 retry=$((${retry} - 1))
128 sig=-9
129 done
130 }
131
132 # Try to unmount all filesystems (no /proc,tmpfs,devfs,etc).
133 # This is needed to make sure we dont have a mounted filesystem
134 # on a LVM volume when shutting LVM down ...
135 ebegin "Unmounting filesystems"
136 unmounts=$( \
137 awk '{ \
138 if (($3 !~ /^(proc|devpts|sysfs|devfs|tmpfs|usb(dev)?fs)$/) && \
139 ($1 != "none") && \
140 ($1 !~ /^(rootfs|\/dev\/root)$/) && \
141 ($2 != "/")) \
142 print $2 }' /proc/mounts | sort -ur)
143 for x in ${unmounts}; do
144 # Do not umount these if we are booting off a livecd
145 if [[ -n ${CDBOOT} ]] && \
146 [[ ${x} == "/mnt/cdrom" || ${x} == "/mnt/livecd" ]] ; then
147 continue
148 fi
149
150 x=${x//\\040/ }
151 if ! umount "${x}" &>/dev/null; then
152 # Kill processes still using this mount
153 /bin/fuser -s -k -9 -m "${x}"
154 sleep 2
155 # Now try to unmount it again ...
156 umount -f -r "${x}" &>/dev/null
157 fi
158 done
159 eend 0
160
161 # Try to remove any dm-crypt mappings
162 stop_addon dm-crypt
163
164 # Stop LVM, etc
165 stop_volumes
166
167 # This is a function because its used twice below
168 ups_kill_power() {
169 local UPS_CTL UPS_POWERDOWN
170 if [[ -f /etc/killpower ]] ; then
171 UPS_CTL=/sbin/upsdrvctl
172 UPS_POWERDOWN="${UPS_CTL} shutdown"
173 elif [[ -f /etc/apcupsd/powerfail ]] ; then
174 UPS_CTL=/etc/apcupsd/apccontrol
175 UPS_POWERDOWN="${UPS_CTL} killpower"
176 else
177 return 0
178 fi
179 if [[ -x ${UPS_CTL} ]] ; then
180 ewarn "Signalling ups driver(s) to kill the load!"
181 ${UPS_POWERDOWN}
182 ewarn "Halt system and wait for the UPS to kill our power"
183 /sbin/halt -id
184 while [ 1 ]; do sleep 60; done
185 fi
186 }
187
188 mount_readonly() {
189 local x=
190 local retval=0
191 local cmd=$1
192
193 # Get better results with a sync and sleep
194 sync; sync
195 sleep 1
196
197 for x in $(awk '$1 != "none" { print $2 }' /proc/mounts | sort -ur) ; do
198 x=${x//\\040/ }
199 if [[ ${cmd} == "u" ]]; then
200 umount -n -r "${x}"
201 else
202 mount -n -o remount,ro "${x}" &>/dev/null
203 fi
204 retval=$((${retval} + $?))
205 done
206 [[ ${retval} -ne 0 ]] && killall5 -9 &>/dev/null
207
208 return ${retval}
209 }
210
211 # Since we use `mount` in mount_readonly(), but we parse /proc/mounts, we
212 # have to make sure our /etc/mtab and /proc/mounts agree
213 cp /proc/mounts /etc/mtab &>/dev/null
214 ebegin "Remounting remaining filesystems readonly"
215 mount_worked=0
216 if ! mount_readonly ; then
217 if ! mount_readonly ; then
218 # If these things really don't want to remount ro, then
219 # let's try to force them to unmount
220 if ! mount_readonly u ; then
221 mount_worked=1
222 fi
223 fi
224 fi
225 eend ${mount_worked}
226 if [[ ${mount_worked} -eq 1 ]]; then
227 ups_kill_power
228 /sbin/sulogin -t 10 /dev/console
229 fi
230
231 # Inform if there is a forced or skipped fsck
232 if [[ -f /fastboot ]]; then
233 echo
234 ewarn "Fsck will be skipped on next startup"
235 elif [[ -f /forcefsck ]]; then
236 echo
237 ewarn "A full fsck will be forced on next startup"
238 fi
239
240 ups_kill_power
241
242
243 # vim:ts=4

Properties

Name Value
svn:eol-style native
svn:executable *
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.20