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
|
#! /bin/bash
# fai-kvm, start kvm host for testing FAI software
#
# Author: Thomas Lange, Uni Koeln, 2011-2012
# License: GPL v2 or any later version
fix="-vga std -k en-us" # if loading cirrusfb (via pcimodules and modprobe cirrusfb) causes errors in kvm
# without it centos initrd is not created properly and results in kernel panic
user=1
size=5 # default size of the disk image
ram=2000
disks=1
cdimage=/files/scratch/fai-cd.iso # default name for CD image
diskdir=/tmp # directory where the disk images will be created, a RAM disk is recommended
# - - - - - - - - - - - - - - - - - - - - - - - - -
boot_pxe() {
# PXE boot
set -x
kvm $gopt -boot order=nc $net $disk
}
# - - - - - - - - - - - - - - - - - - - - - - - - -
boot_cd() {
[ -n "$1" ] && cdimage=$1
# boot fai-cd
set -x
kvm $gopt $net $disk -cdrom $cdimage
}
# - - - - - - - - - - - - - - - - - - - - - - - - -
die() {
echo "$2" 1>&2
exit $1
}
# - - - - - - - - - - - - - - - - - - - - - - - - -
usage() {
cat <<EOF
fai-kvm [options] pxe # start a host booting with PXE from NIC
fai-kvm [options] cd [imagename] # start a host booting from a ISO image
-n create a new empty disk image, which is used as a local disk
-s <size> size of the local disk (GB, defaults to 5GB)
-d <num> number of local disks (default is one)
-m <mem> RAM size (MB, defaults to 2000MB)
-u <num> number of the user (so multiple users can call this script)
EOF
exit 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - -
while getopts "hnu:s:m:d:" opt ; do
case "$opt" in
n) newdisk=1 ;;
u) user=$OPTARG ;;
m) ram=$OPTARG ;;
s) size=$OPTARG ;;
d) disks=$OPTARG ;;
h) usage;;
?) die 1 "Unknown option";;
esac
done
shift $(($OPTIND - 1))
hex=$(echo $user 16o p | dc)
diskfile=$diskdir/faitest-disk-$user
# not all mac addresses work in kvm (or the bridge), be carefull when changing the first two bytes
# If you are generating your own MAC addresses you should use a value that contains 2,6,A or E as the second number as this defines a locally administered MAC address.
# x2:xx:xx:xx:xx:xx
# x6:xx:xx:xx:xx:xx
# xA:xx:xx:xx:xx:xx
# xE:xx:xx:xx:xx:xx
mac=52:54:00:11:23:$hex
net="-net nic,macaddr=$mac,model=virtio -net tap,ifname=tap$user,script=no,downscript=no"
disk=""
for i in `seq 1 $disks` ; do
disk="$disk -drive file=$diskfile-$i.qcow2,if=virtio,index=$i"
if [ $i -eq 1 ] ; then
disk="$disk"
fi
done
gopt="$fix -m $ram -smp 2 -name FAI-test"
# create new disk image
if [ X$newdisk = X1 ]; then
for i in `seq 1 $disks` ; do
rm -f $diskfile-$i.qcow2
qemu-img create -f qcow2 -o preallocation=metadata $diskfile-$i.qcow2 ${size}G
done
fi
case "$1" in
pxe) boot_pxe ;;
cd) boot_cd $2 ;;
*)
echo "Missing argument." >&2
usage
;;
esac
|