forked from TritonDataCenter/debian-lx-brand-image-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-lx-image
executable file
·261 lines (214 loc) · 5.58 KB
/
create-lx-image
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
#!/usr/bin/env bash
#
# Copyright (c) 2015 Joyent Inc., All rights reserved.
#
# Create an lx-brand image from a given tar file
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'
BUILD_DATE=$(date +%Y%m%d)
TARBALL=
KERNEL=
MIN_PLATFORM=
IMAGE_NAME=
DESC=
DOCS=
usage() {
cat <<EOF
Create an lx brand Debian image from a given tar file
Usage:
$0 -t <TARBALL> -k <KERNEL> -m <MIN_PLATFORM> -i <IMAGE_NAME> -d <DESC> -u <DOCS>
Example:
$0 -t /var/tmp/lx-debian-7-20150408.tar.gz -k 3.13.0 -m 20150316T201553Z -i lx-debian-7 -d "Debian 7 64-bit lx-brand image." -u https://docs.joyent.com/images/container-native-linux
OPTIONS:
-t The full path to the tar archive
-k The kernel version
-m The minimum platform required for the image
-i The name of the image as it would appear in the manifest
-d The description of the image as it would appear in the manifest
-u The homepage link of the image as it would appear in the manifest
-h Show this message
EOF
}
while getopts "ht:k:m:i:d:u:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
t)
TARBALL=${OPTARG}
;;
k)
KERNEL=${OPTARG}
;;
m)
MIN_PLATFORM=${OPTARG}
;;
i)
IMAGE_NAME=${OPTARG}
;;
d)
DESC=${OPTARG}
;;
u)
DOCS=${OPTARG}
;;
?)
usage
exit
;;
esac
done
if [[ -z $TARBALL ]]; then
echo "==> ERROR: the install archive is required"
usage
exit 1
fi
if [[ ! -a "$TARBALL" ]]; then
printf "==> ERROR: %s: file or directory not found\n" $TARBALL
exit 1
fi
if [[ "$(echo $TARBALL | cut -c 1)" != "/" ]]; then
printf "==> Pathname '%s' specified to -t must be absolute\n" $TARBALL
exit 1
fi
if [[ ! -r "$TARBALL" ]]; then
printf "==> Cannot read file '%s'\n" $TARBALL
exit 1
fi
if [[ ! -f "$TARBALL" ]]; then
echo "==> ERROR: must be a gzip, bzip2, .Z or uncompressed tar archive"
exit 1
fi
if [[ -z ${KERNEL} ]]; then
echo "Error: missing kernel version (-k) value"
exit 1
fi
if [[ -z ${MIN_PLATFORM} ]]; then
echo "Error: missing minimum platform (-m) value"
exit 1
fi
if [[ -z ${IMAGE_NAME} ]]; then
echo "Error: missing image name (-i) value"
exit 1
fi
if [[ -z ${DESC} ]]; then
echo "Error: missing image description (-d) value"
exit 1
fi
if [[ -z ${DOCS} ]]; then
DOCS="https://docs.joyent.com/images/container-native-linux"
fi
IUUID=${IMAGE_NAME}-$BUILD_DATE
filetype=$({ LC_ALL=C file $TARBALL | awk '{print $2}' ; } 2>/dev/null)
if [[ "$filetype" = "gzip" ]]; then
gtaropts="-xz"
elif [[ "$filetype" = "bzip2" ]]; then
gtaropts="-xj"
elif [[ "$filetype" = "compressed" ]]; then
gtaropts="-xZ"
elif [[ "$filetype" = "USTAR" ]]; then
gtaropts="-x"
else
printf "==> ERROR: must be a gzip, bzip2, .Z or uncompressed tar archive"
exit 1
fi
gtaropts="${gtaropts}f"
zpath="/zones/$IUUID"
zroot="/zones/$IUUID/root"
create_dataset() {
echo "==> Creating dataset for image creation..."
zfs create zones/$IUUID
chmod 700 $zpath
mkdir $zpath/root
chmod 755 $zpath/root
mkdir $zpath/cores
chmod 755 $zpath/cores
echo "==> The dataset is zones/$IUUID"
echo "==>"
}
install_tar() {
echo "==> Installing the tar archive, this will take a few minutes..."
( cd "$zroot" && gtar --strip-components=2 "$gtaropts" "$TARBALL" )
if [[ "$?" -ne "0" ]] ; then
echo "==> Error: extraction from tar archive failed."
zfs destroy -r zones/$IUUID
fi
}
modify_image() {
echo "==> Modifying the image to work in a zone..."
echo "====> Creating /native directories..."
mkdir -p $zroot/native/dev
mkdir -p $zroot/native/etc/default
mkdir -p $zroot/native/etc/svc/volatile
mkdir -p $zroot/native/lib
mkdir -p $zroot/native/proc
mkdir -p $zroot/native/tmp
chmod 1777 $zroot/native/tmp
mkdir -p $zroot/native/usr
mkdir -p $zroot/native/var
echo "====> done."
# Create the /var/ld/ld.config files that will point to /native/lib for our
# Solaris libraries.
echo "====> Creating the /var/ld/ld.config file..."
mkdir $zroot/var/ld
mkdir $zroot/var/ld/64
if crle -c $zroot/var/ld/ld.config -l /native/lib:/native/usr/lib \
-s /native/lib/secure:/native/usr/lib/secure ; then
echo "====> Created \"$zroot/var/ld/ld.config\""
else
echo "====> Creation of \"$zroot/var/ld/ld.config\" failed!"
exit 1
fi
if crle -64 -c $zroot/var/ld/64/ld.config \
-l /native/lib/amd64:/native/usr/lib/amd64 \
-s /native/lib/secure/amd64:/native/usr/lib/secure/amd64 ; then
echo "====> Created \"$zroot/var/ld/64/ld.config\""
else
echo "====> Creation of \"$zroot/var/ld/64/ld.config\" failed!"
exit 1
fi
echo "====> Setting up fstab..."
cat << EOF > $zroot/etc/fstab
none / zfs defaults 1 1
proc /proc proc defaults 0 0
EOF
echo "==> Image modification complete."
}
create_file() {
echo "==> Creating image file:"
echo "====> Creating snapshot..."
zfs snapshot zones/$IUUID@final
echo "====> Doing a zfs send. This may take a few minutes..."
zfs send zones/$IUUID@final | gzip -9 > ${IMAGE_NAME}-${BUILD_DATE}.zfs.gz
echo "====> Done."
echo "====> Cleaning up..."
zfs destroy -r zones/$IUUID
echo "==> Image File created!"
echo "==>"
}
create_manifest() {
echo "==> Creating manifest file..."
./create-manifest -f ${IMAGE_NAME}-${BUILD_DATE}.zfs.gz -k ${KERNEL} -m ${MIN_PLATFORM} -n ${IMAGE_NAME} -o linux -v ${BUILD_DATE} -d ${DESC} -h ${DOCS} > ${IMAGE_NAME}-${BUILD_DATE}.json
echo "==> done!"
echo "==>"
}
show_image_files() {
echo "*** Image creation complete ***"
echo "==> Image files:"
echo "${IMAGE_NAME}-${BUILD_DATE}.zfs.gz"
echo "${IMAGE_NAME}-${BUILD_DATE}.json"
echo ""
}
create_dataset
install_tar
modify_image
create_file
create_manifest
show_image_files
exit 0