summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrtkapiper <andy.piper@arcticwolf.com>2023-07-11 18:21:44 +0000
committerLars Wirzenius <liw@liw.fi>2023-07-14 15:00:46 +0000
commit85fa0750c5b6720d45c8d34d95c91894ac7bf2cc (patch)
treed31d73a96c607b4dafc2ac1ceb3a041df5283320
parenta993ca34fe8a6e190b3216b34c2127f921315b1e (diff)
downloadvmdb2-85fa0750c5b6720d45c8d34d95c91894ac7bf2cc.tar.gz
Add support for images with existing partitions and filesystems
Enable support for running vmdb2 with a disk image that has already been partitioned, with pre-existing filesystems in those partitions. The kpartx_plugin's new "tags" key takes a list of partition/mountpoint tags to use when referring to the partitions later in the vmdb2 spec file. The mount_plugin has been updated to detect the filesystem and uuid if the mountpoint tags for the disk image were defined via the new "tags" kpartx_plugin key, rather than via invoking the mkfs plugin. This allows the mount_plugin to use the fstype to determine if zerofree can be run on a partition when it is unmounted during build teardown.
-rw-r--r--vmdb/plugins/kpartx.mdwn11
-rw-r--r--vmdb/plugins/kpartx_plugin.py19
-rw-r--r--vmdb/plugins/mount_plugin.py19
3 files changed, 44 insertions, 5 deletions
diff --git a/vmdb/plugins/kpartx.mdwn b/vmdb/plugins/kpartx.mdwn
index 9bb81a7..0074f38 100644
--- a/vmdb/plugins/kpartx.mdwn
+++ b/vmdb/plugins/kpartx.mdwn
@@ -8,6 +8,17 @@ Step keys:
* `kpartx` &mdash; REQUIRED; filename of block device with partitions.
+* `tags` &mdash; OPTIONAL; list of tags to apply to partitions when re-using
+ an existing image that has already been populated with formatted partitions
+
Example (in the .vmdb file):
+ # typical use
+ - kpartx: "{{ output }}"
+
+ # using an image that already contains partitions containing filesystems
+ # that should be mounted as `/boot` and `/`
- kpartx: "{{ output }}"
+ tags:
+ - boot
+ - root
diff --git a/vmdb/plugins/kpartx_plugin.py b/vmdb/plugins/kpartx_plugin.py
index baa4f54..2432a16 100644
--- a/vmdb/plugins/kpartx_plugin.py
+++ b/vmdb/plugins/kpartx_plugin.py
@@ -28,14 +28,21 @@ class KpartxPlugin(vmdb.Plugin):
class KpartxStepRunner(vmdb.StepRunnerInterface):
def get_key_spec(self):
- return {"kpartx": str}
+ return {"kpartx": str, "tags": []}
def run(self, values, settings, state):
device = values["kpartx"]
- tags = state.tags.get_tags()
+ tags = values["tags"]
+ if not tags:
+ tags = state.tags.get_tags()
+ else:
+ # add the tags defined in the plugin spec
+ for tag in tags:
+ state.tags.append(tag)
+
devs = self.kpartx(device)
for tag, dev in zip(tags, devs):
- vmdb.progress("remembering {} as {}".format(dev, tag))
+ vmdb.progress(f"remembering {dev} as {tag}")
state.tags.set_dev(tag, dev)
def kpartx(self, device):
@@ -78,6 +85,8 @@ class KpartxStepRunner(vmdb.StepRunnerInterface):
loopdev_info = loopdev_info.pop()
if loopdev_info["back-file"] is None:
continue
- vmdb.progress("Loop device {} is still bound to back-file {}, "
- "removing loop device".format(loop_dev, loopdev_info["back-file"]))
+ vmdb.progress(
+ "Loop device {} is still bound to back-file {}, "
+ "removing loop device".format(loop_dev, loopdev_info["back-file"])
+ )
vmdb.runcmd(["losetup", "-d", loop_dev])
diff --git a/vmdb/plugins/mount_plugin.py b/vmdb/plugins/mount_plugin.py
index b8ebcb1..30554b7 100644
--- a/vmdb/plugins/mount_plugin.py
+++ b/vmdb/plugins/mount_plugin.py
@@ -76,6 +76,25 @@ class MountStepRunner(vmdb.StepRunnerInterface):
state.tags.set_builder_mount_point(tag, mount_point, cached=True)
state.tags.set_target_mount_point(tag, dirname)
+ # detect the filesystem and uuid if they are not already known. This
+ # will be the case if the tags were defined via the kpartx plugin,
+ # rather than by the mkfs plugin
+ if not state.tags.get_fstype(tag):
+ fstype = (
+ vmdb.runcmd(["blkid", "-c/dev/null", "-ovalue", "-sTYPE", device])
+ .decode()
+ .strip()
+ )
+ state.tags.set_fstype(tag, fstype)
+ uuid = state.tags.get_fsuuid(tag)
+ if not uuid:
+ uuid = (
+ vmdb.runcmd(["blkid", "-c/dev/null", "-ovalue", "-sUUID", device])
+ .decode()
+ .strip()
+ )
+ state.tags.set_fsuuid(tag, uuid)
+
return mount_point
def unmount_rootfs(self, values, settings, state):