From 007488488efb16bbb7ed8f41c6843e101827ecf6 Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Tue, 18 Feb 2020 16:17:32 +0100 Subject: [PATCH] objectstore: extract mount code to small helpers Extract the mount code into small little helpers, intended to be reused from different places. Adapt ObjectStore to use those. --- osbuild/objectstore.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/osbuild/objectstore.py b/osbuild/objectstore.py index b2267ce9..aa9660c1 100644 --- a/osbuild/objectstore.py +++ b/osbuild/objectstore.py @@ -27,6 +27,30 @@ def suppress_oserror(*errnos): raise e +def mount(source, target, bind=True, ro=True, private=True, mode="0755"): + options = [] + if bind: + options += ["bind"] + if ro: + options += ["ro"] + if mode: + options += [mode] + + args = [] + if private: + args += ["--make-private"] + if options: + args += ["-o", ",".join(options)] + subprocess.run(["mount"] + args + [source, target], check=True) + + +def umount(target, lazy=True): + args = [] + if lazy: + args += ["--lazy"] + subprocess.run(["umount"] + args + [target], check=True) + + class Object: def __init__(self, store: "ObjectStore"): self._init = True @@ -150,11 +174,11 @@ class ObjectStore: with self.tempdir() as tmp: if object_id: path = self.resolve_ref(object_id) - subprocess.run(["mount", "--make-private", "-o", "bind,ro,mode=0755", path, tmp], check=True) + mount(path, tmp) try: yield tmp finally: - subprocess.run(["umount", "--lazy", tmp], check=True) + umount(tmp) else: # None was given as object_id, just return an empty directory yield tmp