23

I am new to vagrant and have set up a couple of vagrant vm's which I use for development and testing purposes. My issue is that I cant get these boxes to start up automatically on my machine (Ubuntu). It is really annoying to go to the folder and vagrant up each machine every time my host machine starts up.

I have tried adding a cronjob that looks like cd path/to/vm/folder && vagrant up but this does not seem to be working.

I also tried a cronjob for VBoxManage but vagrant changes the name of the VM (rather the number/version 'vmname_version') everytime the VM boots up.

Nithin
  • 3,679
  • 3
  • 30
  • 55
Shrikant Patnaik
  • 245
  • 1
  • 2
  • 6

4 Answers4

18

Cron job doesn't fit into this use case, it is for scheduled jobs.

As you are running Ubuntu as host, I'd recommend using /etc/rc.local, place the commands in the rc.local script which is executed at the end of the init process.

It'll 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.    
cd /path/to/vagrant_vm1 && vagrant up
cd /path/to/vagrant_vm2 && vagrant up
exit 0

NOTE: There will be port conflicts on the host if you start more than 1 Vagrant boxes with the same networking mode - NAT (default), which by default use the same port forwarding rule => guest 22 to host 2222.

If you need to start more than 1 boxes (NAT), consider using public network (bridged) or use VBoxManage controlvm to start the VMs, refer to the answer in Two separate Vagrant machines, windows host, PuTTY - how?

Community
  • 1
  • 1
Terry Wang
  • 13,840
  • 3
  • 50
  • 43
  • In theory that is what I wanted however Vagrant seems to be having some issues with this, It reaches "Waiting for VM to boot" but does not successfully boot it, it ends up with an error saying "Failed to connect to VM via SSH. Please verify the VM successfully booted by looking at the VirtualBox GUI.". Thanks Anyway! – Shrikant Patnaik Aug 08 '13 at 14:48
  • @ShrikantPatnaik See updated answer. I think you are hitting the port conflict issue by starting multiple vagrant boxes. – Terry Wang Aug 09 '13 at 01:43
  • Naa, Ports and everything are configured right because I have all of them running simultaneously with no issues, the controlvm worked, but I had to clone them as I replied in my own answer to the question. – Shrikant Patnaik Aug 12 '13 at 06:07
  • Well, if you use default NAT mode and start using `vagrant up` it'll try to do the port forwarding anyway (guest 22 => host 2222) for all VMs, there will be conflicts but I realized it may NOT be bad enough to prevent VMs from being started. – Terry Wang Aug 14 '13 at 02:35
  • But won't that also be an issue if I manually call vagrant up on my VM's one by one? – Shrikant Patnaik Aug 20 '13 at 07:33
  • 1
    This worked for me in ubuntu 16.04 after specifying full path to vagrant command and running vagrant from the user, that vagrant was initially used with. The command looks like this: cd /path/to/vagrant_vm1 && su -c "/usr/bin/vagrant up" myuser – Oleksii Zymovets Feb 08 '17 at 10:52
7

Here is a startup script found at http://uhowto.urbylog.info/post/54097294891/an-upstart-script-to-launch-vagrant-box-automatically

An upstart script to launch vagrant box automatically Sometimes it could be necessary to start vagrant virtual machines authomatically on host boot. Similarly, providing a graceful shutdown for a guest when the host is being turned off or restarted. Below is an example of an upstart script for launching/halting a vagrant box on behalf of randomjoe user. Change user name and Vagrantfile dir in the script below. Name your upstart configuration file descriptively. Create one /etc/init/*.conf file for each Vagrantfile you need up and running at system boot. /etc/init/vagrant-precise32.conf:

description "Start vagrant precise32 box on system startup"
author "uHOWTO"
env VAGRANTUSR=randomjoe
env VAGRANTBOXPATH=/home/randomjoe/vagrantboxes/precise32
start on stopped rc
stop on runlevel [016]

pre-start script
    cd ${VAGRANTBOXPATH}
    su -c "/usr/bin/vagrant up" ${VAGRANTUSR}
end script

post-stop script
    cd ${VAGRANTBOXPATH}
    su -c "/usr/bin/vagrant halt" ${VAGRANTUSR}
end script
Damian Dennis
  • 718
  • 7
  • 9
1

I'v ended up with a simple cron job. I had to put in a sleep, otherwise Virtualbox complained that it was not correctly initialized.

@reboot cd /home/myuser/VMs/VagrantBox && sleep 20; /bin/su -c "/usr/bin/vagrant up" myuser
Laoneo
  • 1,546
  • 1
  • 18
  • 25
0

I ended up cloning the VM's with VboxManage and just using VBoxManage startvm $vm --type headless in a init.d script

Shrikant Patnaik
  • 245
  • 1
  • 2
  • 6