Gentoo Archives: gentoo-catalyst

From: Felix Bier <Felix.Bier@×××××××××××××.com>
To: "gentoo-catalyst@l.g.o" <gentoo-catalyst@l.g.o>
Subject: [gentoo-catalyst] [PATCH 3/5] Extend get_repo_name to handle squashed repos
Date: Thu, 04 Feb 2021 00:37:10
Message-Id: ca9e6e4827e8500bb5722753881e89117c2d8719.camel@rohde-schwarz.com
1 This commit extends the method get_repo_name to also handle
2 squashed repos. This is done by mounting the squash file to
3 a temporary directory and then extracting the repository from
4 that directory with the already existing code.
5
6 This is motivated by wanting to mount each repo
7 to e.g. /var/db/repos/<repo-name> in a later commit.
8 For squashed repos, we don't know <repo-name> without
9 mounting the repo first. For this reason, it is mounted to
10 a temporary directory first to extract <repo-name>.
11
12 Signed-off-by: Felix Bier <felix.bier@×××××××××××××.com>
13 ---
14 catalyst/support.py | 36 +++++++++++++++++++++++++++++++++++-
15 1 file changed, 35 insertions(+), 1 deletion(-)
16
17 diff --git a/catalyst/support.py b/catalyst/support.py
18 index fc50fa34..14d5a866 100644
19 --- a/catalyst/support.py
20 +++ b/catalyst/support.py
21 @@ -10,10 +10,12 @@ from subprocess import Popen
22 import libmount
23
24 from portage.repository.config import RepoConfig
25 +from tempfile import TemporaryDirectory
26
27 from snakeoil.bash import read_bash_dict
28
29 from catalyst import log
30 +from catalyst.context import namespace
31
32 BASH_BINARY = "/bin/bash"
33
34 @@ -148,7 +150,7 @@ def read_makeconf(mymakeconffile):
35 return makeconf
36
37
38 -def get_repo_name(repo_path):
39 +def get_repo_name_from_dir(repo_path):
40 """ Get the name of the repo at the given repo_path.
41
42 References:
43 @@ -164,6 +166,38 @@ def get_repo_name(repo_path):
44 return repo_config.name
45
46
47 +def get_repo_name_from_squash(repo_squash_path):
48 + """ Get the name of the repo at the given repo_squash_path.
49 + To obtain the name, the squash file is mounted to a temporary directory.
50 + """
51 +
52 + repo_name = None
53 +
54 + # Mount squash file to temp directory in separate mount namespace
55 + with TemporaryDirectory() as temp, namespace(mount=True):
56 + try:
57 + source = str(repo_squash_path)
58 + target = str(temp)
59 + fstype = 'squashfs'
60 + options = 'ro,loop'
61 + cxt = libmount.Context(source=source, target=target,
62 + fstype=fstype, options=options)
63 + cxt.mount()
64 + repo_name = get_repo_name_from_dir(target)
65 +
66 + except Exception as e:
67 + raise CatalystError(f"Couldn't mount: {source}, {e}")
68 +
69 + return repo_name
70 +
71 +
72 +def get_repo_name(repo_path):
73 + if not Path(repo_path).is_dir():
74 + return get_repo_name_from_squash(repo_path)
75 +
76 + return get_repo_name_from_dir(repo_path)
77 +
78 +
79 def ismount(path):
80 """Like os.path.ismount, but also support bind mounts"""
81 path = Path(path)
82 --
83 2.30.0