mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2025-07-29 11:52:58 +00:00
70 lines
2.0 KiB
Python
Executable File
70 lines
2.0 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
|
|
import common
|
|
import shell_helpers
|
|
from shell_helpers import LF
|
|
|
|
container_name = common.consts['repo_short_id']
|
|
container_hostname = common.consts['repo_short_id']
|
|
image_name = common.consts['repo_short_id']
|
|
target_dir = '/root/{}'.format(common.consts['repo_short_id'])
|
|
docker = ['sudo', 'docker']
|
|
def create(args):
|
|
sh.run_cmd(docker + ['build', '-t', image_name, '.', LF])
|
|
# --privileged for KVM:
|
|
# https://stackoverflow.com/questions/48422001/launching-qemu-kvm-from-inside-docker-container
|
|
sh.run_cmd(
|
|
docker +
|
|
[
|
|
'create', LF,
|
|
'--hostname', container_hostname, LF,
|
|
'-i', LF,
|
|
'--name', container_name, LF,
|
|
'--net', 'host', LF,
|
|
'--privileged', LF,
|
|
'-t', LF,
|
|
'-w', target_dir, LF,
|
|
'-v', '{}:{}'.format(os.getcwd(), target_dir), LF,
|
|
image_name,
|
|
]
|
|
)
|
|
def destroy(args):
|
|
stop(args)
|
|
sh.run_cmd(docker + ['rm', container_name, LF])
|
|
sh.run_cmd(docker + ['rmi', image_name, LF])
|
|
def sh_func(args):
|
|
start(args)
|
|
if args:
|
|
sh_args = args
|
|
else:
|
|
sh_args = ['bash']
|
|
exit_status = sh.run_cmd(
|
|
docker + ['exec', '-i', '-t', container_name] +
|
|
sh_args +
|
|
[LF],
|
|
raise_on_failure=False
|
|
)
|
|
sys.exit(exit_status)
|
|
def start(args):
|
|
sh.run_cmd(docker + ['start', container_name, LF])
|
|
def stop(args):
|
|
sh.run_cmd(docker + ['stop', container_name, LF])
|
|
cmd_action_map = {
|
|
'create': lambda args: create(args),
|
|
'DESTROY': lambda args: destroy(args),
|
|
'sh': lambda args: sh_func(args),
|
|
'start': lambda args: start(args),
|
|
'stop': lambda args: stop(args),
|
|
}
|
|
parser = argparse.ArgumentParser()
|
|
parser.add_argument('--dry-run', default=False, action='store_true')
|
|
parser.add_argument('cmd', choices=cmd_action_map, default='sh', nargs='?')
|
|
parser.add_argument('args', nargs='*')
|
|
args = parser.parse_args()
|
|
sh = shell_helpers.ShellHelpers(dry_run=args.dry_run)
|
|
cmd_action_map[args.cmd](args.args)
|