Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Starting Vagrant VM on host boot

Tags:

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.

like image 378
Shrikant Patnaik Avatar asked Aug 07 '13 08:08

Shrikant Patnaik


People also ask

How do I resume a Vagrant VM?

Command: vagrant resume [name|id] This resumes a Vagrant managed machine that was previously suspended, perhaps with the suspend command. The configured provisioners will not run again, by default. You can force the provisioners to re-run by specifying the --provision flag.

What is the command to start a Vagrant box?

Command: vagrant init [name [url]] This initializes the current directory to be a Vagrant environment by creating an initial Vagrantfile if one does not already exist. If a first argument is given, it will prepopulate the config. vm. box setting in the created Vagrantfile.


1 Answers

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?

like image 155
Terry Wang Avatar answered Oct 10 '22 02:10

Terry Wang