|
| 1 | +#!/usr/bin/python |
| 2 | +# |
| 3 | +# Script to retrieve extended information about VIFs that are |
| 4 | +# needed by the controller. This is called by the "vif" script, |
| 5 | +# which is run when virtual interfaces are added and removed. |
| 6 | + |
| 7 | +# Copyright (C) 2009 Nicira Networks, Inc. |
| 8 | +# |
| 9 | +# Copying and distribution of this file, with or without modification, |
| 10 | +# are permitted in any medium without royalty provided the copyright |
| 11 | +# notice and this notice are preserved. This file is offered as-is, |
| 12 | +# without warranty of any kind. |
| 13 | + |
| 14 | +import sys |
| 15 | +import XenAPI |
| 16 | +import xen.lowlevel.xs |
| 17 | + |
| 18 | +# Query XenStore for the opaque reference of this vif |
| 19 | +def get_vif_ref(domid, devid): |
| 20 | + xenstore = xen.lowlevel.xs.xs() |
| 21 | + t = xenstore.transaction_start() |
| 22 | + vif_ref = xenstore.read(t, '/xapi/%s/private/vif/%s/ref' % (domid, devid)) |
| 23 | + xenstore.transaction_end(t) |
| 24 | + return vif_ref |
| 25 | + |
| 26 | +# Query XAPI for the information we need using the vif's opaque reference |
| 27 | +def dump_vif_info(domid, devid, vif_ref): |
| 28 | + try: |
| 29 | + session = XenAPI.xapi_local() |
| 30 | + session.xenapi.login_with_password("root", "") |
| 31 | + vif_rec = session.xenapi.VIF.get_record(vif_ref) |
| 32 | + net_rec = session.xenapi.network.get_record(vif_rec["network"]) |
| 33 | + vm_rec = session.xenapi.VM.get_record(vif_rec["VM"]) |
| 34 | + |
| 35 | + sys.stdout.write('--add=port.vif%s.%s.network-uuid=%s ' |
| 36 | + % (domid, devid, net_rec["uuid"])) |
| 37 | + sys.stdout.write('--add=port.vif%s.%s.vif-mac=%s ' |
| 38 | + % (domid, devid, vif_rec["MAC"])) |
| 39 | + sys.stdout.write('--add=port.vif%s.%s.vif-uuid=%s ' |
| 40 | + % (domid, devid, vif_rec["uuid"])) |
| 41 | + sys.stdout.write('--add=port.vif%s.%s.vm-uuid=%s ' |
| 42 | + % (domid, devid, vm_rec["uuid"])) |
| 43 | + finally: |
| 44 | + session.xenapi.session.logout() |
| 45 | + |
| 46 | +if __name__ == '__main__': |
| 47 | + if (len(sys.argv) != 3): |
| 48 | + sys.stderr.write("ERROR: %s <domid> <devid>\n") |
| 49 | + sys.exit(1) |
| 50 | + |
| 51 | + domid = sys.argv[1] |
| 52 | + devid = sys.argv[2] |
| 53 | + |
| 54 | + vif_ref = get_vif_ref(domid, devid) |
| 55 | + if not vif_ref: |
| 56 | + sys.stderr.write("ERROR: Could not find interface vif%s.%s\n" |
| 57 | + % (domid, devid)) |
| 58 | + sys.exit(1) |
| 59 | + |
| 60 | + dump_vif_info(domid, devid, vif_ref) |
| 61 | + sys.exit(0) |
0 commit comments