#!/bin/ksh -p IMAGES=/var/lib/libvirt/images TEMPLATE=$IMAGES/TEMPLATES XML=/etc/libvirt/qemu template=${1%.template.img} machine=$2 mac=$3 # optional if [ -z "$machine" ] then echo "Syntax: ${0##*/} template machine_name [MAC_address]" exit fi tempfile=$TEMPLATE/$template.template.img destfile=$IMAGES/$machine.img if [ ! -r $tempfile ] then echo Can not find $tempfile echo aborted exit fi if [ -r $XML/$machine.xml ] then echo $XML/$machine.xml already exists echo aborted exit fi if [ -r $destfile ] then echo $destfile already exists echo aborted exit 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="" ;; *) echo Bad format MAC address exit esac fi echo Copying $tempfile to $destfile cp $tempfile $destfile echo Defining VM t=$(mktemp) sed -e "s/#!#NAME#!#/$machine/g" -e "s/#!#MAC#!#/$mac/" $TEMPLATE/TEMPLATE.xml > $t virsh define $t rm $t