Skip to content

Instantly share code, notes, and snippets.

@timhughes
Last active December 29, 2015 17:29
Show Gist options
  • Save timhughes/7704025 to your computer and use it in GitHub Desktop.
Save timhughes/7704025 to your computer and use it in GitHub Desktop.
Basic script to create qcow2 backed kvm domains based on a golden domain. 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 w…
#!/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=$(virsh dumpxml $OLDDOMAIN)
DISK_DIR=$(dirname $(echo $OLD_DOMAIN_XML|xmlstarlet sel -t -m '/domain/devices/disk/source' -v @file))
qemu-img create -f qcow2 -b ${DISK_DIR}/${OLDDOMAIN}.img ${DISK_DIR}/${NEWDOMAIN}.img
chown --reference ${DISK_DIR}/${OLDDOMAIN}.img ${DISK_DIR}/${NEWDOMAIN}.img
virt-sysprep -a ${DISK_DIR}/${NEWDOMAIN}.img
NEW_DOMAIN_XML=$(echo $OLD_DOMAIN_XML| xmlstarlet ed -O -u '/domain/uuid' -v $(uuidgen) 2>/dev/null )
NEW_DOMAIN_XML=$(echo $NEW_DOMAIN_XML| xmlstarlet ed -O -u '/domain/devices/interface/mac/@address' -v $(echo 52:54:00$(hexdump -n3 -e '/1 ":%02X"' /dev/random)) 2>/dev/null)
NEW_DOMAIN_XML=$(echo $NEW_DOMAIN_XML| sed "s/${OLDDOMAIN}/${NEWDOMAIN}/g")
#echo $NEW_DOMAIN_XML |xmllint --format -
#exit
TMP_FILE=$(mktemp)
echo $NEW_DOMAIN_XML |xmllint --format - > $TMP_FILE
virsh define $TMP_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment