-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnonowrap
More file actions
58 lines (52 loc) · 1.89 KB
/
nonowrap
File metadata and controls
58 lines (52 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env bash
set -e
if [[ $# -eq 0 ]]; then
TARGET_CMD=$SHELL
else
TARGET_CMD=("$@")
fi
# TODO: check that TARGET_CMD is not empty and show help otherwise
# declare that we are in a special shell, so that the user sees shell prompts like:
# nonowrap.sh:peter@aenderpad>
PROMPT_NAME=$(basename "$0")
export name=$PROMPT_NAME
# TODO: two nonowraps may share the same fs because this is racy
DATE=$(date --iso-8601=seconds)
PREFIX=/tmp/bwrap-$DATE
UPPER=$PREFIX/overlayfs-upper
WORK=$PREFIX/overlayfs-work
ENV=$PREFIX/env
mkdir -p "$UPPER"
mkdir -p "$WORK"
echo "# This is the environemnt that runs with the $UPPER overlayfs." > "$ENV"
export -p >> "$ENV"
echo "$ENV"
# sudo setpriv --reuid=peter --regid=users --init-groups --inh-caps=-all -- bash --rcfile $ENV -c "$@"
# sudo mkdir -p /alt-root
# sudo mount --bind / /alt-root
# 1. create overlayfs with bwrap
# 2. share special fses and socket: the current nix profiles as read only (current-system); the nix socket (we assume the daemon to be safe); the resolv.conf needed to resolve network addresses (--dir to make sure the path is world readable)
# 2. inject envfile as envvar
# 3. undo sudo
# 4. use bash to restore env for target process (bash will start non-interactively and hence populate env from BASH_ENV)
# 5. replace bash with target process (exec), preserving argument separation
sudo bwrap \
--overlay-src / \
--overlay "$UPPER" "$WORK" / \
--ro-bind /run/current-system /run/current-system \
--bind /nix/var/nix/daemon-socket /nix/var/nix/daemon-socket \
--dir /run/systemd/resolve \
--ro-bind /run/systemd/resolve/stub-resolv.conf /run/systemd/resolve/stub-resolv.conf \
--ro-bind /run/nscd /run/nscd \
--proc /proc \
--dev /dev \
--tmpfs "$XDG_RUNTIME_DIR" \
--setenv BASH_ENV "$ENV" \
-- \
setpriv \
--reuid=peter \
--regid=users \
--init-groups \
--inh-caps=-all \
-- \
bash -c 'exec "$@"' _ "${TARGET_CMD[@]}"