VirtualBox Workaround: Saving Vagrant Images That Don’t Get a NAT Address
Join the DZone community and get the full member experience.Join For Free
as part of testing and demonstrating our advanced deployment automation 1 platform deployit , we at xebialabs use a lot of cloud and devops tooling to be able to handle all the different types of middleware we support and build, ci and ops tooling with which we integrate 2 .
i was recently setting up a
environment to demonstrate deployit’s puppet module, which
automatically registers new puppet-provisioned middleware with your
deployment automation platform to enable application-tier deployments to
it, and ended up wrestling for quite some time with a tricky
the issue in question has been around for over two years now, and relates to virtualbox’s dhcp server sometimes , under as-yet-undetermined circumstances, failing to allocate an ip address to the nat interface.
since all vagrant-managed images get a nat interface by default 4 , this is more than a little inconvenient: vagrant simply hangs during the vm configuration phase.
since the problem doesn’t occur deterministically, one way to work around this issue is simply to avoid having to reboot the image: play the “nat lottery” until you get lucky by killing the vboxmanage process if the image is hanging and trying vagrant up again, then run vagrant suspend rather than vagrant halt and you can resume the images when you need them.
that will work, but i wasn’t particularly happy with this approach because, aside from me not liking the idea of repeatedly killing hypervisor processes (i’m somewhat of a pacifist in this regard ), it effectively “cripples” vagrant: the ease with which you can start, stop and re-configure images is precisely one of the things that makes vagrant so useful!
one of the things i quickly discovered is that, if you start a vagrant-created image via the virtualbox ui and it experiences the problem 5 , cycling the nat adapter with ifdown eth0 && ifup eth0 fixes things: this second time, it is able to pick up an ip address from the dhcp server.
unfortunately, this does’t get you far with an image that vagrant
itself is trying to start: vagrant creates headless sessions, so you
can’t actually access them through the virtualbox ui until you’ve killed
vagrant and the vboxheadless process it starts.
edited april 24th to add: see richard pot’s comment for instructions on how to start boxes in gui mode.
luckily, virtualbox allows you to execute commands on the guest os without having to use the ui, via vboxmanage’s guestcontrol command. so when vagrant was again hanging while waiting to connect to the image, the first thing i tried was
/path/to/vboxmanage guestcontrol my-vagrant-image exec "/usr/bin/sudo" --username vagrant --password vagrant --verbose --wait-stdout ifdown eth0 /path/to/vboxmanage guestcontrol my-vagrant-image exec "/usr/bin/sudo" --username vagrant --password vagrant --verbose --wait-stdout ifup eth0
that did , as hoped, allow the nat adapter to pick up an ip address. unfortunately, it also confused vagrant, which (presumably thinking that the image had gone offline) quit.
happily, you don’t have to bring down the adapter to request a new ip address: dhclient will do just as well. and indeed
/path/to/vboxmanage guestcontrol my-vagrant-image exec "/usr/bin/sudo" --username vagrant --password vagrant --verbose --wait-stdout dhclient
works: the nat adapter picks up an ip address and, after a few seconds, vagrant continues with the image configuration.
something to hopefully help out even if it indeed takes another couple of years to get to bottom of the actual issue
Published at DZone with permission of Andrew Phillips, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.