Skip to content

Commit

Permalink
swap: Use libblockdev to check label and UUID format
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechtrefny committed Nov 1, 2023
1 parent 8c4827c commit 6952fa3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 4 deletions.
17 changes: 13 additions & 4 deletions blivet/formats/swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
from ..errors import FSWriteUUIDError, SwapSpaceError
from ..storage_log import log_method_call
from ..tasks import availability
from ..tasks import fsuuid
from . import DeviceFormat, register_device_format
from ..size import Size
from .. import udev
Expand Down Expand Up @@ -114,8 +113,13 @@ def relabels(self):
return True and self._plugin.available

def label_format_ok(self, label):
"""Returns True since no known restrictions on the label."""
return True
"""Check whether the given label is correct (16 characters or shorter)."""
try:
blockdev.swap.check_label(label)
except blockdev.SwapError:
return False
else:
return True

def write_label(self, dry_run=False):
""" Create a label for this format.
Expand Down Expand Up @@ -154,7 +158,12 @@ def write_label(self, dry_run=False):

def uuid_format_ok(self, uuid):
"""Check whether the given UUID is correct according to RFC 4122."""
return fsuuid.FSUUID._check_rfc4122_uuid(uuid)
try:
blockdev.swap.check_uuid(uuid)
except blockdev.SwapError:
return False
else:
return True

def _set_priority(self, priority):
# pylint: disable=attribute-defined-outside-init
Expand Down
11 changes: 11 additions & 0 deletions tests/unit_tests/formats_tests/swap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,14 @@ def test_swap_max_size(self):
with six.assertRaisesRegex(self, DeviceError, "device is too large for new format"):
StorageDevice("dev", size=Size("17 TiB"),
fmt=get_format("swap"))

def test_swap_uuid_format(self):
fmt = get_format("swap")

# label -- at most 16 characters
self.assertTrue(fmt.label_format_ok("label"))
self.assertFalse(fmt.label_format_ok("a" * 17))

# uuid -- RFC 4122 format
self.assertTrue(fmt.uuid_format_ok("01234567-1234-1234-1234-012345678911"))
self.assertFalse(fmt.uuid_format_ok("aaaa"))

0 comments on commit 6952fa3

Please sign in to comment.