hashicorp / vagrant

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

Vagrant deletes the contents of `.vagrant\machines\default\virtualbox` #12815

Open me-kell opened 2 years ago

me-kell commented 2 years ago

Vagrant version

Vagrant 2.2.19

Host operating system

Guest operating system

any

Vagrantfile

any

Actual behavior

The contents of the .vagrant\machines\default\virtualbox are deleted if the virtual machine is not registered.

Steps to reproduce

See also below under Detailed Steps to reproduce

  1. unregistervm (either via VBoxManage or via GUI) an existing VM.
  2. call vagrant snapshot list or simply vagrant status
  3. the contents of the .vagrant\machines\default\virtualbox are deleted.

References

Maybe related to (https://github.com/hashicorp/vagrant/issues/10977#issue-470214199)

Detailed Steps to reproduce

vagrant init debian/bullseye64
vagrant up && vagrant halt
tree /f
------------------------------------------------------------
S:.
│   Vagrantfile
│
└───.vagrant
    ├───bundler
    │       global.sol
    │
    ├───machines
    │   └───default
    │       └───virtualbox
    │               action_provision
    │               action_set_name
    │               box_meta
    │               creator_uid
    │               id
    │               index_uuid
    │               private_key
    │               synced_folders
    │               vagrant_cwd
    │
    └───rgloader
            loader.rb
------------------------------------------------------------
vboxmanage list vms
    "vagrant_issue_12815_default_1658483998732_67923" {00df5350-9a0c-42fb-bef5-f69a0bb1329e}

vboxmanage unregistervm 00df5350-9a0c-42fb-bef5-f69a0bb1329e
------------------------------------------------------------
vagrant status
tree /f
------------------------------------------------------------
S:.
│   Vagrantfile
│
└───.vagrant
    ├───bundler
    │       global.sol
    │
    ├───machines
    │   └───default
    │       └───virtualbox    <- the contents of this directory are deleted!
    └───rgloader
            loader.rb
soapy1 commented 1 year ago

Hey there, I think this is the right behavior for Vagrant. That is, if the VM managed by Vagrant no longer exists, then Vagrant should remove it from it's index and clean up the associated files. So, I'm going to go ahead and close this issue.

me-kell commented 1 year ago

The VM still exists. It has simply been unregistered from vbox via unregistervm. Not deleted!

If we register the VM again vagrant ignores that the VM still exists. Thus vagrant up will create a new VM. Resulting in two VMs.

Is this really the intended behavior?

me-kell commented 1 year ago

Is this behavior documented?

AFAICS the documentation of vagrant status and vagrant snapshot list simply states that:

vagrant status This will tell you the state of the machines Vagrant is managing.

vagrant snapshot list This command will list all the snapshots taken.

But instead (in the background and without any warning) it deletes the contents of the directory.vagrant\machines\default\virtualbox.

It is very misleading that asking the status or listing snapshots will delete data in the background without asking.

Please either document it or (better) add a message in those function that informs the user about deleting this information.

I'd suggest to reopen this issue.

soapy1 commented 1 year ago

Thanks for the clarification! I'm going to reopen this issue.

Looking into more the crux of the issue seems to be that Vagrant is unable to verify the machine exists. That is, Vagrant machine data dir gets cleared out since the vm gets reloaded. Part of the process checks if the vm exists. This check errors since the machine is unregistered. For example, when I ran this directly I got the error

-> % VBoxManage showvminfo 0b6f8582-2726-4795-b791-5f6ab603c81f
VBoxManage: error: Could not find a registered machine with UUID {0b6f8582-2726-4795-b791-5f6ab603c81f}
VBoxManage: error: Details: code VBOX_E_OBJECT_NOT_FOUND (0x80bb0001), component VirtualBoxWrap, interface IVirtualBox, callee nsISupports
VBoxManage: error: Context: "FindMachine(Bstr(VMNameOrUuid).raw(), machine.asOutParam())" at line 3139 of file VBoxManageInfo.cpp
trushap2198 commented 1 year ago

Were you able to figure out how to get away with the error? Will it affect the new vm installations?