-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontainerization.py
More file actions
200 lines (140 loc) · 5.6 KB
/
containerization.py
File metadata and controls
200 lines (140 loc) · 5.6 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import os
import sys
import traceback
import linux
import stat
import tarfile
import uuid
from pathlib import Path
def create_container_root():
image_directory = "/home/utsav/Projects"
files = os.listdir(image_directory)
ext_list = ['tar', 'gz', 'zip']
def intersection(lst1, lst2):
return list(set(lst1) & set(lst2))
base_images = list(filter(lambda f: intersection(
Path(f).suffixes, ext_list) != 0, files))
# Extract into another directory
file = "/home/utsav/Projects/images_tar/ubuntu-base-18.04.4-base-amd64.tar.gz"
target_path = "/home/utsav/Projects/images/ubuntu-base-18.04.4-base-amd64"
if not os.path.isdir(target_path):
os.mkdir(target_path)
# else:
# os.removedirs(target_path)
tar = tarfile.open(file)
tar.extractall(target_path)
tar.close()
return target_path
BASE_DIR_CGROUP = '/sys/fs/cgroup'
def _set_cgroup_cpu(cid):
base_dir_cpu = os.path.join(BASE_DIR_CGROUP, 'cpu')
container_dir = os.path.join(base_dir_cpu, 'docker_clone', cid)
# Put the container to the newly created cpu cgroup
if not os.path.exists(container_dir):
os.makedirs(container_dir)
task_file = os.path.join(container_dir, 'tasks')
open(task_file, 'w').write(str(os.getpid()))
cpu_shares = input('Insert memory to be allocated: ')
# set cpu_shares
if cpu_shares != 0:
file = os.path.join(container_dir, 'cpu.shares')
open(file, 'w').write(str(cpu_shares))
def _set_cgroup_memory(cid):
base_dir_mem = os.path.join(BASE_DIR_CGROUP, 'memory')
container_dir = os.path.join(base_dir_mem, 'docker_clone', cid)
# Put the container to the newly created memory cgroup
if not os.path.exists(container_dir):
os.makedirs(container_dir)
task_file = os.path.join(container_dir, 'tasks')
open(task_file, 'w').write(str(os.getpid()))
# Ask for memory
memory = input("Insert memory limit in bytes (k, m, g): ")
if memory != 0:
mem_limit_file = os.path.join(container_dir, 'memory.limit_in_bytes')
open(mem_limit_file, 'w').write(str(memory))
# print("Insert memory swap limit in bytes (k, m, g)")
# print("(Memory plus swap): ")
# memory_swap = input()
# if memory != 0:
# memswap_limit_file = os.path.join(container_dir, 'memory.memsw.limit_in_bytes')
# open(memswap_limit_file, 'w').write(str(memory_swap))
# Mount private the root mount and do it recursively to avoid umounting imp stuff like /dev/pts
def _makedev(dev_path):
pid = os.getpid() # Another way to add symlink prolly
for i, dev in enumerate(['stdin', 'stdout', 'stderr']):
# self bc a symlink to the current process
os.symlink('/proc/self/fd/%d' % i, os.path.join(dev_path, dev))
os.symlink('/proc/self/fd', os.path.join(dev_path, 'fd'))
# Add extra devices
DEVICES = {
'null': (stat.S_IFCHR, 1, 3),
'zero': (stat.S_IFCHR, 1, 5),
'random': (stat.S_IFCHR, 1, 8),
'urandom': (stat.S_IFCHR, 1, 9),
'tty': (stat.S_IFCHR, 5, 0),
'console': (stat.S_IFCHR, 136, 1),
'full': (stat.S_IFCHR, 1, 7),
}
for device, (dev_type, major, minor) in DEVICES.iteritems():
os.mknod(os.path.join(dev_path, device), 0o666 |
dev_type, os.makedev(major, minor))
def contain(cmd, cid):
_set_cgroup_cpu(cid)
_set_cgroup_memory(cid)
linux.unshare(linux.CLONE_NEWNS) # create a new mount namespace
linux.unshare(linux.CLONE_NEWUTS) # create a new uts namespace
linux.unshare(linux.CLONE_NEWNET) # create a new n/w namespace
linux.sethostname(cid)
# Use linux.clone in run() before fork and uncomment the above lines
linux.mount(None, '/', None, linux.MS_REC | linux.MS_PRIVATE, None)
new_root = create_container_root()
print("New Root created.")
# When using an already extracted image
# linux.umount(os.path.join(new_root, 'proc'))
# linux.umount(os.path.join(new_root, 'sys'))
linux.mount('proc', os.path.join(new_root, 'proc'), 'proc', 0, '')
linux.mount('sysfs', os.path.join(new_root, 'sys'), 'sysfs', 0, '')
linux.mount('tmpfs', os.path.join(new_root, 'dev'), 'tmpfs', linux.MS_STRICTATIME | linux.MS_NOSUID, 'mode=755')
# Add Basic Devices
devs = os.path.join(new_root, 'dev', 'pts')
if os.path.exists:
pass
else:
os.makedirs(devs)
linux.mount('devpts', devs, 'devpts', 0, '')
_makedev(os.path.join(new_root, 'dev'))
os.chroot(new_root)
os.chdir("/")
os.execvp(cmd[0], cmd)
def run(cmd):
cid = str(uuid.uuid4())
# linux.unshare(linux.CLONE_NEWNS) # create a new mount namespace
# linux.unshare(linux.CLONE_NEWUTS) # create a new uts namespace
# linux.unshare(linux.CLONE_NEWNET) # create a new n/w namespace
# linux.unshare(linux.CLONE_NEWPID)
# linux.sethostname(cid)
flags = linux.CLONE_NEWPID | linux.CLONE_NEWNS | linux.CLONE_NEWUTS
callback = (cmd, cid)
pid = linux.clone(contain, flags, callback)
# pid = os.fork()
# if pid == 0:
# try:
# contain(cmd, cid)
# except Exception:
# traceback.print_exc()
# os._exit(1)
# elif pid < 0:
# raise Exception("Fork Error")
_, status = os.waitpid(pid, 0)
print(str(pid) + " exited with status " + str(status))
def main():
print("*Cmds and Args*")
if len(sys.argv) <= 1:
# raise Exception("Too less arguments.")
print("Too less arguments.")
elif sys.argv[1] != "run":
print("Invalid command")
os._exit(1)
run(sys.argv[2:])
if __name__ == "__main__":
main()