util/mnt: extract MountGuard into new module

Extract the `MountGuard` class from all stages that defined it into
a new `mnt` utility module.
This commit is contained in:
Christian Kellner 2022-08-09 18:07:17 +02:00 committed by Tom Gundersen
parent 7c0f0c1052
commit b49f3f91f9
4 changed files with 43 additions and 107 deletions

40
osbuild/util/mnt.py Normal file
View file

@ -0,0 +1,40 @@
"""Mount utilities
"""
import contextlib
import subprocess
class MountGuard(contextlib.AbstractContextManager):
def __init__(self):
self.mounts = []
def mount(self, source, target, bind=True, ro=False, mode="0755"):
options = []
if bind:
options += ["bind"]
if ro:
options += ["ro"]
if mode:
options += [mode]
args = ["--make-private"]
if options:
args += ["-o", ",".join(options)]
subprocess.run(["mount"] + args + [source, target], check=True)
self.mounts += [{"source": source, "target": target}]
def umount(self):
while self.mounts:
mount = self.mounts.pop() # FILO: get the last mount
target = mount["target"]
# The sync should in theory not be needed but in rare
# cases `target is busy` error has been spotted.
# Calling `sync` does not hurt so we keep it for now.
subprocess.run(["sync", "-f", target], check=True)
subprocess.run(["umount", target], check=True)
def __exit__(self, exc_type, exc_val, exc_tb):
self.umount()