/[baselayout]/trunk/init.d/halt.sh
Gentoo

Contents of /trunk/init.d/halt.sh

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2091 - (show annotations) (download) (as text)
Wed Jun 14 21:13:31 2006 UTC (8 years, 2 months ago) by uberlord
File MIME type: text/x-sh
File size: 6161 byte(s)
    {start,stop}_volumes moved to rc and halt.sh respectively.

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