File: chroot.py

package info (click to toggle)
drgn 0.0.33-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,892 kB
  • sloc: python: 59,081; ansic: 51,400; awk: 423; makefile: 339; sh: 113
file content (64 lines) | stat: -rw-r--r-- 1,830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright (c) 2025 Oracle and/or its affiliates
# SPDX-License-Identifier: LGPL-2.1-or-later
import argparse
import os
from pathlib import Path
import subprocess
import sys

from vmtest.config import ARCHITECTURES, HOST_ARCHITECTURE

if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="run commands in the root filesystems for vmtest",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter,
    )
    parser.add_argument(
        "-d",
        "--directory",
        metavar="DIR",
        type=Path,
        default="build/vmtest",
        help="directory for vmtest artifacts",
    )
    parser.add_argument(
        "-a",
        "--architecture",
        type=str,
        choices=sorted(ARCHITECTURES),
        default=None if HOST_ARCHITECTURE is None else HOST_ARCHITECTURE.name,
        required=HOST_ARCHITECTURE is None,
        help="architecture to run in",
    )
    parser.add_argument(
        "command",
        type=str,
        nargs=argparse.REMAINDER,
        help="command to run in rootfs (default: bash -i)",
    )
    args = parser.parse_args()
    arch = ARCHITECTURES[args.architecture]
    dir = args.directory / arch.name / "rootfs"
    command = args.command or ["bash", "-i"]
    env_passthrough = {
        "TERM",
        "COLORTERM",
    }
    filtered_env = {k: v for k, v in os.environ.items() if k in env_passthrough}
    sys.exit(
        subprocess.run(
            [
                "unshare",
                "--map-root-user",
                "--map-users=auto",
                "--map-groups=auto",
                "--fork",
                "--pid",
                f"--mount-proc={dir / 'proc'}",
                "chroot",
                dir,
                *command,
            ],
            env=filtered_env,
        ).returncode
    )