Package ganeti :: Package storage :: Module base
[hide private]
[frames] | no frames]

Source Code for Module ganeti.storage.base

  1  # 
  2  # 
  3   
  4  # Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013 Google Inc. 
  5  # All rights reserved. 
  6  # 
  7  # Redistribution and use in source and binary forms, with or without 
  8  # modification, are permitted provided that the following conditions are 
  9  # met: 
 10  # 
 11  # 1. Redistributions of source code must retain the above copyright notice, 
 12  # this list of conditions and the following disclaimer. 
 13  # 
 14  # 2. Redistributions in binary form must reproduce the above copyright 
 15  # notice, this list of conditions and the following disclaimer in the 
 16  # documentation and/or other materials provided with the distribution. 
 17  # 
 18  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 
 19  # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 
 20  # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 21  # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 
 22  # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 23  # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 24  # PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
 25  # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 26  # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
 27  # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
 28  # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29   
 30   
 31  """Block device abstraction - base class and utility functions""" 
 32   
 33  import logging 
 34   
 35  from ganeti import objects 
 36  from ganeti import constants 
 37  from ganeti import utils 
 38  from ganeti import errors 
39 40 41 -class BlockDev(object):
42 """Block device abstract class. 43 44 A block device can be in the following states: 45 - not existing on the system, and by `Create()` it goes into: 46 - existing but not setup/not active, and by `Assemble()` goes into: 47 - active read-write and by `Open()` it goes into 48 - online (=used, or ready for use) 49 50 A device can also be online but read-only, however we are not using 51 the readonly state (LV has it, if needed in the future) and we are 52 usually looking at this like at a stack, so it's easier to 53 conceptualise the transition from not-existing to online and back 54 like a linear one. 55 56 The many different states of the device are due to the fact that we 57 need to cover many device types: 58 - logical volumes are created, lvchange -a y $lv, and used 59 - drbd devices are attached to a local disk/remote peer and made primary 60 61 A block device is identified by three items: 62 - the /dev path of the device (dynamic) 63 - a unique ID of the device (static) 64 - it's major/minor pair (dynamic) 65 66 Not all devices implement both the first two as distinct items. LVM 67 logical volumes have their unique ID (the pair volume group, logical 68 volume name) in a 1-to-1 relation to the dev path. For DRBD devices, 69 the /dev path is again dynamic and the unique id is the pair (host1, 70 dev1), (host2, dev2). 71 72 You can get to a device in two ways: 73 - creating the (real) device, which returns you 74 an attached instance (lvcreate) 75 - attaching of a python instance to an existing (real) device 76 77 The second point, the attachment to a device, is different 78 depending on whether the device is assembled or not. At init() time, 79 we search for a device with the same unique_id as us. If found, 80 good. It also means that the device is already assembled. If not, 81 after assembly we'll have our correct major/minor. 82 83 """
84 - def __init__(self, unique_id, children, size, params, dyn_params):
85 self._children = children 86 self.dev_path = None 87 self.unique_id = unique_id 88 self.major = None 89 self.minor = None 90 self.attached = False 91 self.size = size 92 self.params = params 93 self.dyn_params = dyn_params
94
95 - def Assemble(self):
96 """Assemble the device from its components. 97 98 Implementations of this method by child classes must ensure that: 99 - after the device has been assembled, it knows its major/minor 100 numbers; this allows other devices (usually parents) to probe 101 correctly for their children 102 - calling this method on an existing, in-use device is safe 103 - if the device is already configured (and in an OK state), 104 this method is idempotent 105 106 """ 107 pass
108
109 - def Attach(self):
110 """Find a device which matches our config and attach to it. 111 112 """ 113 raise NotImplementedError
114
115 - def Close(self):
116 """Notifies that the device will no longer be used for I/O. 117 118 """ 119 raise NotImplementedError
120 121 @classmethod
122 - def Create(cls, unique_id, children, size, spindles, params, excl_stor, 123 dyn_params):
124 """Create the device. 125 126 If the device cannot be created, it will return None 127 instead. Error messages go to the logging system. 128 129 Note that for some devices, the unique_id is used, and for other, 130 the children. The idea is that these two, taken together, are 131 enough for both creation and assembly (later). 132 133 @type unique_id: 2-element tuple or list 134 @param unique_id: unique identifier; the details depend on the actual device 135 type 136 @type children: list of L{BlockDev} 137 @param children: for hierarchical devices, the child devices 138 @type size: float 139 @param size: size in MiB 140 @type spindles: int 141 @param spindles: number of physical disk to dedicate to the device 142 @type params: dict 143 @param params: device-specific options/parameters 144 @type excl_stor: bool 145 @param excl_stor: whether exclusive_storage is active 146 @type dyn_params: dict 147 @param dyn_params: dynamic parameters of the disk only valid for this node. 148 As set by L{objects.Disk.UpdateDynamicDiskParams}. 149 @rtype: L{BlockDev} 150 @return: the created device, or C{None} in case of an error 151 152 """ 153 raise NotImplementedError
154
155 - def Remove(self):
156 """Remove this device. 157 158 This makes sense only for some of the device types: LV and file 159 storage. Also note that if the device can't attach, the removal 160 can't be completed. 161 162 """ 163 raise NotImplementedError
164
165 - def Rename(self, new_id):
166 """Rename this device. 167 168 This may or may not make sense for a given device type. 169 170 """ 171 raise NotImplementedError
172
173 - def Open(self, force=False):
174 """Make the device ready for use. 175 176 This makes the device ready for I/O. For now, just the DRBD 177 devices need this. 178 179 The force parameter signifies that if the device has any kind of 180 --force thing, it should be used, we know what we are doing. 181 182 @type force: boolean 183 184 """ 185 raise NotImplementedError
186
187 - def Shutdown(self):
188 """Shut down the device, freeing its children. 189 190 This undoes the `Assemble()` work, except for the child 191 assembling; as such, the children on the device are still 192 assembled after this call. 193 194 """ 195 raise NotImplementedError
196
197 - def SetSyncParams(self, params):
198 """Adjust the synchronization parameters of the mirror. 199 200 In case this is not a mirroring device, this is no-op. 201 202 @param params: dictionary of LD level disk parameters related to the 203 synchronization. 204 @rtype: list 205 @return: a list of error messages, emitted both by the current node and by 206 children. An empty list means no errors. 207 208 """ 209 result = [] 210 if self._children: 211 for child in self._children: 212 result.extend(child.SetSyncParams(params)) 213 return result
214
215 - def PauseResumeSync(self, pause):
216 """Pause/Resume the sync of the mirror. 217 218 In case this is not a mirroring device, this is no-op. 219 220 @type pause: boolean 221 @param pause: Whether to pause or resume 222 223 """ 224 result = True 225 if self._children: 226 for child in self._children: 227 result = result and child.PauseResumeSync(pause) 228 return result
229
230 - def GetSyncStatus(self):
231 """Returns the sync status of the device. 232 233 If this device is a mirroring device, this function returns the 234 status of the mirror. 235 236 If sync_percent is None, it means the device is not syncing. 237 238 If estimated_time is None, it means we can't estimate 239 the time needed, otherwise it's the time left in seconds. 240 241 If is_degraded is True, it means the device is missing 242 redundancy. This is usually a sign that something went wrong in 243 the device setup, if sync_percent is None. 244 245 The ldisk parameter represents the degradation of the local 246 data. This is only valid for some devices, the rest will always 247 return False (not degraded). 248 249 @rtype: objects.BlockDevStatus 250 251 """ 252 return objects.BlockDevStatus(dev_path=self.dev_path, 253 major=self.major, 254 minor=self.minor, 255 sync_percent=None, 256 estimated_time=None, 257 is_degraded=False, 258 ldisk_status=constants.LDS_OKAY)
259
260 - def CombinedSyncStatus(self):
261 """Calculate the mirror status recursively for our children. 262 263 The return value is the same as for `GetSyncStatus()` except the 264 minimum percent and maximum time are calculated across our 265 children. 266 267 @rtype: objects.BlockDevStatus 268 269 """ 270 status = self.GetSyncStatus() 271 272 min_percent = status.sync_percent 273 max_time = status.estimated_time 274 is_degraded = status.is_degraded 275 ldisk_status = status.ldisk_status 276 277 if self._children: 278 for child in self._children: 279 child_status = child.GetSyncStatus() 280 281 if min_percent is None: 282 min_percent = child_status.sync_percent 283 elif child_status.sync_percent is not None: 284 min_percent = min(min_percent, child_status.sync_percent) 285 286 if max_time is None: 287 max_time = child_status.estimated_time 288 elif child_status.estimated_time is not None: 289 max_time = max(max_time, child_status.estimated_time) 290 291 is_degraded = is_degraded or child_status.is_degraded 292 293 if ldisk_status is None: 294 ldisk_status = child_status.ldisk_status 295 elif child_status.ldisk_status is not None: 296 ldisk_status = max(ldisk_status, child_status.ldisk_status) 297 298 return objects.BlockDevStatus(dev_path=self.dev_path, 299 major=self.major, 300 minor=self.minor, 301 sync_percent=min_percent, 302 estimated_time=max_time, 303 is_degraded=is_degraded, 304 ldisk_status=ldisk_status)
305
306 - def SetInfo(self, text):
307 """Update metadata with info text. 308 309 Only supported for some device types. 310 311 """ 312 for child in self._children: 313 child.SetInfo(text)
314
315 - def Grow(self, amount, dryrun, backingstore, excl_stor):
316 """Grow the block device. 317 318 @type amount: integer 319 @param amount: the amount (in mebibytes) to grow with 320 @type dryrun: boolean 321 @param dryrun: whether to execute the operation in simulation mode 322 only, without actually increasing the size 323 @param backingstore: whether to execute the operation on backing storage 324 only, or on "logical" storage only; e.g. DRBD is logical storage, 325 whereas LVM, file, RBD are backing storage 326 @type excl_stor: boolean 327 @param excl_stor: Whether exclusive_storage is active 328 329 """ 330 raise NotImplementedError
331
332 - def GetActualSize(self):
333 """Return the actual disk size. 334 335 @note: the device needs to be active when this is called 336 337 """ 338 assert self.attached, "BlockDevice not attached in GetActualSize()" 339 result = utils.RunCmd(["blockdev", "--getsize64", self.dev_path]) 340 if result.failed: 341 ThrowError("blockdev failed (%s): %s", 342 result.fail_reason, result.output) 343 try: 344 sz = int(result.output.strip()) 345 except (ValueError, TypeError), err: 346 ThrowError("Failed to parse blockdev output: %s", str(err)) 347 return sz
348
349 - def GetActualSpindles(self):
350 """Return the actual number of spindles used. 351 352 This is not supported by all devices; if not supported, C{None} is returned. 353 354 @note: the device needs to be active when this is called 355 356 """ 357 assert self.attached, "BlockDevice not attached in GetActualSpindles()" 358 return None
359
360 - def GetActualDimensions(self):
361 """Return the actual disk size and number of spindles used. 362 363 @rtype: tuple 364 @return: (size, spindles); spindles is C{None} when they are not supported 365 366 @note: the device needs to be active when this is called 367 368 """ 369 return (self.GetActualSize(), self.GetActualSpindles())
370
371 - def GetUserspaceAccessUri(self, hypervisor):
372 """Return URIs hypervisors can use to access disks in userspace mode. 373 374 @rtype: string 375 @return: userspace device URI 376 @raise errors.BlockDeviceError: if userspace access is not supported 377 378 """ 379 ThrowError("Userspace access with %s block device and %s hypervisor is not " 380 "supported." % (self.__class__.__name__, 381 hypervisor))
382
383 - def __repr__(self):
384 return ("<%s: unique_id: %s, children: %s, %s:%s, %s>" % 385 (self.__class__, self.unique_id, self._children, 386 self.major, self.minor, self.dev_path))
387
388 389 -def ThrowError(msg, *args):
390 """Log an error to the node daemon and the raise an exception. 391 392 @type msg: string 393 @param msg: the text of the exception 394 @raise errors.BlockDeviceError 395 396 """ 397 if args: 398 msg = msg % args 399 logging.error(msg) 400 raise errors.BlockDeviceError(msg)
401
402 403 -def IgnoreError(fn, *args, **kwargs):
404 """Executes the given function, ignoring BlockDeviceErrors. 405 406 This is used in order to simplify the execution of cleanup or 407 rollback functions. 408 409 @rtype: boolean 410 @return: True when fn didn't raise an exception, False otherwise 411 412 """ 413 try: 414 fn(*args, **kwargs) 415 return True 416 except errors.BlockDeviceError, err: 417 logging.warning("Caught BlockDeviceError but ignoring: %s", str(err)) 418 return False
419