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