2 * Block driver for the Virtual Disk Image (VDI) format
4 * Copyright (c) 2009, 2012 Stefan Weil
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) version 3 or any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * http://forums.virtualbox.org/viewtopic.php?t=8046
22 * This driver supports create / read / write operations on VDI images.
24 * Todo (see also TODO in code):
26 * Some features like snapshots are still missing.
28 * Deallocation of zero-filled blocks and shrinking images are missing, too
29 * (might be added to common block layer).
31 * Allocation of blocks could be optimized (less writes to block map and
34 * Read and write of adjacents blocks could be done in one operation
35 * (current code uses one operation per block (1 MiB).
37 * The code is not thread safe (missing locks for changes in header and
38 * block table, no problem with current QEMU).
42 * Blocks (VDI documentation) correspond to clusters (QEMU).
43 * QEMU's backing files could be implemented using VDI snapshot files (TODO).
44 * VDI snapshot files may also contain the complete machine state.
45 * Maybe this machine state can be converted to QEMU PC machine snapshot data.
47 * The driver keeps a block cache (little endian entries) in memory.
48 * For the standard block size (1 MiB), a 1 TiB disk will use 4 MiB RAM,
49 * so this seems to be reasonable.
52 #include "qemu-common.h"
53 #include "block_int.h"
55 #include "migration.h"
57 #if defined(CONFIG_UUID)
58 #include <uuid/uuid.h>
60 /* TODO: move uuid emulation to some central place in QEMU. */
61 #include "sysemu.h" /* UUID_FMT */
62 typedef unsigned char uuid_t
[16];
63 void uuid_generate(uuid_t out
);
64 int uuid_is_null(const uuid_t uu
);
65 void uuid_unparse(const uuid_t uu
, char *out
);
68 /* Code configuration options. */
70 /* Enable debug messages. */
71 //~ #define CONFIG_VDI_DEBUG
73 /* Support write operations on VDI images. */
74 #define CONFIG_VDI_WRITE
76 /* Support non-standard block (cluster) size. This is untested.
77 * Maybe it will be needed for very large images.
79 //~ #define CONFIG_VDI_BLOCK_SIZE
81 /* Support static (fixed, pre-allocated) images. */
82 #define CONFIG_VDI_STATIC_IMAGE
84 /* Command line option for static images. */
85 #define BLOCK_OPT_STATIC "static"
88 #define MiB (KiB * KiB)
90 #define SECTOR_SIZE 512
91 #define DEFAULT_CLUSTER_SIZE (1 * MiB)
93 #if defined(CONFIG_VDI_DEBUG)
94 #define logout(fmt, ...) \
95 fprintf(stderr, "vdi\t%-24s" fmt, __func__, ##__VA_ARGS__)
97 #define logout(fmt, ...) ((void)0)
100 /* Image signature. */
101 #define VDI_SIGNATURE 0xbeda107f
104 #define VDI_VERSION_1_1 0x00010001
107 #define VDI_TYPE_DYNAMIC 1
108 #define VDI_TYPE_STATIC 2
110 /* Innotek / SUN images use these strings in header.text:
111 * "<<< innotek VirtualBox Disk Image >>>\n"
112 * "<<< Sun xVM VirtualBox Disk Image >>>\n"
113 * "<<< Sun VirtualBox Disk Image >>>\n"
114 * The value does not matter, so QEMU created images use a different text.
116 #define VDI_TEXT "<<< QEMU VM Virtual Disk Image >>>\n"
118 /* A never-allocated block; semantically arbitrary content. */
119 #define VDI_UNALLOCATED 0xffffffffU
121 /* A discarded (no longer allocated) block; semantically zero-filled. */
122 #define VDI_DISCARDED 0xfffffffeU
124 #define VDI_IS_ALLOCATED(X) ((X) < VDI_DISCARDED)
126 #if !defined(CONFIG_UUID)
127 void uuid_generate(uuid_t out
)
129 memset(out
, 0, sizeof(uuid_t
));
132 int uuid_is_null(const uuid_t uu
)
134 uuid_t null_uuid
= { 0 };
135 return memcmp(uu
, null_uuid
, sizeof(uuid_t
)) == 0;
138 void uuid_unparse(const uuid_t uu
, char *out
)
140 snprintf(out
, 37, UUID_FMT
,
141 uu
[0], uu
[1], uu
[2], uu
[3], uu
[4], uu
[5], uu
[6], uu
[7],
142 uu
[8], uu
[9], uu
[10], uu
[11], uu
[12], uu
[13], uu
[14], uu
[15]);
150 uint32_t header_size
;
152 uint32_t image_flags
;
153 char description
[256];
154 uint32_t offset_bmap
;
155 uint32_t offset_data
;
156 uint32_t cylinders
; /* disk geometry, unused here */
157 uint32_t heads
; /* disk geometry, unused here */
158 uint32_t sectors
; /* disk geometry, unused here */
159 uint32_t sector_size
;
163 uint32_t block_extra
; /* unused here */
164 uint32_t blocks_in_image
;
165 uint32_t blocks_allocated
;
167 uuid_t uuid_last_snap
;
174 /* The block map entries are little endian (even in memory). */
176 /* Size of block (bytes). */
178 /* Size of block (sectors). */
179 uint32_t block_sectors
;
180 /* First sector of block map. */
181 uint32_t bmap_sector
;
182 /* VDI header (converted to host endianness). */
185 Error
*migration_blocker
;
188 /* Change UUID from little endian (IPRT = VirtualBox format) to big endian
189 * format (network byte order, standard, see RFC 4122) and vice versa.
191 static void uuid_convert(uuid_t uuid
)
193 bswap32s((uint32_t *)&uuid
[0]);
194 bswap16s((uint16_t *)&uuid
[4]);
195 bswap16s((uint16_t *)&uuid
[6]);
198 static void vdi_header_to_cpu(VdiHeader
*header
)
200 le32_to_cpus(&header
->signature
);
201 le32_to_cpus(&header
->version
);
202 le32_to_cpus(&header
->header_size
);
203 le32_to_cpus(&header
->image_type
);
204 le32_to_cpus(&header
->image_flags
);
205 le32_to_cpus(&header
->offset_bmap
);
206 le32_to_cpus(&header
->offset_data
);
207 le32_to_cpus(&header
->cylinders
);
208 le32_to_cpus(&header
->heads
);
209 le32_to_cpus(&header
->sectors
);
210 le32_to_cpus(&header
->sector_size
);
211 le64_to_cpus(&header
->disk_size
);
212 le32_to_cpus(&header
->block_size
);
213 le32_to_cpus(&header
->block_extra
);
214 le32_to_cpus(&header
->blocks_in_image
);
215 le32_to_cpus(&header
->blocks_allocated
);
216 uuid_convert(header
->uuid_image
);
217 uuid_convert(header
->uuid_last_snap
);
218 uuid_convert(header
->uuid_link
);
219 uuid_convert(header
->uuid_parent
);
222 static void vdi_header_to_le(VdiHeader
*header
)
224 cpu_to_le32s(&header
->signature
);
225 cpu_to_le32s(&header
->version
);
226 cpu_to_le32s(&header
->header_size
);
227 cpu_to_le32s(&header
->image_type
);
228 cpu_to_le32s(&header
->image_flags
);
229 cpu_to_le32s(&header
->offset_bmap
);
230 cpu_to_le32s(&header
->offset_data
);
231 cpu_to_le32s(&header
->cylinders
);
232 cpu_to_le32s(&header
->heads
);
233 cpu_to_le32s(&header
->sectors
);
234 cpu_to_le32s(&header
->sector_size
);
235 cpu_to_le64s(&header
->disk_size
);
236 cpu_to_le32s(&header
->block_size
);
237 cpu_to_le32s(&header
->block_extra
);
238 cpu_to_le32s(&header
->blocks_in_image
);
239 cpu_to_le32s(&header
->blocks_allocated
);
240 cpu_to_le32s(&header
->blocks_allocated
);
241 uuid_convert(header
->uuid_image
);
242 uuid_convert(header
->uuid_last_snap
);
243 uuid_convert(header
->uuid_link
);
244 uuid_convert(header
->uuid_parent
);
247 #if defined(CONFIG_VDI_DEBUG)
248 static void vdi_header_print(VdiHeader
*header
)
251 logout("text %s", header
->text
);
252 logout("signature 0x%04x\n", header
->signature
);
253 logout("header size 0x%04x\n", header
->header_size
);
254 logout("image type 0x%04x\n", header
->image_type
);
255 logout("image flags 0x%04x\n", header
->image_flags
);
256 logout("description %s\n", header
->description
);
257 logout("offset bmap 0x%04x\n", header
->offset_bmap
);
258 logout("offset data 0x%04x\n", header
->offset_data
);
259 logout("cylinders 0x%04x\n", header
->cylinders
);
260 logout("heads 0x%04x\n", header
->heads
);
261 logout("sectors 0x%04x\n", header
->sectors
);
262 logout("sector size 0x%04x\n", header
->sector_size
);
263 logout("image size 0x%" PRIx64
" B (%" PRIu64
" MiB)\n",
264 header
->disk_size
, header
->disk_size
/ MiB
);
265 logout("block size 0x%04x\n", header
->block_size
);
266 logout("block extra 0x%04x\n", header
->block_extra
);
267 logout("blocks tot. 0x%04x\n", header
->blocks_in_image
);
268 logout("blocks all. 0x%04x\n", header
->blocks_allocated
);
269 uuid_unparse(header
->uuid_image
, uuid
);
270 logout("uuid image %s\n", uuid
);
271 uuid_unparse(header
->uuid_last_snap
, uuid
);
272 logout("uuid snap %s\n", uuid
);
273 uuid_unparse(header
->uuid_link
, uuid
);
274 logout("uuid link %s\n", uuid
);
275 uuid_unparse(header
->uuid_parent
, uuid
);
276 logout("uuid parent %s\n", uuid
);
280 static int vdi_check(BlockDriverState
*bs
, BdrvCheckResult
*res
,
283 /* TODO: additional checks possible. */
284 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
285 uint32_t blocks_allocated
= 0;
294 bmap
= g_malloc(s
->header
.blocks_in_image
* sizeof(uint32_t));
295 memset(bmap
, 0xff, s
->header
.blocks_in_image
* sizeof(uint32_t));
297 /* Check block map and value of blocks_allocated. */
298 for (block
= 0; block
< s
->header
.blocks_in_image
; block
++) {
299 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[block
]);
300 if (VDI_IS_ALLOCATED(bmap_entry
)) {
301 if (bmap_entry
< s
->header
.blocks_in_image
) {
303 if (!VDI_IS_ALLOCATED(bmap
[bmap_entry
])) {
304 bmap
[bmap_entry
] = bmap_entry
;
306 fprintf(stderr
, "ERROR: block index %" PRIu32
307 " also used by %" PRIu32
"\n", bmap
[bmap_entry
], bmap_entry
);
311 fprintf(stderr
, "ERROR: block index %" PRIu32
312 " too large, is %" PRIu32
"\n", block
, bmap_entry
);
317 if (blocks_allocated
!= s
->header
.blocks_allocated
) {
318 fprintf(stderr
, "ERROR: allocated blocks mismatch, is %" PRIu32
319 ", should be %" PRIu32
"\n",
320 blocks_allocated
, s
->header
.blocks_allocated
);
329 static int vdi_get_info(BlockDriverState
*bs
, BlockDriverInfo
*bdi
)
331 /* TODO: vdi_get_info would be needed for machine snapshots.
332 vm_state_offset is still missing. */
333 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
335 bdi
->cluster_size
= s
->block_size
;
336 bdi
->vm_state_offset
= 0;
340 static int vdi_make_empty(BlockDriverState
*bs
)
342 /* TODO: missing code. */
344 /* The return value for missing code must be 0, see block.c. */
348 static int vdi_probe(const uint8_t *buf
, int buf_size
, const char *filename
)
350 const VdiHeader
*header
= (const VdiHeader
*)buf
;
355 if (buf_size
< sizeof(*header
)) {
356 /* Header too small, no VDI. */
357 } else if (le32_to_cpu(header
->signature
) == VDI_SIGNATURE
) {
362 logout("no vdi image\n");
364 logout("%s", header
->text
);
370 static int vdi_open(BlockDriverState
*bs
, int flags
)
372 BDRVVdiState
*s
= bs
->opaque
;
378 if (bdrv_read(bs
->file
, 0, (uint8_t *)&header
, 1) < 0) {
382 vdi_header_to_cpu(&header
);
383 #if defined(CONFIG_VDI_DEBUG)
384 vdi_header_print(&header
);
387 if (header
.disk_size
% SECTOR_SIZE
!= 0) {
388 /* 'VBoxManage convertfromraw' can create images with odd disk sizes.
389 We accept them but round the disk size to the next multiple of
391 logout("odd disk size %" PRIu64
" B, round up\n", header
.disk_size
);
392 header
.disk_size
+= SECTOR_SIZE
- 1;
393 header
.disk_size
&= ~(SECTOR_SIZE
- 1);
396 if (header
.version
!= VDI_VERSION_1_1
) {
397 logout("unsupported version %u.%u\n",
398 header
.version
>> 16, header
.version
& 0xffff);
400 } else if (header
.offset_bmap
% SECTOR_SIZE
!= 0) {
401 /* We only support block maps which start on a sector boundary. */
402 logout("unsupported block map offset 0x%x B\n", header
.offset_bmap
);
404 } else if (header
.offset_data
% SECTOR_SIZE
!= 0) {
405 /* We only support data blocks which start on a sector boundary. */
406 logout("unsupported data offset 0x%x B\n", header
.offset_data
);
408 } else if (header
.sector_size
!= SECTOR_SIZE
) {
409 logout("unsupported sector size %u B\n", header
.sector_size
);
411 } else if (header
.block_size
!= 1 * MiB
) {
412 logout("unsupported block size %u B\n", header
.block_size
);
414 } else if (header
.disk_size
>
415 (uint64_t)header
.blocks_in_image
* header
.block_size
) {
416 logout("unsupported disk size %" PRIu64
" B\n", header
.disk_size
);
418 } else if (!uuid_is_null(header
.uuid_link
)) {
419 logout("link uuid != 0, unsupported\n");
421 } else if (!uuid_is_null(header
.uuid_parent
)) {
422 logout("parent uuid != 0, unsupported\n");
426 bs
->total_sectors
= header
.disk_size
/ SECTOR_SIZE
;
428 s
->block_size
= header
.block_size
;
429 s
->block_sectors
= header
.block_size
/ SECTOR_SIZE
;
430 s
->bmap_sector
= header
.offset_bmap
/ SECTOR_SIZE
;
433 bmap_size
= header
.blocks_in_image
* sizeof(uint32_t);
434 bmap_size
= (bmap_size
+ SECTOR_SIZE
- 1) / SECTOR_SIZE
;
436 s
->bmap
= g_malloc(bmap_size
* SECTOR_SIZE
);
438 if (bdrv_read(bs
->file
, s
->bmap_sector
, (uint8_t *)s
->bmap
, bmap_size
) < 0) {
442 /* Disable migration when vdi images are used */
443 error_set(&s
->migration_blocker
,
444 QERR_BLOCK_FORMAT_FEATURE_NOT_SUPPORTED
,
445 "vdi", bs
->device_name
, "live migration");
446 migrate_add_blocker(s
->migration_blocker
);
457 static int vdi_reopen_prepare(BDRVReopenState
*state
,
458 BlockReopenQueue
*queue
, Error
**errp
)
463 static int coroutine_fn
vdi_co_is_allocated(BlockDriverState
*bs
,
464 int64_t sector_num
, int nb_sectors
, int *pnum
)
466 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
467 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
468 size_t bmap_index
= sector_num
/ s
->block_sectors
;
469 size_t sector_in_block
= sector_num
% s
->block_sectors
;
470 int n_sectors
= s
->block_sectors
- sector_in_block
;
471 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
472 logout("%p, %" PRId64
", %d, %p\n", bs
, sector_num
, nb_sectors
, pnum
);
473 if (n_sectors
> nb_sectors
) {
474 n_sectors
= nb_sectors
;
477 return VDI_IS_ALLOCATED(bmap_entry
);
480 static int vdi_co_read(BlockDriverState
*bs
,
481 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
483 BDRVVdiState
*s
= bs
->opaque
;
485 uint32_t block_index
;
486 uint32_t sector_in_block
;
492 while (ret
>= 0 && nb_sectors
> 0) {
493 block_index
= sector_num
/ s
->block_sectors
;
494 sector_in_block
= sector_num
% s
->block_sectors
;
495 n_sectors
= s
->block_sectors
- sector_in_block
;
496 if (n_sectors
> nb_sectors
) {
497 n_sectors
= nb_sectors
;
500 logout("will read %u sectors starting at sector %" PRIu64
"\n",
501 n_sectors
, sector_num
);
503 /* prepare next AIO request */
504 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
505 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
506 /* Block not allocated, return zeros, no need to wait. */
507 memset(buf
, 0, n_sectors
* SECTOR_SIZE
);
510 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
511 (uint64_t)bmap_entry
* s
->block_sectors
+
513 ret
= bdrv_read(bs
->file
, offset
, buf
, n_sectors
);
515 logout("%u sectors read\n", n_sectors
);
517 nb_sectors
-= n_sectors
;
518 sector_num
+= n_sectors
;
519 buf
+= n_sectors
* SECTOR_SIZE
;
525 static int vdi_co_write(BlockDriverState
*bs
,
526 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
)
528 BDRVVdiState
*s
= bs
->opaque
;
530 uint32_t block_index
;
531 uint32_t sector_in_block
;
533 uint32_t bmap_first
= VDI_UNALLOCATED
;
534 uint32_t bmap_last
= VDI_UNALLOCATED
;
535 uint8_t *block
= NULL
;
540 while (ret
>= 0 && nb_sectors
> 0) {
541 block_index
= sector_num
/ s
->block_sectors
;
542 sector_in_block
= sector_num
% s
->block_sectors
;
543 n_sectors
= s
->block_sectors
- sector_in_block
;
544 if (n_sectors
> nb_sectors
) {
545 n_sectors
= nb_sectors
;
548 logout("will write %u sectors starting at sector %" PRIu64
"\n",
549 n_sectors
, sector_num
);
551 /* prepare next AIO request */
552 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
553 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
554 /* Allocate new block and write to it. */
556 bmap_entry
= s
->header
.blocks_allocated
;
557 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
558 s
->header
.blocks_allocated
++;
559 offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
560 (uint64_t)bmap_entry
* s
->block_sectors
;
562 block
= g_malloc(s
->block_size
);
563 bmap_first
= block_index
;
565 bmap_last
= block_index
;
566 /* Copy data to be written to new block and zero unused parts. */
567 memset(block
, 0, sector_in_block
* SECTOR_SIZE
);
568 memcpy(block
+ sector_in_block
* SECTOR_SIZE
,
569 buf
, n_sectors
* SECTOR_SIZE
);
570 memset(block
+ (sector_in_block
+ n_sectors
) * SECTOR_SIZE
, 0,
571 (s
->block_sectors
- n_sectors
- sector_in_block
) * SECTOR_SIZE
);
572 ret
= bdrv_write(bs
->file
, offset
, block
, s
->block_sectors
);
574 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
575 (uint64_t)bmap_entry
* s
->block_sectors
+
577 ret
= bdrv_write(bs
->file
, offset
, buf
, n_sectors
);
580 nb_sectors
-= n_sectors
;
581 sector_num
+= n_sectors
;
582 buf
+= n_sectors
* SECTOR_SIZE
;
584 logout("%u sectors written\n", n_sectors
);
587 logout("finished data write\n");
593 /* One or more new blocks were allocated. */
594 VdiHeader
*header
= (VdiHeader
*) block
;
598 logout("now writing modified header\n");
599 assert(VDI_IS_ALLOCATED(bmap_first
));
601 vdi_header_to_le(header
);
602 ret
= bdrv_write(bs
->file
, 0, block
, 1);
610 logout("now writing modified block map entry %u...%u\n",
611 bmap_first
, bmap_last
);
612 /* Write modified sectors from block map. */
613 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
614 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
615 n_sectors
= bmap_last
- bmap_first
+ 1;
616 offset
= s
->bmap_sector
+ bmap_first
;
617 base
= ((uint8_t *)&s
->bmap
[0]) + bmap_first
* SECTOR_SIZE
;
618 logout("will write %u block map sectors starting from entry %u\n",
619 n_sectors
, bmap_first
);
620 ret
= bdrv_write(bs
->file
, offset
, base
, n_sectors
);
626 static int vdi_create(const char *filename
, QEMUOptionParameter
*options
)
632 size_t block_size
= DEFAULT_CLUSTER_SIZE
;
633 uint32_t image_type
= VDI_TYPE_DYNAMIC
;
640 /* Read out options. */
641 while (options
&& options
->name
) {
642 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
643 bytes
= options
->value
.n
;
644 #if defined(CONFIG_VDI_BLOCK_SIZE)
645 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
646 if (options
->value
.n
) {
647 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
648 block_size
= options
->value
.n
;
651 #if defined(CONFIG_VDI_STATIC_IMAGE)
652 } else if (!strcmp(options
->name
, BLOCK_OPT_STATIC
)) {
653 if (options
->value
.n
) {
654 image_type
= VDI_TYPE_STATIC
;
661 fd
= qemu_open(filename
,
662 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
668 /* We need enough blocks to store the given disk size,
669 so always round up. */
670 blocks
= (bytes
+ block_size
- 1) / block_size
;
672 bmap_size
= blocks
* sizeof(uint32_t);
673 bmap_size
= ((bmap_size
+ SECTOR_SIZE
- 1) & ~(SECTOR_SIZE
-1));
675 memset(&header
, 0, sizeof(header
));
676 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
677 header
.signature
= VDI_SIGNATURE
;
678 header
.version
= VDI_VERSION_1_1
;
679 header
.header_size
= 0x180;
680 header
.image_type
= image_type
;
681 header
.offset_bmap
= 0x200;
682 header
.offset_data
= 0x200 + bmap_size
;
683 header
.sector_size
= SECTOR_SIZE
;
684 header
.disk_size
= bytes
;
685 header
.block_size
= block_size
;
686 header
.blocks_in_image
= blocks
;
687 if (image_type
== VDI_TYPE_STATIC
) {
688 header
.blocks_allocated
= blocks
;
690 uuid_generate(header
.uuid_image
);
691 uuid_generate(header
.uuid_last_snap
);
692 /* There is no need to set header.uuid_link or header.uuid_parent here. */
693 #if defined(CONFIG_VDI_DEBUG)
694 vdi_header_print(&header
);
696 vdi_header_to_le(&header
);
697 if (write(fd
, &header
, sizeof(header
)) < 0) {
702 uint32_t *bmap
= g_malloc0(bmap_size
);
703 for (i
= 0; i
< blocks
; i
++) {
704 if (image_type
== VDI_TYPE_STATIC
) {
707 bmap
[i
] = VDI_UNALLOCATED
;
710 if (write(fd
, bmap
, bmap_size
) < 0) {
716 if (image_type
== VDI_TYPE_STATIC
) {
717 if (ftruncate(fd
, sizeof(header
) + bmap_size
+ blocks
* block_size
)) {
729 static void vdi_close(BlockDriverState
*bs
)
731 BDRVVdiState
*s
= bs
->opaque
;
735 migrate_del_blocker(s
->migration_blocker
);
736 error_free(s
->migration_blocker
);
739 static QEMUOptionParameter vdi_create_options
[] = {
741 .name
= BLOCK_OPT_SIZE
,
743 .help
= "Virtual disk size"
745 #if defined(CONFIG_VDI_BLOCK_SIZE)
747 .name
= BLOCK_OPT_CLUSTER_SIZE
,
749 .help
= "VDI cluster (block) size",
750 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
753 #if defined(CONFIG_VDI_STATIC_IMAGE)
755 .name
= BLOCK_OPT_STATIC
,
757 .help
= "VDI static (pre-allocated) image"
760 /* TODO: An additional option to set UUID values might be useful. */
764 static BlockDriver bdrv_vdi
= {
765 .format_name
= "vdi",
766 .instance_size
= sizeof(BDRVVdiState
),
767 .bdrv_probe
= vdi_probe
,
768 .bdrv_open
= vdi_open
,
769 .bdrv_close
= vdi_close
,
770 .bdrv_reopen_prepare
= vdi_reopen_prepare
,
771 .bdrv_create
= vdi_create
,
772 .bdrv_co_is_allocated
= vdi_co_is_allocated
,
773 .bdrv_make_empty
= vdi_make_empty
,
775 .bdrv_read
= vdi_co_read
,
776 #if defined(CONFIG_VDI_WRITE)
777 .bdrv_write
= vdi_co_write
,
780 .bdrv_get_info
= vdi_get_info
,
782 .create_options
= vdi_create_options
,
783 .bdrv_check
= vdi_check
,
786 static void bdrv_vdi_init(void)
789 bdrv_register(&bdrv_vdi
);
792 block_init(bdrv_vdi_init
);