Blame SOURCES/libvirt-kbase-Add-document-outlining-internals-of-incremental-backup-in-qemu.patch

fbe740
From 02714666a525ea4dd8756f66fae28163fb685d05 Mon Sep 17 00:00:00 2001
fbe740
Message-Id: <02714666a525ea4dd8756f66fae28163fb685d05@dist-git>
fbe740
From: Peter Krempa <pkrempa@redhat.com>
fbe740
Date: Tue, 23 Jun 2020 12:24:06 +0200
fbe740
Subject: [PATCH] kbase: Add document outlining internals of incremental backup
fbe740
 in qemu
fbe740
MIME-Version: 1.0
fbe740
Content-Type: text/plain; charset=UTF-8
fbe740
Content-Transfer-Encoding: 8bit
fbe740
fbe740
Outline the basics and how to integrate with externally created
fbe740
overlays. Other topics will continue later.
fbe740
fbe740
Signed-off-by: Peter Krempa <pkrempa@redhat.com>
fbe740
Reviewed-by: Eric Blake <eblake@redhat.com>
fbe740
(cherry picked from commit da5e5a1e154836abe83077cf477c104b8f98b1d2)
fbe740
https://bugzilla.redhat.com/show_bug.cgi?id=1804593
fbe740
fbe740
Conflicts: docs/kbase.html.in: real time kvm article not backported
fbe740
Message-Id: <e0fea1e270856b642c34827eb3af1c0b01afd510.1592906423.git.pkrempa@redhat.com>
fbe740
Reviewed-by: Ján Tomko <jtomko@redhat.com>
fbe740
---
fbe740
 docs/kbase.html.in                        |   3 +
fbe740
 docs/kbase/incrementalbackupinternals.rst | 217 ++++++++++++++++++++++
fbe740
 2 files changed, 220 insertions(+)
fbe740
 create mode 100644 docs/kbase/incrementalbackupinternals.rst
fbe740
fbe740
diff --git a/docs/kbase.html.in b/docs/kbase.html.in
fbe740
index 7d6caf3cb1..f2975960f6 100644
fbe740
--- a/docs/kbase.html.in
fbe740
+++ b/docs/kbase.html.in
fbe740
@@ -32,6 +32,9 @@
fbe740
 
fbe740
         
Virtio-FS
fbe740
         
Share a filesystem between the guest and the host
fbe740
+
fbe740
+        
Incremental backup internals
fbe740
+        
Incremental backup implementation details relevant for users
fbe740
       
fbe740
     
fbe740
 
fbe740
diff --git a/docs/kbase/incrementalbackupinternals.rst b/docs/kbase/incrementalbackupinternals.rst
fbe740
new file mode 100644
fbe740
index 0000000000..0c4b4f7486
fbe740
--- /dev/null
fbe740
+++ b/docs/kbase/incrementalbackupinternals.rst
fbe740
@@ -0,0 +1,217 @@
fbe740
+================================================
fbe740
+Internals of incremental backup handling in qemu
fbe740
+================================================
fbe740
+
fbe740
+.. contents::
fbe740
+
fbe740
+Libvirt's implementation of incremental backups in the ``qemu`` driver uses
fbe740
+qemu's ``block-dirty-bitmaps`` under the hood to track the guest visible disk
fbe740
+state changes corresponding to the points in time described by a libvirt
fbe740
+checkpoint.
fbe740
+
fbe740
+There are some semantica implications with how libvirt creates and manages the
fbe740
+bitmaps which de-facto become API as they are written into the disk images, and
fbe740
+this document will try to summarize them.
fbe740
+
fbe740
+Glossary
fbe740
+========
fbe740
+
fbe740
+See the knowledge base article on
fbe740
+`domain state capture <https://libvirt.org/kbase/domainstatecapture.html>`_ for
fbe740
+a deeper explanation of some of the concepts.
fbe740
+
fbe740
+Checkpoint
fbe740
+
fbe740
+    A libvirt object which represents a named point in time of the life of the
fbe740
+    vm where libvirt tracks writes the VM has done, thereby allowing a backup of
fbe740
+    only the blocks which changed. Note that state of the VM memory is _not_
fbe740
+    captured.
fbe740
+
fbe740
+    A checkpoint can be created either explicitly via the corresponding API
fbe740
+    (although this isn't very useful on its own), or simultaneously with an
fbe740
+    incremental or full backup of the VM using the ``virDomainBackupBegin`` API
fbe740
+    which allows a next backup to only copy the differences.
fbe740
+
fbe740
+Backup
fbe740
+
fbe740
+    A copy of either all blocks of selected disks (full backup) or blocks changed
fbe740
+    since a checkpoint (incremental backup) at the time the backup job was
fbe740
+    started. (Blocks modified while the backup job is running are not part of the
fbe740
+    backup!)
fbe740
+
fbe740
+Snapshot
fbe740
+
fbe740
+    Similarly to a checkpoint it's a point in time in the lifecycle of the VM
fbe740
+    but the state of the VM including memory is captured at that point allowing
fbe740
+    returning to the state later.
fbe740
+
fbe740
+Blockjob
fbe740
+
fbe740
+    A long running job which modifies the shape and/or location of the disk
fbe740
+    backing chain (images storing the disk contents). Libvirt supports
fbe740
+    ``block pull`` where data is moved up the chain towards the active layer,
fbe740
+    ``block commit`` where data is moved down the chain towards the base/oldest
fbe740
+    image. These blockjobs always remove images from the backing chain. Lastly
fbe740
+    ``block copy`` where image is moved to a different location (and possibly
fbe740
+    collapsed moving all of the data into the new location into the one image).
fbe740
+
fbe740
+block-dirty-bitmap (bitmap)
fbe740
+
fbe740
+    A data structure in qemu tracking which blocks were written by the guest
fbe740
+    OS since the bitmap was created.
fbe740
+
fbe740
+Relationships of bitmaps, checkpoints and VM disks
fbe740
+==================================================
fbe740
+
fbe740
+When a checkpoint is created libvirt creates a block-dirty-bitmap for every
fbe740
+configured VM disk named the same way as the chcheckpoint. The bitmap is
fbe740
+actively recording which blocks were changed by the guest OS from that point on.
fbe740
+Other bitmaps are not impacted by any way as they are self-contained:
fbe740
+
fbe740
+::
fbe740
+
fbe740
+ +----------------+       +----------------+
fbe740
+ | disk: vda      |       | disk: vdb      |
fbe740
+ +--------+-------+       +--------+-------+
fbe740
+          |                        |
fbe740
+ +--------v-------+       +--------v-------+
fbe740
+ | vda-1.qcow2    |       | vdb-1.qcow2    |
fbe740
+ |                |       |                |
fbe740
+ | bitmaps: chk-a |       | bitmaps: chk-a |
fbe740
+ |          chk-b |       |          chk-b |
fbe740
+ |                |       |                |
fbe740
+ +----------------+       +----------------+
fbe740
+
fbe740
+Bitmaps are created at the same time to track changes to all disks in sync and
fbe740
+are active and persisted in the QCOW2 image. Other formats currently don't
fbe740
+support this feature.
fbe740
+
fbe740
+Modification of bitmaps outside of libvirt is not recommended, but when adhering
fbe740
+to the same semantics which the document will describe it should be safe to do
fbe740
+so, even if we obviously can't guarantee that.
fbe740
+
fbe740
+
fbe740
+Integration with external snapshots
fbe740
+===================================
fbe740
+
fbe740
+Handling of bitmaps
fbe740
+-------------------
fbe740
+
fbe740
+Creating an external snapshot involves adding a new layer to the backing chain
fbe740
+on top of the previous chain. In this step there are no new bitmaps created by
fbe740
+default, which would mean that backups become impossible after this step.
fbe740
+
fbe740
+To prevent this from happening we need to re-create the active bitmaps in the
fbe740
+new top/active layer of the backing chain which allows us to continue tracking
fbe740
+the changes with same granularity as before and also allows libvirt to stitch
fbe740
+together all the corresponding bitmaps to do a backup across snapshots.
fbe740
+
fbe740
+After taking a snapshot of the ``vda`` disk from the example above placed into
fbe740
+``vda-2.qcow2`` the following topology will be created:
fbe740
+
fbe740
+::
fbe740
+
fbe740
+   +----------------+
fbe740
+   | disk: vda      |
fbe740
+   +-------+--------+
fbe740
+           |
fbe740
+   +-------v--------+    +----------------+
fbe740
+   | vda-2.qcow2    |    | vda-1.qcow2    |
fbe740
+   |                |    |                |
fbe740
+   | bitmaps: chk-a +----> bitmaps: chk-a |
fbe740
+   |          chk-b |    |          chk-b |
fbe740
+   |                |    |                |
fbe740
+   +----------------+    +----------------+
fbe740
+
fbe740
+Checking bitmap health
fbe740
+----------------------
fbe740
+
fbe740
+QEMU optimizes disk writes by only updating the bitmaps in certain cases. This
fbe740
+also can cause problems in cases when e.g. QEMU crashes.
fbe740
+
fbe740
+For a chain of corresponding bitmaps in a backing chain to be considered valid
fbe740
+and eligible for use with ``virDomainBackupBegin`` it must conform to the
fbe740
+following rules:
fbe740
+
fbe740
+1) Top image must contain the bitmap
fbe740
+2) If any of the backing images in the chain contain the bitmap too, all
fbe740
+   contiguous images must have the bitmap (no gaps)
fbe740
+3) all of the above bitmaps must be marked as active
fbe740
+   (``auto`` flag in ``qemu-img`` output, ``recording`` in qemu)
fbe740
+4) none of the above bitmaps can be inconsistent
fbe740
+   (``in-use`` flag in ``qemu-img`` provided that it's not used on image which
fbe740
+   is currently in use by a qemu instance, or ``inconsistent`` in qemu)
fbe740
+
fbe740
+::
fbe740
+
fbe740
+ # check that image has bitmaps
fbe740
+  $ qemu-img info vda-1.qcow2
fbe740
+   image: vda-1.qcow2
fbe740
+   file format: qcow2
fbe740
+   virtual size: 100 MiB (104857600 bytes)
fbe740
+   disk size: 220 KiB
fbe740
+   cluster_size: 65536
fbe740
+   Format specific information:
fbe740
+       compat: 1.1
fbe740
+       compression type: zlib
fbe740
+       lazy refcounts: false
fbe740
+       bitmaps:
fbe740
+           [0]:
fbe740
+               flags:
fbe740
+                   [0]: in-use
fbe740
+                   [1]: auto
fbe740
+               name: chk-a
fbe740
+               granularity: 65536
fbe740
+           [1]:
fbe740
+               flags:
fbe740
+                   [0]: auto
fbe740
+               name: chk-b
fbe740
+               granularity: 65536
fbe740
+       refcount bits: 16
fbe740
+       corrupt: false
fbe740
+
fbe740
+(See also the ``qemuBlockBitmapChainIsValid`` helper method in
fbe740
+``src/qemu/qemu_block.c``)
fbe740
+
fbe740
+Creating external snapshots manually
fbe740
+--------------------------------------
fbe740
+
fbe740
+To create the same topology outside of libvirt (e.g when doing snapshots offline)
fbe740
+a new ``qemu-img`` which supports the ``bitmap`` subcommand is recommended. The
fbe740
+following algorithm then ensures that the new image after snapshot will work
fbe740
+with backups (note that ``jq`` is a JSON processor):
fbe740
+
fbe740
+::
fbe740
+
fbe740
+  #!/bin/bash
fbe740
+
fbe740
+  # arguments
fbe740
+  SNAP_IMG="vda-2.qcow2"
fbe740
+  BACKING_IMG="vda-1.qcow2"
fbe740
+
fbe740
+  # constants - snapshots and bitmaps work only with qcow2
fbe740
+  SNAP_FMT="qcow2"
fbe740
+  BACKING_IMG_FMT="qcow2"
fbe740
+
fbe740
+  # create snapshot overlay
fbe740
+  qemu-img create -f "$SNAP_FMT" -F "$BACKING_IMG_FMT" -b "$BACKING_IMG" "$SNAP_IMG"
fbe740
+
fbe740
+  BACKING_IMG_INFO=$(qemu-img info --output=json -f "$BACKING_IMG_FMT" "$BACKING_IMG")
fbe740
+  BACKING_BITMAPS=$(jq '."format-specific".data.bitmaps' <<< "$BACKING_IMG_INFO")
fbe740
+
fbe740
+  if [ "x$BACKING_BITMAPS" = "xnull" ]; then
fbe740
+      exit 0
fbe740
+  fi
fbe740
+
fbe740
+  for BACKING_BITMAP_ in $(jq -c '.[]' <<< "$BACKING_BITMAPS"); do
fbe740
+      BITMAP_FLAGS=$(jq -c -r '.flags[]' <<< "$BACKING_BITMAP_")
fbe740
+      BITMAP_NAME=$(jq -r '.name' <<< "$BACKING_BITMAP_")
fbe740
+
fbe740
+      if grep 'in-use' <<< "$BITMAP_FLAGS" ||
fbe740
+         grep -v 'auto' <<< "$BITMAP_FLAGS"; then
fbe740
+         continue
fbe740
+      fi
fbe740
+
fbe740
+      qemu-img bitmap -f "$SNAP_FMT" "$SNAP_IMG" --add "$BITMAP_NAME"
fbe740
+
fbe740
+  done
fbe740
-- 
fbe740
2.27.0
fbe740