Linux Virtualization Cloning a KVM Guest Virtual Machine

From NovaOrdis Knowledge Base
Revision as of 21:09, 30 June 2017 by Ovidiu (talk | contribs) (Created page with "=Internal= * Shut down the guest with virsh shutdown. * Export the XML configuration with virsh dumpxml. * Copy the XML definition under a new name, conventionally t...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Internal

  • Shut down the guest with virsh shutdown.
  • Export the XML configuration with virsh dumpxml.
  • Copy the XML definition under a new name, conventionally the name of the guest being built.
  • Edit the XML as needed. Things you may want to change:
    • The name of the guest.
    • Remove the <uuid> line, a new UUID will be generated.
    • The amount of memory, specified as <memory>. Remove the <currentMemory> line.
    • The number of vCPUs allocated to the guest.
    • Remove the "cdrom" disk(s), if it is not going to be used.
    • 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>
  • 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.
  • Clone the virtual machine image with the new name.
  • Create any other storage volume that may be needed.
  • Define the guest with virsh define.