/[baselayout]/branches/baselayout-1_12/init.d/halt.sh
Gentoo

Contents of /branches/baselayout-1_12/init.d/halt.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1481 - (show annotations) (download) (as text)
Sun Sep 4 17:14:30 2005 UTC (9 years ago) by azarah
File MIME type: text/x-sh
File size: 7055 byte(s)
Do not unmount /usr, but rather remount it ro, else we run into issues
with systems that have locales enabled, as the fuser call in halt.sh tries
to kill bash (due to it using /usr/lib/locale/*).

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 # If its /usr, just ignore it .. we will mount it ro below ...
152 # This is to prevent killing bash on systems using locales.
153 [[ ${x} == "/usr" ]] && continue
154 # Kill processes still using this mount
155 /bin/fuser -k -m -9 "${x}" &>/dev/null
156 sleep 2
157 # Now try to unmount it again ...
158 umount -f -r "${x}" &>/dev/null
159 fi
160 done
161 eend 0
162
163 # Try to remove any dm-crypt mappings
164 stop_addon dm-crypt
165
166 # Stop LVM, etc
167 stop_volumes
168
169 # This is a function because its used twice below
170 ups_kill_power() {
171 local UPS_CTL UPS_POWERDOWN
172 if [[ -f /etc/killpower ]] ; then
173 UPS_CTL=/sbin/upsdrvctl
174 UPS_POWERDOWN="${UPS_CTL} shutdown"
175 elif [[ -f /etc/apcupsd/powerfail ]] ; then
176 UPS_CTL=/etc/apcupsd/apccontrol
177 UPS_POWERDOWN="${UPS_CTL} killpower"
178 else
179 return 0
180 fi
181 if [[ -x ${UPS_CTL} ]] ; then
182 ewarn "Signalling ups driver(s) to kill the load!"
183 ${UPS_POWERDOWN}
184 ewarn "Halt system and wait for the UPS to kill our power"
185 /sbin/halt -id
186 while [ 1 ]; do sleep 60; done
187 fi
188 }
189
190 mount_readonly() {
191 local x=
192 local retval=0
193 local cmd=$1
194
195 # Get better results with a sync and sleep
196 sync; sync
197 sleep 1
198
199 for x in $(awk '$1 != "none" { print $2 }' /proc/mounts | sort -ur) ; do
200 x=${x//\\040/ }
201 if [[ ${cmd} == "u" ]]; then
202 umount -n -r "${x}"
203 else
204 mount -n -o remount,ro "${x}" &>/dev/null
205 fi
206 retval=$((${retval} + $?))
207 done
208 [[ ${retval} -ne 0 ]] && killall5 -9 &>/dev/null
209
210 return ${retval}
211 }
212
213 # Since we use `mount` in mount_readonly(), but we parse /proc/mounts, we
214 # have to make sure our /etc/mtab and /proc/mounts agree
215 cp /proc/mounts /etc/mtab &>/dev/null
216 ebegin "Remounting remaining filesystems readonly"
217 mount_worked=0
218 if ! mount_readonly ; then
219 if ! mount_readonly ; then
220 # If these things really don't want to remount ro, then
221 # let's try to force them to unmount
222 if ! mount_readonly u ; then
223 mount_worked=1
224 fi
225 fi
226 fi
227 eend ${mount_worked}
228 if [[ ${mount_worked} -eq 1 ]]; then
229 ups_kill_power
230 /sbin/sulogin -t 10 /dev/console
231 fi
232
233 # Inform if there is a forced or skipped fsck
234 if [[ -f /fastboot ]]; then
235 echo
236 ewarn "Fsck will be skipped on next startup"
237 elif [[ -f /forcefsck ]]; then
238 echo
239 ewarn "A full fsck will be forced on next startup"
240 fi
241
242 ups_kill_power
243
244
245 # 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