[elbe-devel] [PATCH 3/7] elbepack: imgutils: add mount helper

Thomas Weißschuh thomas.weissschuh at linutronix.de
Tue May 7 11:54:39 CEST 2024


This helper will automatically clean up mounts via the context manager
protocol.
It will replace open-coded calls to mount(8).

Signed-off-by: Thomas Weißschuh <thomas.weissschuh at linutronix.de>
---
 elbepack/imgutils.py | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)

diff --git a/elbepack/imgutils.py b/elbepack/imgutils.py
index e74b46bc41f3..2979c288c511 100644
--- a/elbepack/imgutils.py
+++ b/elbepack/imgutils.py
@@ -17,3 +17,26 @@ def losetup(dev, extra_args=[]):
         yield loopdev
     finally:
         do(f'losetup --detach {loopdev}', check=False)
+
+
+ at contextlib.contextmanager
+def mount(device, target, *, bind=False, type=None, options=None):
+    cmd = ['mount']
+
+    if bind:
+        cmd.append('--bind')
+
+    if options is not None:
+        cmd.extend(['-o', ','.join(options)])
+
+    if type is not None:
+        cmd.extend(['-t', type])
+
+    cmd.extend([device, target])
+
+    do(cmd)
+
+    try:
+        yield
+    finally:
+        do(['umount', target], check=False)

-- 
2.45.0



More information about the elbe-devel mailing list