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.
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It only takes a minute to sign up.
Sign up to join this communityI 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:
# virtualbox defaults file
VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/vbox.cfg
Now we need to create the /etc/vbox/vbox.cfg
file and add
# Default policy is to deny starting a VM, the other option is "allow".
default_policy = deny
# Create an entry for each user allowed to run autostart
myuserid = {
allow = true
}
Note: If the filename vbox.cfg
doesn't work above, try naming it autostart.cfg
.
If you are the only user you can just add the line default_policy = allow
to the vbox.cfg
file.
Set permissions on directory to the vboxuser group and make sure users can write to the directory as well as sticky bit.
sudo chgrp vboxusers /etc/vbox
sudo chmod 1775 /etc/vbox
Add each of the users to the vboxusers
group.
sudo usermod -a -G vboxusers USERNAME
(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
VBoxManage setproperty autostartdbpath /etc/vbox
and enable autostart for an individual VM with
VBoxManage modifyvm <uuid|vmname> --autostart-enabled on
This will create a myuserid.start
file in /etc/vbox
directory
Now restart the vboxautostart-service to read in the changes.
sudo service vboxautostart-service restart
Reboot your system and your VM should start
I had similar unhappy incidents trying this operation on the vanilla LTS.
~$ cat /etc/os-release
NAME="Ubuntu"
VERSION="14.04.1 LTS, Trusty Tahr"
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:
VBOXAUTOSTART_DB=/etc/vbox
VBOXAUTOSTART_CONFIG=/etc/vbox/autostart.cfg
2) Must create /etc/vbox/autostart.cfg as indicated by OP.
6b) Need to get a vboxautostart-service script and make it executable.
cd /etc/init.d/
sudo wget http://www.virtualbox.org/browser/vbox/trunk/src/VBox/Installer/linux/vboxautostart-service.sh?format=raw -O vboxautostart-service
sudo chmod +x vboxautostart-service
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.
sudo update-rc.d vboxautostart-service defaults 24 24
Then rebooting launched the VM correctly.
vboxdrv
in the # Required Start
/ # Required Stop
lines by virtualbox
. It seems that they renamed the service, so the OS cannot detect start/stop priorities correctly. I think you don't need step 6c if you replaced those lines.
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
VBoxManage modifyvm server --autostop-type savestate
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:
@reboot VBoxHeadless --startvm "Centos" &
This can be done using VirtualBox Command Line Management Interface. Just run the following script at start up.
#!/bin/bash
VMUSER="vmuser"
VMNAME="RedHatVM" #also you can put UUID in this variable (VMNAME="4ec6acc1-a232-566d-a040-6bc4aadc19a6")
case "$1" in
start)
echo "===Starting VirtualBox VM==="
sudo -H -u $VMUSER VBoxManage startvm "$VMNAME" --type headless
;;
stop)
echo "===Saving state of Virtualbox VM==="
sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" savestate
sleep 20
;;
shutdown)
echo "===Shutting down Virtualbox VM==="
sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" acpipowerbutton
sleep 20
;;
reset)
echo "===Resetting VirtualBox VM==="
sudo -H -u $VMUSER VBoxManage controlvm "$VMNAME" reset
;;
status)
echo -n "VMNAME->";sudo -H -u $VMUSER VBoxManage showvminfo "$VMNAME" --machinereadable |grep "VMState="| cut -d "=" -f2
;;
echo "Usage: /etc/init.d/VMscript {start|stop|shutdown|reset|status}"
exit 1
;;
esac
exit 0
There is a nice tutorial for more info.
Addon to @kdmurray answer.
If you get error on:
VBoxManage modifyvm <uuid|vmname> --autostart-enabled on
To solve this, I had to manually create files "username".start and "username".stop with:
sudo touch "username".start
sudo touch "username".stop
And change owner with:
sudo chown "username" "username".start
sudo chown "username" "username".stop
rerun:
VBoxManage modifyvm <uuid|vmname> --autostart-enabled on
then restart service:
sudo service vboxautostart-service stop
sudo service vboxautostart-service start
check is all is ok with:
top
and you should see "VBox..." process in a list, if virtual machine is running
On my Ubuntu 20 desktop machine:
Open crontab:
crontab -e
Add this line at the bottom:
@reboot sleep 10 && VBoxHeadless --startvm "MyVirtualMachineName" &
Reboot to confirm it works.
You could also get the gnome session manager described here. Gnome Session Manager
It is in the sofware center or you could just go to a terminal and type
sudo apt-get install gnome-session-common
Next you can create a little script to run and use the gnome session manager to start it. Here is a little sample script to get you going...
#!/bin/sh
#vboxmanage startvm "my virtual machine" --type=headless|gui|sdl (one of those)
# So if your username is user, and virtualbox put your VMs in the default location, and you want headless...
vboxmanage startvm '/home/user/VirtualBox VMs/myvmname' --type=gui
For Ubuntu server 16.04.1 LTS I solved this by doing:
sudo systemctl enable rc-local.service
/etc/rc.local
su virtual_box_user -c "VBoxHeadless -startvm 'virtual_machine_name' &"
Note that the virtual_box_user is the one that can see the virtual machine (that you are willing to start) executing the command:
VBoxManage list vms
Example:
$ VBoxManage list vms
"virtualbox1" {2eb3ba3b-bcb3-4515-95d9-d33bce214c28}
"virtualbox2" {62365634-f2q4-40c0-b4ed-22f4ab84441a}
The modified /etc/rc.local
should look like this
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.
su bob -c "VBoxManage startvm 'virtualbox1' --type headless &"
su bob -c "VBoxManage startvm 'virtualbox2' --type headless &"
exit 0
If you don't choose the right owner of virtualbox, you should see the error typing the command:
sudo systemctl status rc-local.service
I hope this can help
1 - Create a file:
nano /etc/init.d/vm-start
Paste this:
#!/bin/bash
/bin/echo "* Starting VMS..."
/bin/sleep 1
/bin/su validuser -c "/usr/bin/vboxmanage startvm MACHINENAME--type headless"
/bin/sleep 1
/bin/su validuser -c "/usr/bin/vboxmanage startvm MACHINENAME2--type headless"
/bin/sleep 1
2 - Make file vm-start executable:
chmod +x /etc/init.d/vm-start
3 - Edit rc.local
nano /etc/rc.local
Add this:
#!/bin/bash
/etc/init.d/vm-start
exit 0
4 - Reboot.