ObjectiveThe following instruction will explain how to clone KVM-based Virtual Machines on Redhat Linux from command line using
Operating System and Software Versions
- Operating System: - Redhat 7.3
- Software: - libvirtd (libvirt) 2.0.0
RequirementsPrivileged access to your Redhat Linux installation will be required.
- # - requires given linux commands to be executed with root privileges either directly as a root user or by use of
- $ - requires given linux commands to be executed as a regular non-privileged user
Obtain Source Virtual Machine's informationBefore we begin cloning any virtual machine we first need to obtain some basic information about it. The absolute minimum information required about the source virtual machine we are about to clone would be its name and number of disk in use. To get virtual machines name run:
# virsh list Id Name State ---------------------------------------------------- 1 server1.linuxconfig.org runningNext, we may would like to know the number of disk our source virtual machines is using as well as its location. The information about disks location is optional as it only provides us with a hint on where to store new clone disk files for the sake of consistency:
# virsh dumpxml server1.linuxconfig.org | grep "source file" <source file='/var/lib/libvirt/images/server1.linuxconfig.org.qcow2'/> <source file='/var/lib/libvirt/images/server1.linuxconfig.org-1.qcow2'/> <source file='/var/lib/libvirt/images/server1.linuxconfig.org-2.qcow2'/>From the above output we can see that our original virtual machine has three disks stored in location
BLUE SKY STUDIOS are looking for Linux Administrator to maintain and support the Studio's 450+ production Linux workstations, including daily interactions with the Studio’s digital animation artists.
LOCATION: Greenwich, Connecticut, USA
Suspend Source Virtual MachineBefore cloning can take place our source virtual machine needs to pause/suspended:
# virsh suspend server1.linuxconfig.org Domain server1.linuxconfig.org suspended
Auto-Clone Virtual MachineOne way of cloning KVM-based Virtual Machines on Redhat Linux is to use
--auto-cloneswitch. The main advantage of
--auto-cloneswitch is that it automatically clones any number of source disks hence the user does not necessary need to know the location and number of the disk attached to the original virtual machine.
The disadvantage is that the user is unable to specify disk names and pride an alternative location to new cloned virtual disks. The following linux command will clone original virtual machine
server1.linuxconfig.orgto new clone virtual machine
# virt-clone --original=server1.linuxconfig.org --name=server2.linuxconfig.org --auto-clone WARNING Setting the graphics device port to autoport, in order to avoid conflicting. Allocating 'server2.linuxconfig.org.qcow2' | 10 GB 00:00:00 Allocating 'server1.linuxconfig.org-1-clone.qcow2' | 5.0 GB 00:00:07 Allocating 'server1.linuxconfig.org-2-clone.qcow2' | 5.0 GB 00:00:00 Clone 'server2.linuxconfig.org' created successfully.As we can see from the above output the
--auto-cloneautomatically cloned all virtual disks attached to the original virtual machine and appended
clonekeyword to any additional disks. New cloned virtual machine should be now waiting in
shut offstate to be started:
# virsh list --all Id Name State ---------------------------------------------------- 1 server1.linuxconfig.org paused - server2.linuxconfig.org shut off
Clone Manually Multiple DiskTo gain more control over the new cloned disk location and name we can omit the
--auto-cloneswitch and supply all new cloned disk name and destination path using
--fileswitch. Note the number of
--filearguments supplied must be equal to the number of disks attached to the original virtual machine. Example:
# virt-clone --original=server1.linuxconfig.org --name=server2.linuxconfig.org --file /var/lib/libvirt/images/server2.linuxconfig.org.img --file /var/lib/libvirt/images/server2.linuxconfig.org-1.img --file /var/lib/libvirt/images/server2.linuxconfig.org-2.img WARNING Setting the graphics device port to autoport, in order to avoid conflicting. Allocating 'server2.linuxconfig.org.img' | 10 GB 00:00:00 Allocating 'server2.linuxconfig.org-1.img' | 5.0 GB 00:00:07 Allocating 'server2.linuxconfig.org-2.img' | 5.0 GB 00:00:00 Clone 'server2.linuxconfig.org' created successfully.
Resume/StartAt this stage we are ready to resume our original virtual machine
# virsh resume server1.linuxconfig.org Domain server1.linuxconfig.org resumedand also start the newly cloned virtual machine
# virsh start server2.linuxconfig.org Domain server2.linuxconfig.org startedCheck the state of all virtual machines:
# virsh list --all Id Name State ---------------------------------------------------- 1 server1.linuxconfig.org running 3 server2.linuxconfig.org running