-
Notifications
You must be signed in to change notification settings - Fork 11
/
install
executable file
·396 lines (323 loc) · 10.8 KB
/
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
#!/usr/bin/env bash
#
# Copyright (c) 2017 Joyent Inc., All rights reserved.
#
# Install Ubuntu 14.04 into a directory, modify the installation, then tar it up.
#
if [[ -n "$TRACE" ]]; then
export PS4='[\D{%FT%TZ}] ${BASH_SOURCE}:${LINENO}: ${FUNCNAME[0]:+${FUNCNAME[0]}(): }'
set -o xtrace
fi
set -euo pipefail
IFS=$'\n\t'
GUESTTOOLS=guesttools
BUILD_DATE=$(date +%Y%m%d)
EXCLUDES=$PWD/exclude.txt
usage() {
cat <<EOF
Install and modify Ubuntu in a given directory using a given mirror.
Usage:
$0 -r <RELEASE> -a <ARCH> -d <INSTALL_DIR> -m <MIRROR> -i <IMAGE_NAME> -p <NAME> -D <DESC> -u <DOCS> -c <CUSTOMIZE FILE> -b <BASE IMAGE>
Example:
$0 -r trusty -a amd64 -d /mnt/chroot -m http://archive.ubuntu.com/ubuntu/ -i lx-ubuntu-14.04 -p "Ubuntu 14.04 LX Brand" -D "Ubuntu 14.04 64-bit lx-brand image." -u https://docs.joyent.com/images/container-native-linux
OPTIONS:
-r The desired release (e.g "trusty")
-a The desired architecture. Defaults to amd64 (64-bit). Use "i386" for 32-bit. [optional]
-d A path to the install directory
-m A URL for the desired archive mirror
-i The name of the image. This is used for naming the tarball.
-p The proper name of the image. Use quotes. This is used in the MOTD and /etc/product file.
-D A description for the image. This is used in the image manifest and the /etc/product file.
-u A URL to the image docs [optional]
-c Customize file for image you want to create. customize.postgresql, customize.percona
-o Optional arguments to be passed to the customize file.
-b Base image name. ubuntu-14.04
-h Show this message
EOF
}
RELEASE=
ARCH=
INSTALL_DIR=
MIRROR=
IMAGE_NAME=
NAME=
DESC=
DOCS=
CUSTOMIZE=
CUSTOM_OPTS=
BASE_NAME=
while getopts "hr:a:d:m:i:p:D:u:c:o:b:" OPTION
do
case $OPTION in
h)
usage
exit
;;
r)
RELEASE=${OPTARG}
;;
a)
ARCH=${OPTARG}
;;
d)
INSTALL_DIR=${OPTARG%/}
;;
m)
MIRROR=${OPTARG}
;;
i)
IMAGE_NAME=${OPTARG}
;;
p)
NAME=${OPTARG}
;;
D)
DESC=${OPTARG}
;;
u)
DOCS=${OPTARG}
;;
c)
CUSTOMIZE=${OPTARG}
;;
o)
CUSTOM_OPTS=${OPTARG}
;;
b)
BASE_NAME=${OPTARG}
;;
\?)
usage
exit
;;
esac
done
if [[ $# -eq 0 ]]; then
usage
exit 1
fi
if [[ -z ${RELEASE} ]]; then
echo "Error: missing release (-r) value"
exit 1
fi
if [[ -z ${ARCH} ]]; then
ARCH=amd64
fi
if [[ ! -e ${INSTALL_DIR} ]] ; then
echo "Directory $INSTALL_DIR not found"
exit 1
fi
if [[ -z ${INSTALL_DIR} ]]; then
echo "Error: missing install directory (-d) value"
exit 1
fi
if [[ -z ${MIRROR} ]]; then
echo "Error: missing mirror (-m) value"
exit 1
fi
if [[ -z ${IMAGE_NAME} ]]; then
echo "Error: missing image name (-i) value"
exit 1
fi
if [[ -z ${NAME} ]]; then
echo "Error: missing proper name (-p) value"
exit 1
fi
if [[ -z ${DESC} ]]; then
echo "Error: missing image description (-D) value"
exit 1
fi
TARGET="${IMAGE_NAME}-${BUILD_DATE}.tar.gz"
if [[ -z ${DOCS} ]]; then
DOCS="https://docs.joyent.com/images/container-native-linux"
fi
umount_chroot() {
if grep -qs "$INSTALL_DIR/sys" $INSTALL_DIR/proc/mounts; then
echo "====> Unmounting /sys in chroot"
umount -lf $INSTALL_DIR/sys
fi
if grep -qs "$INSTALL_DIR/proc" $INSTALL_DIR/proc/mounts; then
echo "====> Unmounting /proc in chroot"
umount -lf $INSTALL_DIR/proc
fi
}
echo "==> Installing $NAME into $INSTALL_DIR"
if [[ -d $INSTALL_DIR ]]; then
echo "====> Found previous install directory. Deleting and creating a new one."
# umount previous /proc and /sys if they exist
umount_chroot
rm -rf $INSTALL_DIR
mkdir -p $INSTALL_DIR
fi
debootstrap --components=main --arch=$ARCH \
--include=apt-transport-https,ca-certificates,curl,dbus,gettext-base,less,man-db,openssh-server,sudo,wget,vim \
$RELEASE $INSTALL_DIR $MIRROR
echo "==> Done!"
echo "==> Mounting /proc and /sys in chroot"
chroot $INSTALL_DIR mount -t proc none /proc
chroot $INSTALL_DIR mount -t sysfs sysfs /sys
echo "==> Setting TZ to UTC"
rm $INSTALL_DIR/etc/localtime
cp $INSTALL_DIR/usr/share/zoneinfo/UTC $INSTALL_DIR/etc/localtime
echo "==> Setting locale to en_US.UTF-8"
export DEBIAN_FRONTEND=noninteractive
chroot $INSTALL_DIR locale-gen en_US.UTF-8
chroot $INSTALL_DIR dpkg-reconfigure locales
echo "LANG=\"en_US.UTF-8\"" > $INSTALL_DIR/etc/default/locale
echo "==> Updating $INSTALL_DIR/etc/apt/sources.list"
cat << SOURCES > $INSTALL_DIR/etc/apt/sources.list
deb http://archive.ubuntu.com/ubuntu $RELEASE main
deb-src http://archive.ubuntu.com/ubuntu $RELEASE main
deb http://archive.ubuntu.com/ubuntu $RELEASE-updates main
deb-src http://archive.ubuntu.com/ubuntu $RELEASE-updates main
deb http://security.ubuntu.com/ubuntu $RELEASE-security main
deb-src http://security.ubuntu.com/ubuntu $RELEASE-security main
deb http://archive.ubuntu.com/ubuntu $RELEASE universe
deb-src http://archive.ubuntu.com/ubuntu $RELEASE universe
deb http://archive.ubuntu.com/ubuntu $RELEASE-updates universe
deb-src http://archive.ubuntu.com/ubuntu $RELEASE-updates universe
deb http://security.ubuntu.com/ubuntu $RELEASE-security universe
deb-src http://security.ubuntu.com/ubuntu $RELEASE-security universe
SOURCES
echo "==> Updating packages"
# Disable services in chroot via divert before upgrading packages
# See https://bugs.launchpad.net/ubuntu/+source/upstart/+bug/430224
chroot $INSTALL_DIR dpkg-divert --local --rename --add /sbin/initctl
chroot $INSTALL_DIR ln -s /bin/true /sbin/initctl
chroot $INSTALL_DIR apt-get -qq update
chroot $INSTALL_DIR apt-get -qq upgrade
echo "==> Installing additional packages"
chroot $INSTALL_DIR apt-get -qq install --no-install-recommends python-software-properties software-properties-common
# Get release version via os-release
# shellcheck source=/dev/null
CHECK_VERSION=$(echo "$(. $INSTALL_DIR/etc/os-release && echo $VERSION_ID)>14.04" | bc)
if [[ "$CHECK_VERSION" -eq 1 ]]; then
# Uninstall udev
# See https://github.com/joyent/smartos-live/issues/596
echo "Removing udev package"
chroot $INSTALL_DIR apt-get -qq remove udev
chroot $INSTALL_DIR apt-get -qq autoremove
# Systemd overrides for Ubuntu 16.04 and newer
#
# See:
# - https://github.com/joyent/centos-lx-brand-image-builder/issues/5
# - https://github.com/joyent/centos-lx-brand-image-builder/issues/7
# - https://smartos.org/bugview/OS-5304
#
# TODO: This should be removed when the relevant cgroup etc support is in
# the platform.
SERVICES=( systemd-hostnamed systemd-localed systemd-timedated )
for SERVICE in "${SERVICES[@]}"; do
echo "==> Adding systemd overrides for $SERVICE"
OVERRIDE_DIR=$INSTALL_DIR/etc/systemd/system/${SERVICE}.service.d
mkdir -p $OVERRIDE_DIR
cat << OVERRIDE > ${OVERRIDE_DIR}/override.conf
[Service]
PrivateTmp=no
PrivateDevices=no
PrivateNetwork=no
ProtectSystem=no
ProtectHome=no
OVERRIDE
done
fi
echo "==> Pinning makedev package"
# Prevents apt-get upgrade issue when upgrading in a container environment.
# Similar to https://bugs.launchpad.net/ubuntu/+source/makedev/+bug/1675163
cat << MAKEDEV > $INSTALL_DIR/etc/apt/preferences.d/makedev
Package: makedev
Pin: release *
Pin-Priority: -1
MAKEDEV
echo "==> Cleaning up caches etc."
chroot $INSTALL_DIR apt-get -qq autoremove
chroot $INSTALL_DIR apt-get -qq clean
# Remove the divert that disables services
chroot $INSTALL_DIR rm -f /sbin/initctl
chroot $INSTALL_DIR dpkg-divert --local --rename --remove /sbin/initctl
# Remove weekly cron for fstrim which doesn't make much sense for an
# lx-brand image.
if [[ -f $INSTALL_DIR/etc/cron.weekly/fstrim ]]; then
echo "==> Removing /etc/cron.weekly/fstrim"
rm -rf $INSTALL_DIR/etc/cron.weekly/fstrim
fi
echo "==> Disabling PasswordAuthentication"
sed -ri s/^#?PasswordAuthentication\ no/PasswordAuthentication\ no/ -i $INSTALL_DIR/etc/ssh/sshd_config
sed -ri s/^#?PasswordAuthentication\ yes/PasswordAuthentication\ no/ -i $INSTALL_DIR/etc/ssh/sshd_config
echo "==> Removing resolvconf entries in $INSTALL_DIR/etc/resolvconf/resolv.conf.d/"
rm -rf $INSTALL_DIR/etc/resolvconf/resolv.conf.d/tail
rm -rf $INSTALL_DIR/etc/resolvconf/resolv.conf.d/original
echo "==> Adding getty service on ttyS0 for 'vmadm console' command"
cat << TTYS0 > $INSTALL_DIR/etc/init/ttyS0.conf
# ttyS0 - getty
#
# This service maintains a getty on ttyS0 from the point the system is
# started until it is shut down again.
# On SmartOS and Smart Data Center this allows you to attach a console
# via the 'vmadm console UUID' command
start on stopped rc RUNLEVEL=[2345] and (
not-container or
container CONTAINER=lxc or
container CONTAINER=lxc-libvirt)
stop on runlevel [!2345]
respawn
exec /sbin/getty -L 115200 ttyS0 vt102
TTYS0
echo "==> Creating /etc/motd"
cat << MOTD > $INSTALL_DIR/etc/motd
__ . .
_| |_ | .-. . . .-. :--. |-
|_ _| ;| || |(.-' | | |
|__| \`--' \`-' \`;-| \`-' ' ' \`-'
/ ; Instance ($NAME $BUILD_DATE)
\`-' $DOCS
MOTD
echo "==> Creating /etc/product file"
cat << PRODUCT > $INSTALL_DIR/etc/product
Name: Joyent Instance
Image: $NAME $BUILD_DATE
Base Image: $BASE_NAME
Documentation: $DOCS
Description: $DESC
PRODUCT
if [[ ! -d $INSTALL_DIR/var/svc ]]; then
echo "==> Creating $INSTALL_DIR/var/svc"
mkdir -p $INSTALL_DIR/var/svc
fi
if [[ ! -d $INSTALL_DIR/var/db ]]; then
echo "==> Creating $INSTALL_DIR/var/db"
mkdir -p $INSTALL_DIR/var/db
fi
echo "==> Installing Guest tools in $INSTALL_DIR"
echo "====> Initiallizing and fetching submodule $GUESTTOOLS"
git submodule init
git submodule update
echo "====> Running ./install.sh -i $INSTALL_DIR"
(
cd $GUESTTOOLS
./install.sh -i $INSTALL_DIR
)
echo "==> Installing dtracetools-lx in $INSTALL_DIR"
echo "====> Downloading package and SHASUMs from https://us-east.manta.joyent.com/joyentsoln/public/"
DTRACE_PACKAGE="dtracetools-lx_1.0_amd64.deb"
D_PKG_SHASUM="shasum256.txt"
curl -sSLO https://us-east.manta.joyent.com/joyentsoln/public/images/lx-brand/devel/packages/${DTRACE_PACKAGE}
curl -sSLO https://us-east.manta.joyent.com/joyentsoln/public/images/lx-brand/devel/packages/${D_PKG_SHASUM}
echo "====> Verifying package"
grep " ${DTRACE_PACKAGE}\$" ${D_PKG_SHASUM} | sha256sum -c -
echo "====> Installing into $INSTALL_DIR"
dpkg --root $INSTALL_DIR -i ${DTRACE_PACKAGE}
if [[ ! -z ${CUSTOMIZE} ]]; then
echo "==> Executing ${CUSTOMIZE} file.."
chmod +x ${CUSTOMIZE}
if [[ -z ${CUSTOM_OPTS} ]]; then
INSTALL_DIR="${INSTALL_DIR}" ./${CUSTOMIZE}
else
INSTALL_DIR="${INSTALL_DIR}" ./${CUSTOMIZE} ${CUSTOM_OPTS}
fi
fi
echo "==> Saving installation as $TARGET. This may take a few minutes."
tar --numeric-owner --create --auto-compress --exclude-from=$EXCLUDES --file "$TARGET" --directory "$INSTALL_DIR" --transform='s,^./,,' .
echo "==> Installation complete!"
echo "==> $TARGET"
exit 0