-
Notifications
You must be signed in to change notification settings - Fork 84
/
fs.py
1312 lines (1048 loc) · 43.6 KB
/
fs.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
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# filesystems.py
# Filesystem classes for anaconda's storage configuration module.
#
# Copyright (C) 2009 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): Dave Lehman <dlehman@redhat.com>
# David Cantrell <dcantrell@redhat.com>
#
""" Filesystem classes. """
from decimal import Decimal
import os
import tempfile
from ..tasks import fsck
from ..tasks import fsinfo
from ..tasks import fslabeling
from ..tasks import fsminsize
from ..tasks import fsmkfs
from ..tasks import fsmount
from ..tasks import fsreadlabel
from ..tasks import fsresize
from ..tasks import fssize
from ..tasks import fssync
from ..tasks import fswritelabel
from ..errors import FormatCreateError, FSError, FSReadLabelError
from ..errors import FSWriteLabelError, FSResizeError
from . import DeviceFormat, register_device_format
from .. import util
from .. import platform
from ..flags import flags
from parted import fileSystemType
from ..storage_log import log_exception_info, log_method_call
from .. import arch
from ..size import Size, ROUND_UP, ROUND_DOWN, unitStr
from ..i18n import N_
from .. import udev
from ..mounts import mountsCache
from .fslib import kernel_filesystems, update_kernel_filesystems
import logging
log = logging.getLogger("blivet")
class FS(DeviceFormat):
""" Filesystem base class. """
_type = "Abstract Filesystem Class" # fs type name
_name = None
_modules = [] # kernel modules required for support
_labelfs = None # labeling functionality
_fsckClass = fsck.UnimplementedFSCK
_infoClass = fsinfo.UnimplementedFSInfo
_minsizeClass = fsminsize.UnimplementedFSMinSize
_mkfsClass = fsmkfs.UnimplementedFSMkfs
_mountClass = fsmount.FSMount
_readlabelClass = fsreadlabel.UnimplementedFSReadLabel
_resizeClass = fsresize.UnimplementedFSResize
_sizeinfoClass = fssize.UnimplementedFSSize
_syncClass = fssync.UnimplementedFSSync
_writelabelClass = fswritelabel.UnimplementedFSWriteLabel
def __init__(self, **kwargs):
"""
:keyword device: path to the block device node (required for
existing filesystems)
:keyword mountpoint: the filesystem's planned mountpoint
:keyword label: the filesystem label
:keyword uuid: the filesystem UUID
:keyword mountopts: mount options for the filesystem
:type mountopts: str
:keyword size: the filesystem's size in MiB
:keyword exists: indicates whether this is an existing filesystem
:type exists: bool
.. note::
The 'device' kwarg is required for existing formats. For non-
existent formats, it is only necessary that the :attr:`device`
attribute be set before the :meth:`create` method runs. Note
that you can specify the device at the last moment by specifying
it via the 'device' kwarg to the :meth:`create` method.
"""
if self.__class__ is FS:
raise TypeError("FS is an abstract class.")
DeviceFormat.__init__(self, **kwargs)
self._createTaskObjects()
self._current_info = None # info obtained by _info task
self.mountpoint = kwargs.get("mountpoint")
self.mountopts = kwargs.get("mountopts")
self.label = kwargs.get("label")
self.fsprofile = kwargs.get("fsprofile")
# filesystem size does not necessarily equal device size
self._size = kwargs.get("size", Size(0))
self._minInstanceSize = Size(0) # min size of this FS instance
# Resize operations are limited to error-free filesystems whose current
# size is known.
self._resizable = False
if flags.installer_mode and self._resize.available:
# if you want current/min size you have to call updateSizeInfo
try:
self.updateSizeInfo()
except FSError:
log.warning("%s filesystem on %s needs repair", self.type,
self.device)
self._targetSize = self._size
self._chrootedMountpoint = None
if self.supported:
self.loadModule()
def _createTaskObjects(self):
""" Create task objects belonging to this master object. """
# pylint: disable=attribute-defined-outside-init
self._info = self._infoClass(self)
self._fsck = self._fsckClass(self)
self._mkfs = self._mkfsClass(self)
self._mount = self._mountClass(self)
self._readlabel = self._readlabelClass(self)
self._resize = self._resizeClass(self)
self._sync = self._syncClass(self)
self._writelabel = self._writelabelClass(self)
# These two may depend on info class, so create them after
self._minsize = self._minsizeClass(self)
self._sizeinfo = self._sizeinfoClass(self)
def __copy__(self):
# Constructs new task objects for the copied FS.
# This ensures that each task object refers back to the newly
# constructed object, result, rather than self.
cls = self.__class__
result = cls.__new__(cls)
result.__dict__.update(self.__dict__)
result._createTaskObjects()
return result
def __repr__(self):
s = DeviceFormat.__repr__(self)
s += (" mountpoint = %(mountpoint)s mountopts = %(mountopts)s\n"
" label = %(label)s size = %(size)s"
" targetSize = %(targetSize)s\n" %
{"mountpoint": self.mountpoint, "mountopts": self.mountopts,
"label": self.label, "size": self._size,
"targetSize": self.targetSize})
return s
@property
def desc(self):
s = "%s filesystem" % self.type
if self.mountpoint:
s += " mounted at %s" % self.mountpoint
return s
@property
def dict(self):
d = super(FS, self).dict
d.update({"mountpoint": self.mountpoint, "size": self._size,
"label": self.label, "targetSize": self.targetSize,
"mountable": self.mountable})
return d
def labeling(self):
"""Returns True if this filesystem uses labels, otherwise False.
:rtype: bool
"""
return (self._mkfs.canLabel and self._mkfs.available) or self._writelabel.available
def relabels(self):
"""Returns True if it is possible to relabel this filesystem
after creation, otherwise False.
:rtype: bool
"""
return self._writelabel.available
def labelFormatOK(self, label):
"""Return True if the label has an acceptable format for this
filesystem. None, which represents accepting the default for this
device, is always acceptable.
:param label: A possible label
:type label: str or None
"""
return label is None or (self._labelfs is not None and self._labelfs.labelFormatOK(label))
label = property(lambda s: s._getLabel(), lambda s,l: s._setLabel(l),
doc="this filesystem's label")
def _setTargetSize(self, newsize):
""" Set the target size for this filesystem.
:param :class:`~.size.Size` newsize: the newsize
"""
if not isinstance(newsize, Size):
raise ValueError("new size must be of type Size")
if not self.exists:
raise FSError("filesystem has not been created")
if not self.resizable:
raise FSError("filesystem is not resizable")
if newsize < self.minSize:
raise ValueError("requested size %s must be at least minimum size %s" % (newsize, self.minSize))
if self.maxSize and newsize >= self.maxSize:
raise ValueError("requested size %s must be less than maximum size %s" % (newsize, self.maxSize))
self._targetSize = newsize
def _getTargetSize(self):
""" Get this filesystem's target size. """
return self._targetSize
targetSize = property(_getTargetSize, _setTargetSize,
doc="Target size for this filesystem")
def _getSize(self):
""" Get this filesystem's size. """
return self.targetSize if self.resizable else self._size
size = property(_getSize, doc="This filesystem's size, accounting "
"for pending changes")
def updateSizeInfo(self):
""" Update this filesystem's current and minimum size (for resize). """
# This method ensures:
# * If there are fsck errors, self._resizable is False.
# Note that if there is no fsck program, no errors are possible.
# * If it is not possible to obtain the current size of the
# filesystem by interrogating the filesystem, self._resizable
# is False (and self._size is 0).
# * _minInstanceSize is obtained or it is set to _size. Effectively
# this means that it is the actual minimum size, or if that
# cannot be obtained the actual current size of the device.
# If it was not possible to obtain the current size of the device
# then _minInstanceSize is 0, but since _resizable is False
# that information can not be used to shrink the filesystem below
# its unknown actual minimum size.
# * self._getMinSize() is only run if fsck succeeds and a current
# existing size can be obtained.
if not self.exists:
return
self._current_info = None
self._minInstanceSize = Size(0)
self._resizable = self.__class__._resizable
# We can't allow resize if the filesystem has errors.
try:
self.doCheck()
except FSError:
self._resizable = False
raise
finally:
# try to gather current size info anyway
self._size = Size(0)
try:
if self._info.available:
self._current_info = self._info.doTask()
except FSError as e:
log.info("Failed to obtain info for device %s: %s", self.device, e)
try:
self._size = self._sizeinfo.doTask()
self._minInstanceSize = self._size
except (FSError, NotImplementedError) as e:
log.warning("Failed to obtain current size for device %s: %s", self.device, e)
# We absolutely need a current size to enable resize. To shrink the
# filesystem we need a real minimum size provided by the resize
# tool. Failing that, we can default to the current size,
# effectively disabling shrink.
if self._size == Size(0):
self._resizable = False
try:
result = self._minsize.doTask()
size = self._padSize(result)
if result < size:
log.debug("padding min size from %s up to %s", result, size)
else:
log.debug("using current size %s as min size", size)
self._minInstanceSize = size
except (FSError, NotImplementedError) as e:
log.warning("Failed to obtain minimum size for device %s: %s", self.device, e)
@property
def minSize(self):
# If self._minInstanceSize is not 0, then it should be no less than
# self._minSize, by definition, and since a non-zero value indicates
# that it was obtained, it is the preferred value.
# If self._minInstanceSize is less than self._minSize,
# but not 0, then there must be some mistake, so better to use
# self._minSize.
return max(self._minInstanceSize, self._minSize)
def _padSize(self, size):
""" Return a size padded according to some inflating rules.
This method was originally designed solely for minimum sizes,
and may only apply to them.
:param size: any size
:type size: :class:`~.size.Size`
:returns: a padded size
:rtype: :class:`~.size.Size`
"""
# add some padding to the min size
padded = min(size * Decimal('1.1'), size + Size("500 MiB"))
# make sure the padded and rounded min size is not larger than
# the current size
padded = min(padded.roundToNearest(self._resize.unit, rounding=ROUND_UP), self.currentSize)
return padded
@property
def currentSize(self):
""" The filesystem's current actual size. """
return self._size if self.exists else Size(0)
@property
def free(self):
""" The amount of space that can be gained by resizing this
filesystem to its minimum size.
"""
return max(Size(0), self.currentSize - self.minSize)
def _preCreate(self, **kwargs):
super(FS, self)._preCreate(**kwargs)
if not self._mkfs.available:
return
def _create(self, **kwargs):
""" Create the filesystem.
:param options: options to pass to mkfs
:type options: list of strings
:raises: FormatCreateError, FSError
"""
log_method_call(self, type=self.mountType, device=self.device,
mountpoint=self.mountpoint)
if not self.formattable:
return
super(FS, self)._create()
try:
self._mkfs.doTask(options=kwargs.get("options"), label=not self.relabels())
except FSWriteLabelError as e:
log.warning("Choosing not to apply label (%s) during creation of filesystem %s. Label format is unacceptable for this filesystem.", self.label, self.type)
except FSError as e:
raise FormatCreateError(e, self.device)
def _postCreate(self, **kwargs):
super(FS, self)._postCreate(**kwargs)
if self.label is not None and self.relabels():
try:
self.writeLabel()
except FSError as e:
log.warning("Failed to write label (%s) for filesystem %s: %s", self.label, self.type, e)
def doResize(self):
""" Resize this filesystem based on this instance's targetSize attr.
:raises: FSResizeError, FSError
"""
if not self.exists:
raise FSResizeError("filesystem does not exist", self.device)
if not self.resizable:
raise FSResizeError("filesystem not resizable", self.device)
if self.targetSize == self.currentSize:
return
if not self._resize.available:
return
# tmpfs mounts don't need an existing device node
if not self.device == "tmpfs" and not os.path.exists(self.device):
raise FSResizeError("device does not exist", self.device)
# The first minimum size can be incorrect if the fs was not
# properly unmounted. After doCheck the minimum size will be correct
# so run the check one last time and bump up the size if it was too
# small.
self.updateSizeInfo()
# Check again if resizable is True, as updateSizeInfo() can change that
if not self.resizable:
raise FSResizeError("filesystem not resizable", self.device)
if self.targetSize < self.minSize:
self.targetSize = self.minSize
log.info("Minimum size changed, setting targetSize on %s to %s",
self.device, self.targetSize)
# Bump target size to nearest whole number of the resize tool's units.
# We always round down because the fs has to fit on whatever device
# contains it. To round up would risk quietly setting a target size too
# large for the device to hold.
rounded = self.targetSize.roundToNearest(self._resize.unit,
rounding=ROUND_DOWN)
# 1. target size was between the min size and max size values prior to
# rounding (see _setTargetSize)
# 2. we've just rounded the target size down (or not at all)
# 3. the minimum size is already either rounded (see _getMinSize) or is
# equal to the current size (see updateSizeInfo)
# 5. the minimum size is less than or equal to the current size (see
# _getMinSize)
#
# This, I think, is sufficient to guarantee that the rounded target size
# is greater than or equal to the minimum size.
# It is possible that rounding down a target size greater than the
# current size would move it below the current size, thus changing the
# direction of the resize. That means the target size was less than one
# unit larger than the current size, and we should do nothing and return
# early.
if self.targetSize > self.currentSize and rounded < self.currentSize:
log.info("rounding target size down to next %s obviated resize of "
"filesystem on %s", unitStr(self._resize.unit), self.device)
return
else:
self.targetSize = rounded
try:
self._resize.doTask()
except FSError as e:
raise FSResizeError(e, self.device)
self.doCheck()
# XXX must be a smarter way to do this
self._size = self.targetSize
self.notifyKernel()
def doCheck(self):
""" Run a filesystem check.
:raises: FSError
"""
if not self.exists:
raise FSError("filesystem has not been created")
if not self._fsck.available:
return
if not os.path.exists(self.device):
raise FSError("device does not exist")
self._fsck.doTask()
def loadModule(self):
"""Load whatever kernel module is required to support this filesystem."""
if not self._modules or self.mountType in kernel_filesystems:
return
for module in self._modules:
try:
rc = util.run_program(["modprobe", module])
except OSError as e:
log.error("Could not load kernel module %s: %s", module, e)
self._supported = False
return
if rc:
log.error("Could not load kernel module %s", module)
self._supported = False
return
# If we successfully loaded a kernel module for this filesystem, we
# also need to update the list of supported filesystems.
update_kernel_filesystems()
@property
def systemMountpoint(self):
""" Get current mountpoint
returns: mountpoint
rtype: str or None
If there are multiple mountpoints it returns the most recent one.
"""
if not self.exists:
return None
if self._chrootedMountpoint:
return self._chrootedMountpoint
# It is possible to have multiple mountpoints, return the last one
try:
return mountsCache.getMountpoints(self.device,
getattr(self, "subvolspec", None))[-1]
except IndexError:
return None
def testMount(self):
""" Try to mount the fs and return True if successful. """
ret = False
if self.status:
return True
# create a temp dir
prefix = "%s.%s" % (os.path.basename(self.device), self.type)
mountpoint = tempfile.mkdtemp(prefix=prefix)
# try the mount
try:
rc = self.mount(mountpoint=mountpoint)
ret = not rc
except Exception: # pylint: disable=broad-except
log_exception_info(log.info, "test mount failed")
if ret:
self.unmount()
os.rmdir(mountpoint)
return ret
def _preSetup(self, **kwargs):
""" Check to see if the filesystem should be mounted.
:keyword chroot: prefix to apply to mountpoint
:keyword mountpoint: mountpoint (overrides self.mountpoint)
:returns: True if it is ok to mount, False if it is not.
:raises: FSError
"""
chroot = kwargs.get("chroot", "/")
mountpoint = kwargs.get("mountpoint") or self.mountpoint
if not self.exists:
raise FSError("filesystem has not been created")
if not mountpoint:
raise FSError("no mountpoint given")
if not isinstance(self, NoDevFS) and not os.path.exists(self.device):
raise FSError("device %s does not exist" % self.device)
chrootedMountpoint = os.path.normpath("%s/%s" % (chroot, mountpoint))
return self.systemMountpoint != chrootedMountpoint
def _setup(self, **kwargs):
""" Mount this filesystem.
:keyword options: mount options (overrides all other option strings)
:type options: str.
:keyword chroot: prefix to apply to mountpoint
:keyword mountpoint: mountpoint (overrides self.mountpoint)
:raises: FSError
"""
options = kwargs.get("options", "")
chroot = kwargs.get("chroot", "/")
mountpoint = kwargs.get("mountpoint") or self.mountpoint
# XXX os.path.join is FUBAR:
#
# os.path.join("/mnt/foo", "/") -> "/"
#
#mountpoint = os.path.join(chroot, mountpoint)
chrootedMountpoint = os.path.normpath("%s/%s" % (chroot, mountpoint))
self._mount.doTask(chrootedMountpoint, options=options)
if chroot != "/":
self._chrootedMountpoint = chrootedMountpoint
def _postSetup(self, **kwargs):
options = kwargs.get("options", "")
chroot = kwargs.get("chroot", "/")
mountpoint = kwargs.get("mountpoint") or self.mountpoint
if flags.selinux and "ro" not in self._mount.mountOptions(options).split(",") and flags.installer_mode:
ret = util.reset_file_context(mountpoint, chroot)
if not ret:
log.warning("Failed to reset SElinux context for newly mounted filesystem root directory to default.")
lost_and_found_context = util.match_path_context("/lost+found")
lost_and_found_path = os.path.join(mountpoint, "lost+found")
ret = util.set_file_context(lost_and_found_path, lost_and_found_context, chroot)
if not ret:
log.warning("Failed to set SELinux context for newly mounted filesystem lost+found directory at %s to %s", lost_and_found_path, lost_and_found_context)
def _preTeardown(self, **kwargs):
if not super(FS, self)._preTeardown(**kwargs):
return False
# Prefer the explicit mountpoint path, fall back to most recent mountpoint
mountpoint = kwargs.get("mountpoint") or self.systemMountpoint
if not mountpoint:
# not mounted
return False
if not os.path.exists(mountpoint):
raise FSError("mountpoint does not exist")
udev.settle()
return True
def _teardown(self, **kwargs):
""" Unmount this filesystem.
:param str mountpoint: Optional mountpoint to be unmounted.
:raises: FSError
If mountpoint isn't passed this will unmount the most recent mountpoint listed
by the system. Override this behavior by passing a specific mountpoint. FSError
will be raised in either case if the path doesn't exist.
"""
mountpoint = kwargs.get("mountpoint") or self.systemMountpoint
rc = util.umount(mountpoint)
if rc:
# try and catch whatever is causing the umount problem
util.run_program(["lsof", mountpoint])
raise FSError("umount failed")
if mountpoint == self._chrootedMountpoint:
self._chrootedMountpoint = None
def readLabel(self):
"""Read this filesystem's label.
:return: the filesystem's label
:rtype: str
Raises a FSReadLabelError if the label can not be read.
"""
if not self.exists:
raise FSReadLabelError("filesystem has not been created")
if not os.path.exists(self.device):
raise FSReadLabelError("device does not exist")
if not self._readlabel.available:
raise FSReadLabelError("can not read label for filesystem %s" % self.type)
return self._readlabel.doTask()
def writeLabel(self):
""" Create a label for this filesystem.
:raises: FSError
If self.label is None, this means accept the default, so raise
an FSError in this case.
Raises a FSError if the label can not be set.
"""
if self.label is None:
raise FSError("makes no sense to write a label when accepting default label")
if not self.exists:
raise FSError("filesystem has not been created")
if not self._writelabel.available:
raise FSError("no application to set label for filesystem %s" % self.type)
if not self.labelFormatOK(self.label):
raise FSError("bad label format for labelling application %s" % self._writelabel)
if not os.path.exists(self.device):
raise FSError("device does not exist")
self._writelabel.doTask()
self.notifyKernel()
@property
def utilsAvailable(self):
# we aren't checking for fsck because we shouldn't need it
tasks = (self._mkfs, self._resize, self._writelabel, self._info)
return all(not t.implemented or t.available for t in tasks)
@property
def supported(self):
log_method_call(self, supported=self._supported)
return super(FS, self).supported and self.utilsAvailable
@property
def controllable(self):
return super(FS, self).controllable and self.mountable
@property
def mountable(self):
return self._mount.available
@property
def formattable(self):
return super(FS, self).formattable and self._mkfs.available
@property
def resizable(self):
""" Can formats of this filesystem type be resized? """
return super(FS, self).resizable and self._resize.available
def _getOptions(self):
return self.mountopts or ",".join(self._mount.options)
def _setOptions(self, options):
self.mountopts = options
@property
def mountType(self):
return self._mount.mountType
# These methods just wrap filesystem-specific methods in more
# generically named methods so filesystems and formatted devices
# like swap and LVM physical volumes can have a common API.
def mount(self, **kwargs):
""" Mount this filesystem.
:keyword options: mount options (overrides all other option strings)
:type options: str.
:keyword chroot: prefix to apply to mountpoint
:keyword mountpoint: mountpoint (overrides self.mountpoint)
:raises: FSError
.. note::
When mounted multiple times the unmount method needs to be called with
a specific mountpoint to unmount, otherwise it will try to unmount the most
recent one listed by the system.
"""
return self.setup(**kwargs)
def unmount(self, **kwargs):
""" Unmount this filesystem.
:param str mountpoint: Optional mountpoint to be unmounted.
:raises: FSError
If mountpoint isn't passed this will unmount the most recent mountpoint listed
by the system. Override this behavior by passing a specific mountpoint. FSError
will be raised in either case if the path doesn't exist.
"""
return self.teardown(**kwargs)
@property
def status(self):
if not self.exists:
return False
return self.systemMountpoint is not None
def sync(self, root='/'):
""" Ensure that data we've written is at least in the journal.
.. note::
This is a little odd because xfs_freeze will only be
available under the install root.
"""
if not self.status or not self.systemMountpoint or \
not self.systemMountpoint.startswith(root) or \
not self._sync.available:
return
try:
self._sync.doTask(root)
except FSError as e:
log.error(e)
def populateKSData(self, data):
super(FS, self).populateKSData(data)
data.mountpoint = self.mountpoint or "none"
data.label = self.label or ""
if self.options != "defaults":
data.fsopts = self.options
else:
data.fsopts = ""
data.fsprofile = self.fsprofile or ""
class Ext2FS(FS):
""" ext2 filesystem. """
_type = "ext2"
_modules = ["ext2"]
_labelfs = fslabeling.Ext2FSLabeling()
_packages = ["e2fsprogs"]
_formattable = True
_supported = True
_resizable = True
_linuxNative = True
_maxSize = Size("8 TiB")
_dump = True
_check = True
_fsckClass = fsck.Ext2FSCK
_infoClass = fsinfo.Ext2FSInfo
_minsizeClass = fsminsize.Ext2FSMinSize
_mkfsClass = fsmkfs.Ext2FSMkfs
_readlabelClass = fsreadlabel.Ext2FSReadLabel
_resizeClass = fsresize.Ext2FSResize
_sizeinfoClass = fssize.Ext2FSSize
_writelabelClass = fswritelabel.Ext2FSWriteLabel
partedSystem = fileSystemType["ext2"]
register_device_format(Ext2FS)
class Ext3FS(Ext2FS):
""" ext3 filesystem. """
_type = "ext3"
_modules = ["ext3"]
partedSystem = fileSystemType["ext3"]
_mkfsClass = fsmkfs.Ext3FSMkfs
# It is possible for a user to specify an fsprofile that defines a blocksize
# smaller than the default of 4096 bytes and therefore to make liars of us
# with regard to this maximum filesystem size, but if they're doing such
# things they should know the implications of their chosen block size.
_maxSize = Size("16 TiB")
register_device_format(Ext3FS)
class Ext4FS(Ext3FS):
""" ext4 filesystem. """
_type = "ext4"
_modules = ["ext4"]
_mkfsClass = fsmkfs.Ext4FSMkfs
partedSystem = fileSystemType["ext4"]
_maxSize = Size("1 EiB")
register_device_format(Ext4FS)
class FATFS(FS):
""" FAT filesystem. """
_type = "vfat"
_modules = ["vfat"]
_labelfs = fslabeling.FATFSLabeling()
_supported = True
_formattable = True
_maxSize = Size("1 TiB")
_packages = [ "dosfstools" ]
_fsckClass = fsck.DosFSCK
_mkfsClass = fsmkfs.FATFSMkfs
_mountClass = fsmount.FATFSMount
_readlabelClass = fsreadlabel.DosFSReadLabel
_writelabelClass = fswritelabel.DosFSWriteLabel
# FIXME this should be fat32 in some cases
partedSystem = fileSystemType["fat16"]
register_device_format(FATFS)
class EFIFS(FATFS):
_type = "efi"
_name = N_("EFI System Partition")
_minSize = Size("50 MiB")
_check = True
_mountClass = fsmount.EFIFSMount
@property
def supported(self):
return super(EFIFS, self).supported and isinstance(platform.platform, platform.EFI)
register_device_format(EFIFS)
class BTRFS(FS):
""" btrfs filesystem """
_type = "btrfs"
_modules = ["btrfs"]
_formattable = True
_linuxNative = True
_supported = True
_packages = ["btrfs-progs"]
_minSize = Size("256 MiB")
_maxSize = Size("16 EiB")
_mkfsClass = fsmkfs.BTRFSMkfs
# FIXME parted needs to be taught about btrfs so that we can set the
# partition table type correctly for btrfs partitions
# partedSystem = fileSystemType["btrfs"]
def __init__(self, **kwargs):
super(BTRFS, self).__init__(**kwargs)
self.volUUID = kwargs.pop("volUUID", None)
self.subvolspec = kwargs.pop("subvolspec", None)
def create(self, **kwargs):
# filesystem creation is done in blockdev.btrfs.create_volume
self.exists = True
def destroy(self, **kwargs):
# filesystem deletion is done in blockdev.btrfs.delete_volume
self.exists = False
def _preSetup(self, **kwargs):
log_method_call(self, type=self.mountType, device=self.device,
mountpoint=self.mountpoint)
# Since btrfs vols have subvols the format setup is automatic.
# Don't try to mount it if there's no mountpoint.
return self.mountpoint or kwargs.get("mountpoint")
register_device_format(BTRFS)
class GFS2(FS):
""" gfs2 filesystem. """
_type = "gfs2"
_modules = ["dlm", "gfs2"]
_formattable = True
_linuxNative = True
_dump = True
_check = True
_packages = ["gfs2-utils"]
_mkfsClass = fsmkfs.GFS2Mkfs
# FIXME parted needs to be thaught about btrfs so that we can set the
# partition table type correctly for btrfs partitions
# partedSystem = fileSystemType["gfs2"]
@property
def supported(self):
""" Is this filesystem a supported type? """
return self.utilsAvailable if flags.gfs2 else self._supported
register_device_format(GFS2)
class JFS(FS):
""" JFS filesystem """
_type = "jfs"
_modules = ["jfs"]
_labelfs = fslabeling.JFSLabeling()
_maxSize = Size("8 TiB")
_formattable = True
_linuxNative = True
_dump = True
_check = True
_infoClass = fsinfo.JFSInfo
_mkfsClass = fsmkfs.JFSMkfs
_sizeinfoClass = fssize.JFSSize
_writelabelClass = fswritelabel.JFSWriteLabel
partedSystem = fileSystemType["jfs"]
@property
def supported(self):
""" Is this filesystem a supported type? """
return self.utilsAvailable if flags.jfs else self._supported
register_device_format(JFS)
class ReiserFS(FS):
""" reiserfs filesystem """
_type = "reiserfs"
_labelfs = fslabeling.ReiserFSLabeling()
_modules = ["reiserfs"]
_maxSize = Size("16 TiB")
_formattable = True
_linuxNative = True
_dump = True
_check = True
_packages = ["reiserfs-utils"]
_infoClass = fsinfo.ReiserFSInfo
_mkfsClass = fsmkfs.ReiserFSMkfs
_sizeinfoClass = fssize.ReiserFSSize
_writelabelClass = fswritelabel.ReiserFSWriteLabel
partedSystem = fileSystemType["reiserfs"]
@property
def supported(self):
""" Is this filesystem a supported type? """
return self.utilsAvailable if flags.reiserfs else self._supported
register_device_format(ReiserFS)
class XFS(FS):
""" XFS filesystem """
_type = "xfs"
_modules = ["xfs"]