Create two disks for multiple environment machines

2019-06-09 05:04发布

问题:

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

回答1:

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.



回答2:

Well, you really want it to create and attach the extra storage just once. I believe the accepted answer above will cause an error on subsequent runs because the .VDI file is already created (and already attached).

The way to do this is to test if the VDI file is on the host's disk with File.exist?, meaning it's already been created.

For multi-machines, this might be what you want:

(1..3).each do |i|
    config.vm.define "node-#{i}" do |node|
        node.vm.network "private_network", ip: "192.168.200.#{i}"
        file_for_disk = "./large_disk#{i}.vdi"
        node.vm.provider "virtualbox" do |v|
           unless File.exist?(file_for_disk)
               v.customize ['createhd', 
                            '--filename', file_for_disk, 
                            '--size', 80 * 1024]
               v.customize ['storageattach', :id, 
                            '--storagectl', 'SATAController', 
                            '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_for_disk]
           end
       end
   end
end


回答3:

'This works for me.' 'I thought I share this as it might help Vagrant Newbie like me.'

Vagrant.configure("2") do |config|
file_to_disk = '../second_disk.vdi'
# create CrushFTP nodes
   (1..2).each do |i|
     config.vm.define "cftpnode#{i}" do |node|
         node.vm.customize ['createhd', '--filename', file_to_disk, '--size', 500 * 1024]
         node.vm.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', file_to_disk] 
         node.vm.box = "bento/centos-7.2"
         node.vm.hostname = "cftpnode#{i}"
         node.vm.network :private_network, ip: "192.168.0.1#{i}"
         node.vm.provider "virtualbox" do |vb|
             vb.memory = "1024"
                     end
       node.vm.provision :shell, path: "bootstrap-node.sh"
   end
  end
end