KNEL KVM Script
From Knelcorpwiki
This script has evolved over a few generations. It has several useful features:
- Checks if vm is already running and won't start if it is. I implemented this after over writing disk images due to multiple invocations.
- Sets a mac address. Did this after my switch shut down.
- Sets up serial console and monitor console. Very useful those.
The script is pretty simple, and everything can be found via man kvm.
The full process I use is:
1) Create a vm directory in my home directory.
2) Setup a structure to my liking (usually I have prod/dev/test as top level dirs then branch out from there)
3) Create a directory for my vm
4) Copy the generic script below to that directory and create a disk image. I do this via qemu-img create xyz.img 10G for example. man qemu-img for details.
5) Edit the generic script to point to the iso (or netboot) and disk image I wish to use.
6) Change the mac address
7) Invoke the virtual machine via ./scriptname.sh
Here is a script I use for starting Linux guests:
#!/bin/bash
#A script to fire up linux KVM virtual machines
image_name="Public-Services"
if [ -f /home/virtual-machines/.vm/$image_name-running.lock ]
then
echo "Fatal error. $image_name lock file exists. Aborting...."
exit
fi
touch /home/virtual-machines/.vm/$image_name-running.lock
echo "Created lock file. Starting $image_name ..."
disk_image="./prodPublic.img"
cdrom_image="../../isos/ub910x64.iso"
boot_select="c"
kvm_opts="-m 1024 -daemonize -nographic -serial telnet::30000,server,nowait -serial mon:telnet::40000,server,nowait"
kvm \
-hda $disk_image \
-cdrom $cdrom_image \
-boot $boot_select \
-name $image_name \
-net nic,model=rtl8139,macaddr=52:54:00:12:34:03 -net tap \
$kvm_opts
reset
Here is a script I use for starting windows guests:
#!/bin/bash
#A script to fire up windows KVM virtual machines
image_name="Windows-2008-Production"
if [ -f /data/virtual-machines/status/$image_name-running.lock ]
then
echo "Fatal error. $image_name lock file exists. Aborting...."
exit
fi
touch /data/virtual-machines/status/$image_name-running.lock
echo "Created lock file. Starting $image_name ..."
disk_image="./w2k8-prod.img"
cdrom_image="../../isos/w2k8-x64.iso"
#c = hard drive, d = cdrom first time set to d then to c for subsequent reboots
boot_select="c"
#-m is memory, -daemonize means run all the time, -nographic for normal operation
#don't use nographic for first install
kvm_opts="-m 7168 -daemonize -vga std -nographic -vnc :1"
#kvm_opts="-m 512 -daemonize -vga std "
#-hdb $other_disk_image
#CHANGE THE MAC ADDRESS!!!!!!!
kvm \
-hda $disk_image \
-cdrom $cdrom_image \
-boot $boot_select \
-name $image_name \
-net nic,model=rtl8139,macaddr=52:54:00:12:34:01 -net tap \
$kvm_opts
