From fa283d19918e13f6dc8234039b8d3b5ce692568d Mon Sep 17 00:00:00 2001 From: "cl349@firebug.cl.cam.ac.uk" Date: Mon, 6 Jun 2005 17:51:41 +0000 Subject: bitkeeper revision 1.1662.1.10 (42a48d2dOYGp10ZpkS7A_bvbZcKyOw) XendDomainInfo.py, XendDomain.py: Add uuids for domains. uuid.py: new file Signed-off-by: Mike Wray Signed-off-by: Christian Limpach --- .rootkeys | 1 + tools/python/xen/xend/XendDomain.py | 8 ++-- tools/python/xen/xend/XendDomainInfo.py | 25 ++++++++----- tools/python/xen/xend/uuid.py | 65 +++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 13 deletions(-) create mode 100644 tools/python/xen/xend/uuid.py diff --git a/.rootkeys b/.rootkeys index c0914886f0..3759be63f9 100644 --- a/.rootkeys +++ b/.rootkeys @@ -875,6 +875,7 @@ 4294a1bf8rMUcddot-B2-pOxORimOg tools/python/xen/xend/server/relocate.py 41ee5e8dq9NtihbL4nWKjuSLOhXPUg tools/python/xen/xend/server/usbif.py 40c9c469LNxLVizOUpOjEaTKKCm8Aw tools/python/xen/xend/sxp.py +42a48d152jkT7ykQT_LWKnS-ojV_ZA tools/python/xen/xend/uuid.py 40d05079aFRp6NQdo5wIh5Ly31c0cg tools/python/xen/xm/__init__.py 40cf2937gKQcATgXKGtNeWb1PDH5nA tools/python/xen/xm/create.py 40f552eariuUSB9TWqCPnDLz5zvxMw tools/python/xen/xm/destroy.py diff --git a/tools/python/xen/xend/XendDomain.py b/tools/python/xen/xend/XendDomain.py index eed2e7f8a3..5819a9e4bf 100644 --- a/tools/python/xen/xend/XendDomain.py +++ b/tools/python/xen/xend/XendDomain.py @@ -125,7 +125,8 @@ class XendDomain: @param info: domain info from xen @return: domain """ - dominfo = XendDomainInfo.recreate(savedinfo, info) + uuid = sxp.child_value(savedinfo, 'uuid') + dominfo = XendDomainInfo.recreate(savedinfo, info, uuid) self.domains[dominfo.id] = dominfo self.sync_domain(dominfo) return dominfo @@ -295,7 +296,8 @@ class XendDomain: @param vmconfig: vm configuration """ config = sxp.child_value(vmconfig, 'config') - dominfo = XendDomainInfo.restore(config) + uuid = sxp.child_value(vmconfig, 'uuid') + dominfo = XendDomainInfo.restore(config, uuid=uuid) self._add_domain(dominfo) return dominfo @@ -329,7 +331,7 @@ class XendDomain: info = self.xen_domain(id) if info: log.info("Creating entry for unknown domain: id=%d", id) - dominfo = XendDomainInfo.recreate(None, info, unknown=True) + dominfo = XendDomainInfo.recreate(None, info) self._add_domain(dominfo) except Exception, ex: log.exception("Error creating domain info: id=%d", id) diff --git a/tools/python/xen/xend/XendDomainInfo.py b/tools/python/xen/xend/XendDomainInfo.py index dfce49d9e6..257ee38c4f 100644 --- a/tools/python/xen/xend/XendDomainInfo.py +++ b/tools/python/xen/xend/XendDomainInfo.py @@ -29,6 +29,8 @@ from xen.xend.XendLogging import log from XendError import XendError, VmError from xen.xend.XendRoot import get_component +from xen.xend.uuid import getUuid + """Flag for a block device backend domain.""" SIF_BLK_BE_DOMAIN = (1<<4) @@ -143,12 +145,16 @@ class XendDomainInfo: """ MINIMUM_RESTART_TIME = 20 - def _create(cls): - """Create a vm object. + def _create(cls, uuid=None): + """Create a vm object with a uuid. + @param uuid uuid to use (generated if None) @return vm """ + if uuid is None: + uuid = getUuid() vm = cls() + vm.uuid = uuid return vm _create = classmethod(_create) @@ -167,17 +173,14 @@ class XendDomainInfo: create = classmethod(create) - def recreate(cls, savedinfo, info, unknown=False): + def recreate(cls, savedinfo, info, uuid=None): """Create the VM object for an existing domain. @param savedinfo: saved info from the domain DB @param info: domain info from xc @type info: xc domain dict """ - if unknown: - vm = cls._create() - else: - vm = cls() + vm = cls._create(uuid=uuid) log.debug('savedinfo=' + prettyprintstring(savedinfo)) log.debug('info=' + str(info)) @@ -209,12 +212,12 @@ class XendDomainInfo: recreate = classmethod(recreate) - def restore(cls, config): + def restore(cls, config, uuid=None): """Create a domain and a VM object to do a restore. @param config: domain configuration """ - vm = cls._create() + vm = cls._create(uuid=uuid) dom = xc.domain_create() vm.setdom(dom) vm.dom_construct(vm.id, config) @@ -227,6 +230,7 @@ class XendDomainInfo: self.restore = 0 self.config = None + self.uuid = None self.id = None self.cpu_weight = 1 self.start_time = None @@ -365,7 +369,8 @@ class XendDomainInfo: ['id', self.id], ['name', self.name], ['memory', self.memory] ] - + if self.uuid: + sxpr.append(['uuid', self.uuid]) if self.info: sxpr.append(['maxmem', self.info['maxmem_kb']/1024 ]) run = (self.info['running'] and 'r') or '-' diff --git a/tools/python/xen/xend/uuid.py b/tools/python/xen/xend/uuid.py new file mode 100644 index 0000000000..096fef7f9f --- /dev/null +++ b/tools/python/xen/xend/uuid.py @@ -0,0 +1,65 @@ +"""Universal(ly) Unique Identifiers (UUIDs). +""" +import commands +import random + +def uuidgen(random=True): + """Generate a UUID using the command uuidgen. + + If random is true (default) generates a random uuid. + If random is false generates a time-based uuid. + """ + cmd = "uuidgen" + if random: + cmd += " -r" + else: + cmd += " -t" + return commands.getoutput(cmd) + +class UuidFactoryUuidgen: + + """A uuid factory using uuidgen.""" + + def __init__(self): + pass + + def getUuid(self): + return uuidgen() + +class UuidFactoryRandom: + + """A random uuid factory.""" + + def __init__(self): + f = file("/dev/urandom", "r") + seed = f.read(16) + f.close() + self.rand = random.Random(seed) + + def randBytes(self, n): + return [ self.rand.randint(0, 255) for i in range(0, n) ] + + def getUuid(self): + bytes = self.randBytes(16) + # Encode the variant. + bytes[6] = (bytes[6] & 0x0f) | 0x40 + bytes[8] = (bytes[8] & 0x3f) | 0x80 + f = "%02x" + return ( "-".join([f*4, f*2, f*2, f*2, f*6]) % tuple(bytes) ) + +def getFactory(): + """Get the factory to use for creating uuids. + This is so it's easy to change the uuid factory. + For example, for testing we might want repeatable uuids + rather than the random ones we normally use. + """ + global uuidFactory + try: + uuidFactory + except: + #uuidFactory = UuidFactoryUuidgen() + uuidFactory = UuidFactoryRandom() + return uuidFactory + +def getUuid(): + return getFactory().getUuid() -- cgit v1.2.3