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"
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 QEMU_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 /* Please keep in synch with qemu-img.texi */
53 static void help(void)
55 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2008 Fabrice Bellard\n"
56 "usage: qemu-img command [command options]\n"
57 "QEMU disk image utility\n"
60 " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n"
61 " commit [-f fmt] filename\n"
62 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
63 " info [-f fmt] filename\n"
64 " snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename\n"
66 "Command parameters:\n"
67 " 'filename' is a disk image filename\n"
68 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
69 " write image; the copy on write image only stores the modified data\n"
70 " 'output_base_image' forces the output image to be created as a copy on write\n"
71 " image of the specified base image; 'output_base_image' should have the same\n"
72 " content as the input's base image, however the path, image format, etc may\n"
74 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
75 " 'size' is the disk image size in kilobytes. Optional suffixes\n"
76 " 'M' (megabyte, 1024 * 1024) and 'G' (gigabyte, 1024 * 1024 * 1024) are\n"
77 " supported any 'k' or 'K' is ignored\n"
78 " 'output_filename' is the destination disk image filename\n"
79 " 'output_fmt' is the destination format\n"
80 " '-c' indicates that target image must be compressed (qcow format only)\n"
81 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
82 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
83 " '-h' with or without a command shows this help and lists the supported formats\n"
85 "Parameters to snapshot subcommand:\n"
86 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
87 " '-a' applies a snapshot (revert disk to saved state)\n"
88 " '-c' creates a snapshot\n"
89 " '-d' deletes a snapshot\n"
90 " '-l' lists all snapshots in the given image\n"
92 printf("\nSupported formats:");
93 bdrv_iterate_format(format_print
, NULL
);
99 /* XXX: put correct support for win32 */
100 static int read_password(char *buf
, int buf_size
)
103 printf("Password: ");
110 if (i
< (buf_size
- 1))
121 static struct termios oldtty
;
123 static void term_exit(void)
125 tcsetattr (0, TCSANOW
, &oldtty
);
128 static void term_init(void)
135 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
136 |INLCR
|IGNCR
|ICRNL
|IXON
);
137 tty
.c_oflag
|= OPOST
;
138 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
139 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
144 tcsetattr (0, TCSANOW
, &tty
);
149 static int read_password(char *buf
, int buf_size
)
154 printf("password: ");
159 ret
= read(0, &ch
, 1);
161 if (errno
== EAGAIN
|| errno
== EINTR
) {
167 } else if (ret
== 0) {
175 if (i
< (buf_size
- 1))
186 static BlockDriverState
*bdrv_new_open(const char *filename
,
189 BlockDriverState
*bs
;
195 error("Not enough memory");
197 drv
= bdrv_find_format(fmt
);
199 error("Unknown file format '%s'", fmt
);
203 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
204 error("Could not open '%s'", filename
);
206 if (bdrv_is_encrypted(bs
)) {
207 printf("Disk image '%s' is encrypted.\n", filename
);
208 if (read_password(password
, sizeof(password
)) < 0)
209 error("No password given");
210 if (bdrv_set_key(bs
, password
) < 0)
211 error("invalid password");
216 static int img_create(int argc
, char **argv
)
219 const char *fmt
= "raw";
220 const char *filename
;
221 const char *base_filename
= NULL
;
229 c
= getopt(argc
, argv
, "b:f:he6");
237 base_filename
= optarg
;
243 flags
|= BLOCK_FLAG_ENCRYPT
;
246 flags
|= BLOCK_FLAG_COMPAT6
;
252 filename
= argv
[optind
++];
255 BlockDriverState
*bs
;
256 bs
= bdrv_new_open(base_filename
, NULL
);
257 bdrv_get_geometry(bs
, &size
);
264 sizef
= strtod(p
, (char **)&p
);
266 size
= (uint64_t)(sizef
* 1024 * 1024);
267 } else if (*p
== 'G') {
268 size
= (uint64_t)(sizef
* 1024 * 1024 * 1024);
269 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
270 size
= (uint64_t)(sizef
* 1024);
275 drv
= bdrv_find_format(fmt
);
277 error("Unknown file format '%s'", fmt
);
278 printf("Formatting '%s', fmt=%s",
280 if (flags
& BLOCK_FLAG_ENCRYPT
)
281 printf(", encrypted");
282 if (flags
& BLOCK_FLAG_COMPAT6
)
283 printf(", compatibility level=6");
285 printf(", backing_file=%s",
288 printf(", size=%" PRIu64
" kB\n", size
/ 1024);
289 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, flags
);
291 if (ret
== -ENOTSUP
) {
292 error("Formatting or formatting option not supported for file format '%s'", fmt
);
294 error("Error while formatting");
300 static int img_commit(int argc
, char **argv
)
303 const char *filename
, *fmt
;
305 BlockDriverState
*bs
;
309 c
= getopt(argc
, argv
, "f:h");
323 filename
= argv
[optind
++];
327 error("Not enough memory");
329 drv
= bdrv_find_format(fmt
);
331 error("Unknown file format '%s'", fmt
);
335 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
336 error("Could not open '%s'", filename
);
338 ret
= bdrv_commit(bs
);
341 printf("Image committed.\n");
344 error("No disk inserted");
347 error("Image is read-only");
350 error("Image is already committed");
353 error("Error while committing image");
361 static int is_not_zero(const uint8_t *sector
, int len
)
365 for(i
= 0;i
< len
; i
++) {
366 if (((uint32_t *)sector
)[i
] != 0)
373 * Returns true iff the first sector pointed to by 'buf' contains at least
376 * 'pnum' is set to the number of sectors (including and immediately following
377 * the first one) that are known to be in the same allocated/unallocated state.
379 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
387 v
= is_not_zero(buf
, 512);
388 for(i
= 1; i
< n
; i
++) {
390 if (v
!= is_not_zero(buf
, 512))
397 #define IO_BUF_SIZE 65536
399 static int img_convert(int argc
, char **argv
)
401 int c
, ret
, n
, n1
, bs_n
, bs_i
, flags
, cluster_size
, cluster_sectors
;
402 const char *fmt
, *out_fmt
, *out_baseimg
, *out_filename
;
404 BlockDriverState
**bs
, *out_bs
;
405 int64_t total_sectors
, nb_sectors
, sector_num
, bs_offset
;
407 uint8_t buf
[IO_BUF_SIZE
];
416 c
= getopt(argc
, argv
, "f:O:B:hce6");
430 out_baseimg
= optarg
;
433 flags
|= BLOCK_FLAG_COMPRESS
;
436 flags
|= BLOCK_FLAG_ENCRYPT
;
439 flags
|= BLOCK_FLAG_COMPAT6
;
444 bs_n
= argc
- optind
- 1;
445 if (bs_n
< 1) help();
447 out_filename
= argv
[argc
- 1];
449 if (bs_n
> 1 && out_baseimg
)
450 error("-B makes no sense when concatenating multiple input images");
452 bs
= calloc(bs_n
, sizeof(BlockDriverState
*));
454 error("Out of memory");
457 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++) {
458 bs
[bs_i
] = bdrv_new_open(argv
[optind
+ bs_i
], fmt
);
460 error("Could not open '%s'", argv
[optind
+ bs_i
]);
461 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
462 total_sectors
+= bs_sectors
;
465 drv
= bdrv_find_format(out_fmt
);
467 error("Unknown file format '%s'", out_fmt
);
468 if (flags
& BLOCK_FLAG_COMPRESS
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
469 error("Compression not supported for this file format");
470 if (flags
& BLOCK_FLAG_ENCRYPT
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
471 error("Encryption not supported for this file format");
472 if (flags
& BLOCK_FLAG_COMPAT6
&& drv
!= &bdrv_vmdk
)
473 error("Alternative compatibility level not supported for this file format");
474 if (flags
& BLOCK_FLAG_ENCRYPT
&& flags
& BLOCK_FLAG_COMPRESS
)
475 error("Compression and encryption not supported at the same time");
477 ret
= bdrv_create(drv
, out_filename
, total_sectors
, out_baseimg
, flags
);
479 if (ret
== -ENOTSUP
) {
480 error("Formatting not supported for file format '%s'", fmt
);
482 error("Error while formatting '%s'", out_filename
);
486 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
490 bdrv_get_geometry(bs
[0], &bs_sectors
);
492 if (flags
& BLOCK_FLAG_COMPRESS
) {
493 if (bdrv_get_info(out_bs
, &bdi
) < 0)
494 error("could not get block driver info");
495 cluster_size
= bdi
.cluster_size
;
496 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
497 error("invalid cluster size");
498 cluster_sectors
= cluster_size
>> 9;
505 nb_sectors
= total_sectors
- sector_num
;
508 if (nb_sectors
>= cluster_sectors
)
513 bs_num
= sector_num
- bs_offset
;
514 assert (bs_num
>= 0);
517 while (remainder
> 0) {
519 while (bs_num
== bs_sectors
) {
521 assert (bs_i
< bs_n
);
522 bs_offset
+= bs_sectors
;
523 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
525 /* printf("changing part: sector_num=%lld, "
526 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
527 sector_num, bs_i, bs_offset, bs_sectors); */
529 assert (bs_num
< bs_sectors
);
531 nlow
= (remainder
> bs_sectors
- bs_num
) ? bs_sectors
- bs_num
: remainder
;
533 if (bdrv_read(bs
[bs_i
], bs_num
, buf2
, nlow
) < 0)
534 error("error while reading");
541 assert (remainder
== 0);
543 if (n
< cluster_sectors
)
544 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
545 if (is_not_zero(buf
, cluster_size
)) {
546 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
547 cluster_sectors
) != 0)
548 error("error while compressing sector %" PRId64
,
553 /* signal EOF to align */
554 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
556 sector_num
= 0; // total number of sectors converted so far
558 nb_sectors
= total_sectors
- sector_num
;
561 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
562 n
= (IO_BUF_SIZE
/ 512);
566 while (sector_num
- bs_offset
>= bs_sectors
) {
568 assert (bs_i
< bs_n
);
569 bs_offset
+= bs_sectors
;
570 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
571 /* printf("changing part: sector_num=%lld, bs_i=%d, "
572 "bs_offset=%lld, bs_sectors=%lld\n",
573 sector_num, bs_i, bs_offset, bs_sectors); */
576 if (n
> bs_offset
+ bs_sectors
- sector_num
)
577 n
= bs_offset
+ bs_sectors
- sector_num
;
579 /* If the output image is being created as a copy on write image,
580 assume that sectors which are unallocated in the input image
581 are present in both the output's and input's base images (no
582 need to copy them). */
584 if (!bdrv_is_allocated(bs
[bs_i
], sector_num
- bs_offset
, n
, &n1
)) {
588 /* The next 'n1' sectors are allocated in the input image. Copy
589 only those as they may be followed by unallocated sectors. */
593 if (bdrv_read(bs
[bs_i
], sector_num
- bs_offset
, buf
, n
) < 0)
594 error("error while reading");
595 /* NOTE: at the same time we convert, we do not write zero
596 sectors to have a chance to compress the image. Ideally, we
597 should add a specific call to have the info to go faster */
600 /* If the output image is being created as a copy on write image,
601 copy all sectors even the ones containing only NUL bytes,
602 because they may differ from the sectors in the base image. */
603 if (out_baseimg
|| is_allocated_sectors(buf1
, n
, &n1
)) {
604 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
605 error("error while writing");
614 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++)
615 bdrv_delete(bs
[bs_i
]);
621 static int64_t get_allocated_file_size(const char *filename
)
623 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
624 get_compressed_t get_compressed
;
627 /* WinNT support GetCompressedFileSize to determine allocate size */
628 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
629 if (get_compressed
) {
631 low
= get_compressed(filename
, &high
);
632 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
633 return (((int64_t) high
) << 32) + low
;
636 if (_stati64(filename
, &st
) < 0)
641 static int64_t get_allocated_file_size(const char *filename
)
644 if (stat(filename
, &st
) < 0)
646 return (int64_t)st
.st_blocks
* 512;
650 static void dump_snapshots(BlockDriverState
*bs
)
652 QEMUSnapshotInfo
*sn_tab
, *sn
;
656 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
659 printf("Snapshot list:\n");
660 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
661 for(i
= 0; i
< nb_sns
; i
++) {
663 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
668 static int img_info(int argc
, char **argv
)
671 const char *filename
, *fmt
;
673 BlockDriverState
*bs
;
674 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
675 uint64_t total_sectors
;
676 int64_t allocated_size
;
677 char backing_filename
[1024];
678 char backing_filename2
[1024];
683 c
= getopt(argc
, argv
, "f:h");
697 filename
= argv
[optind
++];
701 error("Not enough memory");
703 drv
= bdrv_find_format(fmt
);
705 error("Unknown file format '%s'", fmt
);
709 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
710 error("Could not open '%s'", filename
);
712 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
713 bdrv_get_geometry(bs
, &total_sectors
);
714 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
715 allocated_size
= get_allocated_file_size(filename
);
716 if (allocated_size
< 0)
717 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
719 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
723 "virtual size: %s (%" PRId64
" bytes)\n"
725 filename
, fmt_name
, size_buf
,
726 (total_sectors
* 512),
728 if (bdrv_is_encrypted(bs
))
729 printf("encrypted: yes\n");
730 if (bdrv_get_info(bs
, &bdi
) >= 0) {
731 if (bdi
.cluster_size
!= 0)
732 printf("cluster_size: %d\n", bdi
.cluster_size
);
734 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
735 if (backing_filename
[0] != '\0') {
736 path_combine(backing_filename2
, sizeof(backing_filename2
),
737 filename
, backing_filename
);
738 printf("backing file: %s (actual path: %s)\n",
747 #define SNAPSHOT_LIST 1
748 #define SNAPSHOT_CREATE 2
749 #define SNAPSHOT_APPLY 3
750 #define SNAPSHOT_DELETE 4
752 static void img_snapshot(int argc
, char **argv
)
754 BlockDriverState
*bs
;
756 char *filename
, *snapshot_name
= NULL
;
761 /* Parse commandline parameters */
763 c
= getopt(argc
, argv
, "la:c:d:h");
775 action
= SNAPSHOT_LIST
;
782 action
= SNAPSHOT_APPLY
;
783 snapshot_name
= optarg
;
790 action
= SNAPSHOT_CREATE
;
791 snapshot_name
= optarg
;
798 action
= SNAPSHOT_DELETE
;
799 snapshot_name
= optarg
;
806 filename
= argv
[optind
++];
811 error("Not enough memory");
813 if (bdrv_open2(bs
, filename
, 0, NULL
) < 0) {
814 error("Could not open '%s'", filename
);
817 /* Perform the requested action */
823 case SNAPSHOT_CREATE
:
824 memset(&sn
, 0, sizeof(sn
));
825 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
827 qemu_gettimeofday(&tv
);
828 sn
.date_sec
= tv
.tv_sec
;
829 sn
.date_nsec
= tv
.tv_usec
* 1000;
831 ret
= bdrv_snapshot_create(bs
, &sn
);
833 error("Could not create snapshot '%s': %d (%s)",
834 snapshot_name
, ret
, strerror(-ret
));
838 ret
= bdrv_snapshot_goto(bs
, snapshot_name
);
840 error("Could not apply snapshot '%s': %d (%s)",
841 snapshot_name
, ret
, strerror(-ret
));
844 case SNAPSHOT_DELETE
:
845 ret
= bdrv_snapshot_delete(bs
, snapshot_name
);
847 error("Could not delete snapshot '%s': %d (%s)",
848 snapshot_name
, ret
, strerror(-ret
));
856 int main(int argc
, char **argv
)
865 if (!strcmp(cmd
, "create")) {
866 img_create(argc
, argv
);
867 } else if (!strcmp(cmd
, "commit")) {
868 img_commit(argc
, argv
);
869 } else if (!strcmp(cmd
, "convert")) {
870 img_convert(argc
, argv
);
871 } else if (!strcmp(cmd
, "info")) {
872 img_info(argc
, argv
);
873 } else if (!strcmp(cmd
, "snapshot")) {
874 img_snapshot(argc
, argv
);