# Linux Kernel Module Cheat

### 2.5. Docker host setup

This repository has been tested inside clean Docker containers.

This is a good option if you are on a Linux host, but the native setup failed due to your weird host distribution, and you have better things to do with your life than to debug it. See also: Section 38.1, “Supported hosts”.

For example, to do a QEMU Buildroot setup inside Docker, run:

    sudo apt-get install docker
    ./run-docker create && \
    ./run-docker sh -- ./build --download-dependencies qemu-buildroot
    ./run-docker

You are now left inside a shell in the Docker! From there, just run as usual:

    ./run

The host git top level directory is mounted inside the guest with a 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!

Command breakdown:

- `./run-docker create`: create the image and container.

  Needed only the very first time you use Docker, or if you run `./run-docker DESTROY` to restart for scratch, or save some disk space.

  The image and container name is `lkmc`. The container shows under:

      docker ps -a

  and the image shows under:

      docker images

- `./run-docker`: open a shell on the container.

  If it has not been started previously, start it. This can also be done explicitly with:

      ./run-docker start

  Quit the shell as usual with `Ctrl-D`

  This can be called multiple times from different host terminals to open multiple shells.

- `./run-docker stop`: stop the container.

  This might save a bit of CPU and RAM once you stop working on this project, but it should not be a lot.

- `./run-docker DESTROY`: delete the container and image.

  This doesn’t really clean the build, since we mount the guest’s working directory on the host git top-level, so you basically just got rid of the `apt-get` installs.

  To actually delete the Docker build, run on host:

      # sudo rm -rf out.docker

To use GDB step debug from inside Docker, you need a second shell inside the container. You can either do that from another shell with:

    ./run-docker

or even better, by starting a tmux session inside the container. We install `tmux` by default in the container.

You can also start a second shell and run a command in it at the same time with:

    ./run-docker sh -- ./run-gdb start_kernel

To use QEMU graphic mode from Docker, run:

    ./run --graphic --vnc

and then on host:

    sudo apt-get install vinagre
    ./vnc

TODO make files created inside Docker be owned by the current user in host instead of `root`:

- https://stackoverflow.com/questions/33681396/how-do-i-write-to-a-volume-container-as-non-root-in-docker

- https://stackoverflow.com/questions/23544282/what-is-the-best-way-to-manage-permissions-for-docker-shared-volumes

- https://stackoverflow.com/questions/31779802/shared-volume-file-permissions-ownership-docker