#!/bin/ksh -p if [ ! -w /etc/passwd ] then echo Not root exit fi IMAGES=/var/lib/libvirt/images XML=/etc/libvirt/qemu file=0 graphics="--nographics" console="console=ttyS0,9600" console="console=ttyS0,9600 cmdline" text="text" os=6 ks="" repo="" hardware="" ram=1536 # During install phase STANDARD_RAM=512 dsize=4 # Disk size in GB lvm=Raid10 while getopts fg67d:l: OPT do case $OPT in f) file=1 ;; 6) ks=6 ; repo=http://repo/CentOS/DVD/CentOS-6 ;; 7) ks=7 ; repo=http://repo/CentOS/DVD/CentOS-7 ;; g) graphics="" ; text="" ; console="" ;; d) dsize=$OPTARG ;; l) lvm=$OPTARG ;; esac done if [ -z "$repo" ] then echo Need at least -6 or -7 exit fi shift $(($OPTIND - 1)) machine=$1 mac=$2 # optional if [ -z "$machine" ] then echo "Syntax: ${0##*/} -6|-7 [-f] [-g] [-d#] [-l LVM] machine_name [MAC]" echo " f==> files, not LVM." echo " d==> disk size in Gb; default=4" echo " l==> LVM volume group; default=Raid10" exit fi if [ $file = 1 ] then destfile=$IMAGES/$machine.img else destfile=/dev/$lvm/vm.$machine fi if [ -r $XML/$machine.xml ] then echo $XML/$machine.xml already exists echo aborted exit fi if [ -e $destfile ] then echo $destfile already exists echo aborted exit fi if [ -z "$mac" ] then mac=$(tr -d ' ' < $HOME/etc/localnet/DNS/machines | awk -F';' '$3 == "'$machine'" { print $2}' ) mac=${mac#!} echo Using mac $mac fi if [ -n "$mac" ] then case $mac in [0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]:[0-9A-Fa-f][0-9A-Fa-f]) # Looks good! mac=",mac=$mac" ;; *) echo Bad format MAC address exit esac fi echo Building VM if [ $file = 0 ] then lvcreate -L ${dsize}G -n vm.$machine $lvm || exit else destfile=$destfile,size=${dsize} fi set -x virt-install \ -n $machine \ -r $ram \ --vcpus=1 \ --os-variant=rhel$os \ --accelerate \ -v \ --network=bridge=br0$mac,target=v-$machine \ --disk path=$destfile \ -l $repo \ $graphics $hardware \ -x "ks=http://10.0.0.137/CentOS/kickstart/centos$ks.cfg ksdevice=eth0 ip=dhcp $console" # Reset VM to target RAM size virsh setmem $machine ${STANDARD_RAM}M --config virsh setmem $machine ${STANDARD_RAM}M virsh setmaxmem $machine ${STANDARD_RAM}M --config virsh setmaxmem $machine ${STANDARD_RAM}M