diff --git a/README.adoc b/README.adoc index 732090e..ee07fd7 100644 --- a/README.adoc +++ b/README.adoc @@ -418,7 +418,7 @@ docker ps -a .... * `./run-docker start`: start the container as daemon on the background. + -Needed only after reboot, or if you call `stop` to save CPU or memory resources. +Needed only after reboot, or if you run `./run-docker stop` to save CPU or memory resources. + The container can now be seen on the list of running containers: + @@ -427,14 +427,12 @@ docker ps .... * `./run-docker sh`: open a shell on a previously started Docker daemon. + -Quit the shell as usual with `Ctrl` +Quit the shell as usual with `Ctrl-D` + -Can be called multiple times to open multiple shells. +Can be called multiple times from different host terminals to open multiple shells. The host git top level directory is mounted inside the guest with a link:https://stackoverflow.com/questions/23439126/how-to-mount-a-host-directory-in-a-docker-container[Docker volume], which means for example that you can use your host's GUI text editor directly on the files. Just don't forget that if you nuke that directory on the guest, then it gets nuked on the host as well! -TODO make files created inside Docker be owned by the current user in host instead of `root`: https://stackoverflow.com/questions/23544282/what-is-the-best-way-to-manage-permissions-for-docker-shared-volumes - In order to use functionality such as <> from inside Docker, you need a second shell inside the container. You can either do that from another shell with: .... @@ -478,6 +476,8 @@ To actually delete the Docker build, run: # sudo rm -rf out.docker .... +TODO make files created inside Docker be owned by the current user in host instead of `root`: https://stackoverflow.com/questions/23544282/what-is-the-best-way-to-manage-permissions-for-docker-shared-volumes + [[prebuilt]] === Prebuilt Buildroot setup diff --git a/build b/build index 1a16b17..5c664f5 100755 --- a/build +++ b/build @@ -368,17 +368,18 @@ if args.download_dependencies: } apt_get_pkgs.difference_update(interacive_pkgs) if common.in_docker: - sudo = ['sudo'] + sudo = [] # https://askubuntu.com/questions/909277/avoiding-user-interaction-with-tzdata-when-installing-certbot-in-a-docker-contai os.environ['DEBIAN_FRONTEND'] = 'noninteractive' # https://askubuntu.com/questions/496549/error-you-must-put-some-source-uris-in-your-sources-list - with open(os.path.join('/etc', 'apt', 'sources.list'), 'r') as f: + sources_path = os.path.join('/etc', 'apt', 'sources.list') + with open(sources_path, 'r') as f: sources_txt = f.read() - sources_txt = re.sub('^# deb-src ' 'deb-src ', sources_txt) - with open(os.path.join('/etc', 'apt', 'sources.list'), 'w') as f: + sources_txt = re.sub('^# deb-src ', 'deb-src ', sources_txt, flags=re.MULTILINE) + with open(sources_path, 'w') as f: f.write(sources_txt) else: - sudo = [] + sudo = ['sudo'] if common.in_docker or args.travis: y = ['-y'] else: diff --git a/common.py b/common.py index fde2f77..55a2110 100644 --- a/common.py +++ b/common.py @@ -24,6 +24,7 @@ import urllib import urllib.request this_module = sys.modules[__name__] +repo_short_id = 'lkmc' # https://stackoverflow.com/questions/20010199/how-to-determine-if-a-process-runs-inside-lxc-docker in_docker = os.path.exists('/.dockerenv') root_dir = os.path.dirname(os.path.abspath(__file__)) diff --git a/run-docker b/run-docker index adb1c6e..4b98aed 100755 --- a/run-docker +++ b/run-docker @@ -1,26 +1,67 @@ -#!/usr/bin/env bash -set -eu -cmd="$1" -shift -container_name=lkmc -target_dir=/root/linux-kernel-module-cheat -if [ "$cmd" = create ]; then - # --privileged for KVM: - # https://stackoverflow.com/questions/48422001/launching-qemu-kvm-from-inside-docker-container - sudo docker create --name "$container_name" --net host -i --privileged -t -w "${target_dir}" -v "$(pwd):${target_dir}" ubuntu:18.04 bash -elif [ "$cmd" = start ]; then - sudo docker start "$container_name" -elif [ "$cmd" = stop ]; then - sudo docker stop "$container_name" -elif [ "$cmd" = sh ]; then - if [ "$#" -gt 0 ]; then - c=-c - fi - # https://stackoverflow.com/questions/39794509/how-to-open-multiple-terminals-in-docker - sudo docker exec -it "$container_name" bash $c "$*" -elif [ "$cmd" = DESTROY ]; then - sudo docker rm "$container_name" -else - echo "error: unknown action: ${cmd}" 1>&2 - exit 2 -fi +#!/usr/bin/env python3 + +import argparse +import os + +import common + +container_name = common.repo_short_id +container_hostname = common.repo_short_id +image_name = common.repo_short_id +target_dir = '/root/{}'.format(common.repo_short_id) +docker = ['sudo', 'docker'] +def sh(args): + if args: + sh_args = ['-c'] + args + else: + sh_args = [] + common.run_cmd( + docker + + [ + 'exec', + '-i', + '-t', + container_name, + 'bash', + ] + + sh_args + + [common.Newline], + ) +cmd_action_map = { + 'create': lambda args: + # --privileged for KVM: + # https://stackoverflow.com/questions/48422001/launching-qemu-kvm-from-inside-docker-container + common.run_cmd( + docker + + [ + 'create', common.Newline, + '--hostname', container_hostname, common.Newline, + '-i', common.Newline, + '--name', container_name, common.Newline, + '--net', 'host', common.Newline, + '--privileged', common.Newline, + '-t', common.Newline, + '-w', target_dir, common.Newline, + '-v', '{}:{}'.format(os.getcwd(), target_dir), common.Newline, + 'ubuntu:18.04', common.Newline, + 'bash', common.Newline, + ] + ), + 'start': lambda args: + common.run_cmd(docker + [ 'start', container_name, common.Newline]) + , + 'stop': lambda args: + common.run_cmd(docker + ['stop', container_name, common.Newline]) + , + 'sh': lambda args: sh(args), + 'DESTROY': lambda args: + common.run_cmd(docker + [ 'rm', container_name, common.Newline]) + , +} +parser = argparse.ArgumentParser() +parser.add_argument('cmd', choices=cmd_action_map) +parser.add_argument('args', nargs='*') +common.add_dry_run_argument(parser) +args = parser.parse_args() +common.setup_dry_run_arguments(args) +cmd_action_map[args.cmd](args.args)