-
Notifications
You must be signed in to change notification settings - Fork 8
/
install
executable file
·294 lines (233 loc) · 7.18 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
#!/usr/bin/env bash
#
# Copyright (c) 2015 Joyent Inc., All rights reserved.
#
# Install Alpine 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)
export PATH=$PATH:/sbin:/bin
usage() {
cat <<EOF
Install and modify Alpine in a given directory in a given directory using a given mirror
Usage:
$0 -r <RELEASE> -a <APK_TOOLS> -d <INSTALL_DIR> -m <MIRROR> -i <IMAGE_NAME> -p <NAME> -D <DESC> -u <DOCS>
Example:
$0 -r 3.1 -a apk-tools-static-2.6.1-r0.apk -d /data/alpine -m http://mirrors.gigenet.com/alpinelinux/ -i alpine-3 -p "Alpine Linux" -D "Alpine 3.1.3 64-bit lx-brand image." -u https://docs.joyent.com/images/container-native-linux
OPTIONS:
-r The desired release (e.g "3.2")
-a The required apk-tools-static package (e.g. apk-tools-static-2.6.1-r0.apk)
-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=
APK_TOOLS=
INSTALL_DIR=
MIRROR=
IMAGE_NAME=
NAME=
DESC=
DOCS=
while getopts "hr:a:d:m:i:p:D:u:" OPTION
do
case $OPTION in
h)
usage
exit
;;
r)
RELEASE=${OPTARG}
;;
a)
APK_TOOLS=${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 [[ -z ${APK_TOOLS} ]]; then
echo "Error: missing apk-tools-static package (-a) 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
echo "==> Installing Alpine into $INSTALL_DIR"
if [[ -d $INSTALL_DIR ]]; then
echo "====> Found previous chroot. Deleting and creating a new one."
if [[ $(mount | grep -c $INSTALL_DIR/proc) -gt 0 ]]; then
umount $INSTALL_DIR/proc
fi
if [[ $(mount | grep -c $INSTALL_DIR/sys) -gt 0 ]]; then
umount $INSTALL_DIR/sys
fi
rm -rf $INSTALL_DIR
mkdir -p $INSTALL_DIR
fi
echo "===> Getting Alpine $RELEASE..."
if [[ ! -d tmp/ ]]; then
echo "===> creating tmp/ directory"
mkdir tmp
fi
cd tmp/
curl -sSO -f $MIRROR/v$RELEASE/main/x86_64/$APK_TOOLS
echo "==> Extracting $APK_TOOLS"
tar -xzf $APK_TOOLS
echo "==> Adding alpine-devel@lists.alpinelinux.org keys"
mkdir -p $INSTALL_DIR/etc/apk/keys
alpinekeys=( 4a6a0840 4d07755e 5243ef4b 524d27bb 5261cecb )
for key in "${alpinekeys[@]}"; do
curl -sSo $INSTALL_DIR/etc/apk/keys/alpine-devel@lists.alpinelinux.org-$key.rsa.pub https://alpinelinux.org/keys/alpine-devel%40lists.alpinelinux.org-$key.rsa.pub
done
echo "===> Installing Alpine base"
./sbin/apk.static -X $MIRROR/v$RELEASE/main -U --root $INSTALL_DIR --initdb add alpine-base
cd ..
echo "==> Setting up resolv.conf"
echo "nameserver 8.8.8.8" > $INSTALL_DIR/etc/resolv.conf
echo "nameserver 8.8.4.4" >> $INSTALL_DIR/etc/resolv.conf
echo "==> Setting up apk repositories using $MIRROR"
mkdir -p $INSTALL_DIR/etc/apk
cat << APK > $INSTALL_DIR/etc/apk/repositories
$MIRROR/v$RELEASE/main
$MIRROR/v$RELEASE/community
APK
echo "==> Mounting /proc /sys and /dev in the $INSTALL_DIR"
mount -t proc none $INSTALL_DIR/proc
mount -o bind /sys $INSTALL_DIR/sys
echo "==> Setting TZ to UTC"
chroot $INSTALL_DIR apk add --update tzdata
cp $INSTALL_DIR/usr/share/zoneinfo/UTC $INSTALL_DIR/etc/localtime
chroot $INSTALL_DIR apk del --purge tzdata
echo "==> Installing packages..."
chroot $INSTALL_DIR apk update
chroot $INSTALL_DIR apk add --update bash curl gettext less man ncurses-terminfo openssh vim wget
echo "==> Removing Linux kernel packages"
#chroot $INSTALL_DIR apk del --purge linux-grsec
echo "==> Make sure apk packages are up-to-date"
chroot $INSTALL_DIR apk upgrade
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 "==> Enabling UsePrivilegeSeparation (default is 'sandbox')"
sed s/UsePrivilegeSeparation\ sandbox/UsePrivilegeSeparation\ yes/ -i $INSTALL_DIR/etc/ssh/sshd_config
sed s/#UsePrivilegeSeparation\ yes/UsePrivilegeSeparation\ yes/ -i $INSTALL_DIR/etc/ssh/sshd_config
echo "==> Adding initscripts to make sure Alpine is bootable"
chroot $INSTALL_DIR rc-update add devfs sysinit
chroot $INSTALL_DIR rc-update add dmesg sysinit
chroot $INSTALL_DIR rc-update add mdev sysinit
chroot $INSTALL_DIR rc-update add bootmisc boot
chroot $INSTALL_DIR rc-update add hostname boot
chroot $INSTALL_DIR rc-update add hwclock boot
chroot $INSTALL_DIR rc-update add modules boot
chroot $INSTALL_DIR rc-update add networking boot
chroot $INSTALL_DIR rc-update add sysctl boot
chroot $INSTALL_DIR rc-update add syslog boot
chroot $INSTALL_DIR rc-update add crond default
chroot $INSTALL_DIR rc-update add local default
chroot $INSTALL_DIR rc-update add sshd default
chroot $INSTALL_DIR rc-update add mount-ro shutdown
chroot $INSTALL_DIR rc-update add killprocs shutdown
chroot $INSTALL_DIR rc-update add savecache shutdown
echo "==> Making /sbin/init symlink relative"
rm $INSTALL_DIR/sbin/init
pushd $PWD
cd $INSTALL_DIR/sbin/
ln -s ../bin/busybox init
popd
#echo "==> Making /usr/bin/su symlink relative"
#rm $INSTALL_DIR/usr/bin/su
#pushd $PWD
#cd $INSTALL_DIR/usr/bin/
#ln -s ../../bin/bbsuid ./su
#popd
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 "==> Saving installation as $TARGET. This may take a few minutes."
tar czf $TARGET --exclude-from=exclude.txt $INSTALL_DIR/
echo "==> Installation complete!"
echo "==> $TARGET"
exit 0