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 void *get_mmap_addr(unsigned long size
)
38 void qemu_free(void *ptr
)
43 void *qemu_malloc(size_t size
)
48 void *qemu_mallocz(size_t size
)
51 ptr
= qemu_malloc(size
);
60 void *qemu_memalign(size_t alignment
, size_t size
)
62 return VirtualAlloc(NULL
, size
, MEM_COMMIT
, PAGE_READWRITE
);
67 void *qemu_memalign(size_t alignment
, size_t size
)
69 #if defined(_POSIX_C_SOURCE)
72 ret
= posix_memalign(&ptr
, alignment
, size
);
79 return memalign(alignment
, size
);
85 char *qemu_strdup(const char *str
)
88 ptr
= qemu_malloc(strlen(str
) + 1);
95 static void __attribute__((noreturn
)) error(const char *fmt
, ...)
99 fprintf(stderr
, "qemu-img: ");
100 vfprintf(stderr
, fmt
, ap
);
101 fprintf(stderr
, "\n");
106 static void format_print(void *opaque
, const char *name
)
111 static void help(void)
113 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2008 Fabrice Bellard\n"
114 "usage: qemu-img command [command options]\n"
115 "QEMU disk image utility\n"
118 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
119 " commit [-f fmt] filename\n"
120 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] filename [filename2 [...]] output_filename\n"
121 " info [-f fmt] filename\n"
123 "Command parameters:\n"
124 " 'filename' is a disk image filename\n"
125 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
126 " write image; the copy on write image only stores the modified data\n"
127 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
128 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
129 " and 'G' (gigabyte) are supported\n"
130 " 'output_filename' is the destination disk image filename\n"
131 " 'output_fmt' is the destination format\n"
132 " '-c' indicates that target image must be compressed (qcow format only)\n"
133 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
134 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
136 printf("\nSupported format:");
137 bdrv_iterate_format(format_print
, NULL
);
143 /* XXX: put correct support for win32 */
144 static int read_password(char *buf
, int buf_size
)
147 printf("Password: ");
154 if (i
< (buf_size
- 1))
165 static struct termios oldtty
;
167 static void term_exit(void)
169 tcsetattr (0, TCSANOW
, &oldtty
);
172 static void term_init(void)
179 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
180 |INLCR
|IGNCR
|ICRNL
|IXON
);
181 tty
.c_oflag
|= OPOST
;
182 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
183 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
188 tcsetattr (0, TCSANOW
, &tty
);
193 static int read_password(char *buf
, int buf_size
)
198 printf("password: ");
203 ret
= read(0, &ch
, 1);
205 if (errno
== EAGAIN
|| errno
== EINTR
) {
211 } else if (ret
== 0) {
219 if (i
< (buf_size
- 1))
230 static BlockDriverState
*bdrv_new_open(const char *filename
,
233 BlockDriverState
*bs
;
239 error("Not enough memory");
241 drv
= bdrv_find_format(fmt
);
243 error("Unknown file format '%s'", fmt
);
247 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
248 error("Could not open '%s'", filename
);
250 if (bdrv_is_encrypted(bs
)) {
251 printf("Disk image '%s' is encrypted.\n", filename
);
252 if (read_password(password
, sizeof(password
)) < 0)
253 error("No password given");
254 if (bdrv_set_key(bs
, password
) < 0)
255 error("invalid password");
260 static int img_create(int argc
, char **argv
)
263 const char *fmt
= "raw";
264 const char *filename
;
265 const char *base_filename
= NULL
;
272 c
= getopt(argc
, argv
, "b:f:he6");
280 base_filename
= optarg
;
286 flags
|= BLOCK_FLAG_ENCRYPT
;
289 flags
|= BLOCK_FLAG_COMPAT6
;
295 filename
= argv
[optind
++];
298 BlockDriverState
*bs
;
299 bs
= bdrv_new_open(base_filename
, NULL
);
300 bdrv_get_geometry(bs
, &size
);
307 size
= strtoul(p
, (char **)&p
, 0);
310 } else if (*p
== 'G') {
311 size
*= 1024 * 1024 * 1024;
312 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
318 drv
= bdrv_find_format(fmt
);
320 error("Unknown file format '%s'", fmt
);
321 printf("Formatting '%s', fmt=%s",
323 if (flags
& BLOCK_FLAG_ENCRYPT
)
324 printf(", encrypted");
325 if (flags
& BLOCK_FLAG_COMPAT6
)
326 printf(", compatibility level=6");
328 printf(", backing_file=%s",
331 printf(", size=%" PRIu64
" kB\n", size
/ 1024);
332 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, flags
);
334 if (ret
== -ENOTSUP
) {
335 error("Formatting or formatting option not supported for file format '%s'", fmt
);
337 error("Error while formatting");
343 static int img_commit(int argc
, char **argv
)
346 const char *filename
, *fmt
;
348 BlockDriverState
*bs
;
352 c
= getopt(argc
, argv
, "f:h");
366 filename
= argv
[optind
++];
370 error("Not enough memory");
372 drv
= bdrv_find_format(fmt
);
374 error("Unknown file format '%s'", fmt
);
378 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
379 error("Could not open '%s'", filename
);
381 ret
= bdrv_commit(bs
);
384 printf("Image committed.\n");
387 error("No disk inserted");
390 error("Image is read-only");
393 error("Image is already committed");
396 error("Error while committing image");
404 static int is_not_zero(const uint8_t *sector
, int len
)
408 for(i
= 0;i
< len
; i
++) {
409 if (((uint32_t *)sector
)[i
] != 0)
415 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
423 v
= is_not_zero(buf
, 512);
424 for(i
= 1; i
< n
; i
++) {
426 if (v
!= is_not_zero(buf
, 512))
433 #define IO_BUF_SIZE 65536
435 static int img_convert(int argc
, char **argv
)
437 int c
, ret
, n
, n1
, bs_n
, bs_i
, flags
, cluster_size
, cluster_sectors
;
438 const char *fmt
, *out_fmt
, *out_filename
;
440 BlockDriverState
**bs
, *out_bs
;
441 int64_t total_sectors
, nb_sectors
, sector_num
, bs_offset
;
443 uint8_t buf
[IO_BUF_SIZE
];
451 c
= getopt(argc
, argv
, "f:O:hce6");
465 flags
|= BLOCK_FLAG_COMPRESS
;
468 flags
|= BLOCK_FLAG_ENCRYPT
;
471 flags
|= BLOCK_FLAG_COMPAT6
;
476 bs_n
= argc
- optind
- 1;
477 if (bs_n
< 1) help();
479 out_filename
= argv
[argc
- 1];
481 bs
= calloc(bs_n
, sizeof(BlockDriverState
*));
483 error("Out of memory");
486 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++) {
487 bs
[bs_i
] = bdrv_new_open(argv
[optind
+ bs_i
], fmt
);
489 error("Could not open '%s'", argv
[optind
+ bs_i
]);
490 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
491 total_sectors
+= bs_sectors
;
494 drv
= bdrv_find_format(out_fmt
);
496 error("Unknown file format '%s'", out_fmt
);
497 if (flags
& BLOCK_FLAG_COMPRESS
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
498 error("Compression not supported for this file format");
499 if (flags
& BLOCK_FLAG_ENCRYPT
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
500 error("Encryption not supported for this file format");
501 if (flags
& BLOCK_FLAG_COMPAT6
&& drv
!= &bdrv_vmdk
)
502 error("Alternative compatibility level not supported for this file format");
503 if (flags
& BLOCK_FLAG_ENCRYPT
&& flags
& BLOCK_FLAG_COMPRESS
)
504 error("Compression and encryption not supported at the same time");
506 ret
= bdrv_create(drv
, out_filename
, total_sectors
, NULL
, flags
);
508 if (ret
== -ENOTSUP
) {
509 error("Formatting not supported for file format '%s'", fmt
);
511 error("Error while formatting '%s'", out_filename
);
515 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
519 bdrv_get_geometry(bs
[0], &bs_sectors
);
521 if (flags
& BLOCK_FLAG_COMPRESS
) {
522 if (bdrv_get_info(out_bs
, &bdi
) < 0)
523 error("could not get block driver info");
524 cluster_size
= bdi
.cluster_size
;
525 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
526 error("invalid cluster size");
527 cluster_sectors
= cluster_size
>> 9;
534 nb_sectors
= total_sectors
- sector_num
;
537 if (nb_sectors
>= cluster_sectors
)
542 bs_num
= sector_num
- bs_offset
;
543 assert (bs_num
>= 0);
546 while (remainder
> 0) {
548 while (bs_num
== bs_sectors
) {
550 assert (bs_i
< bs_n
);
551 bs_offset
+= bs_sectors
;
552 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
554 /* printf("changing part: sector_num=%lld, "
555 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
556 sector_num, bs_i, bs_offset, bs_sectors); */
558 assert (bs_num
< bs_sectors
);
560 nlow
= (remainder
> bs_sectors
- bs_num
) ? bs_sectors
- bs_num
: remainder
;
562 if (bdrv_read(bs
[bs_i
], bs_num
, buf2
, nlow
) < 0)
563 error("error while reading");
570 assert (remainder
== 0);
572 if (n
< cluster_sectors
)
573 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
574 if (is_not_zero(buf
, cluster_size
)) {
575 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
576 cluster_sectors
) != 0)
577 error("error while compressing sector %" PRId64
,
582 /* signal EOF to align */
583 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
587 nb_sectors
= total_sectors
- sector_num
;
590 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
591 n
= (IO_BUF_SIZE
/ 512);
595 while (sector_num
- bs_offset
>= bs_sectors
) {
597 assert (bs_i
< bs_n
);
598 bs_offset
+= bs_sectors
;
599 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
600 /* printf("changing part: sector_num=%lld, bs_i=%d, "
601 "bs_offset=%lld, bs_sectors=%lld\n",
602 sector_num, bs_i, bs_offset, bs_sectors); */
605 if (n
> bs_offset
+ bs_sectors
- sector_num
)
606 n
= bs_offset
+ bs_sectors
- sector_num
;
608 if (bdrv_read(bs
[bs_i
], sector_num
- bs_offset
, buf
, n
) < 0)
609 error("error while reading");
610 /* NOTE: at the same time we convert, we do not write zero
611 sectors to have a chance to compress the image. Ideally, we
612 should add a specific call to have the info to go faster */
615 if (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 sprintf(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
);