![通过 Vagrantfile 中的循环创建具有多个磁盘的虚拟机](https://linux22.com/image/1605953/%E9%80%9A%E8%BF%87%20Vagrantfile%20%E4%B8%AD%E7%9A%84%E5%BE%AA%E7%8E%AF%E5%88%9B%E5%BB%BA%E5%85%B7%E6%9C%89%E5%A4%9A%E4%B8%AA%E7%A3%81%E7%9B%98%E7%9A%84%E8%99%9A%E6%8B%9F%E6%9C%BA.png)
我正在尝试使用 vagrant 创建具有多个磁盘的虚拟机,但我希望 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.
答案1
更改以下内容:
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', '--port', hd, '--device', 0, '--type', 'hdd', '--medium', "./disk#{hd}.vdi"]
end
end
end
请注意这一行:
v.customize ['storageattach', :id, '--storagectl', 'IDE', '--port', hd, '--device', 0, '--type', 'hdd', '--medium', "./disk#{hd}.vdi"]
--device:已设置为 0 --port:已设置为 hd 索引(1,然后是 2,然后是 3,然后是 4,然后是 5)
远离--port 0,因为它通常是启动盘。