Page 1 of 5 123 ... LastLast
Results 1 to 10 of 42

Thread: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

  1. #1
    Join Date
    Mar 2006
    Beans
    Hidden!

    Smile HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    I always forget I have a GUI virtual machine open when I go to shutdown, usually because it is on another desktop, out of sight. This howto will show you how to avoid the same thing, and to put any VMs you have open into a saved state, ready for a quick start up next time.

    Before we go any further...

    What this howto will not do:

    1. Save VMs when you CTRL+ALT+BKSPACE.
    2. Save VMS when you Log Out.
    3. Save VMs from VMware, Xen, Qemu, etc.

    That out of the way, all the work is done by a bash script and a bit of configuration on your system. As with all things, there are some caveats
    to be mentioned. The script will work on headless as well as GUI virtual machines, however, there seems to be a bug in VirtualBox that will not allow a saved GUI VM open up as Headless. A saved Headless VM will happily open up as a GUI VM, but for some reason not around the other way. My focus is on sorting things out for desktop GUI VMs.

    The difficulties arise in using a script like vboxtool (which handles headless VMs well, is that all the work has to happen while the window and display managers (GDM/GNOME or GDM/XFCE) are still up and running, hence why you cannot simply write a script and pop it in "init.d" and then update-rc.d it. I know, I spent a week on it!

    So we have to run the script from the desktop while the GUI is up, in order to safely close/save desktop VMs. Adding on the shutdown and reboot routines just adds to the fun

    Requirements:

    1. Virtualbox (tested on closed source version 2.1.4)
    2. Installed virtual machines (do your own thing here)
    3. Debian derivative distro with sudoers file (requires editing)
    4. Zenity (for GUI dialogs)
    5. Replacement of <!user!> with your username in the script and in the sudoers file

    This username should be the logged on user and that of the creator /installer of VirtualBox. (So take a default installation of Ubuntu where you create a user as a part of the install. Once complete you install VBox as that user, and so on)

    Setup:

    Assumes knowledge of installing VirtualBox and VMs

    Copy the script to your home directory and make executable
    Code:
    chmod +x ~/shut.vm
    Install zenity:
    Code:
    sudo apt-get install zenity
    Edit your sudoers file (in a terminal):

    Code:
    sudo visudo
    scroll to the bottom of the file and add the following

    Code:
    <!user!> ALL = NOPASSWD: /sbin/shutdown
    <!user!> ALL = NOPASSWD: /etc/init.d/vboxdrv
    <!user!> ALL = NOPASSWD: /etc/init.d/preload
    replacing <!user!> with your username
    And save out the file. The effects are immediate.
    These changes allow your user (you) to run the shutdown, vboxdrv stop
    and preload stop commands without having to enter your password.


    Look in the script for any lines that contain:
    Code:
    sudo -H -u -<!user!>
    and replace <!user!> with your username (there are 4 lines!)
    Code:
    sudo nano ~/shut.vm
    Running the script:

    Assuming code is in your home directory and you have made it executable,
    you can then run the script from a terminal:
    Code:
    ~/shut.vm
    An easier way is to set up a launcher and point the launcher
    to the script. Because of the different ways of doing this in various
    distros the actual process of adding a button to your taskbar will
    not be covered here, but documentation is widely available.

    Comments:

    I found that I needed to close the vboxdrv and preload services on
    my test machine, before shutdown/reboot. If you don't have preload
    installed, the command doesn't appear to have any adverse effect.
    If it does, then remove the lines with
    Code:
    sudo /etc/init.d/preload stop
    in them, or comment them out with a "#" at the beginning of the line.
    Code:
    sudo nano ~/shut.vm
    to edit.

    If you run the script from a terminal, apart from the zenity dialogs
    you should get plenty of feedback about what is going on and where
    you are in the process.

    Also plenty of more info in the script about what is going on


    The script in all its glory:
    #!/bin/bash
    # /shut.vm
    # Author: Jose Catre-Vandis (Joe90)
    # Web: www.bimma.me.uk

    # Tested on XUBUNTU 8.10 with VirtualBox 2.1.4 (2.1_2.1.4-42893)

    ################################################## ######################

    # Requirements

    # Virtualbox
    # Installed virtual machines
    # Debian derivative distro with sudoers file (requires editing)
    # Zenity
    # Replacement of <!user!> with your username in the script and in
    #+the sudoers file
    # This username should be the logged on user and that of the creator
    #+/installer of VirtualBox. (So take a default installation of Ubuntu
    #+where you create a user as a part of the install. Once complete you
    #+install VBox as that user, and so on)

    ################################################## ######################

    # Setup

    # Assumes knowledge of installing VirtualBox and VMs
    # Install zenity: "sudo apt-get install zenity"
    # edit your sudoers file (in a terminal):

    # sudo visudo
    # scroll to the bottom of the file and add the following
    #+(don't include the #'s in the sudoers file!):

    # <!user!> ALL = NOPASSWD: /sbin/shutdown
    # <!user!> ALL = NOPASSWD: /etc/init.d/vboxdrv
    # <!user!> ALL = NOPASSWD: /etc/init.d/preload

    # replacing <!user!> with your username
    # And save out the file. The effects are immediate.

    # Look in the script for any lines that contain:
    # sudo -H -u -<!user!>
    # replace <!user!> with your username

    ################################################## ######################

    # Running the script

    # Script Location. you can put it anywhere you like. I run it out of
    #+my home directory. You will need to make it executable:
    # chmod +x ~/shut.vm

    # You can then run the script from a terminal:
    # ~/shut.vm

    # An easier way is to set up a launcher and point the launcher
    #+to the script. Because of the different ways of doing this in various
    #+distros the actual process of adding a button to your taskbar will
    #+not be covered here, but documentation is widely available.

    ################################################## ######################

    # Lists running VMs and puts in array RUNVMLIST

    # Here we first iterate through a list of any VMs that
    #+are running, then we extract the names of the VMs and
    #+add them to an array.
    # Then we report (in a terminal if run that way), which
    #+VMs are running and how many there are.

    ################################################## ######################

    declare -a RUNVMLIST
    vmnum=0

    for running in $(VBoxManage -nologo list runningvms)
    do
    name=$(VBoxManage -nologo showvminfo $running | grep "Name:" | awk 'BEGIN{FS="Name: "}{print $2}')
    RUNVMLIST=( "${RUNVMLIST[@]}" "$name")
    vmnum=$(($vmnum + 1))
    done



    if [ ! "$running" ]
    then
    echo "No VMs running"
    else
    echo "Running VMs = "${RUNVMLIST[*]}
    echo "Number of VMs Running = "${#RUNVMLIST[*]}
    fi

    ################################################## ######################

    # Takes all this info and uses it as you shut down or reboot
    # to gracefully close and save any open VMs

    # First off you get a Zenity dialog asking if you want to
    #+Shutdown or Reboot, or Cancel the closedown. If you click
    #+OK, you then are provided with a second Zenity dialog asking
    #+if you want to Shutdown (OK) or Reboot (Cancel).
    # If you choose OK (the Shutdown routine) the script then has a
    #+look at the array to see if there are any running VMs. If there
    #+are no running VMs, the script will proceed directly to Shutdown.
    # If there are running VMs, the script will first save the state of
    #+each VM in turn, putting up a Zenity progress dialog which you
    #+can leave alone as it auto closes, then we proceed to Shutdown.
    # A similar thing happens if you choose Cancel (the Reboot routine)
    #+only that after the save of states, you go to Reboot.

    # I found that I needed to close the vboxdrv and preload services on
    #+my test machine, before shutdown/reboot. If you don't have preload
    #+installed, the command doesn't appear to have any adverse effect.
    #+If it does, then remove the lines with "sudo /etc/init.d/preload stop"
    #+in them, or comment them out with a "#" at the beginning of the line.

    ################################################## ######################

    zenity --text="Click OK if you want to Shutdown or Reboot. Click Cancel to resume." --title="Shutdown or Reboot?" --width="320" --height="150" --question
    closeorcancel=$?
    if [ "${closeorcancel}" == "0" ]
    then
    echo "You chose to Shutdown or Reboot"
    else
    echo "You chose to Cancel and resume your work"
    exit
    fi
    zenity --text="Click OK if you want to Shutdown. Click Cancel to Reboot." --title="Shutdown or Reboot?" --width="320" --height="150" --question
    shutorboot=$?
    if [ "${shutorboot}" == "0" ]
    then
    echo "You chose to Shutdown"
    if [ ! ${RUNVMLIST[*]}]
    then
    echo "No VMs open, straight to shutdown"
    sudo -H -u <!user!> /etc/init.d/vboxdrv stop
    sudo /etc/init.d/preload stop
    sudo /sbin/shutdown -h now
    else
    echo "All open VMs will be closed to a saved state"
    for vm in ${RUNVMLIST[*]}
    do
    (VBoxManage controlvm $vm savestate) | zenity --progress --pulsate --width="320" --height="150" --title="Please Wait while VMs are Saved" --auto-close
    echo "$vm was closed to a saved state"
    done
    echo "Shutting down now"
    sudo -H -u <!user!> /etc/init.d/vboxdrv stop
    sudo /etc/init.d/preload stop
    sudo /sbin/shutdown -h now
    fi
    else
    echo "You chose to Reboot"
    if [ ! ${RUNVMLIST[*]}]
    then
    echo "No VMs open, straight to reboot"
    sudo -H -u <!user!> /etc/init.d/vboxdrv stop
    sudo /etc/init.d/preload stop
    sudo /sbin/shutdown -r now
    else
    echo "All open VMs will be closed to a saved state"
    for vm in ${RUNVMLIST[*]}
    do
    (VBoxManage controlvm $vm savestate) | zenity --progress --pulsate --width="320" --height="150" --title="Please Wait while VMs are Saved" --auto-close
    echo "$vm was closed to a saved state"
    done
    echo "Rebooting Now"
    sudo -H -u <!user!> /etc/init.d/vboxdrv stop
    sudo /etc/init.d/preload stop
    sudo /sbin/shutdown -r now
    fi
    fi

    exit
    I have also attached the script in a zip file for downloading.

    Undoing what you have done:

    1. Delete the script!
    2. Reverse the changes made in your sudoers file
    3. Uninstall zenity (though not necessary)
    Code:
    sudo apt-get remove zenity
    Happy to help with questions, comments, problems, etc, and will probably add some simpler scripts for just saving VMs or gathering info. Will also welcome any suggestions for improvements or developments of the script. I would particularly like to know how to link this up with the standard shutdown/reboot buttons in Ubuntu/Xubuntu to avoid having to create a new shortcut to the script, and streamline things...

    Credits to the writer/s of vboxtool, the best starter-stopper of headless VMs there is (imho), for a couple of snippets of code here and there, and to markba on Ubuntuforums, and TerryE on virtualbox forums for help and pointers
    Attached Files Attached Files
    Last edited by Jose Catre-Vandis; March 2nd, 2009 at 10:50 AM.
    No longer participating......

  2. #2
    Join Date
    Mar 2007
    Beans
    8

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    I'm getting errors when running the script:

    $ sh shut.vm
    shut.vm: 74: declare: not found
    shut.vm: 80: Syntax error: "(" unexpected (expecting "done")

  3. #3
    Join Date
    Mar 2006
    Beans
    Hidden!

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    Quote Originally Posted by woodenfox View Post
    I'm getting errors when running the script:

    $ sh shut.vm
    shut.vm: 74: declare: not found
    shut.vm: 80: Syntax error: "(" unexpected (expecting "done")
    Hmmm, that appears to be related to this part of the code:
    Code:
    declare -a RUNVMLIST
    vmnum=0
    
       for running in $(VBoxManage -nologo list runningvms)
        do
        	name=$(VBoxManage -nologo showvminfo $running | grep "Name:" | awk 'BEGIN{FS="Name:            "}{print $2}')
    	    RUNVMLIST=( "${RUNVMLIST[@]}" "$name")
    	    vmnum=$(($vmnum + 1))
        done
    
    
    
       if [ ! "$running" ] 
        then
    	 echo "No VMs running"
        else
         echo "Running VMs = "${RUNVMLIST[*]}
    	 echo "Number of VMs Running = "${#RUNVMLIST[*]}
       fi
    (lines 74 through to 92)

    Open a terminal and try typing:
    Code:
    declare -a RUNVMLIST
    to see what happens (you should just get taken back to a command prompt.

    If this works OK, copy the above code into a text file, add a "#!/bin/bash" to the top of the file save it and make it executable. then try running it in a terminal to see what you get?
    No longer participating......

  4. #4
    Join Date
    Sep 2007
    Beans
    29

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    You have to run that script with bash - i.e.:
    Code:
    $ bash shut.vm
    Instead of:
    Code:
    $ sh shut.vm
    A better way is to just do:
    Code:
    chmod +x shut.vm
    then:
    Code:
    ./shut.vm

  5. #5
    Join Date
    Sep 2009
    Location
    London
    Beans
    689
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    Hi I tried this, following step by step - bearing in mind I am not a techie, so just copied, but it did not work. I tried a couple of reboots but the virtual machine would not start.

    No adverse effects, just did not work. Any ideas?

  6. #6
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    Hi there,

    I wrote up a quick and dirty script to do what you want.

    Only hitch is that you need to add this to the sudoers file with sudo visudo

    Code:
    <username> ALL = NOPASSWD: /sbin/shutdown
    Here's the scripts:

    stopvmgui.sh
    Code:
    #!/bin/bash
    
    ###
    ### stopvmgui.sh
    ### Saves state on any running VM and prompts to shutdown or restart
    ### Created by CharlesA
    ### Tested 12/01/2010
    ###
    
    USR=/usr/bin/
    
    ${USR}zenity --question --text Shutdown? --title Shutdown?
    if [[ $? = 0 ]]
    then
      if [[ -n $(${USR}VBoxManage -nologo list runningvms) ]]
      then
      for runningvms in $(${USR}VBoxManage -nologo list runningvms | ${USR}awk -F \" '{ print $2 }';)
      do
      ${USR}VBoxManage -nologo controlvm $runningvms savestate && sudo shutdown -h now
      done
      else
      echo "No VMs running" && sudo shutdown -h now
      fi
    else
    ${USR}zenity --question --text Reboot? --title Reboot?
    if [[ $? = 0 ]]
      then
        if [[ -n $(${USR}VBoxManage -nologo list runningvms) ]]
        then
        for runningvms in $(${USR}VBoxManage -nologo list runningvms | ${USR}awk -F \" '{ print $2 }';)
        do
        ${USR}VBoxManage -nologo controlvm $runningvms savestate && sudo shutdown -r now
        done
        else
        echo "No VMs running" && sudo shutdown -r now
        fi
      else
        exit
        fi
    fi
    # eof
    startvmgui.sh
    Code:
    #!/bin/bash
    
    ###
    ### startvm.sh
    ### Script to start saved VMs
    ### Created by CharlesA
    ### Tested 12/01/2010
    ###
    
    BIN=/bin/
    USR=/usr/bin/
    
    for vms in $(${USR}VBoxManage -nologo list vms | ${USR}awk -F \" '{ print $2 }';)
    do
    if [[ -n $(${USR}VBoxManage showvminfo $vms | ${BIN}grep saved) ]]
    then
    ${USR}VirtualBox --startvm $vms &
    fi
    done
    # eof
    Place both scripts in yer home directory and create a launcher on the desktop to stopvmgui.sh.

    Add startvmgui.sh to System > Preferences > Startup Applications.

    Double click on the launcher and it'll run the script.

    Thanks to Jose Catre-Vandis, for giving me a base to work with for zenity.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  7. #7
    Join Date
    Sep 2009
    Location
    London
    Beans
    689
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    thanks Charles.
    I assumed that to create a launcher on the desktop you meant make a link, which I have done and added it to the Startup apps.

    I also assume all I need do is wait until I need to reboot and the scripts will take care of starting up my virtual machine.

  8. #8
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    You'd just have to launch the stopvm script and it'll ask you if you want to shutdown, hit no and it'll ask you if you want to reboot.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

  9. #9
    Join Date
    Sep 2009
    Location
    London
    Beans
    689
    Distro
    Xubuntu 13.04 Raring Ringtail

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    Quote Originally Posted by CharlesA View Post
    You'd just have to launch the stopvm script and it'll ask you if you want to shutdown, hit no and it'll ask you if you want to reboot.
    Did that, and yes it did, so that kinda does what you'd expect.

    I did a reboot, but again, whilst the virtualbox ap opened, as it did before, the virtual machine within it still does not - wife gave me a ticking off, she was mid call to her mother. Oh, err....! I'll check next time!

  10. #10
    Join Date
    Oct 2009
    Beans
    Hidden!
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu

    Did it save the state of the VM at least?

    Also where did you put the scripts? They are meant to be run from the home directory, but I could modify them to run from anywhere without much difficulty.
    Come to #ubuntuforums! We have cookies! | Basic Ubuntu Security Guide

    Tomorrow's an illusion and yesterday's a dream, today is a solution...

Page 1 of 5 123 ... LastLast

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •