lmn-client/roles/lmn_vm/files/pam-umount.sh
2023-09-02 16:19:05 +02:00

59 lines
1.1 KiB
Bash

#!/usr/bin/bash
#
# <umount>/usr/local/sbin/pam-umount.sh %(USER) %(USERUID) %(MNTPT)</umount>'
set -eu
usr="$1"
uid="$2"
mtp="$3"
shutdownVMs(){
local VMs VM
VMs="$(machinectl list --no-legend)"
if [[ -n "$VMs" ]] ; then
for VM in $VMs ; do
machinectl poweroff "$VM{VM%% *}" || true
done
sleep 3
fi
## Check again:
VMs="$(machinectl list --no-legend)"
if [[ -n "$VMs" ]] ; then
sleep 3
for VM in $VMs ; do
machinectl terminate "${VM%% *}" || true
done
fi
#killall -9 virtiofsd
}
unbindmnt(){
local lmuh="/lmn/media/$usr/home"
local lmus="/lmn/media/$usr/share"
local N=0 mh=true ms=true
mountpoint -q $lmuh || mh=false
mountpoint -q $lmus || ms=false
while [[ $N -lt 10 ]] && ([[ $mh = true ]] || [[ $ms = true ]]) ; do
if [[ $mh = true ]] ; then
umount "$lmuh" && mh=false
fi
if [[ $ms = true ]] ; then
umount "$lmus" && ms=false
fi
N=$((N + 1))
sleep 1
done
systemd-cat echo "Tried $N umount(s)."
}
######################
if [[ "$mtp" =~ "/srv/samba/schools" ]] ; then
shutdownVMs
unbindmnt
fi
## Just umount:
exec umount "$mtp"