• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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	(1ULL << 63)
34 #define QCOW_OFLAG_COMPRESSED	(1ULL << 62)
35 
36 #define QCOW_COMPRESSED		1
37 #define QCOW_ENCRYPTED		2
38 #define QCOW_CORRUPTED		3
39 
40 struct ext2_qcow2_hdr {
41 	__u32	magic;
42 	__u32	version;
43 
44 	__u64	backing_file_offset;
45 	__u32	backing_file_size;
46 
47 	__u32	cluster_bits;
48 	__u64	size;
49 	__u32	crypt_method;
50 
51 	__u32	l1_size;
52 	__u64	l1_table_offset;
53 
54 	__u64	refcount_table_offset;
55 	__u32	refcount_table_clusters;
56 
57 	__u32	nb_snapshots;
58 	__u64	snapshots_offset;
59 };
60 
61 typedef struct ext2_qcow2_l2_table L2_CACHE_HEAD;
62 
63 struct ext2_qcow2_l2_table {
64 	__u32		l1_index;
65 	__u64		offset;
66 	__u64		*data;
67 	L2_CACHE_HEAD	*next;
68 };
69 
70 struct ext2_qcow2_l2_cache {
71 	L2_CACHE_HEAD	*used_head;
72 	L2_CACHE_HEAD	*used_tail;
73 	L2_CACHE_HEAD	*free_head;
74 	__u32		free;
75 	__u32		count;
76 	__u64		next_offset;
77 };
78 
79 struct ext2_qcow2_refcount {
80 	__u64	*refcount_table;
81 	__u64	refcount_table_offset;
82 	__u64	refcount_block_offset;
83 
84 	__u32	refcount_table_clusters;
85 	__u32	refcount_table_index;
86 	__u32	refcount_block_index;
87 
88 	__u16	*refcount_block;
89 };
90 
91 struct ext2_qcow2_image {
92 	int	fd;
93 	struct	ext2_qcow2_hdr		*hdr;
94 	struct	ext2_qcow2_l2_cache	*l2_cache;
95 	struct	ext2_qcow2_refcount	refcount;
96 	__u32	cluster_size;
97 	__u32	cluster_bits;
98 	__u32	l1_size;
99 	__u32	l2_size;
100 
101 	__u64	*l1_table;
102 	__u64	l2_offset;
103 	__u64	l1_offset;
104 	__u64	image_size;
105 };
106 
107 /* Function prototypes */
108 
109 /* qcow2.c */
110 
111 /* Functions for converting qcow2 image into raw image */
112 struct ext2_qcow2_hdr *qcow2_read_header(int);
113 int qcow2_write_raw_image(int, int, struct ext2_qcow2_hdr *);
114 
115