/[baselayout]/branches/rc-scripts-1_6/init.d/halt.sh
Gentoo

Contents of /branches/rc-scripts-1_6/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, 4 months ago) by uberlord
File MIME type: text/x-sh
File size: 5399 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 stop_addon devfs
17 stop_addon udev
18
19 # Try to unmount all tmpfs filesystems not in use, else a deadlock may
20 # occure, bug #13599.
21 umount -at tmpfs &>/dev/null
22
23 # Turn off swap and perhaps zero it out for fun
24 swap_list=$(swapon -s 2>/dev/null)
25
26 if [[ -n ${swap_list} ]] ; then
27 ebegin $"Deactivating swap"
28 swapoff -a
29 eend $?
30
31 if [[ ${RC_SWAP_ERASE} == "yes" ]] ; then
32 for s in $(echo "${swap_list}" | awk '$2 == "partition" {print $1}') ; do
33 ebegin $"Erasing swap space" ${s}
34 ssize=$(awk '$4 == "'${s##*/}'" {print $3}' /proc/partitions 2> /dev/null)
35 dd if=/dev/zero of=${s} bs=1024 count=${ssize} 2> /dev/null
36 eend $?
37 ebegin $"Creating swap space" ${s}
38 mkswap ${s} > /dev/null
39 eend $?
40 done
41 fi
42 fi
43
44 # Write a reboot record to /var/log/wtmp before unmounting
45
46 halt -w &>/dev/null
47
48 # Unmounting should use /proc/mounts and work with/without devfsd running
49
50 # Credits for next function to unmount loop devices, goes to:
51 #
52 # Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
53 # Modified for RHS Linux by Damien Neil
54 #
55 #
56 # Unmount file systems, killing processes if we have to.
57 # Unmount loopback stuff first
58 # Use `umount -d` to detach the loopback device
59
60 # Remove loopback devices started by dm-crypt
61
62 remaining=$(awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts | \
63 sort -r | grep -v '/newroot' | grep -v '/mnt/livecd')
64 [[ -n ${remaining} ]] && {
65 sig=
66 retry=3
67
68 while [[ -n ${remaining} && ${retry} -gt 0 ]]; do
69 if [[ ${retry} -lt 3 ]]; then
70 ebegin "Unmounting loopback filesystems (retry)"
71 umount -d ${remaining} &>/dev/null
72 eend $? "Failed to unmount filesystems this retry"
73 else
74 ebegin "Unmounting loopback filesystems"
75 umount -d ${remaining} &>/dev/null
76 eend $? "Failed to unmount filesystems"
77 fi
78
79 remaining=$(awk '!/^#/ && $1 ~ /^\/dev\/loop/ && $2 != "/" {print $2}' /proc/mounts | \
80 sort -r | grep -v '/newroot' | grep -v '/mnt/livecd')
81 [[ -z ${remaining} ]] && break
82
83 /bin/fuser -s -k ${sig} -m ${remaining}
84 sleep 5
85 retry=$((${retry} - 1))
86 sig=-9
87 done
88 }
89
90 # Try to unmount all filesystems (no /proc,tmpfs,devfs,etc).
91 # This is needed to make sure we dont have a mounted filesystem
92 # on a LVM volume when shutting LVM down ...
93 ebegin "Unmounting filesystems"
94 unmounts=$( \
95 awk '{ \
96 if (($3 !~ /^(proc|devpts|sysfs|devfs|tmpfs|usb(dev)?fs)$/) && \
97 ($1 != "none") && \
98 ($1 !~ /^(rootfs|\/dev\/root)$/) && \
99 ($2 != "/")) \
100 print $2 }' /proc/mounts | sort -ur)
101 for x in ${unmounts}; do
102 # Do not umount these if we are booting off a livecd
103 if [[ -n ${CDBOOT} ]] && \
104 [[ ${x} == "/mnt/cdrom" || ${x} == "/mnt/livecd" ]] ; then
105 continue
106 fi
107
108 x=${x//\\040/ }
109 if ! umount "${x}" &>/dev/null; then
110 # Kill processes still using this mount
111 /bin/fuser -s -k -9 -m "${x}"
112 sleep 2
113 # Now try to unmount it again ...
114 umount -f -r "${x}" &>/dev/null
115 fi
116 done
117 eend 0
118
119 # Try to remove any dm-crypt mappings
120 stop_addon dm-crypt
121
122 # Stop LVM, etc
123 stop_volumes
124
125 # This is a function because its used twice below
126 ups_kill_power() {
127 local UPS_CTL UPS_POWERDOWN
128 if [[ -f /etc/killpower ]] ; then
129 UPS_CTL=/sbin/upsdrvctl
130 UPS_POWERDOWN="${UPS_CTL} shutdown"
131 elif [[ -f /etc/apcupsd/powerfail ]] ; then
132 UPS_CTL=/etc/apcupsd/apccontrol
133 UPS_POWERDOWN="${UPS_CTL} killpower"
134 else
135 return 0
136 fi
137 if [[ -x ${UPS_CTL} ]] ; then
138 ewarn "Signalling ups driver(s) to kill the load!"
139 ${UPS_POWERDOWN}
140 ewarn "Halt system and wait for the UPS to kill our power"
141 /sbin/halt -id
142 while [ 1 ]; do sleep 60; done
143 fi
144 }
145
146 mount_readonly() {
147 local x=
148 local retval=0
149 local cmd=$1
150
151 # Get better results with a sync and sleep
152 sync; sync
153 sleep 1
154
155 for x in $(awk '$1 != "none" { print $2 }' /proc/mounts | sort -ur) ; do
156 x=${x//\\040/ }
157 if [[ ${cmd} == "u" ]]; then
158 umount -n -r "${x}"
159 else
160 mount -n -o remount,ro "${x}" &>/dev/null
161 fi
162 retval=$((${retval} + $?))
163 done
164 [[ ${retval} -ne 0 ]] && killall5 -9 &>/dev/null
165
166 return ${retval}
167 }
168
169 # Since we use `mount` in mount_readonly(), but we parse /proc/mounts, we
170 # have to make sure our /etc/mtab and /proc/mounts agree
171 cp /proc/mounts /etc/mtab &>/dev/null
172 ebegin "Remounting remaining filesystems readonly"
173 mount_worked=0
174 if ! mount_readonly ; then
175 if ! mount_readonly ; then
176 # If these things really don't want to remount ro, then
177 # let's try to force them to unmount
178 if ! mount_readonly u ; then
179 mount_worked=1
180 fi
181 fi
182 fi
183 eend ${mount_worked}
184 if [[ ${mount_worked} -eq 1 ]]; then
185 ups_kill_power
186 /sbin/sulogin -t 10 /dev/console
187 fi
188
189 # Inform if there is a forced or skipped fsck
190 if [[ -f /fastboot ]]; then
191 echo
192 ewarn "Fsck will be skipped on next startup"
193 elif [[ -f /forcefsck ]]; then
194 echo
195 ewarn "A full fsck will be forced on next startup"
196 fi
197
198 ups_kill_power
199
200
201 # 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