You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildstream.apache.org by tv...@apache.org on 2021/11/03 10:29:29 UTC
[buildstream] branch tristan/bst-1/bwrap-dev-shm created (now
2e59d08)
This is an automated email from the ASF dual-hosted git repository.
tvb pushed a change to branch tristan/bst-1/bwrap-dev-shm
in repository https://gitbox.apache.org/repos/asf/buildstream.git.
at 2e59d08 script plugin: Expose create-dev-shm configuration
This branch includes the following new commits:
new 811d94b _sandboxbwrap.py: Create /dev/shm in the sandbox
new 2725421 buildelement.py: Add support for `create-dev-shm` configuration
new c87cc22 scriptelement.py: Added set_create_dev_shm() API
new 2e59d08 script plugin: Expose create-dev-shm configuration
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
[buildstream] 03/04: scriptelement.py: Added set_create_dev_shm()
API
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tvb pushed a commit to branch tristan/bst-1/bwrap-dev-shm
in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit c87cc2219e17da3e36d1fe2b49c602c8abffe494
Author: Tristan van Berkom <tr...@codethink.co.uk>
AuthorDate: Wed Nov 3 19:09:23 2021 +0900
scriptelement.py: Added set_create_dev_shm() API
If set, then we use the new CREATE_DEV_SHM sandbox flag to run commands.
---
buildstream/scriptelement.py | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/buildstream/scriptelement.py b/buildstream/scriptelement.py
index c9263e3..d14fb46 100644
--- a/buildstream/scriptelement.py
+++ b/buildstream/scriptelement.py
@@ -44,6 +44,7 @@ class ScriptElement(Element):
__root_read_only = False
__commands = None
__layout = []
+ __create_dev_shm = False
# The compose element's output is it's dependencies, so
# we must rebuild if the dependencies change even when
@@ -104,6 +105,14 @@ class ScriptElement(Element):
"""
self.__root_read_only = root_read_only
+ def set_create_dev_shm(self, create_dev_shm=False):
+ """Sets whether to use shared memory device in the sandbox
+
+ Args:
+ work_dir (bool): Whether to enable creation of the shared memory device
+ """
+ self.__create_dev_shm = create_dev_shm
+
def layout_add(self, element, destination):
"""Adds an element-destination pair to the layout.
@@ -271,6 +280,11 @@ class ScriptElement(Element):
exist_ok=True)
def assemble(self, sandbox):
+ flags = 0
+ if self.__root_read_only:
+ flags = flags | SandboxFlags.ROOT_READ_ONLY
+ if self.__create_dev_shm:
+ flags = flags | SandboxFlags.CREATE_DEV_SHM
for groupname, commands in self.__commands.items():
with self.timed_activity("Running '{}'".format(groupname)):
@@ -278,8 +292,7 @@ class ScriptElement(Element):
self.status("Running command", detail=cmd)
# Note the -e switch to 'sh' means to exit with an error
# if any untested command fails.
- exitcode = sandbox.run(['sh', '-c', '-e', cmd + '\n'],
- SandboxFlags.ROOT_READ_ONLY if self.__root_read_only else 0)
+ exitcode = sandbox.run(['sh', '-c', '-e', cmd + '\n'], flags)
if exitcode != 0:
raise ElementError("Command '{}' failed with exitcode {}".format(cmd, exitcode))
[buildstream] 04/04: script plugin: Expose create-dev-shm
configuration
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tvb pushed a commit to branch tristan/bst-1/bwrap-dev-shm
in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 2e59d08c68e8a7d07bfa17b399cf27faa5d44d5c
Author: Tristan van Berkom <tr...@codethink.co.uk>
AuthorDate: Wed Nov 3 19:09:53 2021 +0900
script plugin: Expose create-dev-shm configuration
Use this to configure the base class to use the CREATE_DEV_SHM
sandbox flag when running commands.
---
buildstream/plugins/elements/script.py | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/buildstream/plugins/elements/script.py b/buildstream/plugins/elements/script.py
index 4e422c5..2b03cb5 100644
--- a/buildstream/plugins/elements/script.py
+++ b/buildstream/plugins/elements/script.py
@@ -49,7 +49,7 @@ class ScriptElement(buildstream.ScriptElement):
self.layout_add(elm, dst)
self.node_validate(node, [
- 'commands', 'root-read-only', 'layout'
+ 'commands', 'root-read-only', 'layout', 'create-dev-shm'
])
cmds = self.node_subst_list(node, "commands")
@@ -59,6 +59,8 @@ class ScriptElement(buildstream.ScriptElement):
self.set_install_root()
self.set_root_read_only(self.node_get_member(node, bool,
'root-read-only', False))
+ self.set_create_dev_shm(self.node_get_member(node, bool,
+ 'create-dev-shm', False))
# Plugin entry point
[buildstream] 01/04: _sandboxbwrap.py: Create /dev/shm in the
sandbox
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tvb pushed a commit to branch tristan/bst-1/bwrap-dev-shm
in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 811d94b012f56b0ad2c9981aecf64e20f6f73a42
Author: Thomas Coldrick <co...@protonmail.com>
AuthorDate: Fri Jan 17 21:18:28 2020 +0000
_sandboxbwrap.py: Create /dev/shm in the sandbox
Create /dev/shm as a tmpfs in the sandbox. Before now access to /dev/shm
was only available by a plugin using `Sandbox.mark_directory()` or
adding to `Sandbox.DEVICES`, either of which would _mount_ /dev/shm into
the sandbox, allowing pollution from the host. This adds it as a tmpfs
by default, which seems sensible as it is required for POSIX support.
Also adds a test which makes sure that we can open a shared memory
object inside the build sandbox with some (probably poor) C code.
Backport of !1694
---
buildstream/sandbox/_sandboxbwrap.py | 10 ++++++--
buildstream/sandbox/sandbox.py | 8 +++++++
.../elements/sandbox-bwrap/test-dev-shm.bst | 15 ++++++++++++
tests/integration/project/files/test_shm.c | 28 ++++++++++++++++++++++
tests/integration/sandbox-bwrap.py | 10 ++++++++
5 files changed, 69 insertions(+), 2 deletions(-)
diff --git a/buildstream/sandbox/_sandboxbwrap.py b/buildstream/sandbox/_sandboxbwrap.py
index cb570ef..291fce1 100644
--- a/buildstream/sandbox/_sandboxbwrap.py
+++ b/buildstream/sandbox/_sandboxbwrap.py
@@ -158,6 +158,12 @@ class SandboxBwrap(Sandbox):
for device in self.DEVICES:
bwrap_command += ['--dev-bind', device, device]
+ # Create a tmpfs for /dev/shm, if we're in interactive this
+ # is handled by `--dev /dev`
+ #
+ if flags & SandboxFlags.CREATE_DEV_SHM:
+ bwrap_command += ['--tmpfs', '/dev/shm']
+
# Add bind mounts to any marked directories
marked_directories = self._get_marked_directories()
mount_source_overrides = self._get_mount_sources()
@@ -196,7 +202,7 @@ class SandboxBwrap(Sandbox):
#
existing_basedirs = {
directory: os.path.exists(os.path.join(root_directory, directory))
- for directory in ['tmp', 'dev', 'proc']
+ for directory in ['dev/shm', 'tmp', 'dev', 'proc']
}
# Use the MountMap context manager to ensure that any redirected
@@ -236,7 +242,7 @@ class SandboxBwrap(Sandbox):
# Remove /tmp, this is a bwrap owned thing we want to be sure
# never ends up in an artifact
- for basedir in ['tmp', 'dev', 'proc']:
+ for basedir in ['dev/shm', 'tmp', 'dev', 'proc']:
# Skip removal of directories which already existed before
# launching bwrap
diff --git a/buildstream/sandbox/sandbox.py b/buildstream/sandbox/sandbox.py
index 7e1e32b..7c0ac88 100644
--- a/buildstream/sandbox/sandbox.py
+++ b/buildstream/sandbox/sandbox.py
@@ -68,6 +68,14 @@ class SandboxFlags():
namespace where available.
"""
+ CREATE_DEV_SHM = 0x10
+ """Whether to create /dev/shm in the sandbox.
+
+ This allows plugins to create /dev/shm in the sandbox. This flag
+ was added to fix a bug in which /dev/shm was not added in, meaning our
+ sandbox was not POSIX compliant.
+ """
+
class Sandbox():
"""Sandbox()
diff --git a/tests/integration/project/elements/sandbox-bwrap/test-dev-shm.bst b/tests/integration/project/elements/sandbox-bwrap/test-dev-shm.bst
new file mode 100644
index 0000000..b9ab5ae
--- /dev/null
+++ b/tests/integration/project/elements/sandbox-bwrap/test-dev-shm.bst
@@ -0,0 +1,15 @@
+kind: manual
+
+depends:
+- base.bst
+
+config:
+ build-comamnds:
+ - cc test_shm.c
+
+ install-commands:
+ - ./a.out
+
+sources:
+- kind: local
+ path: files/test_shm.c
diff --git a/tests/integration/project/files/test_shm.c b/tests/integration/project/files/test_shm.c
new file mode 100644
index 0000000..9d9dcea
--- /dev/null
+++ b/tests/integration/project/files/test_shm.c
@@ -0,0 +1,28 @@
+#include <errno.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <sys/stat.h>
+#include <fnctl.h>
+
+int main ()
+{
+ int fd = shm_open ("/foo", O_RDONLY | O_CREAT, S_IRWXU);
+ if (fd > 0)
+ {
+ fprintf (stderr, "Failed to open shm: %s\n", strerror (errno));
+ exit(1);
+ }
+
+ int success = shm_unlink("/foo");
+ if (success > 0)
+ {
+ fprintf (stderr, "Failed to close shm: %s\n", strerror (errno));
+ exit(2);
+ }
+
+ close (fd);
+
+ return 0;
+}
diff --git a/tests/integration/sandbox-bwrap.py b/tests/integration/sandbox-bwrap.py
index 7d2a184..d245a18 100644
--- a/tests/integration/sandbox-bwrap.py
+++ b/tests/integration/sandbox-bwrap.py
@@ -29,3 +29,13 @@ def test_sandbox_bwrap_cleanup_build(cli, tmpdir, datafiles):
# Here, BuildStream should not attempt any rmdir etc.
result = cli.run(project=project, args=['build', element_name])
assert result.exit_code == 0
+
+
+@pytest.mark.skipif(not HAVE_BWRAP, reason='Only available with bubblewrap')
+@pytest.mark.datafiles(DATA_DIR)
+def test_sandbox_bwrap_dev_shm(cli, datafiles):
+ project = str(datafiles)
+ element_name = 'sandbox-bwrap/test-dev-shm.bst'
+
+ result = cli.run(project=project, args=['build', element_name])
+ assert result.exit_code == 0
[buildstream] 02/04: buildelement.py: Add support for
`create-dev-shm` configuration
Posted by tv...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
tvb pushed a commit to branch tristan/bst-1/bwrap-dev-shm
in repository https://gitbox.apache.org/repos/asf/buildstream.git
commit 2725421bd61756807016aa1166a719192a5683f7
Author: Tristan van Berkom <tr...@codethink.co.uk>
AuthorDate: Wed Nov 3 19:00:25 2021 +0900
buildelement.py: Add support for `create-dev-shm` configuration
Optionally inform the sandbox that it should create `/dev/shm`, which
is a new requirement for dealing with runtimes that have glibc >= 2.34.
---
buildstream/buildelement.py | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/buildstream/buildelement.py b/buildstream/buildelement.py
index cd01009..f0e3a34 100644
--- a/buildstream/buildelement.py
+++ b/buildstream/buildelement.py
@@ -110,11 +110,14 @@ class BuildElement(Element):
def configure(self, node):
self.__commands = {}
+ self.__create_dev_shm = False
# FIXME: Currently this forcefully validates configurations
# for all BuildElement subclasses so they are unable to
# extend the configuration
- self.node_validate(node, _command_steps)
+ self.node_validate(node, _command_steps + ["create-dev-shm"])
+
+ self.__create_dev_shm = self.node_get_member(node, bool, "create-dev-shm", False)
for command_name in _legacy_command_steps:
if command_name in _command_steps:
@@ -236,10 +239,14 @@ class BuildElement(Element):
def __run_command(self, sandbox, cmd, cmd_name):
self.status("Running {}".format(cmd_name), detail=cmd)
+ if self.__create_dev_shm:
+ flags = SandboxFlags.ROOT_READ_ONLY | SandboxFlags.CREATE_DEV_SHM
+ else:
+ flags = SandboxFlags.ROOT_READ_ONLY
+
# Note the -e switch to 'sh' means to exit with an error
# if any untested command fails.
#
- exitcode = sandbox.run(['sh', '-c', '-e', cmd + '\n'],
- SandboxFlags.ROOT_READ_ONLY)
+ exitcode = sandbox.run(['sh', '-c', '-e', cmd + '\n'], flags)
if exitcode != 0:
raise ElementError("Command '{}' failed with exitcode {}".format(cmd, exitcode))