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 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 https://ff.co.za/index.php/documentation/backup-virtual-machine#comments KVM Bridged Networking https://ff.co.za/index.php/documentation/kvm-bridged-networking <span property="schema:name">KVM Bridged Networking</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:14:07+00:00">Mon, 18/06/2018 - 14:14</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=38&amp;2=bookmark" token="v9TvmeJeforxvMovvho9sGXicvsfK_lwgWkr_4bMWec"></drupal-render-placeholder><drupal-render-placeholder callback="flag.link_builder:build" arguments="0=node&amp;1=38&amp;2=like" token="AkREE3pa87mI3OgWE-ygFZgPr1X08vskNmx61vWsr6o"></drupal-render-placeholder> </div> </div> Mon, 18 Jun 2018 12:14:07 +0000 Jeff 38 at https://ff.co.za https://ff.co.za/index.php/documentation/kvm-bridged-networking#comments