Re: HOWTO: Gracefully Shutdown VirtualBox VMs on Reboot or Shutdown of Ubuntu
We got Jonners sorted out in the end I believe. Here's a transcript of requirements:
1. Test the creation of a saved state of an open VM, and then successfully open it up again (using the GUI) ?
2. Next stage will be to do this via the command line. Once that works we can look at how to close VMs down when we ask Ubuntu to closedown (can't do anything about Ubuntu crashing I am afraid!), then on how we start a VM when we start Ubuntu (either from fresh, or from a saved state)
Right, now, as it seems your VM is happy to savestate and resume, we can move onto the next stage. I am going to give you fish as well as teach you how to fish, this is why I am doing it in stages.
You need to now learn how to save and resume your VM from a terminal. Get your VM up and running.
1. Open a terminal
2. Type the following:
VBoxManage -nologo list runningvms
You should get back something like this:
("XP" is the name of the vm I am using, replace XP with the name of your vm, but keep the "")
3. Then type as follows:
VBoxManage -nologo controlvm XP savestate
You should see dots and percentages going up from 10%, and your vm will close. You have saved your vm. You can close your Virtualbox application!
4. Now type this (replacing XP with the name of your vm and make sure you put the & at the end!:
VirtualBox --startvm "XP" &
Your vm should now start up where you left off. You will also notice that you have no main application - more screen real estate! You can always open it up if you need it.
Right with all this working OK, you know that your vm is happy being saved and resuming from the cli. Now we need a script to start it up when Ubuntu starts. Let's assume your vm is the only vm you want to start.
Back to the terminal:
sudo nano /usr/bin/startvm.sh
Enter or copy/paste the following into the terminal/nano window
/usr/bin/VirtualBox --startvm XP &
Then press CTRL+X, Y, and Enter.
Make it executable:
sudo chmod 755 /usr/bin/startvm.sh
Now you need to add this script to your startup.
I use Xubuntu so I go to Xfce Settings Manager -> Sessions and Startup, there is a similar place in Ubuntu where you can add a program you want to start on boot (after login!)
the command you want to enter is:
Right that should be it. I have set the timer at 60 seconds, which should ensure your desktop has set itself up and that the vboxdrv driver is in place. You might try editing the script to sleep 30 or less and see if it starts?
OK close your vm (you have the command now!), and reboot. With any luck your vm should start, just where you left off.
You can use CharlesA's shutdown script (a few posts above this one) to gracefully close down your vm's.
No longer participating......