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.
This commit is contained in:
Christian Kellner 2022-08-09 18:12:36 +02:00 committed by Tom Gundersen
parent a43ea66be2
commit 2e09e7937c
2 changed files with 36 additions and 35 deletions

View file

@ -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

View file

@ -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 = []