1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
# -*- sh -*-
# vim:ft=sh:ts=8:sw=4:noet
AddConfigHandler VirtualboxOptions
AddConfigHelp "SuspendVirtualbox <boolean>" "This will run \"VBoxManage controlvm <VM> savestate\" for each running VM and restore state on resume."
VirtualboxSavestate() {
SHUTDOWN_USERS=""
for i in /tmp/.vbox-*-ipc; do
SHUTDOWN_USERS="$SHUTDOWN_USERS $(echo $i|cut -d'-' -f2)"
done
VIRTUALBOX_STOPPED=""
for user in $SHUTDOWN_USERS; do
if [ -d /tmp/.vbox-$user-ipc ]; then
export VBOX_IPC_SOCKETID="$user"
VMS=`VBoxManage --nologo list runningvms 2>/dev/null`
if [ $? -eq 0 -a -n "$VMS" ]; then
VMS=`echo "$VMS" | sed -e 's/^".*".*{\(.*\)}/\1/'`
vecho 1 "Saving state of remaining VMs from user $user"
for vm in $VMS; do
VBoxManage --nologo controlvm $vm savestate
VIRTUALBOX_STOPPED="$VIRTUALBOX_STOPPED $user:$vm"
done
fi
fi
done
# Tries to remove the VirtualBox modules 21 times before giving up,
# so in practice it sleeps up to 20 seconds. This is done because
# the modules might cause some problems on resume.
local counter
local lsmod_output
VIRTUALBOX_RMMODED=""
counter=0
while [ $counter -le 21 ]
do
lsmod_output=""
lsmod_output=$(lsmod | grep vbox)
if [ -n "$lsmod_output" ]
then
sleep 1s
modprobe -r vboxnetadp
modprobe -r vboxnetflt
modprobe -r vboxdrv
VIRTUALBOX_RMMODED="yes"
fi
counter=$(($counter + 1))
done
# Failure is not USUALLY fatal, but storing VM state saves reasonable
# amount of memory. I say "USUALLY" as I'm not sure if it is virtualbox
# that causes some suspend-related bugs.
return 0
}
VirtualboxRestorestate() {
local vmtag
local user
local vm
# reloads the VirtualBox modules
if [ -n "$VIRTUALBOX_RMMODED" ]; then
modprobe vboxdrv
modprobe vboxnetflt
modprobe vboxnetadp
fi
for vmtag in $VIRTUALBOX_STOPPED; do
user=$(echo $vmtag | cut -d : -f 1)
vm=$(echo $vmtag | cut -d : -f 2)
vecho 1 "Restoring vm $vm for user $user"
su "$user" -c "VBoxManage -nologo startvm $vm"
done
unset VIRTUALBOX_STOPPED
return 0
}
VirtualboxOptions() {
case $1 in
suspendvirtualbox)
if BoolIsOn "$1" "$2" && [ -z "$VIRTUALBOX_HOOKED" ] ; then
AddSuspendHook 20 VirtualboxSavestate
AddResumeHook 20 VirtualboxRestorestate
VIRTUALBOX_HOOKED=1
VIRTUALBOX_STOPPED=""
fi
return 0
;;
esac
return 1
}
|