blob: 3edf25a8f0b88565f5e70b11d974e245b3dec008 [file] [log] [blame]
bellardea2384d2004-08-01 21:59:26 +00001/*
bellardfb43f4d2006-08-07 21:34:46 +00002 * QEMU disk image utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard68d0f702008-01-06 17:21:48 +00004 * Copyright (c) 2003-2008 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellardea2384d2004-08-01 21:59:26 +00006 * 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 */
pbrookfaf07962007-11-11 02:51:17 +000024#include "qemu-common.h"
Kevin Wolf9ea2ea72009-05-18 16:42:11 +020025#include "qemu-option.h"
aliguorif7b4a942009-01-07 17:40:15 +000026#include "osdep.h"
thsec36ba12007-09-16 21:59:02 +000027#include "block_int.h"
aliguori9230eaf2009-03-28 17:55:19 +000028#include <stdio.h>
bellardea2384d2004-08-01 21:59:26 +000029
bellarde8445332006-06-14 15:32:10 +000030#ifdef _WIN32
31#include <windows.h>
32#endif
33
aurel32137519c2008-11-30 19:12:49 +000034/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
35#define BRDV_O_FLAGS BDRV_O_CACHE_WB
36
malca5e50b22009-02-01 22:19:27 +000037static void QEMU_NORETURN error(const char *fmt, ...)
bellardea2384d2004-08-01 21:59:26 +000038{
39 va_list ap;
40 va_start(ap, fmt);
bellard57d1a2b2004-08-03 21:15:11 +000041 fprintf(stderr, "qemu-img: ");
bellardea2384d2004-08-01 21:59:26 +000042 vfprintf(stderr, fmt, ap);
43 fprintf(stderr, "\n");
44 exit(1);
45 va_end(ap);
46}
47
48static void format_print(void *opaque, const char *name)
49{
50 printf(" %s", name);
51}
52
blueswir1d2c639d2009-01-24 18:19:25 +000053/* Please keep in synch with qemu-img.texi */
pbrook3f379ab2007-11-11 03:33:13 +000054static void help(void)
bellardea2384d2004-08-01 21:59:26 +000055{
bellard68d0f702008-01-06 17:21:48 +000056 printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
bellard57d1a2b2004-08-03 21:15:11 +000057 "usage: qemu-img command [command options]\n"
bellardea2384d2004-08-01 21:59:26 +000058 "QEMU disk image utility\n"
59 "\n"
60 "Command syntax:\n"
aliguori15859692009-04-21 23:11:53 +000061 " check [-f fmt] filename\n"
aliguori9230eaf2009-03-28 17:55:19 +000062 " create [-e] [-6] [-F fmt] [-b base_image] [-f fmt] filename [size]\n"
bellardea2384d2004-08-01 21:59:26 +000063 " commit [-f fmt] filename\n"
thsf58c7b32008-06-05 21:53:49 +000064 " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
bellardea2384d2004-08-01 21:59:26 +000065 " info [-f fmt] filename\n"
blueswir1d2c639d2009-01-24 18:19:25 +000066 " snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename\n"
bellardea2384d2004-08-01 21:59:26 +000067 "\n"
68 "Command parameters:\n"
69 " 'filename' is a disk image filename\n"
70 " 'base_image' is the read-only disk image which is used as base for a copy on\n"
71 " write image; the copy on write image only stores the modified data\n"
thsf58c7b32008-06-05 21:53:49 +000072 " 'output_base_image' forces the output image to be created as a copy on write\n"
73 " image of the specified base image; 'output_base_image' should have the same\n"
74 " content as the input's base image, however the path, image format, etc may\n"
75 " differ\n"
bellardea2384d2004-08-01 21:59:26 +000076 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
blueswir1d2c639d2009-01-24 18:19:25 +000077 " 'size' is the disk image size in kilobytes. Optional suffixes\n"
aurel322fbc4092009-03-08 19:49:51 +000078 " 'M' (megabyte, 1024 * 1024) and 'G' (gigabyte, 1024 * 1024 * 1024) are\n"
79 " supported any 'k' or 'K' is ignored\n"
bellardea2384d2004-08-01 21:59:26 +000080 " 'output_filename' is the destination disk image filename\n"
81 " 'output_fmt' is the destination format\n"
82 " '-c' indicates that target image must be compressed (qcow format only)\n"
83 " '-e' indicates that the target image must be encrypted (qcow format only)\n"
thsec36ba12007-09-16 21:59:02 +000084 " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n"
blueswir1d2c639d2009-01-24 18:19:25 +000085 " '-h' with or without a command shows this help and lists the supported formats\n"
aliguorif7b4a942009-01-07 17:40:15 +000086 "\n"
blueswir1d2c639d2009-01-24 18:19:25 +000087 "Parameters to snapshot subcommand:\n"
88 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
89 " '-a' applies a snapshot (revert disk to saved state)\n"
90 " '-c' creates a snapshot\n"
91 " '-d' deletes a snapshot\n"
92 " '-l' lists all snapshots in the given image\n"
bellardea2384d2004-08-01 21:59:26 +000093 );
blueswir1d2c639d2009-01-24 18:19:25 +000094 printf("\nSupported formats:");
bellardea2384d2004-08-01 21:59:26 +000095 bdrv_iterate_format(format_print, NULL);
96 printf("\n");
97 exit(1);
98}
99
bellardea2384d2004-08-01 21:59:26 +0000100#if defined(WIN32)
101/* XXX: put correct support for win32 */
102static int read_password(char *buf, int buf_size)
103{
104 int c, i;
105 printf("Password: ");
106 fflush(stdout);
107 i = 0;
108 for(;;) {
109 c = getchar();
110 if (c == '\n')
111 break;
112 if (i < (buf_size - 1))
113 buf[i++] = c;
114 }
115 buf[i] = '\0';
116 return 0;
117}
118
119#else
120
121#include <termios.h>
122
123static struct termios oldtty;
124
125static void term_exit(void)
126{
127 tcsetattr (0, TCSANOW, &oldtty);
128}
129
130static void term_init(void)
131{
132 struct termios tty;
133
134 tcgetattr (0, &tty);
135 oldtty = tty;
136
137 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
138 |INLCR|IGNCR|ICRNL|IXON);
139 tty.c_oflag |= OPOST;
140 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
141 tty.c_cflag &= ~(CSIZE|PARENB);
142 tty.c_cflag |= CS8;
143 tty.c_cc[VMIN] = 1;
144 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000145
bellardea2384d2004-08-01 21:59:26 +0000146 tcsetattr (0, TCSANOW, &tty);
147
148 atexit(term_exit);
149}
150
pbrook3f379ab2007-11-11 03:33:13 +0000151static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000152{
153 uint8_t ch;
154 int i, ret;
155
156 printf("password: ");
157 fflush(stdout);
158 term_init();
159 i = 0;
160 for(;;) {
161 ret = read(0, &ch, 1);
162 if (ret == -1) {
163 if (errno == EAGAIN || errno == EINTR) {
164 continue;
165 } else {
166 ret = -1;
167 break;
168 }
169 } else if (ret == 0) {
170 ret = -1;
171 break;
172 } else {
173 if (ch == '\r') {
174 ret = 0;
175 break;
176 }
177 if (i < (buf_size - 1))
178 buf[i++] = ch;
179 }
180 }
181 term_exit();
182 buf[i] = '\0';
183 printf("\n");
184 return ret;
185}
186#endif
187
bellard75c23802004-08-27 21:28:58 +0000188static BlockDriverState *bdrv_new_open(const char *filename,
189 const char *fmt)
190{
191 BlockDriverState *bs;
192 BlockDriver *drv;
193 char password[256];
194
195 bs = bdrv_new("");
196 if (!bs)
197 error("Not enough memory");
198 if (fmt) {
199 drv = bdrv_find_format(fmt);
200 if (!drv)
201 error("Unknown file format '%s'", fmt);
202 } else {
203 drv = NULL;
204 }
aurel32137519c2008-11-30 19:12:49 +0000205 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellard75c23802004-08-27 21:28:58 +0000206 error("Could not open '%s'", filename);
207 }
208 if (bdrv_is_encrypted(bs)) {
209 printf("Disk image '%s' is encrypted.\n", filename);
210 if (read_password(password, sizeof(password)) < 0)
211 error("No password given");
212 if (bdrv_set_key(bs, password) < 0)
213 error("invalid password");
214 }
215 return bs;
216}
217
Kevin Wolfefa84d42009-05-18 16:42:12 +0200218static void add_old_style_options(const char *fmt, QEMUOptionParameter *list,
219 int flags, const char *base_filename, const char *base_fmt)
220{
221 if (flags & BLOCK_FLAG_ENCRYPT) {
222 if (set_option_parameter(list, BLOCK_OPT_ENCRYPT, "on")) {
223 error("Encryption not supported for file format '%s'", fmt);
224 }
225 }
226 if (flags & BLOCK_FLAG_COMPAT6) {
227 if (set_option_parameter(list, BLOCK_OPT_COMPAT6, "on")) {
228 error("VMDK version 6 not supported for file format '%s'", fmt);
229 }
230 }
231
232 if (base_filename) {
233 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
234 error("Backing file not supported for file format '%s'", fmt);
235 }
236 }
237 if (base_fmt) {
238 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
239 error("Backing file format not supported for file format '%s'", fmt);
240 }
241 }
242}
243
bellardea2384d2004-08-01 21:59:26 +0000244static int img_create(int argc, char **argv)
245{
thsec36ba12007-09-16 21:59:02 +0000246 int c, ret, flags;
bellardea2384d2004-08-01 21:59:26 +0000247 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000248 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000249 const char *filename;
250 const char *base_filename = NULL;
bellardea2384d2004-08-01 21:59:26 +0000251 BlockDriver *drv;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200252 QEMUOptionParameter *param = NULL;
253 char *options = NULL;
ths3b46e622007-09-17 08:09:54 +0000254
thsec36ba12007-09-16 21:59:02 +0000255 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000256 for(;;) {
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200257 c = getopt(argc, argv, "F:b:f:he6o:");
bellardea2384d2004-08-01 21:59:26 +0000258 if (c == -1)
259 break;
260 switch(c) {
261 case 'h':
262 help();
263 break;
aliguori9230eaf2009-03-28 17:55:19 +0000264 case 'F':
265 base_fmt = optarg;
266 break;
bellardea2384d2004-08-01 21:59:26 +0000267 case 'b':
268 base_filename = optarg;
269 break;
270 case 'f':
271 fmt = optarg;
272 break;
273 case 'e':
thsec36ba12007-09-16 21:59:02 +0000274 flags |= BLOCK_FLAG_ENCRYPT;
bellardea2384d2004-08-01 21:59:26 +0000275 break;
thsd8871c52007-10-24 16:11:42 +0000276 case '6':
thsec36ba12007-09-16 21:59:02 +0000277 flags |= BLOCK_FLAG_COMPAT6;
thsd8871c52007-10-24 16:11:42 +0000278 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200279 case 'o':
280 options = optarg;
281 break;
bellardea2384d2004-08-01 21:59:26 +0000282 }
283 }
ths5fafdf22007-09-16 21:08:06 +0000284 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000285 help();
286 filename = argv[optind++];
aliguori9230eaf2009-03-28 17:55:19 +0000287
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200288 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000289 drv = bdrv_find_format(fmt);
290 if (!drv)
291 error("Unknown file format '%s'", fmt);
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200292
293 if (options) {
294 param = parse_option_parameters(options, drv->create_options, param);
295 if (param == NULL) {
296 error("Invalid options for file format '%s'.", fmt);
297 }
298 } else {
299 param = parse_option_parameters("", drv->create_options, param);
bellard75c23802004-08-27 21:28:58 +0000300 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200301
302 /* Add size to parameters */
303 if (optind < argc) {
304 set_option_parameter(param, BLOCK_OPT_SIZE, argv[optind++]);
305 }
306
307 /* Add old-style options to parameters */
Kevin Wolfefa84d42009-05-18 16:42:12 +0200308 add_old_style_options(fmt, param, flags, base_filename, base_fmt);
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200309
310 // The size for the image must always be specified, with one exception:
311 // If we are using a backing file, we can obtain the size from there
312 if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == 0) {
313
314 QEMUOptionParameter *backing_file =
315 get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
316 QEMUOptionParameter *backing_fmt =
317 get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
318
319 if (backing_file && backing_file->value.s) {
320 BlockDriverState *bs;
321 uint64_t size;
322 const char *fmt = NULL;
323 char buf[32];
324
325 if (backing_fmt && backing_fmt->value.s) {
326 if (bdrv_find_format(backing_fmt->value.s)) {
327 fmt = backing_fmt->value.s;
328 } else {
329 error("Unknown backing file format '%s'",
330 backing_fmt->value.s);
331 }
332 }
333
334 bs = bdrv_new_open(backing_file->value.s, fmt);
335 bdrv_get_geometry(bs, &size);
336 size *= 512;
337 bdrv_delete(bs);
338
339 snprintf(buf, sizeof(buf), "%" PRId64, size);
340 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
341 } else {
342 error("Image creation needs a size parameter");
343 }
344 }
345
346 printf("Formatting '%s', fmt=%s ", filename, fmt);
347 print_option_parameters(param);
348 puts("");
349
350 ret = bdrv_create(drv, filename, param);
351 free_option_parameters(param);
352
bellardea2384d2004-08-01 21:59:26 +0000353 if (ret < 0) {
354 if (ret == -ENOTSUP) {
bellard3c565212004-09-29 21:29:14 +0000355 error("Formatting or formatting option not supported for file format '%s'", fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000356 } else if (ret == -EFBIG) {
357 error("The image size is too large for file format '%s'", fmt);
bellardea2384d2004-08-01 21:59:26 +0000358 } else {
359 error("Error while formatting");
360 }
361 }
362 return 0;
363}
364
aliguori15859692009-04-21 23:11:53 +0000365static int img_check(int argc, char **argv)
366{
367 int c, ret;
368 const char *filename, *fmt;
369 BlockDriver *drv;
370 BlockDriverState *bs;
371
372 fmt = NULL;
373 for(;;) {
374 c = getopt(argc, argv, "f:h");
375 if (c == -1)
376 break;
377 switch(c) {
378 case 'h':
379 help();
380 break;
381 case 'f':
382 fmt = optarg;
383 break;
384 }
385 }
386 if (optind >= argc)
387 help();
388 filename = argv[optind++];
389
390 bs = bdrv_new("");
391 if (!bs)
392 error("Not enough memory");
393 if (fmt) {
394 drv = bdrv_find_format(fmt);
395 if (!drv)
396 error("Unknown file format '%s'", fmt);
397 } else {
398 drv = NULL;
399 }
400 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
401 error("Could not open '%s'", filename);
402 }
403 ret = bdrv_check(bs);
404 switch(ret) {
405 case 0:
406 printf("No errors were found on the image.\n");
407 break;
408 case -ENOTSUP:
409 error("This image format does not support checks");
410 break;
411 default:
412 if (ret < 0) {
413 error("An error occurred during the check");
414 } else {
415 printf("%d errors were found on the image.\n", ret);
416 }
417 break;
418 }
419
420 bdrv_delete(bs);
421 return 0;
422}
423
bellardea2384d2004-08-01 21:59:26 +0000424static int img_commit(int argc, char **argv)
425{
426 int c, ret;
427 const char *filename, *fmt;
428 BlockDriver *drv;
429 BlockDriverState *bs;
430
431 fmt = NULL;
432 for(;;) {
433 c = getopt(argc, argv, "f:h");
434 if (c == -1)
435 break;
436 switch(c) {
437 case 'h':
438 help();
439 break;
440 case 'f':
441 fmt = optarg;
442 break;
443 }
444 }
ths5fafdf22007-09-16 21:08:06 +0000445 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000446 help();
447 filename = argv[optind++];
448
449 bs = bdrv_new("");
450 if (!bs)
451 error("Not enough memory");
452 if (fmt) {
453 drv = bdrv_find_format(fmt);
454 if (!drv)
455 error("Unknown file format '%s'", fmt);
456 } else {
457 drv = NULL;
458 }
aurel32137519c2008-11-30 19:12:49 +0000459 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000460 error("Could not open '%s'", filename);
461 }
462 ret = bdrv_commit(bs);
463 switch(ret) {
464 case 0:
465 printf("Image committed.\n");
466 break;
467 case -ENOENT:
468 error("No disk inserted");
469 break;
470 case -EACCES:
471 error("Image is read-only");
472 break;
473 case -ENOTSUP:
474 error("Image is already committed");
475 break;
476 default:
477 error("Error while committing image");
478 break;
479 }
480
481 bdrv_delete(bs);
482 return 0;
483}
484
485static int is_not_zero(const uint8_t *sector, int len)
486{
487 int i;
488 len >>= 2;
489 for(i = 0;i < len; i++) {
490 if (((uint32_t *)sector)[i] != 0)
491 return 1;
492 }
493 return 0;
494}
495
thsf58c7b32008-06-05 21:53:49 +0000496/*
497 * Returns true iff the first sector pointed to by 'buf' contains at least
498 * a non-NUL byte.
499 *
500 * 'pnum' is set to the number of sectors (including and immediately following
501 * the first one) that are known to be in the same allocated/unallocated state.
502 */
bellardea2384d2004-08-01 21:59:26 +0000503static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
504{
505 int v, i;
506
507 if (n <= 0) {
508 *pnum = 0;
509 return 0;
510 }
511 v = is_not_zero(buf, 512);
512 for(i = 1; i < n; i++) {
513 buf += 512;
514 if (v != is_not_zero(buf, 512))
515 break;
516 }
517 *pnum = i;
518 return v;
519}
520
bellardea2384d2004-08-01 21:59:26 +0000521#define IO_BUF_SIZE 65536
522
523static int img_convert(int argc, char **argv)
524{
balrog926c2d22007-10-31 01:11:44 +0000525 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
thsf58c7b32008-06-05 21:53:49 +0000526 const char *fmt, *out_fmt, *out_baseimg, *out_filename;
bellardea2384d2004-08-01 21:59:26 +0000527 BlockDriver *drv;
balrog926c2d22007-10-31 01:11:44 +0000528 BlockDriverState **bs, *out_bs;
ths96b8f132007-12-17 01:35:20 +0000529 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
530 uint64_t bs_sectors;
bellardea2384d2004-08-01 21:59:26 +0000531 uint8_t buf[IO_BUF_SIZE];
532 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +0000533 BlockDriverInfo bdi;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200534 QEMUOptionParameter *param = NULL;
535 char *options = NULL;
bellardea2384d2004-08-01 21:59:26 +0000536
537 fmt = NULL;
538 out_fmt = "raw";
thsf58c7b32008-06-05 21:53:49 +0000539 out_baseimg = NULL;
thsec36ba12007-09-16 21:59:02 +0000540 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000541 for(;;) {
Kevin Wolfefa84d42009-05-18 16:42:12 +0200542 c = getopt(argc, argv, "f:O:B:hce6o:");
bellardea2384d2004-08-01 21:59:26 +0000543 if (c == -1)
544 break;
545 switch(c) {
546 case 'h':
547 help();
548 break;
549 case 'f':
550 fmt = optarg;
551 break;
552 case 'O':
553 out_fmt = optarg;
554 break;
thsf58c7b32008-06-05 21:53:49 +0000555 case 'B':
556 out_baseimg = optarg;
557 break;
bellardea2384d2004-08-01 21:59:26 +0000558 case 'c':
thsec36ba12007-09-16 21:59:02 +0000559 flags |= BLOCK_FLAG_COMPRESS;
bellardea2384d2004-08-01 21:59:26 +0000560 break;
561 case 'e':
thsec36ba12007-09-16 21:59:02 +0000562 flags |= BLOCK_FLAG_ENCRYPT;
563 break;
564 case '6':
565 flags |= BLOCK_FLAG_COMPAT6;
bellardea2384d2004-08-01 21:59:26 +0000566 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200567 case 'o':
568 options = optarg;
569 break;
bellardea2384d2004-08-01 21:59:26 +0000570 }
571 }
ths3b46e622007-09-17 08:09:54 +0000572
balrog926c2d22007-10-31 01:11:44 +0000573 bs_n = argc - optind - 1;
574 if (bs_n < 1) help();
575
576 out_filename = argv[argc - 1];
thsf58c7b32008-06-05 21:53:49 +0000577
578 if (bs_n > 1 && out_baseimg)
579 error("-B makes no sense when concatenating multiple input images");
balrog926c2d22007-10-31 01:11:44 +0000580
581 bs = calloc(bs_n, sizeof(BlockDriverState *));
582 if (!bs)
583 error("Out of memory");
584
585 total_sectors = 0;
586 for (bs_i = 0; bs_i < bs_n; bs_i++) {
587 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
588 if (!bs[bs_i])
589 error("Could not open '%s'", argv[optind + bs_i]);
590 bdrv_get_geometry(bs[bs_i], &bs_sectors);
591 total_sectors += bs_sectors;
592 }
bellardea2384d2004-08-01 21:59:26 +0000593
Kevin Wolfefa84d42009-05-18 16:42:12 +0200594 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000595 drv = bdrv_find_format(out_fmt);
596 if (!drv)
thsd34dda52007-02-10 22:59:40 +0000597 error("Unknown file format '%s'", out_fmt);
balrog926c2d22007-10-31 01:11:44 +0000598
Kevin Wolfefa84d42009-05-18 16:42:12 +0200599 if (options) {
600 param = parse_option_parameters(options, drv->create_options, param);
601 if (param == NULL) {
602 error("Invalid options for file format '%s'.", out_fmt);
603 }
604 } else {
605 param = parse_option_parameters("", drv->create_options, param);
606 }
607
608 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
609 add_old_style_options(out_fmt, param, flags, out_baseimg, NULL);
610
611 /* Check if compression is supported */
612 if (flags & BLOCK_FLAG_COMPRESS) {
613 QEMUOptionParameter *encryption =
614 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
615
616 if (!drv->bdrv_write_compressed) {
617 error("Compression not supported for this file format");
618 }
619
620 if (encryption && encryption->value.n) {
621 error("Compression and encryption not supported at the same time");
622 }
623 }
624
625 /* Create the new image */
626 ret = bdrv_create(drv, out_filename, param);
627 free_option_parameters(param);
628
bellardea2384d2004-08-01 21:59:26 +0000629 if (ret < 0) {
630 if (ret == -ENOTSUP) {
aliguori93c65b42009-04-05 17:40:43 +0000631 error("Formatting not supported for file format '%s'", out_fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000632 } else if (ret == -EFBIG) {
633 error("The image size is too large for file format '%s'", out_fmt);
bellardea2384d2004-08-01 21:59:26 +0000634 } else {
635 error("Error while formatting '%s'", out_filename);
636 }
637 }
ths3b46e622007-09-17 08:09:54 +0000638
bellardea2384d2004-08-01 21:59:26 +0000639 out_bs = bdrv_new_open(out_filename, out_fmt);
640
balrog926c2d22007-10-31 01:11:44 +0000641 bs_i = 0;
642 bs_offset = 0;
643 bdrv_get_geometry(bs[0], &bs_sectors);
644
645 if (flags & BLOCK_FLAG_COMPRESS) {
bellardfaea38e2006-08-05 21:31:00 +0000646 if (bdrv_get_info(out_bs, &bdi) < 0)
647 error("could not get block driver info");
648 cluster_size = bdi.cluster_size;
bellardea2384d2004-08-01 21:59:26 +0000649 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
650 error("invalid cluster size");
651 cluster_sectors = cluster_size >> 9;
652 sector_num = 0;
653 for(;;) {
balrog926c2d22007-10-31 01:11:44 +0000654 int64_t bs_num;
655 int remainder;
656 uint8_t *buf2;
657
bellardea2384d2004-08-01 21:59:26 +0000658 nb_sectors = total_sectors - sector_num;
659 if (nb_sectors <= 0)
660 break;
661 if (nb_sectors >= cluster_sectors)
662 n = cluster_sectors;
663 else
664 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000665
666 bs_num = sector_num - bs_offset;
667 assert (bs_num >= 0);
668 remainder = n;
669 buf2 = buf;
670 while (remainder > 0) {
671 int nlow;
672 while (bs_num == bs_sectors) {
673 bs_i++;
674 assert (bs_i < bs_n);
675 bs_offset += bs_sectors;
676 bdrv_get_geometry(bs[bs_i], &bs_sectors);
677 bs_num = 0;
678 /* printf("changing part: sector_num=%lld, "
679 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
680 sector_num, bs_i, bs_offset, bs_sectors); */
681 }
682 assert (bs_num < bs_sectors);
683
684 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
685
686 if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0)
687 error("error while reading");
688
689 buf2 += nlow * 512;
690 bs_num += nlow;
691
692 remainder -= nlow;
693 }
694 assert (remainder == 0);
695
bellardea2384d2004-08-01 21:59:26 +0000696 if (n < cluster_sectors)
697 memset(buf + n * 512, 0, cluster_size - n * 512);
698 if (is_not_zero(buf, cluster_size)) {
ths5fafdf22007-09-16 21:08:06 +0000699 if (bdrv_write_compressed(out_bs, sector_num, buf,
bellardfaea38e2006-08-05 21:31:00 +0000700 cluster_sectors) != 0)
bellardec3757d2006-06-14 15:50:07 +0000701 error("error while compressing sector %" PRId64,
702 sector_num);
bellardea2384d2004-08-01 21:59:26 +0000703 }
704 sector_num += n;
705 }
bellardfaea38e2006-08-05 21:31:00 +0000706 /* signal EOF to align */
707 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +0000708 } else {
thsf58c7b32008-06-05 21:53:49 +0000709 sector_num = 0; // total number of sectors converted so far
bellardea2384d2004-08-01 21:59:26 +0000710 for(;;) {
711 nb_sectors = total_sectors - sector_num;
712 if (nb_sectors <= 0)
713 break;
714 if (nb_sectors >= (IO_BUF_SIZE / 512))
715 n = (IO_BUF_SIZE / 512);
716 else
717 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000718
719 while (sector_num - bs_offset >= bs_sectors) {
720 bs_i ++;
721 assert (bs_i < bs_n);
722 bs_offset += bs_sectors;
723 bdrv_get_geometry(bs[bs_i], &bs_sectors);
724 /* printf("changing part: sector_num=%lld, bs_i=%d, "
725 "bs_offset=%lld, bs_sectors=%lld\n",
726 sector_num, bs_i, bs_offset, bs_sectors); */
727 }
728
729 if (n > bs_offset + bs_sectors - sector_num)
730 n = bs_offset + bs_sectors - sector_num;
731
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500732 if (strcmp(drv->format_name, "host_device")) {
aliguori93c65b42009-04-05 17:40:43 +0000733 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
734 n, &n1)) {
735 sector_num += n1;
736 continue;
737 }
738 /* The next 'n1' sectors are allocated in the input image. Copy
739 only those as they may be followed by unallocated sectors. */
740 n = n1;
741 } else {
742 n1 = n;
thsf58c7b32008-06-05 21:53:49 +0000743 }
744
balrog926c2d22007-10-31 01:11:44 +0000745 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
bellardea2384d2004-08-01 21:59:26 +0000746 error("error while reading");
747 /* NOTE: at the same time we convert, we do not write zero
748 sectors to have a chance to compress the image. Ideally, we
749 should add a specific call to have the info to go faster */
750 buf1 = buf;
751 while (n > 0) {
thsf58c7b32008-06-05 21:53:49 +0000752 /* If the output image is being created as a copy on write image,
753 copy all sectors even the ones containing only NUL bytes,
aliguori93c65b42009-04-05 17:40:43 +0000754 because they may differ from the sectors in the base image.
755
756 If the output is to a host device, we also write out
757 sectors that are entirely 0, since whatever data was
758 already there is garbage, not 0s. */
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500759 if (strcmp(drv->format_name, "host_device") == 0 || out_baseimg ||
aliguori93c65b42009-04-05 17:40:43 +0000760 is_allocated_sectors(buf1, n, &n1)) {
ths5fafdf22007-09-16 21:08:06 +0000761 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
bellardea2384d2004-08-01 21:59:26 +0000762 error("error while writing");
763 }
764 sector_num += n1;
765 n -= n1;
766 buf1 += n1 * 512;
767 }
768 }
769 }
770 bdrv_delete(out_bs);
balrog926c2d22007-10-31 01:11:44 +0000771 for (bs_i = 0; bs_i < bs_n; bs_i++)
772 bdrv_delete(bs[bs_i]);
773 free(bs);
bellardea2384d2004-08-01 21:59:26 +0000774 return 0;
775}
776
bellard57d1a2b2004-08-03 21:15:11 +0000777#ifdef _WIN32
778static int64_t get_allocated_file_size(const char *filename)
779{
bellarde8445332006-06-14 15:32:10 +0000780 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
781 get_compressed_t get_compressed;
bellard57d1a2b2004-08-03 21:15:11 +0000782 struct _stati64 st;
bellarde8445332006-06-14 15:32:10 +0000783
784 /* WinNT support GetCompressedFileSize to determine allocate size */
785 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
786 if (get_compressed) {
787 DWORD high, low;
788 low = get_compressed(filename, &high);
789 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
790 return (((int64_t) high) << 32) + low;
791 }
792
ths5fafdf22007-09-16 21:08:06 +0000793 if (_stati64(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000794 return -1;
795 return st.st_size;
796}
797#else
798static int64_t get_allocated_file_size(const char *filename)
799{
800 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000801 if (stat(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000802 return -1;
803 return (int64_t)st.st_blocks * 512;
804}
805#endif
806
bellardfaea38e2006-08-05 21:31:00 +0000807static void dump_snapshots(BlockDriverState *bs)
808{
809 QEMUSnapshotInfo *sn_tab, *sn;
810 int nb_sns, i;
811 char buf[256];
812
813 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
814 if (nb_sns <= 0)
815 return;
816 printf("Snapshot list:\n");
817 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
818 for(i = 0; i < nb_sns; i++) {
819 sn = &sn_tab[i];
820 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
821 }
822 qemu_free(sn_tab);
823}
824
bellardea2384d2004-08-01 21:59:26 +0000825static int img_info(int argc, char **argv)
826{
827 int c;
828 const char *filename, *fmt;
829 BlockDriver *drv;
830 BlockDriverState *bs;
831 char fmt_name[128], size_buf[128], dsize_buf[128];
ths96b8f132007-12-17 01:35:20 +0000832 uint64_t total_sectors;
833 int64_t allocated_size;
bellard93b6b2a2006-08-01 15:51:11 +0000834 char backing_filename[1024];
835 char backing_filename2[1024];
bellardfaea38e2006-08-05 21:31:00 +0000836 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +0000837
838 fmt = NULL;
839 for(;;) {
840 c = getopt(argc, argv, "f:h");
841 if (c == -1)
842 break;
843 switch(c) {
844 case 'h':
845 help();
846 break;
847 case 'f':
848 fmt = optarg;
849 break;
850 }
851 }
ths5fafdf22007-09-16 21:08:06 +0000852 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000853 help();
854 filename = argv[optind++];
855
856 bs = bdrv_new("");
857 if (!bs)
858 error("Not enough memory");
859 if (fmt) {
860 drv = bdrv_find_format(fmt);
861 if (!drv)
862 error("Unknown file format '%s'", fmt);
863 } else {
864 drv = NULL;
865 }
aurel32137519c2008-11-30 19:12:49 +0000866 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000867 error("Could not open '%s'", filename);
868 }
869 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
870 bdrv_get_geometry(bs, &total_sectors);
871 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
bellard57d1a2b2004-08-03 21:15:11 +0000872 allocated_size = get_allocated_file_size(filename);
873 if (allocated_size < 0)
blueswir1a10ea302008-08-24 10:30:33 +0000874 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
bellardde167e42005-04-28 21:15:08 +0000875 else
ths5fafdf22007-09-16 21:08:06 +0000876 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
bellardde167e42005-04-28 21:15:08 +0000877 allocated_size);
bellardea2384d2004-08-01 21:59:26 +0000878 printf("image: %s\n"
879 "file format: %s\n"
bellardec3757d2006-06-14 15:50:07 +0000880 "virtual size: %s (%" PRId64 " bytes)\n"
bellardea2384d2004-08-01 21:59:26 +0000881 "disk size: %s\n",
ths5fafdf22007-09-16 21:08:06 +0000882 filename, fmt_name, size_buf,
bellardec3757d2006-06-14 15:50:07 +0000883 (total_sectors * 512),
bellardea2384d2004-08-01 21:59:26 +0000884 dsize_buf);
885 if (bdrv_is_encrypted(bs))
886 printf("encrypted: yes\n");
bellardfaea38e2006-08-05 21:31:00 +0000887 if (bdrv_get_info(bs, &bdi) >= 0) {
ths5fafdf22007-09-16 21:08:06 +0000888 if (bdi.cluster_size != 0)
bellardfaea38e2006-08-05 21:31:00 +0000889 printf("cluster_size: %d\n", bdi.cluster_size);
890 }
bellard93b6b2a2006-08-01 15:51:11 +0000891 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
bellardfaea38e2006-08-05 21:31:00 +0000892 if (backing_filename[0] != '\0') {
bellard93b6b2a2006-08-01 15:51:11 +0000893 path_combine(backing_filename2, sizeof(backing_filename2),
894 filename, backing_filename);
ths5fafdf22007-09-16 21:08:06 +0000895 printf("backing file: %s (actual path: %s)\n",
bellard93b6b2a2006-08-01 15:51:11 +0000896 backing_filename,
897 backing_filename2);
bellardfaea38e2006-08-05 21:31:00 +0000898 }
899 dump_snapshots(bs);
bellardea2384d2004-08-01 21:59:26 +0000900 bdrv_delete(bs);
901 return 0;
902}
903
aliguorif7b4a942009-01-07 17:40:15 +0000904#define SNAPSHOT_LIST 1
905#define SNAPSHOT_CREATE 2
906#define SNAPSHOT_APPLY 3
907#define SNAPSHOT_DELETE 4
908
909static void img_snapshot(int argc, char **argv)
910{
911 BlockDriverState *bs;
912 QEMUSnapshotInfo sn;
913 char *filename, *snapshot_name = NULL;
aliguori40a45392009-01-15 21:42:12 +0000914 int c, ret;
aliguorif7b4a942009-01-07 17:40:15 +0000915 int action = 0;
916 qemu_timeval tv;
917
918 /* Parse commandline parameters */
919 for(;;) {
920 c = getopt(argc, argv, "la:c:d:h");
921 if (c == -1)
922 break;
923 switch(c) {
924 case 'h':
925 help();
926 return;
927 case 'l':
928 if (action) {
929 help();
930 return;
931 }
932 action = SNAPSHOT_LIST;
933 break;
934 case 'a':
935 if (action) {
936 help();
937 return;
938 }
939 action = SNAPSHOT_APPLY;
940 snapshot_name = optarg;
941 break;
942 case 'c':
943 if (action) {
944 help();
945 return;
946 }
947 action = SNAPSHOT_CREATE;
948 snapshot_name = optarg;
949 break;
950 case 'd':
951 if (action) {
952 help();
953 return;
954 }
955 action = SNAPSHOT_DELETE;
956 snapshot_name = optarg;
957 break;
958 }
959 }
960
961 if (optind >= argc)
962 help();
963 filename = argv[optind++];
964
965 /* Open the image */
966 bs = bdrv_new("");
967 if (!bs)
968 error("Not enough memory");
969
970 if (bdrv_open2(bs, filename, 0, NULL) < 0) {
971 error("Could not open '%s'", filename);
972 }
973
974 /* Perform the requested action */
975 switch(action) {
976 case SNAPSHOT_LIST:
977 dump_snapshots(bs);
978 break;
979
980 case SNAPSHOT_CREATE:
981 memset(&sn, 0, sizeof(sn));
982 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
983
984 qemu_gettimeofday(&tv);
985 sn.date_sec = tv.tv_sec;
986 sn.date_nsec = tv.tv_usec * 1000;
987
988 ret = bdrv_snapshot_create(bs, &sn);
989 if (ret)
990 error("Could not create snapshot '%s': %d (%s)",
991 snapshot_name, ret, strerror(-ret));
992 break;
993
994 case SNAPSHOT_APPLY:
995 ret = bdrv_snapshot_goto(bs, snapshot_name);
996 if (ret)
997 error("Could not apply snapshot '%s': %d (%s)",
998 snapshot_name, ret, strerror(-ret));
999 break;
1000
1001 case SNAPSHOT_DELETE:
1002 ret = bdrv_snapshot_delete(bs, snapshot_name);
1003 if (ret)
1004 error("Could not delete snapshot '%s': %d (%s)",
1005 snapshot_name, ret, strerror(-ret));
1006 break;
1007 }
1008
1009 /* Cleanup */
1010 bdrv_delete(bs);
1011}
1012
bellardea2384d2004-08-01 21:59:26 +00001013int main(int argc, char **argv)
1014{
1015 const char *cmd;
1016
1017 bdrv_init();
1018 if (argc < 2)
1019 help();
1020 cmd = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00001021 argc--; argv++;
bellardea2384d2004-08-01 21:59:26 +00001022 if (!strcmp(cmd, "create")) {
1023 img_create(argc, argv);
aliguori15859692009-04-21 23:11:53 +00001024 } else if (!strcmp(cmd, "check")) {
1025 img_check(argc, argv);
bellardea2384d2004-08-01 21:59:26 +00001026 } else if (!strcmp(cmd, "commit")) {
1027 img_commit(argc, argv);
1028 } else if (!strcmp(cmd, "convert")) {
1029 img_convert(argc, argv);
1030 } else if (!strcmp(cmd, "info")) {
1031 img_info(argc, argv);
aliguorif7b4a942009-01-07 17:40:15 +00001032 } else if (!strcmp(cmd, "snapshot")) {
1033 img_snapshot(argc, argv);
bellardea2384d2004-08-01 21:59:26 +00001034 } else {
1035 help();
1036 }
1037 return 0;
1038}