-
Notifications
You must be signed in to change notification settings - Fork 9
/
create-guest-qcow2.bash
executable file
·248 lines (211 loc) · 6.05 KB
/
create-guest-qcow2.bash
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
#!/bin/bash
#
# Copyright (C) 2016 Red Hat Inc.
# Author: Kashyap Chamarthy <[email protected]>
# Further contributions: Prasad J. Pandit <[email protected]>
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
#
# Purpose: Script to create/install a virtual machine (qcow2 disk)
#
# - If you need linux bridging, it should be configured on the host to
# allow guests to be in the same subnet as hosts
# - References:
# - http://wiki.libvirt.org/page/Networking
# - https://kashyapc.fedorapeople.org/virt/configuring-bridging-f19+.txt
# - The kickstart file contains minimal Fedora packages (@core)
# - This script also provides access to serial console to the VM
#set -x
VERSION="0.3"
prog=`basename $0`
fstype="ext4"
IMAGE_HOME="/var/lib/libvirt/images"
burl="http://dl.fedoraproject.org/pub"
location1="$burl/fedora/linux/releases/22/Server/ARCH/os"
location2="$burl/fedora/linux/releases/23/Server/ARCH/os"
location3="$burl/fedora/linux/development/rawhide/ARCH/os"
# Create a minimal kickstart file and return the temporary file name.
# Do remember to delete this temporary file when it is no longer required.
#
create_ks_file()
{
dist=$1
fkstart=$(mktemp -u --tmpdir=$(pwd) .XXXXXXXXXXXXXX)
cat << EOF > $fkstart
install
text
shutdown
lang en_US.UTF-8
keyboard us
network --bootproto dhcp
rootpw testpwd
firewall --enabled --ssh
selinux --enforcing
timezone --utc America/New_York
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200 rd_NO_PLYMOUTH"
zerombr
clearpart --all --initlabel
autopart --type=$fstype
%packages
@core
%end
EOF
echo "$fkstart"
}
create_guest()
{
name=$1
arch=$2
dist=$3
locn=$4
dimg=$5
fkst=$(create_ks_file $dist)
bnam=$(basename $fkst)
echo "Creating domain $name..."
echo "Disk image will be created at: $dimg"
# Create the qcow2 disk image with preallocation and 'fallocate'
# (which pre-allocates all the blocks to a file) it for maximum
# performance.
#
# fallocate -l `ls -al $diskimage | awk '{print $5}'` $diskimage
#
echo "Creating qcow2 disk image..."
qemu-img create -f qcow2 -o preallocation=metadata $dimg 10G
echo `ls -lash $dimg`
virt-install --connect=qemu:///system \
--network=network:default \
--initrd-inject=$bnam \
--extra-args="ks=file:/$bnam console=tty0 console=ttyS0,115200" \
--name=$name \
--disk path=$dimg,format=qcow2,cache=writeback \
--ram 2048 \
--vcpus=2 \
--check-cpu \
--accelerate \
--os-type linux \
--os-variant $dist \
--hvm \
--location=$locn \
--nographics \
--serial=pty\
--noreboot
rm $fkst
return 0
}
usage ()
{
echo -e "Usage: $prog [OPTIONS] <vm-name> <distro> <arch> [dest-dir]\n"
echo "distro : f22, f23, rawhide,
[Or, path to HTTP URL, like]: http://dl.fedoraproject.org/pub/fedora/linux/development/rawhide/x86_64/os/"
echo "arch : x86_64, i386"
echo "dest-dir : /path/dest-dir [Optional: Alternate directory to store images,
assuming QEMU has access to it, i.e. 'chmod go+rx /path/dest-dir']
EXAMPLES:
# Create a Fedora-23 VM:
./`basename $0` vm1 f23 x86_64
# Create a Fedora-23 VM, and store the VM disk image in the said dir:
./`basename $0` vm2 f23 x86_64 /export/vmimages
# Create a Fedora-23 VM, with the specified Fedora tree URL:
./`basename $0` vm3 http://dl.fedoraproject.org/pub/fedora/linux/development/rawhide/x86_64/os/ x86_64"
}
printh ()
{
format="%-15s %s\n"
usage;
printf "\n%s\n\n" "OPTIONS:"
printf "$format" " -f <FSTYPE>" "specify file system type, default: ext4"
printf "$format" " -h" "display this help"
printf "$format" " -v" "display version information"
printf "\nReport bugs here:
https://github.com/kashyapc/virt-scripts/issues\n"
}
check_options ()
{
while getopts ":+f:hv" arg "$@";
do
case $arg in
:)
printf "$prog: missing argument\n"
exit 0
;;
f)
fstype=$OPTARG
;;
h)
printh
exit 0
;;
v)
printf "%s version %s\n" $prog $VERSION
exit 0
;;
*)
printf "%s: invalid option\n" $prog
exit 255
esac
done
return $(($OPTIND - 1));
}
# main
{
check_options $@;
shift $?;
# check if min no. of arguments are 3
#
if [ "$#" -lt 3 ]; then
printh;
exit 255
fi
# check if Linux bridging is configured
#
show_bridge=`brctl show | awk 'NR==2 {print $1}'`
if [ $? -ne 0 ] ; then
echo "Bridged Networking is not configured. " \
"please do so if your guest needs an IP similar to your host."
exit 255
fi
name=$1
dist=$2
arch=$3
destdir=$4
test -n "$destdir" && IMAGE_HOME="$destdir"
dimg="$IMAGE_HOME/$name.qcow2"
locn=""
case "$dist" in
f22)
dist="fedora22"
locn=${location1/ARCH/$arch}
;;
f23)
dist="fedora23"
locn=${location2/ARCH/$arch}
;;
rawhide)
dist="fedora23"
locn=${location3/ARCH/$arch}
;;
http*)
locn=${dist/ARCH/$arch}
echo "RAW version: $locn"
dist="fedora23"
;;
*)
echo "$0: invalid distribution name"
exit 255
esac
create_guest $name $arch $dist $locn $dimg
exit 0
}