Skip to content

Commit

Permalink
Don't rely on systemd to run minimega components
Browse files Browse the repository at this point in the history
  • Loading branch information
activeshadow committed Feb 14, 2022
1 parent c80bd73 commit 39931de
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 60 deletions.
18 changes: 9 additions & 9 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ COPY ./src ./src
RUN ./build.bash


FROM jrei/systemd-ubuntu:20.04
FROM ubuntu:20.04

RUN apt update && apt install -y \
dnsmasq iproute2 isc-dhcp-client \
Expand All @@ -31,19 +31,19 @@ COPY --from=gobuilder /minimega/bin/miniweb /opt/minimega/bin/miniweb
COPY --from=gobuilder /minimega/bin/miniccc /opt/minimega/bin/miniccc
COPY --from=gobuilder /minimega/bin/miniccc.exe /opt/minimega/bin/miniccc.exe

# For the sake of consistency, let's go ahead and include protonuke in the image
# too so we can easily grab a copy if/when necessary.
# For the sake of consistency, let's go ahead and include protonuke and
# minirouter in the image too so we can easily grab a copy if/when necessary.
COPY --from=gobuilder /minimega/bin/protonuke /opt/minimega/bin/protonuke
COPY --from=gobuilder /minimega/bin/protonuke.exe /opt/minimega/bin/protonuke.exe
COPY --from=gobuilder /minimega/bin/minirouter /opt/minimega/bin/minirouter

COPY ./misc/web /opt/minimega/misc/web

ADD docker/minimega.service /etc/systemd/system/minimega.service
ADD docker/miniweb.service /etc/systemd/system/miniweb.service
COPY ./docker/start-minimega.sh /start-minimega.sh

WORKDIR /etc/systemd/system/multi-user.target.wants

RUN ln -s ../minimega.service \
&& ln -s ../miniweb.service
RUN chmod +x /usr/local/bin/mm \
&& chmod +x /start-minimega.sh

WORKDIR /opt/minimega

CMD ["/start-minimega.sh"]
41 changes: 35 additions & 6 deletions docker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,25 @@

### Build the minimega docker image

> NOTE: Currently, only minimega, miniweb, miniccc, and protonuke will exist in the minimega docker image. If you need additional binaries, add them to the Dockerfile using the `COPY --from=gobuilder …` directive.
> NOTE: Currently, only minimega, miniweb, miniccc, and protonuke will exist in
> the minimega docker image. If you need additional binaries, add them to the
> Dockerfile using the `COPY --from=gobuilder …` directive.

> NOTE: The docker image needs to be built from the base directory of the minimega repository.
> NOTE: The docker image needs to be built from the base directory of the
> minimega repository.

```bash
$ docker build -t minimega -f docker/Dockerfile .
```

### Start the minimega docker container

> NOTE: The additional privileges and system mounts (e.g. /dev) are required for the openvswitch process to run inside the container and to allow minimega to perform file injections.
> NOTE: The additional privileges and system mounts (e.g. /dev) are required for
> the openvswitch process to run inside the container and to allow minimega to
> perform file injections.

```bash
docker run -d -it \
docker run -d \
--name minimega \
--hostname minimega \
--privileged \
Expand All @@ -33,11 +38,13 @@ docker run -d -it \
-v /dev:/dev \
-v /lib/modules:/lib/modules:ro \
-v /sys/fs/cgroup:/sys/fs/cgroup:ro \
--health-cmd "minimega -e version" \
--health-cmd "mm version" \
minimega
```

The container runs systemd as PID 1, which takes care of starting openvswitch, minimega, and miniweb.
The container runs the `start-minimega.sh` script as PID 1, which takes care of
starting openvswitch, miniweb, and finally minimega. This means the minimega
logs will be available in the container logs via Docker.

---

Expand Down Expand Up @@ -75,3 +82,25 @@ $ source ~/.bash_aliases

miniweb gets started in the container automatically.

### minimega configuration

By default, the following values are set for minimega:

```
MM_BASE=/tmp/minimega
MM_FILEPATH=/tmp/minimega/files
MM_BROADCAST=255.255.255.255
MM_PORT=9000
MM_DEGREE=2
MM_CONTEXT=minimega
MM_LOGLEVEL=info
MM_LOGFILE=/var/log/minimega.log
```
These values can be overwritten either by passing environment variables to
Docker when starting the container or by binding a file to
`/etc/default/minimega` in the container that contains updated values.
> NOTE: If a value is specified both as an environment variable to Docker and in
> the file bound to `/etc/default/minimega`, the value in
> `/etc/default/minimega` will be used.
8 changes: 5 additions & 3 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
version: '3.7'
services:
minimega:
build:
context: ../
dockerfile: docker/Dockerfile
image: minimega
container_name: minimega
hostname: minimega
Expand All @@ -19,10 +22,9 @@ services:
volumes:
- /dev:/dev
- /lib/modules:/lib/modules:ro
- /sys/fs/cgroup:/sys/fs/cgroup:ro
- /etc/localtime:/etc/localtime:ro
- /tmp/minimega:/tmp/minimega
- /var/log/minimega:/var/log/minimega
- /root/.ssh:/root/.ssh:ro
- /phenix:/phenix
healthcheck:
test: minimega -e version
test: mm version
30 changes: 0 additions & 30 deletions docker/minimega.service

This file was deleted.

12 changes: 0 additions & 12 deletions docker/miniweb.service

This file was deleted.

28 changes: 28 additions & 0 deletions docker/start-minimega.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/bin/bash

/usr/share/openvswitch/scripts/ovs-ctl start

/opt/minimega/bin/miniweb -root=/opt/minimega/misc/web -addr=0.0.0.0:9001 &

: "${MM_BASE:=/tmp/minimega}"
: "${MM_FILEPATH:=/tmp/minimega/files}"
: "${MM_BROADCAST:=255.255.255.255}"
: "${MM_PORT:=9000}"
: "${MM_DEGREE:=2}"
: "${MM_CONTEXT:=minimega}"
: "${MM_LOGLEVEL:=info}"
: "${MM_LOGFILE:=/var/log/minimega.log}"

[[ -f "/etc/default/minimega" ]] && source "/etc/default/minimega"

/opt/minimega/bin/minimega \
-force \
-nostdin \
-base=${MM_BASE} \
-filepath=${MM_FILEPATH} \
-broadcast=${MM_BROADCAST} \
-port=${MM_PORT} \
-degree=${MM_DEGREE} \
-context=${MM_CONTEXT} \
-level=${MM_LOGLEVEL} \
-logfile=${MM_LOGFILE}

0 comments on commit 39931de

Please sign in to comment.