summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJürg Billeter <j@bitron.ch>2020-02-10 07:24:09 +0100
committerJürg Billeter <j@bitron.ch>2020-02-10 07:24:09 +0100
commit7d8a23fd0a0dd6cdecef9e50a777c14644bc19fe (patch)
treeafb80a5b5fcd47367230e683d4c2d8f4fc112294
parent0591b0c8b038aa9f054b295748f3423384711d50 (diff)
downloadbuildstream-7d8a23fd0a0dd6cdecef9e50a777c14644bc19fe.tar.gz
sandbox: Remove unused _mounter.py
-rw-r--r--src/buildstream/sandbox/_mounter.py148
-rw-r--r--tests/sandboxes/mounting/mount_simple.py52
2 files changed, 0 insertions, 200 deletions
diff --git a/src/buildstream/sandbox/_mounter.py b/src/buildstream/sandbox/_mounter.py
deleted file mode 100644
index 3adf8ff5b..000000000
--- a/src/buildstream/sandbox/_mounter.py
+++ /dev/null
@@ -1,148 +0,0 @@
-#
-# Copyright (C) 2017 Codethink Limited
-#
-# This program is free software; you can redistribute it and/or
-# modify it under the terms of the GNU Lesser General Public
-# License as published by the Free Software Foundation; either
-# version 2 of the License, or (at your option) any later version.
-#
-# This library is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
-# Lesser General Public License for more details.
-#
-# You should have received a copy of the GNU Lesser General Public
-# License along with this library. If not, see <http://www.gnu.org/licenses/>.
-#
-# Authors:
-# Tristan Maat <tristan.maat@codethink.co.uk>
-
-import sys
-from contextlib import contextmanager
-
-from .._exceptions import SandboxError
-from .. import utils, _signals
-
-
-# A class to wrap the `mount` and `umount` system commands
-class Mounter:
- @classmethod
- def _mount(cls, dest, src=None, mount_type=None, stdout=None, stderr=None, options=None, flags=None):
-
- if stdout is None:
- stdout = sys.stdout
- if stderr is None:
- stderr = sys.stderr
-
- argv = [utils.get_host_tool("mount")]
- if mount_type:
- argv.extend(["-t", mount_type])
- if options:
- argv.extend(["-o", options])
- if flags:
- argv.extend(flags)
-
- if src is not None:
- argv += [src]
- argv += [dest]
-
- status, _ = utils._call(argv, terminate=True, stdout=stdout, stderr=stderr)
-
- if status != 0:
- raise SandboxError("`{}` failed with exit code {}".format(" ".join(argv), status))
-
- return dest
-
- @classmethod
- def _umount(cls, path, stdout=None, stderr=None):
- if stdout is None:
- stdout = sys.stdout
- if stderr is None:
- stderr = sys.stderr
-
- cmd = [utils.get_host_tool("umount"), "-R", path]
- status, _ = utils._call(cmd, terminate=True, stdout=stdout, stderr=stderr)
-
- if status != 0:
- raise SandboxError("`{}` failed with exit code {}".format(" ".join(cmd), status))
-
- # mount()
- #
- # A wrapper for the `mount` command. The device is unmounted when
- # the context is left.
- #
- # Args:
- # dest (str) - The directory to mount to
- # src (str) - The directory to mount
- # stdout (file) - stdout
- # stderr (file) - stderr
- # mount_type (str|None) - The mount type (can be omitted or None)
- # kwargs - Arguments to pass to the mount command, such as `ro=True`
- #
- # Yields:
- # (str) The path to the destination
- #
- @classmethod
- @contextmanager
- def mount(cls, dest, src=None, stdout=None, stderr=None, mount_type=None, **kwargs):
- if stdout is None:
- stdout = sys.stdout
- if stderr is None:
- stderr = sys.stderr
-
- def kill_proc():
- cls._umount(dest, stdout, stderr)
-
- options = ",".join([key for key, val in kwargs.items() if val])
-
- path = cls._mount(dest, src, mount_type, stdout=stdout, stderr=stderr, options=options)
- try:
- with _signals.terminator(kill_proc):
- yield path
- finally:
- cls._umount(dest, stdout, stderr)
-
- # bind_mount()
- #
- # Mount a directory to a different location (a hardlink for all
- # intents and purposes). The directory is unmounted when the
- # context is left.
- #
- # Args:
- # dest (str) - The directory to mount to
- # src (str) - The directory to mount
- # stdout (file) - stdout
- # stderr (file) - stderr
- # kwargs - Arguments to pass to the mount command, such as `ro=True`
- #
- # Yields:
- # (str) The path to the destination
- #
- # While this is equivalent to `mount --rbind`, this option may not
- # exist and can be dangerous, requiring careful cleanupIt is
- # recommended to use this function over a manual mount invocation.
- #
- @classmethod
- @contextmanager
- def bind_mount(cls, dest, src=None, stdout=None, stderr=None, **kwargs):
- if stdout is None:
- stdout = sys.stdout
- if stderr is None:
- stderr = sys.stderr
-
- def kill_proc():
- cls._umount(dest, stdout, stderr)
-
- kwargs["rbind"] = True
- options = ",".join([key for key, val in kwargs.items() if val])
-
- path = cls._mount(dest, src, None, stdout, stderr, options)
-
- try:
- with _signals.terminator(kill_proc):
- # Make the rbind a slave to avoid unmounting vital devices in
- # /proc
- cls._mount(dest, flags=["--make-rslave"])
- yield path
- finally:
- cls._umount(dest, stdout, stderr)
diff --git a/tests/sandboxes/mounting/mount_simple.py b/tests/sandboxes/mounting/mount_simple.py
deleted file mode 100644
index 0e78a5603..000000000
--- a/tests/sandboxes/mounting/mount_simple.py
+++ /dev/null
@@ -1,52 +0,0 @@
-import os
-import tempfile
-from contextlib import ExitStack
-
-import pytest
-
-from buildstream.sandbox._mounter import Mounter
-
-
-@pytest.mark.skipif(not os.geteuid() == 0, reason="requires root permissions")
-def test_bind_mount():
- with ExitStack() as stack:
- src = stack.enter_context(tempfile.TemporaryDirectory())
- target = stack.enter_context(tempfile.TemporaryDirectory())
-
- with open(os.path.join(src, "test"), "a") as test:
- test.write("Test")
-
- with Mounter.bind_mount(target, src) as dest:
- # Ensure we get the correct path back
- assert dest == target
-
- # Ensure we can access files from src from target
- with open(os.path.join(target, "test"), "r") as test:
- assert test.read() == "Test"
-
- # Ensure the files from src are gone from target
- with pytest.raises(FileNotFoundError):
- with open(os.path.join(target, "test"), "r"):
- # Actual contents don't matter
- pass
-
- # Ensure the files in src are still in src
- with open(os.path.join(src, "test"), "r") as test:
- assert test.read() == "Test"
-
-
-@pytest.mark.skipif(not os.geteuid() == 0, reason="requires root permissions")
-def test_mount_proc():
- with ExitStack() as stack:
- src = "/proc"
- target = stack.enter_context(tempfile.TemporaryDirectory())
-
- with Mounter.mount(target, src, mount_type="proc", ro=True) as dest:
- # Ensure we get the correct path back
- assert dest == target
-
- # Ensure /proc is actually mounted
- assert os.listdir(src) == os.listdir(target)
-
- # Ensure /proc is unmounted correctly
- assert os.listdir(target) == []