1 |
commit: e042cf37bcb4f4c4e7c1665f69faf971fa8177e8 |
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 14:24:15 2020 +0000 |
6 |
URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=e042cf37 |
7 |
|
8 |
catalyst: Add and use namespace context manager |
9 |
|
10 |
Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org> |
11 |
|
12 |
catalyst/base/stagebase.py | 6 ++++-- |
13 |
catalyst/context.py | 33 +++++++++++++++++++++++++++++++++ |
14 |
catalyst/main.py | 17 +++++++---------- |
15 |
3 files changed, 44 insertions(+), 12 deletions(-) |
16 |
|
17 |
diff --git a/catalyst/base/stagebase.py b/catalyst/base/stagebase.py |
18 |
index da133bf2..2bbbb987 100644 |
19 |
--- a/catalyst/base/stagebase.py |
20 |
+++ b/catalyst/base/stagebase.py |
21 |
@@ -14,6 +14,7 @@ from snakeoil.osutils import pjoin |
22 |
from DeComp.compress import CompressMap |
23 |
|
24 |
from catalyst import log |
25 |
+from catalyst.context import namespace |
26 |
from catalyst.defaults import (confdefaults, MOUNT_DEFAULTS, PORT_LOGDIR_CLEAN) |
27 |
from catalyst.support import (CatalystError, file_locate, normpath, |
28 |
cmd, read_makeconf, ismount, file_check, |
29 |
@@ -1392,8 +1393,9 @@ class StageBase(TargetBase, ClearBase, GenBase): |
30 |
if not self.run_sequence(self.prepare_sequence): |
31 |
return False |
32 |
|
33 |
- if not self.run_sequence(self.build_sequence): |
34 |
- return False |
35 |
+ with namespace(mount=True): |
36 |
+ if not self.run_sequence(self.build_sequence): |
37 |
+ return False |
38 |
|
39 |
if not self.run_sequence(self.finish_sequence): |
40 |
return False |
41 |
|
42 |
diff --git a/catalyst/context.py b/catalyst/context.py |
43 |
new file mode 100644 |
44 |
index 00000000..b53be56e |
45 |
--- /dev/null |
46 |
+++ b/catalyst/context.py |
47 |
@@ -0,0 +1,33 @@ |
48 |
+ |
49 |
+import contextlib |
50 |
+import os |
51 |
+ |
52 |
+from snakeoil.process.namespaces import setns, simple_unshare |
53 |
+ |
54 |
+@××××××××××.contextmanager |
55 |
+def namespace(mount=False, uts=False, ipc=False, net=False, pid=False, |
56 |
+ user=False, hostname=None): |
57 |
+ namespaces = { |
58 |
+ (mount, "mnt"): None, |
59 |
+ (uts, "uts"): None, |
60 |
+ (ipc, "ipc"): None, |
61 |
+ (net, "net"): None, |
62 |
+ (pid, "pid"): None, |
63 |
+ (user, "user"): None, |
64 |
+ } |
65 |
+ process_id = os.getpid() |
66 |
+ |
67 |
+ # Save fds of current namespaces |
68 |
+ for ns in [ns for ns in namespaces if ns[0]]: |
69 |
+ fp = open(f"/proc/{process_id}/ns/{ns[1]}") |
70 |
+ namespaces[ns] = fp |
71 |
+ |
72 |
+ simple_unshare(mount=mount, uts=uts, ipc=ipc, net=net, pid=pid, user=user, |
73 |
+ hostname=hostname) |
74 |
+ try: |
75 |
+ yield None |
76 |
+ finally: |
77 |
+ for ns in [ns for ns in namespaces if ns[0]]: |
78 |
+ fp = namespaces[ns] |
79 |
+ setns(fp.fileno(), 0) |
80 |
+ fp.close() |
81 |
|
82 |
diff --git a/catalyst/main.py b/catalyst/main.py |
83 |
index 543895c6..93a4a0d3 100644 |
84 |
--- a/catalyst/main.py |
85 |
+++ b/catalyst/main.py |
86 |
@@ -7,14 +7,13 @@ import textwrap |
87 |
|
88 |
import toml |
89 |
|
90 |
-from snakeoil.process import namespaces |
91 |
- |
92 |
from DeComp.definitions import (COMPRESS_DEFINITIONS, DECOMPRESS_DEFINITIONS, |
93 |
CONTENTS_DEFINITIONS) |
94 |
from DeComp.contents import ContentsMap |
95 |
|
96 |
from catalyst import log |
97 |
import catalyst.config |
98 |
+from catalyst.context import namespace |
99 |
from catalyst.defaults import (confdefaults, option_messages, |
100 |
DEFAULT_CONFIG_FILE, valid_config_file_values) |
101 |
from catalyst.support import CatalystError |
102 |
@@ -356,15 +355,13 @@ def _main(parser, opts): |
103 |
# use pid & user namespaces, but snakeoil's namespace module has signal |
104 |
# transfer issues (CTRL+C doesn't propagate), and user namespaces need |
105 |
# more work due to Gentoo build process (uses sudo/root/portage). |
106 |
- namespaces.simple_unshare( |
107 |
- mount=True, uts=True, ipc=True, pid=False, net=False, user=False, |
108 |
- hostname='catalyst') |
109 |
+ with namespace(mount=True, uts=True, ipc=True, hostname='catalyst'): |
110 |
+ # everything is setup, so the build is a go |
111 |
+ try: |
112 |
+ success = build_target(addlargs) |
113 |
+ except KeyboardInterrupt: |
114 |
+ log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)') |
115 |
|
116 |
- # everything is setup, so the build is a go |
117 |
- try: |
118 |
- success = build_target(addlargs) |
119 |
- except KeyboardInterrupt: |
120 |
- log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)') |
121 |
if not success: |
122 |
sys.exit(2) |
123 |
sys.exit(0) |