Statistics
| Branch: | Tag: | Revision:

root / lib / storage / base.py @ f505e3ee

History | View | Annotate | Download (13 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013 Google Inc.
5
#
6
# This program is free software; you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation; either version 2 of the License, or
9
# (at your option) any later version.
10
#
11
# This program is distributed in the hope that it will be useful, but
12
# WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
# General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with this program; if not, write to the Free Software
18
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19
# 02110-1301, USA.
20

    
21

    
22
"""Block device abstraction - base class and utility functions"""
23

    
24
import logging
25

    
26
from ganeti import objects
27
from ganeti import constants
28
from ganeti import utils
29
from ganeti import errors
30

    
31

    
32
class BlockDev(object):
33
  """Block device abstract class.
34

35
  A block device can be in the following states:
36
    - not existing on the system, and by `Create()` it goes into:
37
    - existing but not setup/not active, and by `Assemble()` goes into:
38
    - active read-write and by `Open()` it goes into
39
    - online (=used, or ready for use)
40

41
  A device can also be online but read-only, however we are not using
42
  the readonly state (LV has it, if needed in the future) and we are
43
  usually looking at this like at a stack, so it's easier to
44
  conceptualise the transition from not-existing to online and back
45
  like a linear one.
46

47
  The many different states of the device are due to the fact that we
48
  need to cover many device types:
49
    - logical volumes are created, lvchange -a y $lv, and used
50
    - drbd devices are attached to a local disk/remote peer and made primary
51

52
  A block device is identified by three items:
53
    - the /dev path of the device (dynamic)
54
    - a unique ID of the device (static)
55
    - it's major/minor pair (dynamic)
56

57
  Not all devices implement both the first two as distinct items. LVM
58
  logical volumes have their unique ID (the pair volume group, logical
59
  volume name) in a 1-to-1 relation to the dev path. For DRBD devices,
60
  the /dev path is again dynamic and the unique id is the pair (host1,
61
  dev1), (host2, dev2).
62

63
  You can get to a device in two ways:
64
    - creating the (real) device, which returns you
65
      an attached instance (lvcreate)
66
    - attaching of a python instance to an existing (real) device
67

68
  The second point, the attachment to a device, is different
69
  depending on whether the device is assembled or not. At init() time,
70
  we search for a device with the same unique_id as us. If found,
71
  good. It also means that the device is already assembled. If not,
72
  after assembly we'll have our correct major/minor.
73

74
  """
75
  def __init__(self, unique_id, children, size, params, dyn_params):
76
    self._children = children
77
    self.dev_path = None
78
    self.unique_id = unique_id
79
    self.major = None
80
    self.minor = None
81
    self.attached = False
82
    self.size = size
83
    self.params = params
84
    self.dyn_params = dyn_params
85

    
86
  def Assemble(self):
87
    """Assemble the device from its components.
88

89
    Implementations of this method by child classes must ensure that:
90
      - after the device has been assembled, it knows its major/minor
91
        numbers; this allows other devices (usually parents) to probe
92
        correctly for their children
93
      - calling this method on an existing, in-use device is safe
94
      - if the device is already configured (and in an OK state),
95
        this method is idempotent
96

97
    """
98
    pass
99

    
100
  def Attach(self):
101
    """Find a device which matches our config and attach to it.
102

103
    """
104
    raise NotImplementedError
105

    
106
  def Close(self):
107
    """Notifies that the device will no longer be used for I/O.
108

109
    """
110
    raise NotImplementedError
111

    
112
  @classmethod
113
  def Create(cls, unique_id, children, size, spindles, params, excl_stor,
114
             dyn_params):
115
    """Create the device.
116

117
    If the device cannot be created, it will return None
118
    instead. Error messages go to the logging system.
119

120
    Note that for some devices, the unique_id is used, and for other,
121
    the children. The idea is that these two, taken together, are
122
    enough for both creation and assembly (later).
123

124
    @type unique_id: 2-element tuple or list
125
    @param unique_id: unique identifier; the details depend on the actual device
126
        type
127
    @type children: list of L{BlockDev}
128
    @param children: for hierarchical devices, the child devices
129
    @type size: float
130
    @param size: size in MiB
131
    @type spindles: int
132
    @param spindles: number of physical disk to dedicate to the device
133
    @type params: dict
134
    @param params: device-specific options/parameters
135
    @type excl_stor: bool
136
    @param excl_stor: whether exclusive_storage is active
137
    @type dyn_params: dict
138
    @param dyn_params: dynamic parameters of the disk only valid for this node.
139
        As set by L{objects.Disk.UpdateDynamicDiskParams}.
140
    @rtype: L{BlockDev}
141
    @return: the created device, or C{None} in case of an error
142

143
    """
144
    raise NotImplementedError
145

    
146
  def Remove(self):
147
    """Remove this device.
148

149
    This makes sense only for some of the device types: LV and file
150
    storage. Also note that if the device can't attach, the removal
151
    can't be completed.
152

153
    """
154
    raise NotImplementedError
155

    
156
  def Rename(self, new_id):
157
    """Rename this device.
158

159
    This may or may not make sense for a given device type.
160

161
    """
162
    raise NotImplementedError
163

    
164
  def Open(self, force=False):
165
    """Make the device ready for use.
166

167
    This makes the device ready for I/O. For now, just the DRBD
168
    devices need this.
169

170
    The force parameter signifies that if the device has any kind of
171
    --force thing, it should be used, we know what we are doing.
172

173
    @type force: boolean
174

175
    """
176
    raise NotImplementedError
177

    
178
  def Shutdown(self):
179
    """Shut down the device, freeing its children.
180

181
    This undoes the `Assemble()` work, except for the child
182
    assembling; as such, the children on the device are still
183
    assembled after this call.
184

185
    """
186
    raise NotImplementedError
187

    
188
  def SetSyncParams(self, params):
189
    """Adjust the synchronization parameters of the mirror.
190

191
    In case this is not a mirroring device, this is no-op.
192

193
    @param params: dictionary of LD level disk parameters related to the
194
    synchronization.
195
    @rtype: list
196
    @return: a list of error messages, emitted both by the current node and by
197
    children. An empty list means no errors.
198

199
    """
200
    result = []
201
    if self._children:
202
      for child in self._children:
203
        result.extend(child.SetSyncParams(params))
204
    return result
205

    
206
  def PauseResumeSync(self, pause):
207
    """Pause/Resume the sync of the mirror.
208

209
    In case this is not a mirroring device, this is no-op.
210

211
    @type pause: boolean
212
    @param pause: Whether to pause or resume
213

214
    """
215
    result = True
216
    if self._children:
217
      for child in self._children:
218
        result = result and child.PauseResumeSync(pause)
219
    return result
220

    
221
  def GetSyncStatus(self):
222
    """Returns the sync status of the device.
223

224
    If this device is a mirroring device, this function returns the
225
    status of the mirror.
226

227
    If sync_percent is None, it means the device is not syncing.
228

229
    If estimated_time is None, it means we can't estimate
230
    the time needed, otherwise it's the time left in seconds.
231

232
    If is_degraded is True, it means the device is missing
233
    redundancy. This is usually a sign that something went wrong in
234
    the device setup, if sync_percent is None.
235

236
    The ldisk parameter represents the degradation of the local
237
    data. This is only valid for some devices, the rest will always
238
    return False (not degraded).
239

240
    @rtype: objects.BlockDevStatus
241

242
    """
243
    return objects.BlockDevStatus(dev_path=self.dev_path,
244
                                  major=self.major,
245
                                  minor=self.minor,
246
                                  sync_percent=None,
247
                                  estimated_time=None,
248
                                  is_degraded=False,
249
                                  ldisk_status=constants.LDS_OKAY)
250

    
251
  def CombinedSyncStatus(self):
252
    """Calculate the mirror status recursively for our children.
253

254
    The return value is the same as for `GetSyncStatus()` except the
255
    minimum percent and maximum time are calculated across our
256
    children.
257

258
    @rtype: objects.BlockDevStatus
259

260
    """
261
    status = self.GetSyncStatus()
262

    
263
    min_percent = status.sync_percent
264
    max_time = status.estimated_time
265
    is_degraded = status.is_degraded
266
    ldisk_status = status.ldisk_status
267

    
268
    if self._children:
269
      for child in self._children:
270
        child_status = child.GetSyncStatus()
271

    
272
        if min_percent is None:
273
          min_percent = child_status.sync_percent
274
        elif child_status.sync_percent is not None:
275
          min_percent = min(min_percent, child_status.sync_percent)
276

    
277
        if max_time is None:
278
          max_time = child_status.estimated_time
279
        elif child_status.estimated_time is not None:
280
          max_time = max(max_time, child_status.estimated_time)
281

    
282
        is_degraded = is_degraded or child_status.is_degraded
283

    
284
        if ldisk_status is None:
285
          ldisk_status = child_status.ldisk_status
286
        elif child_status.ldisk_status is not None:
287
          ldisk_status = max(ldisk_status, child_status.ldisk_status)
288

    
289
    return objects.BlockDevStatus(dev_path=self.dev_path,
290
                                  major=self.major,
291
                                  minor=self.minor,
292
                                  sync_percent=min_percent,
293
                                  estimated_time=max_time,
294
                                  is_degraded=is_degraded,
295
                                  ldisk_status=ldisk_status)
296

    
297
  def SetInfo(self, text):
298
    """Update metadata with info text.
299

300
    Only supported for some device types.
301

302
    """
303
    for child in self._children:
304
      child.SetInfo(text)
305

    
306
  def Grow(self, amount, dryrun, backingstore, excl_stor):
307
    """Grow the block device.
308

309
    @type amount: integer
310
    @param amount: the amount (in mebibytes) to grow with
311
    @type dryrun: boolean
312
    @param dryrun: whether to execute the operation in simulation mode
313
        only, without actually increasing the size
314
    @param backingstore: whether to execute the operation on backing storage
315
        only, or on "logical" storage only; e.g. DRBD is logical storage,
316
        whereas LVM, file, RBD are backing storage
317
    @type excl_stor: boolean
318
    @param excl_stor: Whether exclusive_storage is active
319

320
    """
321
    raise NotImplementedError
322

    
323
  def GetActualSize(self):
324
    """Return the actual disk size.
325

326
    @note: the device needs to be active when this is called
327

328
    """
329
    assert self.attached, "BlockDevice not attached in GetActualSize()"
330
    result = utils.RunCmd(["blockdev", "--getsize64", self.dev_path])
331
    if result.failed:
332
      ThrowError("blockdev failed (%s): %s",
333
                  result.fail_reason, result.output)
334
    try:
335
      sz = int(result.output.strip())
336
    except (ValueError, TypeError), err:
337
      ThrowError("Failed to parse blockdev output: %s", str(err))
338
    return sz
339

    
340
  def GetActualSpindles(self):
341
    """Return the actual number of spindles used.
342

343
    This is not supported by all devices; if not supported, C{None} is returned.
344

345
    @note: the device needs to be active when this is called
346

347
    """
348
    assert self.attached, "BlockDevice not attached in GetActualSpindles()"
349
    return None
350

    
351
  def GetActualDimensions(self):
352
    """Return the actual disk size and number of spindles used.
353

354
    @rtype: tuple
355
    @return: (size, spindles); spindles is C{None} when they are not supported
356

357
    @note: the device needs to be active when this is called
358

359
    """
360
    return (self.GetActualSize(), self.GetActualSpindles())
361

    
362
  def GetUserspaceAccessUri(self, hypervisor):
363
    """Return URIs hypervisors can use to access disks in userspace mode.
364

365
    @rtype: string
366
    @return: userspace device URI
367
    @raise errors.BlockDeviceError: if userspace access is not supported
368

369
    """
370
    ThrowError("Userspace access with %s block device and %s hypervisor is not "
371
               "supported." % (self.__class__.__name__,
372
                               hypervisor))
373

    
374
  def __repr__(self):
375
    return ("<%s: unique_id: %s, children: %s, %s:%s, %s>" %
376
            (self.__class__, self.unique_id, self._children,
377
             self.major, self.minor, self.dev_path))
378

    
379

    
380
def ThrowError(msg, *args):
381
  """Log an error to the node daemon and the raise an exception.
382

383
  @type msg: string
384
  @param msg: the text of the exception
385
  @raise errors.BlockDeviceError
386

387
  """
388
  if args:
389
    msg = msg % args
390
  logging.error(msg)
391
  raise errors.BlockDeviceError(msg)
392

    
393

    
394
def IgnoreError(fn, *args, **kwargs):
395
  """Executes the given function, ignoring BlockDeviceErrors.
396

397
  This is used in order to simplify the execution of cleanup or
398
  rollback functions.
399

400
  @rtype: boolean
401
  @return: True when fn didn't raise an exception, False otherwise
402

403
  """
404
  try:
405
    fn(*args, **kwargs)
406
    return True
407
  except errors.BlockDeviceError, err:
408
    logging.warning("Caught BlockDeviceError but ignoring: %s", str(err))
409
    return False