summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2022-01-01 14:50:35 +0200
committerLars Wirzenius <liw@liw.fi>2022-01-01 14:50:35 +0200
commit844e57c9e6d77b6485d5659a7894c4ac7ef3fe9d (patch)
treece9783e1148278a4b681eff858c561dab4d003b2
parent685faefd21b22cb390b90cc4edf1363fc41a7657 (diff)
downloadvmdb2-844e57c9e6d77b6485d5659a7894c4ac7ef3fe9d.tar.gz
crypttab support
Sponsored-by: author
-rw-r--r--vmdb/__init__.py2
-rw-r--r--vmdb/plugins/cryptsetup_plugin.py14
-rw-r--r--vmdb/plugins/fstab_plugin.py26
-rw-r--r--vmdb/plugins/mkfs_plugin.py17
-rw-r--r--vmdb/tags.py32
-rw-r--r--vmdb/tags_tests.py26
-rw-r--r--without-tests1
7 files changed, 100 insertions, 18 deletions
diff --git a/vmdb/__init__.py b/vmdb/__init__.py
index 2bc32f2..495c3a3 100644
--- a/vmdb/__init__.py
+++ b/vmdb/__init__.py
@@ -42,6 +42,8 @@ from .tags import (
TagInUse,
AlreadyHasDev,
AlreadyHasFsType,
+ AlreadyHasUuid,
+ AlreadyHasDeviceMapper,
AlreadyHasTargetMountPoint,
AlreadyMounted,
NeedBothMountPoints,
diff --git a/vmdb/plugins/cryptsetup_plugin.py b/vmdb/plugins/cryptsetup_plugin.py
index 8a2bf00..985bc42 100644
--- a/vmdb/plugins/cryptsetup_plugin.py
+++ b/vmdb/plugins/cryptsetup_plugin.py
@@ -49,14 +49,12 @@ class CryptsetupStepRunner(vmdb.StepRunnerInterface):
)
crypt_device = f"/dev/mapper/{name}"
assert os.path.exists(crypt_device)
- state.tags.append(name)
- vmdb.progress("remembering block device {} as {}".format(crypt_device, name))
- state.tags.set_dev(name, crypt_device)
- self.write_crypttab(device, name)
+ uuid = vmdb.runcmd(["cryptsetup", "luksUUID", device]).decode("UTF8").strip()
- shutil.rmtree(tmp)
+ state.tags.append(name)
+ state.tags.set_uuid(name, uuid)
+ state.tags.set_dm(name, name)
+ vmdb.progress("remembering LUKS device {} as {}".format(crypt_device, name))
- def write_crypttab(self, device, name):
- uuid = vmdb.runcmd(["cryptsetup", "luksUUID", device]).strip()
- vmdb.progress(f"crypttab: {name} UUID={uuid} none luks,discard")
+ shutil.rmtree(tmp)
diff --git a/vmdb/plugins/fstab_plugin.py b/vmdb/plugins/fstab_plugin.py
index de21ed7..7f8ef15 100644
--- a/vmdb/plugins/fstab_plugin.py
+++ b/vmdb/plugins/fstab_plugin.py
@@ -40,23 +40,31 @@ class FstabStepRunner(vmdb.StepRunnerInterface):
mount_point = state.tags.get_target_mount_point(tag)
if mount_point is not None:
fstype = state.tags.get_fstype(tag)
- output = vmdb.runcmd(
- ["blkid", "-c", "/dev/null", "-o", "value", "-s", "UUID", device]
- )
- if output:
- uuid = output.decode().strip()
- filesystems.append(
- {"uuid": uuid, "mount_point": mount_point, "fstype": fstype}
- )
- else:
+ uuid = state.tags.get_uuid(tag)
+ if uuid is None:
raise Exception(
"Unknown UUID for device {} (to be mounted on {})".format(
device, mount_point
)
)
+ filesystems.append(
+ {
+ "uuid": uuid,
+ "mount_point": mount_point,
+ "fstype": fstype,
+ }
+ )
+
fstab_path = os.path.join(chroot, "etc/fstab")
line = "UUID={uuid} {mount_point} {fstype} errors=remount-ro 0 1\n"
with open(fstab_path, "w") as fstab:
for entry in filesystems:
fstab.write(line.format(**entry))
+
+ crypttab_path = os.path.join(chroot, "etc/crypttab")
+ line = "{name} UUID={uuid} none luks,discard\n"
+ with open(crypttab_path, "w") as crypttab:
+ for entry in filesystems:
+ if "uuid" in entry and "name" in entry and "device" not in entry:
+ crypttab.write(line.format(**entry))
diff --git a/vmdb/plugins/mkfs_plugin.py b/vmdb/plugins/mkfs_plugin.py
index 83c6aff..9e471c2 100644
--- a/vmdb/plugins/mkfs_plugin.py
+++ b/vmdb/plugins/mkfs_plugin.py
@@ -53,10 +53,25 @@ class MkfsStepRunner(vmdb.StepRunnerInterface):
options = values["options"] or None
if options:
- for opt in options.split(' '):
+ for opt in options.split(" "):
cmd.append(opt)
cmd.append(device)
vmdb.runcmd(cmd)
+ uuid = (
+ vmdb.runcmd(
+ [
+ "blkid",
+ "-c/dev/null",
+ "-ovalue",
+ "-sUUID",
+ device,
+ ]
+ )
+ .decode()
+ .strip()
+ )
+
state.tags.set_fstype(tag, fstype)
+ state.tags.set_uuid(tag, uuid)
diff --git a/vmdb/tags.py b/vmdb/tags.py
index 0e59f1a..25703ef 100644
--- a/vmdb/tags.py
+++ b/vmdb/tags.py
@@ -34,6 +34,14 @@ class Tags:
item = self._get(tag)
return item["dev"]
+ def get_uuid(self, tag):
+ item = self._get(tag)
+ return item["uuid"]
+
+ def get_dm(self, tag):
+ item = self._get(tag)
+ return item["dm"]
+
def get_builder_mount_point(self, tag):
item = self._get(tag)
return item["builder_mount_point"]
@@ -59,6 +67,8 @@ class Tags:
"builder_mount_point": None,
"fstype": None,
"target_mount_point": None,
+ "uuid": None,
+ "dm": None,
}
def set_dev(self, tag, dev):
@@ -80,6 +90,18 @@ class Tags:
raise AlreadyHasFsType(tag)
item["fstype"] = fstype
+ def set_uuid(self, tag, uuid):
+ item = self._get(tag)
+ if item["uuid"] is not None:
+ raise AlreadyHasUuid(tag)
+ item["uuid"] = uuid
+
+ def set_dm(self, tag, name):
+ item = self._get(tag)
+ if item["dm"] is not None:
+ raise AlreadyHasDeviceMapper(tag)
+ item["dm"] = name
+
def set_target_mount_point(self, tag, target_mount_point):
item = self._get(tag)
if item["target_mount_point"] is not None:
@@ -129,6 +151,16 @@ class AlreadyHasTargetMountPoint(Exception):
super().__init__("Already has target mount point: {}".format(tag))
+class AlreadyHasUuid(Exception):
+ def __init__(self, tag):
+ super().__init__("Already has UUID: {}".format(tag))
+
+
+class AlreadyHasDeviceMapper(Exception):
+ def __init__(self, tag):
+ super().__init__("Already has device-mapper name: {}".format(tag))
+
+
class NeedBothMountPoints(Exception):
def __init__(self, target_mp):
super().__init__("Need both mount points set, target: {}".format(target_mp))
diff --git a/vmdb/tags_tests.py b/vmdb/tags_tests.py
index fdcebf3..d3171d8 100644
--- a/vmdb/tags_tests.py
+++ b/vmdb/tags_tests.py
@@ -121,6 +121,32 @@ class TagsTests(unittest.TestCase):
with self.assertRaises(vmdb.AlreadyHasFsType):
tags.set_fstype("first", "ext4")
+ def test_set_uuid(self):
+ tags = vmdb.Tags()
+ tags.append("first")
+ tags.set_uuid("first", "uuid")
+ self.assertEqual(tags.get_uuid("first"), "uuid")
+
+ def test_set_uuid_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")
+
+ def test_set_dm(self):
+ tags = vmdb.Tags()
+ tags.append("first")
+ tags.set_dm("first", "dm")
+ self.assertEqual(tags.get_dm("first"), "dm")
+
+ def test_set_dm_raises_error_for_double_fstype(self):
+ tags = vmdb.Tags()
+ tags.append("first")
+ tags.set_dm("first", "dm")
+ with self.assertRaises(vmdb.AlreadyHasDeviceMapper):
+ tags.set_dm("first", "other")
+
def test_set_target_mount_point(self):
tags = vmdb.Tags()
tags.append("first")
diff --git a/without-tests b/without-tests
index 978ec61..520f822 100644
--- a/without-tests
+++ b/without-tests
@@ -30,6 +30,7 @@ vmdb/plugins/unpack_rootfs_plugin.py
vmdb/plugins/vgcreate_plugin.py
vmdb/plugins/virtualfs_plugin.py
vmdb/plugins/zerofree_plugin.py
+vmdb/plugins/cryptsetup_plugin.py
vmdb/runcmd.py
vmdb/state.py
vmdb/version.py