Skip to contentSkip to conversation

kvm tools: Add read-only support for QCOW2 images

PATCH In review Pekka Enberg 1 participant 2 messages Apr 19, 2011 – Apr 20, 2011 1 message not in the archive KVM

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kvm tools: Add read-only support for QCOW2 images

This patch extends the QCOW1 format to also support QCOW2 images as specified by the following document:

http://people.gnome.org/~markmc/qcow-image-format.html

Cc Asias He <[email protected]>
Cc Cyrill Gorcunov <[email protected]>
Cc Prasad Joshi <[email protected]>
Cc Sasha Levin <[email protected]>
Cc Ingo Molnar <[email protected]>
---
 tools/kvm/include/kvm/qcow.h |   42 ++++++++++-
 tools/kvm/qcow.c             |  177 +++++++++++++++++++++++++++++++++---------
 2 files changed, 181 insertions(+), 38 deletions(-)
tools/kvm/include/kvm/qcow.h +40 -2
index 4be2597..afd776d 100644
@@ -4,9 +4,17 @@
 #include <linux/types.h>
 
 #define QCOW_MAGIC		(('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
+
 #define QCOW1_VERSION		1
+#define QCOW2_VERSION		2
+
+#define QCOW1_OFLAG_COMPRESSED	(1LL << 63)
+
+#define QCOW1_OFLAG_MASK	QCOW1_OFLAG_COMPRESSED
 
-#define QCOW_OFLAG_COMPRESSED	(1LL << 63)
+#define QCOW2_OFLAG_COPIED	(1LL << 63)
+#define QCOW2_OFLAG_COMPRESSED	(1LL << 62)
+#define QCOW2_OFLAG_MASK	(QCOW2_OFLAG_COPIED|QCOW2_OFLAG_COMPRESSED)
 
 struct qcow_table {
 	u32			table_size;
@@ -19,7 +27,16 @@ struct qcow {
 	int			fd;
 };
 
-struct qcow1_header {
+struct qcow_header {
+	u64			size; /* in bytes */
+	u64			l1_table_offset;
+	u32			l1_size;
+	u8			cluster_bits;
+	u8			l2_bits;
+	uint64_t		oflag_mask;
+};
+
+struct qcow1_header_disk {
 	u32			magic;
 	u32			version;
 
@@ -36,6 +53,27 @@ struct qcow1_header {
 	u64			l1_table_offset;
 };
 
+struct qcow2_header_disk {
+	u32			magic;
+	u32			version;
+
+	u64			backing_file_offset;
+	u32			backing_file_size;
+
+	u32			cluster_bits;
+	u64			size; /* in bytes */
+	u32			crypt_method;
+
+	u32			l1_size;
+	u64			l1_table_offset;
+
+	u64			refcount_table_offset;
+	u32			refcount_table_clusters;
+
+	u32			nb_snapshots;
+	u64			snapshots_offset;
+};
+
 struct disk_image *qcow_probe(int fd);
 
 #endif /* KVM__QCOW_H */
tools/kvm/qcow.c +141 -36

263 lines not on this page · raw message

Signature · 1 line
1.7.0.4
#2 lore raw
In reply to a message not in the archive
Quoted 83 lines · Prasad Joshi

On Tue, Apr 19, 2011 at 10:07 PM, Pekka Enberg <[email protected]> wrote:

      This patch extends the QCOW1 format to also support QCOW2 images as specified
      by the following document:

       http://people.gnome.org/~markmc/qcow-image-format.html

      Cc: Asias He <[email protected]>
      Cc: Cyrill Gorcunov <[email protected]>
      Cc: Prasad Joshi <[email protected]>
      Cc: Sasha Levin <[email protected]>
      Cc: Ingo Molnar <[email protected]>
      Signed-off-by: Pekka Enberg <[email protected]>
      ---
       tools/kvm/include/kvm/qcow.h |   42 ++++++++++-
       tools/kvm/qcow.c             |  177 +++++++++++++++++++++++++++++++++---------
       2 files changed, 181 insertions(+), 38 deletions(-)

      diff --git a/tools/kvm/include/kvm/qcow.h b/tools/kvm/include/kvm/qcow.h
      index 4be2597..afd776d 100644
      --- a/tools/kvm/include/kvm/qcow.h
      +++ b/tools/kvm/include/kvm/qcow.h
      @@ -4,9 +4,17 @@
       #include <linux/types.h>

       #define QCOW_MAGIC             (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
      +
       #define QCOW1_VERSION          1
      +#define QCOW2_VERSION          2
      +
      +#define QCOW1_OFLAG_COMPRESSED (1LL << 63)
      +
      +#define QCOW1_OFLAG_MASK       QCOW1_OFLAG_COMPRESSED

      -#define QCOW_OFLAG_COMPRESSED  (1LL << 63)
      +#define QCOW2_OFLAG_COPIED     (1LL << 63)
      +#define QCOW2_OFLAG_COMPRESSED (1LL << 62)
      +#define QCOW2_OFLAG_MASK       (QCOW2_OFLAG_COPIED|QCOW2_OFLAG_COMPRESSED)

       struct qcow_table {
             u32                     table_size;
      @@ -19,7 +27,16 @@ struct qcow {
             int                     fd;
       };

      -struct qcow1_header {
      +struct qcow_header {
      +       u64                     size; /* in bytes */
      +       u64                     l1_table_offset;
      +       u32                     l1_size;
      +       u8                      cluster_bits;
      +       u8                      l2_bits;
      +       uint64_t                oflag_mask;
      +};
      +
      +struct qcow1_header_disk {
             u32                     magic;
             u32                     version;

      @@ -36,6 +53,27 @@ struct qcow1_header {
             u64                     l1_table_offset;
       };

      +struct qcow2_header_disk {
      +       u32                     magic;
      +       u32                     version;
      +
      +       u64                     backing_file_offset;
      +       u32                     backing_file_size;
      +
      +       u32                     cluster_bits;
      +       u64                     size; /* in bytes */
      +       u32                     crypt_method;
      +
      +       u32                     l1_size;
      +       u64                     l1_table_offset;
      +
      +       u64                     refcount_table_offset;
      +       u32                     refcount_table_clusters;
      +
      +       u32                     nb_snapshots;
      +       u64                     snapshots_offset;
      +};
Quoted 1 line

IMHO, as we start adding other features of QCOW, the two structures qcow2_header_disk and qcow_header might eventually become the same.

No, the point of 'struct qcow2_header_disk' is to map to the on-disk representation. 'struct qcow_header' is the in-memory version of the data.

Quoted 4 lines

+       disk_image = disk_image__new(fd, h->size, &qcow1_disk_ops);

qcow1_disk_ops can be changed to qcow_disk_ops.

Sure, there's more qcow1 prefixes that need fixing now as well.