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"
26 #include "block_int.h"
34 /* Default to cache=writeback as data integrity is not important for qemu-tcg. */
35 #define BRDV_O_FLAGS BDRV_O_CACHE_WB
37 static void QEMU_NORETURN
error(const char *fmt
, ...)
41 fprintf(stderr
, "qemu-img: ");
42 vfprintf(stderr
, fmt
, ap
);
43 fprintf(stderr
, "\n");
48 static void format_print(void *opaque
, const char *name
)
53 /* Please keep in synch with qemu-img.texi */
54 static void help(void)
56 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2008 Fabrice Bellard\n"
57 "usage: qemu-img command [command options]\n"
58 "QEMU disk image utility\n"
61 " check [-f fmt] filename\n"
62 " create [-e] [-6] [-F fmt] [-b base_image] [-f fmt] filename [size]\n"
63 " commit [-f fmt] filename\n"
64 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
65 " info [-f fmt] filename\n"
66 " snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename\n"
68 "Command parameters:\n"
69 " 'filename' is a disk image filename\n"
70 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
71 " write image; the copy on write image only stores the modified data\n"
72 " 'output_base_image' forces the output image to be created as a copy on write\n"
73 " image of the specified base image; 'output_base_image' should have the same\n"
74 " content as the input's base image, however the path, image format, etc may\n"
76 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
77 " 'size' is the disk image size in kilobytes. Optional suffixes\n"
78 " 'M' (megabyte, 1024 * 1024) and 'G' (gigabyte, 1024 * 1024 * 1024) are\n"
79 " supported any 'k' or 'K' is ignored\n"
80 " 'output_filename' is the destination disk image filename\n"
81 " 'output_fmt' is the destination format\n"
82 " '-c' indicates that target image must be compressed (qcow format only)\n"
83 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
84 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
85 " '-h' with or without a command shows this help and lists the supported formats\n"
87 "Parameters to snapshot subcommand:\n"
88 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
89 " '-a' applies a snapshot (revert disk to saved state)\n"
90 " '-c' creates a snapshot\n"
91 " '-d' deletes a snapshot\n"
92 " '-l' lists all snapshots in the given image\n"
94 printf("\nSupported formats:");
95 bdrv_iterate_format(format_print
, NULL
);
101 /* XXX: put correct support for win32 */
102 static int read_password(char *buf
, int buf_size
)
105 printf("Password: ");
112 if (i
< (buf_size
- 1))
123 static struct termios oldtty
;
125 static void term_exit(void)
127 tcsetattr (0, TCSANOW
, &oldtty
);
130 static void term_init(void)
137 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
138 |INLCR
|IGNCR
|ICRNL
|IXON
);
139 tty
.c_oflag
|= OPOST
;
140 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
141 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
146 tcsetattr (0, TCSANOW
, &tty
);
151 static int read_password(char *buf
, int buf_size
)
156 printf("password: ");
161 ret
= read(0, &ch
, 1);
163 if (errno
== EAGAIN
|| errno
== EINTR
) {
169 } else if (ret
== 0) {
177 if (i
< (buf_size
- 1))
188 static BlockDriverState
*bdrv_new_open(const char *filename
,
191 BlockDriverState
*bs
;
197 error("Not enough memory");
199 drv
= bdrv_find_format(fmt
);
201 error("Unknown file format '%s'", fmt
);
205 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
206 error("Could not open '%s'", filename
);
208 if (bdrv_is_encrypted(bs
)) {
209 printf("Disk image '%s' is encrypted.\n", filename
);
210 if (read_password(password
, sizeof(password
)) < 0)
211 error("No password given");
212 if (bdrv_set_key(bs
, password
) < 0)
213 error("invalid password");
218 static int img_create(int argc
, char **argv
)
221 const char *fmt
= "raw";
222 const char *base_fmt
= NULL
;
223 const char *filename
;
224 const char *base_filename
= NULL
;
232 c
= getopt(argc
, argv
, "F:b:f:he6");
243 base_filename
= optarg
;
249 flags
|= BLOCK_FLAG_ENCRYPT
;
252 flags
|= BLOCK_FLAG_COMPAT6
;
258 filename
= argv
[optind
++];
261 BlockDriverState
*bs
;
262 BlockDriver
*base_drv
= NULL
;
265 base_drv
= bdrv_find_format(base_fmt
);
266 if (base_drv
== NULL
)
267 error("Unknown basefile format '%s'", base_fmt
);
270 bs
= bdrv_new_open(base_filename
, base_fmt
);
271 bdrv_get_geometry(bs
, &size
);
278 sizef
= strtod(p
, (char **)&p
);
280 size
= (uint64_t)(sizef
* 1024 * 1024);
281 } else if (*p
== 'G') {
282 size
= (uint64_t)(sizef
* 1024 * 1024 * 1024);
283 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
284 size
= (uint64_t)(sizef
* 1024);
289 drv
= bdrv_find_format(fmt
);
291 error("Unknown file format '%s'", fmt
);
292 printf("Formatting '%s', fmt=%s",
294 if (flags
& BLOCK_FLAG_ENCRYPT
)
295 printf(", encrypted");
296 if (flags
& BLOCK_FLAG_COMPAT6
)
297 printf(", compatibility level=6");
299 printf(", backing_file=%s",
302 printf(", backing_fmt=%s",
305 printf(", size=%" PRIu64
" kB\n", size
/ 1024);
306 ret
= bdrv_create2(drv
, filename
, size
/ 512, base_filename
, base_fmt
, flags
);
308 if (ret
== -ENOTSUP
) {
309 error("Formatting or formatting option not supported for file format '%s'", fmt
);
310 } else if (ret
== -EFBIG
) {
311 error("The image size is too large for file format '%s'", fmt
);
313 error("Error while formatting");
319 static int img_check(int argc
, char **argv
)
322 const char *filename
, *fmt
;
324 BlockDriverState
*bs
;
328 c
= getopt(argc
, argv
, "f:h");
342 filename
= argv
[optind
++];
346 error("Not enough memory");
348 drv
= bdrv_find_format(fmt
);
350 error("Unknown file format '%s'", fmt
);
354 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
355 error("Could not open '%s'", filename
);
357 ret
= bdrv_check(bs
);
360 printf("No errors were found on the image.\n");
363 error("This image format does not support checks");
367 error("An error occurred during the check");
369 printf("%d errors were found on the image.\n", ret
);
378 static int img_commit(int argc
, char **argv
)
381 const char *filename
, *fmt
;
383 BlockDriverState
*bs
;
387 c
= getopt(argc
, argv
, "f:h");
401 filename
= argv
[optind
++];
405 error("Not enough memory");
407 drv
= bdrv_find_format(fmt
);
409 error("Unknown file format '%s'", fmt
);
413 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
414 error("Could not open '%s'", filename
);
416 ret
= bdrv_commit(bs
);
419 printf("Image committed.\n");
422 error("No disk inserted");
425 error("Image is read-only");
428 error("Image is already committed");
431 error("Error while committing image");
439 static int is_not_zero(const uint8_t *sector
, int len
)
443 for(i
= 0;i
< len
; i
++) {
444 if (((uint32_t *)sector
)[i
] != 0)
451 * Returns true iff the first sector pointed to by 'buf' contains at least
454 * 'pnum' is set to the number of sectors (including and immediately following
455 * the first one) that are known to be in the same allocated/unallocated state.
457 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
465 v
= is_not_zero(buf
, 512);
466 for(i
= 1; i
< n
; i
++) {
468 if (v
!= is_not_zero(buf
, 512))
475 #define IO_BUF_SIZE 65536
477 static int img_convert(int argc
, char **argv
)
479 int c
, ret
, n
, n1
, bs_n
, bs_i
, flags
, cluster_size
, cluster_sectors
;
480 const char *fmt
, *out_fmt
, *out_baseimg
, *out_filename
;
482 BlockDriverState
**bs
, *out_bs
;
483 int64_t total_sectors
, nb_sectors
, sector_num
, bs_offset
;
485 uint8_t buf
[IO_BUF_SIZE
];
494 c
= getopt(argc
, argv
, "f:O:B:hce6");
508 out_baseimg
= optarg
;
511 flags
|= BLOCK_FLAG_COMPRESS
;
514 flags
|= BLOCK_FLAG_ENCRYPT
;
517 flags
|= BLOCK_FLAG_COMPAT6
;
522 bs_n
= argc
- optind
- 1;
523 if (bs_n
< 1) help();
525 out_filename
= argv
[argc
- 1];
527 if (bs_n
> 1 && out_baseimg
)
528 error("-B makes no sense when concatenating multiple input images");
530 bs
= calloc(bs_n
, sizeof(BlockDriverState
*));
532 error("Out of memory");
535 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++) {
536 bs
[bs_i
] = bdrv_new_open(argv
[optind
+ bs_i
], fmt
);
538 error("Could not open '%s'", argv
[optind
+ bs_i
]);
539 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
540 total_sectors
+= bs_sectors
;
543 drv
= bdrv_find_format(out_fmt
);
545 error("Unknown file format '%s'", out_fmt
);
546 if (flags
& BLOCK_FLAG_COMPRESS
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
547 error("Compression not supported for this file format");
548 if (flags
& BLOCK_FLAG_ENCRYPT
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
549 error("Encryption not supported for this file format");
550 if (flags
& BLOCK_FLAG_COMPAT6
&& drv
!= &bdrv_vmdk
)
551 error("Alternative compatibility level not supported for this file format");
552 if (flags
& BLOCK_FLAG_ENCRYPT
&& flags
& BLOCK_FLAG_COMPRESS
)
553 error("Compression and encryption not supported at the same time");
555 ret
= bdrv_create(drv
, out_filename
, total_sectors
, out_baseimg
, flags
);
557 if (ret
== -ENOTSUP
) {
558 error("Formatting not supported for file format '%s'", out_fmt
);
559 } else if (ret
== -EFBIG
) {
560 error("The image size is too large for file format '%s'", out_fmt
);
562 error("Error while formatting '%s'", out_filename
);
566 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
570 bdrv_get_geometry(bs
[0], &bs_sectors
);
572 if (flags
& BLOCK_FLAG_COMPRESS
) {
573 if (bdrv_get_info(out_bs
, &bdi
) < 0)
574 error("could not get block driver info");
575 cluster_size
= bdi
.cluster_size
;
576 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
577 error("invalid cluster size");
578 cluster_sectors
= cluster_size
>> 9;
585 nb_sectors
= total_sectors
- sector_num
;
588 if (nb_sectors
>= cluster_sectors
)
593 bs_num
= sector_num
- bs_offset
;
594 assert (bs_num
>= 0);
597 while (remainder
> 0) {
599 while (bs_num
== bs_sectors
) {
601 assert (bs_i
< bs_n
);
602 bs_offset
+= bs_sectors
;
603 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
605 /* printf("changing part: sector_num=%lld, "
606 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
607 sector_num, bs_i, bs_offset, bs_sectors); */
609 assert (bs_num
< bs_sectors
);
611 nlow
= (remainder
> bs_sectors
- bs_num
) ? bs_sectors
- bs_num
: remainder
;
613 if (bdrv_read(bs
[bs_i
], bs_num
, buf2
, nlow
) < 0)
614 error("error while reading");
621 assert (remainder
== 0);
623 if (n
< cluster_sectors
)
624 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
625 if (is_not_zero(buf
, cluster_size
)) {
626 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
627 cluster_sectors
) != 0)
628 error("error while compressing sector %" PRId64
,
633 /* signal EOF to align */
634 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
636 sector_num
= 0; // total number of sectors converted so far
638 nb_sectors
= total_sectors
- sector_num
;
641 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
642 n
= (IO_BUF_SIZE
/ 512);
646 while (sector_num
- bs_offset
>= bs_sectors
) {
648 assert (bs_i
< bs_n
);
649 bs_offset
+= bs_sectors
;
650 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
651 /* printf("changing part: sector_num=%lld, bs_i=%d, "
652 "bs_offset=%lld, bs_sectors=%lld\n",
653 sector_num, bs_i, bs_offset, bs_sectors); */
656 if (n
> bs_offset
+ bs_sectors
- sector_num
)
657 n
= bs_offset
+ bs_sectors
- sector_num
;
659 if (drv
!= &bdrv_host_device
) {
660 if (!bdrv_is_allocated(bs
[bs_i
], sector_num
- bs_offset
,
665 /* The next 'n1' sectors are allocated in the input image. Copy
666 only those as they may be followed by unallocated sectors. */
672 if (bdrv_read(bs
[bs_i
], sector_num
- bs_offset
, buf
, n
) < 0)
673 error("error while reading");
674 /* NOTE: at the same time we convert, we do not write zero
675 sectors to have a chance to compress the image. Ideally, we
676 should add a specific call to have the info to go faster */
679 /* If the output image is being created as a copy on write image,
680 copy all sectors even the ones containing only NUL bytes,
681 because they may differ from the sectors in the base image.
683 If the output is to a host device, we also write out
684 sectors that are entirely 0, since whatever data was
685 already there is garbage, not 0s. */
686 if (drv
== &bdrv_host_device
|| out_baseimg
||
687 is_allocated_sectors(buf1
, n
, &n1
)) {
688 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
689 error("error while writing");
698 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++)
699 bdrv_delete(bs
[bs_i
]);
705 static int64_t get_allocated_file_size(const char *filename
)
707 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
708 get_compressed_t get_compressed
;
711 /* WinNT support GetCompressedFileSize to determine allocate size */
712 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
713 if (get_compressed
) {
715 low
= get_compressed(filename
, &high
);
716 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
717 return (((int64_t) high
) << 32) + low
;
720 if (_stati64(filename
, &st
) < 0)
725 static int64_t get_allocated_file_size(const char *filename
)
728 if (stat(filename
, &st
) < 0)
730 return (int64_t)st
.st_blocks
* 512;
734 static void dump_snapshots(BlockDriverState
*bs
)
736 QEMUSnapshotInfo
*sn_tab
, *sn
;
740 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
743 printf("Snapshot list:\n");
744 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
745 for(i
= 0; i
< nb_sns
; i
++) {
747 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
752 static int img_info(int argc
, char **argv
)
755 const char *filename
, *fmt
;
757 BlockDriverState
*bs
;
758 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
759 uint64_t total_sectors
;
760 int64_t allocated_size
;
761 char backing_filename
[1024];
762 char backing_filename2
[1024];
767 c
= getopt(argc
, argv
, "f:h");
781 filename
= argv
[optind
++];
785 error("Not enough memory");
787 drv
= bdrv_find_format(fmt
);
789 error("Unknown file format '%s'", fmt
);
793 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
794 error("Could not open '%s'", filename
);
796 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
797 bdrv_get_geometry(bs
, &total_sectors
);
798 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
799 allocated_size
= get_allocated_file_size(filename
);
800 if (allocated_size
< 0)
801 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
803 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
807 "virtual size: %s (%" PRId64
" bytes)\n"
809 filename
, fmt_name
, size_buf
,
810 (total_sectors
* 512),
812 if (bdrv_is_encrypted(bs
))
813 printf("encrypted: yes\n");
814 if (bdrv_get_info(bs
, &bdi
) >= 0) {
815 if (bdi
.cluster_size
!= 0)
816 printf("cluster_size: %d\n", bdi
.cluster_size
);
818 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
819 if (backing_filename
[0] != '\0') {
820 path_combine(backing_filename2
, sizeof(backing_filename2
),
821 filename
, backing_filename
);
822 printf("backing file: %s (actual path: %s)\n",
831 #define SNAPSHOT_LIST 1
832 #define SNAPSHOT_CREATE 2
833 #define SNAPSHOT_APPLY 3
834 #define SNAPSHOT_DELETE 4
836 static void img_snapshot(int argc
, char **argv
)
838 BlockDriverState
*bs
;
840 char *filename
, *snapshot_name
= NULL
;
845 /* Parse commandline parameters */
847 c
= getopt(argc
, argv
, "la:c:d:h");
859 action
= SNAPSHOT_LIST
;
866 action
= SNAPSHOT_APPLY
;
867 snapshot_name
= optarg
;
874 action
= SNAPSHOT_CREATE
;
875 snapshot_name
= optarg
;
882 action
= SNAPSHOT_DELETE
;
883 snapshot_name
= optarg
;
890 filename
= argv
[optind
++];
895 error("Not enough memory");
897 if (bdrv_open2(bs
, filename
, 0, NULL
) < 0) {
898 error("Could not open '%s'", filename
);
901 /* Perform the requested action */
907 case SNAPSHOT_CREATE
:
908 memset(&sn
, 0, sizeof(sn
));
909 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
911 qemu_gettimeofday(&tv
);
912 sn
.date_sec
= tv
.tv_sec
;
913 sn
.date_nsec
= tv
.tv_usec
* 1000;
915 ret
= bdrv_snapshot_create(bs
, &sn
);
917 error("Could not create snapshot '%s': %d (%s)",
918 snapshot_name
, ret
, strerror(-ret
));
922 ret
= bdrv_snapshot_goto(bs
, snapshot_name
);
924 error("Could not apply snapshot '%s': %d (%s)",
925 snapshot_name
, ret
, strerror(-ret
));
928 case SNAPSHOT_DELETE
:
929 ret
= bdrv_snapshot_delete(bs
, snapshot_name
);
931 error("Could not delete snapshot '%s': %d (%s)",
932 snapshot_name
, ret
, strerror(-ret
));
940 int main(int argc
, char **argv
)
949 if (!strcmp(cmd
, "create")) {
950 img_create(argc
, argv
);
951 } else if (!strcmp(cmd
, "check")) {
952 img_check(argc
, argv
);
953 } else if (!strcmp(cmd
, "commit")) {
954 img_commit(argc
, argv
);
955 } else if (!strcmp(cmd
, "convert")) {
956 img_convert(argc
, argv
);
957 } else if (!strcmp(cmd
, "info")) {
958 img_info(argc
, argv
);
959 } else if (!strcmp(cmd
, "snapshot")) {
960 img_snapshot(argc
, argv
);