Add Disk to VM Path Virtualbox Provider

1,369 views
Skip to first unread message

goo...@bonusbits.com

unread,
Sep 16, 2013, 12:15:43 PM9/16/13
to vagra...@googlegroups.com
This seems like a simple task, but I've been chasing my tail on this for long enough that I thought I'd reach out and see if someone can give me a hand.

ENVIRONMENT:
Vagrant 1.3.1 on Windows (I know.. I'm working on building a CentOS LXC for Ubuntu Vagrant)
Virtualbox 4.2.18
Vagrantfile version 2


GOAL:
Add another (2nd) virtual disk to a box image. (I'll mount it with Chef cookbook etc.)


Q: Is there a way to get the host path to a VM being provisioned in the Vagrantfile.  Meaning is there a variable with the full path or at least root path and then I can append the suggested hostname? 


I'd like to not have to use a system environment variable like I started to code for...


EXAMPLE: (So far)
  # Configuration for Virtualbox provider
  config.vm.provider "virtualbox" do |vb|
    # Memory
    vb.customize [ "modifyvm", :id, "--memory", "1024" ]
    # Chipset (Supposedly better CPU performance)
    vb.customize [ "modifyvm", :id, "--chipset", "ich9" ]
    # NIC 1 (Better TCP over NAT performance, at least on Windows)
    vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
    vb.customize ["modifyvm", :id, "--natsettings1", "9000,1024,1024,1024,1024"]
    # NIC 2 (Host Only Access)
    vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
    vb.customize ["modifyvm", :id, "--hostonlyadapter2", "VirtualBox Host-Only Ethernet Adapter"]

    # Add Second Drive
    if ENV["VBOX_VM_PATH"]
      disk2_path = ENV["VBOX_VM_PATH"] + :vm + "/" + "box-disk2" + ".vmdk"
    else
      disk2_path = Dir.pwd() + "/" + "box-disk2" + ".vmdk"
    end

    vb.customize ["storagectl", :id, "--name", "SATA", "--sataportcount", 2, "--hostiocache", "on"]
    vb.customize ["createhd", "--filename", disk2_path, "--size", 30*1024, "--format", "vmdk"]
    vb.customize ["storageattach", :id,  "--storagectl", "SATA", "--port", 1, "--device", 0,  "--type", "hdd", "--medium", disk2_path]
  end

Thanks!

goo...@bonusbits.com

unread,
Sep 16, 2013, 2:36:35 PM9/16/13
to vagra...@googlegroups.com
This is my current workaround using an environment variable that works... but really would rather not depend on the user/developer to have to set this prior. 

  # Configuration for Virtualbox provider
  config.vm.provider "virtualbox" do |vb|
    # Virtualbox Name
    vb.customize [ "modifyvm", :id, "--name", $suggested_hostname ]

    # Memory
    vb.customize [ "modifyvm", :id, "--memory", "1024" ]
    # Chipset (Supposedly better CPU performance)
    vb.customize [ "modifyvm", :id, "--chipset", "ich9" ]
    # NIC 1 (Better TCP over NAT performance, at least on Windows)
    vb.customize ["modifyvm", :id, "--nictype1", "virtio"]
    vb.customize ["modifyvm", :id, "--natsettings1", "9000,1024,1024,1024,1024"]
    # NIC 2 (Host Only Access)
    vb.customize ["modifyvm", :id, "--nictype2", "virtio"]
    vb.customize ["modifyvm", :id, "--hostonlyadapter2", "VirtualBox Host-Only Ethernet Adapter"]
    # Storage Controller
    #  NOTE: name "SATA" for some images,  "SATA Controller" for others
    #vb.customize ["storagectl", :id, "--name", "SATA", "--hostiocache", "on"]

    # Add Second Drive
    # This is a workaround until I find a better solution. You need to have an environment variable set to the
    #  Virtualbox VM path. i.e. type %VBOX_USER_HOME%\VirtualBox.xml | findstr defaultMachineFolder
    # i.e. set VBOX_VM_PATH=C:\NoAVScans\Vbox_VMs
    if ENV["VBOX_VM_PATH"]
      disk2_path = ENV["VBOX_VM_PATH"] + "/" + $suggested_hostname + "/" + "box-disk2" + ".vmdk"

    else
      disk2_path = Dir.pwd() + "/" + "box-disk2" + ".vmdk"
    end

    vb.customize ["storagectl", :id, "--name", "SATA", "--sataportcount", 2, "--hostiocache", "on"]
    vb.customize ["createhd", "--filename", disk2_path, "--size", 30*1024, "--format", "vmdk"]
    vb.customize ["storageattach", :id,  "--storagectl", "SATA", "--port", 1, "--device", 0,  "--type", "hdd", "--medium", disk2_path]
  end

Andy Bohne

unread,
Jan 13, 2014, 4:38:04 PM1/13/14
to vagra...@googlegroups.com
Did you ever find a better work around for this?  I'm trying to do the same thing (mainly to prevent collisions if I want to have 2 VMs up at the same time).

My initial thought was to do the following:

config.vm.provider :virtualbox do |vb|
  vb.customize ['createhd', '--filename', "#{:id}-drive2.vdi", '--size', 5 * 1024]
  vb.customize ['storageattach', :id, '--storagectl', 'SCSI Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', "#{:id}-drive2.vdi']
end

Unfortunately, :id is being interpolated as "id" when this happens.
Reply all
Reply to author
Forward
0 new messages