ansible-community / molecule-plugins

Collection on molecule plugins
MIT License
109 stars 73 forks source link

Add a disk #60

Open markfaine opened 3 years ago

markfaine commented 3 years ago

I reviewed the docs but didn't see anyway to configure an additional disk. I need to test an LVM role and I need a second disk device. Is this possible?

apatard commented 3 years ago

It has to be done through provider specific options. For instance, with vagrant libvirt (I don't have virtualbox example at hand):

...
driver:
  name: vagrant
  provider:
    name: libvirt
...
platforms:
- name: vm
  box: debian/buster64
  provider_raw_config_args:
  - "storage :file, :type => 'qcow2', :device => 'vdb', :size => '1G'"
...
markfaine commented 3 years ago

That's helpful. But there is still some confusion, sorry. What are the provider_raw_config_args used to represent? libvirt options (when used as a provider), virtualbox command line args, or vagrant configuration?

Any basic example for virtualbox would be helpful.

Currently I'm using libvirt as the provider since I thought it seemed the preferred method, would it make more sense for me to use virtualbox directly as the provider? Is there any benefit in using libvirt over virtualbox for virtualbox VMs.

apatard commented 3 years ago

The options set in the platform definition are used to write the Vagrantfile defining the VMs to create. The molecule-vagrant plugin tries hard to not have configuration options specific to each vagrant provider (as the provider can be virtualbox, libvirt, vmware, ...). This means that some options are not directly available from molecule and in this case, the instance_raw_config_args and provider_raw_config_args have to be used.

I've not tested it but I guess something like this may work for virtualbox provider. The idea is to use the customize option of the virtualbox provider (See https://www.vagrantup.com/docs/providers/virtualbox/configuration#vboxmanage-customizations):

...
driver:
  name: vagrant
  provider:
    name: virtualbox
...
- name: vm
  box: ...
  provider_raw_config_args:
  - "customize ['createhd', '--filename', 'disk.vdi', '--variant', 'Fixed', '--size', 20 * 1024]"
  - "customize ['storageattach', :id,  '--storagectl', 'SATAController', '--port', 1, '--device', 0, '--type', 'hdd', '--medium', 'disk.vdi']"
...

Currently I'm using libvirt as the provider since I thought it seemed the preferred method, would it make more sense for me to use virtualbox directly as the provider? Is there any benefit in using libvirt over virtualbox for virtualbox VMs.

molecule-vagrant doesn't force to use a provider. The only thing to note is that only virtualbox and libvirt providers are tested by the continuous integration. So, if you want to use virtualbox directly without going through libvirt, you can.

markfaine commented 3 years ago

This almost works, slight modification for anyone who needs it:

 - "customize ['createhd', 'disk', '--filename', 'disk.vdi', '--variant', 'Fixed', '--size',  10240]"

However, apparently there is an unrelated issue with WSL2 and virtualbox that prevents the port forwarding to the port 2222 on the vm to function properly. I have not been able to get past this so I'm going to punt and try to use Hyper-V instead, if you have a basic hyperv example that would be appreciated however, it seems that vagrant can't select the network switch for hyperv and so prompts every time, I'm not sure how to work around that. So Hyper-V is also a bust for now.

Also, if you're using WSL you won't be able to change the path, to the disk since it doesn't seem to get translated to the /mnt/c/... path format, and even if you do, it gets prepended with the $wsl path prefix that breaks the path.

I'm going to spin up a VM and maybe try a VM in a VM, though I hate to go that way, my options are limited.

karcaw commented 3 years ago

here is how I connect two VirtualBox disks:

    provider_raw_config_args:
      - "customize ['createhd',  '--filename', 'machine1_disk0', '--size', '8196']"
      - "customize ['createhd',  '--filename', 'machine1_disk1', '--size', '8196']"
      - "customize ['storageattach', :id, '--storagectl', 'SATA Controller','--port', '1', '--type', 'hdd', '--medium', 'machine1_disk0.vdi']"
      - "customize ['storageattach', :id, '--storagectl', 'SATA Controller','--port', '2', '--type', 'hdd', '--medium', 'machine1_disk1.vdi']"
apatard commented 2 years ago

@markfaine did you manage to get the two disks working ? Can we close the bug ?

mconigliaro commented 2 years ago

This is what worked for me:

provider_raw_config_args:
  - "customize ['createmedium', 'disk', '--filename', 'example', '--size', '2048']"
  - "customize ['storageattach', :id, '--storagectl', 'SCSI', '--port', '2', '--type', 'hdd', '--medium', 'example.vdi']"
apatard commented 2 years ago

I've tested the configuration from https://github.com/ansible-community/molecule-plugins/issues/60 on a test system with virtualbox and managed to add disks to the VM. This results in examples in my draft PR : https://github.com/ansible-community/molecule-vagrant/pull/172.

To be a little bit more complete: using createmedium or createhd probably depends on the virtualbox version used.

As concerns this bug, I'll close it when PR ansible-community/molecule-vagrant#172 is merged in one or an other form.

tw0flower commented 2 months ago

On top of the issue @apatard mentioned, the biggest caveat of passing commands to VirtualBox directly is that you would have to write some extra code to clean up. And running molecule prepare again will fail because the file already exists, and createmedium and createhd commands do not have flags to overwrite the file or skip creation gracefully.

I found it best to use Vagrant to configure extra disks. One line to add a 10GB disk that will be managed by Vagrant:

instance_raw_config_args:
  - 'vm.disk :disk, name: "mySmallDisk", size: "10GB"'

Vagrant documentation: https://developer.hashicorp.com/vagrant/docs/disks/configuration