Linux Virtualization Cloning a KVM Guest Virtual Machine

From NovaOrdis Knowledge Base
Jump to navigation Jump to search

Internal

Procedure

Shut Down the Source Virtual Machine

Shut down the guest to be cloned with

 virsh shutdown source_vm

Export the Configuration

Export the configuration of the guest to be cloned with

 virsh dumpxml source_vm

Adjust the Configuration

Copy the XML definition under a new name, conventionally the name of the guest being built. Edit the XML as needed. These are some things you may want to change:

Name of the Guest

Update the name of the guest as:

<domain type='kvm'>
  <name>the_new_name</name>
  ...

For more details on constraints associated with the guest name, see name.

UUID

Remove the <uuid> line, a new UUID will be generated.

Memory

Adjust the amount of memory, specified as <memory>. Remove the <currentMemory> line.

Virtual CPU Count

Adjust the number of virtual CPUs allocated to this virtual machine, using <vcpu> element.

CDROM

Remove the "cdrom" disk(s), if it is not going to be used.

Storage

Edit the <disk> definitions and adjust the names for the new storage volumes that will be provisioned for the new virtual machine. The virtual machine will definitely need a virtual machine image, stored in (usually) qcow2 format, end possible other block storage devices in raw format. Conventionally, the virtual machine image is stored in the main storage pool and it is named based on the name of the VM:

<disk type='file' device='disk'>
  <driver name='qemu' type='qcow2'/>
  <source file='/main-storage-pool/new-vm-name.qcow2'/>
  <target dev='vda' bus='virtio'/>
  ...
</disk>
<disk type='file' device='disk'>
  <driver name='qemu' type='raw'/>
  <source file='/main-storage-pool/'new-vm-name-docker.raw'/>
  <target dev='vdb' bus='virtio'/>
  ...
</disk>

Network

Locate the network interface and replace the value of the mac address with a randomly generated value. A shell script that generate random MAC addresses is available here: bash script that generates a random MAC address.

<interface type='network'>
    <mac address='52:54:00:79:03:0c'/>
    <source network='default'/>
    <model type='virtio'/>
    <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>

See <interface>.

Provision the Storage

For each disk the virtual machine relies on, starting with the virtual machine image storage, clone the disk following the procedure:

Cloning a Storage Volume

If the storage was originally empty, or we want to start with an empty storage, we could provision a new disk, as follows (note that this does not apply to the virtual machine image, we need the content of the virtual machine image to clone the virtual machine):

If a new storage volume is created, and the cloned guest expects a filesystem on it ... ?

Create the Clone

   virsh define

Boot the Clone and Finalize the Configuration

Boot the clone:

 virsh start new-virtual-machine

Connect to it with virsh console, or via SSH using the IP address of the template, and finalize the configuration:

Reconfigure Linux VM Guest Image