-
Notifications
You must be signed in to change notification settings - Fork 84
/
btrfs.py
661 lines (531 loc) · 22.7 KB
/
btrfs.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
# devices/btrfs.py
#
# 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 tempfile
from gi.repository import BlockDev as blockdev
from gi.repository import GLib
from ..devicelibs import btrfs
from ..devicelibs import raid
from .. import errors
from ..flags import flags
from ..storage_log import log_method_call
from .. import udev
from ..formats import getFormat, DeviceFormat
from ..size import Size
import logging
log = logging.getLogger("blivet")
from .storage import StorageDevice
from .container import ContainerDevice
from .raid import RaidDevice
from ..tasks import availability
class BTRFSDevice(StorageDevice):
""" Base class for BTRFS volume and sub-volume devices. """
_type = "btrfs"
_packages = ["btrfs-progs"]
_external_dependencies = [availability.BLOCKDEV_BTRFS_PLUGIN]
def __init__(self, *args, **kwargs):
""" Passing None or no name means auto-generate one like btrfs.%d """
if not args or not args[0]:
args = ("btrfs.%d" % self.id,)
if kwargs.get("parents") is None:
raise errors.BTRFSValueError("BTRFSDevice must have at least one parent")
self.req_size = kwargs.pop("size", None)
super(BTRFSDevice, self).__init__(*args, **kwargs)
def updateSysfsPath(self):
""" Update this device's sysfs path. """
log_method_call(self, self.name, status=self.status)
self.parents[0].updateSysfsPath()
self.sysfsPath = self.parents[0].sysfsPath
log.debug("%s sysfsPath set to %s", self.name, self.sysfsPath)
def updateSize(self):
pass
def _postCreate(self):
super(BTRFSDevice, self)._postCreate()
self.format.exists = True
self.format.device = self.path
def _preDestroy(self):
""" Preparation and precondition checking for device destruction. """
super(BTRFSDevice, self)._preDestroy()
self.setupParents(orig=True)
def _getSize(self):
return sum((d.size for d in self.parents), Size(0))
def _setSize(self, newsize):
raise RuntimeError("cannot directly set size of btrfs volume")
@property
def currentSize(self):
return self.size
@property
def status(self):
return self.exists and all(d.status for d in self.parents)
@property
def _temp_dir_prefix(self):
return "btrfs-tmp.%s" % self.id
def _do_temp_mount(self, orig=False):
if not self.exists:
return
if orig:
fmt = self.originalFormat
else:
fmt = self.format
if fmt.status:
return
tmpdir = tempfile.mkdtemp(prefix=self._temp_dir_prefix)
fmt.mount(mountpoint=tmpdir)
def _undo_temp_mount(self):
if getattr(self.format, "systemMountpoint", None):
fmt = self.format
elif getattr(self.originalFormat, "systemMountpoint", None):
fmt = self.originalFormat
else:
return
mountpoint = fmt.systemMountpoint
if os.path.basename(mountpoint).startswith(self._temp_dir_prefix):
fmt.unmount()
os.rmdir(mountpoint)
@property
def path(self):
return self.parents[0].path if self.parents else None
@property
def direct(self):
""" Is this device directly accessible? """
return True
@property
def fstabSpec(self):
if self.format.volUUID:
spec = "UUID=%s" % self.format.volUUID
else:
spec = super(BTRFSDevice, self).fstabSpec
return spec
@classmethod
def isNameValid(cls, name):
# Override StorageDevice.isNameValid to allow pretty much anything
return not('\x00' in name)
class BTRFSVolumeDevice(BTRFSDevice, ContainerDevice, RaidDevice):
_type = "btrfs volume"
vol_id = btrfs.MAIN_VOLUME_ID
_formatClassName = property(lambda s: "btrfs")
_formatUUIDAttr = property(lambda s: "volUUID")
def __init__(self, *args, **kwargs):
"""
:param str name: the volume name
:keyword bool exists: does this device exist?
:keyword :class:`~.size.Size` size: the device's size
:keyword :class:`~.ParentList` parents: a list of parent devices
:keyword fmt: this device's formatting
:type fmt: :class:`~.formats.DeviceFormat`
:keyword str uuid: UUID of top-level filesystem/volume
:keyword str sysfsPath: sysfs device path
:keyword dataLevel: RAID level for data
:type dataLevel: any valid raid level descriptor
:keyword metaDataLevel: RAID level for metadata
:type metaDataLevel: any valid raid level descriptor
"""
# pop these arguments before the constructor call to avoid
# unrecognized keyword error in superclass constructor
dataLevel = kwargs.pop("dataLevel", None)
metaDataLevel = kwargs.pop("metaDataLevel", None)
super(BTRFSVolumeDevice, self).__init__(*args, **kwargs)
# avoid attribute-defined-outside-init pylint warning
self._dataLevel = self._metaDataLevel = None
# assign after constructor to avoid AttributeErrors in setter functions
try:
self.dataLevel = dataLevel
self.metaDataLevel = metaDataLevel
except errors.BTRFSValueError as e:
# Could not set the levels, so set loose the parents that were
# added in superclass constructor.
for dev in self.parents:
dev.removeChild()
raise e
self.subvolumes = []
self.size_policy = self.size
if self.parents and not self.format.type:
label = getattr(self.parents[0].format, "label", None)
self.format = getFormat("btrfs",
exists=self.exists,
label=label,
volUUID=self.uuid,
device=self.path,
subvolspec=self.vol_id,
mountopts="subvolid=%d" % self.vol_id)
self.originalFormat = copy.copy(self.format)
self._defaultSubVolumeID = None
def _setLevel(self, value, data):
""" Sets a valid level for this device and level type.
:param object value: value for this RAID level
:param bool data: True if for data, False if for metadata
:returns: a valid level for value, if any, else None
:rtype: :class:`~.devicelibs.raid.RAIDLevel` or NoneType
:raises :class:`~.errors.BTRFSValueError`: if value represents
an invalid level.
"""
level = None
if value:
levels = btrfs.RAID_levels if data else btrfs.metadata_levels
try:
level = self._getLevel(value, levels)
except ValueError as e:
raise errors.BTRFSValueError(e)
if data:
self._dataLevel = level
else:
self._metaDataLevel = level
@property
def dataLevel(self):
""" Return the RAID level for data.
:returns: raid level
:rtype: an object that represents a raid level
"""
return self._dataLevel
@dataLevel.setter
def dataLevel(self, value):
""" Set the RAID level for data.
:param object value: new raid level
:returns: None
:raises :class:`~.errors.BTRFSValueError`: if value represents
an invalid level.
"""
self._setLevel(value, True)
@property
def metaDataLevel(self):
""" Return the RAID level for metadata.
:returns: raid level
:rtype: an object that represents a raid level
"""
return self._metaDataLevel
@metaDataLevel.setter
def metaDataLevel(self, value):
""" Set the RAID level for metadata.
:param object value: new raid level
:returns: None
:raises :class:`~.errors.BTRFSValueError`: if value represents
an invalid level.
"""
self._setLevel(value, False)
@property
def formatImmutable(self):
return super(BTRFSVolumeDevice, self).formatImmutable or self.exists
def _setName(self, value):
self._name = value # name is not used outside of blivet
def _setFormat(self, fmt):
""" Set the Device's format. """
super(BTRFSVolumeDevice, self)._setFormat(fmt)
self.name = "btrfs.%d" % self.id
label = getattr(self.format, "label", None)
if label:
self.name = label
if not self.exists:
# propagate mount options specified for members via kickstart
self.format.mountopts = self.parents[0].format.mountopts
def _getSize(self):
size = sum((d.size for d in self.parents), Size(0))
if self.dataLevel in (raid.RAID1, raid.RAID10):
size /= len(self.parents)
return size
def _removeParent(self, member):
levels = (l for l in (self.dataLevel, self.metaDataLevel) if l)
for l in levels:
error_msg = self._validateParentRemoval(l, member)
if error_msg:
raise errors.DeviceError(error_msg)
super(BTRFSVolumeDevice, self)._removeParent(member)
def _addSubVolume(self, vol):
if vol.name in [v.name for v in self.subvolumes]:
raise errors.BTRFSValueError("subvolume %s already exists" % vol.name)
self.subvolumes.append(vol)
def _removeSubVolume(self, name):
if name not in [v.name for v in self.subvolumes]:
raise errors.BTRFSValueError("cannot remove non-existent subvolume %s" % name)
names = [v.name for v in self.subvolumes]
self.subvolumes.pop(names.index(name))
def listSubVolumes(self, snapshotsOnly=False):
subvols = []
if flags.installer_mode:
self.setup(orig=True)
try:
self._do_temp_mount(orig=True)
except errors.FSError as e:
log.debug("btrfs temp mount failed: %s", e)
return subvols
elif not self.originalFormat.status:
return subvols
try:
subvols = blockdev.btrfs_list_subvolumes(self.originalFormat.systemMountpoint,
snapshots_only=snapshotsOnly)
except GLib.GError as e:
log.debug("failed to list subvolumes: %s", e)
else:
self._getDefaultSubVolumeID()
finally:
if flags.installer_mode:
self._undo_temp_mount()
return subvols
def createSubVolumes(self):
self._do_temp_mount()
for _name, subvol in self.subvolumes:
if subvol.exists:
continue
subvol.create(mountpoint=self._temp_dir_prefix)
self._undo_temp_mount()
def removeSubVolume(self, name):
raise NotImplementedError()
def _getDefaultSubVolumeID(self):
subvolid = None
try:
subvolid = blockdev.btrfs_get_default_subvolume_id(self.originalFormat.systemMountpoint)
except GLib.GError as e:
log.debug("failed to get default subvolume id: %s", e)
self._defaultSubVolumeID = subvolid
def _setDefaultSubVolumeID(self, vol_id):
""" Set a new default subvolume by id.
This writes the change to the filesystem, which must be mounted.
"""
try:
blockdev.btrfs_set_default_subvolume(self.originalFormat.systemMountpoint, vol_id)
except GLib.GError as e:
log.error("failed to set new default subvolume id (%s): %s",
vol_id, e)
# The only time we set a new default subvolume is so we can remove
# the current default. If we can't change the default, we won't be
# able to remove the subvolume.
raise
else:
self._defaultSubVolumeID = vol_id
@property
def defaultSubVolume(self):
default = None
if self._defaultSubVolumeID is None:
return None
if self._defaultSubVolumeID == self.vol_id:
return self
for sv in self.subvolumes:
if sv.vol_id == self._defaultSubVolumeID:
default = sv
break
return default
def _preCreate(self):
if any(p.size < btrfs.MIN_MEMBER_SIZE for p in self.parents):
raise errors.DeviceCreateError("All BTRFS member devices must have size at least %s." % btrfs.MIN_MEMBER_SIZE)
super(BTRFSVolumeDevice, self)._preCreate()
def _create(self):
log_method_call(self, self.name, status=self.status)
if self.dataLevel:
data_level = str(self.dataLevel)
else:
data_level = None
if self.metaDataLevel:
md_level = str(self.metaDataLevel)
else:
md_level = None
blockdev.btrfs_create_volume([d.path for d in self.parents],
label=self.format.label,
data_level=data_level,
md_level=md_level)
def _postCreate(self):
super(BTRFSVolumeDevice, self)._postCreate()
info = udev.get_device(self.sysfsPath)
if not info:
log.error("failed to get updated udev info for new btrfs volume")
else:
self.format.volUUID = udev.device_get_uuid(info)
self.format.exists = True
self.originalFormat.exists = True
def _destroy(self):
log_method_call(self, self.name, status=self.status)
for device in self.parents:
device.setup(orig=True)
DeviceFormat(device=device.path, exists=True).destroy()
def _remove(self, member):
log_method_call(self, self.name, status=self.status)
try:
self._do_temp_mount(orig=True)
except errors.FSError as e:
log.debug("btrfs temp mount failed: %s", e)
raise
try:
blockdev.btrfs_remove_device(self.originalFormat.systemMountpoint, member.path)
finally:
self._undo_temp_mount()
def _add(self, member):
try:
self._do_temp_mount(orig=True)
except errors.FSError as e:
log.debug("btrfs temp mount failed: %s", e)
raise
try:
blockdev.btrfs_add_device(self.originalFormat.systemMountpoint, member.path)
finally:
self._undo_temp_mount()
def populateKSData(self, data):
super(BTRFSVolumeDevice, self).populateKSData(data)
data.dataLevel = self.dataLevel.name if self.dataLevel else None
data.metaDataLevel = self.metaDataLevel.name if self.metaDataLevel else None
data.devices = ["btrfs.%d" % p.id for p in self.parents]
data.preexist = self.exists
class BTRFSSubVolumeDevice(BTRFSDevice, RaidDevice):
""" A btrfs subvolume pseudo-device. """
_type = "btrfs subvolume"
_formatImmutable = True
def __init__(self, *args, **kwargs):
"""
:param str name: the subvolume name
:keyword bool exists: does this device exist?
:keyword :class:`~.size.Size` size: the device's size
:keyword :class:`~.ParentList` parents: a list of parent devices
:keyword fmt: this device's formatting
:type fmt: :class:`~.formats.DeviceFormat`
:keyword str sysfsPath: sysfs device path
"""
self.vol_id = kwargs.pop("vol_id", None)
super(BTRFSSubVolumeDevice, self).__init__(*args, **kwargs)
if len(self.parents) != 1:
raise errors.BTRFSValueError("%s must have exactly one parent." % self.type)
if not isinstance(self.parents[0], BTRFSDevice):
raise errors.BTRFSValueError("%s unique parent must be a BTRFSDevice." % self.type)
self.volume._addSubVolume(self)
def _setFormat(self, fmt):
""" Set the Device's format. """
super(BTRFSSubVolumeDevice, self)._setFormat(fmt)
if self.exists:
return
# propagate mount options specified for members via kickstart
opts = "subvol=%s" % self.name
if self.volume.format.mountopts:
opts = "%s,%s" % (self.volume.format.mountopts, opts)
self.format.mountopts = opts
@property
def volume(self):
"""Return the first ancestor that is not a BTRFSSubVolumeDevice.
Note: Assumes that each ancestor in traversal has only one parent.
Raises a DeviceError if the ancestor found is not a
BTRFSVolumeDevice.
"""
parent = self.parents[0]
vol = None
while True:
if not isinstance(parent, BTRFSSubVolumeDevice):
vol = parent
break
parent = parent.parents[0]
if not isinstance(vol, BTRFSVolumeDevice):
raise errors.DeviceError("%s %s's first non subvolume ancestor must be a btrfs volume" % (self.type, self.name))
return vol
@property
def container(self):
return self.volume
def setupParents(self, orig=False):
""" Run setup method of all parent devices. """
log_method_call(self, name=self.name, orig=orig, kids=self.kids)
self.volume.setup(orig=orig)
def _create(self):
log_method_call(self, self.name, status=self.status)
self.volume._do_temp_mount()
mountpoint = self.volume.format.systemMountpoint
if not mountpoint:
raise RuntimeError("btrfs subvol create requires mounted volume")
try:
blockdev.btrfs_create_subvolume(mountpoint, self.name)
finally:
self.volume._undo_temp_mount()
def _postCreate(self):
super(BTRFSSubVolumeDevice, self)._postCreate()
self.format.volUUID = self.volume.format.volUUID
def _destroy(self):
log_method_call(self, self.name, status=self.status)
self.volume._do_temp_mount(orig=True)
if self.volume._defaultSubVolumeID == self.vol_id:
# btrfs does not allow removal of the default subvolume
self.volume._setDefaultSubVolumeID(self.volume.vol_id)
mountpoint = self.volume.originalFormat.systemMountpoint
if not mountpoint:
raise RuntimeError("btrfs subvol destroy requires mounted volume")
blockdev.btrfs_delete_subvolume(mountpoint, self.name)
self.volume._undo_temp_mount()
def removeHook(self, modparent=True):
if modparent:
self.volume._removeSubVolume(self.name)
super(BTRFSSubVolumeDevice, self).removeHook(modparent=modparent)
def addHook(self, new=True):
super(BTRFSSubVolumeDevice, self).addHook(new=new)
if new:
return
if self not in self.volume.subvolumes:
self.volume._addSubVolume(self)
def populateKSData(self, data):
super(BTRFSSubVolumeDevice, self).populateKSData(data)
data.subvol = True
data.name = self.name
data.preexist = self.exists
class BTRFSSnapShotDevice(BTRFSSubVolumeDevice):
""" A btrfs snapshot pseudo-device.
BTRFS snapshots are a specialized type of subvolume that contains a
source attribute which identifies which subvolume the snapshot was taken
from. They do not have to be removed when removing the source subvolume.
"""
_type = "btrfs snapshot"
def __init__(self, *args, **kwargs):
"""
:param str name: the subvolume name
:keyword bool exists: does this device exist?
:keyword :class:`~.size.Size` size: the device's size
:keyword :class:`~.ParentList` parents: a list of parent devices
:keyword fmt: this device's formatting
:type fmt: :class:`~.formats.DeviceFormat`
:keyword str sysfsPath: sysfs device path
:keyword :class:`~.BTRFSDevice` source: the snapshot source
:keyword bool readOnly: create a read-only snapshot
Snapshot source can be either a subvolume or a top-level volume.
"""
source = kwargs.pop("source", None)
if not kwargs.get("exists") and not source:
# it is possible to remove a source subvol and keep snapshots of it
raise errors.BTRFSValueError("non-existent btrfs snapshots must have a source")
if source and not isinstance(source, BTRFSDevice):
raise errors.BTRFSValueError("btrfs snapshot source must be a btrfs subvolume")
if source and not source.exists:
raise errors.BTRFSValueError("btrfs snapshot source must already exist")
self.source = source
""" the snapshot's source subvolume """
self.readOnly = kwargs.pop("readOnly", False)
super(BTRFSSnapShotDevice, self).__init__(*args, **kwargs)
if source and getattr(source, "volume", source) != self.volume:
self.volume._removeSubVolume(self.name)
self.parents = []
raise errors.BTRFSValueError("btrfs snapshot and source must be in the same volume")
def _create(self):
log_method_call(self, self.name, status=self.status)
self.volume._do_temp_mount()
mountpoint = self.volume.format.systemMountpoint
if not mountpoint:
raise RuntimeError("btrfs subvol create requires mounted volume")
if isinstance(self.source, BTRFSVolumeDevice):
source_path = mountpoint
else:
source_path = "%s/%s" % (mountpoint, self.source.name)
dest_path = "%s/%s" % (mountpoint, self.name)
try:
blockdev.btrfs_create_snapshot(source_path, dest_path, ro=self.readOnly)
finally:
self.volume._undo_temp_mount()
def dependsOn(self, dep):
return (dep == self.source or
super(BTRFSSnapShotDevice, self).dependsOn(dep))