2 * QEMU disk image utility
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24 #include "qemu-common.h"
25 #include "block_int.h"
29 #define WIN32_LEAN_AND_MEAN
33 /* Default to cache=writeback as data integrity is not important for qemu-tcg. */
34 #define BRDV_O_FLAGS BDRV_O_CACHE_WB
36 static void __attribute__((noreturn
)) error(const char *fmt
, ...)
40 fprintf(stderr
, "qemu-img: ");
41 vfprintf(stderr
, fmt
, ap
);
42 fprintf(stderr
, "\n");
47 static void format_print(void *opaque
, const char *name
)
52 static void help(void)
54 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2008 Fabrice Bellard\n"
55 "usage: qemu-img command [command options]\n"
56 "QEMU disk image utility\n"
59 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
60 " commit [-f fmt] filename\n"
61 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
62 " info [-f fmt] filename\n"
64 "Command parameters:\n"
65 " 'filename' is a disk image filename\n"
66 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
67 " write image; the copy on write image only stores the modified data\n"
68 " 'output_base_image' forces the output image to be created as a copy on write\n"
69 " image of the specified base image; 'output_base_image' should have the same\n"
70 " content as the input's base image, however the path, image format, etc may\n"
72 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
73 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
74 " and 'G' (gigabyte) are supported\n"
75 " 'output_filename' is the destination disk image filename\n"
76 " 'output_fmt' is the destination format\n"
77 " '-c' indicates that target image must be compressed (qcow format only)\n"
78 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
79 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
81 printf("\nSupported format:");
82 bdrv_iterate_format(format_print
, NULL
);
88 /* XXX: put correct support for win32 */
89 static int read_password(char *buf
, int buf_size
)
99 if (i
< (buf_size
- 1))
110 static struct termios oldtty
;
112 static void term_exit(void)
114 tcsetattr (0, TCSANOW
, &oldtty
);
117 static void term_init(void)
124 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
125 |INLCR
|IGNCR
|ICRNL
|IXON
);
126 tty
.c_oflag
|= OPOST
;
127 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
128 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
133 tcsetattr (0, TCSANOW
, &tty
);
138 static int read_password(char *buf
, int buf_size
)
143 printf("password: ");
148 ret
= read(0, &ch
, 1);
150 if (errno
== EAGAIN
|| errno
== EINTR
) {
156 } else if (ret
== 0) {
164 if (i
< (buf_size
- 1))
175 static BlockDriverState
*bdrv_new_open(const char *filename
,
178 BlockDriverState
*bs
;
184 error("Not enough memory");
186 drv
= bdrv_find_format(fmt
);
188 error("Unknown file format '%s'", fmt
);
192 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
193 error("Could not open '%s'", filename
);
195 if (bdrv_is_encrypted(bs
)) {
196 printf("Disk image '%s' is encrypted.\n", filename
);
197 if (read_password(password
, sizeof(password
)) < 0)
198 error("No password given");
199 if (bdrv_set_key(bs
, password
) < 0)
200 error("invalid password");
205 static int img_create(int argc
, char **argv
)
208 const char *fmt
= "raw";
209 const char *filename
;
210 const char *base_filename
= NULL
;
217 c
= getopt(argc
, argv
, "b:f:he6");
225 base_filename
= optarg
;
231 flags
|= BLOCK_FLAG_ENCRYPT
;
234 flags
|= BLOCK_FLAG_COMPAT6
;
240 filename
= argv
[optind
++];
243 BlockDriverState
*bs
;
244 bs
= bdrv_new_open(base_filename
, NULL
);
245 bdrv_get_geometry(bs
, &size
);
252 size
= strtoul(p
, (char **)&p
, 0);
255 } else if (*p
== 'G') {
256 size
*= 1024 * 1024 * 1024;
257 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
263 drv
= bdrv_find_format(fmt
);
265 error("Unknown file format '%s'", fmt
);
266 printf("Formatting '%s', fmt=%s",
268 if (flags
& BLOCK_FLAG_ENCRYPT
)
269 printf(", encrypted");
270 if (flags
& BLOCK_FLAG_COMPAT6
)
271 printf(", compatibility level=6");
273 printf(", backing_file=%s",
276 printf(", size=%" PRIu64
" kB\n", size
/ 1024);
277 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, flags
);
279 if (ret
== -ENOTSUP
) {
280 error("Formatting or formatting option not supported for file format '%s'", fmt
);
282 error("Error while formatting");
288 static int img_commit(int argc
, char **argv
)
291 const char *filename
, *fmt
;
293 BlockDriverState
*bs
;
297 c
= getopt(argc
, argv
, "f:h");
311 filename
= argv
[optind
++];
315 error("Not enough memory");
317 drv
= bdrv_find_format(fmt
);
319 error("Unknown file format '%s'", fmt
);
323 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
324 error("Could not open '%s'", filename
);
326 ret
= bdrv_commit(bs
);
329 printf("Image committed.\n");
332 error("No disk inserted");
335 error("Image is read-only");
338 error("Image is already committed");
341 error("Error while committing image");
349 static int is_not_zero(const uint8_t *sector
, int len
)
353 for(i
= 0;i
< len
; i
++) {
354 if (((uint32_t *)sector
)[i
] != 0)
361 * Returns true iff the first sector pointed to by 'buf' contains at least
364 * 'pnum' is set to the number of sectors (including and immediately following
365 * the first one) that are known to be in the same allocated/unallocated state.
367 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
375 v
= is_not_zero(buf
, 512);
376 for(i
= 1; i
< n
; i
++) {
378 if (v
!= is_not_zero(buf
, 512))
385 #define IO_BUF_SIZE 65536
387 static int img_convert(int argc
, char **argv
)
389 int c
, ret
, n
, n1
, bs_n
, bs_i
, flags
, cluster_size
, cluster_sectors
;
390 const char *fmt
, *out_fmt
, *out_baseimg
, *out_filename
;
392 BlockDriverState
**bs
, *out_bs
;
393 int64_t total_sectors
, nb_sectors
, sector_num
, bs_offset
;
395 uint8_t buf
[IO_BUF_SIZE
];
404 c
= getopt(argc
, argv
, "f:O:B:hce6");
418 out_baseimg
= optarg
;
421 flags
|= BLOCK_FLAG_COMPRESS
;
424 flags
|= BLOCK_FLAG_ENCRYPT
;
427 flags
|= BLOCK_FLAG_COMPAT6
;
432 bs_n
= argc
- optind
- 1;
433 if (bs_n
< 1) help();
435 out_filename
= argv
[argc
- 1];
437 if (bs_n
> 1 && out_baseimg
)
438 error("-B makes no sense when concatenating multiple input images");
440 bs
= calloc(bs_n
, sizeof(BlockDriverState
*));
442 error("Out of memory");
445 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++) {
446 bs
[bs_i
] = bdrv_new_open(argv
[optind
+ bs_i
], fmt
);
448 error("Could not open '%s'", argv
[optind
+ bs_i
]);
449 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
450 total_sectors
+= bs_sectors
;
453 drv
= bdrv_find_format(out_fmt
);
455 error("Unknown file format '%s'", out_fmt
);
456 if (flags
& BLOCK_FLAG_COMPRESS
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
457 error("Compression not supported for this file format");
458 if (flags
& BLOCK_FLAG_ENCRYPT
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
459 error("Encryption not supported for this file format");
460 if (flags
& BLOCK_FLAG_COMPAT6
&& drv
!= &bdrv_vmdk
)
461 error("Alternative compatibility level not supported for this file format");
462 if (flags
& BLOCK_FLAG_ENCRYPT
&& flags
& BLOCK_FLAG_COMPRESS
)
463 error("Compression and encryption not supported at the same time");
465 ret
= bdrv_create(drv
, out_filename
, total_sectors
, out_baseimg
, flags
);
467 if (ret
== -ENOTSUP
) {
468 error("Formatting not supported for file format '%s'", fmt
);
470 error("Error while formatting '%s'", out_filename
);
474 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
478 bdrv_get_geometry(bs
[0], &bs_sectors
);
480 if (flags
& BLOCK_FLAG_COMPRESS
) {
481 if (bdrv_get_info(out_bs
, &bdi
) < 0)
482 error("could not get block driver info");
483 cluster_size
= bdi
.cluster_size
;
484 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
485 error("invalid cluster size");
486 cluster_sectors
= cluster_size
>> 9;
493 nb_sectors
= total_sectors
- sector_num
;
496 if (nb_sectors
>= cluster_sectors
)
501 bs_num
= sector_num
- bs_offset
;
502 assert (bs_num
>= 0);
505 while (remainder
> 0) {
507 while (bs_num
== bs_sectors
) {
509 assert (bs_i
< bs_n
);
510 bs_offset
+= bs_sectors
;
511 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
513 /* printf("changing part: sector_num=%lld, "
514 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
515 sector_num, bs_i, bs_offset, bs_sectors); */
517 assert (bs_num
< bs_sectors
);
519 nlow
= (remainder
> bs_sectors
- bs_num
) ? bs_sectors
- bs_num
: remainder
;
521 if (bdrv_read(bs
[bs_i
], bs_num
, buf2
, nlow
) < 0)
522 error("error while reading");
529 assert (remainder
== 0);
531 if (n
< cluster_sectors
)
532 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
533 if (is_not_zero(buf
, cluster_size
)) {
534 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
535 cluster_sectors
) != 0)
536 error("error while compressing sector %" PRId64
,
541 /* signal EOF to align */
542 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
544 sector_num
= 0; // total number of sectors converted so far
546 nb_sectors
= total_sectors
- sector_num
;
549 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
550 n
= (IO_BUF_SIZE
/ 512);
554 while (sector_num
- bs_offset
>= bs_sectors
) {
556 assert (bs_i
< bs_n
);
557 bs_offset
+= bs_sectors
;
558 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
559 /* printf("changing part: sector_num=%lld, bs_i=%d, "
560 "bs_offset=%lld, bs_sectors=%lld\n",
561 sector_num, bs_i, bs_offset, bs_sectors); */
564 if (n
> bs_offset
+ bs_sectors
- sector_num
)
565 n
= bs_offset
+ bs_sectors
- sector_num
;
567 /* If the output image is being created as a copy on write image,
568 assume that sectors which are unallocated in the input image
569 are present in both the output's and input's base images (no
570 need to copy them). */
572 if (!bdrv_is_allocated(bs
[bs_i
], sector_num
- bs_offset
, n
, &n1
)) {
576 /* The next 'n1' sectors are allocated in the input image. Copy
577 only those as they may be followed by unallocated sectors. */
581 if (bdrv_read(bs
[bs_i
], sector_num
- bs_offset
, buf
, n
) < 0)
582 error("error while reading");
583 /* NOTE: at the same time we convert, we do not write zero
584 sectors to have a chance to compress the image. Ideally, we
585 should add a specific call to have the info to go faster */
588 /* If the output image is being created as a copy on write image,
589 copy all sectors even the ones containing only NUL bytes,
590 because they may differ from the sectors in the base image. */
591 if (out_baseimg
|| is_allocated_sectors(buf1
, n
, &n1
)) {
592 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
593 error("error while writing");
602 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++)
603 bdrv_delete(bs
[bs_i
]);
609 static int64_t get_allocated_file_size(const char *filename
)
611 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
612 get_compressed_t get_compressed
;
615 /* WinNT support GetCompressedFileSize to determine allocate size */
616 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
617 if (get_compressed
) {
619 low
= get_compressed(filename
, &high
);
620 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
621 return (((int64_t) high
) << 32) + low
;
624 if (_stati64(filename
, &st
) < 0)
629 static int64_t get_allocated_file_size(const char *filename
)
632 if (stat(filename
, &st
) < 0)
634 return (int64_t)st
.st_blocks
* 512;
638 static void dump_snapshots(BlockDriverState
*bs
)
640 QEMUSnapshotInfo
*sn_tab
, *sn
;
644 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
647 printf("Snapshot list:\n");
648 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
649 for(i
= 0; i
< nb_sns
; i
++) {
651 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
656 static int img_info(int argc
, char **argv
)
659 const char *filename
, *fmt
;
661 BlockDriverState
*bs
;
662 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
663 uint64_t total_sectors
;
664 int64_t allocated_size
;
665 char backing_filename
[1024];
666 char backing_filename2
[1024];
671 c
= getopt(argc
, argv
, "f:h");
685 filename
= argv
[optind
++];
689 error("Not enough memory");
691 drv
= bdrv_find_format(fmt
);
693 error("Unknown file format '%s'", fmt
);
697 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
698 error("Could not open '%s'", filename
);
700 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
701 bdrv_get_geometry(bs
, &total_sectors
);
702 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
703 allocated_size
= get_allocated_file_size(filename
);
704 if (allocated_size
< 0)
705 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
707 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
711 "virtual size: %s (%" PRId64
" bytes)\n"
713 filename
, fmt_name
, size_buf
,
714 (total_sectors
* 512),
716 if (bdrv_is_encrypted(bs
))
717 printf("encrypted: yes\n");
718 if (bdrv_get_info(bs
, &bdi
) >= 0) {
719 if (bdi
.cluster_size
!= 0)
720 printf("cluster_size: %d\n", bdi
.cluster_size
);
722 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
723 if (backing_filename
[0] != '\0') {
724 path_combine(backing_filename2
, sizeof(backing_filename2
),
725 filename
, backing_filename
);
726 printf("backing file: %s (actual path: %s)\n",
735 int main(int argc
, char **argv
)
744 if (!strcmp(cmd
, "create")) {
745 img_create(argc
, argv
);
746 } else if (!strcmp(cmd
, "commit")) {
747 img_commit(argc
, argv
);
748 } else if (!strcmp(cmd
, "convert")) {
749 img_convert(argc
, argv
);
750 } else if (!strcmp(cmd
, "info")) {
751 img_info(argc
, argv
);