Umount script: Kill blocking processes and destroy VMs.

This commit is contained in:
Andreas B. Mundt 2023-09-05 09:41:44 +02:00
parent 11042c2e52
commit a1e47cf64b

View file

@ -9,50 +9,30 @@ uid="$2"
mtp="$3" mtp="$3"
shutdownVMs(){ shutdownVMs(){
local VMs VM local VM
VMs="$(machinectl list --no-legend)" for VM in $(sudo -u $usr XDG_RUNTIME_DIR="/run/user/$uid" \
if [[ -n "$VMs" ]] ; then XDG_CONFIG_HOME="/tmp/$uid/.config/" \
for VM in $VMs ; do XDG_CACHE_HOME="/var/cache/user/$uid/" \
machinectl poweroff "$VM{VM%% *}" || true virsh list --state-running | \
sed -nE "s/.*\s+(\S+)\s+running/\1/p") ; do
sudo -u $usr XDG_RUNTIME_DIR="/run/user/$uid" \
XDG_CONFIG_HOME="/tmp/$uid/.config/" \
XDG_CACHE_HOME="/var/cache/user/$uid/" \
virsh destroy "$VM" 2>&1 | systemd-cat || true
done 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 ## This is the first mount we need to get rid of:
if [[ "$mtp" =~ "/lmn/media/$usr/share" ]] ; then
shutdownVMs shutdownVMs
unbindmnt sleep 5 # leave some time to write caches …
sudo -u ${usr} killall gvfsd | systemd-cat
sudo -u ${usr} killall dbus-daemon | systemd-cat
systemctl kill system-virtiofs.slice
# debug to find processes blocking umount:
# lsof > /tmp/lsof.log
fi fi
## Just umount: ## Just umount: