From 659ce42c830cb3457a3d6bf75b29053f67584bea Mon Sep 17 00:00:00 2001 From: Tom Gundersen Date: Mon, 29 Jul 2019 16:36:46 +0200 Subject: [PATCH] BuildRoot: don't use nspawn's --volatile mode We want the same functionality, but we now impleent it ourselves. In addition to bind-mounting in /usr into the target container (which is all nspawn does), we also add /bin, /sbin, /lib and /lib64, if they exist and are not symlinks (presuambly into /usr). This means we can work on distros who have not implemented the usr-move, like Ubuntu Bionic (used by Travis). Signed-off-by: Tom Gundersen --- osbuild/__init__.py | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/osbuild/__init__.py b/osbuild/__init__.py index 1e0f8bbb..b46fd010 100644 --- a/osbuild/__init__.py +++ b/osbuild/__init__.py @@ -73,21 +73,28 @@ class BuildRoot: def __init__(self, path="/run/osbuild"): self.root = tempfile.mkdtemp(prefix="osbuild-buildroot-", dir=path) self.api = tempfile.mkdtemp(prefix="osbuild-api-", dir=path) - self.mounted = False - try: - subprocess.run(["mount", "-o", "bind,ro", "/", self.root], check=True) - self.mounted = True - except subprocess.CalledProcessError: - self.unmount() - raise + self.mounts = [] + for p in ["usr", "bin", "sbin", "lib", "lib64"]: + source = os.path.join("/", p) + target = os.path.join(self.root, p) + if not os.path.isdir(source) or os.path.islink(source): + continue # only bind-mount real dirs + os.mkdir(target) + try: + subprocess.run(["mount", "-o", "bind,ro", source, target], check=True) + except subprocess.CalledProcessError: + self.unmount() + raise + self.mounts.append(target) def unmount(self): - if not self.root: - return - if self.mounted: - subprocess.run(["umount", "--lazy", self.root], check=True) - os.rmdir(self.root) - self.root = None + for path in self.mounts: + subprocess.run(["umount", "--lazy", path], check=True) + os.rmdir(path) + self.mounts = [] + if self.root: + shutil.rmtree(self.root) + self.root = None if self.api: shutil.rmtree(self.api) self.api = None @@ -97,13 +104,13 @@ class BuildRoot: Its arguments mean the same as those for subprocess.run(). """ + return subprocess.run([ "systemd-nspawn", "--quiet", "--register=no", "--as-pid2", "--link-journal=no", - "--volatile=yes", "--property=DeviceAllow=block-loop rw", f"--directory={self.root}", *[f"--bind={b}" for b in (binds or [])],