Shutdown a vagrant box that is no longer configured - vagrant

I initially created a Vagrantfile that provisioned a default box. Later, I updated the Vagrantfile to use a named box ("db") with a slightly different config. Unfortunately, I forgot to halt the initial box before I provisioned the new box. Now there's a rogue vagrant box running in the background that I can't halt, even after rebooting the host machine.
$ vagrant global-status
id name provider state directory
---------------------------------------------------------------------------------------------------------------
6b855b0 default virtualbox running /Users/chrisbloom7/Projects/rails-upgrade
d348c3b db virtualbox poweroff /Users/chrisbloom7/Projects/rails-upgrade
$ vagrant halt -f 6b855b0
The machine with the name 'default' was not found configured for
this Vagrant environment.
$ cat Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.define "db", primary: true do |db|
db.vm.provider :virtualbox do |vb|
vb.name = "rails-upgrade-development-mysql"
end
# ...
end
end
$ git show HEAD^:Vagrantfile
Vagrant.configure(2) do |config|
config.vm.box = "box-cutter/ubuntu1004"
config.vm.provider :virtualbox do |vb|
vb.name = "rails-upgrade-development"
end
# ...
end
I've checked in VirtualBox, but I don't see any boxes that are running. ps aux | grep vagrant and ps aux | grep virtualbox don't show anything either.

Turns out the vagrant global-status output was showing a red herring: It reads the status of all machines from ~/.vagrant.d/data/machines-index/index. There really was no machine running as evident by the fact that no process were showing up using the ps command (H/T to #CEPA), and no machines appeared to be running in the VirtualBox application. That also explains why it's status was still "running" even after a reboot. Running vagrant global-status --prune cleaned up the file and thus the global status report.

When I have a Vagrant box I can't communicate with, I kill the process. The process name is usually VBoxHeadless.exe and will have the name in the full path where you can grep for it. ps aux | grep -i vbox | grep [put name here]

Related

Create Vagrant VM with an alias

I want to create a couple of Vagrant VM's. Most of them could be Ubuntu 16.04. But I want the VM's to be named as webserver01, webserver02 and webserver03. Basically I want the Vagrantfile for each VM to be inside the directory name I provided above.
Command vagrant init ubuntu/xenial64 might create a new VM but how do I make sure I create three webservers{1..3} as above and run vagrant up command from inside that directory?
I want all the VM's to be in a specific directory so I just open CMD inside that directory and run vagrant up from inside them.
you can spawn as many VMs with the help of loops.
https://www.vagrantup.com/docs/vagrantfile/tips.
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu/trusty64"
(1..3).each do |i|
config.vm.define "node-#{i}" do |node|
node.vm.provision "shell",
inline: "echo hello from node #{i}"
end
end
end
you can access any machine from the cli e.g. from the given example in the link you can control node-1 like
vagrant up node-1 && vagrant ssh node-1

Vagrant Waiting For Domain To Get An IP Address

First, apologies: I'm a newbie.
I've created a very basic Vagrantfile by running Vagrant init. I only made a few changes:
config.vm.box = "generic/fedora28"
config.vm.box_version = "1.8.32"
config.vm.provider "libvirt" do |lv|
lv.memory = "4096"
end
(There are also a few items in my config.vm.provision section).
After running vagrant up , the process gets stuck at
==> default: Waiting for domain to get an IP address...
I'm running this off a Fedora 27 box, which uses version 2.0.2 of the Vagrant package (even though current is 2.1.5).
I've tried adding this line:
config.vm.network "private_network", ip: "192.168.100.101"
but it had no effect.
Can anyone help?
I have a Vagrant File that spins up 4 VMs, of image generic/ubuntu2004 on libvirt kvm, to make a k3s cluster that's accessible on the LAN. (multipass + k3s is only accessible via localhost b/c it doesn't allow easy bridging.)
I ran both of these commands > 50 times
sudo vagrant destroy --force --parallel
sudo vagrant up
On the ~51'st time, I noticed vagrant up got stuck on "Waiting for domain to get an IP address..."
What fixed it for me was sudo reboot. You know the classic have you tried unplugging it and plugging it back in?
Something else to try (I rebooted before trying it)
https://bugzilla.redhat.com/show_bug.cgi?id=1283989

Is "vagrant up" possible from different local directories with custom URLs?

I have two directories under /var/www/html, named vlp1 and vlp2 with Vagrant setup. If I run vagrant up separately in these folders the default local URL is vagrant.local for both. But vagrant.local/ always loads site from vlp1 folder.
As it should be, vagrant global-status shows that I have two virtual machines running under these directories. I require to run both sites side by side and to do this the only option I see is to have separate URLs. I believe there is a way to assign different URLs to different sites but don't have any idea how!
What I need to do to accomplish the above so that my sites run like local.vlp1.com and local.vlp2.com or may be like vagrant.vlp1 and vagrant.vlp2?
According to project instructions I have to keep two sites completely separated in two folders and have to use separate Vagrantfiles.
OS: Ubuntu 14.04 64 bit
Installed VitualBox version: 5.1
Installed Vagrant version: 1:1.9.4
Thank you!
UPDATE
Went through the following steps as advised by Henri:
Step 1:
$ vagrant global-status
Output:
id name provider state directory
------------------------------------------------------------------------
eb9b569 default virtualbox running /var/www/html/vlp1
190608c default virtualbox running /var/www/html/vlp2
Step 2: Did a graceful shutdown on vlp2
$ vagrant halt 190608c
Step 3: Output of vagrant global-status now is
id name provider state directory
-------------------------------------------------------------------------
eb9b569 default virtualbox running /var/www/html/vlp1
190608c default virtualbox poweroff /var/www/html/vlp2
Step 4:
$ sudo gedit /var/www/html/vlp2/Vagrantfile
if CONF['ip'] == "dhcp"
config.vm.network :private_network, type: "dhcp", hostsupdater: "skip"
else
config.vm.network :private_network, ip: "192.168.2.5"
end
Step 5: Re up'd vlp2 machine
$ vagrant up 190608c
Step 6:
$ vagrant global-status now shows both machine are back to running state again.
id name provider state directory
------------------------------------------------------------------------
eb9b569 default virtualbox running /var/www/html/vlp1
190608c default virtualbox running /var/www/html/vlp2
Step 7:
In /etc/hosts added following entry
192.168.2.5 vagrant.vlp2
Finally I tried vagrant.vlp2/ in browser but ended up with the following message:
This site can’t be reached
http://vagrant.vlp2/ is unreachable.
As I understand you're using multiple VM so the easiest in this case is to assign each of the VM a static IP (that must be different) so you do so in Vagrantfile
Vagrant.configure("2") do |config|
...
config.vm.network "private_network", ip: "192.168.10.x"
...
end
and you use another IP for the second VM
Vagrant.configure("2") do |config|
...
config.vm.network "private_network", ip: "192.168.20.y"
...
end
and then you update your /etc/hosts file with the information
192.168.10.x vagrant.vlp1
192.168.20.y vagrant.vlp2

Vagrant global-status all VM's named default

When I run vagrant global-status, I get a listing back of all the vagrant managed VM's I'm running, but something I have not expected is output with the machine details. Even though I give every machine an explicit name during initialization, the value for name in the global-status output for every machine is always default.
Why does vagrant status or vagrant global-status not list the actual names of the boxes? At times, when I've destroyed a box, it has actually destroyed more than one box.
id name provider state directory
-------------------------------------------------------------------------
e44520d default virtualbox poweroff /Users/name/code/foo
1310726 default virtualbox poweroff /Users/name/code/bar
430fd52 default virtualbox poweroff /Users/name/code/baz
The machines are named foo, bar, baz... why are they showing as default?
If you have default Vagrantfile as configuration, your machine VM will be named default, so something like
Vagrant.configure(2) do |config|
config.vm.box = "ubuntu-12.04"
config.vm.hostname = "foo"
config.vm.network "forwarded_port", guest: 80, host: 8080
. . .
Now, if you have a multi machine configuration, you will have something like this in your Vagrantfile (simplified)
Vagrant.configure("2") do |config|
config.vm.box = "ubuntu-12.04"
config.vm.define "db" do |db|
db.vm.hostname = db.test
db.vm.provider "vmware_fusion" do |vm|
vm.vmx["memsize"] = "2048"
end
db.vm.provision "shell", path: "bootstrap-dev.sh"
end
config.vm.define "app", primary: true do |app|
app.vm.hostname = app.test
app.vm.network "private_network", "192.168.50.11"
app.vm.synced_folder "project/site", "/var/www"
end
end
In this case, you have defined 2 machines db and app and the vagrant global-status will prompt
id name provider state directory
-----------------------------------------------------------------------------------------------------------
de264ac db vmware_fusion not running /Volumes/Transcend/Project1
bdd4385 app vmware_fusion not running /Volumes/Transcend/Project1

Forward Ports from boot2docker using the Vagrant Docker provider

I'm trying to utilize Vagrant 1.6's Docker provider and I seem to have run into a snag. I can successfully bring up the Docker container and guest OS, but then I can't access the service I've brought up within the container from the host OS. Here's my Vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.network :forwarded_port, guest: 8000, host: 8000
config.vm.define "icecast" do |v|
v.vm.provider "docker" do |d|
d.image = "moul/icecast"
d.ports = ["8000:8000"]
d.env = {
# SOURCE_PASSWORD: 'password',
ADMIN_PASSWORD: 'password',
# PASSWORD: 'password',
# RELAY_PASSWORD: 'password'
}
end
end
end
My understanding is that running vagrant up --provider=docker on OS X will start a VM running boot2docker that will then run my container. Running vagrant docker-logs seems to confirm that my container is created and the service started, but now I can't for the life of me figure out how to access the service from my OS X host. If I was using a standard VirtualBox provider, I would expect the config.vm.network :forwarded_port directive to handle the forwarding, but adding that doesn't seem to make any difference.
What do I need to do to be able to access this service from my OS X host?
Update: For reference, here is the image's Dockerfile: https://github.com/moul/docker-icecast/blob/master/Dockerfile
Ok, so I finally figured this out and it turns out the solution is to not use boot2docker at all. Based on some diving I did through the Vagrant source, reading issues, and rewatching the Docker provider introduction videos, it turns out that you need to use a proxy VM to host your containers instead of boot2docker.
To set this up, I modified my Vagrantfile to include a configuration option for vagrant_vagrantfile:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.define "icecast" do |v|
v.vm.provider "docker" do |d|
d.image = "moul/icecast"
d.ports = ["8000:8000"]
d.env = {
# SOURCE_PASSWORD: 'password',
ADMIN_PASSWORD: 'password',
# PASSWORD: 'password',
# RELAY_PASSWORD: 'password'
}
d.vagrant_vagrantfile = "./Vagrantfile.proxy"
end
end
end
Then I added an additional file (Vagrantfile.proxy) that Vagrant will use to spin up the proxy VM:
VAGRANTFILE_API_VERSION = "2"
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.vm.box = "ubuntu/trusty64"
config.vm.provision "docker"
config.vm.provision "shell", inline:
"ps aux | grep 'sshd:' | awk '{print $2}' | xargs kill"
config.vm.network :forwarded_port, guest: 8000, host: 8000
end
Using the Docker provisioner will automatically install Docker on the proxy VM for you. The inline shell script forces Vagrant to log back into the box so that it can utilize Docker after it's been installed. Finally, I forward the port I need in this Vagrantfile as opposed to the original (while still using the ports config option in the original).
Just like with the default boot2docker strategy, Vagrant will be smart enough to reuse existing instances of the proxy VM for any image that utilizes it.
Hopefully this will be helpful to someone down the road.
To forward Ports from boot2docker (as opposed to forwarding ports from a custom proxy VM that's not using boot2docker), you need to add port forwards manually through VirtualBox, or run the following script after running vagrant up:
export PORT=3306
export REASON=mysql
export HOST_VM=`VBoxManage list runningvms | grep docker-host | awk '{ print $1 }' | sed 's/"//g'`
VBoxManage controlvm $HOST_VM natpf1 "docker-$REASON-$PORT-port-forward,tcp,127.0.0.1,$PORT,,$PORT"

Resources