mirror of
https://github.com/xen-project/xen.git
synced 2026-10-29 02:30:03 +01:00
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 <mike.wray@hp.com> Signed-off-by: Christian Limpach <Christian.Limpach@cl.cam.ac.uk>
This commit is contained in:
1 parent
f6a4e3e4a6
commit
fa283d1991
4 files changed
+86
-13
No files matched your search
@@ -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)
|
||||
|
||||
@@ -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 '-'
|
||||
|
||||
@@ -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()
|
||||