I want to create a Vagrant box based on the output of another Vagrant box. The first one builds a .bin file. Ideally I would like to create a new Vagrant box based on this file. Something like

$ mkdir node2 ; cd node2
$ vagrant init node2 ../build/qMp_3.2.1-Clearance_VirtualBox_x86_factory_20170406-2203.bin --minimal
$ vagrant up

However it gives an error

bsdtar.EXE: Error opening archive: Unrecognized archive format

I can convert from .bin to .vdi, but it's still not enough to create a Vagrant box.

vboxmanage convertfromraw --format vdi qMp_3.2.1-Clearance_VirtualBox_x86_factory_20180325-0702.bin qmp-nycmesh-3.2.1.vdi

It still gives the 'error opening archive' message. How do I create a Vagrant box from a VirtualBox disk image?


I don't know how to create a new vagrant environment from an existing .vdi per say, but you can associate a vagrant environement with an existing Vbox machine and then generate a box from the vagrant VM:

Go into the vagrant project directory, then create a file name id in the right place with ther VM UUID:

$ cd <vagrant-project-folder> && \
$ echo -n \
$ `VBoxManage list vms \
$  | grep <vm-short-name> \
$  | sed 's/.*{\(.*\)}[^{]*$/\1/'` \
$ > .vagrant/machines/<vm-short-name>/virtualbox/id

If the project only have one VM then should be equal to default.

It directly inject whatever between {} in the result line of the grep to the file without newline.

The VM must be up in Virtualbox for the VBoxManage list vms to work

Check if the change was taken into account:

$ vagrant status \
> | grep -qE '<vm-short-name>.*not created'

If vagrant status return a line with our and "not created" it means it didn't work, otherwise it's OK and you can then run vagrant up <vm-short-name> to get into the VM and test it, tweak it, clean it etc...

Then you can package it into a box:

$ vagrant package --output <box-name>.box

Add it to the box local repository:

$ vagrant box add <box-name> <box-name>.box

Cleanup with vagrant destroy and rm the work directory.

Then init a new project as with any box:

$ vagrant init -m <box-name>
| improve this answer | |

I realized Vagrantfile is just Ruby so I just used Ruby to generate the VDI and Vagrant to attach the VDI to the box. The only problem is it requires a dummy placeholder box, and after you replace the storage medium, it will never delete the placeholder disk image! I don't know how to fix that.

Vagrant.configure("2") do |config|
  latest_bin = `ls -t ../build/*.bin | head -1`.strip
  #latest_bin = Dir.glob('../build/*.bin').sort{ |a,b| File.new(a).stat <=> File.new(b).stat }.last
  vdi_file = 'nycmesh-qmp-openwrt.vdi'
  system "vboxmanage convertfromraw --format vdi #{latest_bin} #{vdi_file}" unless File.exist?(vdi_file)
  config.vm.box = "centos/7" # dummy box
  config.vm.provider "virtualbox" do |vb|
    # add the newly created build disk firmware
    vb.customize ['storageattach', :id, '--storagectl', 'IDE', '--port', 0, '--device', 0, '--type', 'hdd', '--medium', "nycmesh-qmp-openwrt.vdi"]
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.