[Kimchi-devel] [RFC PATCH] List IPs of VM Ifaces

Christy Perez christy at linux.vnet.ibm.com
Thu May 21 18:28:15 UTC 2015


Also know, I do plan on adding doc and tests and test fixes of course.
Just getting initial feedback here.

Thanks!

- Christy

On 05/21/2015 12:04 PM, Christy Perez wrote:
> Signed-off-by: Christy Perez <christy at linux.vnet.ibm.com>
> ---
>  src/kimchi/model/vmifaces.py | 29 +++++++++++++++++++++++++++++
>  1 file changed, 29 insertions(+)
> 
> diff --git a/src/kimchi/model/vmifaces.py b/src/kimchi/model/vmifaces.py
> index 8bf6b3d..0b1ffe3 100644
> --- a/src/kimchi/model/vmifaces.py
> +++ b/src/kimchi/model/vmifaces.py
> @@ -25,6 +25,7 @@ from lxml import etree, objectify
>  from kimchi.exception import InvalidParameter, MissingParameter, NotFoundError
>  from kimchi.model.config import CapabilitiesModel
>  from kimchi.model.vms import DOM_STATE_MAP, VMModel
> +from kimchi.utils import run_command
>  from kimchi.xmlutils.interface import get_iface_xml
> 
> 
> @@ -122,9 +123,37 @@ class VMIfaceModel(object):
>              info['network'] = iface.source.get('network')
>          if info['type'] == 'bridge':
>              info['bridge'] = iface.source.get('bridge')
> +        info['ips'] = self._get_ips(info['mac'], info['network'])
> 
>          return info
> 
> +    def _get_ips(self, mac, network):
> +        ips = []
> +        conn = self.conn.get()
> +        # An iface may have multiple IPs
> +        # An IP could have been assigned without libvirt.
> +        # First check the ARP cache.
> +        (stdout, stderr, returncode) = run_command(['arp', '-n'])
> +        if returncode == 0:
> +            ips = [line.split()[0] for line in stdout.splitlines() if mac in line]
> +        # Some ifaces may be inactive, so if the ARP cache didn't have them,
> +        # and they happen to be assigned via DHCP, we can check there too.
> +        try:
> +            net = conn.networkLookupByName(network)
> +            leases = net.DHCPLeases(mac)
> +            for lease in leases:
> +                ip = lease.get('ipaddr')
> +                if ip not in ips:
> +                    ips.append(ip)
> +        except libvirt.libvirtError:
> +            pass
> +
> +        # @TODO: One other option is introspection. It is not, however, 
> +        # guaranteed that any host OS would have the same files & commands
> +        # for storing and retreiving IPs. So, this may be as good as we can do.
> +
> +        return ips
> +
>      def delete(self, vm, mac):
>          dom = VMModel.get_vm(vm, self.conn)
>          iface = self._get_vmiface(vm, mac)
> 




More information about the Kimchi-devel mailing list