Gentoo Archives: gentoo-catalyst

From: Matt Turner <mattst88@g.o>
To: gentoo-catalyst@l.g.o
Cc: Matt Turner <mattst88@g.o>
Subject: [gentoo-catalyst] [PATCH 08/12] catalyst: Add and use namespace context manager
Date: Thu, 29 Oct 2020 16:16:51
Message-Id: 20201029161632.146732-8-mattst88@gentoo.org
In Reply to: [gentoo-catalyst] [PATCH 01/12] catalyst: Replace pathcompare() by Matt Turner
1 Wraps snakeoil's simple_unshare; returns to the previous namespaces on
2 context exit. Will be used by the next commit.
3
4 Signed-off-by: Matt Turner <mattst88@g.o>
5 ---
6 catalyst/context.py | 32 ++++++++++++++++++++++++++++++++
7 catalyst/main.py | 17 +++++++----------
8 2 files changed, 39 insertions(+), 10 deletions(-)
9 create mode 100644 catalyst/context.py
10
11 diff --git a/catalyst/context.py b/catalyst/context.py
12 new file mode 100644
13 index 00000000..936b5c6b
14 --- /dev/null
15 +++ b/catalyst/context.py
16 @@ -0,0 +1,32 @@
17 +
18 +import contextlib
19 +import os
20 +
21 +from snakeoil.process.namespaces import setns, simple_unshare
22 +
23 +@××××××××××.contextmanager
24 +def namespace(mount=False, uts=False, ipc=False, net=False, pid=False,
25 + user=False, hostname=None):
26 + namespaces = {
27 + (mount, "mnt"): None,
28 + (uts, "uts"): None,
29 + (ipc, "ipc"): None,
30 + (net, "net"): None,
31 + (pid, "pid"): None,
32 + (user, "user"): None,
33 + }
34 +
35 + # Save fds of current namespaces
36 + for ns in [ns for ns in namespaces if ns[0]]:
37 + fp = open(f"/proc/self/ns/{ns[1]}")
38 + namespaces[ns] = fp
39 +
40 + simple_unshare(mount=mount, uts=uts, ipc=ipc, net=net, pid=pid, user=user,
41 + hostname=hostname)
42 + try:
43 + yield None
44 + finally:
45 + for ns in [ns for ns in namespaces if ns[0]]:
46 + fp = namespaces[ns]
47 + setns(fp.fileno(), 0)
48 + fp.close()
49 diff --git a/catalyst/main.py b/catalyst/main.py
50 index 543895c6..93a4a0d3 100644
51 --- a/catalyst/main.py
52 +++ b/catalyst/main.py
53 @@ -7,14 +7,13 @@ import textwrap
54
55 import toml
56
57 -from snakeoil.process import namespaces
58 -
59 from DeComp.definitions import (COMPRESS_DEFINITIONS, DECOMPRESS_DEFINITIONS,
60 CONTENTS_DEFINITIONS)
61 from DeComp.contents import ContentsMap
62
63 from catalyst import log
64 import catalyst.config
65 +from catalyst.context import namespace
66 from catalyst.defaults import (confdefaults, option_messages,
67 DEFAULT_CONFIG_FILE, valid_config_file_values)
68 from catalyst.support import CatalystError
69 @@ -356,15 +355,13 @@ def _main(parser, opts):
70 # use pid & user namespaces, but snakeoil's namespace module has signal
71 # transfer issues (CTRL+C doesn't propagate), and user namespaces need
72 # more work due to Gentoo build process (uses sudo/root/portage).
73 - namespaces.simple_unshare(
74 - mount=True, uts=True, ipc=True, pid=False, net=False, user=False,
75 - hostname='catalyst')
76 + with namespace(mount=True, uts=True, ipc=True, hostname='catalyst'):
77 + # everything is setup, so the build is a go
78 + try:
79 + success = build_target(addlargs)
80 + except KeyboardInterrupt:
81 + log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
82
83 - # everything is setup, so the build is a go
84 - try:
85 - success = build_target(addlargs)
86 - except KeyboardInterrupt:
87 - log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
88 if not success:
89 sys.exit(2)
90 sys.exit(0)
91 --
92 2.26.2