# usage: mount-latest-subvol
-# TODO, check our current directory, make sure we arent going to kill ourselves
-
+cd /
[[ $EUID == 0 ]] || exec sudo -E "$BASH_SOURCE" "$@"
errcatch() {
tu /etc/fstab <<<"$l"
done
}
+pid-check() {
+ for p in ${pids}; do
+ for m in ${my_pids[@]}; do
+ if (( p == m )); then
+ echo "$0: error: pids to kill includes our pid or a parent" >&2
+ ps -f -p $p
+ exit 1
+ fi
+ done
+ done
+}
kill-dir() {
for sig; do
echo kill-dir $sig
if pids=$(timeout 4 lsof -t $dir); then
found_pids=true
timeout 4 lsof -w $dir
+ pid-check
kill -$sig $pids
fi
# fuser will find open sockets that lsof won't, for example from gpg-agent.
# note: -v shows kernel processes, which then doesn't return true when we want
- if timeout 4 fuser -m $dir &>/dev/null; then
+ if pids=$(timeout 4 fuser -m $dir 2>/dev/null); then
+ pid-check
found_pids=true
fuser -$sig -mvk $dir
fi
fi
##### end setup fstab for subvols we care about ######
+# get pids that this program depends on so we dont kill them
+my_pids=($$ $PPID)
+loop_limit=30
+count=0
+while [[ ${my_pids[-1]} != 1 && ${my_pids[-1]} != ${my_pids[-2]} && $count -lt $loop_limit ]]; do
+ count=$((count + 1))
+ p=$(ps -p ${my_pids[-1]} -o ppid=)
+ if [[ $p == 0 || ! $p ]]; then
+ break
+ fi
+ my_pids+=($p)
+done
+
+
for vol in q a o i; do
d=/$vol
if ! awk '{print $2}' /etc/fstab | grep -xF $d &>/dev/null; then