forked from TritonDataCenter/centos-lx-brand-image-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·356 lines (298 loc) · 11 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
#!/usr/bin/env bash
#
# Copyright (c) 2017 Joyent Inc., All rights reserved.
#
# Install RHEL 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() {
# CentOS invocation 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
cat <<EOF
Install and modify RHEL 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 invocation using a mirror site to download the redhat-release rpm:
$0 -d /data/chroot -m https://access.cdn.redhat.com//content/origin/rpms/redhat-release-server/7.4/18.el7/fd431d51 -r redhat-release-server-7.4-18.el7.x86_64.rpm -a "_auth_=1519933726_bb24fee620873fc44c2576d565fdcbd7" -i lx-redhat-7 -p "RedHat 7.4 LX Brand" -D "RedHat 7.4 64-bit lx-brand image."
Example invocation NOT using a mirror site to download the redhat-release rpm:
$0 -d /opt/chroot -r redhat-release-server-7.4-18.el7.x86_64.rpm -i lx-redhat-7 -p "RedHat 7.4 LX Brand" -D "RedHat 7.4 64-bit lx-brand image."
OPTIONS:
-d A path to the install directory
-m A URL for the desired archive mirror in which the redhat-release rpm will be fetched from
-a Authentication URL params for rpm mirror (eg. "_auth_=<redhat_customer_portal_auth_token>")
-r The filename of the redhat-release rpm used for installing RedHat
-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=
MIRROR_AUTH_PARAMS=
RELEASE_PACKAGE=
IMAGE_NAME=
NAME=
DESC=
DOCS=
while getopts "hd:m:a:r:i:p:D:u:" OPTION
do
case $OPTION in
h)
usage
exit
;;
d)
INSTALL_DIR=${OPTARG%/}
;;
m)
MIRROR=${OPTARG}
;;
a)
MIRROR_AUTH_PARAMS=${OPTARG}
;;
r)
RELEASE_PACKAGE=${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 [[ ! -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 RHEL 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
# initialize rpm database
rpm --root $INSTALL_DIR --initdb
# fetch OS release rpm via mirror
if [ -n "${MIRROR}" ]; then
echo "==> Getting $RELEASE_PACKAGE from mirror..."
rc=
# support optional mirror authentication URL params
if [[ -n ${MIRROR_AUTH_PARAMS} ]]; then
curl -sS --fail -o /var/tmp/$RELEASE_PACKAGE $MIRROR/$RELEASE_PACKAGE?$MIRROR_AUTH_PARAMS
rc=$?
else
curl -sS --fail -o /var/tmp/$RELEASE_PACKAGE $MIRROR/$RELEASE_PACKAGE
rc=$?
fi
# exit if the redhat-release rpm fetch failed
[ $rc -ne 0 ] && exit $rc
# download OS release rpm via yum
else
echo "==> Getting redhat-release rpm..."
# download and install the redhat-release package, it contains our repository sources
yum --noplugins reinstall --downloadonly --downloaddir . redhat-release
mv redhat-release*.rpm /var/tmp/$RELEASE_PACKAGE
fi
# 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 --root $INSTALL_DIR --import $INSTALL_DIR/etc/pki/rpm-gpg/*
echo "==> Verifying $RELEASE_PACKAGE..."
rpm -K /var/tmp/$RELEASE_PACKAGE
echo "==> Installing rpm package..."
rpm -ivh --root=$INSTALL_DIR --nosignature /var/tmp/$RELEASE_PACKAGE
rpm --root $INSTALL_DIR --import $INSTALL_DIR/etc/pki/rpm-gpg/*
# install yum without docs and install only the english language files during the process
# NOTE: the below command will likely result in the following error message:
# "This system is not registered with an entitlement server. You can use subscription-manager to register."
# but it still seems to continue so it should be able to be ignored but for some reason it wasn't able
# to be ignored on the rest of the "yum --installroot" calls below
# don't set the "override_install_langs" flag so that all the locales get installed
# don't set the "nodocs" flag so that man pages get installed
# yum --installroot=$INSTALL_DIR --noplugins --setopt=tsflags='nodocs' --setopt=override_install_langs=en_US.utf8 -y install yum
yum --installroot=$INSTALL_DIR --noplugins -y install yum
# It seems that after the Core group is installed you can't install any more packages
# unless you copy /etc/yum.repos.d/redhat.repo to $INSTALL_DIR/etc/yum.repos.d/
# otherwise yum complains and says that there are no repos. I'm not sure how
# installing yum and the Core group work before this though.
echo "==> Cloning yum redhat.repo file..."
[ -f "$INSTALL_DIR/etc/yum.repos.d/redhat.repo" ] && {
mv $INSTALL_DIR/etc/yum.repos.d/redhat.repo $INSTALL_DIR/etc/yum.repos.d/redhat.repo.orig
}
cp /etc/yum.repos.d/redhat.repo $INSTALL_DIR/etc/yum.repos.d/
echo "==> Installing Core package group..."
yum --installroot=$INSTALL_DIR --noplugins --setopt=group_package_types=mandatory -y groupinstall "Core"
echo "==> Setting TZ to PST"
if [[ -f $INSTALL_DIR/etc/localtime ]]; then
rm $INSTALL_DIR/etc/localtime
fi
# cp $INSTALL_DIR/usr/share/zoneinfo/UTC $INSTALL_DIR/etc/localtime
ln -s $INSTALL_DIR/usr/share/zoneinfo/America/Los_Angeles $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..."
# use the --noplugins flag so that we don't see the following warning from the subscription-manager plugin:
# "This system is not registered with an entitlement server. You can use subscription-manager to register."
yum --installroot=$INSTALL_DIR --noplugins -y groupinstall "Base"
yum --installroot=$INSTALL_DIR --noplugins -y install gettext # procps-ng iputils
echo "==> Updating packages..."
yum --installroot=$INSTALL_DIR --noplugins -y update
echo "==> Cleaning up yum cache..."
yum --installroot=$INSTALL_DIR --noplugins clean all
[ -f "$INSTALL_DIR/etc/yum.repos.d/redhat.repo.orig" ] && {
echo "==> Restoring original yum redhat.repo file..."
mv -f $INSTALL_DIR/etc/yum.repos.d/redhat.repo.orig $INSTALL_DIR/etc/yum.repos.d/redhat.repo
}
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
# NOTE: commented out the below code because SSH is not installed now that we can't install
# the additional yum packages and package groups
# 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
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