-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinstall
executable file
·296 lines (243 loc) · 7.52 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
#!/usr/bin/env bash
#
# Copyright (c) 2017 Joyent Inc., All rights reserved.
#
# Install CentOS into a directory, modify the installation, then tar it up.
#
# shellcheck disable=2154
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 CentOS in a given directory in a given directory using a given mirror
Usage:
$0 -d <INSTALL_DIR> -m <MIRROR> -r <RELEASE_PACKAGE> -i <IMAGE_NAME> -p <NAME> -D <DESC> -u <DOCS>
Example:
$0 -d /data/chroot -m http://mirror.centos.org/centos/6/os/x86_64/Packages -r centos-release-6-6.el6.centos.12.2.x86_64.rpm -i lx-centos-6 -p "CentOS 6.6 LX Brand" -D "CentOS 6.6 64-bit lx-brand image." -u https://docs.joyent.com/images/container-native-linux
OPTIONS:
-d A path to the install directory
-m A URL for the desired archive mirror
-r The release package used for installing CentOS
-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
}
INSTALL_DIR=
MIRROR=
RELEASE_PACKAGE=
IMAGE_NAME=
NAME=
DESC=
DOCS=
while getopts "hd:m:r:i:p:D:u:" OPTION
do
case $OPTION in
h)
usage
exit
;;
d)
INSTALL_DIR=${OPTARG%/}
;;
m)
MIRROR=${OPTARG}
;;
r)
RELEASE_PACKAGE=${OPTARG}
;;
i)
IMAGE_NAME=${OPTARG}
;;
p)
NAME=${OPTARG}
;;
D)
DESC=${OPTARG}
;;
u)
DOCS=${OPTARG}
;;
*)
usage
exit 1
;;
esac
done
if [[ $# -eq 0 ]]; then
usage
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 ${RELEASE_PACKAGE} ]]; then
echo "Error: missing release package (-r) 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
echo "==> Installing CentOS into $INSTALL_DIR"
if [[ -d $INSTALL_DIR ]]; then
echo "====> Found previous chroot. Deleting and creating a new one."
rm -rf "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR"
mkdir -p "$INSTALL_DIR/var/lib/rpm"
fi
rpm --rebuilddb --root="$INSTALL_DIR"
echo "==> Getting $RELEASE_PACKAGE..."
curl -sS -o "/var/tmp/$RELEASE_PACKAGE" "$MIRROR/$RELEASE_PACKAGE"
echo "==> Downloading and installing signing keys"
RELEASES=( 6 7 )
URL=https://www.centos.org/keys
for RELEASE in "${RELEASES[@]}"; do
curl -sS -o "/etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-$RELEASE" "$URL/RPM-GPG-KEY-CentOS-$RELEASE"
done
rpm --import /etc/pki/rpm-gpg/*
echo "==> Veryfying $RELEASE_PACKAGE..."
rpm -K "/var/tmp/$RELEASE_PACKAGE"
echo "==> Installing rpm package..."
rpm -i --root="$INSTALL_DIR" --nodeps "/var/tmp/$RELEASE_PACKAGE"
echo "==> Installing Core package group..."
yum --installroot="$INSTALL_DIR" --setopt=group_package_types=mandatory \
-y groupinstall "Core"
echo "==> Setting TZ to UTC"
if [[ -f $INSTALL_DIR/etc/localtime ]]; then
rm "$INSTALL_DIR/etc/localtime"
fi
cp "$INSTALL_DIR/usr/share/zoneinfo/UTC" "$INSTALL_DIR/etc/localtime"
# Set locale.
# CentOS 6 does not have a /etc/os-release file
echo "==> Setting locale to en_US.UTF-8"
if [[ -f $INSTALL_DIR/etc/os-release ]]; then
# Set locale for CentOS 7 and up using /etc/locale.conf
# shellcheck source=/dev/null
CHECK_VERSION=$(. "$INSTALL_DIR/etc/os-release" && echo "$VERSION_ID")
if [[ "$CHECK_VERSION" -ge 7 ]]; then
echo "LANG=\"en_US.UTF-8\"" > "$INSTALL_DIR/etc/locale.conf"
fi
else
# Assume this must be CentOS 6 or earlier so we use /etc/sysconfig/i18n
echo "LANG=\"en_US.UTF-8\"" > "$INSTALL_DIR/etc/sysconfig/i18n"
fi
echo "==> Installing Base package group and additional packages..."
yum --installroot="$INSTALL_DIR" -y groupinstall "Base"
yum --installroot="$INSTALL_DIR" -y install gettext
echo "==> Updating packages..."
yum --installroot="$INSTALL_DIR" -y update
echo "==> Cleaning up yum cache..."
yum --installroot="$INSTALL_DIR" clean all
echo "==> Removing /var/tmp/$RELEASE_PACKAGE"
rm -rf "/var/tmp/$RELEASE_PACKAGE"
# Get release version via os-release
# CentOS 6 does not have this file
if [[ -f $INSTALL_DIR/etc/os-release ]]; then
# shellcheck source=/dev/null
CHECK_VERSION=$(. "$INSTALL_DIR/etc/os-release" && echo "$VERSION_ID")
if [[ "$CHECK_VERSION" -ge 7 ]]; then
# Systemd overrides in CentOS 7 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
# Override for httpd (apache) service
# See IMAGE-926 and OS-5304
SERVICES=( httpd )
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
OVERRIDE
done
fi
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 "==> Setting UsePrivilegeSeparation"
# The sandbox value causes an issue with lx-brand. Change to "yes"
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 "==> 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