forked from ValveSoftware/steam-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_chroot.sh
executable file
·327 lines (273 loc) · 7.02 KB
/
setup_chroot.sh
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env bash
SCRIPT="$(readlink -f "$0")"
SCRIPTNAME="$(basename "$SCRIPT")"
SCRIPT_DIR="$(dirname "$SCRIPT")"
LOGFILE=""
CHROOT_PREFIX="steamrt_"
CHROOT_DIR="/var/chroots"
CHROOT_NAME=""
# exit on any script line that fails
set -o errexit
# bail on any unitialized variable reads
set -o nounset
# bail on failing commands before last pipe
set -o pipefail
# Output helpers
COLOR_OFF=""
COLOR_ON=""
if [[ $(tput colors 2>/dev/null || echo 0) -gt 0 ]]; then
COLOR_ON=$'\e[93;1m'
COLOR_OFF=$'\e[0m'
fi
FORCE=0
sh_quote ()
{
local quoted
if [ $# -gt 0 ]; then
quoted="$(printf '%q ' "$@")"
echo "${quoted:0:-1}"
fi
}
prebuild_chroot()
{
local name
# install some packages
echo -e "\\n${COLOR_ON}Installing schroot...${COLOR_OFF}"
sudo -E apt-get install -y schroot
# Check if there are any active schroot sessions right now and warn if so...
schroot_list=$(schroot --list --all-sessions | head -n 1)
if [ -n "$schroot_list" ]; then
tput setaf 3
echo -e "\\nWARNING: Schroot says you have a currently active session!\\n"
tput sgr0
echo " ${schroot_list}"
echo ""
if [ ${FORCE} -eq 0 ]; then
read -r -p "Are you sure you want to continue (y/n)? "
if [[ "$REPLY" != [Yy] ]]; then
echo -e "Cancelled...\\n"
exit 1
fi
fi
fi
STEAM_RUNTIME_SPEW_WARNING=
for var in "$@"; do
if [ -n "$CHROOT_NAME" ]; then
name="$CHROOT_NAME"
else
name="${CHROOT_PREFIX}${var/--/}"
fi
dirname="${CHROOT_DIR}/${name}"
if [ -d "${dirname}" ]; then
tput setaf 3
STEAM_RUNTIME_SPEW_WARNING=1
echo -e "About to remove ${dirname} and re-install..."
tput sgr0
fi
done
if [[ "$STEAM_RUNTIME_SPEW_WARNING" == "1" ]]; then
if [ ${FORCE} -eq 0 ]; then
read -r -p " This ok (y/n)? "
if [[ "$REPLY" != [Yy] ]]; then
echo -e "Cancelled...\\n"
exit 1
fi
fi
fi
}
copy_apt_settings ()
{
local sysroot="$1"
if [ "$1/" -ef / ]; then
echo "Internal error: sysroot $1 is the same file as the real root" >&2
exit 1
fi
# Copy over proxy settings from host machine
echo -e "\\n${COLOR_ON}Adding proxy info to chroot (if set)...${COLOR_OFF}"
set +o pipefail
env | grep -i "_proxy=" | grep -v PERSISTENT_HISTORY_LAST | xargs -i echo export {} | sudo tee "$sysroot/etc/profile.d/steamrtproj.sh"
env | grep -i "_proxy=" | grep -v PERSISTENT_HISTORY_LAST | xargs -i echo export {} | sudo tee -a "$sysroot/etc/environment"
set -o pipefail
sudo rm -rf "$sysroot/etc/apt/apt.conf"
if [ -f /etc/apt/apt.conf ]; then sudo cp "/etc/apt/apt.conf" "$sysroot/etc/apt"; fi
}
untar_chroot ()
{
# untar_chroot {--amd64 | --i386} TARBALL
# Unpack a sysroot tarball for the specified architecture.
case "$1" in
"--i386" )
pkg="i386"
personality="linux32"
;;
"--amd64" )
pkg="amd64"
personality="linux"
;;
* )
echo "Error: Unrecognized argument: $1"
exit 1
;;
esac
shift
local tarball="$1"
local name
if [ -n "$CHROOT_NAME" ]; then
name="$CHROOT_NAME"
else
name="${CHROOT_PREFIX}${pkg}"
fi
local sysroot="${CHROOT_DIR}/${name}"
# blow away existing directories and recreate empty ones
echo -e "\\n${COLOR_ON}Creating $sysroot..."
sudo rm -rf "$sysroot"
sudo mkdir -p "$sysroot"
# Create our schroot .conf file
echo -e "\\n${COLOR_ON}Creating /etc/schroot/chroot.d/${name}.conf...${COLOR_OFF}"
printf '[%s]\ndescription=%s\ndirectory=%s\npersonality=%s\ngroups=sudo\nroot-groups=sudo\npreserve-environment=true\ntype=directory\n' "${name}" "${tarball##*/}" "${sysroot}" "${personality}" | sudo tee "/etc/schroot/chroot.d/${name}.conf"
# Create our chroot
echo -e "\\n${COLOR_ON}Unpacking the chroot...${COLOR_OFF}"
sudo tar --auto-compress -C "$sysroot" -xf "$tarball"
copy_apt_settings "$sysroot"
# Implement --extra-apt-source
if [ -n "${extra_apt_sources+set}" ]; then
for line in "${extra_apt_sources[@]}"; do
printf '%s\n' "$line"
done > "$sysroot/etc/apt/sources.list.d/steamrt-extra.list"
fi
if [ -n "$(sudo find "$sysroot" -xdev '(' -uid +99 -o -gid +99 ')' -ls)" ]; then
echo -e "\\n${COLOR_ON}Warning: these files might have incorrect uid mapping${COLOR_OFF}" >&2
sudo find "$sysroot" -xdev '(' -uid +99 -o -gid +99 ')' -ls >&2
fi
}
# https://stackoverflow.com/questions/64786/error-handling-in-bash
function cleanup()
{
echo -e "\\nenv is:\\n$(env)\\n"
echo "ERROR: ${SCRIPTNAME} just hit error handler."
echo " BASH_COMMAND is \"${BASH_COMMAND}\""
echo " BASH_VERSION is $BASH_VERSION"
echo " pwd is \"$(pwd)\""
echo " PATH is \"$PATH\""
echo ""
tput setaf 3
echo "A command returned error. See the logfile: ${LOGFILE}"
tput sgr0
}
usage()
{
if [ "$1" -ne 0 ]; then
exec >&2
fi
echo "Usage: $0 [--beta | --suite SUITE] [--name NAME] [--extra-apt-source 'deb http://MIRROR SUITE COMPONENT...'] [--output-dir <DIRNAME>] [--logfile FILE] --tarball TARBALL --i386|--amd64"
exit "$1"
}
main()
{
local getopt_temp
getopt_temp="$(getopt -o '' --long \
'force,amd64,beta,extra-apt-source:,i386,name:,output-dir:,logfile:,suite:,tarball:,help' \
-n "$0" -- "$@")"
eval set -- "$getopt_temp"
unset getopt_temp
local arch=
local -a setup_arguments=()
local suite=scout
local suite_suffix=
local tarball=
# Create a copy of the arguments
local args=("$@")
while [ "$#" -gt 0 ]; do
case "$1" in
(--force)
FORCE=1
shift
;;
(--amd64|--i386)
if [ -n "$arch" ] && [ "$arch" != "$1" ]; then
echo "Error: $1 conflicts with $arch" >&2
usage 2
fi
arch="$1"
shift
;;
(--beta)
setup_arguments+=(--beta)
suite_suffix=_beta
shift
;;
(--name)
CHROOT_NAME="$2"
shift 2
;;
(--extra-apt-source)
setup_arguments+=("$1" "$2")
shift 2
;;
(--help)
usage 0
;;
(--logfile)
LOGFILE="$2"
shift 2
;;
(--output-dir)
CHROOT_DIR="$2"
shift 2
;;
(--suite)
suite="$2"
setup_arguments+=(--suite "$2")
shift 2
;;
(--tarball)
tarball="$2"
shift 2
;;
(--)
shift
break
;;
(-*)
usage 2
;;
(*)
# no non-option arguments are currently allowed
usage 2
break
;;
esac
done
# Launch ourselves with script so we can time this and get a log file
if [[ ! -v SETUP_CHROOT_LOGGING_STARTED ]]; then
if command -v script >/dev/null; then
export SETUP_CHROOT_LOGGING_STARTED=1
export SHELL=/bin/bash
if [ -z "$LOGFILE" ]; then
LOGFILE="$(mktemp --tmpdir steam-runtime-setup-chroot-XXX.log)"
fi
script --return --command "time $SCRIPT $(sh_quote "${args[@]}")" "${LOGFILE}"
exit $?
else
echo >&2 "!! 'script' command not found, will not auto-generate a log file"
# Continue
fi
fi
CHROOT_PREFIX="${CHROOT_PREFIX}${suite}${suite_suffix}_"
if [ -z "$tarball" ]; then
echo "This script no longer bootstraps chroots from first principles." >&2
echo "Use --tarball to provide a pre-prepared sysroot." >&2
usage 2
fi
if [ -z "$arch" ]; then
usage 2
fi
# Building root(s)
prebuild_chroot "$arch"
trap cleanup EXIT
untar_chroot "$arch" "$tarball"
trap - EXIT
echo -e "\\n${COLOR_ON}Done...${COLOR_OFF}"
}
main "$@"
# vi: ts=4 sw=4 noexpandtab