LinuxCommandLibrary

virt-clone

Clone existing virtual machines

TLDR

Clone a virtual machine and automatically generate a new name, storage path, and MAC address

$ virt-clone [[-o|--original]] [vm_name] --auto-clone
copy

Clone a virtual machine and specify the new name, storage path, and MAC address
$ virt-clone [[-o|--original]] [vm_name] [[-n|--name]] [new_vm_name] [[-f|--file]] [path/to/new_storage] [[-m|--mac]] [ff:ff:ff:ff:ff:ff|RANDOM]
copy

SYNOPSIS


virt-clone --original ORIGINAL_NAME --name NEW_NAME [OPTION...]

PARAMETERS

--original ORIGINAL_NAME
    
Specifies the name of the existing virtual machine to be cloned. This VM must be defined in libvirt.

--name NEW_NAME
    
Sets the name for the new cloned virtual machine. This name must be unique.

--file TARGET_FILE
    
Defines the path for the new disk image file(s) for the cloned VM. Can be specified multiple times for multiple disks.

--preserve-data
    
Instructs virt-clone to reuse the original disk image(s) rather than copying them. Useful for creating a new VM configuration pointing to existing data, but be cautious of data corruption if both VMs run simultaneously.

--force
    
Proceed with the cloning even if it would overwrite an existing disk image or domain configuration. Use with caution.

--check-only
    
Performs a dry run, checking if the cloning operation is possible without actually making any changes.

--print-xml
    
Prints the generated XML configuration for the new VM to standard output instead of defining it in libvirt.

--connect URI
    
Connects to a specific libvirt hypervisor URI. Defaults to the system default.

--auto-clone
    
Automatically attempts to choose unique names and file paths for the cloned VM and its disks.

--keep-storage
    
Reuses the original storage volume or path without creating a new copy. Similar to --preserve-data but specifically for storage definition.

--replace
    
If a VM with --name already exists, replace it. Requires --force.

--sparse
    
When cloning raw disk images, create them as sparse files (allocate space only as needed).

--nonsparse
    
When cloning raw disk images, create them as non-sparse files (full allocation).

--nvram NVRAM_PATH
    
Specifies a new NVRAM file for UEFI firmware configuration of the cloned VM.

--disk SOURCE=...,TARGET=...
    
Fine-grained control over disk cloning, e.g., specifying a new pool or path for a specific disk. Can be used multiple times for multiple disks.

--mac AA:BB:CC:DD:EE:FF
    
Sets a specific MAC address for the new VM's first network interface. Can be specified multiple times for additional interfaces.

--os-variant VARIANT
    
Specifies the OS variant for the cloned VM, helping libvirt optimize performance for that OS.

--reflink
    
Use Btrfs or XFS reflink cloning if supported by the filesystem and disk format, for instant, space-efficient copies without duplicating data blocks.

--template
    
Treat the original VM as a template, which might affect how certain attributes (like persistent disk IDs) are handled during cloning.

DESCRIPTION

virt-clone is a command-line tool that facilitates the creation of new virtual machines (VMs) by cloning an existing one. It is part of the libvirt virtualization management framework, providing a high-level abstraction over various hypervisors like KVM, Xen, and LXC. The primary function of virt-clone is to copy the virtual machine's disk images and create a new XML configuration file, adjusting parameters such as the MAC addresses and UUIDs to prevent conflicts. This utility streamlines the process of deploying multiple identical or nearly identical VMs, making it invaluable for development, testing, and creating template-based environments. It intelligently handles various storage formats and can be used to duplicate a VM without needing to reinstall the operating system, significantly reducing deployment time. While virt-clone handles the virtualization layer, it's crucial to note that it does not perform operating system-level customization (like changing hostname or IP address) within the cloned VM; this typically requires post-cloning steps or tools like virt-sysprep.

CAVEATS


Operating System Customization: virt-clone only handles the virtualization layer. It does not modify the operating system inside the cloned VM. Critical post-cloning steps include changing the hostname, regenerating SSH host keys, modifying IP addresses, and handling application-specific configurations to prevent conflicts. For automated OS preparation, consider using virt-sysprep.
MAC Address Conflicts: By default, virt-clone generates new MAC addresses. However, if using --preserve-data or specific network configurations, ensure no MAC address conflicts arise on your network.
Storage Type Limitations: While it supports various storage types (qcow2, raw, LVM), the behavior with complex storage configurations or shared storage might require careful planning. --reflink only works on supporting filesystems (Btrfs, XFS) and disk formats.

POST-CLONING STEPS


After cloning, it is almost always necessary to boot the new VM and perform operating system-level adjustments. This includes setting a unique hostname, configuring network interfaces (IP addresses, DNS), and regenerating any unique identifiers such as SSH host keys or machine IDs (e.g., /etc/machine-id on Linux).

DISK IMAGE HANDLING


By default, virt-clone creates a full copy of the original VM's disk images. This ensures data integrity and isolation between the original and cloned VMs. Options like --preserve-data and --keep-storage are advanced features that should be used with a clear understanding of their implications on data sharing and potential conflicts if both VMs are run simultaneously.

RESOURCE ALLOCATION


While virt-clone copies the VM configuration, it typically retains the original CPU and memory allocations. You might need to use virsh edit NEW_NAME or virt-manager to adjust these resources for the new VM based on your requirements.

HISTORY


virt-clone emerged as part of the virt-manager project, developed primarily by Red Hat, aiming to provide higher-level, user-friendly tools for managing virtual machines via libvirt. Its development focused on simplifying repetitive tasks like VM duplication that would otherwise require manual manipulation of complex XML definitions or low-level disk operations. It has evolved over time to support new libvirt features, storage technologies, and hypervisor capabilities, becoming a staple for virtual machine administrators.

SEE ALSO

Copied to clipboard