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 "qemu-option.h"
27 #include "block_int.h"
34 typedef struct img_cmd_t
{
36 int (*handler
)(int argc
, char **argv
);
39 /* Default to cache=writeback as data integrity is not important for qemu-tcg. */
40 #define BRDV_O_FLAGS BDRV_O_CACHE_WB
42 static void QEMU_NORETURN
error(const char *fmt
, ...)
46 fprintf(stderr
, "qemu-img: ");
47 vfprintf(stderr
, fmt
, ap
);
48 fprintf(stderr
, "\n");
53 static void format_print(void *opaque
, const char *name
)
58 /* Please keep in synch with qemu-img.texi */
59 static void help(void)
61 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2008 Fabrice Bellard\n"
62 "usage: qemu-img command [command options]\n"
63 "QEMU disk image utility\n"
66 #define DEF(option, callback, arg_string) \
68 #include "qemu-img-cmds.h"
72 "Command parameters:\n"
73 " 'filename' is a disk image filename\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 " 'options' is a comma separated list of format specific options in a\n"
81 " name=value format. Use -o ? for an overview of the options supported by the\n"
83 " '-c' indicates that target image must be compressed (qcow format only)\n"
84 " '-h' with or without a command shows this help and lists the supported formats\n"
86 "Parameters to snapshot subcommand:\n"
87 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
88 " '-a' applies a snapshot (revert disk to saved state)\n"
89 " '-c' creates a snapshot\n"
90 " '-d' deletes a snapshot\n"
91 " '-l' lists all snapshots in the given image\n"
93 printf("\nSupported formats:");
94 bdrv_iterate_format(format_print
, NULL
);
100 /* XXX: put correct support for win32 */
101 static int read_password(char *buf
, int buf_size
)
104 printf("Password: ");
111 if (i
< (buf_size
- 1))
122 static struct termios oldtty
;
124 static void term_exit(void)
126 tcsetattr (0, TCSANOW
, &oldtty
);
129 static void term_init(void)
136 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
137 |INLCR
|IGNCR
|ICRNL
|IXON
);
138 tty
.c_oflag
|= OPOST
;
139 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
140 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
145 tcsetattr (0, TCSANOW
, &tty
);
150 static int read_password(char *buf
, int buf_size
)
155 printf("password: ");
160 ret
= read(0, &ch
, 1);
162 if (errno
== EAGAIN
|| errno
== EINTR
) {
168 } else if (ret
== 0) {
176 if (i
< (buf_size
- 1))
187 static BlockDriverState
*bdrv_new_open(const char *filename
,
190 BlockDriverState
*bs
;
196 error("Not enough memory");
198 drv
= bdrv_find_format(fmt
);
200 error("Unknown file format '%s'", fmt
);
204 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
205 error("Could not open '%s'", filename
);
207 if (bdrv_is_encrypted(bs
)) {
208 printf("Disk image '%s' is encrypted.\n", filename
);
209 if (read_password(password
, sizeof(password
)) < 0)
210 error("No password given");
211 if (bdrv_set_key(bs
, password
) < 0)
212 error("invalid password");
217 static void add_old_style_options(const char *fmt
, QEMUOptionParameter
*list
,
218 int flags
, const char *base_filename
, const char *base_fmt
)
220 if (flags
& BLOCK_FLAG_ENCRYPT
) {
221 if (set_option_parameter(list
, BLOCK_OPT_ENCRYPT
, "on")) {
222 error("Encryption not supported for file format '%s'", fmt
);
225 if (flags
& BLOCK_FLAG_COMPAT6
) {
226 if (set_option_parameter(list
, BLOCK_OPT_COMPAT6
, "on")) {
227 error("VMDK version 6 not supported for file format '%s'", fmt
);
232 if (set_option_parameter(list
, BLOCK_OPT_BACKING_FILE
, base_filename
)) {
233 error("Backing file not supported for file format '%s'", fmt
);
237 if (set_option_parameter(list
, BLOCK_OPT_BACKING_FMT
, base_fmt
)) {
238 error("Backing file format not supported for file format '%s'", fmt
);
243 static int img_create(int argc
, char **argv
)
246 const char *fmt
= "raw";
247 const char *base_fmt
= NULL
;
248 const char *filename
;
249 const char *base_filename
= NULL
;
251 QEMUOptionParameter
*param
= NULL
;
252 char *options
= NULL
;
256 c
= getopt(argc
, argv
, "F:b:f:he6o:");
267 base_filename
= optarg
;
273 flags
|= BLOCK_FLAG_ENCRYPT
;
276 flags
|= BLOCK_FLAG_COMPAT6
;
284 /* Find driver and parse its options */
285 drv
= bdrv_find_format(fmt
);
287 error("Unknown file format '%s'", fmt
);
289 if (options
&& !strcmp(options
, "?")) {
290 print_option_help(drv
->create_options
);
294 /* Create parameter list with default values */
295 param
= parse_option_parameters("", drv
->create_options
, param
);
296 set_option_parameter_int(param
, BLOCK_OPT_SIZE
, -1);
298 /* Parse -o options */
300 param
= parse_option_parameters(options
, drv
->create_options
, param
);
302 error("Invalid options for file format '%s'.", fmt
);
306 /* Get the filename */
309 filename
= argv
[optind
++];
311 /* Add size to parameters */
313 set_option_parameter(param
, BLOCK_OPT_SIZE
, argv
[optind
++]);
316 /* Add old-style options to parameters */
317 add_old_style_options(fmt
, param
, flags
, base_filename
, base_fmt
);
319 // The size for the image must always be specified, with one exception:
320 // If we are using a backing file, we can obtain the size from there
321 if (get_option_parameter(param
, BLOCK_OPT_SIZE
)->value
.n
== -1) {
323 QEMUOptionParameter
*backing_file
=
324 get_option_parameter(param
, BLOCK_OPT_BACKING_FILE
);
325 QEMUOptionParameter
*backing_fmt
=
326 get_option_parameter(param
, BLOCK_OPT_BACKING_FMT
);
328 if (backing_file
&& backing_file
->value
.s
) {
329 BlockDriverState
*bs
;
331 const char *fmt
= NULL
;
334 if (backing_fmt
&& backing_fmt
->value
.s
) {
335 if (bdrv_find_format(backing_fmt
->value
.s
)) {
336 fmt
= backing_fmt
->value
.s
;
338 error("Unknown backing file format '%s'",
339 backing_fmt
->value
.s
);
343 bs
= bdrv_new_open(backing_file
->value
.s
, fmt
);
344 bdrv_get_geometry(bs
, &size
);
348 snprintf(buf
, sizeof(buf
), "%" PRId64
, size
);
349 set_option_parameter(param
, BLOCK_OPT_SIZE
, buf
);
351 error("Image creation needs a size parameter");
355 printf("Formatting '%s', fmt=%s ", filename
, fmt
);
356 print_option_parameters(param
);
359 ret
= bdrv_create(drv
, filename
, param
);
360 free_option_parameters(param
);
363 if (ret
== -ENOTSUP
) {
364 error("Formatting or formatting option not supported for file format '%s'", fmt
);
365 } else if (ret
== -EFBIG
) {
366 error("The image size is too large for file format '%s'", fmt
);
368 error("Error while formatting");
374 static int img_check(int argc
, char **argv
)
377 const char *filename
, *fmt
;
379 BlockDriverState
*bs
;
383 c
= getopt(argc
, argv
, "f:h");
397 filename
= argv
[optind
++];
401 error("Not enough memory");
403 drv
= bdrv_find_format(fmt
);
405 error("Unknown file format '%s'", fmt
);
409 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
410 error("Could not open '%s'", filename
);
412 ret
= bdrv_check(bs
);
415 printf("No errors were found on the image.\n");
418 error("This image format does not support checks");
422 error("An error occurred during the check");
424 printf("%d errors were found on the image.\n", ret
);
433 static int img_commit(int argc
, char **argv
)
436 const char *filename
, *fmt
;
438 BlockDriverState
*bs
;
442 c
= getopt(argc
, argv
, "f:h");
456 filename
= argv
[optind
++];
460 error("Not enough memory");
462 drv
= bdrv_find_format(fmt
);
464 error("Unknown file format '%s'", fmt
);
468 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
469 error("Could not open '%s'", filename
);
471 ret
= bdrv_commit(bs
);
474 printf("Image committed.\n");
477 error("No disk inserted");
480 error("Image is read-only");
483 error("Image is already committed");
486 error("Error while committing image");
494 static int is_not_zero(const uint8_t *sector
, int len
)
498 for(i
= 0;i
< len
; i
++) {
499 if (((uint32_t *)sector
)[i
] != 0)
506 * Returns true iff the first sector pointed to by 'buf' contains at least
509 * 'pnum' is set to the number of sectors (including and immediately following
510 * the first one) that are known to be in the same allocated/unallocated state.
512 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
520 v
= is_not_zero(buf
, 512);
521 for(i
= 1; i
< n
; i
++) {
523 if (v
!= is_not_zero(buf
, 512))
530 #define IO_BUF_SIZE (2 * 1024 * 1024)
532 static int img_convert(int argc
, char **argv
)
534 int c
, ret
, n
, n1
, bs_n
, bs_i
, flags
, cluster_size
, cluster_sectors
;
535 const char *fmt
, *out_fmt
, *out_baseimg
, *out_filename
;
537 BlockDriverState
**bs
, *out_bs
;
538 int64_t total_sectors
, nb_sectors
, sector_num
, bs_offset
;
540 uint8_t buf
[IO_BUF_SIZE
];
543 QEMUOptionParameter
*param
= NULL
;
544 char *options
= NULL
;
551 c
= getopt(argc
, argv
, "f:O:B:hce6o:");
565 out_baseimg
= optarg
;
568 flags
|= BLOCK_FLAG_COMPRESS
;
571 flags
|= BLOCK_FLAG_ENCRYPT
;
574 flags
|= BLOCK_FLAG_COMPAT6
;
582 bs_n
= argc
- optind
- 1;
583 if (bs_n
< 1) help();
585 out_filename
= argv
[argc
- 1];
587 if (bs_n
> 1 && out_baseimg
)
588 error("-B makes no sense when concatenating multiple input images");
590 bs
= calloc(bs_n
, sizeof(BlockDriverState
*));
592 error("Out of memory");
595 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++) {
596 bs
[bs_i
] = bdrv_new_open(argv
[optind
+ bs_i
], fmt
);
598 error("Could not open '%s'", argv
[optind
+ bs_i
]);
599 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
600 total_sectors
+= bs_sectors
;
603 /* Find driver and parse its options */
604 drv
= bdrv_find_format(out_fmt
);
606 error("Unknown file format '%s'", out_fmt
);
608 if (options
&& !strcmp(options
, "?")) {
609 print_option_help(drv
->create_options
);
614 param
= parse_option_parameters(options
, drv
->create_options
, param
);
616 error("Invalid options for file format '%s'.", out_fmt
);
619 param
= parse_option_parameters("", drv
->create_options
, param
);
622 set_option_parameter_int(param
, BLOCK_OPT_SIZE
, total_sectors
* 512);
623 add_old_style_options(out_fmt
, param
, flags
, out_baseimg
, NULL
);
625 /* Check if compression is supported */
626 if (flags
& BLOCK_FLAG_COMPRESS
) {
627 QEMUOptionParameter
*encryption
=
628 get_option_parameter(param
, BLOCK_OPT_ENCRYPT
);
630 if (!drv
->bdrv_write_compressed
) {
631 error("Compression not supported for this file format");
634 if (encryption
&& encryption
->value
.n
) {
635 error("Compression and encryption not supported at the same time");
639 /* Create the new image */
640 ret
= bdrv_create(drv
, out_filename
, param
);
641 free_option_parameters(param
);
644 if (ret
== -ENOTSUP
) {
645 error("Formatting not supported for file format '%s'", out_fmt
);
646 } else if (ret
== -EFBIG
) {
647 error("The image size is too large for file format '%s'", out_fmt
);
649 error("Error while formatting '%s'", out_filename
);
653 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
657 bdrv_get_geometry(bs
[0], &bs_sectors
);
659 if (flags
& BLOCK_FLAG_COMPRESS
) {
660 if (bdrv_get_info(out_bs
, &bdi
) < 0)
661 error("could not get block driver info");
662 cluster_size
= bdi
.cluster_size
;
663 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
664 error("invalid cluster size");
665 cluster_sectors
= cluster_size
>> 9;
672 nb_sectors
= total_sectors
- sector_num
;
675 if (nb_sectors
>= cluster_sectors
)
680 bs_num
= sector_num
- bs_offset
;
681 assert (bs_num
>= 0);
684 while (remainder
> 0) {
686 while (bs_num
== bs_sectors
) {
688 assert (bs_i
< bs_n
);
689 bs_offset
+= bs_sectors
;
690 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
692 /* printf("changing part: sector_num=%lld, "
693 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
694 sector_num, bs_i, bs_offset, bs_sectors); */
696 assert (bs_num
< bs_sectors
);
698 nlow
= (remainder
> bs_sectors
- bs_num
) ? bs_sectors
- bs_num
: remainder
;
700 if (bdrv_read(bs
[bs_i
], bs_num
, buf2
, nlow
) < 0)
701 error("error while reading");
708 assert (remainder
== 0);
710 if (n
< cluster_sectors
)
711 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
712 if (is_not_zero(buf
, cluster_size
)) {
713 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
714 cluster_sectors
) != 0)
715 error("error while compressing sector %" PRId64
,
720 /* signal EOF to align */
721 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
723 sector_num
= 0; // total number of sectors converted so far
725 nb_sectors
= total_sectors
- sector_num
;
728 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
729 n
= (IO_BUF_SIZE
/ 512);
733 while (sector_num
- bs_offset
>= bs_sectors
) {
735 assert (bs_i
< bs_n
);
736 bs_offset
+= bs_sectors
;
737 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
738 /* printf("changing part: sector_num=%lld, bs_i=%d, "
739 "bs_offset=%lld, bs_sectors=%lld\n",
740 sector_num, bs_i, bs_offset, bs_sectors); */
743 if (n
> bs_offset
+ bs_sectors
- sector_num
)
744 n
= bs_offset
+ bs_sectors
- sector_num
;
746 if (strcmp(drv
->format_name
, "host_device")) {
747 /* If the output image is being created as a copy on write image,
748 assume that sectors which are unallocated in the input image
749 are present in both the output's and input's base images (no
750 need to copy them). */
752 if (!bdrv_is_allocated(bs
[bs_i
], sector_num
- bs_offset
,
757 /* The next 'n1' sectors are allocated in the input image. Copy
758 only those as they may be followed by unallocated sectors. */
765 if (bdrv_read(bs
[bs_i
], sector_num
- bs_offset
, buf
, n
) < 0)
766 error("error while reading");
767 /* NOTE: at the same time we convert, we do not write zero
768 sectors to have a chance to compress the image. Ideally, we
769 should add a specific call to have the info to go faster */
772 /* If the output image is being created as a copy on write image,
773 copy all sectors even the ones containing only NUL bytes,
774 because they may differ from the sectors in the base image.
776 If the output is to a host device, we also write out
777 sectors that are entirely 0, since whatever data was
778 already there is garbage, not 0s. */
779 if (strcmp(drv
->format_name
, "host_device") == 0 || out_baseimg
||
780 is_allocated_sectors(buf1
, n
, &n1
)) {
781 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
782 error("error while writing");
791 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++)
792 bdrv_delete(bs
[bs_i
]);
798 static int64_t get_allocated_file_size(const char *filename
)
800 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
801 get_compressed_t get_compressed
;
804 /* WinNT support GetCompressedFileSize to determine allocate size */
805 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
806 if (get_compressed
) {
808 low
= get_compressed(filename
, &high
);
809 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
810 return (((int64_t) high
) << 32) + low
;
813 if (_stati64(filename
, &st
) < 0)
818 static int64_t get_allocated_file_size(const char *filename
)
821 if (stat(filename
, &st
) < 0)
823 return (int64_t)st
.st_blocks
* 512;
827 static void dump_snapshots(BlockDriverState
*bs
)
829 QEMUSnapshotInfo
*sn_tab
, *sn
;
833 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
836 printf("Snapshot list:\n");
837 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
838 for(i
= 0; i
< nb_sns
; i
++) {
840 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
845 static int img_info(int argc
, char **argv
)
848 const char *filename
, *fmt
;
850 BlockDriverState
*bs
;
851 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
852 uint64_t total_sectors
;
853 int64_t allocated_size
;
854 char backing_filename
[1024];
855 char backing_filename2
[1024];
860 c
= getopt(argc
, argv
, "f:h");
874 filename
= argv
[optind
++];
878 error("Not enough memory");
880 drv
= bdrv_find_format(fmt
);
882 error("Unknown file format '%s'", fmt
);
886 if (bdrv_open2(bs
, filename
, BRDV_O_FLAGS
, drv
) < 0) {
887 error("Could not open '%s'", filename
);
889 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
890 bdrv_get_geometry(bs
, &total_sectors
);
891 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
892 allocated_size
= get_allocated_file_size(filename
);
893 if (allocated_size
< 0)
894 snprintf(dsize_buf
, sizeof(dsize_buf
), "unavailable");
896 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
900 "virtual size: %s (%" PRId64
" bytes)\n"
902 filename
, fmt_name
, size_buf
,
903 (total_sectors
* 512),
905 if (bdrv_is_encrypted(bs
))
906 printf("encrypted: yes\n");
907 if (bdrv_get_info(bs
, &bdi
) >= 0) {
908 if (bdi
.cluster_size
!= 0)
909 printf("cluster_size: %d\n", bdi
.cluster_size
);
911 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
912 if (backing_filename
[0] != '\0') {
913 path_combine(backing_filename2
, sizeof(backing_filename2
),
914 filename
, backing_filename
);
915 printf("backing file: %s (actual path: %s)\n",
924 #define SNAPSHOT_LIST 1
925 #define SNAPSHOT_CREATE 2
926 #define SNAPSHOT_APPLY 3
927 #define SNAPSHOT_DELETE 4
929 static int img_snapshot(int argc
, char **argv
)
931 BlockDriverState
*bs
;
933 char *filename
, *snapshot_name
= NULL
;
938 /* Parse commandline parameters */
940 c
= getopt(argc
, argv
, "la:c:d:h");
952 action
= SNAPSHOT_LIST
;
959 action
= SNAPSHOT_APPLY
;
960 snapshot_name
= optarg
;
967 action
= SNAPSHOT_CREATE
;
968 snapshot_name
= optarg
;
975 action
= SNAPSHOT_DELETE
;
976 snapshot_name
= optarg
;
983 filename
= argv
[optind
++];
988 error("Not enough memory");
990 if (bdrv_open2(bs
, filename
, 0, NULL
) < 0) {
991 error("Could not open '%s'", filename
);
994 /* Perform the requested action */
1000 case SNAPSHOT_CREATE
:
1001 memset(&sn
, 0, sizeof(sn
));
1002 pstrcpy(sn
.name
, sizeof(sn
.name
), snapshot_name
);
1004 qemu_gettimeofday(&tv
);
1005 sn
.date_sec
= tv
.tv_sec
;
1006 sn
.date_nsec
= tv
.tv_usec
* 1000;
1008 ret
= bdrv_snapshot_create(bs
, &sn
);
1010 error("Could not create snapshot '%s': %d (%s)",
1011 snapshot_name
, ret
, strerror(-ret
));
1014 case SNAPSHOT_APPLY
:
1015 ret
= bdrv_snapshot_goto(bs
, snapshot_name
);
1017 error("Could not apply snapshot '%s': %d (%s)",
1018 snapshot_name
, ret
, strerror(-ret
));
1021 case SNAPSHOT_DELETE
:
1022 ret
= bdrv_snapshot_delete(bs
, snapshot_name
);
1024 error("Could not delete snapshot '%s': %d (%s)",
1025 snapshot_name
, ret
, strerror(-ret
));
1035 static const img_cmd_t img_cmds
[] = {
1036 #define DEF(option, callback, arg_string) \
1037 { option, callback },
1038 #include "qemu-img-cmds.h"
1044 int main(int argc
, char **argv
)
1046 const img_cmd_t
*cmd
;
1047 const char *cmdname
;
1055 /* find the command */
1056 for(cmd
= img_cmds
; cmd
->name
!= NULL
; cmd
++) {
1057 if (!strcmp(cmdname
, cmd
->name
)) {
1058 return cmd
->handler(argc
, argv
);