debuggers.hg

view tools/python/xen/xend/XendVDI.py @ 0:7d21f7218375

Exact replica of unstable on 051908 + README-this
author Mukesh Rathor
date Mon May 19 15:34:57 2008 -0700 (2008-05-19)
parents
children b22f9ab1716a
line source
1 #!/usr/bin/python
2 #============================================================================
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of version 2.1 of the GNU Lesser General Public
5 # License as published by the Free Software Foundation.
6 #
7 # This library is distributed in the hope that it will be useful,
8 # but WITHOUT ANY WARRANTY; without even the implied warranty of
9 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 # Lesser General Public License for more details.
11 #
12 # You should have received a copy of the GNU Lesser General Public
13 # License along with this library; if not, write to the Free Software
14 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15 #============================================================================
16 # Copyright (C) 2006 XenSource Ltd.
17 #============================================================================
18 #
19 # Representation of a Xen API VDI
20 #
22 import os
24 from xen.util.xmlrpclib2 import stringify
25 from xmlrpclib import dumps, loads
26 from xen.util import xsconstants
27 import xen.util.xsm.xsm as security
28 from xen.xend.XendError import SecurityError
30 KB = 1024
31 MB = 1024 * 1024
33 class AutoSaveObject(object):
35 def __init__(self):
36 self.cfg_path = None
37 self.auto_save = True
38 object
40 def save_config(self, cfg_file = None):
41 raise NotImplementedError()
43 def __setattr__(self, name, value):
44 """A very simple way of making sure all attribute changes are
45 flushed to disk.
46 """
47 object.__setattr__(self, name, value)
48 if name != 'auto_save' and getattr(self, 'auto_save', False):
49 self.save_config()
51 class XendVDI(AutoSaveObject):
52 """Generic Xen API compatible VDI representation.
54 @cvar SAVED_CFG: list of configuration attributes to save.
55 @cvar SAVED_CFG_INT: list of configurations that should be ints.
56 """
58 SAVED_CFG = ['name_label',
59 'name_description',
60 'virtual_size',
61 'physical_utilisation',
62 'sharable',
63 'read_only']
65 SAVED_CFG_INT = ['sector_size', 'virtual_size', 'physical_utilisation']
67 def __init__(self, uuid, sr_uuid):
68 self.uuid = uuid
69 self.sr_uuid = sr_uuid
70 self.name_label = ""
71 self.name_description = ""
72 self.virtual_size = 0
73 self.physical_utilisation = 0
74 self.sharable = False
75 self.read_only = False
76 self.type = "system"
77 self.other_config = {}
78 self.vbds = []
80 def addVBD(self, vbd_ref):
81 self.vbds.append(vbd_ref)
83 def removeVBD(self, vbd_ref):
84 self.vbds.remove(vbd_ref)
86 def getVBDs(self):
87 return self.vbds
89 def load_config_dict(self, cfg):
90 """Loads configuration into the object from a dict.
92 @param cfg: configuration dict
93 @type cfg: dict
94 """
95 self.auto_save = False
96 for key in self.SAVED_CFG:
97 if key in cfg:
98 if key in self.SAVED_CFG_INT:
99 setattr(self, key, int(cfg[key]))
100 else:
101 setattr(self, key, cfg[key])
102 self.auto_save = True
104 def load_config(self, cfg_path):
105 """Loads configuration from an XMLRPC parameter format.
107 @param cfg_path: configuration file path
108 @type cfg_path: type
109 @rtype: bool
110 @return: Successful or not.
111 """
112 try:
113 cfg, _ = loads(open(cfg_path).read())
114 cfg = cfg[0]
115 self.load_config_dict(cfg)
116 self.cfg_path = cfg_path
117 except IOError, e:
118 return False
120 return True
122 def save_config(self, cfg_path = None):
123 """Saves configuration at give path in XMLRPC parameter format.
125 If cfg_path is not give, it defaults to the where the VDI
126 configuration as loaded if it load_config was called.
128 @keyword cfg_path: optional configuration file path
129 @rtype: bool
130 @return: Successful or not.
131 """
132 try:
133 if not cfg_path and not self.cfg_path:
134 return False
136 if not cfg_path:
137 cfg_path = self.cfg_path
139 cfg = {}
140 for key in self.SAVED_CFG:
141 try:
142 cfg[key] = getattr(self, key)
143 except AttributeError:
144 pass
145 open(cfg_path, 'w').write(dumps((stringify(cfg),),
146 allow_none = True))
147 except IOError, e:
148 return False
150 return True
152 def get_record(self, transient = True):
153 return {'uuid': self.uuid,
154 'name_label': self.name_label,
155 'name_description': self.name_description,
156 'virtual_size': self.virtual_size,
157 'physical_utilisation': self.physical_utilisation,
158 'sharable': False,
159 'readonly': False,
160 'SR': self.sr_uuid,
161 'other_config': self.other_config,
162 'VBDs': []}
164 def get_location(self):
165 raise NotImplementedError()
167 def set_security_label(self, sec_lab, old_lab):
168 image = self.get_location()
169 rc = security.set_resource_label_xapi(image, sec_lab, old_lab)
170 if rc != xsconstants.XSERR_SUCCESS:
171 raise SecurityError(rc)
172 return rc
174 def get_security_label(self):
175 image = self.get_location()
176 return security.get_resource_label_xapi(image)
179 class XendQCoWVDI(XendVDI):
180 def __init__(self, uuid, sr_uuid, qcow_path, cfg_path, vsize, psize):
181 XendVDI.__init__(self, uuid, sr_uuid)
182 self.auto_save = False
183 self.qcow_path = qcow_path
184 self.cfg_path = cfg_path
185 self.physical_utilisation = psize
186 self.virtual_size = vsize
187 self.auto_save = True
188 self.other_config['location'] = 'tap:qcow:%s' % self.qcow_path
190 def get_location(self):
191 return self.other_config['location']
193 class XendLocalVDI(XendVDI):
194 def __init__(self, vdi_struct):
195 vdi_uuid = vdi_struct['uuid']
196 sr_uuid = vdi_struct['SR']
197 XendVDI.__init__(self, vdi_uuid, sr_uuid)
199 self.auto_save = False
200 self.cfg_path = None
201 self.name_label = vdi_struct.get('name_label','')
202 self.name_description = vdi_struct.get('name_description', '')
203 self.physical_utilisation = 0
204 self.virtual_size = 0
205 self.type = vdi_struct.get('type', '')
206 self.sharable = vdi_struct.get('sharable', False)
207 self.read_only = vdi_struct.get('read_only', False)
208 self.other_config = vdi_struct.get('other_config', {})
210 def get_location(self):
211 return self.other_config['location']