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
35 void *qemu_memalign(size_t alignment
, size_t size
)
37 return VirtualAlloc(NULL
, size
, MEM_COMMIT
, PAGE_READWRITE
);
42 void *qemu_memalign(size_t alignment
, size_t size
)
44 #if defined(_POSIX_C_SOURCE)
47 ret
= posix_memalign(&ptr
, alignment
, size
);
54 return memalign(alignment
, size
);
60 static void __attribute__((noreturn
)) error(const char *fmt
, ...)
64 fprintf(stderr
, "qemu-img: ");
65 vfprintf(stderr
, fmt
, ap
);
66 fprintf(stderr
, "\n");
71 static void format_print(void *opaque
, const char *name
)
76 static void help(void)
78 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2008 Fabrice Bellard\n"
79 "usage: qemu-img command [command options]\n"
80 "QEMU disk image utility\n"
83 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
84 " commit [-f fmt] filename\n"
85 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
86 " info [-f fmt] filename\n"
88 "Command parameters:\n"
89 " 'filename' is a disk image filename\n"
90 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
91 " write image; the copy on write image only stores the modified data\n"
92 " 'output_base_image' forces the output image to be created as a copy on write\n"
93 " image of the specified base image; 'output_base_image' should have the same\n"
94 " content as the input's base image, however the path, image format, etc may\n"
96 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
97 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
98 " and 'G' (gigabyte) are supported\n"
99 " 'output_filename' is the destination disk image filename\n"
100 " 'output_fmt' is the destination format\n"
101 " '-c' indicates that target image must be compressed (qcow format only)\n"
102 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
103 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
105 printf("\nSupported format:");
106 bdrv_iterate_format(format_print
, NULL
);
112 /* XXX: put correct support for win32 */
113 static int read_password(char *buf
, int buf_size
)
116 printf("Password: ");
123 if (i
< (buf_size
- 1))
134 static struct termios oldtty
;
136 static void term_exit(void)
138 tcsetattr (0, TCSANOW
, &oldtty
);
141 static void term_init(void)
148 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
149 |INLCR
|IGNCR
|ICRNL
|IXON
);
150 tty
.c_oflag
|= OPOST
;
151 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
152 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
157 tcsetattr (0, TCSANOW
, &tty
);
162 static int read_password(char *buf
, int buf_size
)
167 printf("password: ");
172 ret
= read(0, &ch
, 1);
174 if (errno
== EAGAIN
|| errno
== EINTR
) {
180 } else if (ret
== 0) {
188 if (i
< (buf_size
- 1))
199 static BlockDriverState
*bdrv_new_open(const char *filename
,
202 BlockDriverState
*bs
;
208 error("Not enough memory");
210 drv
= bdrv_find_format(fmt
);
212 error("Unknown file format '%s'", fmt
);
216 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
217 error("Could not open '%s'", filename
);
219 if (bdrv_is_encrypted(bs
)) {
220 printf("Disk image '%s' is encrypted.\n", filename
);
221 if (read_password(password
, sizeof(password
)) < 0)
222 error("No password given");
223 if (bdrv_set_key(bs
, password
) < 0)
224 error("invalid password");
229 static int img_create(int argc
, char **argv
)
232 const char *fmt
= "raw";
233 const char *filename
;
234 const char *base_filename
= NULL
;
241 c
= getopt(argc
, argv
, "b:f:he6");
249 base_filename
= optarg
;
255 flags
|= BLOCK_FLAG_ENCRYPT
;
258 flags
|= BLOCK_FLAG_COMPAT6
;
264 filename
= argv
[optind
++];
267 BlockDriverState
*bs
;
268 bs
= bdrv_new_open(base_filename
, NULL
);
269 bdrv_get_geometry(bs
, &size
);
276 size
= strtoul(p
, (char **)&p
, 0);
279 } else if (*p
== 'G') {
280 size
*= 1024 * 1024 * 1024;
281 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
287 drv
= bdrv_find_format(fmt
);
289 error("Unknown file format '%s'", fmt
);
290 printf("Formatting '%s', fmt=%s",
292 if (flags
& BLOCK_FLAG_ENCRYPT
)
293 printf(", encrypted");
294 if (flags
& BLOCK_FLAG_COMPAT6
)
295 printf(", compatibility level=6");
297 printf(", backing_file=%s",
300 printf(", size=%" PRIu64
" kB\n", size
/ 1024);
301 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, flags
);
303 if (ret
== -ENOTSUP
) {
304 error("Formatting or formatting option not supported for file format '%s'", fmt
);
306 error("Error while formatting");
312 static int img_commit(int argc
, char **argv
)
315 const char *filename
, *fmt
;
317 BlockDriverState
*bs
;
321 c
= getopt(argc
, argv
, "f:h");
335 filename
= argv
[optind
++];
339 error("Not enough memory");
341 drv
= bdrv_find_format(fmt
);
343 error("Unknown file format '%s'", fmt
);
347 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
348 error("Could not open '%s'", filename
);
350 ret
= bdrv_commit(bs
);
353 printf("Image committed.\n");
356 error("No disk inserted");
359 error("Image is read-only");
362 error("Image is already committed");
365 error("Error while committing image");
373 static int is_not_zero(const uint8_t *sector
, int len
)
377 for(i
= 0;i
< len
; i
++) {
378 if (((uint32_t *)sector
)[i
] != 0)
385 * Returns true iff the first sector pointed to by 'buf' contains at least
388 * 'pnum' is set to the number of sectors (including and immediately following
389 * the first one) that are known to be in the same allocated/unallocated state.
391 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
399 v
= is_not_zero(buf
, 512);
400 for(i
= 1; i
< n
; i
++) {
402 if (v
!= is_not_zero(buf
, 512))
409 #define IO_BUF_SIZE 65536
411 static int img_convert(int argc
, char **argv
)
413 int c
, ret
, n
, n1
, bs_n
, bs_i
, flags
, cluster_size
, cluster_sectors
;
414 const char *fmt
, *out_fmt
, *out_baseimg
, *out_filename
;
416 BlockDriverState
**bs
, *out_bs
;
417 int64_t total_sectors
, nb_sectors
, sector_num
, bs_offset
;
419 uint8_t buf
[IO_BUF_SIZE
];
428 c
= getopt(argc
, argv
, "f:O:B:hce6");
442 out_baseimg
= optarg
;
445 flags
|= BLOCK_FLAG_COMPRESS
;
448 flags
|= BLOCK_FLAG_ENCRYPT
;
451 flags
|= BLOCK_FLAG_COMPAT6
;
456 bs_n
= argc
- optind
- 1;
457 if (bs_n
< 1) help();
459 out_filename
= argv
[argc
- 1];
461 if (bs_n
> 1 && out_baseimg
)
462 error("-B makes no sense when concatenating multiple input images");
464 bs
= calloc(bs_n
, sizeof(BlockDriverState
*));
466 error("Out of memory");
469 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++) {
470 bs
[bs_i
] = bdrv_new_open(argv
[optind
+ bs_i
], fmt
);
472 error("Could not open '%s'", argv
[optind
+ bs_i
]);
473 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
474 total_sectors
+= bs_sectors
;
477 drv
= bdrv_find_format(out_fmt
);
479 error("Unknown file format '%s'", out_fmt
);
480 if (flags
& BLOCK_FLAG_COMPRESS
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
481 error("Compression not supported for this file format");
482 if (flags
& BLOCK_FLAG_ENCRYPT
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
483 error("Encryption not supported for this file format");
484 if (flags
& BLOCK_FLAG_COMPAT6
&& drv
!= &bdrv_vmdk
)
485 error("Alternative compatibility level not supported for this file format");
486 if (flags
& BLOCK_FLAG_ENCRYPT
&& flags
& BLOCK_FLAG_COMPRESS
)
487 error("Compression and encryption not supported at the same time");
489 ret
= bdrv_create(drv
, out_filename
, total_sectors
, out_baseimg
, flags
);
491 if (ret
== -ENOTSUP
) {
492 error("Formatting not supported for file format '%s'", fmt
);
494 error("Error while formatting '%s'", out_filename
);
498 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
502 bdrv_get_geometry(bs
[0], &bs_sectors
);
504 if (flags
& BLOCK_FLAG_COMPRESS
) {
505 if (bdrv_get_info(out_bs
, &bdi
) < 0)
506 error("could not get block driver info");
507 cluster_size
= bdi
.cluster_size
;
508 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
509 error("invalid cluster size");
510 cluster_sectors
= cluster_size
>> 9;
517 nb_sectors
= total_sectors
- sector_num
;
520 if (nb_sectors
>= cluster_sectors
)
525 bs_num
= sector_num
- bs_offset
;
526 assert (bs_num
>= 0);
529 while (remainder
> 0) {
531 while (bs_num
== bs_sectors
) {
533 assert (bs_i
< bs_n
);
534 bs_offset
+= bs_sectors
;
535 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
537 /* printf("changing part: sector_num=%lld, "
538 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
539 sector_num, bs_i, bs_offset, bs_sectors); */
541 assert (bs_num
< bs_sectors
);
543 nlow
= (remainder
> bs_sectors
- bs_num
) ? bs_sectors
- bs_num
: remainder
;
545 if (bdrv_read(bs
[bs_i
], bs_num
, buf2
, nlow
) < 0)
546 error("error while reading");
553 assert (remainder
== 0);
555 if (n
< cluster_sectors
)
556 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
557 if (is_not_zero(buf
, cluster_size
)) {
558 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
559 cluster_sectors
) != 0)
560 error("error while compressing sector %" PRId64
,
565 /* signal EOF to align */
566 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
568 sector_num
= 0; // total number of sectors converted so far
570 nb_sectors
= total_sectors
- sector_num
;
573 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
574 n
= (IO_BUF_SIZE
/ 512);
578 while (sector_num
- bs_offset
>= bs_sectors
) {
580 assert (bs_i
< bs_n
);
581 bs_offset
+= bs_sectors
;
582 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
583 /* printf("changing part: sector_num=%lld, bs_i=%d, "
584 "bs_offset=%lld, bs_sectors=%lld\n",
585 sector_num, bs_i, bs_offset, bs_sectors); */
588 if (n
> bs_offset
+ bs_sectors
- sector_num
)
589 n
= bs_offset
+ bs_sectors
- sector_num
;
591 /* If the output image is being created as a copy on write image,
592 assume that sectors which are unallocated in the input image
593 are present in both the output's and input's base images (no
594 need to copy them). */
596 if (!bdrv_is_allocated(bs
[bs_i
], sector_num
- bs_offset
, n
, &n1
)) {
600 /* The next 'n1' sectors are allocated in the input image. Copy
601 only those as they may be followed by unallocated sectors. */
605 if (bdrv_read(bs
[bs_i
], sector_num
- bs_offset
, buf
, n
) < 0)
606 error("error while reading");
607 /* NOTE: at the same time we convert, we do not write zero
608 sectors to have a chance to compress the image. Ideally, we
609 should add a specific call to have the info to go faster */
612 /* If the output image is being created as a copy on write image,
613 copy all sectors even the ones containing only NUL bytes,
614 because they may differ from the sectors in the base image. */
615 if (out_baseimg
|| is_allocated_sectors(buf1
, n
, &n1
)) {
616 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
617 error("error while writing");
626 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++)
627 bdrv_delete(bs
[bs_i
]);
633 static int64_t get_allocated_file_size(const char *filename
)
635 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
636 get_compressed_t get_compressed
;
639 /* WinNT support GetCompressedFileSize to determine allocate size */
640 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
641 if (get_compressed
) {
643 low
= get_compressed(filename
, &high
);
644 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
645 return (((int64_t) high
) << 32) + low
;
648 if (_stati64(filename
, &st
) < 0)
653 static int64_t get_allocated_file_size(const char *filename
)
656 if (stat(filename
, &st
) < 0)
658 return (int64_t)st
.st_blocks
* 512;
662 static void dump_snapshots(BlockDriverState
*bs
)
664 QEMUSnapshotInfo
*sn_tab
, *sn
;
668 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
671 printf("Snapshot list:\n");
672 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
673 for(i
= 0; i
< nb_sns
; i
++) {
675 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
680 static int img_info(int argc
, char **argv
)
683 const char *filename
, *fmt
;
685 BlockDriverState
*bs
;
686 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
687 uint64_t total_sectors
;
688 int64_t allocated_size
;
689 char backing_filename
[1024];
690 char backing_filename2
[1024];
695 c
= getopt(argc
, argv
, "f:h");
709 filename
= argv
[optind
++];
713 error("Not enough memory");
715 drv
= bdrv_find_format(fmt
);
717 error("Unknown file format '%s'", fmt
);
721 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
722 error("Could not open '%s'", filename
);
724 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
725 bdrv_get_geometry(bs
, &total_sectors
);
726 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
727 allocated_size
= get_allocated_file_size(filename
);
728 if (allocated_size
< 0)
729 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
731 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
735 "virtual size: %s (%" PRId64
" bytes)\n"
737 filename
, fmt_name
, size_buf
,
738 (total_sectors
* 512),
740 if (bdrv_is_encrypted(bs
))
741 printf("encrypted: yes\n");
742 if (bdrv_get_info(bs
, &bdi
) >= 0) {
743 if (bdi
.cluster_size
!= 0)
744 printf("cluster_size: %d\n", bdi
.cluster_size
);
746 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
747 if (backing_filename
[0] != '\0') {
748 path_combine(backing_filename2
, sizeof(backing_filename2
),
749 filename
, backing_filename
);
750 printf("backing file: %s (actual path: %s)\n",
759 int main(int argc
, char **argv
)
768 if (!strcmp(cmd
, "create")) {
769 img_create(argc
, argv
);
770 } else if (!strcmp(cmd
, "commit")) {
771 img_commit(argc
, argv
);
772 } else if (!strcmp(cmd
, "convert")) {
773 img_convert(argc
, argv
);
774 } else if (!strcmp(cmd
, "info")) {
775 img_info(argc
, argv
);