2014-10-14 16:02:12 +00:00
|
|
|
#!/bin/bash -e
|
2015-02-10 15:10:52 +00:00
|
|
|
# vim: set ts=4 sw=4 sts=4 et :
|
2014-10-14 16:02:12 +00:00
|
|
|
|
|
|
|
#
|
2015-08-04 15:59:50 +00:00
|
|
|
# Written by Jason Mehring (nrgaway@gmail.com)
|
|
|
|
#
|
2014-10-14 16:02:12 +00:00
|
|
|
|
2015-08-04 15:59:50 +00:00
|
|
|
# Kills any processes within the mounted location and
|
2014-10-14 16:02:12 +00:00
|
|
|
# unmounts any mounts active within.
|
2015-08-04 15:59:50 +00:00
|
|
|
#
|
2014-10-14 16:02:12 +00:00
|
|
|
# To keep the actual mount mounted, add a '/' to end
|
|
|
|
#
|
2015-02-10 15:10:52 +00:00
|
|
|
# ${1}: directory to umount
|
2014-10-14 16:02:12 +00:00
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# To kill all processes and mounts within 'chroot-jessie' but keep
|
|
|
|
# 'chroot-jessie' mounted:
|
|
|
|
#
|
2015-08-04 15:59:50 +00:00
|
|
|
# ./umount_kill.sh chroot-jessie/
|
2014-10-14 16:02:12 +00:00
|
|
|
#
|
|
|
|
# To kill all processes and mounts within 'chroot-jessie' AND also
|
|
|
|
# umount 'chroot-jessie' mount:
|
|
|
|
#
|
2014-10-14 21:04:42 +00:00
|
|
|
# ./umount_kill.sh chroot-jessie
|
2015-08-04 15:59:50 +00:00
|
|
|
#
|
2014-10-14 16:02:12 +00:00
|
|
|
|
2015-08-10 20:58:07 +00:00
|
|
|
if [ "$VERBOSE" -ge 2 -o "$DEBUG" == "1" ]; then
|
|
|
|
set -x
|
|
|
|
fi
|
|
|
|
|
2014-10-16 16:03:05 +00:00
|
|
|
. ./functions.sh
|
|
|
|
|
2015-02-10 15:10:52 +00:00
|
|
|
mountPoint() {
|
|
|
|
local mount_point="${1}"
|
2014-10-14 16:02:12 +00:00
|
|
|
|
|
|
|
# We need absolute paths here so we don't kill everything
|
2015-02-10 15:10:52 +00:00
|
|
|
if ! [[ "${mount_point}" = /* ]]; then
|
|
|
|
mount_point="$(readlink -m .)/${mount_point}"
|
2014-10-14 16:02:12 +00:00
|
|
|
fi
|
|
|
|
|
2014-10-14 20:30:12 +00:00
|
|
|
# Strip any extra trailing slashes ('/') from path if they exist
|
|
|
|
# since we are doing an exact string match on the path
|
2015-02-10 15:10:52 +00:00
|
|
|
echo "$(echo "${mount_point}" | sed s#//*#/#g)"
|
|
|
|
}
|
|
|
|
|
|
|
|
mountPoints() {
|
|
|
|
local mount_point="$(mountPoint "${1}")"
|
|
|
|
echo "$(sudo grep "${mount_point}" /proc/mounts | cut -f2 -d" " | sort -r | grep "^${mount_point}")"
|
|
|
|
}
|
|
|
|
|
2015-08-04 15:59:50 +00:00
|
|
|
# ${1} = full path to mountpoint;
|
2015-02-10 15:10:52 +00:00
|
|
|
# ${2} = if set will not umount; only kill processes in mount
|
|
|
|
umount_kill() {
|
2015-08-04 15:59:50 +00:00
|
|
|
if [ "${VERBOSE}" -le 2 ]; then
|
|
|
|
# If enabled, turn off xtrace and remember its current setting.
|
|
|
|
if test -o xtrace ; then
|
|
|
|
true "$FUNCNAME: Disabling xtrace, because variable VERBOSE (${VERBOSE}) is lower than or equal 2..."
|
2015-08-04 23:41:35 +00:00
|
|
|
set +x
|
2015-08-04 15:59:50 +00:00
|
|
|
XTRACE_WAS_SET=true
|
|
|
|
fi
|
|
|
|
fi
|
2014-10-14 20:30:12 +00:00
|
|
|
|
2015-02-10 15:10:52 +00:00
|
|
|
local mount_point="$(mountPoint "${1}")"
|
|
|
|
local kill_only="${2}"
|
|
|
|
declare -A cache
|
|
|
|
|
|
|
|
# Sync the disk before un-mounting to be sure everything is written
|
2014-11-07 04:58:34 +00:00
|
|
|
sync
|
|
|
|
|
2015-02-10 15:10:52 +00:00
|
|
|
output "${red}Attempting to kill any processes still running in '${mount_point}' before un-mounting${reset}"
|
|
|
|
mounts="$(mountPoints "${mount_point}")"
|
|
|
|
for dir in ${mounts[@]}
|
2014-10-14 16:02:12 +00:00
|
|
|
do
|
2015-02-10 15:10:52 +00:00
|
|
|
# Escape filename (convert spaces to '\ ', etc
|
|
|
|
dir="$(printf "${dir}")"
|
|
|
|
|
|
|
|
# Skip if already in cache
|
|
|
|
[[ ${cache["${dir}"]+_} ]] && continue || cache["${dir}"]=1
|
|
|
|
|
|
|
|
# Kill of any processes within mountpoint
|
|
|
|
sudo lsof "${dir}" 2> /dev/null | \
|
|
|
|
grep "${dir}" | \
|
2014-10-14 16:02:12 +00:00
|
|
|
tail -n +2 | \
|
|
|
|
awk '{print $2}' | \
|
|
|
|
xargs --no-run-if-empty sudo kill -9
|
|
|
|
|
2015-02-10 15:10:52 +00:00
|
|
|
# Umount
|
|
|
|
if ! [ "${kill_only}" ]; then
|
|
|
|
|
|
|
|
# Mount point found in mtab
|
2017-07-11 21:56:30 +00:00
|
|
|
if $(sudo mountpoint -q "${dir}"); then
|
2015-02-10 15:10:52 +00:00
|
|
|
info "umount ${dir}"
|
|
|
|
sudo umount -n "${dir}" 2> /dev/null || \
|
|
|
|
sudo umount -n -l "${dir}" 2> /dev/null || \
|
|
|
|
error "umount ${dir} unsuccessful!"
|
|
|
|
|
2017-07-11 21:56:30 +00:00
|
|
|
# Umount entries not found within 'mountpoint'
|
2015-02-10 15:10:52 +00:00
|
|
|
else
|
|
|
|
# Look for (deleted) mountpoints
|
|
|
|
info "not a regular mount point: ${dir}"
|
|
|
|
base="$(basename "${dir}")"
|
|
|
|
dir="$(dirname "${dir}")"
|
|
|
|
base="$(echo "${base}" | sed 's/[\].*$//')"
|
|
|
|
dir="${dir}/${base}"
|
|
|
|
sudo umount -v -f -n "${dir}" 2> /dev/null || \
|
|
|
|
sudo umount -v -f -n -l "${dir}" 2> /dev/null || \
|
|
|
|
error "umount ${dir} unsuccessful!"
|
|
|
|
fi
|
2014-10-14 16:02:12 +00:00
|
|
|
fi
|
|
|
|
done
|
2015-02-10 15:10:52 +00:00
|
|
|
|
2015-08-04 15:59:50 +00:00
|
|
|
if [ "$XTRACE_WAS_SET" == "true" ] ; then
|
|
|
|
set -x
|
2015-08-04 23:41:35 +00:00
|
|
|
true "$FUNCNAME: Restoring xtrace..."
|
2015-08-04 15:59:50 +00:00
|
|
|
fi
|
2014-10-14 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
kill_processes_in_mount() {
|
2015-02-10 15:10:52 +00:00
|
|
|
umount_kill ${1} "false" || :
|
2014-10-14 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
2015-02-10 15:10:52 +00:00
|
|
|
if [ $(basename "${0}") == "umount_kill.sh" -a "${1}" ]; then
|
|
|
|
umount_kill "${1}"
|
2014-10-14 16:02:12 +00:00
|
|
|
fi
|