I have many Virtual box machines in my Ubuntu12.04, each time i shutdown or reboot i have to start them one by one.
I wanna a help in writing script for automatically autostart the VBox machines when booting.
I have many Virtual box machines in my Ubuntu12.04, each time i shutdown or reboot i have to start them one by one.
I wanna a help in writing script for automatically autostart the VBox machines when booting.
You can use the VirtualBox Auto-start service. A good tutorial describing how to do this is posted on the "Life of a Geek Admin" blog.
The following steps are adapted from the linked blog post:
First you need to create the file
/etc/default/virtualbox
and add a few variables.VBOXAUTOSTART_DB which contains an absolute path to the autostart database directory and
VBOXAUTOSTART_CONFIG which contains the location of the autostart config settings. The file should look similar to this:
Now we need to create the
/etc/vbox/vbox.cfg
file and addNote: If the filename
vbox.cfg
doesn't work above, try naming itautostart.cfg
.If you are the only user you can just add the line
default_policy = allow
to thevbox.cfg
file.Set permissions on directory to the vboxuser group and make sure users can write to the directory as well as sticky bit.
Add each of the users to the
vboxusers
group.(replace
USERNAME
with the username)NOTE: If you have changed group permissions for the current user, log out and back in again to refresh the permissions. (credit @kR105)
Every user who wants to enable autostart for individual machines has to set the path to the autostart database directory with
and enable autostart for an individual VM with
This will create a
myuserid.start
file in/etc/vbox
directoryNow restart the vboxautostart-service to read in the changes.
Reboot your system and your VM should start
I had similar unhappy incidents trying this operation on the vanilla LTS.
On this version, the key file /etc/init.d/vboxautostart-service was not installed.
As far as I know all the VitualBox and requirements were put in by apt-get, so I cannot say why the 'vboxautostart-service' file was not also provided. But to get over this here are my update to kdmurray's post.
1) /etc/default/virtualbox file existed for me. So must add vars:
2) Must create /etc/vbox/autostart.cfg as indicated by OP.
6b) Need to get a vboxautostart-service script and make it executable.
6c) Alert the rc.d controller, but I used 24 as the start time. Putting just 20 and it did not start up. Perhaps it ran even before virtualbox was working.
Then rebooting launched the VM correctly.
You can use
vboxmanage startvm "my virtual machine" --type=headless|gui|sdl
(one of those). Use "headless" if they're servers that you connect to by other means than using the gui.To actually run these commands at the right time during boot, you'll want to read up on Upstart.
After many unhappy hours trying to get the procedures in from kdmurray above to work, without success I eventually found a method that worked simply.
First my Linux host (Mint 17) was set up at boot time to login automatically to my main account.
Second I used the facilities in VirtualBox GUI (v4.3.12) to create a shortcut for each Virtual Machine on the desktop. {right-click the VM in left column & select "Create Shortcut on Desktop"}
Next: I had already found that bringing up Menu->Control Centre -> Startup Applications -> Add and adding the desktop shortcuts gave files that did not work, I spotted from other answer on this page that the location of the startups is /home/USERNAME/.config/autostart so I did a right-click on each of the desktop VM shortcuts and then pasted them into that folder, overwriting the earlier crap files. Now they developed the 'pretty' VirtualBox icons.
And sure enough on restarting the host computer, the 3 VMs started automagically.
:-)
Bill Williams
PS: alas I still have something missing, because often the Virtual Machines will not start up on boot, instead they generate an error dialog box which says they are locked, even if I have told them to shutdown before the host was re-booted.
In addition to the good description of the vboxautostart-service above, you can do
to automatically stop the guest at shutdown. For Ubuntu 14.04 I had to add a sleep 30 to the vboxautostart-service stop section, so that it waits until guest has been saved completely.
I found a simpler way under Ubuntu 16.04 running VBox 5.1.34.
While in VBox, right-click on the running or idle VM in the list, select 'Create Shortcut on Desktop". Once you see the icon on your desktop, open with any tool that let's you see the underlying command VBox created (on my system, I right click and select 'properties') Copy this to the clipboard.
Open the program to update 'startup applications' and create a new application that starts at boot time, pasting in the command you copied from the desktop link created by VirtualBox.
To start a VM on logging in to our session we may want to define a .desktop file:
When this file was copied or moved to our autostart directory it will then be executed after we log in:
Running Virtual Box in user space may help to avoid unwanted side effects of starting them during boot (e.g. the VM runs as root, boot time increases incredibly, ...).
This is how i start my Centos (name of my virtual machine) when my host machine reboots. I use crontab for that job. Here is my crontab entry:
This can be done using VirtualBox Command Line Management Interface. Just run the following script at start up.
There is a nice tutorial for more info.
Addon to @kdmurray answer.
If you get error on:
To solve this, I had to manually create files "username".start and "username".stop with:
And change owner with:
rerun:
then restart service:
check is all is ok with:
and you should see "VBox..." process in a list, if virtual machine is running