Created
March 20, 2026 18:31
-
-
Save digitalsignalperson/c4c14a00572c0f4ee028a474b103a80b to your computer and use it in GitHub Desktop.
Example use of slirp4netns with bubblewrap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| status=$(mktemp -u) | |
| mkfifo $status | |
| # TODO error if slirp4netns not installed | |
| set -e | |
| cleanup() { | |
| echo "cleanup" | |
| rm -f "$status" | |
| [[ ! -z "$BWRAP_PID" ]] && kill "$SLIRP_PID" 2>/dev/null && echo "killed bwrap" | |
| [[ ! -z "$SLIRP_PID" ]] && kill "$SLIRP_PID" 2>/dev/null && echo "killed slirp" | |
| } | |
| trap cleanup EXIT | |
| exec 4<<< "nameserver 10.0.2.3" | |
| 3>$status \ | |
| bwrap \ | |
| --symlink usr/bin /bin \ | |
| --symlink usr/bin /sbin \ | |
| --symlink usr/lib /lib \ | |
| --symlink usr/lib64 /lib64 \ | |
| --ro-bind /usr/bin /usr/bin \ | |
| --ro-bind /usr/lib /usr/lib \ | |
| --ro-bind /usr/lib64 /usr/lib64 \ | |
| --ro-bind /usr/share /usr/share \ | |
| --ro-bind /etc/ssl /etc/ssl \ | |
| --ro-bind /etc/ca-certificates /etc/ca-certificates \ | |
| --ro-bind /etc/fonts /etc/fonts \ | |
| --tmpfs /tmp \ | |
| --proc /proc \ | |
| --dev /dev \ | |
| --dev-bind /dev/dri/renderD129 /dev/dri/renderD129 \ | |
| --dev-bind /dev/dri/card1 /dev/dri/card1 \ | |
| --ro-bind /sys/dev/char /sys/dev/char \ | |
| --ro-bind /sys/devices /sys/devices \ | |
| --dir "$XDG_RUNTIME_DIR" \ | |
| --ro-bind "$XDG_RUNTIME_DIR/wayland-0" "$XDG_RUNTIME_DIR/wayland-0" \ | |
| --ro-bind "$XDG_RUNTIME_DIR/pipewire-0" "$XDG_RUNTIME_DIR/pipewire-0" \ | |
| --ro-bind "$XDG_RUNTIME_DIR/pulse" "$XDG_RUNTIME_DIR/pulse" \ | |
| --unshare-all \ | |
| --die-with-parent \ | |
| --new-session \ | |
| --bind $HOME/Downloads $HOME/Downloads \ | |
| --chdir $HOME \ | |
| --ro-bind-data 4 /etc/resolv.conf \ | |
| --json-status-fd 3 \ | |
| -- chromium --force-dark-mode --class=Chromium-Extra --ozone-platform=wayland & | |
| # -- konsole & | |
| # --ro-bind /etc/resolv.conf /etc/resolv.conf \ | |
| # --dev-bind /dev/dri/renderD130 /dev/dri/renderD130 \ | |
| # --dev-bind /dev/dri/card2 /dev/dri/card2 \ | |
| # --dev-bind /dev/dri /dev/dri \ | |
| BWRAP_PID=$! | |
| echo "bwrap pid $BWRAP_PID" | |
| while true; do | |
| if read -r line < $status; then | |
| CHILD_PID=$(echo "$line" | jq -r '.["child-pid"]') | |
| if [[ -n $CHILD_PID ]]; then | |
| echo "child pid $CHILD_PID" | |
| break | |
| fi | |
| fi | |
| done | |
| echo "child pid $CHILD_PID" | |
| slirp4bwrap $CHILD_PID & | |
| SLIRP_PID=$! | |
| echo "slirp pid $SLIRP_PID" | |
| wait $BWRAP_PID | |
| BWRAP_EXIT_STATUS=$? | |
| kill "$SLIRP_PID" | |
| wait "$SLIRP_PID" 2>/dev/null | |
| exit "$BWRAP_EXIT_STATUS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment