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 execute(cls, init_params: ContainerInitParams): flags = (linux.CLONE_NEWPID | linux.CLONE_NEWNS | linux.CLONE_NEWUTS | linux.CLONE_NEWNET) # 子プロセスを起動してコンテナを実行する pid = linux.clone(ContainerExecuter().execute, flags, (init_params, )) # 子プロセスが終了するまで待つ _, status = os.waitpid(pid, 0) print('{} exited with status {}'.format(pid, status))
def run(cpu_shares, image_name, image_dir, container_dir, command): container_id = str(uuid.uuid4()) # linux.clone(callback, flags, callback_args) modeled after the Glibc version. see: "man 2 clone" pid = linux.clone(contain, linux.CLONE_NEWPID | linux.CLONE_NEWNS | linux.CLONE_NEWUTS | linux.CLONE_NEWNET, (command, image_name, image_dir, container_id, container_dir, cpu_shares)) # This is the parent, pid contains the PID of the forked process _, status = os.waitpid(pid, 0) # wait for the forked child, fetch the exit status print('{} exited with status {}'.format(pid, status))
def run(image_name, image_dir, container_dir, command): container_id = str(uuid.uuid4()) # TODO: switch to a new NET namespace # linux.clone(callback, flags, callback_args) is modeled after the Glibc # version. see: "man 2 clone" flags = linux.CLONE_NEWPID | linux.CLONE_NEWNS | linux.CLONE_NEWUTS callback_args = (command, image_name, image_dir, container_id, container_dir) pid = linux.clone(contain, flags, callback_args) # This is the parent, pid contains the PID of the forked process # wait for the forked child, fetch the exit status _, status = os.waitpid(pid, 0) print("{} exited with status {}".format(pid, status))
def run(image_name, image_dir, container_dir, command): container_id = str(uuid.uuid4()) # TODO: switch to a new NET namespace # linux.clone(callback, flags, callback_args) is modeled after the Glibc # version. see: "man 2 clone" flags = linux.CLONE_NEWPID | linux.CLONE_NEWNS | linux.CLONE_NEWUTS | linux.CLONE_NEWNET callback_args = (command, image_name, image_dir, container_id, container_dir) pid = linux.clone(contain, flags, callback_args) # This is the parent, pid contains the PID of the forked process # wait for the forked child, fetch the exit status _, status = os.waitpid(pid, 0) print('{} exited with status {}'.format(pid, status))
def run(memory, memory_swap, cpu_shares, user, image_name, image_dir, container_dir, command): container_id = str(uuid.uuid4()) # linux.clone(callback, flags, callback_args) is modeled after the Glibc # version. see: "man 2 clone" flags = (linux.CLONE_NEWPID | linux.CLONE_NEWNS | linux.CLONE_NEWUTS | linux.CLONE_NEWNET) callback_args = (command, image_name, image_dir, container_id, container_dir, cpu_shares, memory, memory_swap, user) pid = linux.clone(contain, flags, callback_args) # This is the parent, pid contains the PID of the forked process # Wait for the forked child, fetch the exit status _, status = os.waitpid(pid, 0) print('{} exited with status {}'.format(pid, status))
def test_clone_args(): import linux import ctypes def callback(val): # phrase = ctypes.c_char_p.from_buffer(val) print('Hello') print(val) valu = ctypes.cast(val, ctypes.py_object) print('Value is:', valu.value) print('Types is:', type(valu)) return 0 pid = linux.clone(callback, args=['Mehmood']) status = linux.waitpid(pid) print('{} exited with status {}'.format(pid, status)) return
def test_clone(): import linux import os import time import utils def callback(): print("here") print(os.getpid()) time.sleep(10) return 0 pid = linux.clone(callback) assert utils.is_alive(pid) status = linux.waitpid(pid) assert not utils.is_alive(pid) print('{} exited with status {}'.format(pid, status)) return
def run(image_name, image_dir, container_dir, command): container_id = str(uuid.uuid4()) # TODO: Switching to a new PID namespace (using unshare) would only affect # the children of a process (because we can't change the PID of a # running process), so we'll have to unshare here OR replace # os.fork() with linux.clone() try: pid = linux.clone(contain, linux.CLONE_NEWNS | linux.CLONE_NEWUTS | linux.CLONE_NEWPID, (command, image_name, image_dir, container_id, container_dir)) except Exception: traceback.print_exc() os._exit(1) # something went wrong in contain() # This is the parent, pid contains the PID of the forked process # wait for the forked child, fetch the exit status _, status = os.waitpid(pid, 0) print('{} exited with status {}'.format(pid, status))