2

OS is Debian 10, vagrant provider is libvirt.

vagrant global-status is supposed to provide information on all vms on a system, but I can see shutoff and running vms in virt-manager that are not listed by vagrant global-status. I have only used libvirt for Vagrant so I know Vagrant is the culprit.

How do I determine the associated directory/Vagrantfile for each of these vms?

I think this command would do the trick:

find / -name Vagrantfile -exec echo {} \; -exec bash -c 'cd "$(dirname {})" && vagrant status | grep -E shutoff\|running' \; 2>/dev/null

But it seems absurd that something like this would be necessary, and would take a very long time on a large filesystem.

How exactly can I determine the directory for the associated Vagrantfile for each vm? There must be an easier way besides exhaustively searching the filesystem.

1
  • I answered, but then realized I didn't read the question properly!
    – shearn89
    Dec 24, 2021 at 9:05

1 Answer 1

2
+50

Based on the template [1], Vagrant does not seem to keep track of the project directory associated with the VM in libvirt domain metadata.

Internally Vagrant keeps the info about the VM (including the directory) in a JSON file ~/vagrant.d/data/machine-index/index, check the example [2]

I guess it's possible to modify the template to add the directory as a metadata, similar to how Openstack Nova add some metadata to the Guest's libvirt domain, the metadata can be used to get the VM name, Flavor, etc. directly from the compute node without querying Nova.

Obviously this does not apply to the existing VMs prior the change.

[1] https://github.com/vagrant-libvirt/vagrant-libvirt/blob/master/lib/vagrant-libvirt/templates/domain.xml.erb

[2]

cat .\index | python -m json.tool
{
    "version": 1,
    "machines": {
        "4f9bf5cd740f409c867553c19c52a81c": {
            "local_data_path": "C:/Vagrant-Machines/bionic-vm/.vagrant",
            "name": "default",
            "provider": "hyperv",
            "state": "running",
            "vagrantfile_name": null,
            "vagrantfile_path": "C:/Vagrant-Machines/bionic-vm",
            "updated_at": null,
            "extra_data": {
                "box": {
                    "name": "hashicorp/bionic64",
                    "provider": "hyperv",
                    "version": "1.0.282"
                }
            }
        }
    }
}

For reference, here how the metedata looks in the case of Nova:

 virsh dumpxml instance-0033dd00 | head
<domain type='kvm' id='93'>
  <name>instance-0033dd00</name>
  <uuid>XXXXXXXXXXXXXXXXXXX</uuid>
  <metadata>
    <nova:instance xmlns:nova="http://openstack.org/xmlns/libvirt/nova/1.0">
      <nova:package version="17.0.13"/>
      <nova:name>VM-NAME</nova:name>
      <nova:creationTime>2021-05-11 10:50:49</nova:creationTime>
      <nova:flavor name="m1.small">
        <nova:memory>16384</nova:memory>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.