forked from TritonDataCenter/debian-lx-brand-image-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·301 lines (244 loc) · 7.79 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
#!/usr/bin/env bash
#
# Copyright (c) 2015 Joyent Inc., All rights reserved.
#
# Install Debian 7 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)
usage() {
cat <<EOF
Install and modify Debian in a given directory in a given directory using a given mirror
Usage:
$0 -r <RELEASE> -d <INSTALL_DIR> -m <MIRROR> -i <IMAGE_NAME> -p <NAME> -D <DESC> -u <DOCS>
Example:
$0 -r wheezy -d /data/chroot -m http://httpredir.debian.org/debian/ -i lx-debian-7 -p "Debian 7 LX Brand" -D "Debian 7 64-bit lx-brand image." -u https://docs.joyent.com/images/container-native-linux
OPTIONS:
-r The desired release (e.g "wheezy")
-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]
-h Show this message
EOF
}
RELEASE=
INSTALL_DIR=
MIRROR=
IMAGE_NAME=
NAME=
DESC=
DOCS=
while getopts "hr:d:m:i:p:D:u:" OPTION
do
case $OPTION in
h)
usage
exit
;;
r)
RELEASE=${OPTARG}
;;
d)
INSTALL_DIR=${OPTARG%/}
;;
m)
MIRROR=${OPTARG}
;;
i)
IMAGE_NAME=${OPTARG}
;;
p)
NAME=${OPTARG}
;;
D)
DESC=${OPTARG}
;;
u)
DOCS=${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 [[ ! -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
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/dev/pts" $INSTALL_DIR/proc/mounts; then
echo "====> Unmounting /dev/pts in chroot"
umount -lf $INSTALL_DIR/dev/pts
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 Debian into $INSTALL_DIR"
if [[ -d $INSTALL_DIR ]]; then
echo "====> Found previous chroot. Deleting and creating a new one."
# umount previous /proc and /dev/pts if they exist
umount_chroot
rm -rf $INSTALL_DIR
mkdir -p $INSTALL_DIR
fi
debootstrap --components=main \
--include=apt-transport-https,ca-certificates,curl,dbus,gettext-base,keyboard-configuration,less,locales,openssh-server,sudo,wget,vim \
$RELEASE $INSTALL_DIR $MIRROR
echo "==> Done!"
echo "==> Mounting /proc and /dev/pts in chroot"
chroot $INSTALL_DIR mount proc /proc -t proc
mkdir -p $INSTALL_DIR/dev/pts
mount --bind /dev/pts $INSTALL_DIR/dev/pts
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"
echo "en_US.UTF-8 UTF-8" >> $INSTALL_DIR/etc/locale.gen
echo "LANG=\"en_US.UTF-8\"" > $INSTALL_DIR/etc/default/locale
chroot $INSTALL_DIR locale-gen
echo "==> Updating $INSTALL_DIR/etc/apt/sources.list"
cat << SOURCES > $INSTALL_DIR/etc/apt/sources.list
deb http://httpredir.debian.org/debian $RELEASE main
deb-src http://httpredir.debian.org/debian $RELEASE main
deb http://httpredir.debian.org/debian $RELEASE-updates main
deb-src http://httpredir.debian.org/debian $RELEASE-updates main
deb http://security.debian.org/ $RELEASE/updates main
deb-src http://security.debian.org/ $RELEASE/updates main
SOURCES
echo "==> Updating packages"
export DEBIAN_FRONTEND=noninteractive
chroot $INSTALL_DIR apt-get -qq update
chroot $INSTALL_DIR apt-get -qq upgrade
# Get release version via os-release
VERSION_ID=
# shellcheck source=/dev/null
CHECK_VERSION=$(. $INSTALL_DIR/etc/os-release && echo $VERSION_ID)
echo "==> Installing additional packages"
if [[ "$CHECK_VERSION" -ge 9 ]] || [[ -z "$CHECK_VERSION" ]]; then
chroot $INSTALL_DIR apt-get -qq install --no-install-recommends software-properties-common
else
chroot $INSTALL_DIR apt-get -qq install --no-install-recommends python-software-properties software-properties-common
fi
if [[ "$CHECK_VERSION" -ge 8 ]]; then
# Pin udev version for Debian 8 and newer
# See https://github.com/joyent/smartos-live/issues/596
echo "==> Pinning udev package"
cat <<-UDEV > $INSTALL_DIR/etc/apt/preferences.d/udev
Package: udev
Pin: release *
Pin-Priority: -1
UDEV
# Systemd overrides Debian 8 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 "==> Cleaning up caches etc."
chroot $INSTALL_DIR apt-get -qq autoremove
chroot $INSTALL_DIR apt-get -qq clean
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 "==> Disable UsePrivilegeSeparation"
sed -ri s/^#?UsePrivilegeSeparation\ sandbox/UsePrivilegeSeparation\ no/ -i $INSTALL_DIR/etc/ssh/sshd_config
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
Documentation: $DOCS
Description: $DESC
PRODUCT
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}
echo "==> Saving installation as $TARGET. This may take a few minutes."
tar czf $TARGET --exclude-from=exclude.txt $INSTALL_DIR/
echo "==> Installation complete!"
echo "==> $TARGET"
# umount previous /proc and /dev/pts if they exist
umount_chroot
exit 0