-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
alpine-chroot-install
executable file
·422 lines (350 loc) · 20.8 KB
/
alpine-chroot-install
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/bin/sh
# vim: set ts=4:
#---help---
# Usage: alpine-chroot-install [options]
#
# This script installs Alpine Linux into a chroot and optionally sets up
# qemu-user and binfmt to emulate different architecture (e.g. armhf).
#
# If qemu-user and binfmt is needed, the script checks if both are available.
# If not, it tries to install them using apt-get. Beside this the script should
# work on any Linux system.
#
# It also creates script "enter-chroot" inside the chroot directory, that may
# be used to enter the chroot environment. That script do the following:
#
# 1. saves environment variables specified by $CHROOT_KEEP_VARS and PWD,
# 2. chroots into $CHROOT_DIR,
# 3. starts clean environment using "env -i",
# 4. switches user and simulates full login using "su -l",
# 5. loads saved environment variables and changes directory to saved PWD,
# 6. executes specified command or "sh" if not provided.
#
# Example:
# sudo alpine-chroot-install -d /alpine -p build-base -p cmake
# /alpine/enter-chroot -u $USER ./build
#
# Options and environment variables:
# -a ARCH CPU architecture for the chroot. If not set, then it's
# the same as the host's architecture. If it's different
# from the host's architecture, then it will be emulated
# using qemu-user. Options: x86_64, x86, aarch64, armhf,
# armv7, ppc64le, riscv64, s390x.
#
# -b ALPINE_BRANCH Alpine branch to install (default is latest-stable).
#
# -d CHROOT_DIR Absolute path to the directory where Alpine chroot
# should be installed (default is /alpine).
#
# -i BIND_DIR Absolute path to the directory on the host system that
# should be mounted on the same path inside the chroot
# (default is PWD, if it's under /home, or none).
#
# -k CHROOT_KEEP_VARS... Names of the environment variables to pass from the
# host environment into chroot by the enter-chroot
# script. Name may be an extended regular expression.
# Default: ARCH CI QEMU_EMULATOR TRAVIS_.*.
#
# -m ALPINE_MIRROR... URI of the Aports mirror to fetch packages from
# (default is http://dl-cdn.alpinelinux.org/alpine).
#
# -p ALPINE_PACKAGES... Alpine packages to install into the chroot (default is
# build-base ca-certificates ssl_client).
#
# -r EXTRA_REPOS... Alpine repositories to be added to
# /etc/apk/repositories (main and community from
# $ALPINE_MIRROR are always added).
#
# -t TEMP_DIR Absolute path to the directory where to store temporary
# files (defaults to `mktemp -d`).
#
# -h Show this help message and exit.
#
# -v Print version and exit.
#
# APK_TOOLS_URI URL of apk.static to download. Default is x86_64
# binary from
# https://gitlab.alpinelinux.org/alpine/apk-tools/-/packages.
#
# APK_TOOLS_SHA256 SHA-256 checksum of $APK_TOOLS_URI.
#
# Each option can be also provided by environment variable. If both option and
# variable is specified and the option accepts only one argument, then the
# option takes precedence.
#
# https://github.com/alpinelinux/alpine-chroot-install
#---help---
set -eu
#======================= C o n s t a n t s =======================#
: ${APK_TOOLS_URI:="https://gitlab.alpinelinux.org/api/v4/projects/5/packages/generic/v2.12.10/x86_64/apk.static"}
: ${APK_TOOLS_SHA256:="d7506bb11327b337960910daffed75aa289d8bb350feab624c52965be82ceae8"}
# Alpine APK keys for packages verification.
ALPINE_KEYS='
4a6a0840:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA1yHJxQgsHQREclQu4Ohe\nqxTxd1tHcNnvnQTu/UrTky8wWvgXT+jpveroeWWnzmsYlDI93eLI2ORakxb3gA2O\nQ0Ry4ws8vhaxLQGC74uQR5+/yYrLuTKydFzuPaS1dK19qJPXB8GMdmFOijnXX4SA\njixuHLe1WW7kZVtjL7nufvpXkWBGjsfrvskdNA/5MfxAeBbqPgaq0QMEfxMAn6/R\nL5kNepi/Vr4S39Xvf2DzWkTLEK8pcnjNkt9/aafhWqFVW7m3HCAII6h/qlQNQKSo\nGuH34Q8GsFG30izUENV9avY7hSLq7nggsvknlNBZtFUcmGoQrtx3FmyYsIC8/R+B\nywIDAQAB
5243ef4b:MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvNijDxJ8kloskKQpJdx+\nmTMVFFUGDoDCbulnhZMJoKNkSuZOzBoFC94omYPtxnIcBdWBGnrm6ncbKRlR+6oy\nDO0W7c44uHKCFGFqBhDasdI4RCYP+fcIX/lyMh6MLbOxqS22TwSLhCVjTyJeeH7K\naA7vqk+QSsF4TGbYzQDDpg7+6aAcNzg6InNePaywA6hbT0JXbxnDWsB+2/LLSF2G\nmnhJlJrWB1WGjkz23ONIWk85W4S0XB/ewDefd4Ly/zyIciastA7Zqnh7p3Ody6Q0\nsS2MJzo7p3os1smGjUF158s6m/JbVh4DN6YIsxwl2OjDOz9R0OycfJSDa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FntEqIokCAwEAAQ==
'
# Version of alpine-chroot-install script.
VERSION='0.14.0'
#======================= F u n c t i o n s =======================#
die() {
printf '\033[1;31mERROR:\033[0m %s\n' "$@" >&2 # bold red
exit 1
}
einfo() {
printf '\n\033[1;36m> %s\033[0m\n' "$@" >&2 # bold cyan
}
ewarn() {
printf '\033[1;33m> %s\033[0m\n' "$@" >&2 # bold yellow
}
# Writes Alpine APK keys embedded in this script into directory $1.
dump_alpine_keys() {
local dest_dir="$1"
local content id line
mkdir -p "$dest_dir"
for line in $ALPINE_KEYS; do
id=${line%%:*}
content=${line#*:}
printf -- "-----BEGIN PUBLIC KEY-----\n$content\n-----END PUBLIC KEY-----\n" \
> "$dest_dir/alpine-devel@lists.alpinelinux.org-$id.rsa.pub"
done
}
normalize_arch() {
case "$1" in
x86 | i[3456]86) echo 'i386';;
armhf | armv[4-9]) echo 'arm';;
*) echo "$1";;
esac
}
fetch_url() {
local url="$1"
echo "fetch $url" >&2
if command -v curl >/dev/null; then
curl --remote-name --connect-timeout 10 -fsSL "$url"
elif command -v wget >/dev/null; then
wget -T 10 --no-verbose "$url"
else
die 'Cannot download a file: neither curl nor wget is available!'
fi
}
download_file() (
local url="$1"
local sha256="$2"
local dest="${3:-.}"
mkdir -p "$dest" \
&& cd "$dest" \
&& rm -f "${url##*/}" \
&& fetch_url "$url" \
&& echo "$sha256 ${url##*/}" | sha256sum -c
)
usage() {
sed -En '/^#---help---/,/^#---help---/p' "$0" | sed -E 's/^# ?//; 1d;$d;'
}
gen_chroot_script() {
cat <<-EOF
#!/bin/sh
set -e
ENV_FILTER_REGEX='($(echo "$CHROOT_KEEP_VARS" | tr -s ' ' '|'))'
EOF
if [ -n "$QEMU_EMULATOR" ]; then
printf 'export QEMU_EMULATOR="%s"' "$QEMU_EMULATOR"
fi
cat <<-'EOF'
user='root'
if [ $# -ge 2 ] && [ "$1" = '-u' ]; then
user="$2"; shift 2
fi
oldpwd="$(pwd)"
[ "$(id -u)" -eq 0 ] || _sudo='sudo'
tmpfile="$(mktemp)"
chmod 644 "$tmpfile"
export | sed -En "s/^([^=]+ ${ENV_FILTER_REGEX}=)('.*'|\".*\")$/\1\3/p" > "$tmpfile" || true
cd "$(dirname "$0")"
$_sudo mv "$tmpfile" env.sh
$_sudo chroot . /usr/bin/env -i su -l "$user" \
sh -c ". /etc/profile; . /env.sh; cd '$oldpwd' 2>/dev/null; \"\$@\"" \
-- "${@:-sh}"
EOF
# NOTE: ash does not load login profile when run with QEMU user-mode
# emulation (I have no clue why), that's why /etc/profile is sourced here.
}
gen_destroy_script() {
cat <<-'EOF'
#!/bin/sh
set -e
remove=no
case "$1" in
-r | --remove) remove=yes;;
'') ;;
*) echo "Usage: $0 [-r | --remove]"; exit 1;;
esac
SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)
[ "$(id -u)" -eq 0 ] || _sudo='sudo'
# Unmounts all filesystem under the specified directory tree.
cat /proc/mounts | cut -d' ' -f2 | grep "^$SCRIPT_DIR." | sort -r | while read path; do
echo "Unmounting $path" >&2
$_sudo umount -fn "$path" || exit 1
done
if [ "$remove" = yes ]; then
rm_opts=''
# Just to be extra careful...
rm --help 2>&1 | grep -Fq 'one-file-system' && rm_opts='--one-file-system'
echo "Removing $SCRIPT_DIR" >&2
$_sudo rm -Rf $rm_opts "$SCRIPT_DIR"
else
echo "If you want to remove $SCRIPT_DIR directory, run: $0 --remove" >&2
fi
EOF
}
#------------------------- Debian/Ubuntu ---------------------------#
APT_UPDATED=no
apt_install() {
if [ "$APT_UPDATED" != yes ]; then
apt-get update
APT_UPDATED=yes
fi
# -o=Dpkg::Use-Pty=0 - get rid of annoying "Reading database ..." messages.
apt-get install -y -o=Dpkg::Use-Pty=0 --no-install-recommends "$@"
}
# Installs and enables binfmt-support on Debian/Ubuntu host.
install_binfmt_support() {
apt_install binfmt-support \
|| die 'Failed to install binfmt-support using apt-get!'
update-binfmts --enable \
|| die 'Failed to enable binfmt!'
}
# Installs QEMU user mode emulation binaries on Debian/Ubuntu host.
install_qemu_user() {
apt_install qemu-user-static \
|| die 'Failed to install qemu-user-static using apt-get!'
}
#============================ M a i n ============================#
while getopts 'a:b:d:i:k:m:p:r:t:hv' OPTION; do
case "$OPTION" in
a) ARCH="$OPTARG";;
b) ALPINE_BRANCH="$OPTARG";;
d) CHROOT_DIR="$OPTARG";;
i) BIND_DIR="$OPTARG";;
k) CHROOT_KEEP_VARS="${CHROOT_KEEP_VARS:-} $OPTARG";;
m) ALPINE_MIRROR="$OPTARG";;
p) ALPINE_PACKAGES="${ALPINE_PACKAGES:-} $OPTARG";;
r) EXTRA_REPOS="${EXTRA_REPOS:-} $OPTARG";;
t) TEMP_DIR="$OPTARG";;
h) usage; exit 0;;
v) echo "alpine-chroot-install $VERSION"; exit 0;;
esac
done
: ${ALPINE_BRANCH:="latest-stable"}
: ${ALPINE_MIRROR:="http://dl-cdn.alpinelinux.org/alpine"}
: ${ALPINE_PACKAGES:="build-base ca-certificates ssl_client"}
: ${ARCH:=}
: ${BIND_DIR:=}
: ${CHROOT_DIR:="/alpine"}
: ${CHROOT_KEEP_VARS:="ARCH CI QEMU_EMULATOR TRAVIS_.*"}
: ${EXTRA_REPOS:=}
: ${TEMP_DIR:=$(mktemp -d || echo /tmp/alpine)}
# Note: Binding $PWD into chroot as default was a bad idea. It's convenient
# on Travis, but dangerous in general. However, all existing .travis.yml relies
# on this behaviour, so we can't (shouldn't) remove it completely.
[ "$BIND_DIR" ] || case "$(pwd)" in
/home/*) BIND_DIR="$(pwd)";;
esac
if [ "$(id -u)" -ne 0 ]; then
die 'This script must be run as root!'
fi
mkdir -p "$CHROOT_DIR"
cd "$CHROOT_DIR"
# Install QEMU user mode emulation if needed (works only on Debian and derivates)
QEMU_EMULATOR=''
if [ -n "$ARCH" ] && [ $(normalize_arch $ARCH) != $(normalize_arch $(uname -m)) ]; then
qemu_arch="$(normalize_arch $ARCH)"
QEMU_EMULATOR="/usr/bin/qemu-$qemu_arch-static"
if [ ! -x "$QEMU_EMULATOR" ]; then
einfo 'Installing qemu-user-static on host system...'
install_qemu_user
fi
if [ ! -e /proc/sys/fs/binfmt_misc/qemu-$qemu_arch ]; then
einfo 'Installing and enabling binfmt-support on host system...'
install_binfmt_support
fi
mkdir -p usr/bin
cp -v "$QEMU_EMULATOR" usr/bin/
fi
einfo 'Downloading static apk-tools'
download_file "$APK_TOOLS_URI" "$APK_TOOLS_SHA256" "$TEMP_DIR"
APK="$TEMP_DIR/apk.static"
chmod +x "$APK"
einfo "Installing Alpine Linux $ALPINE_BRANCH ($ARCH) into chroot"
mkdir -p "$CHROOT_DIR"/etc/apk
cd "$CHROOT_DIR"
printf '%s\n' \
"$ALPINE_MIRROR/$ALPINE_BRANCH/main" \
"$ALPINE_MIRROR/$ALPINE_BRANCH/community" \
$EXTRA_REPOS \
> etc/apk/repositories
dump_alpine_keys etc/apk/keys/
cp /etc/resolv.conf etc/resolv.conf
"$APK" add \
--root . --update-cache --initdb --no-progress \
${ARCH:+--arch $ARCH} \
alpine-baselayout apk-tools busybox busybox-suid musl-utils
if "$APK" info --root . --no-progress --quiet alpine-release >/dev/null; then
"$APK" add --root . --no-progress alpine-release
else
# In Alpine <3.17, this package contains /etc/os-release,
# /etc/alpine-release and /etc/issue, but we don't wanna install all
# its dependencies (e.g. openrc).
"$APK" fetch --root . --no-progress --stdout alpine-base \
| tar -xz etc
fi
gen_chroot_script > enter-chroot
gen_destroy_script > destroy
chmod +x enter-chroot destroy
einfo 'Binding filesystems into chroot'
mount -v -t proc none proc
mount -v --rbind /sys sys
mount --make-rprivate sys
mount -v --rbind /dev dev
mount --make-rprivate dev
# Some systems (Ubuntu?) symlinks /dev/shm to /run/shm.
if [ -L /dev/shm ] && [ -d /run/shm ]; then
mkdir -p run/shm
mount -v --bind /run/shm run/shm
mount --make-private run/shm
fi
if [ -d "$BIND_DIR" ]; then
mkdir -p "${CHROOT_DIR}${BIND_DIR}"
mount -v --bind "$BIND_DIR" "${CHROOT_DIR}${BIND_DIR}"
mount --make-private "${CHROOT_DIR}${BIND_DIR}"
fi
einfo 'Setting up Alpine'
./enter-chroot <<-EOF
set -e
apk update
apk add $ALPINE_PACKAGES
if [ -d /etc/sudoers.d ] && [ ! -e /etc/sudoers.d/wheel ]; then
echo '%wheel ALL=(ALL) NOPASSWD: ALL' > /etc/sudoers.d/wheel
fi
if [ -n "${SUDO_USER:-}" ]; then
adduser -u "${SUDO_UID:-1000}" -G users -s /bin/sh -D "${SUDO_USER:-}" || true
fi
EOF
cat >&2 <<-EOF
---
Alpine installation is complete
Run $CHROOT_DIR/enter-chroot [-u <user>] [command] to enter the chroot
and $CHROOT_DIR/destroy [--remove] to destroy it.
EOF