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