forked from storaged-project/blivet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrfs.py
663 lines (523 loc) · 23.1 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
662
663
# 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
import gi
gi.require_version("BlockDev", "2.0")
from gi.repository import BlockDev as blockdev
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 .. import util
from ..formats import get_format, 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
from contextlib import contextmanager
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 update_sysfs_path(self):
""" Update this device's sysfs path. """
log_method_call(self, self.name, status=self.status)
self.parents[0].update_sysfs_path()
self.sysfs_path = self.parents[0].sysfs_path
log.debug("%s sysfs_path set to %s", self.name, self.sysfs_path)
def update_size(self, newsize=None):
pass
def _post_create(self):
super(BTRFSDevice, self)._post_create()
self.format.exists = True
self.format.device = self.path
def _pre_destroy(self):
""" Preparation and precondition checking for device destruction. """
super(BTRFSDevice, self)._pre_destroy()
self.setup_parents(orig=True)
def _get_size(self):
return sum((d.size for d in self.parents), Size(0))
def _set_size(self, newsize):
raise RuntimeError("cannot directly set size of btrfs volume")
@property
def current_size(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
@contextmanager
def _do_temp_mount(self, orig=False):
if not self.exists:
raise errors.FSError("format doesn't exist")
if orig:
fmt = self.original_format
else:
fmt = self.format
if fmt.status:
yield fmt.system_mountpoint
else:
tmpdir = tempfile.mkdtemp(prefix=self._temp_dir_prefix)
try:
fmt.mount(mountpoint=tmpdir)
except errors.FSError as e:
log.debug("btrfs temp mount failed: %s", e)
raise
try:
yield tmpdir
finally:
util.umount(mountpoint=tmpdir)
os.rmdir(tmpdir)
@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 fstab_spec(self):
if self.format.vol_uuid:
spec = "UUID=%s" % self.format.vol_uuid
else:
spec = super(BTRFSDevice, self).fstab_spec
return spec
def is_name_valid(self, name):
# Override StorageDevice.is_name_valid to allow pretty much anything
return btrfs.is_btrfs_name_valid(name)
class BTRFSVolumeDevice(BTRFSDevice, ContainerDevice, RaidDevice):
_type = "btrfs volume"
vol_id = btrfs.MAIN_VOLUME_ID
_format_class_name = property(lambda s: "btrfs")
_format_uuid_attr = property(lambda s: "vol_uuid")
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 sysfs_path: sysfs device path
:keyword data_level: RAID level for data
:type data_level: any valid raid level descriptor
:keyword metadata_level: RAID level for metadata
:type metadata_level: any valid raid level descriptor
"""
# pop these arguments before the constructor call to avoid
# unrecognized keyword error in superclass constructor
data_level = kwargs.pop("data_level", None)
metadata_level = kwargs.pop("metadata_level", None)
create_options = kwargs.pop("create_options", None)
super(BTRFSVolumeDevice, self).__init__(*args, **kwargs)
# avoid attribute-defined-outside-init pylint warning
self._data_level = self._metadata_level = None
# assign after constructor to avoid AttributeErrors in setter functions
try:
self.data_level = data_level
self.metadata_level = metadata_level
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.remove_child(self)
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 = get_format("btrfs",
exists=self.exists,
label=label,
vol_uuid=self.uuid,
device=self.path,
subvolspec=self.vol_id,
mountopts="subvolid=%d" % self.vol_id,
create_options=create_options)
self.original_format = copy.deepcopy(self.format)
self._default_subvolume_id = None
@property
def members(self):
return list(self.parents)
def _set_level(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._get_level(value, levels)
except ValueError as e:
raise errors.BTRFSValueError(e)
if data:
self._data_level = level
else:
self._metadata_level = level
@property
def data_level(self):
""" Return the RAID level for data.
:returns: raid level
:rtype: an object that represents a raid level
"""
return self._data_level
@data_level.setter
def data_level(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._set_level(value, True)
@property
def metadata_level(self):
""" Return the RAID level for metadata.
:returns: raid level
:rtype: an object that represents a raid level
"""
return self._metadata_level
@metadata_level.setter
def metadata_level(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._set_level(value, False)
@property
def format_immutable(self):
return super(BTRFSVolumeDevice, self).format_immutable or self.exists
def _set_name(self, value):
self._name = value # name is not used outside of blivet
def _set_format(self, fmt):
""" Set the Device's format. """
super(BTRFSVolumeDevice, self)._set_format(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 _get_size(self):
# Calculate the size as if it were a RAID with no superblock and a chunk_size of 1
data_level = self.data_level or raid.Single
return data_level.get_size([d.size for d in self.parents],
chunk_size=Size(1),
superblock_size_func=lambda x: 0)
def _remove_parent(self, parent):
levels = (l for l in (self.data_level, self.metadata_level) if l)
for l in levels:
error_msg = self._validate_parent_removal(l, parent)
if error_msg:
raise errors.DeviceError(error_msg)
super(BTRFSVolumeDevice, self)._remove_parent(parent)
def _add_subvolume(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 _remove_subvolume(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 list_subvolumes(self, snapshots_only=False):
subvols = []
if flags.auto_dev_updates:
self.setup(orig=True)
elif not self.original_format.status:
return subvols
try:
with self._do_temp_mount(orig=True) as mountpoint:
try:
subvols = blockdev.btrfs.list_subvolumes(mountpoint,
snapshots_only=snapshots_only)
except blockdev.BtrfsError as e:
log.debug("failed to list subvolumes: %s", e)
else:
self._get_default_subvolume_id()
except errors.FSError as e:
pass
return subvols
def remove_subvolume(self, name):
raise NotImplementedError()
def _get_default_subvolume_id(self):
subvolid = None
with self._do_temp_mount() as mountpoint:
try:
subvolid = blockdev.btrfs.get_default_subvolume_id(mountpoint)
except blockdev.BtrfsError as e:
log.debug("failed to get default subvolume id: %s", e)
self._default_subvolume_id = subvolid
def _set_default_subvolume_id(self, vol_id):
""" Set a new default subvolume by id.
This writes the change to the filesystem, which must be mounted.
"""
with self._do_temp_mount() as mountpoint:
try:
blockdev.btrfs.set_default_subvolume(mountpoint, vol_id)
except blockdev.BtrfsError 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._default_subvolume_id = vol_id
@property
def default_subvolume(self):
default = None
if self._default_subvolume_id is None:
return None
if self._default_subvolume_id == self.vol_id:
return self
for sv in self.subvolumes:
if sv.vol_id == self._default_subvolume_id:
default = sv
break
return default
def _pre_create(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)._pre_create()
def _create(self):
log_method_call(self, self.name, status=self.status)
if self.data_level:
data_level = str(self.data_level)
else:
data_level = None
if self.metadata_level:
md_level = str(self.metadata_level)
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 _post_create(self):
super(BTRFSVolumeDevice, self)._post_create()
info = udev.get_device(self.sysfs_path)
if not info:
log.error("failed to get updated udev info for new btrfs volume")
else:
self.format.vol_uuid = udev.device_get_uuid(info)
if not self.format.vol_uuid:
try:
bd_info = blockdev.btrfs.filesystem_info(self.parents[0].path)
except blockdev.BtrfsError as e:
log.error("failed to get filesystem info for new btrfs volume %s", e)
else:
self.format.vol_uuid = bd_info.uuid
self.format.exists = True
self.original_format.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)
with self._do_temp_mount() as mountpoint:
blockdev.btrfs.remove_device(mountpoint, member.path)
def _add(self, member):
with self._do_temp_mount() as mountpoint:
blockdev.btrfs.add_device(mountpoint, member.path)
def populate_ksdata(self, data):
super(BTRFSVolumeDevice, self).populate_ksdata(data)
data.data_level = self.data_level.name if self.data_level else None
data.metadata_level = self.metadata_level.name if self.metadata_level else None
data.devices = ["btrfs.%d" % p.id for p in self.parents]
data.preexist = self.exists
class BTRFSSubVolumeDevice(BTRFSDevice):
""" A btrfs subvolume pseudo-device. """
_type = "btrfs subvolume"
_format_immutable = 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 sysfs_path: 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._add_subvolume(self)
def _set_format(self, fmt):
""" Set the Device's format. """
super(BTRFSSubVolumeDevice, self)._set_format(fmt)
if self.exists:
return
# propagate mount options specified for members via kickstart
opts = "subvol=%s" % self.name
has_compress = False
if self.volume.format.mountopts:
for opt in self.volume.format.mountopts.split(","):
# do not add members subvol spec
if not opt.startswith("subvol"):
opts += ",%s" % opt
if opt.startswith("compress"):
has_compress = True
# add default compression settings
if flags.btrfs_compression and not has_compress:
opts += ",compress=%s" % flags.btrfs_compression
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 setup_parents(self, orig=False):
""" Run setup method of all parent devices. """
log_method_call(self, name=self.name, orig=orig)
self.volume.setup(orig=orig)
def _create(self):
log_method_call(self, self.name, status=self.status)
with self.volume._do_temp_mount() as mountpoint:
blockdev.btrfs.create_subvolume(mountpoint, self.name)
def _post_create(self):
super(BTRFSSubVolumeDevice, self)._post_create()
self.format.vol_uuid = self.volume.format.vol_uuid
def _destroy(self):
log_method_call(self, self.name, status=self.status)
with self.volume._do_temp_mount() as mountpoint:
if self.volume._default_subvolume_id == self.vol_id:
# btrfs does not allow removal of the default subvolume
self.volume._set_default_subvolume_id(self.volume.vol_id)
blockdev.btrfs.delete_subvolume(mountpoint, self.name)
def remove_hook(self, modparent=True):
if modparent:
self.volume._remove_subvolume(self.name)
super(BTRFSSubVolumeDevice, self).remove_hook(modparent=modparent)
def add_hook(self, new=True):
super(BTRFSSubVolumeDevice, self).add_hook(new=new)
if new:
return
if self not in self.volume.subvolumes:
self.volume._add_subvolume(self)
def populate_ksdata(self, data):
super(BTRFSSubVolumeDevice, self).populate_ksdata(data)
data.subvol = True
data.name = self.name
data.preexist = self.exists
# Identify the volume this subvolume belongs to by means of its
# label. If the volume has no label, do nothing.
# Note that doing nothing will create an invalid kickstart.
# See rhbz#1072060
label = self.parents[0].format.label
if label:
data.devices = ["LABEL=%s" % label]
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 sysfs_path: sysfs device path
:keyword :class:`~.BTRFSDevice` source: the snapshot source
:keyword bool read_only: 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.read_only = kwargs.pop("read_only", False)
super(BTRFSSnapShotDevice, self).__init__(*args, **kwargs)
if source and getattr(source, "volume", source) != self.volume:
self.volume._remove_subvolume(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)
with self.volume._do_temp_mount() as mountpoint:
if isinstance(self.source, BTRFSVolumeDevice):
source_path = mountpoint
else:
source_path = "%s/%s" % (mountpoint, self.source.name)
dest_path = "%s/%s" % (mountpoint, self.name)
blockdev.btrfs.create_snapshot(source_path, dest_path, ro=self.read_only)
def depends_on(self, dep):
return (dep == self.source or
super(BTRFSSnapShotDevice, self).depends_on(dep))