8

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.

1
  • This guy says its not possible, but I am still looking around...
    – rahuL
    Jun 21, 2013 at 6:22

6 Answers 6

4

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.

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.

2

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

1

Might be a bit late, but you can use the qemu-guest-agent for that. This will allow you to create files on the guest systems from the hypervisor:

root@hypervisor-node-01:~# echo "`date`: testing file write from hypervisor[`hostname`]" | base64
  VGh1IE1heSAyNCAwOToxNDoxMCBDRVNUIDIwMTg6IHRlc3RpbmcgZmlsZSB3cml0ZSBmcm9tIGh5
  cGVydmlzb3JbaHlwZXJ2aXNvci1ub2RlLTAxXQo=

root@hypervisor-node-01:~# socat /var/run/qemu-server/101.qga -
  {"execute":"guest-file-open", "arguments":{"path":"/tmp/hypervisor","mode":"w+"}}
  {"return": 1032}
  {"execute":"guest-file-write", "arguments":{"handle":1032,"buf-b64":"VGh1IE1heSAyNCAwOToxNDoxMCBDRVNUIDIwMTg6IHRlc3RpbmcgZmlsZSB3cml0ZSBmcm9tIGh5cGVydmlzb3JbaHlwZXJ2aXNvci1ub2RlLTAxXQo="}}
  {"return": {"count": 86, "eof": false}}
  {"execute":"guest-file-close", "arguments":{"handle":1032}}
  {"return": {}}

Result on the guest:

root@kvm-guest-01:~$ cat /tmp/hypervisor
  Thu May 24 09:14:10 CEST 2018: testing file write from hypervisor[hypervisor-node-01]

Created a script to set puppet facts about the hypervisor on my guests:

0

Since Linux 4.4.x, for amd64 and aarch64, you can pass some key-value pairs to /, which then end up in the SMBIOS/DMI table readable from the guest.

Access from guest-side is as easy as reading the files in /sys/class/dmi/id/* in guest Linux. Similarly comfortable ways to access these strings should exist for all modern Operating Systems.

You could use e.g. chassis_asset_tag to convey information about the HV host.

Add e.g. -smbios type=3,asset=PatrickUltra1 to your hypervisor command line.

Here is a page with the complete DMI/SMBIOS mappings:

On Linux:

ls /sys/class/dmi/id/
bios_date        board_vendor       ec_firmware_release  product_uuid
bios_release     board_version      modalias             product_version
bios_vendor      chassis_asset_tag  power                subsystem
bios_version     chassis_serial     product_family       sys_vendor
board_asset_tag  chassis_type       product_name         uevent
board_name       chassis_vendor     product_serial
board_serial     chassis_version    product_sku
0

As others have said, guest VMs cannot access such info about their hosts unless the host has made it available to the guests. There are ways and ways of doing this, but my favorite is setting custom guestinfo. Because guestinfo is created anew at VM startup, it's always up to date.

Option 2 in this 2011 article by William Lam explains how to do it. There may be changes needed if you're using newer VMware software, but anyway, here's the link: https://williamlam.com/2011/01/how-to-extract-host-information-from.html

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .