139
150
# available under the LGPL,
140
151
# Copyright 2004, 2005 Mike Wray <mike.wray@hp.com>
141
152
# Copyright 2005 XenSource Ltd
153
def randomMAC(type = "xen"):
143
154
"""Generate a random MAC address.
145
Uses OUI (Organizationally Unique Identifier) 00-16-3E, allocated to
146
Xensource, Inc. The OUI list is available at
147
http://standards.ieee.org/regauth/oui/oui.txt.
156
00-16-3E allocated to xensource
157
54-52-00 used by qemu/kvm
159
The OUI list is available at http://standards.ieee.org/regauth/oui/oui.txt.
149
161
The remaining 3 fields are random, with the first bit of the first
150
162
random field set 0.
164
>>> randomMAC().startswith("00:16:36")
166
>>> randomMAC("foobar").startswith("00:16:36")
168
>>> randomMAC("xen").startswith("00:16:36")
170
>>> randomMAC("qemu").startswith("54:52:00")
152
173
@return: MAC address string
154
mac = [ 0x00, 0x16, 0x3e,
175
ouis = { 'xen': [ 0x00, 0x16, 0x36 ], 'qemu': [ 0x54, 0x52, 0x00 ] }
155
183
random.randint(0x00, 0x7f),
156
184
random.randint(0x00, 0xff),
157
185
random.randint(0x00, 0xff) ]
219
253
str = str.replace("<", "<")
220
254
str = str.replace(">", ">")
257
def compareMAC(p, q):
258
"""Compare two MAC addresses"""
262
if len(pa) != len(qa):
268
for i in xrange(len(pa)):
269
n = int(pa[i], 0x10) - int(qa[i], 0x10)
276
def default_keymap():
277
"""Look in /etc/sysconfig for the host machine's keymap, and attempt to
278
map it to a keymap supported by qemu"""
280
# Set keymap to same as hosts
284
f = open(KEYBOARD_DIR, "r")
286
logging.debug('Could not open "/etc/sysconfig/keyboard" ' + str(e))
292
if re.search("KEYTABLE", s) != None:
294
if keytable.keytable.has_key(kt.lower()):
295
keymap = keytable.keytable[kt]
297
logging.debug("Didn't find keymap '%s' in keytable!" % kt)
301
def pygrub_path(conn=None):
303
Return the pygrub path for the current host, or connection if
306
# FIXME: This should be removed/deprecated when capabilities are
307
# fixed to provide bootloader info
309
cap = CapabilitiesParser.parse(conn.getCapabilities())
310
if (cap.host.arch == "i86pc"):
311
return "/usr/lib/xen/bin/pygrub"
313
return "/usr/bin/pygrub"
315
if platform.system() == "SunOS":
316
return "/usr/lib/xen/bin/pygrub"
317
return "/usr/bin/pygrub"
321
Parse a libvirt hypervisor uri into it's individual parts
322
@returns: tuple of the form (scheme (ex. 'qemu', 'xen+ssh'), username,
323
hostname, path (ex. '/system'), query,
326
def splitnetloc(url, start=0):
327
for c in '/?#': # the order is important!
328
delim = url.find(c, start)
333
return url[start:delim], url[delim:]
335
username = netloc = query = fragment = ''
338
scheme, uri = uri[:i].lower(), uri[i+1:]
340
netloc, uri = splitnetloc(uri, 2)
341
offset = netloc.find("@")
343
username = netloc[0:offset]
344
netloc = netloc[offset+1:]
346
uri, fragment = uri.split('#', 1)
348
uri, query = uri.split('?', 1)
351
return scheme, username, netloc, uri, query, fragment
354
def is_uri_remote(uri):
356
(scheme, username, netloc, path, query, fragment) = uri_split(uri)
361
logging.exception("Error parsing URI in is_remote: %s" % e)
364
def get_uri_hostname(uri):
366
(scheme, username, netloc, path, query, fragment) = uri_split(uri)
371
logging.warning("Cannot parse URI %s: %s" % (uri, str(e)))
374
def get_uri_transport(uri):
376
(scheme, username, netloc, path, query, fragment) = uri_split(uri)
378
offset = scheme.index("+")
380
return [scheme[offset+1:], username]
385
def get_uri_driver(uri):
387
(scheme, username, netloc, path, query, fragment) = uri_split(uri)
389
offset = scheme.find("+")
391
return scheme[:offset]
397
def is_storage_capable(conn):
398
"""check if virConnectPtr passed has storage API support"""
401
if not isinstance(conn, libvirt.virConnect):
402
raise ValueError(_("'conn' must be a virConnect instance."))
404
if not dir(conn).count("listStoragePools"):
406
n = conn.listStoragePools()
407
except libvirt.libvirtError, e:
408
if e.get_error_code() == libvirt.VIR_ERR_RPC or \
409
e.get_error_code() == libvirt.VIR_ERR_NO_SUPPORT:
413
def get_xml_path(xml, path):
414
"""return the xpath from the passed xml"""
419
doc = libxml2.parseDoc(xml)
420
ctx = doc.xpathNewContext()
421
ret = ctx.xpathEval(path)
424
if type(ret) == list:
434
ctx.xpathFreeContext()
437
def lookup_pool_by_path(conn, path):
439
Return the first pool with matching matching target path.
440
return the first we find, active or inactive. This iterates over
441
all pools and dumps their xml, so it is NOT quick.
442
@return virStoragePool object if found, None otherwise
444
if not is_storage_capable(conn):
447
pool_list = conn.listStoragePools() + conn.listDefinedStoragePools()
448
for name in pool_list:
449
pool = conn.storagePoolLookupByName(name)
450
if get_xml_path(pool.XMLDesc(0), "/pool/target/path") == path:
458
if __name__ == "__main__":