-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathcreate_image.sh
executable file
·275 lines (237 loc) · 7.98 KB
/
create_image.sh
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
#!/bin/bash
# @(#)create_image.sh
#
#
# Copyright (C) 2013, GC3, University of Zurich. All rights
# reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2 of the License, or (at your
# option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
PROG="$(basename $0)"
usage () {
cat <<EOF
Usage: $PROG [options] kickstart name
A short description of what the program does should be here,
but it's not (yet).
Options:
--distribution, -d DISTRO
Specify the distribution to use. Valid values are:
- centos5: CentOS 5
- centos6: CentOS 6
- precise: Ubuntu Precise Pangolin 12.04 LTS
- quantal: Ubuntu Quantal Quetzal 12.10
- raring: Ubuntu Raring Ringtail 13.04
- saucy: Ubuntu Saucy Salamander 13.10
- trusty: Ubuntu Trusty Tahr 14.04 LTS
- wheezy: Debian Wheezy 7.1
--upload-to-glance, -u
Upload the image to glance. You need to set the proper
environment variables in order to make it work.
--upload-public-image, -p
Upload as public image. Implies '-u'
--gui, -x Install using the graphical interface instead of using the
terminal as a console
--help, -h Print this help text.
EOF
}
## helper functions
die () {
rc="$1"
shift
(echo -n "$PROG: ERROR: ";
if [ $# -gt 0 ]; then echo "$@"; else cat; fi) 1>&2
exit $rc
}
invalid_usage () {
rc="$1"
shift
(echo -n "$PROG: ERROR: ";
if [ $# -gt 0 ]; then echo "$@"; else cat; fi) 1>&2
echo
usage
exit $rc
}
warn () {
(echo -n "$PROG: WARNING: ";
if [ $# -gt 0 ]; then echo "$@"; else cat; fi) 1>&2
}
have_command () {
type "$1" >/dev/null 2>/dev/null
}
require_command () {
if ! have_command "$1"; then
die 1 "Could not find required command '$1' in system PATH. Aborting."
fi
}
is_absolute_path () {
expr match "$1" '/' >/dev/null 2>/dev/null
}
## parse command-line
short_opts='hd:uxp'
long_opts='help,distribution:,upload-to-glance,upload-public-image,gui'
if [ "x$(getopt -T)" != 'x--' ]; then
# GNU getopt
args=$(getopt --name "$PROG" --shell sh -l "$long_opts" -o "$short_opts" -- "$@")
if [ $? -ne 0 ]; then
die 1 "Type '$PROG --help' to get usage information."
fi
# use 'eval' to remove getopt quoting
eval set -- $args
else
# old-style getopt, use compatibility syntax
args=$(getopt "$short_opts" "$@")
if [ $? -ne 0 ]; then
die 1 "Type '$PROG --help' to get usage information."
fi
set -- $args
fi
DISTR=centos6
UPLOAD_TO_GLANCE=0
GLANCE_PUBLIC_IMAGE=false
GRAPHICS=--nographics
CONSOLE="text console=tty0 utf8 console=ttyS0,115200"
while [ $# -gt 0 ]; do
case "$1" in
--distribution|-d)
shift
DISTR=$1
;;
--upload-to-glance|-u)
UPLOAD_TO_GLANCE=1
require_command glance
;;
--upload-public-image|-p)
GLANCE_PUBLIC_IMAGE=true
;;
--gui|-x)
GRAPHICS="--graphics vnc"
CONSOLE="text console=tty0 utf8"
;;
--help|-h) usage; exit 0 ;;
--) shift; break ;;
esac
shift
done
ksfile=$1
name=$2
[ -z "$ksfile" ] && invalid_usage 1 "Missing 'kickstart' file"
[ -z "$name" ] && invalid_usage 1 "Missing 'name' argument"
[ -n "$3" ] && invalid_usage 1 "Invalid option '$3'"
ks=$(basename $ksfile)
if ! [ -f "$ksfile" ]; then
die 2 "Kickstart file $ks not found"
fi
case $DISTR in
centos5)
OSVARIANT=rhel5
OSLOCATION=http://mirror.centos.org/centos/5/os/x86_64
OSEXTRAARGS="ks=file:///$ks"
;;
centos6)
OSVARIANT=rhel6
OSLOCATION=http://mirrors.kernel.org/centos/6/os/x86_64
OSEXTRAARGS="ks=file:///$ks"
;;
precise)
OSVARIANT=ubuntuprecise
OSLOCATION=http://archive.ubuntu.com/ubuntu/dists/precise/main/installer-amd64/
OSEXTRAARGS="auto=true url=file:///$ks DEBCONF_DEBUG=5 netcfg/get_hostname=ubuntu"
;;
quantal)
OSVARIANT=ubuntuquantal
OSLOCATION=http://archive.ubuntu.com/ubuntu/dists/quantal/main/installer-amd64/
OSEXTRAARGS="auto=true url=file:///$ks DEBCONF_DEBUG=5 netcfg/get_hostname=ubuntu"
;;
raring)
OSVARIANT=ubunturaring
OSLOCATION=http://archive.ubuntu.com/ubuntu/dists/raring/main/installer-amd64/
OSEXTRAARGS="auto=true url=file:///$ks DEBCONF_DEBUG=5 netcfg/get_hostname=ubuntu"
;;
saucy)
OSVARIANT=ubuntusaucy
OSLOCATION=http://archive.ubuntu.com/ubuntu/dists/saucy/main/installer-amd64/
OSEXTRAARGS="auto=true url=file:///$ks DEBCONF_DEBUG=5 netcfg/get_hostname=ubuntu"
;;
trusty)
OSVARIANT=ubuntutrusty
OSLOCATION=http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/
OSEXTRAARGS="auto=true url=file:///$ks DEBCONF_DEBUG=5 netcfg/get_hostname=ubuntu"
;;
wheezy)
OSVARIANT=debianwheezy
OSLOCATION=http://ftp.ch.debian.org/debian/dists/stable/main/installer-amd64/
OSEXTRAARGS="auto=true url=file:///$ks DEBCONF_DEBUG=5 netcfg/get_hostname=debian netcfg/get_domain=localdomain"
;;
*)
die 2 "Distribution not supported."
;;
esac
require_command virt-install
require_command virt-sysprep
require_command virt-sparsify
sudo virt-install --version >/dev/null || die 3 "sudo command might not work. Check that you can run commands as root using sudo."
sanitized_name=$(echo -n "$name" | tr '[:space:]' '_' | tr -s '_' | tr '[A-Z]' '[a-z]')
imgfile=$sanitized_name.img
qcowfile=$sanitized_name.qcow2
# --nographics --os-type=linux \
echo "Running virt-install"
out=$(virt-install --name "$sanitized_name" \
--connect=qemu:///session \
--ram 1024 --cpu host --vcpus 1 \
--os-type=linux $GRAPHICS \
--os-variant=$OSVARIANT --location=$OSLOCATION \
--initrd-inject=$ksfile --extra-args="$OSEXTRAARGS $CONSOLE" \
--disk path=`pwd`/$imgfile,size=4,bus=virtio --force --noreboot 2>&1)
rc=$?
if [ $rc -ne 0 ]
then
if echo "${out}" | grep "Guest name '$sanitized_name' is already in use"
then
echo "WARNING: Guest name in use."
echo "Try running:"
echo " virsh -c qemu:///session destroy $sanitized_name"
echo " virsh -c qemu:///session undefine $sanitized_name"
fi
die $rc "Virt-install failed!"
fi
virsh --connect qemu:///session undefine "$sanitized_name"
echo "Preparing image for cloud"
sudo virt-sysprep --no-selinux-relabel -a $imgfile \
|| die 9 "Virt-sysprep failed1"
echo "Sparsify disk file and converting to qcow2"
sudo virt-sparsify --convert qcow2 -o compat=0.10 $imgfile $qcowfile \
|| die 9 "Virt-sparsify failed1"
if [ $UPLOAD_TO_GLANCE -eq 1 ]
then
echo "Uploading to glance"
glance image-create --name "$name" --disk-format qcow2 --container-format bare --is-public $GLANCE_PUBLIC_IMAGE --file $qcowfile
fi
echo
echo "Creation of image done."
echo "Raw image: $imgfile"
echo "QCOW2 image: $qcowfile"
# # Info on how to mount the qcow2 image:
# # also check http://alexeytorkhov.blogspot.ch/2009/09/mounting-raw-and-qcow2-vm-disk-images.html
# modprobe nbd max_part=32
# qemu-nbd -c /dev/nbd0 $qcowfile
# # assuming you want to mount partition 1:
# mount /dev/nbd0p1 /mnt
# # If you are using LVM instead:
# vgscan
# vgchange -ay
# mount /dev/VolGroupName/LogVolName /mnt
# # To unmount:
# umount /mnt
# qemu-nbd -c /dev/nbd0