From: Matt Turner <mattst88@gentoo.org>
To: gentoo-catalyst@lists.gentoo.org
Cc: Matt Turner <mattst88@gentoo.org>
Subject: [gentoo-catalyst] [PATCH 08/12] catalyst: Add and use namespace context manager
Date: Thu, 29 Oct 2020 12:16:28 -0400 [thread overview]
Message-ID: <20201029161632.146732-8-mattst88@gentoo.org> (raw)
In-Reply-To: <20201029161632.146732-1-mattst88@gentoo.org>
Wraps snakeoil's simple_unshare; returns to the previous namespaces on
context exit. Will be used by the next commit.
Signed-off-by: Matt Turner <mattst88@gentoo.org>
---
catalyst/context.py | 32 ++++++++++++++++++++++++++++++++
catalyst/main.py | 17 +++++++----------
2 files changed, 39 insertions(+), 10 deletions(-)
create mode 100644 catalyst/context.py
diff --git a/catalyst/context.py b/catalyst/context.py
new file mode 100644
index 00000000..936b5c6b
--- /dev/null
+++ b/catalyst/context.py
@@ -0,0 +1,32 @@
+
+import contextlib
+import os
+
+from snakeoil.process.namespaces import setns, simple_unshare
+
+@contextlib.contextmanager
+def namespace(mount=False, uts=False, ipc=False, net=False, pid=False,
+ user=False, hostname=None):
+ namespaces = {
+ (mount, "mnt"): None,
+ (uts, "uts"): None,
+ (ipc, "ipc"): None,
+ (net, "net"): None,
+ (pid, "pid"): None,
+ (user, "user"): None,
+ }
+
+ # Save fds of current namespaces
+ for ns in [ns for ns in namespaces if ns[0]]:
+ fp = open(f"/proc/self/ns/{ns[1]}")
+ namespaces[ns] = fp
+
+ simple_unshare(mount=mount, uts=uts, ipc=ipc, net=net, pid=pid, user=user,
+ hostname=hostname)
+ try:
+ yield None
+ finally:
+ for ns in [ns for ns in namespaces if ns[0]]:
+ fp = namespaces[ns]
+ setns(fp.fileno(), 0)
+ fp.close()
diff --git a/catalyst/main.py b/catalyst/main.py
index 543895c6..93a4a0d3 100644
--- a/catalyst/main.py
+++ b/catalyst/main.py
@@ -7,14 +7,13 @@ import textwrap
import toml
-from snakeoil.process import namespaces
-
from DeComp.definitions import (COMPRESS_DEFINITIONS, DECOMPRESS_DEFINITIONS,
CONTENTS_DEFINITIONS)
from DeComp.contents import ContentsMap
from catalyst import log
import catalyst.config
+from catalyst.context import namespace
from catalyst.defaults import (confdefaults, option_messages,
DEFAULT_CONFIG_FILE, valid_config_file_values)
from catalyst.support import CatalystError
@@ -356,15 +355,13 @@ def _main(parser, opts):
# use pid & user namespaces, but snakeoil's namespace module has signal
# transfer issues (CTRL+C doesn't propagate), and user namespaces need
# more work due to Gentoo build process (uses sudo/root/portage).
- namespaces.simple_unshare(
- mount=True, uts=True, ipc=True, pid=False, net=False, user=False,
- hostname='catalyst')
+ with namespace(mount=True, uts=True, ipc=True, hostname='catalyst'):
+ # everything is setup, so the build is a go
+ try:
+ success = build_target(addlargs)
+ except KeyboardInterrupt:
+ log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
- # everything is setup, so the build is a go
- try:
- success = build_target(addlargs)
- except KeyboardInterrupt:
- log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
if not success:
sys.exit(2)
sys.exit(0)
--
2.26.2
next prev parent reply other threads:[~2020-10-29 16:16 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-29 16:16 [gentoo-catalyst] [PATCH 01/12] catalyst: Replace pathcompare() Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 02/12] catalyst: Rewrite ismount() to use libmount Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 03/12] catalyst: Use libmount for handling mounts Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 04/12] catalyst: Move action_sequence out of self.settings[] Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 05/12] catalyst: Use .extend() and .append() for action_sequence Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 06/12] catalyst: Split action_sequence into prepare/build/finish Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 07/12] catalyst: Factor out run_sequence() Matt Turner
2020-10-29 16:16 ` Matt Turner [this message]
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 09/12] catalyst: Run the build sequence in new mount namespace Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 10/12] catalyst: Remove kill_support_pids() Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 11/12] catalyst: Remove mount_safety_check() Matt Turner
2020-10-29 16:16 ` [gentoo-catalyst] [PATCH 12/12] catalyst: Drop unbind() Matt Turner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20201029161632.146732-8-mattst88@gentoo.org \
--to=mattst88@gentoo.org \
--cc=gentoo-catalyst@lists.gentoo.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox