]> git.proxmox.com Git - qemu.git/blame - qemu-img.c
show real allocated disk image size on Windows (Frediano Ziglio)
[qemu.git] / qemu-img.c
CommitLineData
ea2384d3
FB
1/*
2 * create a COW disk image
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * THE SOFTWARE.
23 */
24#include "vl.h"
25
e8445331
FB
26#ifdef _WIN32
27#include <windows.h>
28#endif
29
ea2384d3
FB
30void *get_mmap_addr(unsigned long size)
31{
32 return NULL;
33}
34
35void qemu_free(void *ptr)
36{
37 free(ptr);
38}
39
40void *qemu_malloc(size_t size)
41{
42 return malloc(size);
43}
44
45void *qemu_mallocz(size_t size)
46{
47 void *ptr;
48 ptr = qemu_malloc(size);
49 if (!ptr)
50 return NULL;
51 memset(ptr, 0, size);
52 return ptr;
53}
54
55char *qemu_strdup(const char *str)
56{
57 char *ptr;
58 ptr = qemu_malloc(strlen(str) + 1);
59 if (!ptr)
60 return NULL;
61 strcpy(ptr, str);
62 return ptr;
63}
64
65void pstrcpy(char *buf, int buf_size, const char *str)
66{
67 int c;
68 char *q = buf;
69
70 if (buf_size <= 0)
71 return;
72
73 for(;;) {
74 c = *str++;
75 if (c == 0 || q >= buf + buf_size - 1)
76 break;
77 *q++ = c;
78 }
79 *q = '\0';
80}
81
82/* strcat and truncate. */
83char *pstrcat(char *buf, int buf_size, const char *s)
84{
85 int len;
86 len = strlen(buf);
87 if (len < buf_size)
88 pstrcpy(buf + len, buf_size - len, s);
89 return buf;
90}
91
92int strstart(const char *str, const char *val, const char **ptr)
93{
94 const char *p, *q;
95 p = str;
96 q = val;
97 while (*q != '\0') {
98 if (*p != *q)
99 return 0;
100 p++;
101 q++;
102 }
103 if (ptr)
104 *ptr = p;
105 return 1;
106}
107
108void term_printf(const char *fmt, ...)
109{
110 va_list ap;
111 va_start(ap, fmt);
112 vprintf(fmt, ap);
113 va_end(ap);
114}
115
116void __attribute__((noreturn)) error(const char *fmt, ...)
117{
118 va_list ap;
119 va_start(ap, fmt);
57d1a2b6 120 fprintf(stderr, "qemu-img: ");
ea2384d3
FB
121 vfprintf(stderr, fmt, ap);
122 fprintf(stderr, "\n");
123 exit(1);
124 va_end(ap);
125}
126
127static void format_print(void *opaque, const char *name)
128{
129 printf(" %s", name);
130}
131
132void help(void)
133{
f5a8510c 134 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2005 Fabrice Bellard\n"
57d1a2b6 135 "usage: qemu-img command [command options]\n"
ea2384d3
FB
136 "QEMU disk image utility\n"
137 "\n"
138 "Command syntax:\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"
143 "\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"
155 );
156 printf("\nSupported format:");
157 bdrv_iterate_format(format_print, NULL);
158 printf("\n");
159 exit(1);
160}
161
162
163#define NB_SUFFIXES 4
164
165static void get_human_readable_size(char *buf, int buf_size, int64_t size)
166{
167 char suffixes[NB_SUFFIXES] = "KMGT";
168 int64_t base;
169 int i;
170
171 if (size <= 999) {
b8076a74 172 snprintf(buf, buf_size, "%lld", (long long) size);
ea2384d3
FB
173 } else {
174 base = 1024;
175 for(i = 0; i < NB_SUFFIXES; i++) {
176 if (size < (10 * base)) {
177 snprintf(buf, buf_size, "%0.1f%c",
178 (double)size / base,
179 suffixes[i]);
180 break;
181 } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) {
182 snprintf(buf, buf_size, "%lld%c",
b8076a74 183 (long long) ((size + (base >> 1)) / base),
ea2384d3
FB
184 suffixes[i]);
185 break;
186 }
187 base = base * 1024;
188 }
189 }
190}
191
192#if defined(WIN32)
193/* XXX: put correct support for win32 */
194static int read_password(char *buf, int buf_size)
195{
196 int c, i;
197 printf("Password: ");
198 fflush(stdout);
199 i = 0;
200 for(;;) {
201 c = getchar();
202 if (c == '\n')
203 break;
204 if (i < (buf_size - 1))
205 buf[i++] = c;
206 }
207 buf[i] = '\0';
208 return 0;
209}
210
211#else
212
213#include <termios.h>
214
215static struct termios oldtty;
216
217static void term_exit(void)
218{
219 tcsetattr (0, TCSANOW, &oldtty);
220}
221
222static void term_init(void)
223{
224 struct termios tty;
225
226 tcgetattr (0, &tty);
227 oldtty = tty;
228
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);
234 tty.c_cflag |= CS8;
235 tty.c_cc[VMIN] = 1;
236 tty.c_cc[VTIME] = 0;
237
238 tcsetattr (0, TCSANOW, &tty);
239
240 atexit(term_exit);
241}
242
243int read_password(char *buf, int buf_size)
244{
245 uint8_t ch;
246 int i, ret;
247
248 printf("password: ");
249 fflush(stdout);
250 term_init();
251 i = 0;
252 for(;;) {
253 ret = read(0, &ch, 1);
254 if (ret == -1) {
255 if (errno == EAGAIN || errno == EINTR) {
256 continue;
257 } else {
258 ret = -1;
259 break;
260 }
261 } else if (ret == 0) {
262 ret = -1;
263 break;
264 } else {
265 if (ch == '\r') {
266 ret = 0;
267 break;
268 }
269 if (i < (buf_size - 1))
270 buf[i++] = ch;
271 }
272 }
273 term_exit();
274 buf[i] = '\0';
275 printf("\n");
276 return ret;
277}
278#endif
279
75c23805
FB
280static BlockDriverState *bdrv_new_open(const char *filename,
281 const char *fmt)
282{
283 BlockDriverState *bs;
284 BlockDriver *drv;
285 char password[256];
286
287 bs = bdrv_new("");
288 if (!bs)
289 error("Not enough memory");
290 if (fmt) {
291 drv = bdrv_find_format(fmt);
292 if (!drv)
293 error("Unknown file format '%s'", fmt);
294 } else {
295 drv = NULL;
296 }
297 if (bdrv_open2(bs, filename, 0, drv) < 0) {
298 error("Could not open '%s'", filename);
299 }
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");
306 }
307 return bs;
308}
309
ea2384d3
FB
310static int img_create(int argc, char **argv)
311{
312 int c, ret, encrypted;
313 const char *fmt = "raw";
314 const char *filename;
315 const char *base_filename = NULL;
316 int64_t size;
317 const char *p;
318 BlockDriver *drv;
319
320 encrypted = 0;
321 for(;;) {
322 c = getopt(argc, argv, "b:f:he");
323 if (c == -1)
324 break;
325 switch(c) {
326 case 'h':
327 help();
328 break;
329 case 'b':
330 base_filename = optarg;
331 break;
332 case 'f':
333 fmt = optarg;
334 break;
335 case 'e':
336 encrypted = 1;
337 break;
338 }
339 }
ea2384d3
FB
340 if (optind >= argc)
341 help();
342 filename = argv[optind++];
343 size = 0;
75c23805
FB
344 if (base_filename) {
345 BlockDriverState *bs;
346 bs = bdrv_new_open(base_filename, NULL);
347 bdrv_get_geometry(bs, &size);
348 size *= 512;
349 bdrv_delete(bs);
350 } else {
ea2384d3
FB
351 if (optind >= argc)
352 help();
353 p = argv[optind];
354 size = strtoul(p, (char **)&p, 0);
355 if (*p == 'M') {
356 size *= 1024 * 1024;
357 } else if (*p == 'G') {
358 size *= 1024 * 1024 * 1024;
359 } else if (*p == 'k' || *p == 'K' || *p == '\0') {
360 size *= 1024;
361 } else {
362 help();
363 }
364 }
365 drv = bdrv_find_format(fmt);
366 if (!drv)
367 error("Unknown file format '%s'", fmt);
368 printf("Formating '%s', fmt=%s",
369 filename, fmt);
370 if (encrypted)
371 printf(", encrypted");
75c23805
FB
372 if (base_filename) {
373 printf(", backing_file=%s",
ea2384d3 374 base_filename);
75c23805 375 }
b8076a74 376 printf(", size=%lld kB\n", (long long) (size / 1024));
ea2384d3
FB
377 ret = bdrv_create(drv, filename, size / 512, base_filename, encrypted);
378 if (ret < 0) {
379 if (ret == -ENOTSUP) {
3c56521b 380 error("Formatting or formatting option not supported for file format '%s'", fmt);
ea2384d3
FB
381 } else {
382 error("Error while formatting");
383 }
384 }
385 return 0;
386}
387
388static int img_commit(int argc, char **argv)
389{
390 int c, ret;
391 const char *filename, *fmt;
392 BlockDriver *drv;
393 BlockDriverState *bs;
394
395 fmt = NULL;
396 for(;;) {
397 c = getopt(argc, argv, "f:h");
398 if (c == -1)
399 break;
400 switch(c) {
401 case 'h':
402 help();
403 break;
404 case 'f':
405 fmt = optarg;
406 break;
407 }
408 }
ea2384d3
FB
409 if (optind >= argc)
410 help();
411 filename = argv[optind++];
412
413 bs = bdrv_new("");
414 if (!bs)
415 error("Not enough memory");
416 if (fmt) {
417 drv = bdrv_find_format(fmt);
418 if (!drv)
419 error("Unknown file format '%s'", fmt);
420 } else {
421 drv = NULL;
422 }
423 if (bdrv_open2(bs, filename, 0, drv) < 0) {
424 error("Could not open '%s'", filename);
425 }
426 ret = bdrv_commit(bs);
427 switch(ret) {
428 case 0:
429 printf("Image committed.\n");
430 break;
431 case -ENOENT:
432 error("No disk inserted");
433 break;
434 case -EACCES:
435 error("Image is read-only");
436 break;
437 case -ENOTSUP:
438 error("Image is already committed");
439 break;
440 default:
441 error("Error while committing image");
442 break;
443 }
444
445 bdrv_delete(bs);
446 return 0;
447}
448
449static int is_not_zero(const uint8_t *sector, int len)
450{
451 int i;
452 len >>= 2;
453 for(i = 0;i < len; i++) {
454 if (((uint32_t *)sector)[i] != 0)
455 return 1;
456 }
457 return 0;
458}
459
460static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
461{
462 int v, i;
463
464 if (n <= 0) {
465 *pnum = 0;
466 return 0;
467 }
468 v = is_not_zero(buf, 512);
469 for(i = 1; i < n; i++) {
470 buf += 512;
471 if (v != is_not_zero(buf, 512))
472 break;
473 }
474 *pnum = i;
475 return v;
476}
477
ea2384d3
FB
478#define IO_BUF_SIZE 65536
479
480static int img_convert(int argc, char **argv)
481{
482 int c, ret, n, n1, compress, cluster_size, cluster_sectors, encrypt;
483 const char *filename, *fmt, *out_fmt, *out_filename;
484 BlockDriver *drv;
485 BlockDriverState *bs, *out_bs;
486 int64_t total_sectors, nb_sectors, sector_num;
487 uint8_t buf[IO_BUF_SIZE];
488 const uint8_t *buf1;
489
490 fmt = NULL;
491 out_fmt = "raw";
492 compress = 0;
493 encrypt = 0;
494 for(;;) {
495 c = getopt(argc, argv, "f:O:hce");
496 if (c == -1)
497 break;
498 switch(c) {
499 case 'h':
500 help();
501 break;
502 case 'f':
503 fmt = optarg;
504 break;
505 case 'O':
506 out_fmt = optarg;
507 break;
508 case 'c':
509 compress = 1;
510 break;
511 case 'e':
512 encrypt = 1;
513 break;
514 }
515 }
ea2384d3
FB
516 if (optind >= argc)
517 help();
518 filename = argv[optind++];
519 if (optind >= argc)
520 help();
521 out_filename = argv[optind++];
522
523 bs = bdrv_new_open(filename, fmt);
524
525 drv = bdrv_find_format(out_fmt);
526 if (!drv)
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);
536 if (ret < 0) {
537 if (ret == -ENOTSUP) {
3c56521b 538 error("Formatting not supported for file format '%s'", fmt);
ea2384d3
FB
539 } else {
540 error("Error while formatting '%s'", out_filename);
541 }
542 }
543
544 out_bs = bdrv_new_open(out_filename, out_fmt);
545
546 if (compress) {
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;
551 sector_num = 0;
552 for(;;) {
553 nb_sectors = total_sectors - sector_num;
554 if (nb_sectors <= 0)
555 break;
556 if (nb_sectors >= cluster_sectors)
557 n = cluster_sectors;
558 else
559 n = nb_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 %lld", sector_num);
567 }
568 sector_num += n;
569 }
570 } else {
571 sector_num = 0;
572 for(;;) {
573 nb_sectors = total_sectors - sector_num;
574 if (nb_sectors <= 0)
575 break;
576 if (nb_sectors >= (IO_BUF_SIZE / 512))
577 n = (IO_BUF_SIZE / 512);
578 else
579 n = nb_sectors;
580 if (bdrv_read(bs, sector_num, buf, n) < 0)
581 error("error while reading");
582 /* NOTE: at the same time we convert, we do not write zero
583 sectors to have a chance to compress the image. Ideally, we
584 should add a specific call to have the info to go faster */
585 buf1 = buf;
586 while (n > 0) {
587 if (is_allocated_sectors(buf1, n, &n1)) {
588 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
589 error("error while writing");
590 }
591 sector_num += n1;
592 n -= n1;
593 buf1 += n1 * 512;
594 }
595 }
596 }
597 bdrv_delete(out_bs);
598 bdrv_delete(bs);
599 return 0;
600}
601
57d1a2b6
FB
602#ifdef _WIN32
603static int64_t get_allocated_file_size(const char *filename)
604{
e8445331
FB
605 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
606 get_compressed_t get_compressed;
57d1a2b6 607 struct _stati64 st;
e8445331
FB
608
609 /* WinNT support GetCompressedFileSize to determine allocate size */
610 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
611 if (get_compressed) {
612 DWORD high, low;
613 low = get_compressed(filename, &high);
614 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
615 return (((int64_t) high) << 32) + low;
616 }
617
57d1a2b6
FB
618 if (_stati64(filename, &st) < 0)
619 return -1;
620 return st.st_size;
621}
622#else
623static int64_t get_allocated_file_size(const char *filename)
624{
625 struct stat st;
626 if (stat(filename, &st) < 0)
627 return -1;
628 return (int64_t)st.st_blocks * 512;
629}
630#endif
631
ea2384d3
FB
632static int img_info(int argc, char **argv)
633{
634 int c;
635 const char *filename, *fmt;
636 BlockDriver *drv;
637 BlockDriverState *bs;
638 char fmt_name[128], size_buf[128], dsize_buf[128];
57d1a2b6 639 int64_t total_sectors, allocated_size;
ea2384d3
FB
640
641 fmt = NULL;
642 for(;;) {
643 c = getopt(argc, argv, "f:h");
644 if (c == -1)
645 break;
646 switch(c) {
647 case 'h':
648 help();
649 break;
650 case 'f':
651 fmt = optarg;
652 break;
653 }
654 }
ea2384d3
FB
655 if (optind >= argc)
656 help();
657 filename = argv[optind++];
658
659 bs = bdrv_new("");
660 if (!bs)
661 error("Not enough memory");
662 if (fmt) {
663 drv = bdrv_find_format(fmt);
664 if (!drv)
665 error("Unknown file format '%s'", fmt);
666 } else {
667 drv = NULL;
668 }
669 if (bdrv_open2(bs, filename, 0, drv) < 0) {
670 error("Could not open '%s'", filename);
671 }
672 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
673 bdrv_get_geometry(bs, &total_sectors);
674 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
57d1a2b6
FB
675 allocated_size = get_allocated_file_size(filename);
676 if (allocated_size < 0)
de167e41
FB
677 sprintf(dsize_buf, "unavailable");
678 else
679 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
680 allocated_size);
ea2384d3
FB
681 printf("image: %s\n"
682 "file format: %s\n"
683 "virtual size: %s (%lld bytes)\n"
684 "disk size: %s\n",
685 filename, fmt_name, size_buf,
b8076a74 686 (long long) (total_sectors * 512),
ea2384d3
FB
687 dsize_buf);
688 if (bdrv_is_encrypted(bs))
689 printf("encrypted: yes\n");
690 bdrv_delete(bs);
691 return 0;
692}
693
694int main(int argc, char **argv)
695{
696 const char *cmd;
697
698 bdrv_init();
699 if (argc < 2)
700 help();
701 cmd = argv[1];
e3888186 702 optind++;
ea2384d3
FB
703 if (!strcmp(cmd, "create")) {
704 img_create(argc, argv);
705 } else if (!strcmp(cmd, "commit")) {
706 img_commit(argc, argv);
707 } else if (!strcmp(cmd, "convert")) {
708 img_convert(argc, argv);
709 } else if (!strcmp(cmd, "info")) {
710 img_info(argc, argv);
711 } else {
712 help();
713 }
714 return 0;
715}