forked from TritonDataCenter/mi-centos-7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-image
executable file
·270 lines (237 loc) · 5.69 KB
/
create-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
262
263
264
265
266
267
268
269
270
#!/usr/bin/env bash
#
# Copyright (c) 2015 Joyent Inc., All rights reserved.
#
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'
ISO=
IMAGE_NAME=
OWNER_UUID=
DESC=
HOMEPAGE=
IP=
NETMASK=
GATEWAY=
VLAN_ID=
NETWORK_UUID=
BLANK_MANIFEST=blank.json
BUILD_DATE=$(date +%Y%m%d)
VM_UUID=
usage() {
cat <<EOF
Create an kvm image from a given ISO file.
Usage:
$0 -i <ISO> -n <IMAGE_NAME> -d <DESC> -u <HOMEPAGE> -o <OWNER_UUID> -p <IP> -m NETMASK -g <GATEWAY> -v <VLAN_ID> -U <NETWORK_UUID>
Example:
$0 -i custom.iso -n centos-7 -d "CentOS 7 64-bit image with just essential packages installed. Ideal for users who are comfortable with setting up their own environment and tools." -u https://docs.joyent.com/images/linux/centos -o 00000000-0000-0000-0000-000000000000 -p 192.0.2.21 -m <NETMASK> -g <GATEWAY> -v <VLAN_ID> -U <NETWORK_UUID>
OPTIONS:
-i The ISO
-n The name of the image as it would appear in the manifest
-d The description of the image
-u The homepage URL for the image
-o The onwer UUID for the blank VM
-p The IP address for the blank VM
-m The netmask for the blank VM
-g The gateway for the blank VM
-v The vlan ID for blank VM
-U The Network UUID for the blank vm
-h Show this message
EOF
}
while getopts "hi:n:d:u:o:p:m:g:v:U:" OPTION
do
case $OPTION in
h)
usage
exit 1
;;
i)
ISO=${OPTARG}
;;
n)
IMAGE_NAME=${OPTARG}
;;
d)
DESC=${OPTARG}
;;
u)
HOMEPAGE=${OPTARG}
;;
o)
OWNER_UUID=${OPTARG}
;;
p)
IP=${OPTARG}
;;
m)
NETMASK=${OPTARG}
;;
g)
GATEWAY=${OPTARG}
;;
v)
VLAN_ID=${OPTARG}
;;
U)
NETWORK_UUID=${OPTARG}
;;
?)
usage
exit 1
;;
esac
done
if [[ -z ${ISO} || -z ${IMAGE_NAME} || -z ${DESC} || -z ${HOMEPAGE} \
|| -z ${OWNER_UUID} || -z ${IP} || -z ${NETMASK} || -z ${VLAN_ID} \
|| -z ${NETWORK_UUID} || -z ${GATEWAY} ]]; then
echo "FATAL: All of -i, -n, -d, -u, -o, -p, -m, -g, -v, and -U are required."
usage
exit 1
fi
# The manifest for the blank VM, saved as blank.json
cat << MANIFEST > $BLANK_MANIFEST
{
"brand": "kvm",
"alias": "$IMAGE_NAME",
"hostname": "$IMAGE_NAME",
"autoboot": false,
"ram": 1024,
"vcpus": 2,
"cpu_type": "host",
"disk_driver": "virtio",
"nic_driver": "virtio",
"max_physical_memory": 2048,
"quota": 10,
"disks": [
{
"boot": true,
"model": "virtio",
"size": 10240
}
],
"owner_uuid": "$OWNER_UUID",
"resolvers": [
"8.8.8.8",
"8.8.4.4"
],
"nics": [
{
"interface": "net0",
"nic_tag": "external",
"ip": "$IP",
"primary": "true",
"netmask": "$NETMASK",
"gateway": "$GATEWAY",
"vlan_id": $VLAN_ID,
"model": "virtio",
"network_uuid": "$NETWORK_UUID"
}
]
}
MANIFEST
check_for_blank() {
echo "==> Checking for existing blank $IMAGE_NAME VM."
set +e
blank=$(pfexec vmadm list | grep $IMAGE_NAME)
if [[ $? -eq 0 ]]; then
echo "==> Blank VM $IMAGE_NAME found:"
echo "==> $blank"
echo -n "==> Deleting..."
blank_uuid=$(echo $blank | cut -f1 -d ' ')
pfexec vmadm delete $blank_uuid
echo "done!"
echo "==> Sleeping for 5 seconds before we continue."
sleep 5
else
echo "No Blank VM $IMAGE_NAME found. It's safe to create a new one."
fi
set -e
echo "==>"
}
create_blank() {
echo -n "==> Creating blank $IMAGE_NAME VM and getting VM_UUID..."
VM_UUID=$(pfexec vmadm create -f $BLANK_MANIFEST 2>&1 | awk '{print $4;}')
echo "done!"
echo "==> VM_UUID is ${VM_UUID}"
echo "==>"
}
copy_iso() {
echo -n "==> Copying $ISO to /zones/${VM_UUID}/root/..."
pfexec cp $ISO /zones/${VM_UUID}/root/
echo "done!"
echo "==>"
}
start_blank() {
echo "==> Starting ${VM_UUID} with cdrom=/$ISO:"
pfexec vmadm start ${VM_UUID} order=cd,once=d cdrom=/$ISO,ide
echo "==>"
}
get_VNC() {
echo "==> Getting VNC info for ${VM_UUID}:"
host=$(pfexec vmadm info ${VM_UUID} vnc 2>&1 | json -a vnc.host)
port=$(pfexec vmadm info ${VM_UUID} vnc 2>&1 | json -a vnc.port)
echo "==> Host: $host"
echo "==> Port: $port"
echo "==>"
}
check_state() {
echo -n "==> Checking state of '$IMAGE_NAME' VM to ensure it's stopped before we proceed..."
STATE="running"
while [[ $STATE != "stopped" ]]; do
STATE=$(pfexec vmadm get ${VM_UUID} | json -a state)
echo -n "."
sleep 1
done
echo "ready!"
echo "==> The '$IMAGE_NAME' VM is stopped. Sleping for 15 seconds before we continue."
sleep 15
echo "==>"
}
snapshot() {
echo "==> Creating snapshot..."
zfs snapshot zones/${VM_UUID}-disk0@dataset-${BUILD_DATE}
echo "==> done!"
echo "==>"
}
create_file() {
echo "==> Creating image file..."
zfs send zones/${VM_UUID}-disk0@dataset-${BUILD_DATE} | gzip -9 > ${IMAGE_NAME}-${BUILD_DATE}.zfs.gz
echo "==> done!"
echo "==>"
}
create_manifest() {
echo "==> Creating manifest file..."
./sdc-vmmanifest -f ${IMAGE_NAME}-${BUILD_DATE}.zfs.gz -n ${IMAGE_NAME} -s 10240 -v ${BUILD_DATE} -d "$DESC" -h $HOMEPAGE -o linux > ${IMAGE_NAME}-${BUILD_DATE}.json
echo "==> done!"
echo "==>"
}
show_image_files() {
echo "*** Image creation complete ***"
echo "==> Image files:"
echo "${IMAGE_NAME}-${BUILD_DATE}.json"
echo "${IMAGE_NAME}-${BUILD_DATE}.zfs.gz"
echo ""
}
clean_up() {
rm -rf blank.json
echo "==> Deleting blank $IMAGE_NAME VM: ${VM_UUID}"
pfexec vmadm delete ${VM_UUID}
}
# MAIN
echo "*** Starting image creation process! ***"
check_for_blank
create_blank
copy_iso
start_blank
get_VNC
check_state
snapshot
create_file
create_manifest
show_image_files
clean_up
exit 0