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

kvm-virtualizationlibvirtvirtualization

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.

Best Answer

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.