7

I have Vagrant + VirtualBox.

In my Vagrantfile I have

config.vm.provider "virtualbox" do |v|
    v.customize [ "createhd", "--filename", "disk", "--size", 100000 ]
    v.customize [ 'storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', "disk"]
end

When I fire up with vagrant up it looks for "disk" in C:\HashiCorp\Vagrant\bin\disk

VBoxManage.exe: error: Could not find file for the medium 'C:\HashiCorp\Vagrant\bin\disk' (VERR_FILE_NOT_FOUND)

I would like the disk to live alongside the virtual machine's first disk in C:\Users\jma47\VirtualBox VMs\bin_build_1389371691

How can I do this in Vagrantfile?

Jepper
  • 1,092
  • 3
  • 11
  • 24

3 Answers3

11

This can be done if you define a name for virtual machine:

# Try to make it work on both Windows and Linux
if (/cygwin|mswin|mingw|bccwin|wince|emx/ =~ RUBY_PLATFORM) != nil
  vboxmanage_path = "C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage.exe"
else
  vboxmanage_path = "VBoxManage" # Assume it's in the path on Linux
end

Vagrant.configure(2) do |config|
  config.vm.box = "debian/wheezy64"

  config.vm.provider "virtualbox" do |vb|
    vb.name = "VM Name"

    # Get disk path
    line = `"#{vboxmanage_path}" list systemproperties`.split(/\n/).grep(/Default machine folder/).first
    vb_machine_folder = line.split(':', 2)[1].strip()
    second_disk = File.join(vb_machine_folder, vb.name, 'disk2.vdi')

    # Create and attach disk
    unless File.exist?(second_disk)
      vb.customize ['createhd', '--filename', second_disk, '--format', 'VDI', '--size', 60 * 1024]
    end
    vb.customize ['storageattach', :id, '--storagectl', 'IDE Controller', '--port', 0, '--device', 1, '--type', 'hdd', '--medium', second_disk]
  end
end
EM0
  • 5,369
  • 7
  • 51
  • 85
xuhcc
  • 2,400
  • 24
  • 23
  • Any chance to make getting default machine folder portable, i.e. working also on Windows? – Kombajn zbożowy Jan 24 '16 at 11:32
  • 1
    To use this in Windows, run your vagrant up command from cygwin. – Jepper Nov 27 '16 at 22:58
  • 1
    To make it portable change this line: `line = \`vboxmanage list systemproperties\`.split(/\n/).grep(/Default machine folder/).first` – Chloe Mar 29 '18 at 03:01
  • I found this didn't quite work, because 1) I didn't have VBoxManage in the path and 2) it split the path at the : in "C:\...". To fix this: `vboxmanage_path = "C:\\Program Files\\Oracle\\VirtualBox\\VBoxManage.exe"` then `line = `"#{vboxmanage_path}" list systemproperties`.split(/\n/).grep(/Default machine folder/).first` then `vb_machine_folder = line.split(':', 2)[1].strip()` – EM0 Mar 03 '20 at 14:55
8

You need to use something like this in your Vagrantfile:

For Vagrant API v1:

# Where to store the disk file
disk = 'C:\Users\jma47\VirtualBox VMs\bin_build_1389371691\extra_disk.vdi'

Vagrant::Config.run do |config|
  config.vm.box = 'base'

  config.vm.provider "virtualbox" do | v |
    unless File.exist?(disk)
      config.vm.customize ['createhd', '--filename', disk, '--size', 500 * 1024]
    end
    config.vm.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', disk]
  end
end

For Vagrant API v2:

# Where to store the disk file
disk = 'C:\Users\jma47\VirtualBox VMs\bin_build_1389371691\extra_disk.vdi'

VAGRANTFILE_API_VERSION = "2"

Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
  config.vm.box = 'base'

  config.vm.provider "virtualbox" do | p |
    unless File.exist?(disk)
      p.customize ['createhd', '--filename', disk, '--size', 1 * 1024]
    end
    p.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', disk]
  end
end
Duncan Lock
  • 12,351
  • 5
  • 40
  • 47
  • 2
    For anyone else who wanders by here, 'SATA Controller' will change depending on what the box / OS expects. 'SATA' is another common option. – nelsonda Jan 30 '15 at 16:29
  • `==> default: Box 'base' could not be found. Attempting to find and install...` and `Couldn't open file /C:/Users/Chloe/workspace/project/base` – Chloe Apr 07 '17 at 01:01
  • @Chloe replace `base` with whatever vagrant base box you want to start from, eg. `debian/wheezy64` – Duncan Lock Apr 07 '17 at 01:35
  • It's an embedded system that I have generated myself. There is no Vagrant base. http://dev.qmp.cat/projects/qmp/wiki/Virtualbox – Chloe Apr 07 '17 at 01:38
  • I think a box is required for Vagrant - but they're easy to make yourself from an existing VM: https://www.vagrantup.com/docs/virtualbox/boxes.html – Duncan Lock Apr 28 '17 at 00:43
  • `IDE` instead of `SATA Controller` worked for me on a `Centos/7` box. In general, you can also use the VirtualBox GUI to find out the controller's name in the `Settings->Storage` menu (box needs to be turned off prior to access). – Dirk Jun 05 '19 at 10:08
0

The "disk" parameter should be a path, Virtualbox needs it to store the second disk.

Use an absolute one like "c:\temp.disk" or "/tmp/disk.img"

Giovanni Toraldo
  • 206
  • 1
  • 13
  • 1
    Don't prefix the path with redundant './' either - I've seen an issue with 'tmp/file.vdi' compared to './tmp/file.vdi' which are essentially the same file but cause issues with createhd occasionally duriong vagrant up on a clean install. – Steve Grove Apr 26 '16 at 09:54