forked from kashyapc/virt-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-guest-qcow2-btrfs.bash
168 lines (142 loc) · 4.19 KB
/
create-guest-qcow2-btrfs.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
#!/bin/bash
#set -x
# Copyright (C) 2014 Red Hat Inc.
# Author <[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.
#
# Script to create/install unattended Virtual Machines (with bridging)
#
# 1 Note: Bridging should be configured on the host to allow guests to be
# in the same subnet as hosts
# 2 Creting a bridge, refer this: http://wiki.libvirt.org/page/Networking
# 3 The kickstart file contains minimal fedora pkgs like core and text internet
# 4 Also adds a serial console
#
IMAGE_HOME="/var/lib/libvirt/images"
burl="http://dl.fedoraproject.org/pub"
location1="$burl/fedora/linux/releases/19/Fedora/ARCH/os"
location2="$burl/fedora/linux/releases/20/Fedora/ARCH/os"
# External F20 mirror
# http://dl.fedoraproject.org/pub/fedora/linux/releases/20/Fedora/x86_64/
# 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
reboot
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=btrfs
%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=bridge:virbr0 \
--initrd-inject=$bnam \
--extra-args="ks=file:/$bnam console=tty0 console=ttyS0,115200" \
--name=$name \
--disk path=$dimg,format=qcow2,cache=none \
--ram 2048 \
--vcpus=2 \
--check-cpu \
--accelerate \
--cpuset auto \
--os-type linux \
--os-variant $dist \
--hvm \
--location=$locn \
--nographics \
--console=pty
rm $fkst
return 0
}
# main
{
# check if min no. of arguments are 3
#
if [ "$#" != 3 ]; then
echo -e "Usage: $0 vm-name distro arch\n"
echo -e "\tdistro: f19 f20"
echo -e "\t arch: i386, x86_64
e.g. ./`basename $0` f20vm1 f20 x86_64 # create fedora 20 VM
./`basename $0` f19vm1 f19 x86_64 # create fedora f19 VM"
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
dimg="$IMAGE_HOME/$name.qcow2"
locn=""
case "$dist" in
f19)
dist="fedora19"
locn=${location1/ARCH/$arch}
;;
f20)
dist="fedora20"
locn=${location2/ARCH/$arch}
;;
*)
echo "$0: invalid distribution name"
exit 255
esac
create_guest $name $arch $dist $locn $dimg
exit 0
}