Skip to content

Commit

Permalink
Add support for setting label when creating GFS2 format
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechtrefny committed Jun 3, 2024
1 parent e23d354 commit 8ecb48f
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 6 deletions.
1 change: 1 addition & 0 deletions blivet/formats/fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,6 +1123,7 @@ class GFS2(FS):
_check = True
_packages = ["gfs2-utils"]
_mkfs_class = fsmkfs.GFS2Mkfs
_labelfs = fslabeling.GFS2Labeling()
# FIXME parted needs to be thaught about btrfs so that we can set the
# partition table type correctly for btrfs partitions
# parted_system = fileSystemType["gfs2"]
Expand Down
17 changes: 17 additions & 0 deletions blivet/tasks/fslabeling.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
# Red Hat Author(s): Anne Mulhern <[email protected]>

import abc
import string

import gi
gi.require_version("BlockDev", "3.0")
Expand Down Expand Up @@ -92,3 +93,19 @@ class F2FSLabeling(FSLabeling):
@classmethod
def label_format_ok(cls, label):
return cls._blockdev_check_label("f2fs", label)


class GFS2Labeling(FSLabeling):

@classmethod
def label_format_ok(cls, label):
try:
clustername, lockspace = label.split(":")
except ValueError:
return False

if len(clustername) > 32 or len(lockspace) > 30:
return False

allowed = string.ascii_letters + string.digits + "-_"
return all(c in allowed for c in clustername) and all(c in allowed for c in lockspace)
2 changes: 1 addition & 1 deletion blivet/tasks/fsmkfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def do_task(self, options=None, label=False, set_uuid=False, nodiscard=False):

class GFS2Mkfs(FSMkfs):
ext = availability.MKFS_GFS2_APP
label_option = None
label_option = "-t"
nodiscard_option = None
get_uuid_args = None

Expand Down
13 changes: 13 additions & 0 deletions tests/storage_tests/formats_test/fs_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ class BTRFSTestCase(fstesting.FSAsRoot):

class GFS2TestCase(fstesting.FSAsRoot):
_fs_class = fs.GFS2
_valid_label = "label:label"

@classmethod
def setUpClass(cls):
# allow creating gfs2
flags.gfs2 = True
super(GFS2TestCase, cls).setUpClass()

@classmethod
def tearDownClass(cls):
# reset flag back to default
flags.gfs2 = False
super(GFS2TestCase, cls).tearDownClass()


class XFSTestCase(fstesting.FSAsRoot):
Expand Down
11 changes: 6 additions & 5 deletions tests/storage_tests/formats_test/fstesting.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class FSAsRoot(loopbackedtestcase.LoopBackedTestCase, metaclass=abc.ABCMeta):
doc="The class of the filesystem being tested on.")

_DEVICE_SIZE = Size("100 MiB")
_valid_label = "label"

def __init__(self, methodName='run_test'):
super(FSAsRoot, self).__init__(methodName=methodName, device_spec=[self._DEVICE_SIZE])
Expand Down Expand Up @@ -125,15 +126,15 @@ def test_labeling(self):
if not an_fs.formattable or not an_fs.labeling():
self.skipTest("can not label filesystem %s" % an_fs.name)
an_fs.device = self.loop_devices[0]
an_fs.label = "label"
self.assertTrue(an_fs.label_format_ok("label"))
an_fs.label = self._valid_label
self.assertTrue(an_fs.label_format_ok(self._valid_label))
self.assertIsNone(an_fs.create())
try:
label = an_fs.read_label()
if an_fs.type in ("vfat", "efi"):
self.assertEqual(label, "LABEL")
self.assertEqual(label, self._valid_label.upper())
else:
self.assertEqual(label, "label")
self.assertEqual(label, self._valid_label)
except FSError:
pass

Expand All @@ -144,7 +145,7 @@ def test_relabeling(self):
an_fs.device = self.loop_devices[0]
self.assertIsNone(an_fs.create())
an_fs.label = "label"
self.assertTrue(an_fs.label_format_ok("label"))
self.assertTrue(an_fs.label_format_ok(self._valid_label))
if an_fs.relabels():
self.assertIsNone(an_fs.write_label())
else:
Expand Down
6 changes: 6 additions & 0 deletions tests/storage_tests/formats_test/labeling_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ class NTFSTestCase(fslabeling.CompleteLabelingAsRoot):
_default_label = ""


class GFS2TestCase(fslabeling.CompleteLabelingAsRoot):
_fs_class = fs.GFS2
_invalid_label = "label:label*"
_default_label = ""


class LabelingSwapSpaceTestCase(loopbackedtestcase.LoopBackedTestCase):

def test_labeling(self):
Expand Down
8 changes: 8 additions & 0 deletions tests/unit_tests/formats_tests/init_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ def test_labels(self):
self.assertFalse(formats.fs.NTFS().label_format_ok("n" * 129))
self.assertTrue(formats.fs.NTFS().label_format_ok("n" * 128))

# GFS2 has special format: two parts, limited lengths and characters
self.assertFalse(formats.fs.GFS2().label_format_ok("label"))
self.assertFalse(formats.fs.GFS2().label_format_ok("label:label:label"))
self.assertFalse(formats.fs.GFS2().label_format_ok("n" * 33 + ":label"))
self.assertFalse(formats.fs.GFS2().label_format_ok("label:" + "n" * 31))
self.assertFalse(formats.fs.GFS2().label_format_ok("label:label*"))
self.assertTrue(formats.fs.GFS2().label_format_ok("label:label"))

# all devices are permitted to be passed a label argument of None
# some will ignore it completely
for _k, v in formats.device_formats.items():
Expand Down

0 comments on commit 8ecb48f

Please sign in to comment.