{"id":7736,"date":"2021-01-21T23:28:23","date_gmt":"2021-01-21T20:28:23","guid":{"rendered":"https:\/\/kifarunix.com\/?p=7736"},"modified":"2024-03-19T20:46:02","modified_gmt":"2024-03-19T17:46:02","slug":"how-to-clone-kvm-virtual-machines","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/how-to-clone-kvm-virtual-machines\/","title":{"rendered":"How to Clone KVM Virtual Machines"},"content":{"rendered":"<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1020\" height=\"334\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-kvm-virtual-machines.png\" alt=\"How to Clone KVM Virtual Machines\" class=\"wp-image-7739\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-kvm-virtual-machines.png?v=1611250085 1020w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-kvm-virtual-machines-768x251.png?v=1611250085 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-kvm-virtual-machines-150x49.png?v=1611250085 150w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-kvm-virtual-machines-300x98.png?v=1611250085 300w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-kvm-virtual-machines-696x228.png?v=1611250085 696w\" sizes=\"(max-width: 1020px) 100vw, 1020px\" \/><\/figure><\/div>\n\n\n<p>What is the quick and easy way to clone KVM virtual machines? In this guide, we&#8217;ll explore the step-by-step procedure for cloning KVM virtual machines using tools like <code>virt-clone<\/code> and <code>virt-manager<\/code>. Cloning KVM (Kernel-based Virtual Machine) virtual machines is a practical and efficient way to replicate existing VMs for various purposes, such as testing, scaling, or backup.<\/p>\n\n\n\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\"><h2>Table of Contents<\/h2><nav><ul><li><a href=\"#cloning-kvm-virtual-machines\">Cloning KVM Virtual Machines<\/a><ul><li><a href=\"#clone-virtual-machines-using-virt-manager\">Clone Virtual Machines using Virt-Manager<\/a><ul><li><a href=\"#launch-kvm-virt-manager\">Launch KVM Virt-Manager <\/a><\/li><li><a href=\"#shutdown-or-pause-virtual-machine-to-clone\">Shutdown or Pause Virtual Machine to Clone<\/a><\/li><li><a href=\"#clone-the-virtual-machine\">Clone the Virtual Machine<\/a><\/li><li><a href=\"#configure-the-kvm-clone\">Configure the KVM Clone<\/a><\/li><\/ul><\/li><li><a href=\"#clone-kvm-v-ms-using-virt-clone-command\">Clone KVM VMs using virt-clone command<\/a><\/li><li><a href=\"#clone-kvm-virtual-machine-with-multiple-disks-using-virt-clone\">Clone KVM Virtual Machine with Multiple Disks using virt-clone<\/a><\/li><li><a href=\"#other-tutorials\">Other Tutorials<\/a><\/li><\/ul><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"cloning-kvm-virtual-machines\">Cloning KVM Virtual Machines<\/h2>\n\n\n\n<p>There are various ways in which you can clone KVM VMs;<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><a class=\"rank-math-link\" href=\"#clone-kvm-vm-with-virt-manager\">Clone Virtual Machines using Virt-Manager<\/a><\/li>\n\n\n\n<li><a class=\"rank-math-link\" href=\"#clone-kvm-vm-with-virt-clone\">Clone Virtual Machines using virt-clone command<\/a><\/li>\n<\/ol>\n\n\n\n<p>Let us quickly see how you can clone virtual machines using either of the methods above.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"clone-virtual-machines-using-virt-manager\">Clone Virtual Machines using Virt-Manager<\/h3>\n\n\n\n<p>This involves using the KVM Virtual Machine Manager application to clone virtual machines.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"launch-kvm-virt-manager\">Launch KVM Virt-Manager <\/h4>\n\n\n\n<p>In order to use this method, you need to launch the Virt-manager either from the system applications menu or directly from the terminal by running the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>virt-manager<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\" id=\"shutdown-or-pause-virtual-machine-to-clone\">Shutdown or Pause Virtual Machine to Clone<\/h4>\n\n\n\n<p>From the Virtual machine manager, simply select the vm in question and right click on it or simply click on the power to access the power menu to shutdown or pause a VM.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"950\" height=\"384\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/shutdown-kvm-vm.png\" alt=\"\" class=\"wp-image-7742\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/shutdown-kvm-vm.png?v=1611260527 950w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/shutdown-kvm-vm-768x310.png?v=1611260527 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/shutdown-kvm-vm-150x61.png?v=1611260527 150w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/shutdown-kvm-vm-300x121.png?v=1611260527 300w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/shutdown-kvm-vm-696x281.png?v=1611260527 696w\" sizes=\"(max-width: 950px) 100vw, 950px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"clone-the-virtual-machine\">Clone the Virtual Machine<\/h4>\n\n\n\n<p>Once the machine is paused or shutdown, right click on it and click <strong>Clone<\/strong> to open the cloning window.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"951\" height=\"391\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-option.png\" alt=\"\" class=\"wp-image-7743\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-option.png?v=1611260559 951w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-option-768x316.png?v=1611260559 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-option-150x62.png?v=1611260559 150w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-option-300x123.png?v=1611260559 300w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/clone-option-696x286.png?v=1611260559 696w\" sizes=\"(max-width: 951px) 100vw, 951px\" \/><\/figure><\/div>\n\n\n<h4 class=\"wp-block-heading\" id=\"configure-the-kvm-clone\">Configure the KVM Clone<\/h4>\n\n\n\n<p>Set the name of the new cloned virtual machine<\/p>\n\n\n\n<p>Change the MAC address of each NIC by clicking on the <strong>Details<\/strong> button and enter the new MAC address. Usually generated by default. Click <strong>Ok<\/strong> when done.<\/p>\n\n\n\n<p>For each disk attached to the virtual machine being cloned, you can choose to;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Clone the disk so as to clone the disk for the new virtual machine<\/li>\n\n\n\n<li>Share the disks so as to share the existing disks between the new and the original virtual machine.<\/li>\n\n\n\n<li>Click <strong>Details<\/strong> for each option to be able to set a new path for the new cloned disk.<\/li>\n<\/ul>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"944\" height=\"844\" src=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/configure-kvm-clone.png\" alt=\"\" class=\"wp-image-7744\" title=\"\" srcset=\"https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/configure-kvm-clone.png?v=1611260586 944w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/configure-kvm-clone-768x687.png?v=1611260586 768w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/configure-kvm-clone-150x134.png?v=1611260586 150w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/configure-kvm-clone-300x268.png?v=1611260586 300w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/configure-kvm-clone-696x622.png?v=1611260586 696w, https:\/\/kifarunix.com\/wp-content\/uploads\/2021\/01\/configure-kvm-clone-470x420.png?v=1611260586 470w\" sizes=\"(max-width: 944px) 100vw, 944px\" \/><\/figure><\/div>\n\n\n<p>Once you are done configuring the KVM clone, click <strong>Clone<\/strong> to start the cloning process.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"clone-kvm-v-ms-using-virt-clone-command\">Clone KVM VMs using virt-clone command<\/h3>\n\n\n\n<p>You can as well clone KVM virtual machine using <strong><code>virt-clone<\/code><\/strong> command. According to man pages<em>;<\/em><\/p>\n\n\n\n<p>&#8220;<em>virt-clone is a command line tool for cloning existing virtual machine images using the &#8220;libvirt&#8221; hypervisor management library. It will copy the disk images of any existing virtual machine, and define a new guest with an identical virtual hardware configuration. Elements which require uniqueness will be updated to avoid a clash between old and new guests.<\/em><\/p>\n\n\n\n<p><em>By default, virt-clone will show an error if the necessary information to clone the guest is not provided. The &#8211;auto-clone option will generate all needed input, aside from the source guest to clone. Please note, virt-clone does not change anything _inside_ the guest OS, it only duplicates disks and does host side changes. So things like changing passwords, changing static IP address, etc are outside the scope of this tool. For these types of changes, please see virt-sysprep<\/em>&#8220;<\/p>\n\n\n\n<p>.To clone a virtual machine, get the name and shut it down.<\/p>\n\n\n\n<p>For example, to list all KVM virtual machines, run the command;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">virsh list --all<\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code> Id   Name            State\n--------------------------------\n -    kolla-ansible   shut off\n -    ubuntu20.04     shut off\n<\/code><\/pre>\n\n\n\n<p>If the virtual machine is running, then shut it down;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">virsh shutdown &lt;name here&gt;<\/pre>\n\n\n\n<p>Once the vm is shutdown, use <code>virt-clone<\/code> command to clone it. For example, to clone a virtual machine called ubuntu20.04;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">virt-clone --original ubuntu20.04 --auto-clone<\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>--original<\/code><\/strong>: Name of the original guest to be cloned. This guest must be shut off or paused since it is not possible to safely clone active guests at this time.<\/li>\n\n\n\n<li><strong><code>--auto-clone<\/code><\/strong>: Generate a new guest name, MAC address, and paths for new vm storage. If generated names collide with existing VMs or storage, a number is appended, e.g. foobar-clone-1.img<\/li>\n<\/ul>\n\n\n\n<p>To clone KVM Virtual Machine and set new name, use the command;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>virt-clone --connect=qemu:\/\/\/system --original source_vm --name new_vm --auto-clone<\/code><\/pre>\n\n\n\n<p>Where;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>--connect=qemu:\/\/\/system<\/code>: Specifies the hypervisor connection URI. Adjust this based on your specific setup. This option is optional.<\/li>\n\n\n\n<li><code>--name new_vm<\/code>: Specifies the desired name for the new virtual machine.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"clone-kvm-virtual-machine-with-multiple-disks-using-virt-clone\">Clone KVM Virtual Machine with Multiple Disks using virt-clone<\/h3>\n\n\n\n<p>Cloning a virtual machine with multiple disks and specifying the custom name and path to new file\/partition\/logical partition for the new guest&#8217;s virtual disk is also possible. See example command below;<\/p>\n\n\n\n<pre class=\"scroll-box\"><code>virt-clone --connect qemu:\/\/\/system \\\n\t--original demo --name newdemo \\\n\t--file \/var\/lib\/kvm\/images\/newdemo.img \\\n\t--file \/var\/lib\/kvm\/images\/newdata.img\n<\/code><\/pre>\n\n\n\n<p>Read more about using <code><strong>virt-clone<\/strong><\/code> on;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">virt-clone --help<\/pre>\n\n\n\n<pre class=\"scroll-box\"><code>usage: virt-clone --original [NAME] ...\n\nDuplicate a virtual machine, changing all the unique host side configuration like MAC address, name, etc. \n\nThe VM contents are NOT altered: virt-clone does not change anything _inside_ the guest OS, it only duplicates disks and does host side changes. So things like changing passwords, changing static IP address, etc are outside the scope of this tool. For these types of changes, please see virt-sysprep(1).\n\noptional arguments:\n  -h, --help            show this help message and exit\n  --version             show program's version number and exit\n  --connect URI         Connect to hypervisor with libvirt URI\n\nGeneral Options:\n  -o ORIGINAL_GUEST, --original ORIGINAL_GUEST\n                        Name of the original guest; The status must be shut off or paused.\n  --original-xml ORIGINAL_XML\n                        XML file to use as the original guest.\n  --auto-clone          Auto generate clone name and storage paths from the original guest configuration.\n  -n NEW_NAME, --name NEW_NAME\n                        Name for the new guest\n  --reflink             use btrfs COW lightweight copy\n\nStorage Configuration:\n  -f NEW_DISKFILE, --file NEW_DISKFILE\n                        New file to use as the disk image for the new guest\n  --force-copy TARGET   Force to copy devices (eg, if 'hdc' is a readonly cdrom device, --force-copy=hdc)\n  --skip-copy SKIP_COPY\n                        Skip copy of the device target. (eg, if 'vda' is a disk you don't want to copy and use the same path in the new VM, use --skip-copy=vda)\n  --nonsparse           Do not use a sparse file for the clone's disk image\n  --preserve-data       Do not clone storage, new disk images specified via --file are preserved unchanged\n  --nvram NEW_NVRAM     New file to use as storage for nvram VARS\n\nNetworking Configuration:\n  -m NEW_MAC, --mac NEW_MAC\n                        New fixed MAC address for the clone guest. Default is a randomly generated MAC\n\nMiscellaneous Options:\n  --replace             Don't check name collision, overwrite any guest with the same name.\n  --print-xml           Print the generated domain XML rather than create the guest.\n  --check CHECK         Enable or disable validation checks. Example:\n                        --check path_in_use=off\n                        --check all=off\n  -q, --quiet           Suppress non-error output\n  -d, --debug           Print debugging information\n\nSee man page for examples and full option syntax.\n<\/code><\/pre>\n\n\n\n<p>Once the cloning is done, you list available vms to check;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">virsh list --all<\/pre>\n\n\n\n<pre class=\"scroll-sz\"><code> Id   Name                State\n------------------------------------\n -    kolla-ansible       shut off\n -    ubuntu20.04         shut off\n -    ubuntu20.04-clone   shut off\n<\/code><\/pre>\n\n\n\n<p>You can then start your cloned virtual machine and start setting it up including changing password, setting up IPs e.t.c.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">virsh start ubuntu20.04-clone<\/pre>\n\n\n\n<p>And that is all on how to clone KVM virtual machine.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"other-tutorials\">Other Tutorials<\/h3>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" class=\"rank-math-link\" href=\"https:\/\/kifarunix.com\/how-to-fix-qemu-kvm-not-connected-error-on-ubuntu-20-04\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to fix QEMU\/KVM Not Connected Error on Linux<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/use-virtualbox-vms-on-kvm\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Use VirtualBox VMs on KVM<\/a><\/p>\n\n\n\n<p><a aria-label=\" (opens in a new tab)\" href=\"https:\/\/kifarunix.com\/how-to-rename-kvm-virtual-machine-with-virsh-command\/\" target=\"_blank\" rel=\"noreferrer noopener\" class=\"rank-math-link\">How to Rename KVM Virtual Machine with virsh command<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is the quick and easy way to clone KVM virtual machines? In this guide, we&#8217;ll explore the step-by-step procedure for cloning KVM virtual machines<\/p>\n","protected":false},"author":1,"featured_media":7739,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[112,121,36],"tags":[3042,3044,3043,3048,3045,3046,3047],"class_list":["post-7736","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kvm","category-howtos","category-virtualization","tag-clone-kvm-guest-vms","tag-clone-kvm-virtual-machine","tag-clone-virtual-machine-on-kvm","tag-cloning-virtual-machine","tag-qemu-clone-guests","tag-virt-clone","tag-virt-manager","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50","resize-featured-image"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7736"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=7736"}],"version-history":[{"count":9,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7736\/revisions"}],"predecessor-version":[{"id":21961,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7736\/revisions\/21961"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media\/7739"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=7736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=7736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=7736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}