【问题标题】:Using a loop in VagrantFile to create X number of disks在 VagrantFile 中使用循环创建 X 个磁盘
【发布时间】:2020-02-04 20:20:04
【问题描述】:

我正在尝试使用具有多个磁盘的 vagrant 创建一个 vm,但我希望 vagrantfile 使用循环来创建它们。 Vagrant 无法正确处理循环,因为它似乎会两次或三次遍历循环。然后 Vagrant 失败了,因为它已经创建了 disk1.vdi

警告:我不是 Ruby 方面的专家...

我尝试过使用数组和 ruby​​ 的 .each 方法。尝试了一个while循环。都因同样的问题而失败。

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  config.vm.provider "virtualbox" do |v|
          Drives = [1,2,3,4,5]
          Drives.each do |hd|
                puts "harddrive #{hd}"
                v.customize ['createhd', '--filename', "./disk#{hd}.vdi",'--variant', 'Fixed', '--size', 20 * 1024]
                v.customize ['storageattach', :id,  '--storagectl', 'IDE', '--device', hd+1, '--type', 'hdd', '--medium', "./disk#{hd}.vdi"]  
          end
  end

end

我期望的是具有 5+1 个驱动器的虚拟机

我得到的是

$ vagrant up
harddrive 1
harddrive 2
harddrive 3
harddrive 4
harddrive 5
/home/brian/projects/centos/Vagrantfile:7: warning: already initialized constant Drives
/home/brian/projects/centos/Vagrantfile:7: warning: previous definition of Drives was here
harddrive 1
harddrive 2
harddrive 3
harddrive 4
harddrive 5
/home/brian/projects/centos/Vagrantfile:7: warning: already initialized constant Drives
/home/brian/projects/centos/Vagrantfile:7: warning: previous definition of Drives was here
harddrive 1
harddrive 2
harddrive 3
harddrive 4
harddrive 5
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Checking if box 'centos/7' version '1905.1' is up to date...
==> default: Clearing any previously set forwarded ports...
==> default: Fixed port collision for 22 => 2222. Now on port 2200.
==> default: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2200 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
A customization command failed:

["createhd", "--filename", "./disk1.vdi", "--variant", "Fixed", "--size", 20480]

The following error was experienced:

#<Vagrant::Errors::VBoxManageError: There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["createhd", "--filename", "./disk1.vdi", "--variant", "Fixed", "--size", "20480"]

Stderr: 0%...
Progress state: VBOX_E_FILE_ERROR
VBoxManage: error: Failed to create medium
VBoxManage: error: Could not create the medium storage unit '/home/brian/projects/centos/disk1.vdi'.
VBoxManage: error: VDI: cannot create image '/home/brian/projects/centos/disk1.vdi' (VERR_ALREADY_EXISTS)
VBoxManage: error: Details: code VBOX_E_FILE_ERROR (0x80bb0004), component MediumWrap, interface IMedium
VBoxManage: error: Context: "RTEXITCODE handleCreateMedium(HandlerArg*)" at line 462 of file VBoxManageDisk.cpp
>

Please fix this customization and try again.

【问题讨论】:

  • 由于这是创建额外硬盘而不是代码的问题,因此超级用户可能会更好。
  • 这有帮助吗?:cobbaut.blogspot.com/2014/04/…
  • 这些示例创建了 X 台具有恒定驱动器数量的机器。

标签: loops vagrant disk vagrantfile


【解决方案1】:

我认为重要的部分是 storagectl 的 --portcount 和除非 File.exist?避免尝试重新创建磁盘

servers = [
  { :hostname => "node01", :ip => "192.168.1.10", :memory => "2048", :disks => 2 },
  { :hostname => "node02", :ip => "192.168.1.20", :memory => "2048", :disks => 2 },
  { :hostname => "node03", :ip => "192.168.1.30", :memory => "2048", :disks => 2 },
]


Vagrant.configure("2") do |config|
    config.vm.box = "centos/7"
    servers.each do |conf|
        config.vm.define conf[:hostname] do |node|
            node.vm.hostname = conf[:hostname]
            node.vm.network "private_network", ip: conf[:ip]
            node.vm.provider "virtualbox" do |vb|
                vb.customize ['storagectl', :id, '--name', 'SATA Controller', '--portcount', conf[:disks]+1]
                (1..conf[:disks]).each do |x|
                    file_to_disk = './disk_'+conf[:hostname]+'_'+x.to_s()+'.vdi'
                    unless File.exist?(file_to_disk)
                        vb.customize ['createhd', '--filename', file_to_disk, '--size', 20 * 1024]
                    end
                    vb.customize ['storageattach', :id, '--storagectl', 'SATA Controller', '--port', x, '--device', 0, '--type', 'hdd', '--medium', file_to_disk]
                end
                vb.memory = conf[:memory]
            end
        end
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-15
    • 2020-08-03
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多