1
0
Fork 0
dotfiles/desktop-bin/resec

71 lines
2.1 KiB
Bash
Executable File

#!/bin/bash
#
# Forcefully unmount ~/.enc and run any hooks
#
# Copyright (C) 2013 Mike Gerwitz
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# This script mercilessly unmounts ~/.enc by killing any processes that are
# using files within it, running hooks both before and after. The preunmount
# hook has the chance to abort or delay the operation (delay by re-invoking
# this script).
#
# Run this as root to be certain that unmount will succeed.
##
encpath="$HOME/.enc"
avail="$encpath/.available"
preunmount="$encpath/.preunmount"
postunmount="$encpath/.postunmount"
# if not mounted, then abort
[ -e "$avail" ] || exit
# execute pre-mount script to allow system-specific preparation
[ -x "$preunmount" ] && {
"$preunmount" || {
err=$?
echo "fatal: $preunmount failed!" >&2
exit $err
}
}
# kill anything using this process, attempting to do so gracefully first by
# giving them some time to handle SIGTERM, after which we force any
# remaining processes to terminate
s=5
fuser -Mm "$encpath" -k -TERM \
&& echo "Waiting $s seconds for above processes to terminate (SIGTERM)..." \
&& sleep "$s" \
&& echo "Terminating any remaining processes (SIGKILL)..." \
&& fuser -Mm "$encpath" -k -KILL
# now that no processes are using the directory, unmount
fusermount -u "$encpath" \
&& {
[ ! -x "$postunmount" ] || "$postunmount" || {
err=$?
echo "warning: unmounted, but $postunmount failed!"
exit $err
}
} \
|| {
err=$?
echo "fatal: umount failed!"
exit $?
}
echo "$encpath unmounted."