ad0fd61b14e4a0c0ffac450fa591678e14e22adc
[icn.git] / deploy / metal3-vm / vm-setup / roles / libvirt / files / get-domain-ip.sh
1 #!/usr/bin/env bash
2 set -eu -o pipefail
3
4 # This script will attempt to get the ip address of the a given libvirt guest.
5
6 PATH=$PATH:/usr/sbin:/sbin
7
8 VMNAME=$1
9
10 # Get the MAC address of the first interface by looking for looking for the
11 # `<mac address...` line.  Yes, we're parsing XML with awk.  It's probably
12 # safe (because the XML is coming from libvirt, so we can be reasonably
13 # confident that the formatting will remain the same).
14 mac=$(virsh dumpxml $VMNAME | awk -F "'" '/mac address/ { print $2; exit }')
15
16 # Look up the MAC address in the ARP table.
17 ip=$(ip neigh | grep $mac | awk '{print $1;}')
18
19 if [ -z "$ip" ]; then
20     echo "vm ip is not available" >&2
21     exit 1
22 fi
23
24 echo $ip