How to detect hypervisor host name/IP from inside a KVM virtual machine?

We have a small fleet of KVM/libvirt hosts with a few hundred virtual machines on it, managed by a group of sysadmins in disparate locations. I've run into a problem where I am connected to a virtual machine by SSH, but I can't figure out the name of the bare metal host which owns it. Does anyone know a way to find the name of the bare metal host from inside a virtual machine?

If this isn't something KVM does out of the box, are there any examples of configurations we could apply on all our hypervisors to expose this information?

We do use inventory management software to keep track of our virtual machines & their bare metal hosts - however it has bugs, and from time to time we lose track of what host owns a virtual machine. I'll fix this problem - but the concept of finding a KVM host from inside a VM would be useful regardless.

I know this goes against many security principles employed in shared environments where each virtual machine is a product sold to a different customer - but in our environment where we own the hosts, the security is not an issue.


Solution 1:

You're thinking about this the wrong way. Instead of trying to derive the KVM hostname from the guest (impossible by design), try to figure out how the KVM host might inform the guest of its name.

Data can be easily shared with a guest from a KVM host by using the Filesystem Passthrough feature. There's even a GUI option for it in Virtual Machine Manager (See Add Hardware -> Filesystem).

Just create a directory somewhere on the KVM host with whatever stuff in it you want to share with the guest. This can be a text file containing the hostname, some setup or maintenance script, or whatever you want really. Mark it read-only for simplicity and security. Then configure Filesystem Passthrough using this path and mount it somewhere convenient in each guest.

Obviously this approach requires slight modifications to each KVM host and guest, but with a few hundred hosts out there, I assume you have some sort of configuration management in place (like Puppet, Ansible, etc.), thus greatly simplifying the matter.

Sorry this answer is super late, but given that the question has 5,000+ views I figured I should post it anyway.

Solution 2:

I'm pretty damn sure this isn't possible, for security reasons.. Not to do what you want to directly, by querying the guest.

However, you could install a scriptable agent on the host, then use something like virsh list to dump a list of VM names, and have a policy (actually a meatspace policy, not a technical one) to ensure that the hostname of the guest exactly matches the VM name.

OR.. Do the same as above, except use something like virsh to dump the VM name, and the IP address of the guest, then recursively work your way through the list, sshing into them in order, running some kind of data collection script, and shoving it into your inventory.

That way, you know the host it was run on (because you ran virsh from it and got a list), and you know what's running there.

Doesn't have to be virsh, you can do most/all of this with libvirtd directly, or the Python bindings thereof.

Solution 3:

Not possible without working around the issue, basically some sort of agent in the VM that would keep the host data in it. If those guests migrate, then it's got to be updated via network somehow.

If you use a KVM management system (with hundreds of VMs, you definitely should) like oVirt, you can see on which host a VM runs in the admin control panel

enter image description here