forked from bazurbat/spawn
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit-ubuntu-root
More file actions
executable file
·494 lines (406 loc) · 12.5 KB
/
Copy pathinit-ubuntu-root
File metadata and controls
executable file
·494 lines (406 loc) · 12.5 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
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#!/bin/sh
# deboostrap tweaks are taken from Docker repository:
# https://github.com/docker/docker/blob/master/contrib/mkimage/debootstrap
set -eu
program_name="init-ubuntu-root"
suffix="_added_by_${program_name}"
show_usage() {
cat <<EOF
Usage: $program_name [OPTION...] <root-dir>
Initializes unpacked Ubuntu rootfs for chroot or spawn.
OPTIONS
-h|--help Show help
--default Run default setup
--dev Setup for development
--add-i386 Add i386 arch
--install-java6 Install Oracle JDK 6
--install-java8-openjdk Install Open JDK 8
--install-wine Install Wine
--install-perforce Install Perforce
--install-dev-tools Install development tools
--skip-update Do not run 'apt-get update' before install
--add-user Add dev user
The <root-dir> argument is mandatory and should point to unpacked Ubuntu root
directory. Default tweaks are taken from Docker debootstrap setup. The
selection of development tools is from a personal list collected over time.
This script creates and modifies files in the system areas inside the
<root-dir>, and the running user should have the appropriate privileges to do
so. This usually means that it should be run as root (sudo and such).
The default setup means:
* Run deboostrap steps with minor adjustments
* Copy /etc/resolv.conf to the <root-dir>
* Add LANG=C.UTF-8 config to profile.d
* Optionally add proxy config to profile.d and apt.conf.d
* Run 'apt-get update' then 'apt-get dist-upgrade'
* Add dev user
NOTE: proxy configuration is added only if http_proxy or https_proxy
variables are set in the current environment.
IMAGES
Images can be downloaded from:
https://partner-images.canonical.com/core/trusty/current/ubuntu-trusty-core-cloudimg-amd64-root.tar.gz
https://partner-images.canonical.com/core/xenial/current/ubuntu-xenial-core-cloudimg-amd64-root.tar.gz
Unpack with:
mkdir <root-dir>
sudo tar -C <root-dir> -xpf <filename>
EOF
}
die() {
echo Error: $*
exit 1
}
in_chroot() {
PATH='/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin' \
LC_ALL=C.UTF-8 \
DEBIAN_FRONTEND=noninteractive \
chroot "$root_dir" "$@"
}
get_codename() {
if [ -r "$root_dir/etc/lsb-release" ]; then
echo $(. "$root_dir/etc/lsb-release" && echo $DISTRIB_CODENAME)
fi
}
debootstrap_setup() {
# prevent init scripts from running during install/update
cat > "$root_dir/usr/sbin/policy-rc.d" <<'EOF'
#!/bin/sh
exit 101
EOF
chmod +x "$root_dir/usr/sbin/policy-rc.d"
# prevent upstart scripts from running during install/update
in_chroot dpkg-divert --local --rename --add /sbin/initctl
cat > "$root_dir/sbin/initctl" <<'EOF'
#!/bin/sh
exit 0
EOF
chmod +x "$root_dir/sbin/initctl"
# this file is one APT creates to make sure we don't "autoremove" our currently
# in-use kernel, which doesn't really apply to debootstraps/Docker images that
# don't even have kernels installed
rm -f "$root_dir/etc/apt/apt.conf.d/01autoremove-kernels"
# force dpkg not to call sync() after package extraction (speeding up installs)
cat > "$root_dir/etc/dpkg/dpkg.cfg.d/apt-speedup${suffix}" <<'EOF'
force-unsafe-io
EOF
# remove apt-cache translations for fast "apt-get update"
cat > "$root_dir/etc/apt/apt.conf.d/no-languages${suffix}" <<'EOF'
Acquire::Languages "none";
EOF
# cat > "$root_dir/etc/apt/apt.conf.d/docker-gzip-indexes" <<'EOF'
# Acquire::GzipIndexes "true";
# Acquire::CompressionTypes::Order:: "gz";
# EOF
# update "autoremove" configuration to be aggressive about removing suggests
# deps that weren't manually installed, see
# https://aptitude.alioth.debian.org/doc/en/ch02s05s05.html#configApt-AutoRemove-SuggestsImportant
cat > "$root_dir/etc/apt/apt.conf.d/autoremove-suggests${suffix}" <<'EOF'
Apt::AutoRemove::SuggestsImportant "false";
EOF
# enable Universe repositories which is disabled in Ubuntu Core by default
sed -ri 's/# (deb .*universe)/\1/' "$root_dir/etc/apt/sources.list"
}
inject_resolv_conf() {
rm -f "$root_dir/etc/resolv.conf"
cp -L "/etc/resolv.conf" "$root_dir/etc/resolv.conf"
}
setup_locale() {
cat > "$root_dir/etc/profile.d/locale${suffix}.sh" <<'EOF'
export LANG=C.UTF-8
EOF
}
setup_proxy() {
local profile_config="$root_dir/etc/profile.d/proxy${suffix}.sh"
local apt_config="$root_dir/etc/apt/apt.conf.d/proxy${suffix}"
# NOTE: setting proxy for apt is needed to get it to work during the init
if [ "${http_proxy-}" -o "${https_proxy-}" -o "${ftp_proxy-}" ]; then
: > "$profile_config"
if [ "${http_proxy-}" ]; then
echo "export http_proxy=\"$http_proxy\"" >> "$profile_config"
echo "Acquire::http::Proxy \"$http_proxy\";" > "$apt_config"
fi
if [ "${https_proxy-}" ]; then
echo "export https_proxy=\"$https_proxy\"" >> "$profile_config"
echo "Acquire::http::Proxy \"$https_proxy\";" > "$apt_config"
fi
if [ "${ftp_proxy-}" ]; then
echo "export ftp_proxy=\"$ftp_proxy\"" >> "$profile_config"
fi
if [ "${no_proxy-}" ]; then
echo "export no_proxy=\"$no_proxy\"" >> "$profile_config"
fi
fi
}
# oracle java installer
add_webupda8team_ppa() {
echo "deb http://ppa.launchpad.net/webupd8team/java/ubuntu $codename main" \
> "$root_dir/etc/apt/sources.list.d/webupd8team-java.list"
in_chroot apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
echo "debconf shared/accepted-oracle-license-v1-1 select true" \
| in_chroot debconf-set-selections
echo "debconf shared/accepted-oracle-license-v1-1 seen true" \
| in_chroot debconf-set-selections
}
install_java6() {
in_chroot apt-get -y --no-install-recommends install oracle-java6-installer
in_chroot rm -rf /var/cache/oracle-jdk6-installer
}
install_java8_openjdk() {
in_chroot apt-get -y install openjdk-8-jdk
}
add_wine_ppa() {
echo "deb http://ppa.launchpad.net/ubuntu-wine/ppa/ubuntu $codename main" \
> "$root_dir/etc/apt/sources.list.d/ubuntu-wine.list"
in_chroot apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys F9CB8DB0
}
install_wine()
{
in_chroot apt-get -y --allow-unauthenticated install wine1.8
}
add_perforce_ppa() {
wget -qO - https://package.perforce.com/perforce.pubkey | in_chroot apt-key add -
echo "deb http://package.perforce.com/apt/ubuntu $codename release" \
> "$root_dir/etc/apt/sources.list.d/perforce.list"
}
install_perforce()
{
in_chroot apt-get -y --allow-unauthenticated install helix-cli
}
enable_backports() {
sed -ri "s/# (dev .*$codename-backports)/\1" "$root_dir/etc/apt/sources.list"
}
add_i386_arch() {
in_chroot dpkg --add-architecture i386
}
update_apt() {
[ "${skip_update-}" ] && return 0
in_chroot apt-get update
}
upgrade_dist() {
in_chroot apt-get dist-upgrade -y
}
cleanup_apt() {
in_chroot apt-get -y autoremove
in_chroot apt-get -y clean
#rm -rf /var/lib/apt/lists/* /var/tmp/* /tmp/*
}
install_dev_tools() {
in_chroot apt-get -y --no-install-recommends install \
mercurial \
smbclient
in_chroot apt-get -y install \
autoconf \
autopoint \
bash-completion \
bison \
build-essential \
ccache \
cmake \
cpp \
cpp-4.8 \
cryptsetup-bin \
curl \
flex \
g++ \
g++-4.8 \
g++-multilib \
gawk \
gcc \
gcc-4.8 \
genromfs \
gettext \
git \
gperf \
help2man \
intltool \
libdbus-1-dev \
libexpat1-dev \
libgcrypt11-dev \
libglib2.0-dev \
libgpgme11-dev \
libreadline-dev \
libsqlite3-dev \
libswitch-perl \
libtool \
libxml2-utils \
lua5.1 \
ncurses-dev \
ninja-build \
pkg-config \
python \
python-dev \
realpath \
squashfs-tools \
sudo \
u-boot-tools \
xsltproc \
yasm \
zip
if [ "${with_i386-}" ]; then
in_chroot apt-get -y install \
zlib1g-dev:i386
fi
# for 'dig' command which some third-party scripts like too much
in_chroot apt-get -y install \
dnsutils
# for some other scripts
in_chroot apt-get -y install \
libterm-readkey-perl
# for OpenEmbedded
# texinfo provides 'makeinfo' which some build scripts need
in_chroot apt-get -y install \
chrpath \
cpio \
diffstat \
libsdl1.2-dev \
texinfo \
wget
in_chroot apt-get -y install \
python-gobject-2 python-gtk2
}
install_misc_tools() {
in_chroot apt-get -y install \
dialog \
man
}
add_user() {
local uid=1000
local user=dev
local group=users
local home_dir="$root_dir/home/$user"
local sudoers_file="$root_dir/etc/sudoers.d/$user"
in_chroot useradd -u $uid -g $group -G sudo -Nm $user
echo $user:$user | in_chroot chpasswd
chmod 0770 "$home_dir"
mkdir -p "$(dirname "$sudoers_file")"
cat > "$sudoers_file" <<EOF
$user ALL = (ALL) NOPASSWD: ALL
EOF
chmod 0640 "$sudoers_file"
}
main() {
if [ $# = 0 ]; then
show_usage
exit 0
fi
while [ $# -gt 0 ]; do
case $1 in
-h|--help)
show_usage; exit 0 ;;
--init)
with_debootstrap=1
with_resolv=1
with_locale=1
with_proxy=1
;;
--default)
with_debootstrap=1
with_resolv=1
with_locale=1
with_proxy=1
with_update=1
with_upgrade=1
with_user=1
;;
--dev)
with_debootstrap=1
with_resolv=1
with_locale=1
with_proxy=1
with_i386=1
with_dev_tools=1
with_update=1
with_upgrade=1
with_user=1
;;
--enable-backports)
enable_backports=1 ;;
--add-i386)
with_i386=1; with_update=1 ;;
--install-java6)
with_java6=1; with_update=1 ;;
--install-java8-openjdk)
with_java8_openjdk=1; with_update=1 ;;
--install-wine)
with_wine=1; with_update=1 ;;
--install-perforce)
with_perforce=1; with_update=1 ;;
--install-dev-tools)
with_dev_tools=1; with_update=1 ;;
--add-user)
with_user=1 ;;
--skip-update)
skip_update=1 ;;
-*)
die "invalid argument: $1" ;;
*)
if [ -z "${root_dir-}" ]; then
root_dir="$1"
fi ;;
esac
shift
done
if [ -z "$root_dir" ]; then
die "a root directory is not specified"
fi
if ! [ -d "$root_dir" ]; then
die "the specified root directory '$root_dir' is not exists"
fi
codename=$(get_codename)
[ "$codename" ] || \
die "Could not find distribution codename"
if [ "${with_debootstrap-}" ]; then
debootstrap_setup
fi
if [ "${with_resolv-}" ]; then
inject_resolv_conf
fi
if [ "${with_locale-}" ]; then
setup_locale
fi
if [ "${with_proxy-}" ]; then
setup_proxy
fi
if [ "${enable_backports-}" ]; then
enable_backports
fi
if [ "${with_i386-}" ]; then
add_i386_arch
fi
if [ "${with_java6-}" ]; then
add_webupda8team_ppa
fi
if [ "${with_wine-}" ]; then
add_wine_ppa
fi
if [ "${with_perforce-}" ]; then
add_perforce_ppa
fi
if [ "${with_update-}" ]; then
update_apt
fi
if [ "${with_upgrade-}" ]; then
upgrade_dist
fi
if [ "${with_java6-}" ]; then
install_java6
fi
if [ "${with_java8_openjdk-}" ]; then
install_java8_openjdk
fi
if [ "${with_wine-}" ]; then
install_wine
fi
if [ "${with_perforce-}" ]; then
install_perforce
fi
if [ "${with_dev_tools-}" ]; then
install_dev_tools
install_misc_tools
fi
if [ "${with_update-}" ]; then
cleanup_apt
fi
if [ "${with_user-}" ]; then
add_user
fi
}
main "$@"