2 * QEMU disk image utility
4 * Copyright (c) 2003-2007 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
30 void *get_mmap_addr(unsigned long size
)
35 void qemu_free(void *ptr
)
40 void *qemu_malloc(size_t size
)
45 void *qemu_mallocz(size_t size
)
48 ptr
= qemu_malloc(size
);
55 char *qemu_strdup(const char *str
)
58 ptr
= qemu_malloc(strlen(str
) + 1);
65 void term_printf(const char *fmt
, ...)
73 void term_print_filename(const char *filename
)
75 term_printf(filename
);
78 void __attribute__((noreturn
)) error(const char *fmt
, ...)
82 fprintf(stderr
, "qemu-img: ");
83 vfprintf(stderr
, fmt
, ap
);
84 fprintf(stderr
, "\n");
89 static void format_print(void *opaque
, const char *name
)
96 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2007 Fabrice Bellard\n"
97 "usage: qemu-img command [command options]\n"
98 "QEMU disk image utility\n"
101 " create [-e] [-b base_image] [-f fmt] filename [size]\n"
102 " commit [-f fmt] filename\n"
103 " convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename\n"
104 " info [-f fmt] filename\n"
106 "Command parameters:\n"
107 " 'filename' is a disk image filename\n"
108 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
109 " write image; the copy on write image only stores the modified data\n"
110 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
111 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
112 " and 'G' (gigabyte) are supported\n"
113 " 'output_filename' is the destination disk image filename\n"
114 " 'output_fmt' is the destination format\n"
115 " '-c' indicates that target image must be compressed (qcow format only)\n"
116 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
118 printf("\nSupported format:");
119 bdrv_iterate_format(format_print
, NULL
);
125 /* XXX: put correct support for win32 */
126 static int read_password(char *buf
, int buf_size
)
129 printf("Password: ");
136 if (i
< (buf_size
- 1))
147 static struct termios oldtty
;
149 static void term_exit(void)
151 tcsetattr (0, TCSANOW
, &oldtty
);
154 static void term_init(void)
161 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
162 |INLCR
|IGNCR
|ICRNL
|IXON
);
163 tty
.c_oflag
|= OPOST
;
164 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
165 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
170 tcsetattr (0, TCSANOW
, &tty
);
175 int read_password(char *buf
, int buf_size
)
180 printf("password: ");
185 ret
= read(0, &ch
, 1);
187 if (errno
== EAGAIN
|| errno
== EINTR
) {
193 } else if (ret
== 0) {
201 if (i
< (buf_size
- 1))
212 static BlockDriverState
*bdrv_new_open(const char *filename
,
215 BlockDriverState
*bs
;
221 error("Not enough memory");
223 drv
= bdrv_find_format(fmt
);
225 error("Unknown file format '%s'", fmt
);
229 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
230 error("Could not open '%s'", filename
);
232 if (bdrv_is_encrypted(bs
)) {
233 printf("Disk image '%s' is encrypted.\n", filename
);
234 if (read_password(password
, sizeof(password
)) < 0)
235 error("No password given");
236 if (bdrv_set_key(bs
, password
) < 0)
237 error("invalid password");
242 static int img_create(int argc
, char **argv
)
244 int c
, ret
, encrypted
;
245 const char *fmt
= "raw";
246 const char *filename
;
247 const char *base_filename
= NULL
;
254 c
= getopt(argc
, argv
, "b:f:he");
262 base_filename
= optarg
;
274 filename
= argv
[optind
++];
277 BlockDriverState
*bs
;
278 bs
= bdrv_new_open(base_filename
, NULL
);
279 bdrv_get_geometry(bs
, &size
);
286 size
= strtoul(p
, (char **)&p
, 0);
289 } else if (*p
== 'G') {
290 size
*= 1024 * 1024 * 1024;
291 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
297 drv
= bdrv_find_format(fmt
);
299 error("Unknown file format '%s'", fmt
);
300 printf("Formatting '%s', fmt=%s",
303 printf(", encrypted");
305 printf(", backing_file=%s",
308 printf(", size=%" PRId64
" kB\n", (int64_t) (size
/ 1024));
309 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, encrypted
);
311 if (ret
== -ENOTSUP
) {
312 error("Formatting or formatting option not supported for file format '%s'", fmt
);
314 error("Error while formatting");
320 static int img_commit(int argc
, char **argv
)
323 const char *filename
, *fmt
;
325 BlockDriverState
*bs
;
329 c
= getopt(argc
, argv
, "f:h");
343 filename
= argv
[optind
++];
347 error("Not enough memory");
349 drv
= bdrv_find_format(fmt
);
351 error("Unknown file format '%s'", fmt
);
355 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
356 error("Could not open '%s'", filename
);
358 ret
= bdrv_commit(bs
);
361 printf("Image committed.\n");
364 error("No disk inserted");
367 error("Image is read-only");
370 error("Image is already committed");
373 error("Error while committing image");
381 static int is_not_zero(const uint8_t *sector
, int len
)
385 for(i
= 0;i
< len
; i
++) {
386 if (((uint32_t *)sector
)[i
] != 0)
392 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
400 v
= is_not_zero(buf
, 512);
401 for(i
= 1; i
< n
; i
++) {
403 if (v
!= is_not_zero(buf
, 512))
410 #define IO_BUF_SIZE 65536
412 static int img_convert(int argc
, char **argv
)
414 int c
, ret
, n
, n1
, compress
, cluster_size
, cluster_sectors
, encrypt
;
415 const char *filename
, *fmt
, *out_fmt
, *out_filename
;
417 BlockDriverState
*bs
, *out_bs
;
418 int64_t total_sectors
, nb_sectors
, sector_num
;
419 uint8_t buf
[IO_BUF_SIZE
];
428 c
= getopt(argc
, argv
, "f:O:hce");
451 filename
= argv
[optind
++];
454 out_filename
= argv
[optind
++];
456 bs
= bdrv_new_open(filename
, fmt
);
458 drv
= bdrv_find_format(out_fmt
);
460 error("Unknown file format '%s'", out_fmt
);
461 if (compress
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
462 error("Compression not supported for this file format");
463 if (encrypt
&& drv
!= &bdrv_qcow
&& drv
!= &bdrv_qcow2
)
464 error("Encryption not supported for this file format");
465 if (compress
&& encrypt
)
466 error("Compression and encryption not supported at the same time");
467 bdrv_get_geometry(bs
, &total_sectors
);
468 ret
= bdrv_create(drv
, out_filename
, total_sectors
, NULL
, encrypt
);
470 if (ret
== -ENOTSUP
) {
471 error("Formatting not supported for file format '%s'", fmt
);
473 error("Error while formatting '%s'", out_filename
);
477 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
480 if (bdrv_get_info(out_bs
, &bdi
) < 0)
481 error("could not get block driver info");
482 cluster_size
= bdi
.cluster_size
;
483 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
484 error("invalid cluster size");
485 cluster_sectors
= cluster_size
>> 9;
488 nb_sectors
= total_sectors
- sector_num
;
491 if (nb_sectors
>= cluster_sectors
)
495 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
496 error("error while reading");
497 if (n
< cluster_sectors
)
498 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
499 if (is_not_zero(buf
, cluster_size
)) {
500 if (bdrv_write_compressed(out_bs
, sector_num
, buf
,
501 cluster_sectors
) != 0)
502 error("error while compressing sector %" PRId64
,
507 /* signal EOF to align */
508 bdrv_write_compressed(out_bs
, 0, NULL
, 0);
512 nb_sectors
= total_sectors
- sector_num
;
515 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
516 n
= (IO_BUF_SIZE
/ 512);
519 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
520 error("error while reading");
521 /* NOTE: at the same time we convert, we do not write zero
522 sectors to have a chance to compress the image. Ideally, we
523 should add a specific call to have the info to go faster */
526 if (is_allocated_sectors(buf1
, n
, &n1
)) {
527 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
528 error("error while writing");
542 static int64_t get_allocated_file_size(const char *filename
)
544 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
545 get_compressed_t get_compressed
;
548 /* WinNT support GetCompressedFileSize to determine allocate size */
549 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
550 if (get_compressed
) {
552 low
= get_compressed(filename
, &high
);
553 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
554 return (((int64_t) high
) << 32) + low
;
557 if (_stati64(filename
, &st
) < 0)
562 static int64_t get_allocated_file_size(const char *filename
)
565 if (stat(filename
, &st
) < 0)
567 return (int64_t)st
.st_blocks
* 512;
571 static void dump_snapshots(BlockDriverState
*bs
)
573 QEMUSnapshotInfo
*sn_tab
, *sn
;
577 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
580 printf("Snapshot list:\n");
581 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
582 for(i
= 0; i
< nb_sns
; i
++) {
584 printf("%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
589 static int img_info(int argc
, char **argv
)
592 const char *filename
, *fmt
;
594 BlockDriverState
*bs
;
595 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
596 int64_t total_sectors
, allocated_size
;
597 char backing_filename
[1024];
598 char backing_filename2
[1024];
603 c
= getopt(argc
, argv
, "f:h");
617 filename
= argv
[optind
++];
621 error("Not enough memory");
623 drv
= bdrv_find_format(fmt
);
625 error("Unknown file format '%s'", fmt
);
629 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
630 error("Could not open '%s'", filename
);
632 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
633 bdrv_get_geometry(bs
, &total_sectors
);
634 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
635 allocated_size
= get_allocated_file_size(filename
);
636 if (allocated_size
< 0)
637 sprintf(dsize_buf
, "unavailable");
639 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
643 "virtual size: %s (%" PRId64
" bytes)\n"
645 filename
, fmt_name
, size_buf
,
646 (total_sectors
* 512),
648 if (bdrv_is_encrypted(bs
))
649 printf("encrypted: yes\n");
650 if (bdrv_get_info(bs
, &bdi
) >= 0) {
651 if (bdi
.cluster_size
!= 0)
652 printf("cluster_size: %d\n", bdi
.cluster_size
);
654 bdrv_get_backing_filename(bs
, backing_filename
, sizeof(backing_filename
));
655 if (backing_filename
[0] != '\0') {
656 path_combine(backing_filename2
, sizeof(backing_filename2
),
657 filename
, backing_filename
);
658 printf("backing file: %s (actual path: %s)\n",
667 int main(int argc
, char **argv
)
676 if (!strcmp(cmd
, "create")) {
677 img_create(argc
, argv
);
678 } else if (!strcmp(cmd
, "commit")) {
679 img_commit(argc
, argv
);
680 } else if (!strcmp(cmd
, "convert")) {
681 img_convert(argc
, argv
);
682 } else if (!strcmp(cmd
, "info")) {
683 img_info(argc
, argv
);