Skip to content

Instantly share code, notes, and snippets.

@cjj25
Forked from aojea/linked-clone.sh
Created October 10, 2021 13:49
Show Gist options
  • Save cjj25/4dc4ad0e0c04665265c2defd0bb4e494 to your computer and use it in GitHub Desktop.
Save cjj25/4dc4ad0e0c04665265c2defd0bb4e494 to your computer and use it in GitHub Desktop.
Script to create a linked clone with libvirt
#!/bin/bash
set -xe
# This script takes as a parameter the name of the VM
# and creates a linked clone
# Ref: https://unix.stackexchange.com/a/33584
# The scripts assumes that it runs from the same folder
# where the vm image is located and it coincides with the
# image name
# if less than two arguments supplied, display usage
if [ $# -ne 2 ]
then
echo "This script takes as input the name of the VM to clone"
echo "Usage: $0 vm_name_orig vm_name_clone"
exit 1
fi
VM_NAME=$1
VM_CLONE=$2
# You cannot "clone" a running vm, stop it. suspend and destroy
# are also valid options for less graceful cloning
if virsh --connect=qemu:///system list | grep $VM_NAME
then
virsh --connect=qemu:///system shutdown $VM_NAME
sleep 60
fi
# Make the golden image read only
chmod a-w $VM_NAME.qcow2
# dump the xml for the original
virsh --connect=qemu:///system dumpxml $VM_NAME > /tmp/golden-vm.xml
# Create a linked clone in the current folder
qemu-img create -f qcow2 -b $VM_NAME.qcow2 $VM_CLONE.qcow2
# hardware addresses need to be removed, libvirt will assign
# new addresses automatically
sed -i /uuid/d /tmp/golden-vm.xml
sed -i '/mac address/d' /tmp/golden-vm.xml
# and actually rename the vm: (this also updates the storage path)
sed -i s/$VM_NAME/$VM_CLONE/ /tmp/golden-vm.xml
# finally, create the new vm
virsh --connect=qemu:///system define /tmp/golden-vm.xml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment