QEMU Get Guest IP
-
QEMU has a guest agent like other hypervisors. If you have the guest agent you can get some info out of the guest directly from the host. Here's a way to get the IP address from the guest:
virsh qemu-agent-command $guest '{"execute":"guest-network-get-interfaces"}' | python -mjson.tool
This spits out some json:
{ "return": [ { "hardware-address": "00:00:00:00:00:00", "ip-addresses": [ { "ip-address": "127.0.0.1", "ip-address-type": "ipv4", "prefix": 8 }, { "ip-address": "::1", "ip-address-type": "ipv6", "prefix": 128 } ], "name": "lo" }, { "hardware-address": "52:54:00:1b:3a:ba", "ip-addresses": [ { "ip-address": "10.1.30.6", "ip-address-type": "ipv4", "prefix": 24 }, { "ip-address": "fe80::5054:ff:fe1b:3aba", "ip-address-type": "ipv6", "prefix": 64 } ], "name": "eth0" } ] }
-
After looking around, you don't have to pass commands to QEMU directly. You can do the same thing using virsh and the domifaddr command:
[jhooks@kvm ~]$ sudo virsh domifaddr gluster1 --source agent Name MAC address Protocol Address ------------------------------------------------------------------------------- lo 00:00:00:00:00:00 ipv4 127.0.0.1/8 - - ipv6 ::1/128 eth0 52:54:00:50:d0:77 ipv4 10.1.30.220/24 - - ipv6 fe80::5054:ff:fe50:d077/64
This is a little nicer since it doesn't spit out a ton of JSON to parse through. However, this means the last few hours I've spent trying to get Ansible to read just the one hardware address was for nothing.
-
But you learned something along the way.