1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
85 - def __init__(self, unique_id, children, size, params, dyn_params, *args):
86 self._children = children
87 self.dev_path = None
88 self.unique_id = unique_id
89 self.major = None
90 self.minor = None
91 self.attached = False
92 self.size = size
93 self.params = params
94 self.dyn_params = dyn_params
95
97 """Assemble the device from its components.
98
99 Implementations of this method by child classes must ensure that:
100 - after the device has been assembled, it knows its major/minor
101 numbers; this allows other devices (usually parents) to probe
102 correctly for their children
103 - calling this method on an existing, in-use device is safe
104 - if the device is already configured (and in an OK state),
105 this method is idempotent
106
107 """
108 pass
109
111 """Find a device which matches our config and attach to it.
112
113 """
114 raise NotImplementedError
115
117 """Notifies that the device will no longer be used for I/O.
118
119 """
120 raise NotImplementedError
121
122 @classmethod
123 - def Create(cls, unique_id, children, size, spindles, params, excl_stor,
124 dyn_params, *args):
125 """Create the device.
126
127 If the device cannot be created, it will return None
128 instead. Error messages go to the logging system.
129
130 Note that for some devices, the unique_id is used, and for other,
131 the children. The idea is that these two, taken together, are
132 enough for both creation and assembly (later).
133
134 @type unique_id: 2-element tuple or list
135 @param unique_id: unique identifier; the details depend on the actual device
136 type
137 @type children: list of L{BlockDev}
138 @param children: for hierarchical devices, the child devices
139 @type size: float
140 @param size: size in MiB
141 @type spindles: int
142 @param spindles: number of physical disk to dedicate to the device
143 @type params: dict
144 @param params: device-specific options/parameters
145 @type excl_stor: bool
146 @param excl_stor: whether exclusive_storage is active
147 @type dyn_params: dict
148 @param dyn_params: dynamic parameters of the disk only valid for this node.
149 As set by L{objects.Disk.UpdateDynamicDiskParams}.
150 @rtype: L{BlockDev}
151 @return: the created device, or C{None} in case of an error
152
153 """
154 raise NotImplementedError
155
157 """Remove this device.
158
159 This makes sense only for some of the device types: LV and file
160 storage. Also note that if the device can't attach, the removal
161 can't be completed.
162
163 """
164 raise NotImplementedError
165
167 """Rename this device.
168
169 This may or may not make sense for a given device type.
170
171 """
172 raise NotImplementedError
173
174 - def Open(self, force=False):
175 """Make the device ready for use.
176
177 This makes the device ready for I/O. For now, just the DRBD
178 devices need this.
179
180 The force parameter signifies that if the device has any kind of
181 --force thing, it should be used, we know what we are doing.
182
183 @type force: boolean
184
185 """
186 raise NotImplementedError
187
189 """Shut down the device, freeing its children.
190
191 This undoes the `Assemble()` work, except for the child
192 assembling; as such, the children on the device are still
193 assembled after this call.
194
195 """
196 raise NotImplementedError
197
199 """Builds the shell command for importing data to device.
200
201 This method returns the command that will be used by the caller to
202 import data to the target device during the disk template conversion
203 operation.
204
205 Block devices that provide a more efficient way to transfer their
206 data can override this method to use their specific utility.
207
208 @rtype: list of strings
209 @return: List containing the import command for device
210
211 """
212 if not self.minor and not self.Attach():
213 ThrowError("Can't attach to target device during Import()")
214
215
216
217 return [constants.DD_CMD,
218 "of=%s" % self.dev_path,
219 "bs=%s" % constants.DD_BLOCK_SIZE,
220 "oflag=direct", "conv=notrunc"]
221
223 """Builds the shell command for exporting data from device.
224
225 This method returns the command that will be used by the caller to
226 export data from the source device during the disk template conversion
227 operation.
228
229 Block devices that provide a more efficient way to transfer their
230 data can override this method to use their specific utility.
231
232 @rtype: list of strings
233 @return: List containing the export command for device
234
235 """
236 if not self.minor and not self.Attach():
237 ThrowError("Can't attach to source device during Import()")
238
239 return [constants.DD_CMD,
240 "if=%s" % self.dev_path,
241 "bs=%s" % constants.DD_BLOCK_SIZE,
242 "count=%s" % self.size,
243 "iflag=direct"]
244
245 - def Snapshot(self, snap_name, snap_size):
246 """Creates a snapshot of the block device.
247
248 Currently this is used only during LUInstanceExport.
249
250 @type snap_name: string
251 @param snap_name: The name of the snapshot.
252 @type snap_size: int
253 @param snap_size: The size of the snapshot.
254 @rtype: tuple
255 @return: The logical id of the newly created disk.
256
257 """
258 ThrowError("Snapshot is not supported for disk %s of type %s.",
259 self.unique_id, self.__class__.__name__)
260
262 """Adjust the synchronization parameters of the mirror.
263
264 In case this is not a mirroring device, this is no-op.
265
266 @param params: dictionary of LD level disk parameters related to the
267 synchronization.
268 @rtype: list
269 @return: a list of error messages, emitted both by the current node and by
270 children. An empty list means no errors.
271
272 """
273 result = []
274 if self._children:
275 for child in self._children:
276 result.extend(child.SetSyncParams(params))
277 return result
278
280 """Pause/Resume the sync of the mirror.
281
282 In case this is not a mirroring device, this is no-op.
283
284 @type pause: boolean
285 @param pause: Whether to pause or resume
286
287 """
288 result = True
289 if self._children:
290 for child in self._children:
291 result = result and child.PauseResumeSync(pause)
292 return result
293
295 """Returns the sync status of the device.
296
297 If this device is a mirroring device, this function returns the
298 status of the mirror.
299
300 If sync_percent is None, it means the device is not syncing.
301
302 If estimated_time is None, it means we can't estimate
303 the time needed, otherwise it's the time left in seconds.
304
305 If is_degraded is True, it means the device is missing
306 redundancy. This is usually a sign that something went wrong in
307 the device setup, if sync_percent is None.
308
309 The ldisk parameter represents the degradation of the local
310 data. This is only valid for some devices, the rest will always
311 return False (not degraded).
312
313 @rtype: objects.BlockDevStatus
314
315 """
316 return objects.BlockDevStatus(dev_path=self.dev_path,
317 major=self.major,
318 minor=self.minor,
319 sync_percent=None,
320 estimated_time=None,
321 is_degraded=False,
322 ldisk_status=constants.LDS_OKAY)
323
325 """Calculate the mirror status recursively for our children.
326
327 The return value is the same as for `GetSyncStatus()` except the
328 minimum percent and maximum time are calculated across our
329 children.
330
331 @rtype: objects.BlockDevStatus
332
333 """
334 status = self.GetSyncStatus()
335
336 min_percent = status.sync_percent
337 max_time = status.estimated_time
338 is_degraded = status.is_degraded
339 ldisk_status = status.ldisk_status
340
341 if self._children:
342 for child in self._children:
343 child_status = child.GetSyncStatus()
344
345 if min_percent is None:
346 min_percent = child_status.sync_percent
347 elif child_status.sync_percent is not None:
348 min_percent = min(min_percent, child_status.sync_percent)
349
350 if max_time is None:
351 max_time = child_status.estimated_time
352 elif child_status.estimated_time is not None:
353 max_time = max(max_time, child_status.estimated_time)
354
355 is_degraded = is_degraded or child_status.is_degraded
356
357 if ldisk_status is None:
358 ldisk_status = child_status.ldisk_status
359 elif child_status.ldisk_status is not None:
360 ldisk_status = max(ldisk_status, child_status.ldisk_status)
361
362 return objects.BlockDevStatus(dev_path=self.dev_path,
363 major=self.major,
364 minor=self.minor,
365 sync_percent=min_percent,
366 estimated_time=max_time,
367 is_degraded=is_degraded,
368 ldisk_status=ldisk_status)
369
371 """Update metadata with info text.
372
373 Only supported for some device types.
374
375 """
376 for child in self._children:
377 child.SetInfo(text)
378
379 - def Grow(self, amount, dryrun, backingstore, excl_stor):
380 """Grow the block device.
381
382 @type amount: integer
383 @param amount: the amount (in mebibytes) to grow with
384 @type dryrun: boolean
385 @param dryrun: whether to execute the operation in simulation mode
386 only, without actually increasing the size
387 @param backingstore: whether to execute the operation on backing storage
388 only, or on "logical" storage only; e.g. DRBD is logical storage,
389 whereas LVM, file, RBD are backing storage
390 @type excl_stor: boolean
391 @param excl_stor: Whether exclusive_storage is active
392
393 """
394 raise NotImplementedError
395
397 """Return the actual disk size.
398
399 @note: the device needs to be active when this is called
400
401 """
402 assert self.attached, "BlockDevice not attached in GetActualSize()"
403 result = utils.RunCmd(["blockdev", "--getsize64", self.dev_path])
404 if result.failed:
405 ThrowError("blockdev failed (%s): %s",
406 result.fail_reason, result.output)
407 try:
408 sz = int(result.output.strip())
409 except (ValueError, TypeError), err:
410 ThrowError("Failed to parse blockdev output: %s", str(err))
411 return sz
412
414 """Return the actual number of spindles used.
415
416 This is not supported by all devices; if not supported, C{None} is returned.
417
418 @note: the device needs to be active when this is called
419
420 """
421 assert self.attached, "BlockDevice not attached in GetActualSpindles()"
422 return None
423
425 """Return the actual disk size and number of spindles used.
426
427 @rtype: tuple
428 @return: (size, spindles); spindles is C{None} when they are not supported
429
430 @note: the device needs to be active when this is called
431
432 """
433 return (self.GetActualSize(), self.GetActualSpindles())
434
436 """Return URIs hypervisors can use to access disks in userspace mode.
437
438 @rtype: string
439 @return: userspace device URI
440 @raise errors.BlockDeviceError: if userspace access is not supported
441
442 """
443 ThrowError("Userspace access with %s block device and %s hypervisor is not "
444 "supported." % (self.__class__.__name__,
445 hypervisor))
446
448 return ("<%s: unique_id: %s, children: %s, %s:%s, %s>" %
449 (self.__class__, self.unique_id, self._children,
450 self.major, self.minor, self.dev_path))
451
454 """Log an error to the node daemon and the raise an exception.
455
456 @type msg: string
457 @param msg: the text of the exception
458 @raise errors.BlockDeviceError
459
460 """
461 if args:
462 msg = msg % args
463 logging.error(msg)
464 raise errors.BlockDeviceError(msg)
465
468 """Executes the given function, ignoring BlockDeviceErrors.
469
470 This is used in order to simplify the execution of cleanup or
471 rollback functions.
472
473 @rtype: boolean
474 @return: True when fn didn't raise an exception, False otherwise
475
476 """
477 try:
478 fn(*args, **kwargs)
479 return True
480 except errors.BlockDeviceError, err:
481 logging.warning("Caught BlockDeviceError but ignoring: %s", str(err))
482 return False
483