{"id":425,"date":"2014-12-01T01:22:23","date_gmt":"2014-12-01T06:22:23","guid":{"rendered":"http:\/\/muthii.com\/blog\/?p=425"},"modified":"2019-02-14T01:55:05","modified_gmt":"2019-02-14T06:55:05","slug":"kvm-usage-tips","status":"publish","type":"post","link":"https:\/\/muthii.com\/blog\/?p=425","title":{"rendered":"KVM usage tips"},"content":{"rendered":"<p>I have been playing around with KVM lately, just to see how it stacks up against VirtualBox and am loving it. I have found the following commands interesting while working with VMs.<\/p>\n<p><strong>List all VMs<\/strong><br \/>\n<code>virsh list --all<\/code><\/p>\n<p><strong>Start stop a VM<\/strong><br \/>\n<code>virsh start\/stop vmName<\/code><\/p>\n<p><strong>Clone VM <\/strong><br \/>\n<code>virt-clone --original vmName --name newVmName --file \/some\/path\/newVmName.qcow2<\/code><\/p>\n<p><strong>Clone VM and assign Random Mac address to new image<\/strong><br \/>\n<code>virt-clone --original vmName --name newVmName --file \/some\/path\/newVmName.qcow2 --mac=RANDOM<\/code><br \/>\nCopy over the xml for the new clone which you can use to define your clone if moving the image to another host<br \/>\n<code>cp \/etc\/libvirt\/qemu\/newVmName.xml \/some\/path\/newVmName.xml<\/code><\/p>\n<p><strong>Shrink\/Sparcify VM image<\/strong><br \/>\n<code>virt-sparsify --format qcow2 --compress vmName.bkp.qcow2 vmName.qcow2 --tmp \/some\/path<\/code><br \/>\nThis command assumes you renamed the original VM image file to &#8220;vmName.bkp.qcow2&#8221; incase the new image has problems you can revert to original image. You can skip the &#8211;tmp argument if you have more than double the size of the VM image available in your \/tmp directory. You will need to make sure you have package &#8220;libguestfs-tools&#8221; installed which supplies the sparsify command.<\/p>\n<p><strong>Delete VM<\/strong><br \/>\n<code>virsh undefine vmName<\/code><br \/>\nThis removes the XMl configuration for qemu you might have to delete the VM image yourself.<\/p>\n<p><strong>List storage pools<\/strong><br \/>\n<code>virsh pool-list<\/code><\/p>\n<p><strong>Refresh pool data<\/strong><br \/>\n<code>virsh pool-refresh poolName<\/code><br \/>\nYou will need to run this command if you delete a VM and you need to reuse the VM name you had used, so make sure you refresh the pool where your deleted image was located.<\/p>\n<h2>Disable autostarting of a VM<\/h2>\n<p><code>virsh autostart vmName --disable<\/code><\/p>\n<p><strong>Move VM to another system Offline<\/strong><br \/>\nStep 1 from original host shutdown your VM, copy xml and image to backup location.<br \/>\n<code><br \/>\nvirsh dumpxml vmName &gt; \/bkpLocation\/vmName.xml<br \/>\ncp \/locationofVMs\/vmname.qcow2 \/bkpLocation\/vmName.qcow2<br \/>\n<\/code><br \/>\nBye default if you didn&#8217;t change image storage location it will be &#8220;\/var\/lib\/libvirt\/images&#8221;. I prefer to store them in a separate partition from the OS.<\/p>\n<p>Step 2 on new host get VM xml and image and enjoy.<br \/>\n<code><br \/>\nvirsh define \/bkpLocation\/vmName.xml<br \/>\nDomain vmName defined from \/bkpLocation\/vmName.xml<br \/>\nvirsh start vmName<br \/>\n<\/code><br \/>\nThis all works if you are moving from host1 and host2 with similar cpus, for my purpose host2 had a different cpu type so I had to first create a new vm in host2 and compare the xml with my backed up copy then changed a few lines specific to CPU on host2.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been playing around with KVM lately, just to see how it stacks up against VirtualBox and am loving it. I have found the following commands interesting while working with VMs. List all VMs virsh list &#8211;all Start stop a VM virsh start\/stop vmName Clone VM virt-clone &#8211;original vmName &#8211;name newVmName &#8211;file \/some\/path\/newVmName.qcow2 Clone &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/muthii.com\/blog\/?p=425\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;KVM usage tips&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[102],"tags":[140],"_links":{"self":[{"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/425"}],"collection":[{"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=425"}],"version-history":[{"count":9,"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions"}],"predecessor-version":[{"id":538,"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/425\/revisions\/538"}],"wp:attachment":[{"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/muthii.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}