hashicorp / vagrant

Vagrant is a tool for building and distributing development environments.
https://www.vagrantup.com
Other
26.02k stars 4.42k forks source link

Snapshotting leads to subsequent errors with additional disks #13352

Open rsommer opened 4 months ago

rsommer commented 4 months ago

Using a very simple Vagrantfile for testing purposes I can reproduce the following unexpected behaviour.

Vagrantfile in use:

Vagrant.configure(2) do |config|
  config.vm.box = "alpine/alpine64"
  config.vm.disk :disk, size: "10MB", name: "data"
end

vagrant up succeeds, containing the output:

default: Disk 'data' not found in guest. Creating and attaching disk to guest...

as expected. Calling vagrant reload does succeed at this point.

Now create a snapshot:

$ vagrant snapshot save default base
==> default: Snapshotting the machine as 'base'...
==> default: Snapshot saved! You can restore the snapshot at any time by
==> default: using `vagrant snapshot restore`. You can delete it using
==> default: `vagrant snapshot delete`.

And now, while trying to reload:

$ vagrant reload
==> default: Attempting graceful shutdown of VM...
==> default: Checking if box 'alpine/alpine64' version '3.7.0' 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: Configuring storage mediums...
    default: Disk 'data' not found in guest. Creating and attaching disk to guest...
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", "/home/nvimtest/VirtualBox VMs/test_default_1708697784014_9523/data.vdi", "--sizebyte", "10485760", "--format", "VDI"]

Stderr: 0%...VBOX_E_FILE_ERROR
VBoxManage: error: Failed to create medium
VBoxManage: error: Could not create the medium storage unit '/home/nvimtest/VirtualBox VMs/test_default_1708697784014_9523/data.vdi'.
VBoxManage: error: VDI: cannot create image '/home/nvimtest/VirtualBox VMs/test_default_1708697784014_9523/data.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 630 of file VBoxManageDisk.cpp

The root cause seems to be at https://github.com/hashicorp/vagrant/blob/d8fdc500b76c840cdeaa69869d0c000530b036b3/plugins/providers/virtualbox/cap/configure_disks.rb#L132

The comparison File.dirname(d["Location"]) == File.dirname(primary[:location]) does not yield true, because after creating the snapshot, primary[:location] looks like /home/nvimtest/VirtualBox VMs/test_default_1708696655312_30742/Snapshots while the extra disks d["Location"] is /home/nvimtest/VirtualBox VMs/test_default_1708696655312_30742. As the disks name is derived from the image file name and the snapshot lacks the actual name, the snapshot of the additional disk is not taken into account.

$ VBoxManage list hdds
[...]
UUID:           ccbf1111-39ce-4003-bf2e-2f2ec675b81b
Parent UUID:    base
State:          created
Type:           normal (base)
Location:       /home/nvimtest/VirtualBox VMs/test_default_1708697784014_9523/data.vdi
Storage format: VDI
Capacity:       10 MBytes
Encryption:     disabled

UUID:           d4c5b37b-69b4-4b79-aa3c-c219fca581cd
Parent UUID:    ccbf1111-39ce-4003-bf2e-2f2ec675b81b
State:          created
Type:           normal (differencing)
Location:       /home/nvimtest/VirtualBox VMs/test_default_1708697784014_9523/Snapshots/{d4c5b37b-69b4-4b79-aa3c-c219fca581cd}.vdi
Storage format: VDI
Capacity:       10 MBytes
Encryption:     disabled

As this output is parsed to get the disk's name, {d4c5b37b-69b4-4b79-aa3c-c219fca581cd} does not match the expected name data.

Expected behavior

The box should reload and use the existing additional disk.

Actual behavior

The reload tries to recreate the already existing additional disk and failes.

Reproduction information

See explanation at the top.

Vagrant version

2.4.1

Virtualbox version

7.0.12

Host operating system

Ubuntu 22.04

Guest operating system

Tried with debian, ubuntu and alpine