2014-10-14 16:02:12 +00:00
|
|
|
#!/bin/bash -e
|
|
|
|
|
|
|
|
#
|
|
|
|
# Written by Jason Mehring (nrgaway@gmail.com)
|
|
|
|
#
|
|
|
|
|
|
|
|
# Kills any processes within the mounted location and
|
|
|
|
# unmounts any mounts active within.
|
|
|
|
#
|
|
|
|
# To keep the actual mount mounted, add a '/' to end
|
|
|
|
#
|
|
|
|
# $1: directory to umount
|
|
|
|
#
|
|
|
|
# Examples:
|
|
|
|
# To kill all processes and mounts within 'chroot-jessie' but keep
|
|
|
|
# 'chroot-jessie' mounted:
|
|
|
|
#
|
2014-10-14 21:04:42 +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
|
2014-10-14 16:02:12 +00:00
|
|
|
#
|
|
|
|
|
2014-10-16 16:03:05 +00:00
|
|
|
. ./functions.sh
|
|
|
|
|
2014-10-14 16:02:12 +00:00
|
|
|
# $1 = full path to mount;
|
|
|
|
# $2 = if set will not umount; only kill processes in mount
|
2014-10-14 21:04:42 +00:00
|
|
|
umount_kill() {
|
2014-10-14 16:02:12 +00:00
|
|
|
MOUNTDIR="$1"
|
|
|
|
|
|
|
|
# We need absolute paths here so we don't kill everything
|
2014-10-28 08:09:55 +00:00
|
|
|
if ! [[ "${MOUNTDIR}" = /* ]]; then
|
2014-10-14 16:02:12 +00:00
|
|
|
MOUNTDIR="${PWD}/${MOUNTDIR}"
|
|
|
|
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
|
2014-10-28 08:09:55 +00:00
|
|
|
MOUNTDIR=$(echo "${MOUNTDIR}" | sed s#//*#/#g)
|
2014-10-14 20:30:12 +00:00
|
|
|
|
2014-10-28 08:09:55 +00:00
|
|
|
warn "-> Attempting to kill any processes still running in '${MOUNTDIR}' before un-mounting"
|
|
|
|
for dir in $(sudo grep "${MOUNTDIR}" /proc/mounts | cut -f2 -d" " | sort -r | grep "^${MOUNTDIR}")
|
2014-10-14 16:02:12 +00:00
|
|
|
do
|
|
|
|
sudo lsof "$dir" 2> /dev/null | \
|
|
|
|
grep "$dir" | \
|
|
|
|
tail -n +2 | \
|
|
|
|
awk '{print $2}' | \
|
|
|
|
xargs --no-run-if-empty sudo kill -9
|
|
|
|
|
|
|
|
if ! [ "$2" ] && $(mountpoint -q "$dir"); then
|
2014-10-18 03:34:01 +00:00
|
|
|
info "un-mounting $dir"
|
2014-10-14 16:02:12 +00:00
|
|
|
sudo umount -n "$dir" 2> /dev/null || \
|
|
|
|
sudo umount -n -l "$dir" 2> /dev/null || \
|
2014-10-16 16:03:05 +00:00
|
|
|
error "umount $dir unsuccessful!"
|
|
|
|
elif ! [ "$2" ]; then
|
|
|
|
# Look for (deleted) mountpoints
|
2014-10-18 03:34:01 +00:00
|
|
|
info "not a regular mount point: $dir"
|
2014-10-16 16:03:05 +00:00
|
|
|
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!"
|
2014-10-14 16:02:12 +00:00
|
|
|
fi
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
kill_processes_in_mount() {
|
2014-10-14 21:04:42 +00:00
|
|
|
umount_kill $1 "false" || :
|
2014-10-14 16:02:12 +00:00
|
|
|
}
|
|
|
|
|
2014-10-14 21:04:42 +00:00
|
|
|
if [ $(basename "$0") == "umount_kill.sh" -a "$1" ]; then
|
|
|
|
umount_kill "$1"
|
2014-10-14 16:02:12 +00:00
|
|
|
fi
|