Mazzy Mazzy - 4 months ago 11
Ruby Question

Create two disks for multiple environment machines in Vagrant

I'm going to create a multiple environments machines with Vagrant. Here is the VagrantFile I'm trying to configure. I would like to instantiate seven machines connected them across a private networks. Every of this one should have two disks. I have found in Vagrant documentation the VBoxManage that expose the

createhd
command. I'm not sure where should I place this command. Inside every machine block or inside virtual provider block config?

Vagrant.configure(2) do |config|

config.vm.provision "shell", inline: "echo OpenStack"

config.vm.box = "ubuntu/trusty64"

config.vm.provider "virtualbox" do |vb|
vb.customize ["modifyvm", :id, "--memory", "2048", "--cpus", "1"]
end

config.vm.define "machine1" do |machine1|

machine1.vm.hostname = "machine1"

machine1.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine1_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine1_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine1_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine1_disk1.vdi"]
end

machine1.vm.network "private_network", ip: "192.168.10.10"
machine1.vm.network "private_network", ip: "192.168.10.15"
end

config.vm.define "machine2" do |machine2|

machine2.vm.hostname = "machine2"

machine2.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine2_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine2_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine2_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine2_disk1.vdi"]
end

machine2.vm.network "private_network", ip: "192.168.10.20"
machine2.vm.network "private_network", ip: "192.168.10.25"
end

config.vm.define "machine3" do |machine3|

machine3.vm.hostname = "machine3"

machine3.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine3_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine3_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine3_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine3_disk1.vdi"]
end

machine3.vm.network "private_network", ip: "192.168.10.30"
end

config.vm.define "machine4" do |machine4|

machine4.vm.hostname = "machine4"

machine4.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine4_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine4_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine4_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine4_disk1.vdi"]
end

machine4.vm.network "private_network", ip: "192.168.10.40"
end

config.vm.define "machine5" do |machine5|

machine5.vm.hostname = "machine5"

machine5.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine5_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine5_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine5_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine5_disk1.vdi"]
end

machine5.vm.network "private_network", ip: "192.168.10.50"
end

config.vm.define "machine6" do |machine6|

machine6.vm.hostname = "machine6"

machine6.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine6_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine6_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine6_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine6_disk1.vdi"]
end

machine6.vm.network "private_network", ip: "192.168.10.60"
end

config.vm.define "machine7" do |machine7|

machine7.vm.hostname = "machine7"

machine7.vm.provider "virtualbox" do |vb|
vb.customize ["createhd", "--filename", "machine7_disk0", "--size", "4096"]
vb.customize ["createhd", "--filename", "machine7_disk1", "--size", "4096"]
vb.customize ["storagectl", :id, "--name", "SATA Controller", "--add", "sata"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "machine7_disk0.vdi"]
vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "2", "--type", "hdd", "--medium", "machine7_disk1.vdi"]
end

machine7.vm.network "private_network", ip: "192.168.10.70"
end


end

Answer

First of all customizations, like createhd, must be added to provider. If you add it to config provider

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

It will be defined globally, and these parameters will be used by all machines. (Not sure) In result only one disk will be created, and will be shared among defined machines.

I think you should define provider in each machine. E.g

config.vm.define "machine4" do |machine4|
    machine4.vm.network "private_network", ip: "192.168.10.40"
    machine4.vm.provider :virtualbox do |vb|
        vb.customize ["createhd",  "--filename", "m4_disk0", "--size", "2048"]
        vb.customize ["storageattach", :id, "--storagectl", "SATA Controller", "--port", "1", "--type", "hdd", "--medium", "m4_disk0.vdi"]

    end
end

Please treat this Vagrantfile as a reference.