-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-container-v2.py
More file actions
60 lines (45 loc) · 1.53 KB
/
python-container-v2.py
File metadata and controls
60 lines (45 loc) · 1.53 KB
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
# A simple realization of container based on python
# - Adapted for Cgroup v2
import os
import subprocess
import sys
def run():
print(f"Running {sys.argv[2:]}")
script_path = os.path.abspath(sys.argv[0])
python_interpreter = sys.executable
cmd = ["sudo", python_interpreter, script_path, "child"] + sys.argv[2:]
subprocess.run(cmd, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
def child():
print(f"Running {sys.argv[2:]}")
cg()
cmd = sys.argv[2:]
subprocess.run(cmd, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
# Clean up
subprocess.run(["umount", "proc"])
subprocess.run(["umount", "thing"])
def set_hostname(new_hostname):
# This function will be called from within the unshare/namespace
subprocess.run(["hostnamectl", "set-hostname", new_hostname])
def cg():
cgroups = "/sys/fs/cgroup/"
pids_max_path = os.path.join(cgroups, "pids.max")
os.makedirs(pids_max_path, exist_ok=True)
with open(os.path.join(pids_max_path, "pids.max"), "w") as f:
f.write("20")
with open(os.path.join(pids_max_path, "cgroup.procs"), "w") as f:
f.write(str(os.getpid()))
def main():
if len(sys.argv) < 2:
print("Usage: sudo python script.py run <cmd> <args>")
sys.exit(1)
if sys.argv[1] == "run":
run()
elif sys.argv[1] == "child":
child()
elif sys.argv[1] == "set_hostname":
set_hostname(sys.argv[2])
else:
print("Invalid command")
sys.exit(1)
if __name__ == "__main__":
main()