blob: 81e0ec932657f7c5c24437cc567b5b04985a9a19 [file] [log] [blame]
Lukas Czernerbf0449b2011-05-18 13:36:53 +02001/*
2 * qcow2.h --- structures and function prototypes for qcow2.c to generate
3 * qcow2 formatted disk images. This format is used originally by QEMU
4 * for virtual machines, and stores the filesystem data on disk in a
5 * packed format to avoid creating sparse image files that need lots of
6 * seeking to read and write.
7 *
8 * The qcow2 format supports zlib compression, but that is not yet
9 * implemented.
10 *
11 * It is possible to directly mount a qcow2 image using qemu-nbd:
12 *
13 * [root]# modprobe nbd max_part=63
14 * [root]# qemu-nbd -c /dev/nbd0 image.img
15 * [root]# mount /dev/nbd0p1 /mnt/qemu
16 *
17 * Format details at http://people.gnome.org/~markmc/qcow-image-format.html
18 *
19 * Copyright (C) 2010 Red Hat, Inc., Lukas Czerner <lczerner@redhat.com>
20 *
21 * %Begin-Header%
22 * This file may be redistributed under the terms of the GNU Public
23 * License.
24 * %End-Header%
25 */
26
27/* Number of l2 tables in memory before writeback */
28#define L2_CACHE_PREALLOC 512
29
30
31#define QCOW_MAGIC (('Q' << 24) | ('F' << 16) | ('I' << 8) | 0xfb)
32#define QCOW_VERSION 2
33#define QCOW_OFLAG_COPIED (1LL << 63)
34#define QCOW_OFLAG_COMPRESSED (1LL << 62)
35
36#define QCOW_COMPRESSED 1
37#define QCOW_ENCRYPTED 2
38
39struct ext2_qcow2_hdr {
40 __u32 magic;
41 __u32 version;
42
43 __u64 backing_file_offset;
44 __u32 backing_file_size;
45
46 __u32 cluster_bits;
47 __u64 size;
48 __u32 crypt_method;
49
50 __u32 l1_size;
51 __u64 l1_table_offset;
52
53 __u64 refcount_table_offset;
54 __u32 refcount_table_clusters;
55
56 __u32 nb_snapshots;
57 __u64 snapshots_offset;
58};
59
60typedef struct ext2_qcow2_l2_table L2_CACHE_HEAD;
61
62struct ext2_qcow2_l2_table {
63 __u32 l1_index;
64 __u64 offset;
65 __u64 *data;
66 L2_CACHE_HEAD *next;
67};
68
69struct ext2_qcow2_l2_cache {
70 L2_CACHE_HEAD *used_head;
71 L2_CACHE_HEAD *used_tail;
72 L2_CACHE_HEAD *free_head;
73 __u32 free;
74 __u32 count;
75 __u64 next_offset;
76};
77
78struct ext2_qcow2_refcount {
79 __u64 *refcount_table;
80 __u64 refcount_table_offset;
81 __u64 refcount_block_offset;
82
83 __u32 refcount_table_clusters;
84 __u32 refcount_table_index;
85 __u32 refcount_block_index;
86
87 __u16 *refcount_block;
88};
89
90struct ext2_qcow2_image {
91 int fd;
92 struct ext2_qcow2_hdr *hdr;
93 struct ext2_qcow2_l2_cache *l2_cache;
94 struct ext2_qcow2_refcount refcount;
95 __u32 cluster_size;
96 __u32 cluster_bits;
97 __u32 l1_size;
98 __u32 l2_size;
99
100 __u64 *l1_table;
101 __u64 l2_offset;
102 __u64 l1_offset;
103 __u64 image_size;
104};
105
106/* Function prototypes */
107
108/* qcow2.c */
109
110/* Functions for converting qcow2 image into raw image */
111struct ext2_qcow2_hdr *qcow2_read_header(int);
112int qcow2_write_raw_image(int, int, struct ext2_qcow2_hdr *);
113