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] 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 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
93 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
94 " and 'G' (gigabyte) are supported\n"
95 " 'output_filename' is the destination disk image filename\n"
96 " 'output_fmt' is the destination format\n"
97 " '-c' indicates that target image must be compressed (qcow format only)\n"
98 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
99 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
101 printf("\nSupported format:");
102 bdrv_iterate_format(format_print
, NULL
);
108 /* XXX: put correct support for win32 */
109 static int read_password(char *buf
, int buf_size
)
112 printf("Password: ");
119 if (i
< (buf_size
- 1))
130 static struct termios oldtty
;
132 static void term_exit(void)
134 tcsetattr (0, TCSANOW
, &oldtty
);
137 static void term_init(void)
144 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
145 |INLCR
|IGNCR
|ICRNL
|IXON
);
146 tty
.c_oflag
|= OPOST
;
147 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
148 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
153 tcsetattr (0, TCSANOW
, &tty
);
158 static int read_password(char *buf
, int buf_size
)
163 printf("password: ");
168 ret
= read(0, &ch
, 1);
170 if (errno
== EAGAIN
|| errno
== EINTR
) {
176 } else if (ret
== 0) {
184 if (i
< (buf_size
- 1))
195 static BlockDriverState
*bdrv_new_open(const char *filename
,
198 BlockDriverState
*bs
;
204 error("Not enough memory");
206 drv
= bdrv_find_format(fmt
);
208 error("Unknown file format '%s'", fmt
);
212 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
213 error("Could not open '%s'", filename
);
215 if (bdrv_is_encrypted(bs
)) {
216 printf("Disk image '%s' is encrypted.\n", filename
);
217 if (read_password(password
, sizeof(password
)) < 0)
218 error("No password given");
219 if (bdrv_set_key(bs
, password
) < 0)
220 error("invalid password");
225 static int img_create(int argc
, char **argv
)
228 const char *fmt
= "raw";
229 const char *filename
;
230 const char *base_filename
= NULL
;
237 c
= getopt(argc
, argv
, "b:f:he6");
245 base_filename
= optarg
;
251 flags
|= BLOCK_FLAG_ENCRYPT
;
254 flags
|= BLOCK_FLAG_COMPAT6
;
260 filename
= argv
[optind
++];
263 BlockDriverState
*bs
;
264 bs
= bdrv_new_open(base_filename
, NULL
);
265 bdrv_get_geometry(bs
, &size
);
272 size
= strtoul(p
, (char **)&p
, 0);
275 } else if (*p
== 'G') {
276 size
*= 1024 * 1024 * 1024;
277 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
283 drv
= bdrv_find_format(fmt
);
285 error("Unknown file format '%s'", fmt
);
286 printf("Formatting '%s', fmt=%s",
288 if (flags
& BLOCK_FLAG_ENCRYPT
)
289 printf(", encrypted");
290 if (flags
& BLOCK_FLAG_COMPAT6
)
291 printf(", compatibility level=6");
293 printf(", backing_file=%s",
296 printf(", size=%" PRIu64
" kB\n", size
/ 1024);
297 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, flags
);
299 if (ret
== -ENOTSUP
) {
300 error("Formatting or formatting option not supported for file format '%s'", fmt
);
302 error("Error while formatting");
308 static int img_commit(int argc
, char **argv
)
311 const char *filename
, *fmt
;
313 BlockDriverState
*bs
;
317 c
= getopt(argc
, argv
, "f:h");
331 filename
= argv
[optind
++];
335 error("Not enough memory");
337 drv
= bdrv_find_format(fmt
);
339 error("Unknown file format '%s'", fmt
);
343 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
344 error("Could not open '%s'", filename
);
346 ret
= bdrv_commit(bs
);
349 printf("Image committed.\n");
352 error("No disk inserted");
355 error("Image is read-only");
358 error("Image is already committed");
361 error("Error while committing image");
369 static int is_not_zero(const uint8_t *sector
, int len
)
373 for(i
= 0;i
< len
; i
++) {
374 if (((uint32_t *)sector
)[i
] != 0)
380 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
388 v
= is_not_zero(buf
, 512);
389 for(i
= 1; i
< n
; i
++) {
391 if (v
!= is_not_zero(buf
, 512))
398 #define IO_BUF_SIZE 65536
400 static int img_convert(int argc
, char **argv
)
402 int c
, ret
, n
, n1
, bs_n
, bs_i
, flags
, cluster_size
, cluster_sectors
;
403 const char *fmt
, *out_fmt
, *out_filename
;
405 BlockDriverState
**bs
, *out_bs
;
406 int64_t total_sectors
, nb_sectors
, sector_num
, bs_offset
;
408 uint8_t buf
[IO_BUF_SIZE
];
416 c
= getopt(argc
, argv
, "f:O:hce6");
430 flags
|= BLOCK_FLAG_COMPRESS
;
433 flags
|= BLOCK_FLAG_ENCRYPT
;
436 flags
|= BLOCK_FLAG_COMPAT6
;
441 bs_n
= argc
- optind
- 1;
442 if (bs_n
< 1) help();
444 out_filename
= argv
[argc
- 1];
446 bs
= calloc(bs_n
, sizeof(BlockDriverState
*));
448 error("Out of memory");
451 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++) {
452 bs
[bs_i
] = bdrv_new_open(argv
[optind
+ bs_i
], fmt
);
454 error("Could not open '%s'", argv
[optind
+ bs_i
]);
455 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
456 total_sectors
+= bs_sectors
;
459 drv
= bdrv_find_format(out_fmt
);
461 error("Unknown file format '%s'", out_fmt
);
462 if (flags
& BLOCK_FLAG_COMPRESS
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
463 error("Compression not supported for this file format");
464 if (flags
& BLOCK_FLAG_ENCRYPT
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
465 error("Encryption not supported for this file format");
466 if (flags
& BLOCK_FLAG_COMPAT6
&& drv
!= &bdrv_vmdk
)
467 error("Alternative compatibility level not supported for this file format");
468 if (flags
& BLOCK_FLAG_ENCRYPT
&& flags
& BLOCK_FLAG_COMPRESS
)
469 error("Compression and encryption not supported at the same time");
471 ret
= bdrv_create(drv
, out_filename
, total_sectors
, NULL
, flags
);
473 if (ret
== -ENOTSUP
) {
474 error("Formatting not supported for file format '%s'", fmt
);
476 error("Error while formatting '%s'", out_filename
);
480 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
484 bdrv_get_geometry(bs
[0], &bs_sectors
);
486 if (flags
& BLOCK_FLAG_COMPRESS
) {
487 if (bdrv_get_info(out_bs
, &bdi
) < 0)
488 error("could not get block driver info");
489 cluster_size
= bdi
.cluster_size
;
490 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
491 error("invalid cluster size");
492 cluster_sectors
= cluster_size
>> 9;
499 nb_sectors
= total_sectors
- sector_num
;
502 if (nb_sectors
>= cluster_sectors
)
507 bs_num
= sector_num
- bs_offset
;
508 assert (bs_num
>= 0);
511 while (remainder
> 0) {
513 while (bs_num
== bs_sectors
) {
515 assert (bs_i
< bs_n
);
516 bs_offset
+= bs_sectors
;
517 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
519 /* printf("changing part: sector_num=%lld, "
520 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
521 sector_num, bs_i, bs_offset, bs_sectors); */
523 assert (bs_num
< bs_sectors
);
525 nlow
= (remainder
> bs_sectors
- bs_num
) ? bs_sectors
- bs_num
: remainder
;
527 if (bdrv_read(bs
[bs_i
], bs_num
, buf2
, nlow
) < 0)
528 error("error while reading");
535 assert (remainder
== 0);
537 if (n
< cluster_sectors
)
538 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
539 if (is_not_zero(buf
, cluster_size
)) {
540 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
541 cluster_sectors
) != 0)
542 error("error while compressing sector %" PRId64
,
547 /* signal EOF to align */
548 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
552 nb_sectors
= total_sectors
- sector_num
;
555 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
556 n
= (IO_BUF_SIZE
/ 512);
560 while (sector_num
- bs_offset
>= bs_sectors
) {
562 assert (bs_i
< bs_n
);
563 bs_offset
+= bs_sectors
;
564 bdrv_get_geometry(bs
[bs_i
], &bs_sectors
);
565 /* printf("changing part: sector_num=%lld, bs_i=%d, "
566 "bs_offset=%lld, bs_sectors=%lld\n",
567 sector_num, bs_i, bs_offset, bs_sectors); */
570 if (n
> bs_offset
+ bs_sectors
- sector_num
)
571 n
= bs_offset
+ bs_sectors
- sector_num
;
573 if (bdrv_read(bs
[bs_i
], sector_num
- bs_offset
, buf
, n
) < 0)
574 error("error while reading");
575 /* NOTE: at the same time we convert, we do not write zero
576 sectors to have a chance to compress the image. Ideally, we
577 should add a specific call to have the info to go faster */
580 if (is_allocated_sectors(buf1
, n
, &n1
)) {
581 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
582 error("error while writing");
591 for (bs_i
= 0; bs_i
< bs_n
; bs_i
++)
592 bdrv_delete(bs
[bs_i
]);
598 static int64_t get_allocated_file_size(const char *filename
)
600 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
601 get_compressed_t get_compressed
;
604 /* WinNT support GetCompressedFileSize to determine allocate size */
605 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
606 if (get_compressed
) {
608 low
= get_compressed(filename
, &high
);
609 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
610 return (((int64_t) high
) << 32) + low
;
613 if (_stati64(filename
, &st
) < 0)
618 static int64_t get_allocated_file_size(const char *filename
)
621 if (stat(filename
, &st
) < 0)
623 return (int64_t)st
.st_blocks
* 512;
627 static void dump_snapshots(BlockDriverState
*bs
)
629 QEMUSnapshotInfo
*sn_tab
, *sn
;
633 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
636 printf("Snapshot list:\n");
637 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
638 for(i
= 0; i
< nb_sns
; i
++) {
640 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
645 static int img_info(int argc
, char **argv
)
648 const char *filename
, *fmt
;
650 BlockDriverState
*bs
;
651 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
652 uint64_t total_sectors
;
653 int64_t allocated_size
;
654 char backing_filename
[1024];
655 char backing_filename2
[1024];
660 c
= getopt(argc
, argv
, "f:h");
674 filename
= argv
[optind
++];
678 error("Not enough memory");
680 drv
= bdrv_find_format(fmt
);
682 error("Unknown file format '%s'", fmt
);
686 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
687 error("Could not open '%s'", filename
);
689 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
690 bdrv_get_geometry(bs
, &total_sectors
);
691 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
692 allocated_size
= get_allocated_file_size(filename
);
693 if (allocated_size
< 0)
694 sprintf(dsize_buf
, "unavailable");
696 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
700 "virtual size: %s (%" PRId64
" bytes)\n"
702 filename
, fmt_name
, size_buf
,
703 (total_sectors
* 512),
705 if (bdrv_is_encrypted(bs
))
706 printf("encrypted: yes\n");
707 if (bdrv_get_info(bs
, &bdi
) >= 0) {
708 if (bdi
.cluster_size
!= 0)
709 printf("cluster_size: %d\n", bdi
.cluster_size
);
711 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
712 if (backing_filename
[0] != '\0') {
713 path_combine(backing_filename2
, sizeof(backing_filename2
),
714 filename
, backing_filename
);
715 printf("backing file: %s (actual path: %s)\n",
724 int main(int argc
, char **argv
)
733 if (!strcmp(cmd
, "create")) {
734 img_create(argc
, argv
);
735 } else if (!strcmp(cmd
, "commit")) {
736 img_commit(argc
, argv
);
737 } else if (!strcmp(cmd
, "convert")) {
738 img_convert(argc
, argv
);
739 } else if (!strcmp(cmd
, "info")) {
740 img_info(argc
, argv
);