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 coroutine_fn
vdi_co_is_allocated(BlockDriverState
*bs
,
458 int64_t sector_num
, int nb_sectors
, int *pnum
)
460 /* TODO: Check for too large sector_num (in bdrv_is_allocated or here). */
461 BDRVVdiState
*s
= (BDRVVdiState
*)bs
->opaque
;
462 size_t bmap_index
= sector_num
/ s
->block_sectors
;
463 size_t sector_in_block
= sector_num
% s
->block_sectors
;
464 int n_sectors
= s
->block_sectors
- sector_in_block
;
465 uint32_t bmap_entry
= le32_to_cpu(s
->bmap
[bmap_index
]);
466 logout("%p, %" PRId64
", %d, %p\n", bs
, sector_num
, nb_sectors
, pnum
);
467 if (n_sectors
> nb_sectors
) {
468 n_sectors
= nb_sectors
;
471 return VDI_IS_ALLOCATED(bmap_entry
);
474 static int vdi_co_read(BlockDriverState
*bs
,
475 int64_t sector_num
, uint8_t *buf
, int nb_sectors
)
477 BDRVVdiState
*s
= bs
->opaque
;
479 uint32_t block_index
;
480 uint32_t sector_in_block
;
486 while (ret
>= 0 && nb_sectors
> 0) {
487 block_index
= sector_num
/ s
->block_sectors
;
488 sector_in_block
= sector_num
% s
->block_sectors
;
489 n_sectors
= s
->block_sectors
- sector_in_block
;
490 if (n_sectors
> nb_sectors
) {
491 n_sectors
= nb_sectors
;
494 logout("will read %u sectors starting at sector %" PRIu64
"\n",
495 n_sectors
, sector_num
);
497 /* prepare next AIO request */
498 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
499 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
500 /* Block not allocated, return zeros, no need to wait. */
501 memset(buf
, 0, n_sectors
* SECTOR_SIZE
);
504 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
505 (uint64_t)bmap_entry
* s
->block_sectors
+
507 ret
= bdrv_read(bs
->file
, offset
, buf
, n_sectors
);
509 logout("%u sectors read\n", n_sectors
);
511 nb_sectors
-= n_sectors
;
512 sector_num
+= n_sectors
;
513 buf
+= n_sectors
* SECTOR_SIZE
;
519 static int vdi_co_write(BlockDriverState
*bs
,
520 int64_t sector_num
, const uint8_t *buf
, int nb_sectors
)
522 BDRVVdiState
*s
= bs
->opaque
;
524 uint32_t block_index
;
525 uint32_t sector_in_block
;
527 uint32_t bmap_first
= VDI_UNALLOCATED
;
528 uint32_t bmap_last
= VDI_UNALLOCATED
;
529 uint8_t *block
= NULL
;
534 while (ret
>= 0 && nb_sectors
> 0) {
535 block_index
= sector_num
/ s
->block_sectors
;
536 sector_in_block
= sector_num
% s
->block_sectors
;
537 n_sectors
= s
->block_sectors
- sector_in_block
;
538 if (n_sectors
> nb_sectors
) {
539 n_sectors
= nb_sectors
;
542 logout("will write %u sectors starting at sector %" PRIu64
"\n",
543 n_sectors
, sector_num
);
545 /* prepare next AIO request */
546 bmap_entry
= le32_to_cpu(s
->bmap
[block_index
]);
547 if (!VDI_IS_ALLOCATED(bmap_entry
)) {
548 /* Allocate new block and write to it. */
550 bmap_entry
= s
->header
.blocks_allocated
;
551 s
->bmap
[block_index
] = cpu_to_le32(bmap_entry
);
552 s
->header
.blocks_allocated
++;
553 offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
554 (uint64_t)bmap_entry
* s
->block_sectors
;
556 block
= g_malloc(s
->block_size
);
557 bmap_first
= block_index
;
559 bmap_last
= block_index
;
560 /* Copy data to be written to new block and zero unused parts. */
561 memset(block
, 0, sector_in_block
* SECTOR_SIZE
);
562 memcpy(block
+ sector_in_block
* SECTOR_SIZE
,
563 buf
, n_sectors
* SECTOR_SIZE
);
564 memset(block
+ (sector_in_block
+ n_sectors
) * SECTOR_SIZE
, 0,
565 (s
->block_sectors
- n_sectors
- sector_in_block
) * SECTOR_SIZE
);
566 ret
= bdrv_write(bs
->file
, offset
, block
, s
->block_sectors
);
568 uint64_t offset
= s
->header
.offset_data
/ SECTOR_SIZE
+
569 (uint64_t)bmap_entry
* s
->block_sectors
+
571 ret
= bdrv_write(bs
->file
, offset
, buf
, n_sectors
);
574 nb_sectors
-= n_sectors
;
575 sector_num
+= n_sectors
;
576 buf
+= n_sectors
* SECTOR_SIZE
;
578 logout("%u sectors written\n", n_sectors
);
581 logout("finished data write\n");
587 /* One or more new blocks were allocated. */
588 VdiHeader
*header
= (VdiHeader
*) block
;
592 logout("now writing modified header\n");
593 assert(VDI_IS_ALLOCATED(bmap_first
));
595 vdi_header_to_le(header
);
596 ret
= bdrv_write(bs
->file
, 0, block
, 1);
604 logout("now writing modified block map entry %u...%u\n",
605 bmap_first
, bmap_last
);
606 /* Write modified sectors from block map. */
607 bmap_first
/= (SECTOR_SIZE
/ sizeof(uint32_t));
608 bmap_last
/= (SECTOR_SIZE
/ sizeof(uint32_t));
609 n_sectors
= bmap_last
- bmap_first
+ 1;
610 offset
= s
->bmap_sector
+ bmap_first
;
611 base
= ((uint8_t *)&s
->bmap
[0]) + bmap_first
* SECTOR_SIZE
;
612 logout("will write %u block map sectors starting from entry %u\n",
613 n_sectors
, bmap_first
);
614 ret
= bdrv_write(bs
->file
, offset
, base
, n_sectors
);
620 static int vdi_create(const char *filename
, QEMUOptionParameter
*options
)
626 size_t block_size
= DEFAULT_CLUSTER_SIZE
;
627 uint32_t image_type
= VDI_TYPE_DYNAMIC
;
635 /* Read out options. */
636 while (options
&& options
->name
) {
637 if (!strcmp(options
->name
, BLOCK_OPT_SIZE
)) {
638 bytes
= options
->value
.n
;
639 #if defined(CONFIG_VDI_BLOCK_SIZE)
640 } else if (!strcmp(options
->name
, BLOCK_OPT_CLUSTER_SIZE
)) {
641 if (options
->value
.n
) {
642 /* TODO: Additional checks (SECTOR_SIZE * 2^n, ...). */
643 block_size
= options
->value
.n
;
646 #if defined(CONFIG_VDI_STATIC_IMAGE)
647 } else if (!strcmp(options
->name
, BLOCK_OPT_STATIC
)) {
648 if (options
->value
.n
) {
649 image_type
= VDI_TYPE_STATIC
;
656 fd
= qemu_open(filename
,
657 O_WRONLY
| O_CREAT
| O_TRUNC
| O_BINARY
| O_LARGEFILE
,
663 /* We need enough blocks to store the given disk size,
664 so always round up. */
665 blocks
= (bytes
+ block_size
- 1) / block_size
;
667 bmap_size
= blocks
* sizeof(uint32_t);
668 bmap_size
= ((bmap_size
+ SECTOR_SIZE
- 1) & ~(SECTOR_SIZE
-1));
670 memset(&header
, 0, sizeof(header
));
671 pstrcpy(header
.text
, sizeof(header
.text
), VDI_TEXT
);
672 header
.signature
= VDI_SIGNATURE
;
673 header
.version
= VDI_VERSION_1_1
;
674 header
.header_size
= 0x180;
675 header
.image_type
= image_type
;
676 header
.offset_bmap
= 0x200;
677 header
.offset_data
= 0x200 + bmap_size
;
678 header
.sector_size
= SECTOR_SIZE
;
679 header
.disk_size
= bytes
;
680 header
.block_size
= block_size
;
681 header
.blocks_in_image
= blocks
;
682 if (image_type
== VDI_TYPE_STATIC
) {
683 header
.blocks_allocated
= blocks
;
685 uuid_generate(header
.uuid_image
);
686 uuid_generate(header
.uuid_last_snap
);
687 /* There is no need to set header.uuid_link or header.uuid_parent here. */
688 #if defined(CONFIG_VDI_DEBUG)
689 vdi_header_print(&header
);
691 vdi_header_to_le(&header
);
692 if (write(fd
, &header
, sizeof(header
)) < 0) {
698 bmap
= (uint32_t *)g_malloc0(bmap_size
);
700 for (i
= 0; i
< blocks
; i
++) {
701 if (image_type
== VDI_TYPE_STATIC
) {
704 bmap
[i
] = VDI_UNALLOCATED
;
707 if (write(fd
, bmap
, bmap_size
) < 0) {
711 if (image_type
== VDI_TYPE_STATIC
) {
712 if (ftruncate(fd
, sizeof(header
) + bmap_size
+ blocks
* block_size
)) {
724 static void vdi_close(BlockDriverState
*bs
)
726 BDRVVdiState
*s
= bs
->opaque
;
730 migrate_del_blocker(s
->migration_blocker
);
731 error_free(s
->migration_blocker
);
734 static QEMUOptionParameter vdi_create_options
[] = {
736 .name
= BLOCK_OPT_SIZE
,
738 .help
= "Virtual disk size"
740 #if defined(CONFIG_VDI_BLOCK_SIZE)
742 .name
= BLOCK_OPT_CLUSTER_SIZE
,
744 .help
= "VDI cluster (block) size",
745 .value
= { .n
= DEFAULT_CLUSTER_SIZE
},
748 #if defined(CONFIG_VDI_STATIC_IMAGE)
750 .name
= BLOCK_OPT_STATIC
,
752 .help
= "VDI static (pre-allocated) image"
755 /* TODO: An additional option to set UUID values might be useful. */
759 static BlockDriver bdrv_vdi
= {
760 .format_name
= "vdi",
761 .instance_size
= sizeof(BDRVVdiState
),
762 .bdrv_probe
= vdi_probe
,
763 .bdrv_open
= vdi_open
,
764 .bdrv_close
= vdi_close
,
765 .bdrv_create
= vdi_create
,
766 .bdrv_co_is_allocated
= vdi_co_is_allocated
,
767 .bdrv_make_empty
= vdi_make_empty
,
769 .bdrv_read
= vdi_co_read
,
770 #if defined(CONFIG_VDI_WRITE)
771 .bdrv_write
= vdi_co_write
,
774 .bdrv_get_info
= vdi_get_info
,
776 .create_options
= vdi_create_options
,
777 .bdrv_check
= vdi_check
,
780 static void bdrv_vdi_init(void)
783 bdrv_register(&bdrv_vdi
);
786 block_init(bdrv_vdi_init
);