Reputation: 101
I am trying to create a vm with vagrant with multiple disks, but I want the vagrantfile to use a loop to create them. Vagrant does not handle loops correctly as it seems to double or triple traverse the loop. Then Vagrant fails as it already created disk1.vdi
Warning: I am no expert on Ruby...
I have tried using arrays and ruby's .each method. Tried a while loop. All fail with the same problem.
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
What I expect is a vm with 5+1 drives
what I get is
$ 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.
Upvotes: 0
Views: 2065
Reputation: 369
I think the important parts are the --portcount at storagectl and the unless File.exist? to avoid trying to recreate the disks
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
Upvotes: 1