//
you're reading...
Bash Scripts, Centos, Code, KVM, Systems

Kicking Centos 6.3 KVM Guests

Simple script used to setup a KVM guest.
Some of the prereqs for this are:

  1. Have a copy of centos/os/ARCH accessible from a web URL. This could be from a web site like kernel.org or a local copy( how I roll ).
  2. Have a kickstart file available from a web URL, again used a local copy in my case.
  3. Have KVM installed and working, with bridged interface ( I used openvswitch )

Now you are ready for the script:


#!/bin/bash

function valid_ip()
{
	local  ip=$1
	local  stat=1

	if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
		OIFS=$IFS
		IFS='.'
		ip=($ip)
		IFS=$OIFS
		[[ ${ip[0]} -le 255 && ${ip[1]} -le 255 \
			&& ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
		stat=$?
	fi
	return $stat
}

if [ -z $1 ];then
	echo -n "Enter VM Name: "
	read VM_NAME
else
	VM_NAME=$1
fi

if [ -z $2 ];then
	echo -n "Enter VM IP: "
	read VM_IP
else
	VM_IP=$2
fi

if [ -z "$VM_NAME" ] || [ -z "$VM_IP" ]; then echo "Name or IP not given"; exit 1;fi

VM_NAME="`echo "${VM_NAME}" | tr -cd '[:alnum:]'`"
echo "VM NAme: ${VM_NAME}"

if ! valid_ip $VM_IP; then echo "VM IP not legit"; exit 1;fi
echo "VM IP: ${VM_IP}"

echo -n "Create VM? (y/n): "
read YN

if [ $YN != 'y' ];then echo "Exiting..."; exit 1;fi

cat >/var/www/html/${VM_NAME}.ks <<EOF
install
text
url --url http://10.0.0.1/centos
lang en_US.UTF-8
keyboard us
network --bootproto=static --ip=$VM_IP --netmask=255.255.255.0 --hostname=$VM_NAME --onboot=true
rootpw changeme
firewall --enabled --port=22:tcp
authconfig --enableshadow --enablemd5
selinux --disabled
timezone --utc America/Los_Angeles
zerombr yes
bootloader --location=mbr --driveorder=vda
clearpart --all --initlabel --drives=vda
part /boot --fstype ext3 --size=100 --ondisk=vda
part pv.2 --size=1 --grow --ondisk=vda
volgroup vg_system --pesize=32768 pv.2
logvol / --fstype ext3 --name=lv_root --vgname=vg_system --size=8064
logvol swap --name=lv_swap --vgname=vg_system --grow --size=512 --maxsize=1024
skipx
reboot

%packages
@core
@base
sgpio
screen
sudo
wget
mlocate
ntpdate
git
links
EOF

virt-install \
	--hvm \
	--virt-type=kvm \
	--name=$VM_NAME \
	--cpuset=1 \
	--ram=2048 \
	--network bridge=ovsbr1 \
	--disk path=/home/vmstore/${VM_NAME}.img,size=9 \
	--os-variant=rhel6 \
	--arch=x86_64 \
	--location http://10.0.0.1/centos/ \
	--extra-args "console=ttyS0,115200 text headless ks=http://10.0.0.1/$VM_NAME.ks ip=$VM_IP" \
	--graphics none

echo "${VM_IP}	${VM_NAME}" >>/etc/hosts
VM_PORT="`echo $VM_IP | cut -d "." -f4`"
iptables -A PREROUTING -t nat -i eth0 -p tcp --dport $VM_PORT -j DNAT --to ${VM_IP}:22

Things to note:

  • Root Password should be changed to a crypted key, rootpw –iscrypted
  • 10.0.0.1 for me is the openvswitch created bridge interface on my server running KVM
  • ovsbr1 the bridge interface libvirtd uses to create a vnet on.
  • I already have NAT setup on the server, so the end of just script just adds a forwarded port so I can ssh to the server.
  • Deleting a VM means also removing the entry in iptables and the /etc/hosts file.
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s