-
-
Save reanimat0r/63a4f2335745610fd1d9fc43b03fa943 to your computer and use it in GitHub Desktop.
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 | |
thisvm=$1 | |
thatvm=$2 | |
# You cannot "clone" a running vm, stop it. suspend and destroy | |
# are also valid options for less graceful cloning | |
virsh shutdown ${thisvm} | |
# dump the xml for the original | |
virsh dumpxml ${thisvm} > /tmp/${thatvm}.xml | |
# copy the storage. | |
disk=$(cat /tmp/${thatvm}.xml | grep 'source file' | cut -d"'" -f2) | |
if [ $# > 2 ]; then | |
outd=$3 | |
else | |
outd=$(echo $disk | sed -i s/${thisvm}/${thatvm}/ ) | |
fi | |
echo -n "[*] copying $disk to $outd..." | |
#cp $disk $outd | |
qemu-img convert -O qcow2 $disk $outd | |
echo -e -n "done\n[*] renaming xml..." | |
# hardware addresses need to be removed, libvirt will assign | |
# new addresses automatically | |
sed -i /uuid/d /tmp/${thatvm}.xml | |
sed -i '/mac address/d' /tmp/${thatvm}.xml | |
# and actually rename the vm: (this also updates the storage path) | |
sed -i "s#$disk#$outd#" /tmp/${thatvm}.xml | |
sed -i "s/$thisvm/$thatvm/" /tmp/${thatvm}.xml | |
echo -e "done" | |
# finally, create the new vm | |
virsh define /tmp/${thatvm}.xml | |
#for sn in $( virsh snapshot-list $thisvm | tail -n+3 | head -n+1 | awk '{print $1}' ); do | |
# virsh snapshot-dumpxml ${thisvm} ${sn} --security-info > /tmp/${thatvm}.${sn}.xml | |
# #clean old shit | |
# sed -i /uuid/d /tmp/${thatvm}.${sn}.xml | |
# sed -i '/mac address/d' /tmp/${thatvm}.${sn}.xml | |
# sed -i "s#$disk#$outd#" /tmp/${thatvm}.${sn}.xml | |
# sed -i "s/$thisvm/$thatvm/" /tmp/${thatvm}.${sn}.xml | |
# virsh snapshot-create ${thatvm} /tmp/${thatvm}.${sn}.xml --redefine --current | |
# rm /tmp/${thatvm}.${sn}.xml | |
#done | |
rm /tmp/${thatvm}.xml | |
#virsh start $thatvm |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment