From 2e09e7937c6d62053b68db23c04320fadbbad7cb Mon Sep 17 00:00:00 2001 From: Christian Kellner Date: Tue, 9 Aug 2022 18:12:36 +0200 Subject: [PATCH] objectstore, move {u,}mount methods to util.mnt Move the mount and umount helpers to the new mount utility module. No semantic change in the function. --- osbuild/objectstore.py | 36 +----------------------------------- osbuild/util/mnt.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 35 deletions(-) diff --git a/osbuild/objectstore.py b/osbuild/objectstore.py index 9fc0031f..dab5e297 100644 --- a/osbuild/objectstore.py +++ b/osbuild/objectstore.py @@ -7,6 +7,7 @@ from typing import Optional, Iterator, Set from osbuild.util.types import PathLike from osbuild.util import jsoncomm, rmrf +from osbuild.util.mnt import mount, umount from . import api @@ -15,41 +16,6 @@ __all__ = [ ] -def mount(source, target, bind=True, ro=True, private=True, mode="0755"): - options = [] - if ro: - options += ["ro"] - if mode: - options += [mode] - - args = [] - if bind: - args += ["--rbind"] - if private: - args += ["--make-rprivate"] - if options: - args += ["-o", ",".join(options)] - - r = subprocess.run(["mount"] + args + [source, target], - stderr=subprocess.STDOUT, - stdout=subprocess.PIPE, - encoding="utf-8", - check=False) - - if r.returncode != 0: - code = r.returncode - msg = r.stdout.strip() - raise RuntimeError(f"{msg} (code: {code})") - - -def umount(target, lazy=False): - args = [] - if lazy: - args += ["--lazy"] - subprocess.run(["sync", "-f", target], check=True) - subprocess.run(["umount", "-R"] + args + [target], check=True) - - class Object: def __init__(self, store: "ObjectStore"): self._init = True diff --git a/osbuild/util/mnt.py b/osbuild/util/mnt.py index 40753f28..7169bd39 100644 --- a/osbuild/util/mnt.py +++ b/osbuild/util/mnt.py @@ -5,6 +5,41 @@ import contextlib import subprocess +def mount(source, target, bind=True, ro=True, private=True, mode="0755"): + options = [] + if ro: + options += ["ro"] + if mode: + options += [mode] + + args = [] + if bind: + args += ["--rbind"] + if private: + args += ["--make-rprivate"] + if options: + args += ["-o", ",".join(options)] + + r = subprocess.run(["mount"] + args + [source, target], + stderr=subprocess.STDOUT, + stdout=subprocess.PIPE, + encoding="utf-8", + check=False) + + if r.returncode != 0: + code = r.returncode + msg = r.stdout.strip() + raise RuntimeError(f"{msg} (code: {code})") + + +def umount(target, lazy=False): + args = [] + if lazy: + args += ["--lazy"] + subprocess.run(["sync", "-f", target], check=True) + subprocess.run(["umount", "-R"] + args + [target], check=True) + + class MountGuard(contextlib.AbstractContextManager): def __init__(self): self.mounts = []