Orderly save of VirtualBox VM's on logout

This is for discussing general topics about how to use VirtualBox.
Post Reply
lyallp
Posts: 49
Joined: 14. Nov 2008, 01:53
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: Linux & Win xp,7,8

Orderly save of VirtualBox VM's on logout

Post by lyallp »

I am using VirtualBox on Linux with FluxBox as my window manager.
When I logout, I want to be able to execute a script such that any running VM's are 'saved'.
I have written a simple script to save any running VM's but I can't find where to put it where it is useful.
If I put it in the .Xsession file, fluxbox has already exited, killing all GUI apps, before my script runs.
This includes killing all the VirtualBox windows, and thus, the VM's.

Is there a way to I can configure VirtualBox to 'Save' a VM rather than abort it, on exit?

I am currently using 3.0.12 but am actively investigating ways of moving to a later version - given my environmental constraints.

...Lyall
lyallp
Posts: 49
Joined: 14. Nov 2008, 01:53
Primary OS: Linux other
VBox Version: OSE self-compiled
Guest OSses: Linux & Win xp,7,8

Re: Orderly save of VirtualBox VM's on logout

Post by lyallp »

Since I use Linux as my host, the following script works for me.
It's a bit of a hack and I had issues with Xdialog, but I simply put this in my logout script.

Code: Select all

#!/bin/bash
# Script to locate and 'save' all running VirtualBox VM's
# Attempted to use Xdialog to do progress dialogs but if this is used, VBoxManage seems to hang.
# Attempt using X option at your own risk.
#
# Usage : SaveAllVMs.sh [testing] [X]
#
# Written by Lyall Peace (c) January 2010
#
#  Released under GPLv3 (http://www.gnu.org/licenses/gpl.html)
#
TMP="${TMP:-/tmp}"
runningVMs="${TMP}/runningVMs.txt"
if [ "$1" = "test" ]
then
    testing=yes
    shift
else
    testing=no
fi
if [ "$1" = "X" ]
then
    useXDialog=yes
    shift
else
    useXDialog=no
fi

trap 'rm -f "${runningVMs}"' exit

VBoxManage --nologo list runningvms | egrep '^"' > "${runningVMs}"
if [ $? -eq 0 ]
then
    while read theVM
    do
	vmGUID=${theVM##*{}
	vmGUID=${vmGUID%%\}*}

	[ "${useXDialog}" = 'no' ] && echo -e "${vmName} GUID ${vmGUID} is running...\c"
	if [ "${testing}" = "yes" ]
	then
	    [ "${useXDialog}" = 'no' ] && echo "NOT saved."
	    [ "${useXDialog}" = 'yes' ] && Xdialog --title 'Testing Save/Shutdown script' --msgbox "Would save ${theVM}." 10 60
else
	    [ "${useXDialog}" = 'no' ] && echo -e "Saving...\c"
	    if [ "${useXDialog}" = 'yes' ]
	    then
		# | sed -e 's/\.\.\./
                #/g' | 
		VBoxManage --nologo controlvm "${vmGUID}" savestate | Xdialog --title "Saving Running VirtualBox VMs" --progress "Saving ${theVM}" 10 60
		RC=$?
	    else
		VBoxManage --nologo controlvm "${vmGUID}" savestate 
		RC=$?
	    fi
	    if [ $RC -eq 0 ]
	    then
	        [ "${useXDialog}" = 'no' ] && echo "Saved."
	    else
		
		[ "${useXDialog}" = 'no' ] && echo "Save FAILED."
		[ "${useXDialog}" = 'yes' ] && Xdialog --title 'VirtualBox VM Save Failure' --msgbox "Failed to save ${theVM}." 10 60
		echo "VM '${vmName}' '${vmGUID}' Failed to save at $(date) for $(id)" >> /tmp/FailedVmSaves.txt
	    fi
	fi
    done < "${runningVMs}"
fi
Post Reply