From 5c654df9299fe86a040533b16cd613ee8fa84a3f Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Sat, 1 Jan 2022 20:25:55 +0200 Subject: be more explicit about cryptsetup info in tags Sponsored-by: author --- vmdb/__init__.py | 3 ++- vmdb/plugins/cryptsetup_plugin.py | 4 ++-- vmdb/plugins/fstab_plugin.py | 35 ++++++++++++++++++++--------------- vmdb/tags.py | 34 +++++++++++++++++++++++++--------- vmdb/tags_tests.py | 27 ++++++++++++++++++++------- 5 files changed, 69 insertions(+), 34 deletions(-) diff --git a/vmdb/__init__.py b/vmdb/__init__.py index 495c3a3..c308950 100644 --- a/vmdb/__init__.py +++ b/vmdb/__init__.py @@ -42,7 +42,8 @@ from .tags import ( TagInUse, AlreadyHasDev, AlreadyHasFsType, - AlreadyHasUuid, + AlreadyHasFsUuid, + AlreadyHasLuksUuid, AlreadyHasDeviceMapper, AlreadyHasTargetMountPoint, AlreadyMounted, diff --git a/vmdb/plugins/cryptsetup_plugin.py b/vmdb/plugins/cryptsetup_plugin.py index 75b4c4f..238d7ab 100644 --- a/vmdb/plugins/cryptsetup_plugin.py +++ b/vmdb/plugins/cryptsetup_plugin.py @@ -54,9 +54,9 @@ class CryptsetupStepRunner(vmdb.StepRunnerInterface): state.tags.append(name) state.tags.set_dev(name, crypt_device) - state.tags.set_uuid(name, uuid) + state.tags.set_luksuuid(name, uuid) state.tags.set_dm(name, name) - vmdb.progress(f"LUKS: name={name} dev={crypt_device} uuid={uuid} dm={name}") + vmdb.progress(f"LUKS: name={name} dev={crypt_device} luksuuid={uuid} dm={name}") vmdb.progress(f"LUKS: {state.tags._tags}") vmdb.progress("remembering LUKS device {} as {}".format(crypt_device, name)) diff --git a/vmdb/plugins/fstab_plugin.py b/vmdb/plugins/fstab_plugin.py index dfba1b1..fdc358c 100644 --- a/vmdb/plugins/fstab_plugin.py +++ b/vmdb/plugins/fstab_plugin.py @@ -34,16 +34,19 @@ class FstabStepRunner(vmdb.StepRunnerInterface): chroot = state.tags.get_builder_mount_point(tag) filesystems = [] + crypts = [] for tag in state.tags.get_tags(): device = state.tags.get_dev(tag) mount_point = state.tags.get_target_mount_point(tag) - if mount_point is not None: - fstype = state.tags.get_fstype(tag) - uuid = state.tags.get_uuid(tag) - dm = state.tags.get_dm(tag) - if uuid is None: + fstype = state.tags.get_fstype(tag) + fsuuid = state.tags.get_fsuuid(tag) + luksuuid = state.tags.get_luksuuid(tag) + dm = state.tags.get_dm(tag) + + if mount_point is not None: + if fsuuid is None: raise Exception( "Unknown UUID for device {} (to be mounted on {})".format( device, mount_point @@ -52,12 +55,18 @@ class FstabStepRunner(vmdb.StepRunnerInterface): filesystems.append( { - "uuid": uuid, - "dm": dm, + "uuid": fsuuid, "mount_point": mount_point, "fstype": fstype, } ) + elif luksuuid is not None and dm is not None: + crypts.append( + { + "dm": dm, + "luksuuid": luksuuid, + } + ) fstab_path = os.path.join(chroot, "etc/fstab") line = "UUID={uuid} {mount_point} {fstype} errors=remount-ro 0 1\n" @@ -65,14 +74,10 @@ class FstabStepRunner(vmdb.StepRunnerInterface): for entry in filesystems: fstab.write(line.format(**entry)) - entries = [ - e for e in filesystems if e["uuid"] is not None and e["dm"] is not None - ] - vmdb.progress(f"filesystems: {filesystems}") - vmdb.progress(f"crypttab entries: {entries}") - if entries: + vmdb.progress(f"crypts: {crypts}") + if crypts: crypttab_path = os.path.join(chroot, "etc/crypttab") - line = "{dm} UUID={uuid} none luks,discard\n" + line = "{dm} UUID={luksuuid} none luks,discard\n" with open(crypttab_path, "w") as crypttab: - for entry in entries: + for entry in crypts: crypttab.write(line.format(**entry)) diff --git a/vmdb/tags.py b/vmdb/tags.py index 25703ef..60001d1 100644 --- a/vmdb/tags.py +++ b/vmdb/tags.py @@ -34,9 +34,13 @@ class Tags: item = self._get(tag) return item["dev"] - def get_uuid(self, tag): + def get_fsuuid(self, tag): item = self._get(tag) - return item["uuid"] + return item["fsuuid"] + + def get_luksuuid(self, tag): + item = self._get(tag) + return item["luksuuid"] def get_dm(self, tag): item = self._get(tag) @@ -67,7 +71,8 @@ class Tags: "builder_mount_point": None, "fstype": None, "target_mount_point": None, - "uuid": None, + "fsuuid": None, + "luksuuid": None, "dm": None, } @@ -90,11 +95,17 @@ class Tags: raise AlreadyHasFsType(tag) item["fstype"] = fstype - def set_uuid(self, tag, uuid): + def set_fsuuid(self, tag, uuid): + item = self._get(tag) + if item["fsuuid"] is not None: + raise AlreadyHasFsUuid(tag) + item["fsuuid"] = uuid + + def set_luksuuid(self, tag, uuid): item = self._get(tag) - if item["uuid"] is not None: - raise AlreadyHasUuid(tag) - item["uuid"] = uuid + if item["luksuuid"] is not None: + raise AlreadyHasLuksUuid(tag) + item["luksuuid"] = uuid def set_dm(self, tag, name): item = self._get(tag) @@ -151,9 +162,14 @@ class AlreadyHasTargetMountPoint(Exception): super().__init__("Already has target mount point: {}".format(tag)) -class AlreadyHasUuid(Exception): +class AlreadyHasFsUuid(Exception): + def __init__(self, tag): + super().__init__("Already has fs UUID: {}".format(tag)) + + +class AlreadyHasLuksUuid(Exception): def __init__(self, tag): - super().__init__("Already has UUID: {}".format(tag)) + super().__init__("Already has LuksUUID: {}".format(tag)) class AlreadyHasDeviceMapper(Exception): diff --git a/vmdb/tags_tests.py b/vmdb/tags_tests.py index d3171d8..5264c95 100644 --- a/vmdb/tags_tests.py +++ b/vmdb/tags_tests.py @@ -121,18 +121,31 @@ class TagsTests(unittest.TestCase): with self.assertRaises(vmdb.AlreadyHasFsType): tags.set_fstype("first", "ext4") - def test_set_uuid(self): + def test_set_fsuuid(self): tags = vmdb.Tags() tags.append("first") - tags.set_uuid("first", "uuid") - self.assertEqual(tags.get_uuid("first"), "uuid") + tags.set_fsuuid("first", "uuid") + self.assertEqual(tags.get_fsuuid("first"), "uuid") - def test_set_uuid_raises_error_for_double_fstype(self): + def test_set_fsuuid_raises_error_for_double_fstype(self): tags = vmdb.Tags() tags.append("first") - tags.set_uuid("first", "uuid") - with self.assertRaises(vmdb.AlreadyHasUuid): - tags.set_uuid("first", "other") + tags.set_fsuuid("first", "uuid") + with self.assertRaises(vmdb.AlreadyHasFsUuid): + tags.set_fsuuid("first", "other") + + def test_set_luksuuid(self): + tags = vmdb.Tags() + tags.append("first") + tags.set_luksuuid("first", "uuid") + self.assertEqual(tags.get_luksuuid("first"), "uuid") + + def test_set_luksuuid_raises_error_for_double_fstype(self): + tags = vmdb.Tags() + tags.append("first") + tags.set_luksuuid("first", "uuid") + with self.assertRaises(vmdb.AlreadyHasLuksUuid): + tags.set_luksuuid("first", "other") def test_set_dm(self): tags = vmdb.Tags() -- cgit v1.2.1