qemu-kvm https://ff.co.za/index.php/ en Resize qcow2 with blockresize on running VM https://ff.co.za/index.php/documentation/resize-qcow2-blockresize-running-vm <span>Resize qcow2 with blockresize on running VM</span> <span><span lang="" about="/index.php/user/jeff" typeof="schema:Person" property="schema:name" datatype="">Jeff</span></span> <span>Wed, 09/03/2022 - 05:46</span> <div class="layout layout--onecol"> <div class="layout__region layout__region--content"> <div class="body-padding field field--name-body field--type-text-with-summary field--label-hidden field--item"><nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <p><strong>Table of Contents</strong></p> <ul> <li><a href="/documentation/install-qemu-kvm-debian-9">Install QEMU-KVM on Debian 9</a></p> <ul> <li><a href="/documentation/allow-users-run-virsh-and-manage-vms">Allow users to run virsh and manage VMs</a></li> <li><a href="/documentation/kvm-bridged-networking">KVM Bridged Networking</a></li> <li><a href="/documentation/kvm-virsh-console-access-debian-and-freebsd-vms">KVM Virsh Console Access for Debian and FreeBSD VMs</a></li> </ul> </li> </ul> </nav> <nav aria-label="Book outline for Elliptic Curve Crypto &amp; LetsEncrypt" role="navigation"> <ul></ul> </nav> <nav aria-label="Book outline for Managing qcow2 images" role="navigation"> <ul> <li> <ul> <li><a href="https://forum.ff.co.za/documentation/managing-qcow2-images" hreflang="en">Managing qcow2 images</a></p> <ul> <li><a href="/documentation/backup-virtual-machine">Backup virtual machine</a></li> <li><a href="/copy-sparse-file-across-network-using-dd-or-rsync" hreflang="en">Copy sparse file across network using dd or rsync</a></li> <li><a href="/documentation/how-mount-qcow2-image-qemu-nbd" hreflang="en">How to mount a qcow2 image with qemu-nbd</a></li> <li><a href="/documentation/reclaiming-space-qcow2-disk-image" hreflang="en">Reclaiming space on qcow2 disk image</a></li> <li><u><em><strong><a href="/documentation/resize-qcow2-blockresize-running-vm">Resize qcow2 with blockresize on running VM</a></strong></em></u></li> </ul> </li> </ul> </li> </ul> </nav> <ul> <li><a href="/documentation/migrate-vm-minimal-downtime">Migrate a VM with Minimal Downtime</a></li> </ul> <p> </p> <p>I was pretty apprehensive about the potential downtime with resizing a disk image with virsh img-resize, and searched all over for, specifically, what I had in mind, which was to resize the backing image while 'quiescent' and the server's data being stored to the snapshot image.</p> </nav> <p>So I sent an email to <a href="mailto:libvirt-users@redhat.com">libvirt-users@redhat.com</a> (there are some amazing wizards there!) and got an almost immediate response from Peter:</p> <p>You can resize a QCOW2 (or any VM disk image) with virsh blockresize VM /path/to/image.qcow2 SIZE'.</p> <p><strong>This <em>requires</em> the VM to be running.</strong></p> <p><code>virsh blockresize ISPA /var/lib/libvirt/images/ispa.qcow2 300G</code></p> <p><strong>Result:</strong><br /><code>jeff@ispa-mx ~ $ sudo fdisk -l<br /> [sudo] password for jeff:  <br /> Disk /dev/sda: 300 GiB, 322122547200 bytes, 629145600 sectors</code><br />  </p> <p>Next, Install gdisk and cloud utils package for growpart, a Linux command line tool used to extend a partition in a partition table to fill available space.</p> <p><code>apt install cloud-utils gdisk</code></p> <p>Problem is that often/usually, in our case, the swap partition sits at the end of the disk, so it needs to be removed and added back after growing the primary data partition sda1.</p> <pre> <code>root@ispa-mx:~# lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 300G 0 disk ├─sda1 8:1 0 212.2G 0 part / └─sda2 8:2 0 7.8G 0 part [SWAP]</code></pre><p> Turn swap OFF.</p> <p>swapoff -a</p> <p><strong>EDIT /etc/fstab and comment it OUT as a reboot may be required and it would hang the system because after recreating the partition the UUID will have changed!</strong></p> <p>Overview to end: Run fdisk, delete the swap, then recreate the swap partition at the end of the disk where it belongs. Write fdisk changes and run partprobe to get the kernel reinitialised with the change. </p> <p><strong>Then:</strong></p> <p><code>growpart /dev/sda 1 #Note the space</code></p> <p><code>resize2fs /dev/sda1</code></p> <p><strong>Then fix the swap partition:</strong></p> <pre> <code>mkswap /dev/sda2 Setting up swapspace version 1, size = 7.8 GiB (8389423104 bytes) no label, UUID=5a00a73e-a01c-4900-8a63-ae76c42fa61f </code></pre><p><strong>Turn swap back on:</strong></p> <p><code>swapon -a</code></p> <p>Note the new UUID. Edit /etc/fstab and update the swap UUID.</p> <p>Fin.</p> </div> <div class="field field--name-field-category field--type-entity-reference field--label-above"> <div class="field--label">Category</div> <div class="field--item"><a href="/index.php/documentation" hreflang="en">Documentation</a></div> </div> <div class="fftags field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <div class="field--items"> <div class="field--item"><a href="/index.php/tags/kvm" hreflang="en">KVM</a></div> <div class="field--item"><a href="/index.php/tags/qemu-kvm" hreflang="en">qemu-kvm</a></div> <div class="field--item"><a href="/index.php/tags/swap" hreflang="en">swap</a></div> <div class="field--item"><a href="/index.php/tags/fdisk" hreflang="en">fdisk</a></div> <div class="field--item"><a href="/index.php/tags/partprobe" hreflang="en">partprobe</a></div> <div class="field--item"><a href="/index.php/tags/growpart" hreflang="en">growpart</a></div> <div class="field--item"><a href="/index.php/tags/resize2fs" hreflang="en">resize2fs</a></div> </div> </div> </div> </div> Wed, 09 Mar 2022 03:46:15 +0000 Jeff 241 at https://ff.co.za Migrate a VM with Minimal Downtime https://ff.co.za/index.php/documentation/migrate-vm-minimal-downtime <span>Migrate a VM with Minimal Downtime</span> <span><span lang="" about="/index.php/user/admin" typeof="schema:Person" property="schema:name" datatype="">Admin</span></span> <span>Tue, 26/11/2019 - 10:17</span> <div class="layout layout--onecol"> <div class="layout__region layout__region--content"> <div class="body-padding field field--name-body field--type-text-with-summary field--label-hidden field--item"><nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <p><strong>Table of Contents</strong></p> <ul> <li><a href="/documentation/install-qemu-kvm-debian-9">Install QEMU-KVM on Debian 9</a></p> <ul> <li><a href="/documentation/allow-users-run-virsh-and-manage-vms">Allow users to run virsh and manage VMs</a></li> <li><a href="/documentation/kvm-bridged-networking">KVM Bridged Networking</a></li> <li><a href="/documentation/kvm-virsh-console-access-debian-and-freebsd-vms">KVM Virsh Console Access for Debian and FreeBSD VMs</a></li> </ul> </li> </ul> </nav> <nav aria-label="Book outline for Elliptic Curve Crypto &amp; LetsEncrypt" role="navigation"> <ul></ul> </nav> <nav aria-label="Book outline for Managing qcow2 images" role="navigation"> <ul> <li> <ul> <li><a href="https://forum.ff.co.za/documentation/managing-qcow2-images" hreflang="en">Managing qcow2 images</a></p> <ul> <li><a href="/documentation/backup-virtual-machine">Backup virtual machine</a></li> <li><a href="/copy-sparse-file-across-network-using-dd-or-rsync" hreflang="en">Copy sparse file across network using dd or rsync</a></li> <li><a href="/documentation/how-mount-qcow2-image-qemu-nbd" hreflang="en">How to mount a qcow2 image with qemu-nbd</a></li> <li><a href="/documentation/reclaiming-space-qcow2-disk-image" hreflang="en">Reclaiming space on qcow2 disk image</a></li> <li><a href="/documentation/resize-qcow2-blockresize-running-vm">Resize qcow2 with blockresize on running VM</a></li> </ul> </li> </ul> </li> </ul> </nav> <ul> <li><em><u><strong><a href="/documentation/migrate-vm-minimal-downtime">Migrate a VM with Minimal Downtime</a></strong></u></em></li> </ul> <p> </p> <p>This script takes a snaphot of the running VM, which leaves the qcow2 backing images in a known state. All VM activity is then recorded in the snapshots, while the backing images are rsync'd to a reciprocal hypervisor on the 1G VLAN.</p> <p>After the backing images are transferred, the VM is shutdown and snapshots copied to the reciprocal hypervisor. The server is defined on the new host, started, and the snapshots are merged into the backing image/s.</p> <p> Total downtime depends on the size of the snapshots, but is usually less than a minute or two. The whole process takes about an hour an a half for a ~200GB QCOW2 image over a 1GB/sec VLAN.</p> <p><span id="cke_bm_139S" style="display: none;"> </span> Example 1:</p> <pre> <code class="language-bash">jeff@sheri:/var/lib/libvirt/images/scripts$ cat skillsregistry.migrate.sh #!/bin/bash ################################################################################ ###################### Migrate VM to a new hypervisor ######################## # Authors: Jeff Brown / Lucio de Re # Date: 2019/11/26 # # This script takes a snaphot of the running VM, which leaves the qcow2 backing # images in a known state. All VM activity is then recorded in the snapshots, # while the backing images are rsync'd to a reciprocal hypervisor on the VLAN. # After the backing images are transferred, the VM is shutdown and snapshots # copied to the reciprocal hypervisor. The server is defined on the new host, # started, and the snapshots are merged into the backing image/s. # Total downtime depends on the size of the snapshots, but is usually # less than a minute or two. The whole process takes about an hour and a half. # # NB. This script must be run by a user who is a member of libvirt-qemu group. # NB. The user must also be able to run sudo aa-teardown/chown/chmod without password. # E.g. jeff ALL=NOPASSWD:/bin/chown, /bin/chmod # # 192.100.0.4 (vespa.org.za) # 192.100.0.82 (abispa.org.za) # ################################################################################ cd /var/lib/libvirt/images/ virsh snapshot-create-as --domain skillsregistry skillsregistry.snapshot --diskspec sda,file=/var/lib/libvirt/images/snapshots/skillsregistry.snapshot.qcow2 --disk-only --atomic export RSYNC_PASSWORD="real password for rsyncd secret" rsync -ShavW --progress /var/lib/libvirt/images/skillsregistry.qcow2 rsync://tergum@192.168.100.82/bvar/lib/libvirt/images/skillsregistry.qcow2 virsh shutdown skillsregistry x=0 while sleep 5; do virsh list --all | grep skillsregistry | grep "shut off" &amp;&amp; break x=$(expr $x + 1) [ $x -gt 120 ] &amp;&amp; { echo &gt;&amp;2 timed out after $(expr $x \* 5) seconds; exit 3; } done virsh autostart skillsregistry --disable virsh -c qemu:///system dumpxml skillsregistry &gt; /var/lib/libvirt/images/xml/skillsregistry.xml scp /var/lib/libvirt/images/xml/skillsregistry.xml 192.168.100.82:/var/lib/libvirt/images/xml/ sudo find . -type f -exec chown libvirt-qemu:libvirt-qemu {} \; -exec chmod g+rw {} \; rsync -ShavW --progress /var/lib/libvirt/images/snapshots/skillsregistry.snapshot.qcow2 rsync://tergum@192.168.100.82/bvar/lib/libvirt/images/snapshots/ export RSYNC_PASSWORD="nopass" virsh -c qemu+ssh://192.168.100.82/system define /var/lib/libvirt/images/xml/skillsregistry.xml virsh -c qemu+ssh://192.168.100.82/system start skillsregistry x=0 while sleep 5; do virsh -c qemu+ssh://192.168.100.82/system list --all | grep skillsregistry | grep "running" &amp;&amp; break x=$(expr $x + 1) [ $x -gt 120 ] &amp;&amp; { echo &gt;&amp;2 timed out after $(expr $x \* 5) seconds; exit 3; } done # Required for Debian 10 + to disable apparmor! : ssh 192.168.100.82 sudo aa-teardown virsh -c qemu+ssh://192.168.100.82/system blockcommit skillsregistry sda --active --pivot --shallow --verbose virsh -c qemu+ssh://192.168.100.82/system autostart skillsregistry </code></pre><p><strong><em>(Don't forget to delete the snapshots left behind in case we need to revert!)</em></strong></p> <p> </p> <p><strong>Different examples to copy sparse images:</strong></p> <div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;">rsync --rsync-path="ionice -n 7 nice -n19 rsync" -havW --progress /var/lib/libvirt/images/UAT.qcow2 192.168.100.1:/var/lib/libvirt/images/</div> <div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"> </div> <div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;">cd /var/lib/libvirt/images/; tar czf - DNC.qcow2 | ssh 192.168.100.1 cd /var/lib/libvirt/images/ \&amp;\&amp; tar xzvf -</div> <div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"> </div> <div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;">rsync -SWv --show-progress rsync://tergum@192.168.100.4/bvar/lib/libvirt/images/FFDev.qcow2 /tmp/</div> <div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"> </div> <div style="caret-color: rgb(0, 0, 0); color: rgb(0, 0, 0); font-family: Helvetica; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none;"> <div> </div> </div> </div> <div class="field field--name-field-category field--type-entity-reference field--label-above"> <div class="field--label">Category</div> <div class="field--item"><a href="/index.php/documentation" hreflang="en">Documentation</a></div> </div> <div class="fftags field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <div class="field--items"> <div class="field--item"><a href="/index.php/tags/qemu-kvm" hreflang="en">qemu-kvm</a></div> </div> </div> </div> </div> Tue, 26 Nov 2019 08:17:45 +0000 Admin 210 at https://ff.co.za https://ff.co.za/index.php/documentation/migrate-vm-minimal-downtime#comments Install QEMU-KVM on Debian 9 https://ff.co.za/index.php/documentation/install-qemu-kvm-debian-9 <span property="schema:name">Install QEMU-KVM on Debian 9</span> <span rel="schema:author"><span lang="" about="/index.php/user/jeff" typeof="schema:Person" property="schema:name" datatype="">Jeff</span></span> <span property="schema:dateCreated" content="2019-02-25T07:17:07+00:00">Mon, 25/02/2019 - 09:17</span> <div class="layout layout--onecol"> <div class="layout__region layout__region--content"> <drupal-render-placeholder callback="flag.link_builder:build" arguments="0=node&amp;1=151&amp;2=bookmark" token="XPK8bLm8YkE4mPq5OZrq4ElhcB8rr0clcAAWrTEPcRU"></drupal-render-placeholder><drupal-render-placeholder callback="flag.link_builder:build" arguments="0=node&amp;1=151&amp;2=like" token="QbRN8_kXQEwTO4zTEE9eJoP0Fgyb_IWTGh09e-nWwo0"></drupal-render-placeholder> </div> </div> Mon, 25 Feb 2019 07:17:07 +0000 Jeff 151 at https://ff.co.za https://ff.co.za/index.php/documentation/install-qemu-kvm-debian-9#comments Allow users to run virsh and manage VMs https://ff.co.za/index.php/documentation/allow-users-run-virsh-and-manage-vms <span>Allow users to run virsh and manage VMs</span> <span><span lang="" about="/index.php/user/jeff" typeof="schema:Person" property="schema:name" datatype="">Jeff</span></span> <span>Tue, 13/11/2018 - 06:59</span> <div class="layout layout--onecol"> <div class="layout__region layout__region--content"> <div class="body-padding field field--name-body field--type-text-with-summary field--label-hidden field--item"><nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <p><strong>Table of Contents</strong></p> <ul> <li><a href="/documentation/install-qemu-kvm-debian-9">Install QEMU-KVM on Debian 9</a></p> <ul> <li><em><u><strong><a href="/documentation/allow-users-run-virsh-and-manage-vms">Allow users to run virsh and manage VMs</a></strong></u></em></li> <li><a href="/documentation/kvm-bridged-networking">KVM Bridged Networking</a></li> <li><a href="/documentation/kvm-virsh-console-access-debian-and-freebsd-vms">KVM Virsh Console Access for Debian and FreeBSD VMs</a></li> </ul> </li> </ul> </nav> <nav aria-label="Book outline for Elliptic Curve Crypto &amp; LetsEncrypt" role="navigation"> <ul></ul> </nav> <nav aria-label="Book outline for Managing qcow2 images" role="navigation"> <ul> <li> <ul> <li><a href="/documentation/managing-kvm-and-qcow2-images" hreflang="en">Managing qcow2 images</a></p> <ul> <li><a href="/documentation/backup-virtual-machine">Backup virtual machine</a></li> <li><a href="/documentation/copy-sparse-file-across-network-using-dd-or-rsync" hreflang="en">Copy sparse file across network using dd or rsync</a></li> <li><a href="/documentation/how-mount-qcow2-image-qemu-nbd" hreflang="en">How to mount a qcow2 image with qemu-nbd</a></li> <li><a href="/documentation/reclaiming-space-qcow2-disk-image" hreflang="en">Reclaiming space on qcow2 disk image</a></li> <li><a href="/documentation/resize-qcow2-blockresize-running-vm">Resize qcow2 with blockresize on running VM</a></li> </ul> </li> </ul> </li> </ul> </nav> <ul> <li><a href="/documentation/migrate-vm-minimal-downtime">Migrate a VM with Minimal Downtime</a></li> </ul> </nav> <nav aria-label="Book outline for Elliptic Curve Crypto &amp; LetsEncrypt" role="navigation"> <p>Sources:</p> </nav> <p><a href="https://libvirt.org/aclpolkit.html">https://libvirt.org/aclpolkit.html</a><br /> <a href="https://major.io/2015/04/11/run-virsh-and-access-libvirt-as-a-regular-user/">https://major.io/2015/04/11/run-virsh-and-access-libvirt-as-a-regular-u…</a><br /> <a href="https://www.poftut.com/use-virt-manager-libvirt-normal-user-without-root-privileges-without-asking-password/">https://www.poftut.com/use-virt-manager-libvirt-normal-user-without-roo…</a></p> <p>Defining custom rules requires creation of a file in the /etc/polkit-1/rules.d directory with a name chosen by the administrator (100-libvirt-acl.rules would be a reasonable choice). See the polkit(8) manual page for a description of how to write these files in general.</p> <p>We're going to allow members of the libvirt group to manage VM's:</p> <p><code>mkdir /etc/polkit-1/rules.d</code><br /> vi /etc/polkit-1/rules.d/100-libvirt-acl.rules</p> <p><code>polkit.addRule(function(action, subject) </code><code>{<br /> if (action.id == "org.libvirt.unix.manage" &amp;&amp; subject.local &amp;&amp; subject.active &amp;&amp; subject.isInGroup("libvirt")) {<br /> return polkit.Result.YES;<br /> }<br /> });</code></p> <p><strong>NB. Add to user's .bashrc:</strong></p> <p><code>export LIBVIRT_DEFAULT_URI=qemu:///system</code></p> <p> </p> <h2>Add bash completion library:</h2> <p>Download the bash completion library: <a href="https://github.com/LuyaoHuang/virsh-bash-completion/blob/master/virsh_bash_completion">virsh_bash_completion</a></p> <p>How to use it: Copy virsh_bash_completion file to /etc/bash_completion.d/ then restart bash.</p> <p>(<a href="https://github.com/LuyaoHuang/virsh-bash-completion">Source</a>)</p> <p> </p> </div> <div class="field field--name-field-category field--type-entity-reference field--label-above"> <div class="field--label">Category</div> <div class="field--item"><a href="/index.php/documentation" hreflang="en">Documentation</a></div> </div> <div class="fftags field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <div class="field--items"> <div class="field--item"><a href="/index.php/tags/libvirt" hreflang="en">libvirt</a></div> <div class="field--item"><a href="/index.php/tags/virsh" hreflang="en">virsh</a></div> <div class="field--item"><a href="/index.php/tags/qemu-kvm" hreflang="en">qemu-kvm</a></div> <div class="field--item"><a href="/index.php/tags/polkit" hreflang="en">polkit</a></div> </div> </div> </div> </div> Tue, 13 Nov 2018 04:59:46 +0000 Jeff 127 at https://ff.co.za Backup Virtual Machine https://ff.co.za/index.php/documentation/backup-virtual-machine <span>Backup Virtual Machine</span> <span><span lang="" about="/index.php/user/jeff" typeof="schema:Person" property="schema:name" datatype="">Jeff</span></span> <span>Sun, 11/11/2018 - 10:21</span> <div class="layout layout--onecol"> <div class="layout__region layout__region--content"> <div class="body-padding field field--name-body field--type-text-with-summary field--label-hidden field--item"><nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <p><strong>Table of Contents</strong></p> <ul> <li><a href="/documentation/install-qemu-kvm-debian-9">Install QEMU-KVM on Debian 9</a></p> <ul> <li><a href="/documentation/allow-users-run-virsh-and-manage-vms">Allow users to run virsh and manage VMs</a></li> <li><a href="/documentation/kvm-bridged-networking">KVM Bridged Networking</a></li> <li><a href="/documentation/kvm-virsh-console-access-debian-and-freebsd-vms">KVM Virsh Console Access for Debian and FreeBSD VMs</a></li> </ul> </li> </ul> </nav> <nav aria-label="Book outline for Elliptic Curve Crypto &amp; LetsEncrypt" role="navigation"> <ul></ul> </nav> <nav aria-label="Book outline for Managing qcow2 images" role="navigation"> <ul> <li> <ul> <li><a href="/documentation/managing-kvm-and-qcow2-images" hreflang="en">Managing qcow2 images</a></p> <ul> <li><em><u><strong><a href="/documentation/backup-virtual-machine">Backup virtual machine</a></strong></u></em></li> <li><a href="/documentation/copy-sparse-file-across-network-using-dd-or-rsync" hreflang="en">Copy sparse file across network using dd or rsync</a></li> <li><a href="/documentation/how-mount-qcow2-image-qemu-nbd" hreflang="en">How to mount a qcow2 image with qemu-nbd</a></li> <li><a href="/documentation/reclaiming-space-qcow2-disk-image" hreflang="en">Reclaiming space on qcow2 disk image</a></li> <li><a href="/documentation/resize-qcow2-blockresize-running-vm">Resize qcow2 with blockresize on running VM</a></li> </ul> </li> </ul> </li> </ul> </nav> <ul> <li><a href="/documentation/migrate-vm-minimal-downtime">Migrate a VM with Minimal Downtime</a></li> </ul> <p> </p> </nav> <p><strong>Sources:</strong></p> <ul> <li><a href="https://www.virtkick.com/docs/how-to-perform-a-live-backup-on-your-kvm-virtual-machines.html">www.virtkick.com/docs/how-to-perform-a-live-backup-on-your-kvm-virtual-machines.html</a></li> <li><a href="https://wiki.libvirt.org/page/Live-disk-backup-with-active-blockcommit">wiki.libvirt.org/page/Live-disk-backup-with-active-blockcommit</a></li> </ul> <p> </p> <h2>1. As root, locate the virtual machine you want to back up using virsh:</h2> <p><code>virsh -c qemu:///system list # shows only running</code></p> <p>or</p> <p><code>virsh list --all # shows all</code></p> <p> </p> <h2>2. Dump the configuration for your virtual machine:</h2> <p><code>virsh -c qemu:///system dumpxml MACHINE_NAME &gt; /var/lib/libvirt/images/safe/MACHINE_NAME.datestamp.xml</code></p> <p> </p> <h2>3. You may need to locate the image directory:</h2> <p><code>virsh -c qemu:///system domblklist MACHINE_NAME</code></p> <p> </p> <h2>4. Now prepare the backup of this machine using a snapshot:</h2> <p><code>virsh snapshot-create-as --domain MACHINE_NAME IMAGE_NAME.snapshot --diskspec sda,file=/var/lib/libvirt/images/safe/IMAGE_NAME.snapshot.qcow2 --disk-only --atomic</code></p> <p>Note: If you get an error, then the disk is not converted to virtio-scsi, and should be <strong>vda</strong>, not sda.</p> <p><em>After creating the snapshot, all disk operations from the guest will be directed to it so that you can copy the original disk image to a safe place.</em></p> <p>You can establish this by running, as example:</p> <pre> <code class="language-bash">virsh -c qemu:///system domblklist ispa Target Source ------------------------------------------------ hda - sda /var/lib/libvirt/images/safe/ispa.snapshot.qcow2</code></pre><p> </p> <h2>5. Copy to external server and include both the qcow2 disk image and the machine configuration (the XML file):</h2> <p>su - yourusername   # you must be in libvirt-qemu group</p> <p><code>rsync -hav --progress /var/lib/libvirt/images/IMAGE_NAME.qcow2 192.168.100.2:backups/ # IP address of prionyx on 1GB VLAN</code></p> <p> </p> <h2>6. Merge snapshot:</h2> <p>After the file transfer is completed, you should merge changes written to snapshot, back to original disk image. This operation is called 'active block commit':</p> <p><code>virsh blockcommit MACHINE_NAME sda --active --pivot --shallow --verbose</code></p> <p>Check block devices again. If the block commit completed successfuly your virtual machine should use its original image again.</p> <p><code>virsh -c qemu:///system domblklist MACHINE_NAME</code></p> <p> </p> <h2>Example Backup script for running VMs:</h2> <pre> <code class="language-bash">jeff@sheri:~$ cat /var/lib/libvirt/images/scripts/ispa.backup.sh #!/bin/bash ################################################################################ # # This script takes a snaphot of the running VM, which leaves the main qcow2 # image in a known state. All VM activity is then recorded in the snapshot, # while the main qcow2 image can be rsync'd to a remote backup server. # After the image has been transferred, the snapshot is merged back to the # main image, and then deleted. # # NB. This script must be run by a user who is a member of libvirt-qemu group. # ################################################################################ virsh -c qemu:///system dumpxml ISPA &gt; /var/lib/libvirt/images/xml/ispa.xml scp /var/lib/libvirt/images/xml/ispa.xml 192.168.100.4:/var/lib/libvirt/images/xml/ virsh snapshot-create-as --domain ISPA ISPA.snapshot --diskspec sda,file=/var/lib/libvirt/images/snapshots/ispa.snapshot.qcow2 --disk-only --atomic rsync --rsync-path="ionice -c 3 nice rsync" -havW --progress /var/lib/libvirt/images/ispa.qcow2 192.168.100.4:/var/lib/libvirt/images/sheriff/ virsh blockcommit ISPA sda --active --pivot --shallow --verbose virsh snapshot-delete ISPA --metadata ISPA.snapshot rm -rf /var/lib/libvirt/images/snapshots/ispa.snapshot.qcow2 #rm /var/lib/libvirt/images/xml/ispa.xml </code></pre><p> </p> </div> <div class="field field--name-field-category field--type-entity-reference field--label-above"> <div class="field--label">Category</div> <div class="field--item"><a href="/index.php/documentation" hreflang="en">Documentation</a></div> </div> <div class="fftags field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <div class="field--items"> <div class="field--item"><a href="/index.php/tags/backups" hreflang="en">backups</a></div> <div class="field--item"><a href="/index.php/tags/kvm" hreflang="en">KVM</a></div> <div class="field--item"><a href="/index.php/tags/rysnc" hreflang="en">rysnc</a></div> <div class="field--item"><a href="/index.php/tags/qemu-kvm" hreflang="en">qemu-kvm</a></div> </div> </div> </div> </div> Sun, 11 Nov 2018 08:21:51 +0000 Jeff 126 at https://ff.co.za Managing KVM and qcow2 images https://ff.co.za/index.php/documentation/managing-kvm-and-qcow2-images <span>Managing KVM and qcow2 images</span> <span><span lang="" about="/index.php/user/jeff" typeof="schema:Person" property="schema:name" datatype="">Jeff</span></span> <span>Sat, 01/09/2018 - 10:11</span> <div class="layout layout--onecol"> <div class="layout__region layout__region--content"> <div class="body-padding field field--name-body field--type-text-with-summary field--label-hidden field--item"><p>This is a series of examples to provide a reference manual to manage qcow2 disk images.</p> <p>It will include reclaiming space, mounting an unused image, resizing images, and more. So far we have:</p> <nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <nav aria-label="Book outline for Install QEMU-KVM on Debian 9" role="navigation"> <p><strong>Table of Contents</strong></p> <ul> <li><a href="/documentation/install-qemu-kvm-debian-9">Install QEMU-KVM on Debian 9</a></p> <ul> <li><a href="/documentation/allow-users-run-virsh-and-manage-vms">Allow users to run virsh and manage VMs</a></li> <li><a href="/documentation/kvm-bridged-networking">KVM Bridged Networking</a></li> <li><a href="/documentation/kvm-virsh-console-access-debian-and-freebsd-vms">KVM Virsh Console Access for Debian and FreeBSD VMs</a></li> </ul> </li> </ul> </nav> <nav aria-label="Book outline for Elliptic Curve Crypto &amp; LetsEncrypt" role="navigation"> <ul></ul> </nav> <nav aria-label="Book outline for Managing qcow2 images" role="navigation"> <ul> <li> <ul> <li><em><u><strong><a href="https://forum.ff.co.za/documentation/managing-kvm-and-qcow2-images" hreflang="en">Managing qcow2 images</a></strong></u></em></p> <ul> <li><a href="/documentation/backup-virtual-machine">Backup virtual machine</a></li> <li><a href="/documentation/copy-sparse-file-across-network-using-dd-or-rsync" hreflang="en">Copy sparse file across network using dd or rsync</a></li> <li><a href="/documentation/how-mount-qcow2-image-qemu-nbd" hreflang="en">How to mount a qcow2 image with qemu-nbd</a></li> <li><a href="/documentation/reclaiming-space-qcow2-disk-image" hreflang="en">Reclaiming space on qcow2 disk image</a></li> <li><a href="/documentation/resize-qcow2-blockresize-running-vm">Resize qcow2 with blockresize on running VM</a></li> </ul> </li> </ul> </li> </ul> </nav> <ul> <li><a href="/documentation/migrate-vm-minimal-downtime">Migrate a VM with Minimal Downtime</a></li> </ul> </nav> <p> </p> </div> <div class="field field--name-field-category field--type-entity-reference field--label-above"> <div class="field--label">Category</div> <div class="field--item"><a href="/index.php/documentation" hreflang="en">Documentation</a></div> </div> <div class="fftags field field--name-field-tags field--type-entity-reference field--label-inline"> <div class="field--label">Tags</div> <div class="field--items"> <div class="field--item"><a href="/index.php/tags/qcow2" hreflang="en">qcow2</a></div> <div class="field--item"><a href="/index.php/tags/qemu-kvm" hreflang="en">qemu-kvm</a></div> </div> </div> </div> </div> Sat, 01 Sep 2018 08:11:06 +0000 Jeff 77 at https://ff.co.za https://ff.co.za/index.php/documentation/managing-kvm-and-qcow2-images#comments KVM Virsh Console Access for Debian and FreeBSD VMs https://ff.co.za/index.php/documentation/kvm-virsh-console-access-debian-and-freebsd-vms <span property="schema:name">KVM Virsh Console Access for Debian and FreeBSD VMs</span> <span rel="schema:author"><span lang="" about="/index.php/user/jeff" typeof="schema:Person" property="schema:name" datatype="">Jeff</span></span> <span property="schema:dateCreated" content="2018-06-18T12:11:56+00:00">Mon, 18/06/2018 - 14:11</span> <div class="layout layout--onecol"> <div class="layout__region layout__region--content"> <drupal-render-placeholder callback="flag.link_builder:build" arguments="0=node&amp;1=39&amp;2=bookmark" token="YuRzor9WIOotUYV2V4bznrkMFfGYtud55eXfuILRE0U"></drupal-render-placeholder><drupal-render-placeholder callback="flag.link_builder:build" arguments="0=node&amp;1=39&amp;2=like" token="dEqRm3NdgQAzonoVRZEUTDJRKjTAKiv9G6lRcTpltso"></drupal-render-placeholder> </div> </div> Mon, 18 Jun 2018 12:11:56 +0000 Jeff 39 at https://ff.co.za