Ubuntu – Vagrant fails to load because grub2 waits for kernel selection after ungraceful shutdown

grub2vagrantvirtualbox

I have a Vagrant box with Ubuntu 12.04 LTS. Sometimes after forcing a shutdown (e.g. because the machine became unresponsive) the vagrant box fails to boot the next time.

$ vagrant up
Bringing machine 'default' up with 'virtualbox' provider...
[default] Clearing any previously set forwarded ports...
[default] Creating shared folders metadata...
[default] Clearing any previously set network interfaces...
[default] Preparing network interfaces based on configuration...
[default] Forwarding ports...
[default] -- 22 => 2222 (adapter 1)
[default] Running 'pre-boot' VM customizations...
[default] Booting VM...
[default] Waiting for machine to boot. This may take a few minutes...

The preview screenshot in Virtualbox suggests that the box hangs at the grub stage, forever waiting for a user input to select the kernel.

Screenshot of Vagrantbox stuck at grub

So every time this occurs, I set vb.gui = true in the Vagrantfile, restart the machine and select the kernel to boot manually. The interesting thing is that after selecting the kernel once, subsequent headless starts again just work fine – until the next ungraceful machine shutdown.

My questions is: Is there a more elegant way that does not require manual intervention to prevent that the vagrant box hangs at the grub kernel selection screen after a forced shutdown?

Best Answer

First, get the machine id

# Before v1.1
# MACHINE_ID=$(awk -F\" '{print $6}' .vagrant)
# After v1.1
MACHINE_ID=$(cat .vagrant/machines/default/virtualbox/id)

Power off the VM

VBoxManage controlvm ${MACHINE_ID} poweroff

Then boot the machine with a GUI console

VBoxManage startvm ${MACHINE_ID}

Wait for it to boot, login, run:

sudo update-grub

When successful, shut it down

VBoxManage controlvm ${MACHINE_ID} poweroff

And after this, vagrant up will just boot your VM normally

Source

Related Question