Vagrant: Vagrant Disks feature -- Disk name must be unique

Created on 10 Jul 2020  ยท  5Comments  ยท  Source: hashicorp/vagrant

Vagrant version

Vagrant 2.2.9

Host operating system

macOS

Guest operating system

Oracle Linux (Issue is not related to that box, any linux box will do)

Vagrantfile

Vagrant.require_version ">= 2.2.8"
ENV['VAGRANT_EXPERIMENTAL'] = 'disks'

Vagrant.configure("2") do |config|
  config.vm.box = "oraclelinux/7"
  config.vm.box_url = "https://oracle.github.io/vagrant-projects/boxes/oraclelinux/7.json"

  config.vm.provider "virtualbox" do |vb|
     vb.memory = "1024"
  end

  config.vm.disk :disk, size: '16GB', name: 'storage'

  (1..2).each do |i|
    config.vm.define "guest#{i}" do |guest|
      guest.vm.hostname = "guest#{i}"
    end
  end
end

Debug output

https://gist.github.com/AmedeeBulle/409d0bbea980c75b31e955700b2ab64b

Expected behavior

Bring up 2 VMS each having an additional disk

Actual behavior

The second VM tries to attach the disk from the first VM:

==> guest2: Booting VM...
There was an error while executing `VBoxManage`, a CLI used by Vagrant
for controlling VirtualBox. The command and stderr is shown below.

Command: ["startvm", "43b7196b-f72d-4973-82a1-19a834b9716c", "--type", "headless"]

Stderr: VBoxManage: error: Locking of attached media failed. A possible reason is that one of the media is attached to a running VM
VBoxManage: error: Details: code VBOX_E_INVALID_OBJECT_STATE (0x80bb0007), component SessionMachine, interface IMachine

Steps to reproduce

vagrant up

Analysis

The problem comes from

https://github.com/hashicorp/vagrant/blob/34747cafd681f3a3a8636e8d5d426e2f2241b474/plugins/providers/virtualbox/cap/configure_disks.rb#L52-L71

where we match any disk in the VirtualBox storage having the same basename (Line 67).

An obvious workaround for my particular issue is to use unique names with e.g.:

      guest.vm.provider "virtualbox" do |vb, override|
        override.vm.disk :disk, size: '16GB', name: "storage_#{i}"
      end

but this issue is broader than multi-machines; even with a single-machine Vagrantfile:

  • I won't be able to use the same Vagrantfile 2 times (or a different vagrant project using accidentally the same name)
  • There is a risk to use (and damage) a file from a random VM (not even managed by Vagrant)

I am not familiar with the code, but shouldn't we only match against the disks somehow related to that VM? (metadata?)

bug disks providevirtualbox

All 5 comments

Hey there @AmedeeBulle - I think what you found was just a bug with the disk feature. Disk names should always be unique for a single guest, not an entire VirtualBox environment. It looks like the virtualbox method for listing harddisks wasn't isolated to a single guest like I thought it had been, so that explains why it looks like Vagrant thinks the disk it finds is one that belongs to the second guest.

For now, you can work around it like you mentioned before, giving the disk a unique name. You shouldn't need to define the disk in a provider override block either. You can move it down into your guest definition and use your enumerator to add a number onto the name of the disk:

  (1..2).each do |i|
    config.vm.define "guest#{i}" do |guest|
      guest.vm.hostname = "guest#{i}"
      guest.vm.disk :disk, size: '16GB', name: "storage-#{i}"
    end
  end

For the fix on our end....

https://github.com/hashicorp/vagrant/blob/6ee180a0239dc00606c537d4e2f7240e682519cc/plugins/providers/virtualbox/cap/configure_disks.rb#L90-L107

We'll just need to update what information we're looking at to determine the existence of a disk for a given guest. The proper fix for this is to move up grabbing vm_info to always be called at the beginning of the method here. Then when the case that the disk we're configuring isn't the primary disk, we'll want to look for disk name with the proper storage controller there instead to check if it already exists or not rather than looking at what we currently have as all_disks. Additionally, we could keep all_disks around and earlier on grab all of the disks attached to the guest with the info from vm_info in its own method and return it at the beginning of get_current_disk so that it could be used similar to how all_disks is used now. This should be the only place in configure_disks that its used.

Thanks for trying out the experimental disk feature! At least there is a workaround for now until we get it fixed and pushed out in a new release.

Thanks for the quick reply!

(The reason I use a provider override in my workaround is coming from my real use case where I only use disk for the virtualbox provider, my other provider -- libvirt -- having a similar feature at provider level)

Hey there @AmedeeBulle - I've opened a pull request via https://github.com/hashicorp/vagrant/pull/11767 that should resolve the issue. Thanks!

I confirm it works for me: 3 machines using the same disk name:

$ ls -l VirtualBoxVMs/k8s_*/*vdi
-rw-------  1 pvanhaes  staff  2097152 Jul 15 14:29 VirtualBoxVMs/k8s_master_1594816180705_45765/k8s_disk.vdi
-rw-------  1 pvanhaes  staff  2097152 Jul 15 14:38 VirtualBoxVMs/k8s_worker1_1594816721931_16057/k8s_disk.vdi
-rw-------  1 pvanhaes  staff  2097152 Jul 15 14:42 VirtualBoxVMs/k8s_worker2_1594816917038_93698/k8s_disk.vdi

Thanks!

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings