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.
7 # Copyright (C) 2009 Nicira Networks, Inc.
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.
16 import xen.lowlevel.xs
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)
26 # Query XAPI for the information we need using the vif's opaque reference
27 def dump_vif_info(domid, devid, vif_ref):
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"])
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"]))
44 session.xenapi.session.logout()
46 if __name__ == '__main__':
47 if (len(sys.argv) != 3):
48 sys.stderr.write("ERROR: %s <domid> <devid>\n")
54 vif_ref = get_vif_ref(domid, devid)
56 sys.stderr.write("ERROR: Could not find interface vif%s.%s\n"
60 dump_vif_info(domid, devid, vif_ref)