-
Notifications
You must be signed in to change notification settings - Fork 83
/
storage.py
752 lines (614 loc) · 25.8 KB
/
storage.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
# devices/storage.py
# Base class for block device classes.
#
# Copyright (C) 2009-2014 Red Hat, Inc.
#
# This copyrighted material is made available to anyone wishing to use,
# modify, copy, or redistribute it subject to the terms and conditions of
# the GNU General Public License v.2, or (at your option) any later version.
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY expressed or implied, including the implied warranties of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details. You should have received a copy of the
# GNU General Public License along with this program; if not, write to the
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA. Any Red Hat trademarks that are incorporated in the
# source code or documentation are not subject to the GNU General Public
# License and may only be used or replicated with the express permission of
# Red Hat, Inc.
#
# Red Hat Author(s): David Lehman <dlehman@redhat.com>
#
import os
import copy
import parted
import _ped
import pyudev
from .. import errors
from .. import util
from ..flags import flags
from ..storage_log import log_method_call
from .. import udev
from ..formats import getFormat
from ..size import Size
import logging
log = logging.getLogger("blivet")
from .device import Device
from .network import NetworkStorageDevice
class StorageDevice(Device):
""" A generic storage device.
A fully qualified path to the device node can be obtained via the
path attribute, although it is not guaranteed to be useful, or
even present, unless the StorageDevice's setup method has been
run.
"""
_resizable = False
"""Whether this type of device is inherently resizable."""
_type = "blivet"
_devDir = "/dev"
_formatImmutable = False
_partitionable = False
_isDisk = False
_encrypted = False
def __init__(self, name, fmt=None, uuid=None,
size=None, major=None, minor=None,
sysfsPath='', parents=None, exists=False, serial=None,
vendor="", model="", bus=""):
"""
:param name: the device name (generally a device node's basename)
:type name: str
:keyword exists: does this device exist?
:type exists: bool
:keyword size: the device's size
:type size: :class:`~.size.Size`
:keyword parents: a list of parent devices
:type parents: list of :class:`StorageDevice`
:keyword fmt: this device's formatting
:type fmt: :class:`~.formats.DeviceFormat` or a subclass of it
:keyword uuid: universally unique identifier (device -- not fs)
:type uuid: str
:keyword sysfsPath: sysfs device path
:type sysfsPath: str
:keyword major: the device major
:type major: int
:keyword minor: the device minor
:type minor: int
:keyword serial: the ID_SERIAL_SHORT for this device
:type serial: str
:keyword vendor: the manufacturer of this Device
:type vendor: str
:keyword model: manufacturer's device model string
:type model: str
:keyword bus: the interconnect this device uses
:type bus: str
"""
# allow specification of individual parents
if isinstance(parents, Device):
parents = [parents]
self.exists = exists
self.uuid = uuid
# Set these fields before super call as MDRaidArrayDevice._addParent()
# reads them, through calls to status() and partedDevice().
self.sysfsPath = sysfsPath
self._partedDevice = None
super(StorageDevice, self).__init__(name, parents=parents)
self._format = None
self._size = Size(util.numeric_type(size))
self.major = util.numeric_type(major)
self.minor = util.numeric_type(minor)
self._serial = serial
self._vendor = vendor
self._model = model
self.bus = bus
self._readonly = False
self._protected = False
self.controllable = not flags.testing
self.format = fmt
self.originalFormat = copy.copy(self.format)
self.fstabComment = ""
self._targetSize = self._size
self.deviceLinks = []
if self.exists and flags.testing and not self._size:
def read_int_from_sys(path):
return int(open(path).readline().strip())
device_root = "/sys/class/block/%s" % self.name
if os.path.exists("%s/queue" % device_root):
sector_size = read_int_from_sys("%s/queue/logical_block_size"
% device_root)
size = read_int_from_sys("%s/size" % device_root)
self._size = Size(size * sector_size)
def __str__(self):
exist = "existing"
if not self.exists:
exist = "non-existent"
s = "%s %s %s" % (exist, self.size, super(StorageDevice, self).__str__())
if self.format.type:
s += " with %s" % self.format
return s
@property
def packages(self):
packages = super(StorageDevice, self).packages
return packages + [p for p in self.format.packages if p not in packages]
@property
def disks(self):
""" A list of all disks this device depends on, including itself. """
_disks = []
for parent in self.parents:
_disks.extend(d for d in parent.disks if d not in _disks)
if self.isDisk and not self.format.hidden:
_disks.append(self)
return _disks
@property
def encrypted(self):
""" True if this device, or any it requires, is encrypted. """
return self._encrypted or any(p.encrypted for p in self.parents)
def _getPartedDevicePath(self):
return self.path
@property
def partedDevice(self):
devicePath = self._getPartedDevicePath()
if self.exists and self.status and not self._partedDevice:
log.debug("looking up parted Device: %s", devicePath)
# We aren't guaranteed to be able to get a device. In
# particular, built-in USB flash readers show up as devices but
# do not always have any media present, so parted won't be able
# to find a device.
try:
self._partedDevice = parted.Device(path=devicePath)
except (_ped.IOException, _ped.DeviceException):
pass
return self._partedDevice
@property
def raw_device(self):
""" The device itself, or when encrypted, the backing device. """
return self
def _setName(self, value):
"""Set the device's name.
:param value: the new device name
:raises errors.DeviceError: if the device exists
"""
if value == self._name:
return
if self.exists:
raise errors.DeviceError("Cannot rename existing device.")
super(StorageDevice, self)._setName(value)
def alignTargetSize(self, newsize):
""" Return a proposed target size adjusted for device specifics.
:param :class:`~.Size` newsize: the proposed/unaligned target size
:returns: newsize modified to yield an aligned device
:rtype: :class:`~.Size`
"""
return newsize
def _getTargetSize(self):
return self._targetSize
def _setTargetSize(self, newsize):
if not isinstance(newsize, Size):
raise ValueError("new size must of type Size")
if self.maxSize and newsize > self.maxSize:
log.error("requested size %s is larger than maximum %s",
newsize, self.maxSize)
raise ValueError("size is larger than the maximum for this device")
elif self.minSize and newsize < self.minSize:
log.error("requested size %s is smaller than minimum %s",
newsize, self.minSize)
raise ValueError("size is smaller than the minimum for this device")
if self.alignTargetSize(newsize) != newsize:
raise ValueError("new size would violate alignment requirements")
self._targetSize = newsize
targetSize = property(lambda s: s._getTargetSize(),
lambda s, v: s._setTargetSize(v),
doc="Target size of this device")
def __repr__(self):
s = Device.__repr__(self)
s += (" uuid = %(uuid)s size = %(size)s\n"
" format = %(format)s\n"
" major = %(major)s minor = %(minor)s exists = %(exists)s"
" protected = %(protected)s\n"
" sysfs path = %(sysfs)s partedDevice = %(partedDevice)s\n"
" target size = %(targetSize)s path = %(path)s\n"
" format args = %(formatArgs)s originalFormat = %(origFmt)s" %
{"uuid": self.uuid, "format": self.format, "size": self.size,
"major": self.major, "minor": self.minor, "exists": self.exists,
"sysfs": self.sysfsPath, "partedDevice": self.partedDevice,
"targetSize": self.targetSize, "path": self.path,
"protected": self.protected,
"formatArgs": self.formatArgs, "origFmt": self.originalFormat.type})
return s
@property
def dict(self):
d = super(StorageDevice, self).dict
d.update({"uuid": self.uuid, "size": self.size,
"format": self.format.dict, "removable": self.removable,
"major": self.major, "minor": self.minor,
"exists": self.exists, "sysfs": self.sysfsPath,
"targetSize": self.targetSize, "path": self.path})
return d
@property
def path(self):
""" Device node representing this device. """
return "%s/%s" % (self._devDir, self.name)
def updateSysfsPath(self):
""" Update this device's sysfs path. """
# We're using os.path.exists as a stand-in for status. We can't use
# the status property directly because MDRaidArrayDevice.status calls
# this method.
log_method_call(self, self.name, status=os.path.exists(self.path))
if not self.exists:
raise errors.DeviceError("device has not been created", self.name)
try:
udev_device = pyudev.Device.from_device_file(udev.global_udev,
self.path)
# from_device_file() does not process exceptions but just propagates
# ValueErrors and EnvironmentErrors that may be raised. So catch
# those errors, but also catch DeviceNotFoundError, which is the
# appropriate exception and which it may raise in future.
except (pyudev.DeviceNotFoundError, EnvironmentError, ValueError) as e:
log.error("failed to update sysfs path for %s: %s", self.name, e)
self.sysfsPath = ''
else:
self.sysfsPath = udev_device.sys_path
log.debug("%s sysfsPath set to %s", self.name, self.sysfsPath)
@property
def formatArgs(self):
""" Device-specific arguments to format creation program. """
return []
@property
def resizable(self):
""" Can this device be resized? """
return (self._resizable and self.exists and
(self.format.type is None or self.format.resizable or
not self.format.exists))
def notifyKernel(self):
""" Send a 'change' uevent to the kernel for this device. """
log_method_call(self, self.name, status=self.status)
if not self.exists:
log.debug("not sending change uevent for non-existent device")
return
if not self.status:
log.debug("not sending change uevent for inactive device")
return
path = os.path.normpath(self.sysfsPath)
try:
util.notify_kernel(path, action="change")
except (ValueError, IOError) as e:
log.warning("failed to notify kernel of change: %s", e)
@property
def fstabSpec(self):
spec = self.path
if self.format and self.format.uuid:
spec = "UUID=%s" % self.format.uuid
return spec
def resize(self):
""" Resize a device to self.targetSize.
This method should only be invoked via the
ActionResizeDevice.execute method. All the pre-conditions
enforced by ActionResizeDevice.__init__ are assumed to hold.
Returns nothing.
"""
if self._resizable:
raise NotImplementedError("method not implemented for device type %s" % self.type)
else:
raise errors.DeviceError("device type %s is not resizable" % self.type)
@property
def readonly(self):
# A device is read-only if it or any parent device is read-only
return self._readonly or any(p.readonly for p in self.parents)
@readonly.setter
def readonly(self, value):
self._readonly = value
@property
def protected(self):
return self.readonly or self._protected
@protected.setter
def protected(self, value):
self._protected = value
#
# setup
#
def _preSetup(self, orig=False):
""" Preparation and pre-condition checking for device setup.
Return True if setup should proceed or False if not.
"""
if not self.exists:
raise errors.DeviceError("device has not been created", self.name)
if self.status or not self.controllable:
return False
self.setupParents(orig=orig)
return True
def _setup(self, orig=False):
""" Perform device-specific setup operations. """
pass
def setup(self, orig=False):
""" Open, or set up, a device. """
log_method_call(self, self.name, orig=orig, status=self.status,
controllable=self.controllable)
if not self._preSetup(orig=orig):
return
self._setup(orig=orig)
self._postSetup()
def _postSetup(self):
""" Perform post-setup operations. """
udev.settle()
# we always probe since the device may not be set up when we want
# information about it
self._size = self.currentSize
#
# teardown
#
def _preTeardown(self, recursive=None):
""" Preparation and pre-condition checking for device teardown.
Return True if teardown should proceed or False if not.
"""
if not self.exists and not recursive:
raise errors.DeviceError("device has not been created", self.name)
if not self.status or not self.controllable:
return False
if self.originalFormat.exists:
self.originalFormat.teardown()
if self.format.exists:
self.format.teardown()
udev.settle()
return True
def _teardown(self, recursive=None):
""" Perform device-specific teardown operations. """
pass
def teardown(self, recursive=None):
""" Close, or tear down, a device. """
log_method_call(self, self.name, status=self.status,
controllable=self.controllable)
if not self._preTeardown(recursive=recursive):
return
self._teardown(recursive=recursive)
self._postTeardown(recursive=recursive)
def _postTeardown(self, recursive=None):
""" Perform post-teardown operations. """
if recursive:
self.teardownParents(recursive=recursive)
#
# create
#
def _preCreate(self):
""" Preparation and pre-condition checking for device creation. """
if self.exists:
raise errors.DeviceError("device has already been created", self.name)
self.setupParents()
def _create(self):
""" Perform device-specific create operations. """
pass
def create(self):
""" Create the device. """
log_method_call(self, self.name, status=self.status)
self._preCreate()
try:
self._create()
except Exception as e:
raise errors.DeviceCreateError(str(e), self.name)
else:
self._postCreate()
def _postCreate(self):
""" Perform post-create operations. """
self.exists = True
self.setup()
self.updateSysfsPath()
udev.settle()
# make sure that targetSize is updated to reflect the actual size
if self.resizable:
self._partedDevice = None
self._targetSize = self.currentSize
self._updateNetDevMountOption()
#
# destroy
#
def _preDestroy(self):
""" Preparation and precondition checking for device destruction. """
if not self.exists:
raise errors.DeviceError("device has not been created", self.name)
if not self.isleaf:
raise errors.DeviceError("Cannot destroy non-leaf device", self.name)
self.teardown()
def _destroy(self):
""" Perform device-specific destruction operations. """
pass
def destroy(self):
""" Destroy the device. """
log_method_call(self, self.name, status=self.status)
self._preDestroy()
self._destroy()
self._postDestroy()
def _postDestroy(self):
""" Perform post-destruction operations. """
self.exists = False
def setupParents(self, orig=False):
""" Run setup method of all parent devices. """
log_method_call(self, name=self.name, orig=orig, kids=self.kids)
for parent in self.parents:
parent.setup(orig=orig)
if orig:
_format = parent.originalFormat
else:
_format = parent.format
# set up the formatting, if present
if _format.type and _format.exists:
_format.setup()
def _getSize(self):
""" Get the device's size, accounting for pending changes. """
if self.exists and not self.mediaPresent:
return Size(0)
if self.exists and self.partedDevice:
self._size = self.currentSize
size = self._size
if self.exists and self.resizable:
size = self.targetSize
return size
def _setSize(self, newsize):
""" Set the device's size to a new value. """
if not isinstance(newsize, Size):
raise ValueError("new size must of type Size")
if self.maxSize and newsize > self.maxSize:
raise errors.DeviceError("device cannot be larger than %s" %
(self.maxSize,), self.name)
self._size = newsize
size = property(lambda x: x._getSize(),
lambda x, y: x._setSize(y),
doc="The device's size, accounting for pending changes")
@property
def currentSize(self):
""" The device's actual size, generally the size discovered by using
system tools. May use a cached value if the information is
currently unavailable.
If the device does not exist, then the actual size is 0.
"""
size = Size(0)
if self.exists and self.partedDevice:
size = Size(self.partedDevice.getLength(unit="B"))
elif self.exists:
size = self._size
return size
@property
def minSize(self):
""" The minimum size this device can be. """
return self.alignTargetSize(self.format.minSize) if self.resizable else self.currentSize
@property
def maxSize(self):
""" The maximum size this device can be. """
return self.alignTargetSize(self.format.maxSize) if self.resizable else self.currentSize
@property
def status(self):
""" This device's status.
For now, this should return a boolean:
True the device is open and ready for use
False the device is not open
"""
if not self.exists:
return False
return os.access(self.path, os.W_OK)
def _setFormat(self, fmt):
""" Set the Device's format. """
if not fmt:
fmt = getFormat(None, device=self.path, exists=self.exists)
log_method_call(self, self.name, type=fmt.type,
current=getattr(self._format, "type", None))
if self._format and self._format.status:
# FIXME: self.format.status doesn't mean much
raise errors.DeviceError("cannot replace active format", self.name)
self._format = fmt
self._format.device = self.path
self._updateNetDevMountOption()
def _updateNetDevMountOption(self):
""" Fix mount options to include or exclude _netdev as appropriate. """
if not hasattr(self._format, "mountpoint"):
return
netdev_option = "_netdev"
option_list = self._format.options.split(",")
is_netdev = any(isinstance(a, NetworkStorageDevice)
for a in self.ancestors)
has_netdev_option = netdev_option in option_list
if not is_netdev and has_netdev_option:
option_list.remove(netdev_option)
self._format.options = ",".join(option_list)
elif is_netdev and not has_netdev_option:
option_list.append(netdev_option)
self._format.options = ",".join(option_list)
def _getFormat(self):
return self._format
format = property(lambda d: d._getFormat(),
lambda d,f: d._setFormat(f),
doc="The device's formatting.")
def preCommitFixup(self, *args, **kwargs):
""" Do any necessary pre-commit fixups."""
pass
@property
def removable(self):
devpath = os.path.normpath(self.sysfsPath)
remfile = os.path.normpath("%s/removable" % devpath)
return (self.sysfsPath and os.path.exists(devpath) and
os.access(remfile, os.R_OK) and
open(remfile).readline().strip() == "1")
@property
def formatImmutable(self):
""" Is it possible to execute format actions on this device? """
return self._formatImmutable or self.protected
@property
def direct(self):
""" Is this device directly accessible? """
return self.isleaf
@property
def isDisk(self):
return self._isDisk
@property
def partitionable(self):
return self._partitionable
@property
def partitioned(self):
return self.format.type == "disklabel" and self.partitionable
@property
def serial(self):
return self._serial
@property
def model(self):
if not self._model:
self._model = getattr(self.partedDevice, "model", "")
return self._model
@property
def vendor(self):
return self._vendor
@property
def growable(self):
""" True if this device or its component devices are growable. """
return getattr(self, "req_grow", False) or any(p.growable for p in self.parents)
def checkSize(self):
""" Check to make sure the size of the device is allowed by the
format used.
Returns:
0 - ok
1 - Too large
-1 - Too small
"""
if self.format.maxSize and self.size > self.format.maxSize:
return 1
elif self.format.minSize and self.size < self.format.minSize:
return -1
return 0
# pylint: disable=unused-argument
def removeHook(self, modparent=True):
""" Perform actions related to removing a device from the devicetree.
:keyword bool modparent: whether to account for removal in parents
Parent child counts are adjusted regardless of modparent's value.
The intended use of modparent is to prevent doing things like
removing a parted.Partition from the disk that contains it as part
of msdos extended partition management. In general, you should not
override the default value of modparent in new code.
"""
for parent in self.parents:
parent.removeChild()
def addHook(self, new=True):
""" Perform actions related to adding a device to the devicetree.
:keyword bool new: whether this device is new to the devicetree
The only intended use case for new=False is when unhiding a device
from the devicetree. Additional operations are performed when new is
False that are normally performed as part of the device constructor.
"""
if not new:
map(lambda p: p.addChild(), self.parents)
def populateKSData(self, data):
# the common pieces are basically the formatting
self.format.populateKSData(data)
# this is a little bit of a hack for container member devices that
# need aliases, but even more of a hack for btrfs since you cannot tell
# from inside the BTRFS class whether you're dealing with a member or a
# volume/subvolume
if self.format.type == "btrfs" and not self.type.startswith("btrfs"):
data.mountpoint = "btrfs." # continued below, also for lvm, raid
if data.mountpoint.endswith("."):
data.mountpoint += str(self.id)
@classmethod
def isNameValid(cls, name):
# This device corresponds to a file in /dev, so no /'s or nulls,
# and the name cannot be . or ..
# ...except some names *do* contain directory components, for this
# is an imperfect world of joy and sorrow mingled. For cciss, split
# the path into its components and do the real check on each piece
if name.startswith("cciss/"):
return all(cls.isNameValid(n) for n in name.split('/'))
badchars = any(c in ('\x00', '/') for c in name)
return not(badchars or name == '.' or name == '..')