Gentoo Archives: gentoo-commits

From: Matt Turner <mattst88@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/catalyst:pending/mattst88 commit in: catalyst/
Date: Wed, 28 Oct 2020 23:06:22
Message-Id: 1603926369.e1be09eedea2dba8c605a49d9211c868c2ee4dcc.mattst88@gentoo
1 commit: e1be09eedea2dba8c605a49d9211c868c2ee4dcc
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: Wed Oct 28 23:06:09 2020 +0000
6 URL: https://gitweb.gentoo.org/proj/catalyst.git/commit/?id=e1be09ee
7
8 catalyst: ...
9
10 Signed-off-by: Matt Turner <mattst88 <AT> gentoo.org>
11
12 catalyst/main.py | 59 +++++++++++++++++++++++++++++++++++++++++++++++---------
13 1 file changed, 50 insertions(+), 9 deletions(-)
14
15 diff --git a/catalyst/main.py b/catalyst/main.py
16 index 543895c6..8f54ba89 100644
17 --- a/catalyst/main.py
18 +++ b/catalyst/main.py
19 @@ -1,4 +1,5 @@
20 import argparse
21 +import contextlib
22 import datetime
23 import hashlib
24 import os
25 @@ -7,7 +8,7 @@ import textwrap
26
27 import toml
28
29 -from snakeoil.process import namespaces
30 +from snakeoil.process.namespaces import setns, simple_unshare
31
32 from DeComp.definitions import (COMPRESS_DEFINITIONS, DECOMPRESS_DEFINITIONS,
33 CONTENTS_DEFINITIONS)
34 @@ -22,6 +23,33 @@ from catalyst.version import get_version
35
36 conf_values = confdefaults
37
38 +@××××××××××.contextmanager
39 +def namespace(mount=True, uts=True, ipc=True, net=False, pid=False,
40 + user=False, hostname=None):
41 + namespaces = {
42 + (mount, "mnt"): None,
43 + (uts, "uts"): None,
44 + (ipc, "ipc"): None,
45 + (net, "net"): None,
46 + (pid, "pid"): None,
47 + (user, "user"): None,
48 + }
49 + pid = os.getpid()
50 +
51 + # Save fds of current namespaces
52 + for ns in [ns for ns in namespaces if ns[0]]:
53 + fp = open(f"/proc/{pid}/ns/{ns[1]}")
54 + namespaces[ns] = fp
55 +
56 + simple_unshare(mount=mount, uts=uts, ipc=ipc, net=net, pid=pid, user=user,
57 + hostname=hostname)
58 + try:
59 + yield None
60 + finally:
61 + for ns in [ns for ns in namespaces if ns[0]]:
62 + fp = namespaces[ns]
63 + setns(fp.fileno(), 0)
64 + fp.close()
65
66 def version():
67 log.info(get_version())
68 @@ -352,19 +380,32 @@ def _main(parser, opts):
69 # catalyst cannot be run as a normal user due to chroots, mounts, etc
70 log.critical('This script requires root privileges to operate')
71
72 + cxt = libmount.Context()
73 + print("Before")
74 + while (fs := cxt.mtab.next_fs()) is not None:
75 + print(fs.target)
76 +
77 # Start off by creating unique namespaces to run in. Would be nice to
78 # use pid & user namespaces, but snakeoil's namespace module has signal
79 # transfer issues (CTRL+C doesn't propagate), and user namespaces need
80 # more work due to Gentoo build process (uses sudo/root/portage).
81 - namespaces.simple_unshare(
82 - mount=True, uts=True, ipc=True, pid=False, net=False, user=False,
83 - hostname='catalyst')
84 + with namespace(mount=True, uts=True, ipc=True, net=False, pid=False,
85 + user=False, hostname='catalyst'):
86 + # everything is setup, so the build is a go
87 + try:
88 + success = build_target(addlargs)
89 + cxt = libmount.Context()
90 + print("During")
91 + while (fs := cxt.mtab.next_fs()) is not None:
92 + print(fs.target)
93 + except KeyboardInterrupt:
94 + log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
95 +
96 + cxt = libmount.Context()
97 + print("After")
98 + while (fs := cxt.mtab.next_fs()) is not None:
99 + print(fs.target)
100
101 - # everything is setup, so the build is a go
102 - try:
103 - success = build_target(addlargs)
104 - except KeyboardInterrupt:
105 - log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
106 if not success:
107 sys.exit(2)
108 sys.exit(0)