Skip to content

Commit

Permalink
Update 'create-guest-raw' script
Browse files Browse the repository at this point in the history
  - NOTE: This script should be made redundant and converge
    this with 'create-guest-qcow2' by providing a simple CLI option
  • Loading branch information
kashyapc committed Jan 9, 2015
1 parent 66ad2af commit 48b5187
Showing 1 changed file with 170 additions and 121 deletions.
291 changes: 170 additions & 121 deletions create-guest-raw.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#!/bin/bash
#
# Copyright (C) 2013 Red Hat Inc.
#
# 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
Expand All @@ -15,113 +17,41 @@
# 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 auto 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


# check if Bridged Networking is configured

show_bridge=`brctl show | awk 'NR==2 {print $1}'`
if [ $? -ne 0 ] ; then
echo "Bridged Networking is not configured, please do so to get an IP similar to your host."
exit 255
fi
#check if no. of arguments are 3
if [ "$#" != 3 ]; then
echo " Please provide the args according to usage"
echo ""
echo " usage: "$0 domname distro arch" "
echo ""
echo " where 'domname'= f15vm1 [OR] el6vm2"
echo " where 'distro' = rhel6 [OR] f15"
echo " where 'arch' = x86_64 [OR] i386"
echo ""
exit 255
fi

# check if /export/vmimgs dir exists
if [ ! -d /export/vmimgs ] ; then
echo "'/export/vmimgs' directory does not exist(to store vm images). Please create that."
exit 255
fi

domname=$1
distro=$2
arch=$3

location1=http://foo.bar.redhat.com/pub/rhel/released/RHEL-6/6.4/Server/$arch/os/
locatin2=http://dl.fedoraproject.org/pub/fedora/linux/releases/18/Fedora/$arch/os/


#Disk Image location
diskimage=/var/lib/libvirt/images/$name.qcow2
#diskimage=/export/vmimgs2/$name.qcow2

# Create the qcow2 disk image with preallocation and 'fallocate'(which
# pre-allocates all the blocks to a file) it for max. performance

echo "Creating qcow2 disk image.."
qemu-img create -f qcow2 -o preallocation=metadata $diskimage 10G
#fallocate -l `ls -al $diskimage | awk '{print $5}'` $diskimage
echo `ls -lash $diskimage`


echo "Creating domain $domname..."
echo "Image is here $vmimage"
echo "Location of the OS sources $location..."


if [ "$distro" = rhel6 ]; then
#Create the minimal kickstart file for RHEL
cat << EOF > rhel.ks
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
key --skip
clearpart --all --initlabel
autopart
#
#
# 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

%packages
@core
EOF

# Create the guest
virt-install --connect=qemu:///system \
--network=bridge:br0 \
--initrd-inject=./rhel.ks \
--extra-args="ks=file:/rhel.ks console=tty0 console=ttyS0,115200" \
--name=$domname \
--disk /var/lib/libvirt/images/$domname.img,size=10,cache=none \
--ram 2048 \
--vcpus=2 \
--check-cpu \
--accelerate \
--cpuset auto \
--os-type linux \
--os-variant rhel6 \
--hvm \
--location=$location1 \
--nographics
#set -x

exit 255
#########################################################################
VERSION="0.1"
prog=`basename $0`

elif [ "$distro" = f19 ]; then
#Create Minimal kickstart file for Fedora
cat << EOF > fed.ks
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
Expand All @@ -135,30 +65,149 @@ timezone --utc America/New_York
bootloader --location=mbr --append="console=tty0 console=ttyS0,115200 rd_NO_PLYMOUTH"
zerombr
clearpart --all --initlabel
autopart
autopart --type=$fstype
%packages
@core
%end
EOF

# Create the guest
virt-install --connect=qemu:///system \
--network=bridge:br0 \
--initrd-inject=./fed.ks \
--extra-args="ks=file:/fed.ks console=tty0 console=ttyS0,115200" \
--name=$domname \
--disk /var/lib/libvirt/images/$domname.img,size=20,cache=none \
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 \
--cpuset auto \
--os-type linux \
--os-variant fedora18 \
--os-variant $dist \
--hvm \
--location=$location2 \
--nographics

fi

--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
}

0 comments on commit 48b5187

Please sign in to comment.