Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
[Solved]Orderly save/shutdown of VM's on logout
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Unsupported Software
View previous topic :: View next topic  
Author Message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1400
Location: Adelaide/Australia

PostPosted: Tue Jan 12, 2010 12:54 am    Post subject: [Solved]Orderly save/shutdown of VM's on logout Reply with quote

I am using VirtualBox on Linux with FluxBox 1.1.1-r1 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 I can execute my shutdown script before Fluxbox kills all the apps?

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


Last edited by lyallp on Wed Jan 13, 2010 3:23 am; edited 1 time in total
Back to top
View user's profile Send private message
pianosaurus
l33t
l33t


Joined: 19 Apr 2004
Posts: 943
Location: Bash$

PostPosted: Tue Jan 12, 2010 1:38 am    Post subject: Reply with quote

I haven't used fluxbox in ages, so I'm not sure if there is a good place to put it, but you could always use your script to exit fluxbox. Just kill fluxbox at the end of it, stick it in the menu and call it Exit.
_________________
PKA Cuber
Please add [SOLVED] to the subject of your original post when you feel that your problem is resolved.
Adopt an unanswered post
Back to top
View user's profile Send private message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1400
Location: Adelaide/Australia

PostPosted: Tue Jan 12, 2010 12:17 pm    Post subject: Reply with quote

Excellent idea! I will give it a try and update this post with my results.
_________________
...Lyall
Back to top
View user's profile Send private message
lyallp
Veteran
Veteran


Joined: 15 Jul 2004
Posts: 1400
Location: Adelaide/Australia

PostPosted: Wed Jan 13, 2010 3:27 am    Post subject: Reply with quote

A bash script to locate and save all running VM's.
It could probably have been much nicer using Python bindings, but I have not taken the time to learn python. I know bash. :-)

Code:

#!/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


A script to execute the above, then terminate fluxbox.
Code:

#!/bin/bash
# Invoke a script to save all active VM's
echo "Saving VM's..."
bash ~/Scripts/SaveAllVMs.sh
# Now, terminate Fluxbox
echo "Terminating fluxbox..."
pkill fluxbox


The original menu line in the fluxbox ~/.fluxbox/menu file
Code:

    [exit] (Logout) {}

replaced by
Code:

    [exec] (Logout) {xterm "-e" "~/Scripts/SaveVMsThenQuitFluxbox.sh"}

_________________
...Lyall
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Unsupported Software All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum