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