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
26 void *get_mmap_addr(unsigned long size)
31 void qemu_free(void *ptr)
36 void *qemu_malloc(size_t size)
41 void *qemu_mallocz(size_t size)
44 ptr = qemu_malloc(size);
51 char *qemu_strdup(const char *str)
54 ptr = qemu_malloc(strlen(str) + 1);
61 void pstrcpy(char *buf, int buf_size, const char *str)
71 if (c == 0 || q >= buf + buf_size - 1)
78 /* strcat and truncate. */
79 char *pstrcat(char *buf, int buf_size, const char *s)
84 pstrcpy(buf + len, buf_size - len, s);
88 int strstart(const char *str, const char *val, const char **ptr)
104 void term_printf(const char *fmt, ...)
112 void __attribute__((noreturn)) error(const char *fmt, ...)
116 fprintf(stderr, "qemu-img: ");
117 vfprintf(stderr, fmt, ap);
118 fprintf(stderr, "\n");
123 static void format_print(void *opaque, const char *name)
130 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004 Fabrice Bellard\n"
131 "usage: qemu-img command [command options]\n"
132 "QEMU disk image utility\n"
135 " create [-e] [-b base_image] [-f fmt] filename [size]\n"
136 " commit [-f fmt] filename\n"
137 " convert [-c] [-e] [-f fmt] filename [-O output_fmt] output_filename\n"
138 " info [-f fmt] filename\n"
140 "Command parameters:\n"
141 " 'filename' is a disk image filename\n"
142 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
143 " write image; the copy on write image only stores the modified data\n"
144 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
145 " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n"
146 " and 'G' (gigabyte) are supported\n"
147 " 'output_filename' is the destination disk image filename\n"
148 " 'output_fmt' is the destination format\n"
149 " '-c' indicates that target image must be compressed (qcow format only)\n"
150 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
152 printf("\nSupported format:");
153 bdrv_iterate_format(format_print, NULL);
159 #define NB_SUFFIXES 4
161 static void get_human_readable_size(char *buf, int buf_size, int64_t size)
163 char suffixes[NB_SUFFIXES] = "KMGT";
168 snprintf(buf, buf_size, "%lld", size);
171 for(i = 0; i < NB_SUFFIXES; i++) {
172 if (size < (10 * base)) {
173 snprintf(buf, buf_size, "%0.1f%c",
177 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
178 snprintf(buf, buf_size, "%lld%c",
179 (size + (base >> 1)) / base,
189 /* XXX: put correct support for win32 */
190 static int read_password(char *buf, int buf_size)
193 printf("Password: ");
200 if (i < (buf_size - 1))
211 static struct termios oldtty;
213 static void term_exit(void)
215 tcsetattr (0, TCSANOW, &oldtty);
218 static void term_init(void)
225 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
226 |INLCR|IGNCR|ICRNL|IXON);
227 tty.c_oflag |= OPOST;
228 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
229 tty.c_cflag &= ~(CSIZE|PARENB);
234 tcsetattr (0, TCSANOW, &tty);
239 int read_password(char *buf, int buf_size)
244 printf("password: ");
249 ret = read(0, &ch, 1);
251 if (errno == EAGAIN || errno == EINTR) {
257 } else if (ret == 0) {
265 if (i < (buf_size - 1))
276 static BlockDriverState *bdrv_new_open(const char *filename,
279 BlockDriverState *bs;
285 error("Not enough memory");
287 drv = bdrv_find_format(fmt);
289 error("Unknown file format '%s'", fmt);
293 if (bdrv_open2(bs, filename, 0, drv) < 0) {
294 error("Could not open '%s'", filename);
296 if (bdrv_is_encrypted(bs)) {
297 printf("Disk image '%s' is encrypted.\n", filename);
298 if (read_password(password, sizeof(password)) < 0)
299 error("No password given");
300 if (bdrv_set_key(bs, password) < 0)
301 error("invalid password");
306 static int img_create(int argc, char **argv)
308 int c, ret, encrypted;
309 const char *fmt = "raw";
310 const char *filename;
311 const char *base_filename = NULL;
318 c = getopt(argc, argv, "b:f:he");
326 base_filename = optarg;
339 filename = argv[optind++];
342 BlockDriverState *bs;
343 bs = bdrv_new_open(base_filename, NULL);
344 bdrv_get_geometry(bs, &size);
351 size = strtoul(p, (char **)&p, 0);
354 } else if (*p == 'G') {
355 size *= 1024 * 1024 * 1024;
356 } else if (*p == 'k' || *p == 'K' || *p == '\0') {
362 drv = bdrv_find_format(fmt);
364 error("Unknown file format '%s'", fmt);
365 printf("Formating '%s', fmt=%s",
368 printf(", encrypted");
370 printf(", backing_file=%s",
373 printf(", size=%lld kB\n", size / 1024);
374 ret = bdrv_create(drv, filename, size / 512, base_filename, encrypted);
376 if (ret == -ENOTSUP) {
377 error("Formatting or formatting option not supported for file format '%s'", fmt);
379 error("Error while formatting");
385 static int img_commit(int argc, char **argv)
388 const char *filename, *fmt;
390 BlockDriverState *bs;
394 c = getopt(argc, argv, "f:h");
409 filename = argv[optind++];
413 error("Not enough memory");
415 drv = bdrv_find_format(fmt);
417 error("Unknown file format '%s'", fmt);
421 if (bdrv_open2(bs, filename, 0, drv) < 0) {
422 error("Could not open '%s'", filename);
424 ret = bdrv_commit(bs);
427 printf("Image committed.\n");
430 error("No disk inserted");
433 error("Image is read-only");
436 error("Image is already committed");
439 error("Error while committing image");
447 static int is_not_zero(const uint8_t *sector, int len)
451 for(i = 0;i < len; i++) {
452 if (((uint32_t *)sector)[i] != 0)
458 static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
466 v = is_not_zero(buf, 512);
467 for(i = 1; i < n; i++) {
469 if (v != is_not_zero(buf, 512))
476 #define IO_BUF_SIZE 65536
478 static int img_convert(int argc, char **argv)
480 int c, ret, n, n1, compress, cluster_size, cluster_sectors, encrypt;
481 const char *filename, *fmt, *out_fmt, *out_filename;
483 BlockDriverState *bs, *out_bs;
484 int64_t total_sectors, nb_sectors, sector_num;
485 uint8_t buf[IO_BUF_SIZE];
493 c = getopt(argc, argv, "f:O:hce");
517 filename = argv[optind++];
520 out_filename = argv[optind++];
522 bs = bdrv_new_open(filename, fmt);
524 drv = bdrv_find_format(out_fmt);
526 error("Unknown file format '%s'", fmt);
527 if (compress && drv != &bdrv_qcow)
528 error("Compression not supported for this file format");
529 if (encrypt && drv != &bdrv_qcow)
530 error("Encryption not supported for this file format");
531 if (compress && encrypt)
532 error("Compression and encryption not supported at the same time");
533 bdrv_get_geometry(bs, &total_sectors);
534 ret = bdrv_create(drv, out_filename, total_sectors, NULL, encrypt);
536 if (ret == -ENOTSUP) {
537 error("Formatting not supported for file format '%s'", fmt);
539 error("Error while formatting '%s'", out_filename);
543 out_bs = bdrv_new_open(out_filename, out_fmt);
546 cluster_size = qcow_get_cluster_size(out_bs);
547 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
548 error("invalid cluster size");
549 cluster_sectors = cluster_size >> 9;
552 nb_sectors = total_sectors - sector_num;
555 if (nb_sectors >= cluster_sectors)
559 if (bdrv_read(bs, sector_num, buf, n) < 0)
560 error("error while reading");
561 if (n < cluster_sectors)
562 memset(buf + n * 512, 0, cluster_size - n * 512);
563 if (is_not_zero(buf, cluster_size)) {
564 if (qcow_compress_cluster(out_bs, sector_num, buf) != 0)
565 error("error while compressing sector %lld", sector_num);
572 nb_sectors = total_sectors - sector_num;
575 if (nb_sectors >= (IO_BUF_SIZE / 512))
576 n = (IO_BUF_SIZE / 512);
579 if (bdrv_read(bs, sector_num, buf, n) < 0)
580 error("error while reading");
581 /* NOTE: at the same time we convert, we do not write zero
582 sectors to have a chance to compress the image. Ideally, we
583 should add a specific call to have the info to go faster */
586 if (is_allocated_sectors(buf1, n, &n1)) {
587 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
588 error("error while writing");
602 static int64_t get_allocated_file_size(const char *filename)
605 if (_stati64(filename, &st) < 0)
610 static int64_t get_allocated_file_size(const char *filename)
613 if (stat(filename, &st) < 0)
615 return (int64_t)st.st_blocks * 512;
619 static int img_info(int argc, char **argv)
622 const char *filename, *fmt;
624 BlockDriverState *bs;
625 char fmt_name[128], size_buf[128], dsize_buf[128];
626 int64_t total_sectors, allocated_size;
630 c = getopt(argc, argv, "f:h");
645 filename = argv[optind++];
649 error("Not enough memory");
651 drv = bdrv_find_format(fmt);
653 error("Unknown file format '%s'", fmt);
657 if (bdrv_open2(bs, filename, 0, drv) < 0) {
658 error("Could not open '%s'", filename);
660 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
661 bdrv_get_geometry(bs, &total_sectors);
662 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
663 allocated_size = get_allocated_file_size(filename);
664 if (allocated_size < 0)
665 error("Could not get file size '%s'", filename);
666 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
670 "virtual size: %s (%lld bytes)\n"
672 filename, fmt_name, size_buf,
675 if (bdrv_is_encrypted(bs))
676 printf("encrypted: yes\n");
681 int main(int argc, char **argv)
689 if (!strcmp(cmd, "create")) {
690 img_create(argc, argv);
691 } else if (!strcmp(cmd, "commit")) {
692 img_commit(argc, argv);
693 } else if (!strcmp(cmd, "convert")) {
694 img_convert(argc, argv);
695 } else if (!strcmp(cmd, "info")) {
696 img_info(argc, argv);