How can I provision multiple drives for one vm in a loop?

408 views
Skip to first unread message

Brian Carpenter

unread,
Oct 11, 2019, 10:16:35 AM10/11/19
to Vagrant
I am trying to provision identical drives for a ZFS sandbox vm to learn ZFS but it seems to only create the first drive and then fail

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
  (1..5).each do |hd|
      config.vm.provider :virtualbox do |v|
                puts "harddrive #{hd}"
                v.customize ['createmedium', '--filename', "./disk#{hd}.vdi",'--variant', 'Fixed', '--size', 20 * 1024]

                v.customize ['storageattach', :id,  '--storagectl', 'IDE', '--device', hd, '--type', 'hdd', '--medium', "./disk#{hd}.vdi"]
  end
  end
end


Output....
$vagrant up

harddrive 1
harddrive 2
harddrive 3
harddrive 4
harddrive 5
harddrive 1
harddrive 2
harddrive 3
harddrive 4
harddrive 5
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: Clearing any previously set network interfaces...
==> default: Preparing network interfaces based on configuration...
    default: Adapter 1: nat
==> default: Forwarding ports...
    default: 22 (guest) => 2222 (host) (adapter 1)
==> default: Running 'pre-boot' VM customizations...
A customization command failed:

["createmedium", "--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: ["createmedium", "--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.



Salty Vagrant

unread,
Oct 15, 2019, 12:11:30 PM10/15/19
to Vagrant

My guess would be that the default/system drive is created as disk1.vdi. Try starting at disk2.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/126acb4c-ad09-40bf-bc11-8e15c84a091a%40googlegroups.com.

Brian Carpenter

unread,
Oct 15, 2019, 12:30:44 PM10/15/19
to vagra...@googlegroups.com
There are no extra drives and the initial system drive is called: centos-7-1-1.x86_64.vmdk

Tried changing 
 (1..5).each do |hd|

to 
 (2..5).each do |hd|

And got the same thing

A customization command failed:

["createmedium", "--filename", "./disk2.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: ["createmedium", "--filename", "./disk2.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/disk2.vdi'.
VBoxManage: error: VDI: cannot create image '/home/brian/projects/centos/disk2.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
You received this message because you are subscribed to a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/yoheUgqb5TE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vagrant-up+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/vagrant-up/5DC3E3F9-3A6B-4EBB-884A-98A2FBB4683A%40gmail.com.


--



Western Oklahoma State College LogoBrian Carpenter 
Information Systems Specialist
Western Oklahoma State College
2801 N. Main St.  |  Altus, OK 73521
(580) 477-7919  |  Office: HLC 126
wosc facebook wosc instagram wosc twitter wosc snapchat

The mission of Western Oklahoma State College is to provide high quality education, support student success, and empower individuals to become productive members of local, regional, and global communities.


Karl Vietmeier

unread,
Oct 15, 2019, 2:47:13 PM10/15/19
to vagra...@googlegroups.com
Brian, 

The command looks correct.

Could be the loop not executing correctly, you should move it into the vm.config loop itself so it executes for each VM you are creating.

Also - it could be that the disks are getting cleaned up after the vagrant up fails so you need to clear them out between iterations.  When it is all working the drives will get removed when you "vagrant destroy".


I create multiple drives for each VM I spin up. Notice how deeply the disk creation loop is nested.  Also note the 
" unless File.exist?("#{servers["name"]}-OSD-0#{num}.vdi")" construct - it will check to see if the file exists first and not error out if it does.

This might be easier to follow - I worked out the code using a single VM:




--
---------------------------------------------------------------
Karl Vietmeier, Cloud Solutions Architect

Salty Vagrant

unread,
Oct 15, 2019, 4:52:29 PM10/15/19
to vagra...@googlegroups.com

I had to resort to a Google search. Look at https://coderwall.com/p/8m--dq/purge-deleted-hard-disks-from-virtual-box This shows how to purge deleted disks. vagrant destroy does not purge custom disks and simply deleting the vdi does not correctly manage virtual box metadata.

You still have an issue with storageattach; you need to specify device and port. I don’t have time to research this at the moment but I’m sure Google will provide.

Brian Carpenter

unread,
Oct 15, 2019, 4:55:18 PM10/15/19
to Vagrant
Thanks for the examples Karl, 

I have changed my Vagrantfile to the following:
cat Vagrantfile && vagrant destroy -f &&echo "files before up"&& ls -a1 &&vagrant up; echo "files after up"; ls -a1
# -*- mode: ruby -*-
# vi: set ft=ruby :

Vagrant.configure("2") do |config|
  config.vm.box = "centos/7"
    config.vm.provider "virtualbox" do |vb|
vb.customize ['storagectl', :id, '--name',  'SATA Controller', '--add', 'sata',  '--controller', 'IntelAhci', '--portcount', 8]

(1..5).each do |hd|
  unless File.exist?("./disk#{hd}.vdi")
    vb.customize ['createhd', '--filename', "./disk#{hd}.vdi", '--size', 1024]
  end
          vb.customize ['storageattach', :id,  '--storagectl', 'SATA Controller', '--port', "#{hd}" ,'--device', 0, '--type', 'hdd', '--medium', "./disk#{hd}.vdi"]
  end
  end
end
==> default: Forcing shutdown of VM...
==> default: Destroying VM and associated drives...
files before up
.
..
.vagrant
Vagrantfile
Bringing machine 'default' up with 'virtualbox' provider...
==> default: Importing base box 'centos/7'...
==> default: Matching MAC address for NAT networking...
==> default: Checking if box 'centos/7' version '1905.1' is up to date...
==> default: Setting the name of the VM: centos_default_1571172546672_17886
==> 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...
==> default: Booting VM...
==> default: Waiting for machine to boot. This may take a few minutes...
    default: SSH address: 127.0.0.1:2200
    default: SSH username: vagrant
    default: SSH auth method: private key
    default: 
    default: Vagrant insecure key detected. Vagrant will automatically replace
    default: this with a newly generated keypair for better security.
    default: 
    default: Inserting generated public key within guest...
    default: Removing insecure key from the guest if it's present...
    default: Key inserted! Disconnecting and reconnecting using new SSH key...
==> default: Machine booted and ready!
==> default: Checking for guest additions in VM...
    default: No guest additions were detected on the base box for this VM! Guest
    default: additions are required for forwarded ports, shared folders, host only
    default: networking, and more. If SSH fails on this machine, please install
    default: the guest additions and repackage the box to continue.
    default: 
    default: This is not an error message; everything may continue to work properly,
    default: in which case you may ignore this message.
==> default: Rsyncing folder: /home/brian/projects/centos/ => /vagrant
files after up
.
..
disk1.vdi
disk2.vdi
disk3.vdi
disk4.vdi
disk5.vdi
.vagrant
Vagrantfile



Brian Carpenter

unread,
Oct 15, 2019, 5:00:47 PM10/15/19
to Vagrant
Thanks I noticed that as well. 
Switched to creating a SATA controller and attaching drives to it.

The metadata problem may have been part of my headache. After trying Karl's examples and them working out of the box, I deleted the box in virtualbox and deleted my .vagrant directory as well.
Then things started to work more correctly... 

Discovered a typo in my "Unless file.exists" line  ( must include the path, duh) 

See my response to Karl for the correct Vagrantfile. 
To unsubscribe from this group and stop receiving emails from it, send an email to vagra...@googlegroups.com.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to a topic in the Google Groups "Vagrant" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vagrant-up/yoheUgqb5TE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vagra...@googlegroups.com.


--



Western Oklahoma State College LogoBrian Carpenter 
Information Systems Specialist
Western Oklahoma State College
2801 N. Main St.  |  Altus, OK 73521
(580) 477-7919  |  Office: HLC 126
wosc facebook wosc instagram wosc twitter wosc snapchat

The mission of Western Oklahoma State College is to provide high quality education, support student success, and empower individuals to become productive members of local, regional, and global communities.


--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagra...@googlegroups.com.

Karl Vietmeier

unread,
Oct 20, 2019, 6:02:23 PM10/20/19
to vagra...@googlegroups.com
I'm glad they worked for you! It took me a few iterations to sort out the right syntax. Sometimes you need to carefully check the dates and versions of the examples you are looking at as it is easy to start down a path with outdated information or incomplete examples (Ansible suffers from this as well). Sally's comment about Vagrant not removing drives is a good example.  That was true of older Vagrant/Vbox combinations but if you use the right Ruby syntax Vagrant will correctly clean up after itself after a "vagrant destroy". My examples consistently work correctly - unless interrupted mid-flight - then some manual cleanup might be necessary.

Regards,



--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/mitchellh/vagrant/issues
IRC: #vagrant on Freenode
---
You received this message because you are subscribed to the Google Groups "Vagrant" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vagrant-up+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages