2 * create a COW disk image
4 * Copyright (c) 2003 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 pstrcpy(char *buf
, int buf_size
, const char *str
)
75 if (c
== 0 || q
>= buf
+ buf_size
- 1)
82 /* strcat and truncate. */
83 char *pstrcat(char *buf
, int buf_size
, const char *s
)
88 pstrcpy(buf
+ len
, buf_size
- len
, s
);
92 int strstart(const char *str
, const char *val
, const char **ptr
)
108 void term_printf(const char *fmt
, ...)
116 void __attribute__((noreturn
)) error(const char *fmt
, ...)
120 fprintf(stderr
, "qemu-img: ");
121 vfprintf(stderr
, fmt
, ap
);
122 fprintf(stderr
, "\n");
127 static void format_print(void *opaque
, const char *name
)
134 printf("qemu-img version " QEMU_VERSION
", Copyright (c) 2004-2005 Fabrice Bellard\n"
135 "usage: qemu-img command [command options]\n"
136 "QEMU disk image utility\n"
139 " create [-e] [-b base_image] [-f fmt] filename [size]\n"
140 " commit [-f fmt] filename\n"
141 " convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename\n"
142 " info [-f fmt] filename\n"
144 "Command parameters:\n"
145 " 'filename' is a disk image filename\n"
146 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
147 " write image; the copy on write image only stores the modified data\n"
148 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
149 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
150 " and 'G' (gigabyte) are supported\n"
151 " 'output_filename' is the destination disk image filename\n"
152 " 'output_fmt' is the destination format\n"
153 " '-c' indicates that target image must be compressed (qcow format only)\n"
154 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
156 printf("\nSupported format:");
157 bdrv_iterate_format(format_print
, NULL
);
163 #define NB_SUFFIXES 4
165 static void get_human_readable_size(char *buf
, int buf_size
, int64_t size
)
167 static const char suffixes
[NB_SUFFIXES
] = "KMGT";
172 snprintf(buf
, buf_size
, "%" PRId64
, size
);
175 for(i
= 0; i
< NB_SUFFIXES
; i
++) {
176 if (size
< (10 * base
)) {
177 snprintf(buf
, buf_size
, "%0.1f%c",
181 } else if (size
< (1000 * base
) || i
== (NB_SUFFIXES
- 1)) {
182 snprintf(buf
, buf_size
, "%" PRId64
"%c",
183 ((size
+ (base
>> 1)) / base
),
193 /* XXX: put correct support for win32 */
194 static int read_password(char *buf
, int buf_size
)
197 printf("Password: ");
204 if (i
< (buf_size
- 1))
215 static struct termios oldtty
;
217 static void term_exit(void)
219 tcsetattr (0, TCSANOW
, &oldtty
);
222 static void term_init(void)
229 tty
.c_iflag
&= ~(IGNBRK
|BRKINT
|PARMRK
|ISTRIP
230 |INLCR
|IGNCR
|ICRNL
|IXON
);
231 tty
.c_oflag
|= OPOST
;
232 tty
.c_lflag
&= ~(ECHO
|ECHONL
|ICANON
|IEXTEN
);
233 tty
.c_cflag
&= ~(CSIZE
|PARENB
);
238 tcsetattr (0, TCSANOW
, &tty
);
243 int read_password(char *buf
, int buf_size
)
248 printf("password: ");
253 ret
= read(0, &ch
, 1);
255 if (errno
== EAGAIN
|| errno
== EINTR
) {
261 } else if (ret
== 0) {
269 if (i
< (buf_size
- 1))
280 static BlockDriverState
*bdrv_new_open(const char *filename
,
283 BlockDriverState
*bs
;
289 error("Not enough memory");
291 drv
= bdrv_find_format(fmt
);
293 error("Unknown file format '%s'", fmt
);
297 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
298 error("Could not open '%s'", filename
);
300 if (bdrv_is_encrypted(bs
)) {
301 printf("Disk image '%s' is encrypted.\n", filename
);
302 if (read_password(password
, sizeof(password
)) < 0)
303 error("No password given");
304 if (bdrv_set_key(bs
, password
) < 0)
305 error("invalid password");
310 static int img_create(int argc
, char **argv
)
312 int c
, ret
, encrypted
;
313 const char *fmt
= "raw";
314 const char *filename
;
315 const char *base_filename
= NULL
;
322 c
= getopt(argc
, argv
, "b:f:he");
330 base_filename
= optarg
;
342 filename
= argv
[optind
++];
345 BlockDriverState
*bs
;
346 bs
= bdrv_new_open(base_filename
, NULL
);
347 bdrv_get_geometry(bs
, &size
);
354 size
= strtoul(p
, (char **)&p
, 0);
357 } else if (*p
== 'G') {
358 size
*= 1024 * 1024 * 1024;
359 } else if (*p
== 'k' || *p
== 'K' || *p
== '\0') {
365 drv
= bdrv_find_format(fmt
);
367 error("Unknown file format '%s'", fmt
);
368 printf("Formating '%s', fmt=%s",
371 printf(", encrypted");
373 printf(", backing_file=%s",
376 printf(", size=%" PRId64
" kB\n", (int64_t) (size
/ 1024));
377 ret
= bdrv_create(drv
, filename
, size
/ 512, base_filename
, encrypted
);
379 if (ret
== -ENOTSUP
) {
380 error("Formatting or formatting option not supported for file format '%s'", fmt
);
382 error("Error while formatting");
388 static int img_commit(int argc
, char **argv
)
391 const char *filename
, *fmt
;
393 BlockDriverState
*bs
;
397 c
= getopt(argc
, argv
, "f:h");
411 filename
= argv
[optind
++];
415 error("Not enough memory");
417 drv
= bdrv_find_format(fmt
);
419 error("Unknown file format '%s'", fmt
);
423 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
424 error("Could not open '%s'", filename
);
426 ret
= bdrv_commit(bs
);
429 printf("Image committed.\n");
432 error("No disk inserted");
435 error("Image is read-only");
438 error("Image is already committed");
441 error("Error while committing image");
449 static int is_not_zero(const uint8_t *sector
, int len
)
453 for(i
= 0;i
< len
; i
++) {
454 if (((uint32_t *)sector
)[i
] != 0)
460 static int is_allocated_sectors(const uint8_t *buf
, int n
, int *pnum
)
468 v
= is_not_zero(buf
, 512);
469 for(i
= 1; i
< n
; i
++) {
471 if (v
!= is_not_zero(buf
, 512))
478 #define IO_BUF_SIZE 65536
480 static int img_convert(int argc
, char **argv
)
482 int c
, ret
, n
, n1
, compress
, cluster_size
, cluster_sectors
, encrypt
;
483 const char *filename
, *fmt
, *out_fmt
, *out_filename
;
485 BlockDriverState
*bs
, *out_bs
;
486 int64_t total_sectors
, nb_sectors
, sector_num
;
487 uint8_t buf
[IO_BUF_SIZE
];
495 c
= getopt(argc
, argv
, "f:O:hce");
518 filename
= argv
[optind
++];
521 out_filename
= argv
[optind
++];
523 bs
= bdrv_new_open(filename
, fmt
);
525 drv
= bdrv_find_format(out_fmt
);
527 error("Unknown file format '%s'", fmt
);
528 if (compress
&& drv
!= &bdrv_qcow
)
529 error("Compression not supported for this file format");
530 if (encrypt
&& drv
!= &bdrv_qcow
)
531 error("Encryption not supported for this file format");
532 if (compress
&& encrypt
)
533 error("Compression and encryption not supported at the same time");
534 bdrv_get_geometry(bs
, &total_sectors
);
535 ret
= bdrv_create(drv
, out_filename
, total_sectors
, NULL
, encrypt
);
537 if (ret
== -ENOTSUP
) {
538 error("Formatting not supported for file format '%s'", fmt
);
540 error("Error while formatting '%s'", out_filename
);
544 out_bs
= bdrv_new_open(out_filename
, out_fmt
);
547 cluster_size
= qcow_get_cluster_size(out_bs
);
548 if (cluster_size
<= 0 || cluster_size
> IO_BUF_SIZE
)
549 error("invalid cluster size");
550 cluster_sectors
= cluster_size
>> 9;
553 nb_sectors
= total_sectors
- sector_num
;
556 if (nb_sectors
>= cluster_sectors
)
560 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
561 error("error while reading");
562 if (n
< cluster_sectors
)
563 memset(buf
+ n
* 512, 0, cluster_size
- n
* 512);
564 if (is_not_zero(buf
, cluster_size
)) {
565 if (qcow_compress_cluster(out_bs
, sector_num
, buf
) != 0)
566 error("error while compressing sector %" PRId64
,
574 nb_sectors
= total_sectors
- sector_num
;
577 if (nb_sectors
>= (IO_BUF_SIZE
/ 512))
578 n
= (IO_BUF_SIZE
/ 512);
581 if (bdrv_read(bs
, sector_num
, buf
, n
) < 0)
582 error("error while reading");
583 /* NOTE: at the same time we convert, we do not write zero
584 sectors to have a chance to compress the image. Ideally, we
585 should add a specific call to have the info to go faster */
588 if (is_allocated_sectors(buf1
, n
, &n1
)) {
589 if (bdrv_write(out_bs
, sector_num
, buf1
, n1
) < 0)
590 error("error while writing");
604 static int64_t get_allocated_file_size(const char *filename
)
606 typedef DWORD (WINAPI
* get_compressed_t
)(const char *filename
, DWORD
*high
);
607 get_compressed_t get_compressed
;
610 /* WinNT support GetCompressedFileSize to determine allocate size */
611 get_compressed
= (get_compressed_t
) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
612 if (get_compressed
) {
614 low
= get_compressed(filename
, &high
);
615 if (low
!= 0xFFFFFFFFlu
|| GetLastError() == NO_ERROR
)
616 return (((int64_t) high
) << 32) + low
;
619 if (_stati64(filename
, &st
) < 0)
624 static int64_t get_allocated_file_size(const char *filename
)
627 if (stat(filename
, &st
) < 0)
629 return (int64_t)st
.st_blocks
* 512;
633 static int img_info(int argc
, char **argv
)
636 const char *filename
, *fmt
;
638 BlockDriverState
*bs
;
639 char fmt_name
[128], size_buf
[128], dsize_buf
[128];
640 int64_t total_sectors
, allocated_size
;
644 c
= getopt(argc
, argv
, "f:h");
658 filename
= argv
[optind
++];
662 error("Not enough memory");
664 drv
= bdrv_find_format(fmt
);
666 error("Unknown file format '%s'", fmt
);
670 if (bdrv_open2(bs
, filename
, 0, drv
) < 0) {
671 error("Could not open '%s'", filename
);
673 bdrv_get_format(bs
, fmt_name
, sizeof(fmt_name
));
674 bdrv_get_geometry(bs
, &total_sectors
);
675 get_human_readable_size(size_buf
, sizeof(size_buf
), total_sectors
* 512);
676 allocated_size
= get_allocated_file_size(filename
);
677 if (allocated_size
< 0)
678 sprintf(dsize_buf
, "unavailable");
680 get_human_readable_size(dsize_buf
, sizeof(dsize_buf
),
684 "virtual size: %s (%" PRId64
" bytes)\n"
686 filename
, fmt_name
, size_buf
,
687 (total_sectors
* 512),
689 if (bdrv_is_encrypted(bs
))
690 printf("encrypted: yes\n");
695 int main(int argc
, char **argv
)
704 if (!strcmp(cmd
, "create")) {
705 img_create(argc
, argv
);
706 } else if (!strcmp(cmd
, "commit")) {
707 img_commit(argc
, argv
);
708 } else if (!strcmp(cmd
, "convert")) {
709 img_convert(argc
, argv
);
710 } else if (!strcmp(cmd
, "info")) {
711 img_info(argc
, argv
);