From afb81e05605603f15e0bc223b4c5dbc690ad8e49 Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Tue, 17 Nov 2026 15:54:24 +0100 Subject: Use `container` and not `container machine` Move back to using plain containers, as they have stronger security guarantees (`container machine` forwards SSH socket for example). --- Makefile | 1 + README.md | 2 +- dbx | 166 +++++++++++++++++++++++++++++++++++++++++--------------------- 3 files changed, 113 insertions(+), 56 deletions(-) diff --git a/Makefile b/Makefile index d8b7109..ca4d168 100644 --- a/Makefile +++ b/Makefile @@ -3,6 +3,7 @@ PREFIX = $(HOME)/.local INSTALL = install +OS ?= $(shell uname -s) ifeq ($(OS),Darwin) CONTAINER = container else diff --git a/README.md b/README.md index bb7d6ab..1c34d70 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ # Containerized Development Environments -`dbx` is a set of scripts for managing a local containerized development environment. It works by creating a long-lived container machine named `dbx` that that has no access to the host where you can install various developer tools and dependencies without losing sleep at night. It uses Apple `container` tool, which creates a dedicated virtual machine for the `dbx` container machine, providing increased isolation and security. The host connects to the container via SSH, which allows any development tools with remote editing capabilities to be used (such as Emacs, Zed or Visual Studio Code). +`dbx` is a set of scripts for managing a local containerized development environment. It works by creating a long-lived container named `dbx` that has no access to the host where you can install various developer tools and dependencies without losing sleep at night. It uses a VM-based container engine - Apple `container` on macOS, and `podman` with the `krun` runtime on Linux - which creates a dedicated virtual machine for the `dbx` container, providing increased isolation and security. The host connects to the container via SSH, which allows any development tools with remote editing capabilities to be used (such as Emacs, Zed or Visual Studio Code). At its core, it is similar to Toolbx and Distrobox, but with improved isolation and security. In general, it does not try to create a convenient container that is seamlessly integrated with the host, but rather a restricted sandbox where you can install development CLI tools, and reduce `npm install` risk. diff --git a/dbx b/dbx index f9a4bd2..5940e2b 100755 --- a/dbx +++ b/dbx @@ -9,6 +9,8 @@ name="dbx" image=ghcr.io/nlordell/dbx:latest skel= script= +cpus= +memory= asroot= preserve= recursive= @@ -43,8 +45,10 @@ cp options: -p preserve file times and mode bits -r copy recursively create options: + -c CPUS number of CPUs allocated to the devbox -i IMAGE devbox container image (default: $image) -k SKEL overlay skeleton directory onto the user's home + -m MEMORY amount of memory allocated to the devbox in MiB -x SCRIPT override post-installation script enter/exec options: -R connect as root instead of the development user @@ -95,8 +99,8 @@ parse_name() { local name="$1" # Restrict allowed names only lower case letters and digits, with hyphens - # only allowed in the middle of the name. This is restrictive, but matches - # what is allowed by `container machine`. + # only allowed in the middle of the name. This is restrictive, but is + # a valid container name on all supported engines. case "$name" in "" | [!a-z0-9]* | *[!a-z0-9-]* | *[!a-z0-9]) usage_error "invalid devbox name '$name'" @@ -117,6 +121,18 @@ parse_image() { printf "%s\n" "$image" } +parse_int() { + local val="$1" + local what="$2" + + case "$val" in + "" | [!1-9]* | *[!0-9]*) + usage_error "invalid $what '$val'" + ;; + esac + printf "%s\n" "$val" +} + parse_port() { local port="$1" @@ -164,6 +180,8 @@ parse_opt() { v) version ;; n) name="$(parse_name "$arg")" ;; i) image="$(parse_image "$arg")" ;; + c) cpus="$(parse_int "$arg" "number of CPUs")" ;; + m) memory="$(parse_int "$arg" "amount of memory")" ;; k) skel="$(parse_skel "$arg")" ;; x) script="$(parse_script "$arg")" ;; R) asroot=t ;; @@ -199,7 +217,7 @@ parse_opts() { case "$command" in cp) optstr="${optstr}Rpr" ;; - create) optstr="${optstr}i:k:x:" ;; + create) optstr="${optstr}c:i:k:m:x:" ;; enter | exec) optstr="${optstr}Rs" ;; ssh) optstr="${optstr}Fg" ;; proxy | start | stop) ;; @@ -316,6 +334,16 @@ Host $name SSH_CONFIG } +ssh_waitforsshd() { + for _ in {1..20}; do + if ssh -q -F "$(ssh_config)" "root@$name" true; then + return 0 + fi + sleep 0.1 2>/dev/null || sleep 1 + done + general_error "devbox SSH server did not become ready" +} + cmd_cp() { local flags=() @@ -408,7 +436,7 @@ cmd_start() { dbx_mkdirs engine_start ssh_configure - engine_waitforsshd + ssh_waitforsshd } cmd_stop() { @@ -425,6 +453,35 @@ cmd_stop() { engine_stop } +vm_cpus() { + local cpus="${cpus-}" + + if [ -z "$cpus" ]; then + if [ "$OS" = "Darwin" ]; then + cpus="$(sysctl -n hw.ncpu)" + else + cpus="$(grep -c '^processor[[:space:]]*:' /proc/cpuinfo)" + fi + cpus="$((cpus <= 32 ? (cpus + 1) / 2 : 16))" + fi + printf "%d\n" "$cpus" +} + +vm_memory() { + local memory="${memory-}" + + if [ -z "$memory" ]; then + if [ "$OS" = "Darwin" ]; then + memory="$(sysctl -n hw.memsize)" + else + memory="$(awk '/^MemTotal:/ {printf "%d", $2 * 1024}' /proc/meminfo)" + memory=$((memory / 1048576)) + fi + memory="$(((memory + 1) / 2))" + fi + printf "%d\n" "$memory" +} + if [ "$OS" = "Darwin" ] && has_commands container; then engine_init() { if ! container system status &>/dev/null; then @@ -433,67 +490,80 @@ if [ "$OS" = "Darwin" ] && has_commands container; then } engine_create() { - container machine create \ - --name="$name" \ - --no-boot \ - --home-mount=none \ - "$image" - - # Provision the devbox. Note that there is no way to access the - # filesystem on the container machine directly, so use the script - # to authorise our SSH key. Additionally, remove the automatically - # given `sudo` access from the devbox user; the expectation is to - # use `-R` to perform privileged operations. - container machine run \ + local user + + # Use a plain container for the devbox instead of a container + # machine. Note that a container machine - even with the home + # mounting disabled - runs with all Linux capabilities, no masked + # or read-only `/proc` and `/sys` paths, and forwards the SSH agent + # into the container, making it much less isolated. + # + # Note that, unlike container machines, container resources may + # only be configured at creation time, so they cannot be set from + # `engine_start`. + container create \ --name="$name" \ - --root \ - " + --cpus="$(vm_cpus)" \ + --memory="$(vm_memory)M" \ + "$image" >/dev/null + + engine_start + + # Provision the devbox. Note that a plain container - unlike a + # container machine - does not get an automatically provisioned + # user matching the host user, so create that, and authorise our + # SSH key. No `sudo` access is granted; the expectation is to use + # `-R` to perform privileged operations. + # + # Note that unlike on Linux, the devbox user is not given the host + # user's UID and GID, as macOS uses a different set of *ID semantics + # than Linux (e.g. the typical `staff` group is GID 20 which is + # `dialout` on Debian). + user="$(id -un)" + container exec \ + --user=root \ + "$name" \ + sh -c " + set -eu printf '%s\n' '$(cat "$(ssh_id).pub")' > /etc/ssh/authorized_keys - rm '/etc/sudoers.d/$(id -un | tr . _)' + useradd -m -s \"\$(command -v bash)\" '$user' " engine_stop } engine_start() { - # The only way to start a machine is to run a command - use one that - # does nothing but return a `0` exit code. - container machine run \ - --name="$name" \ - --root \ - true - } - - engine_waitforsshd() { - : + container start "$name" >/dev/null } engine_stop() { - container machine stop "$name" >/dev/null + container stop "$name" >/dev/null } engine_iscreated() { - container machine inspect "$name" &>/dev/null + container inspect "$name" &>/dev/null } engine_isrunning() { - local status + local state - status="$( - container machine inspect "$name" 2>/dev/null | - grep '"status"' | + state="$( + container inspect "$name" 2>/dev/null | + grep '"state"' | cut -d'"' -f4 )" - [ "$status" = "running" ] + [ "$state" = "running" ] } engine_hostname() { local ipaddress ipaddress="$( - container machine inspect "$name" | - grep '"ipAddress"' | + container inspect "$name" | + grep '"ipv4Address"' | + head -n 1 | cut -d'"' -f4 )" + ipaddress="${ipaddress%%/*}" if [ -z "$ipaddress" ]; then general_error "failed to read devbox IP address" fi @@ -515,8 +585,6 @@ elif [ "$OS" = "Linux" ] && has_commands podman krun; then } engine_create() { - local cpus - local mem local user local uid local gid @@ -524,12 +592,10 @@ elif [ "$OS" = "Linux" ] && has_commands podman krun; then # Set the default `krun` VM configuration. Do not overwrite existing # user configuration if the file already exists. if [ ! -f "$(krun_vmconfig)" ]; then - cpus="$(grep -c '^processor[[:space:]]*:' /proc/cpuinfo)" - mem="$(awk '/^MemTotal:/ {printf "%d", $2/1024}' /proc/meminfo)" cat <"$(krun_vmconfig)" { - "cpus": $((cpus <= 32 ? (cpus + 1) / 2 : 16)), - "ram_mib": $(((mem + 1) / 2)), + "cpus": $(vm_cpus), + "ram_mib": $(vm_memory), "use_passt": 1 } CONFIG @@ -550,7 +616,7 @@ CONFIG # support `podman exec` so execute the script over SSH. engine_start ssh_configure - engine_waitforsshd + ssh_waitforsshd user="$(id -un)" uid="$(id -u)" gid="$(id -g)" @@ -573,16 +639,6 @@ CONFIG podman start "$name" >/dev/null } - engine_waitforsshd() { - for _ in {1..10}; do - if ssh -q -F "$(ssh_config)" "root@$name" true; then - return 0 - fi - sleep 0.1 2>/dev/null || sleep 1 - done - general_error "devbox SSH server did not become ready" - } - engine_stop() { podman stop "$name" >/dev/null } -- cgit v1.2.3