-
Notifications
You must be signed in to change notification settings - Fork 9
/
create-guest-raw.bash
executable file
·213 lines (181 loc) · 4.73 KB
/
create-guest-raw.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
#!/bin/bash
#
# Copyright (C) 2015 Red Hat Inc.
# Author: <[email protected]>
# Further contributions: <[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 (raw 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 a serial console
#set -x
VERSION="0.1"
prog=`basename $0`
fstype="ext4"
IMAGE_HOME="/var/lib/libvirt/images"
burl="http://dl.fedoraproject.org/pub"
location1="$burl/fedora/linux/releases/20/Fedora/ARCH/os"
location2="$burl/fedora/linux/releases/21/Server/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
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=$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"
echo "Creating RAW disk image..."
qemu-img create -f raw $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=raw,cache=writeback \
--ram 2048 \
--vcpus=2 \
--check-cpu \
--accelerate \
--os-type linux \
--os-variant $dist \
--hvm \
--location=$locn \
--nographics \
--serial=pty
rm $fkst
return 0
}
usage ()
{
echo -e "Usage: $prog [OPTIONS] <vm-name> <distro> <arch>\n"
echo " distro: f20 f21"
echo " arch: i386, 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 to Kashyap <[email protected]>\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 [ "$#" != 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
dimg="$IMAGE_HOME/$name.img"
locn=""
case "$dist" in
f20)
dist="fedora20"
locn=${location1/ARCH/$arch}
;;
f21)
dist="fedora21"
locn=${location2/ARCH/$arch}
;;
*)
echo "$0: invalid distribution name"
exit 255
esac
create_guest $name $arch $dist $locn $dimg
exit 0
}