-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbwrap-home
More file actions
executable file
·136 lines (116 loc) · 2.76 KB
/
Copy pathbwrap-home
File metadata and controls
executable file
·136 lines (116 loc) · 2.76 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env bash
# Run command with a fake $HOME and optionally tmpfs in /tmp,
# mounting everything but $PWD read-only.
#
# Useful for running those "modern" language-specific package managers
# in a controlled environment.
set -eu -o pipefail
# shellcheck source-path=..
. "$HOME"/bin/.o
options=$(
unset GETOPT_COMPATIBLE
getopt \
-o hH:Ttm:M: \
--long "help,home:,home-tmpfs,tmp:,tmpfs,mount:,mount-rw:" \
-- "$@"
)
eval "set -- $options"
home=$PWD/.home
home_tmpfs=
tmpdir=
tmpfs=
mount=()
mount_rw=()
while (( $# )); do
opt=$1; shift
case "$opt" in
-h|--help)
echo "Usage: bwrap-home [-h|--home=PATH] [-T|--home-tmpfs] [--tmp=PATH] [-t|--tmpfs] [-m|--mount=PATH] [-M|--mount-rw=PATH] -- <command> [<args>…]"
exit
;;
-H|--home) home=$1; shift ;;
-T|--home-tmpfs) home_tmpfs=: ;;
--tmp) tmpdir=$1; shift ;;
-t|--tmpfs) tmpfs=: ;;
-m|--mount) mount+=("$1"); shift ;;
-M|--mount-rw) mount_rw+=("$1"); shift ;;
--) break ;;
esac
done
real_HOME=$(readlink -f "$HOME")
if [[ "${real_HOME%/}"/ == "${PWD%/}"/* ]]; then
echo "\$HOME under \$PWD, would mount \$HOME read-write, exiting"
exit 1
fi
bwrap_opts=()
if [[ $tmpfs ]]; then
real_tmp=$(readlink -f /tmp)
if [[ "${real_tmp%/}"/ == "${PWD%/}"/* ]]; then
echo "/tmp under \$PWD, would mount /tmp read-write, exiting"
exit 1
fi
bwrap_opts+=(--tmpfs /tmp)
elif [[ $tmpdir ]]; then
[[ -d "$tmpdir" ]] || o mkdir -p "$tmpdir"
real_tmp=$(readlink -f /tmp)
real_tmpdir=$(readlink -f "$tmpdir")
if [[ "${real_tmpdir%/}"/ != "${real_tmp%/}"/* ]]; then
echo "--tmp $tmpdir must be under /tmp, exiting"
exit 1
fi
bwrap_opts+=(--bind "$tmpdir" /tmp)
else
bwrap_opts+=(--bind /tmp{,})
fi
orig_HOME="$HOME"
if [[ $home_tmpfs ]]; then
bwrap_opts+=(
--tmpfs "$HOME"
--bind "$PWD"{,}
)
else
home=$(readlink -f "$home")
if [[ "${real_HOME%/}"/ == "${home%/}"/* ]]; then
echo "\$HOME under --home $home, would mount \$HOME read-write, exiting"
exit 1
fi
[[ -d "$home" ]] || o mkdir -p "$home"
export HOME="$home"
bwrap_opts+=(
--bind "$PWD"{,}
--bind "$HOME"{,}
)
fi
if [[ "$HOME" != "$orig_HOME" ]]; then
bwrap_opts+=(
--ro-bind {"$orig_HOME","$HOME"}/.config/mise
--ro-bind {"$orig_HOME","$HOME"}/.local/share/mise
)
fi
for m in "${mount[@]}" "${mount_rw[@]}"; do
if [[ "${real_HOME%/}"/ == "${m%/}"/* ]]; then
echo "\$HOME under $m, would make \$HOME accessible, exiting"
exit 1
fi
if [[ $tmpfs && "${real_tmp%/}"/ == "${m%/}"/* ]]; then
echo "/tmp under $m, would make /tmp accessible, exiting"
exit 1
fi
done
for m in "${mount[@]}"; do
bwrap_opts+=(
--ro-bind "$m"{,}
)
done
for m in "${mount_rw[@]}"; do
bwrap_opts+=(
--bind "$m"{,}
)
done
(( $# )) || set -- bash
o exec bwrap \
--ro-bind /{,} \
--dev /dev \
--proc /proc \
"${bwrap_opts[@]}" \
-- "$@"