Basic script to create qcow2 backed kvm domains based on a golden
domain. Your golden domain must be using qcow2
To install the the dependencies on fedora 19 run the following:
yum install libvirt-client qemu-img libguestfs-tools xmlstarlet libxml2
The virt-sysprep tool can do much more than this and I could potentially set the hostname and configure the machine to run some scripts when it starts up to check in with puppet which I may do in the future.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Requirements on Fedora are: | |
# libguestfs-tools for virt-sysprep | |
# xmlstarlet | |
# libvirt-client | |
# qemu-img | |
# libxml2 for xmllint | |
# | |
# Usage: | |
# Shutdown the domain_golden virtual machine and run the following command as root. | |
# | |
# /path/to/virt-slice domain_golden domain_new | |
# | |
OLDDOMAIN=$1 | |
NEWDOMAIN=$2 | |
OLD_DOMAIN_XML=(virshdumpxmlOLDDOMAIN) | |
DISK_DIR=(dirname(echo $OLD_DOMAIN_XML|xmlstarlet sel -t -m '/domain/devices/disk/source' -v @file)) | |
qemu-img create -f qcow2 -b DISKDIR/{OLDDOMAIN}.img DISKDIR/{NEWDOMAIN}.img | |
chown --reference DISKDIR/{OLDDOMAIN}.img DISKDIR/{NEWDOMAIN}.img | |
virt-sysprep -a DISKDIR/{NEWDOMAIN}.img | |
NEW_DOMAIN_XML=(echoOLD_DOMAIN_XML| xmlstarlet ed -O -u '/domain/uuid' -v $(uuidgen) 2>/dev/null ) | |
NEW_DOMAIN_XML=(echoNEW_DOMAIN_XML| xmlstarlet ed -O -u '/domain/devices/interface/mac/@address' -v (echo52:54:00(hexdump -n3 -e '/1 ":%02X"' /dev/random)) 2>/dev/null) | |
NEW_DOMAIN_XML=(echoNEW_DOMAIN_XML| sed "s/OLDDOMAIN/{NEWDOMAIN}/g") | |
#echo $NEW_DOMAIN_XML |xmllint --format - | |
#exit | |
TMP_FILE=$(mktemp) | |
echo NEWDOMAINXML|xmllint−−format−>TMP_FILE | |
virsh define $TMP_FILE |