blob: aff3980806b796ea597f4c1274b516750ffabb93 [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"
Kevin Wolfeff44262009-06-04 15:39:39 +020062 " create [-F fmt] [-b base_image] [-f fmt] [-o options] filename [size]\n"
bellardea2384d2004-08-01 21:59:26 +000063 " commit [-f fmt] filename\n"
Kevin Wolfeff44262009-06-04 15:39:39 +020064 " convert [-c] [-f fmt] [-O output_fmt] [-o options] [-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"
Kevin Wolfeff44262009-06-04 15:39:39 +020082 " 'options' is a comma separated list of format specific options in a\n"
83 " name=value format. Use -o ? for an overview of the options supported by the\n"
84 " used format\n"
bellardea2384d2004-08-01 21:59:26 +000085 " '-c' indicates that target image must be compressed (qcow format only)\n"
blueswir1d2c639d2009-01-24 18:19:25 +000086 " '-h' with or without a command shows this help and lists the supported formats\n"
aliguorif7b4a942009-01-07 17:40:15 +000087 "\n"
blueswir1d2c639d2009-01-24 18:19:25 +000088 "Parameters to snapshot subcommand:\n"
89 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
90 " '-a' applies a snapshot (revert disk to saved state)\n"
91 " '-c' creates a snapshot\n"
92 " '-d' deletes a snapshot\n"
93 " '-l' lists all snapshots in the given image\n"
bellardea2384d2004-08-01 21:59:26 +000094 );
blueswir1d2c639d2009-01-24 18:19:25 +000095 printf("\nSupported formats:");
bellardea2384d2004-08-01 21:59:26 +000096 bdrv_iterate_format(format_print, NULL);
97 printf("\n");
98 exit(1);
99}
100
bellardea2384d2004-08-01 21:59:26 +0000101#if defined(WIN32)
102/* XXX: put correct support for win32 */
103static int read_password(char *buf, int buf_size)
104{
105 int c, i;
106 printf("Password: ");
107 fflush(stdout);
108 i = 0;
109 for(;;) {
110 c = getchar();
111 if (c == '\n')
112 break;
113 if (i < (buf_size - 1))
114 buf[i++] = c;
115 }
116 buf[i] = '\0';
117 return 0;
118}
119
120#else
121
122#include <termios.h>
123
124static struct termios oldtty;
125
126static void term_exit(void)
127{
128 tcsetattr (0, TCSANOW, &oldtty);
129}
130
131static void term_init(void)
132{
133 struct termios tty;
134
135 tcgetattr (0, &tty);
136 oldtty = tty;
137
138 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
139 |INLCR|IGNCR|ICRNL|IXON);
140 tty.c_oflag |= OPOST;
141 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
142 tty.c_cflag &= ~(CSIZE|PARENB);
143 tty.c_cflag |= CS8;
144 tty.c_cc[VMIN] = 1;
145 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000146
bellardea2384d2004-08-01 21:59:26 +0000147 tcsetattr (0, TCSANOW, &tty);
148
149 atexit(term_exit);
150}
151
pbrook3f379ab2007-11-11 03:33:13 +0000152static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000153{
154 uint8_t ch;
155 int i, ret;
156
157 printf("password: ");
158 fflush(stdout);
159 term_init();
160 i = 0;
161 for(;;) {
162 ret = read(0, &ch, 1);
163 if (ret == -1) {
164 if (errno == EAGAIN || errno == EINTR) {
165 continue;
166 } else {
167 ret = -1;
168 break;
169 }
170 } else if (ret == 0) {
171 ret = -1;
172 break;
173 } else {
174 if (ch == '\r') {
175 ret = 0;
176 break;
177 }
178 if (i < (buf_size - 1))
179 buf[i++] = ch;
180 }
181 }
182 term_exit();
183 buf[i] = '\0';
184 printf("\n");
185 return ret;
186}
187#endif
188
bellard75c23802004-08-27 21:28:58 +0000189static BlockDriverState *bdrv_new_open(const char *filename,
190 const char *fmt)
191{
192 BlockDriverState *bs;
193 BlockDriver *drv;
194 char password[256];
195
196 bs = bdrv_new("");
197 if (!bs)
198 error("Not enough memory");
199 if (fmt) {
200 drv = bdrv_find_format(fmt);
201 if (!drv)
202 error("Unknown file format '%s'", fmt);
203 } else {
204 drv = NULL;
205 }
aurel32137519c2008-11-30 19:12:49 +0000206 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellard75c23802004-08-27 21:28:58 +0000207 error("Could not open '%s'", filename);
208 }
209 if (bdrv_is_encrypted(bs)) {
210 printf("Disk image '%s' is encrypted.\n", filename);
211 if (read_password(password, sizeof(password)) < 0)
212 error("No password given");
213 if (bdrv_set_key(bs, password) < 0)
214 error("invalid password");
215 }
216 return bs;
217}
218
Kevin Wolfefa84d42009-05-18 16:42:12 +0200219static void add_old_style_options(const char *fmt, QEMUOptionParameter *list,
220 int flags, const char *base_filename, const char *base_fmt)
221{
222 if (flags & BLOCK_FLAG_ENCRYPT) {
223 if (set_option_parameter(list, BLOCK_OPT_ENCRYPT, "on")) {
224 error("Encryption not supported for file format '%s'", fmt);
225 }
226 }
227 if (flags & BLOCK_FLAG_COMPAT6) {
228 if (set_option_parameter(list, BLOCK_OPT_COMPAT6, "on")) {
229 error("VMDK version 6 not supported for file format '%s'", fmt);
230 }
231 }
232
233 if (base_filename) {
234 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
235 error("Backing file not supported for file format '%s'", fmt);
236 }
237 }
238 if (base_fmt) {
239 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
240 error("Backing file format not supported for file format '%s'", fmt);
241 }
242 }
243}
244
bellardea2384d2004-08-01 21:59:26 +0000245static int img_create(int argc, char **argv)
246{
thsec36ba12007-09-16 21:59:02 +0000247 int c, ret, flags;
bellardea2384d2004-08-01 21:59:26 +0000248 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000249 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000250 const char *filename;
251 const char *base_filename = NULL;
bellardea2384d2004-08-01 21:59:26 +0000252 BlockDriver *drv;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200253 QEMUOptionParameter *param = NULL;
254 char *options = NULL;
ths3b46e622007-09-17 08:09:54 +0000255
thsec36ba12007-09-16 21:59:02 +0000256 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000257 for(;;) {
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200258 c = getopt(argc, argv, "F:b:f:he6o:");
bellardea2384d2004-08-01 21:59:26 +0000259 if (c == -1)
260 break;
261 switch(c) {
262 case 'h':
263 help();
264 break;
aliguori9230eaf2009-03-28 17:55:19 +0000265 case 'F':
266 base_fmt = optarg;
267 break;
bellardea2384d2004-08-01 21:59:26 +0000268 case 'b':
269 base_filename = optarg;
270 break;
271 case 'f':
272 fmt = optarg;
273 break;
274 case 'e':
thsec36ba12007-09-16 21:59:02 +0000275 flags |= BLOCK_FLAG_ENCRYPT;
bellardea2384d2004-08-01 21:59:26 +0000276 break;
thsd8871c52007-10-24 16:11:42 +0000277 case '6':
thsec36ba12007-09-16 21:59:02 +0000278 flags |= BLOCK_FLAG_COMPAT6;
thsd8871c52007-10-24 16:11:42 +0000279 break;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200280 case 'o':
281 options = optarg;
282 break;
bellardea2384d2004-08-01 21:59:26 +0000283 }
284 }
aliguori9230eaf2009-03-28 17:55:19 +0000285
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200286 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000287 drv = bdrv_find_format(fmt);
288 if (!drv)
289 error("Unknown file format '%s'", fmt);
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200290
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200291 if (options && !strcmp(options, "?")) {
292 print_option_help(drv->create_options);
293 return 0;
294 }
295
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200296 if (options) {
297 param = parse_option_parameters(options, drv->create_options, param);
298 if (param == NULL) {
299 error("Invalid options for file format '%s'.", fmt);
300 }
301 } else {
302 param = parse_option_parameters("", drv->create_options, param);
bellard75c23802004-08-27 21:28:58 +0000303 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200304
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200305 /* Get the filename */
306 if (optind >= argc)
307 help();
308 filename = argv[optind++];
309
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200310 /* Add size to parameters */
311 if (optind < argc) {
312 set_option_parameter(param, BLOCK_OPT_SIZE, argv[optind++]);
313 }
314
315 /* Add old-style options to parameters */
Kevin Wolfefa84d42009-05-18 16:42:12 +0200316 add_old_style_options(fmt, param, flags, base_filename, base_fmt);
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200317
318 // The size for the image must always be specified, with one exception:
319 // If we are using a backing file, we can obtain the size from there
320 if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == 0) {
321
322 QEMUOptionParameter *backing_file =
323 get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
324 QEMUOptionParameter *backing_fmt =
325 get_option_parameter(param, BLOCK_OPT_BACKING_FMT);
326
327 if (backing_file && backing_file->value.s) {
328 BlockDriverState *bs;
329 uint64_t size;
330 const char *fmt = NULL;
331 char buf[32];
332
333 if (backing_fmt && backing_fmt->value.s) {
334 if (bdrv_find_format(backing_fmt->value.s)) {
335 fmt = backing_fmt->value.s;
336 } else {
337 error("Unknown backing file format '%s'",
338 backing_fmt->value.s);
339 }
340 }
341
342 bs = bdrv_new_open(backing_file->value.s, fmt);
343 bdrv_get_geometry(bs, &size);
344 size *= 512;
345 bdrv_delete(bs);
346
347 snprintf(buf, sizeof(buf), "%" PRId64, size);
348 set_option_parameter(param, BLOCK_OPT_SIZE, buf);
349 } else {
350 error("Image creation needs a size parameter");
351 }
352 }
353
354 printf("Formatting '%s', fmt=%s ", filename, fmt);
355 print_option_parameters(param);
356 puts("");
357
358 ret = bdrv_create(drv, filename, param);
359 free_option_parameters(param);
360
bellardea2384d2004-08-01 21:59:26 +0000361 if (ret < 0) {
362 if (ret == -ENOTSUP) {
bellard3c565212004-09-29 21:29:14 +0000363 error("Formatting or formatting option not supported for file format '%s'", fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000364 } else if (ret == -EFBIG) {
365 error("The image size is too large for file format '%s'", fmt);
bellardea2384d2004-08-01 21:59:26 +0000366 } else {
367 error("Error while formatting");
368 }
369 }
370 return 0;
371}
372
aliguori15859692009-04-21 23:11:53 +0000373static int img_check(int argc, char **argv)
374{
375 int c, ret;
376 const char *filename, *fmt;
377 BlockDriver *drv;
378 BlockDriverState *bs;
379
380 fmt = NULL;
381 for(;;) {
382 c = getopt(argc, argv, "f:h");
383 if (c == -1)
384 break;
385 switch(c) {
386 case 'h':
387 help();
388 break;
389 case 'f':
390 fmt = optarg;
391 break;
392 }
393 }
394 if (optind >= argc)
395 help();
396 filename = argv[optind++];
397
398 bs = bdrv_new("");
399 if (!bs)
400 error("Not enough memory");
401 if (fmt) {
402 drv = bdrv_find_format(fmt);
403 if (!drv)
404 error("Unknown file format '%s'", fmt);
405 } else {
406 drv = NULL;
407 }
408 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
409 error("Could not open '%s'", filename);
410 }
411 ret = bdrv_check(bs);
412 switch(ret) {
413 case 0:
414 printf("No errors were found on the image.\n");
415 break;
416 case -ENOTSUP:
417 error("This image format does not support checks");
418 break;
419 default:
420 if (ret < 0) {
421 error("An error occurred during the check");
422 } else {
423 printf("%d errors were found on the image.\n", ret);
424 }
425 break;
426 }
427
428 bdrv_delete(bs);
429 return 0;
430}
431
bellardea2384d2004-08-01 21:59:26 +0000432static int img_commit(int argc, char **argv)
433{
434 int c, ret;
435 const char *filename, *fmt;
436 BlockDriver *drv;
437 BlockDriverState *bs;
438
439 fmt = NULL;
440 for(;;) {
441 c = getopt(argc, argv, "f:h");
442 if (c == -1)
443 break;
444 switch(c) {
445 case 'h':
446 help();
447 break;
448 case 'f':
449 fmt = optarg;
450 break;
451 }
452 }
ths5fafdf22007-09-16 21:08:06 +0000453 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000454 help();
455 filename = argv[optind++];
456
457 bs = bdrv_new("");
458 if (!bs)
459 error("Not enough memory");
460 if (fmt) {
461 drv = bdrv_find_format(fmt);
462 if (!drv)
463 error("Unknown file format '%s'", fmt);
464 } else {
465 drv = NULL;
466 }
aurel32137519c2008-11-30 19:12:49 +0000467 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000468 error("Could not open '%s'", filename);
469 }
470 ret = bdrv_commit(bs);
471 switch(ret) {
472 case 0:
473 printf("Image committed.\n");
474 break;
475 case -ENOENT:
476 error("No disk inserted");
477 break;
478 case -EACCES:
479 error("Image is read-only");
480 break;
481 case -ENOTSUP:
482 error("Image is already committed");
483 break;
484 default:
485 error("Error while committing image");
486 break;
487 }
488
489 bdrv_delete(bs);
490 return 0;
491}
492
493static int is_not_zero(const uint8_t *sector, int len)
494{
495 int i;
496 len >>= 2;
497 for(i = 0;i < len; i++) {
498 if (((uint32_t *)sector)[i] != 0)
499 return 1;
500 }
501 return 0;
502}
503
thsf58c7b32008-06-05 21:53:49 +0000504/*
505 * Returns true iff the first sector pointed to by 'buf' contains at least
506 * a non-NUL byte.
507 *
508 * 'pnum' is set to the number of sectors (including and immediately following
509 * the first one) that are known to be in the same allocated/unallocated state.
510 */
bellardea2384d2004-08-01 21:59:26 +0000511static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
512{
513 int v, i;
514
515 if (n <= 0) {
516 *pnum = 0;
517 return 0;
518 }
519 v = is_not_zero(buf, 512);
520 for(i = 1; i < n; i++) {
521 buf += 512;
522 if (v != is_not_zero(buf, 512))
523 break;
524 }
525 *pnum = i;
526 return v;
527}
528
bellardea2384d2004-08-01 21:59:26 +0000529#define IO_BUF_SIZE 65536
530
531static int img_convert(int argc, char **argv)
532{
balrog926c2d22007-10-31 01:11:44 +0000533 int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors;
thsf58c7b32008-06-05 21:53:49 +0000534 const char *fmt, *out_fmt, *out_baseimg, *out_filename;
bellardea2384d2004-08-01 21:59:26 +0000535 BlockDriver *drv;
balrog926c2d22007-10-31 01:11:44 +0000536 BlockDriverState **bs, *out_bs;
ths96b8f132007-12-17 01:35:20 +0000537 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
538 uint64_t bs_sectors;
bellardea2384d2004-08-01 21:59:26 +0000539 uint8_t buf[IO_BUF_SIZE];
540 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +0000541 BlockDriverInfo bdi;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200542 QEMUOptionParameter *param = NULL;
543 char *options = NULL;
bellardea2384d2004-08-01 21:59:26 +0000544
545 fmt = NULL;
546 out_fmt = "raw";
thsf58c7b32008-06-05 21:53:49 +0000547 out_baseimg = NULL;
thsec36ba12007-09-16 21:59:02 +0000548 flags = 0;
bellardea2384d2004-08-01 21:59:26 +0000549 for(;;) {
Kevin Wolfefa84d42009-05-18 16:42:12 +0200550 c = getopt(argc, argv, "f:O:B:hce6o:");
bellardea2384d2004-08-01 21:59:26 +0000551 if (c == -1)
552 break;
553 switch(c) {
554 case 'h':
555 help();
556 break;
557 case 'f':
558 fmt = optarg;
559 break;
560 case 'O':
561 out_fmt = optarg;
562 break;
thsf58c7b32008-06-05 21:53:49 +0000563 case 'B':
564 out_baseimg = optarg;
565 break;
bellardea2384d2004-08-01 21:59:26 +0000566 case 'c':
thsec36ba12007-09-16 21:59:02 +0000567 flags |= BLOCK_FLAG_COMPRESS;
bellardea2384d2004-08-01 21:59:26 +0000568 break;
569 case 'e':
thsec36ba12007-09-16 21:59:02 +0000570 flags |= BLOCK_FLAG_ENCRYPT;
571 break;
572 case '6':
573 flags |= BLOCK_FLAG_COMPAT6;
bellardea2384d2004-08-01 21:59:26 +0000574 break;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200575 case 'o':
576 options = optarg;
577 break;
bellardea2384d2004-08-01 21:59:26 +0000578 }
579 }
ths3b46e622007-09-17 08:09:54 +0000580
balrog926c2d22007-10-31 01:11:44 +0000581 bs_n = argc - optind - 1;
582 if (bs_n < 1) help();
583
584 out_filename = argv[argc - 1];
thsf58c7b32008-06-05 21:53:49 +0000585
586 if (bs_n > 1 && out_baseimg)
587 error("-B makes no sense when concatenating multiple input images");
balrog926c2d22007-10-31 01:11:44 +0000588
589 bs = calloc(bs_n, sizeof(BlockDriverState *));
590 if (!bs)
591 error("Out of memory");
592
593 total_sectors = 0;
594 for (bs_i = 0; bs_i < bs_n; bs_i++) {
595 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt);
596 if (!bs[bs_i])
597 error("Could not open '%s'", argv[optind + bs_i]);
598 bdrv_get_geometry(bs[bs_i], &bs_sectors);
599 total_sectors += bs_sectors;
600 }
bellardea2384d2004-08-01 21:59:26 +0000601
Kevin Wolfefa84d42009-05-18 16:42:12 +0200602 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000603 drv = bdrv_find_format(out_fmt);
604 if (!drv)
thsd34dda52007-02-10 22:59:40 +0000605 error("Unknown file format '%s'", out_fmt);
balrog926c2d22007-10-31 01:11:44 +0000606
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200607 if (options && !strcmp(options, "?")) {
608 print_option_help(drv->create_options);
609 return 0;
610 }
611
Kevin Wolfefa84d42009-05-18 16:42:12 +0200612 if (options) {
613 param = parse_option_parameters(options, drv->create_options, param);
614 if (param == NULL) {
615 error("Invalid options for file format '%s'.", out_fmt);
616 }
617 } else {
618 param = parse_option_parameters("", drv->create_options, param);
619 }
620
621 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
622 add_old_style_options(out_fmt, param, flags, out_baseimg, NULL);
623
624 /* Check if compression is supported */
625 if (flags & BLOCK_FLAG_COMPRESS) {
626 QEMUOptionParameter *encryption =
627 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
628
629 if (!drv->bdrv_write_compressed) {
630 error("Compression not supported for this file format");
631 }
632
633 if (encryption && encryption->value.n) {
634 error("Compression and encryption not supported at the same time");
635 }
636 }
637
638 /* Create the new image */
639 ret = bdrv_create(drv, out_filename, param);
640 free_option_parameters(param);
641
bellardea2384d2004-08-01 21:59:26 +0000642 if (ret < 0) {
643 if (ret == -ENOTSUP) {
aliguori93c65b42009-04-05 17:40:43 +0000644 error("Formatting not supported for file format '%s'", out_fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000645 } else if (ret == -EFBIG) {
646 error("The image size is too large for file format '%s'", out_fmt);
bellardea2384d2004-08-01 21:59:26 +0000647 } else {
648 error("Error while formatting '%s'", out_filename);
649 }
650 }
ths3b46e622007-09-17 08:09:54 +0000651
bellardea2384d2004-08-01 21:59:26 +0000652 out_bs = bdrv_new_open(out_filename, out_fmt);
653
balrog926c2d22007-10-31 01:11:44 +0000654 bs_i = 0;
655 bs_offset = 0;
656 bdrv_get_geometry(bs[0], &bs_sectors);
657
658 if (flags & BLOCK_FLAG_COMPRESS) {
bellardfaea38e2006-08-05 21:31:00 +0000659 if (bdrv_get_info(out_bs, &bdi) < 0)
660 error("could not get block driver info");
661 cluster_size = bdi.cluster_size;
bellardea2384d2004-08-01 21:59:26 +0000662 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE)
663 error("invalid cluster size");
664 cluster_sectors = cluster_size >> 9;
665 sector_num = 0;
666 for(;;) {
balrog926c2d22007-10-31 01:11:44 +0000667 int64_t bs_num;
668 int remainder;
669 uint8_t *buf2;
670
bellardea2384d2004-08-01 21:59:26 +0000671 nb_sectors = total_sectors - sector_num;
672 if (nb_sectors <= 0)
673 break;
674 if (nb_sectors >= cluster_sectors)
675 n = cluster_sectors;
676 else
677 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000678
679 bs_num = sector_num - bs_offset;
680 assert (bs_num >= 0);
681 remainder = n;
682 buf2 = buf;
683 while (remainder > 0) {
684 int nlow;
685 while (bs_num == bs_sectors) {
686 bs_i++;
687 assert (bs_i < bs_n);
688 bs_offset += bs_sectors;
689 bdrv_get_geometry(bs[bs_i], &bs_sectors);
690 bs_num = 0;
691 /* printf("changing part: sector_num=%lld, "
692 "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n",
693 sector_num, bs_i, bs_offset, bs_sectors); */
694 }
695 assert (bs_num < bs_sectors);
696
697 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
698
699 if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0)
700 error("error while reading");
701
702 buf2 += nlow * 512;
703 bs_num += nlow;
704
705 remainder -= nlow;
706 }
707 assert (remainder == 0);
708
bellardea2384d2004-08-01 21:59:26 +0000709 if (n < cluster_sectors)
710 memset(buf + n * 512, 0, cluster_size - n * 512);
711 if (is_not_zero(buf, cluster_size)) {
ths5fafdf22007-09-16 21:08:06 +0000712 if (bdrv_write_compressed(out_bs, sector_num, buf,
bellardfaea38e2006-08-05 21:31:00 +0000713 cluster_sectors) != 0)
bellardec3757d2006-06-14 15:50:07 +0000714 error("error while compressing sector %" PRId64,
715 sector_num);
bellardea2384d2004-08-01 21:59:26 +0000716 }
717 sector_num += n;
718 }
bellardfaea38e2006-08-05 21:31:00 +0000719 /* signal EOF to align */
720 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +0000721 } else {
thsf58c7b32008-06-05 21:53:49 +0000722 sector_num = 0; // total number of sectors converted so far
bellardea2384d2004-08-01 21:59:26 +0000723 for(;;) {
724 nb_sectors = total_sectors - sector_num;
725 if (nb_sectors <= 0)
726 break;
727 if (nb_sectors >= (IO_BUF_SIZE / 512))
728 n = (IO_BUF_SIZE / 512);
729 else
730 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +0000731
732 while (sector_num - bs_offset >= bs_sectors) {
733 bs_i ++;
734 assert (bs_i < bs_n);
735 bs_offset += bs_sectors;
736 bdrv_get_geometry(bs[bs_i], &bs_sectors);
737 /* printf("changing part: sector_num=%lld, bs_i=%d, "
738 "bs_offset=%lld, bs_sectors=%lld\n",
739 sector_num, bs_i, bs_offset, bs_sectors); */
740 }
741
742 if (n > bs_offset + bs_sectors - sector_num)
743 n = bs_offset + bs_sectors - sector_num;
744
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500745 if (strcmp(drv->format_name, "host_device")) {
aliguori93c65b42009-04-05 17:40:43 +0000746 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
747 n, &n1)) {
748 sector_num += n1;
749 continue;
750 }
751 /* The next 'n1' sectors are allocated in the input image. Copy
752 only those as they may be followed by unallocated sectors. */
753 n = n1;
754 } else {
755 n1 = n;
thsf58c7b32008-06-05 21:53:49 +0000756 }
757
balrog926c2d22007-10-31 01:11:44 +0000758 if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0)
bellardea2384d2004-08-01 21:59:26 +0000759 error("error while reading");
760 /* NOTE: at the same time we convert, we do not write zero
761 sectors to have a chance to compress the image. Ideally, we
762 should add a specific call to have the info to go faster */
763 buf1 = buf;
764 while (n > 0) {
thsf58c7b32008-06-05 21:53:49 +0000765 /* If the output image is being created as a copy on write image,
766 copy all sectors even the ones containing only NUL bytes,
aliguori93c65b42009-04-05 17:40:43 +0000767 because they may differ from the sectors in the base image.
768
769 If the output is to a host device, we also write out
770 sectors that are entirely 0, since whatever data was
771 already there is garbage, not 0s. */
Anthony Liguori5efa9d52009-05-09 17:03:42 -0500772 if (strcmp(drv->format_name, "host_device") == 0 || out_baseimg ||
aliguori93c65b42009-04-05 17:40:43 +0000773 is_allocated_sectors(buf1, n, &n1)) {
ths5fafdf22007-09-16 21:08:06 +0000774 if (bdrv_write(out_bs, sector_num, buf1, n1) < 0)
bellardea2384d2004-08-01 21:59:26 +0000775 error("error while writing");
776 }
777 sector_num += n1;
778 n -= n1;
779 buf1 += n1 * 512;
780 }
781 }
782 }
783 bdrv_delete(out_bs);
balrog926c2d22007-10-31 01:11:44 +0000784 for (bs_i = 0; bs_i < bs_n; bs_i++)
785 bdrv_delete(bs[bs_i]);
786 free(bs);
bellardea2384d2004-08-01 21:59:26 +0000787 return 0;
788}
789
bellard57d1a2b2004-08-03 21:15:11 +0000790#ifdef _WIN32
791static int64_t get_allocated_file_size(const char *filename)
792{
bellarde8445332006-06-14 15:32:10 +0000793 typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high);
794 get_compressed_t get_compressed;
bellard57d1a2b2004-08-03 21:15:11 +0000795 struct _stati64 st;
bellarde8445332006-06-14 15:32:10 +0000796
797 /* WinNT support GetCompressedFileSize to determine allocate size */
798 get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA");
799 if (get_compressed) {
800 DWORD high, low;
801 low = get_compressed(filename, &high);
802 if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR)
803 return (((int64_t) high) << 32) + low;
804 }
805
ths5fafdf22007-09-16 21:08:06 +0000806 if (_stati64(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000807 return -1;
808 return st.st_size;
809}
810#else
811static int64_t get_allocated_file_size(const char *filename)
812{
813 struct stat st;
ths5fafdf22007-09-16 21:08:06 +0000814 if (stat(filename, &st) < 0)
bellard57d1a2b2004-08-03 21:15:11 +0000815 return -1;
816 return (int64_t)st.st_blocks * 512;
817}
818#endif
819
bellardfaea38e2006-08-05 21:31:00 +0000820static void dump_snapshots(BlockDriverState *bs)
821{
822 QEMUSnapshotInfo *sn_tab, *sn;
823 int nb_sns, i;
824 char buf[256];
825
826 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
827 if (nb_sns <= 0)
828 return;
829 printf("Snapshot list:\n");
830 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
831 for(i = 0; i < nb_sns; i++) {
832 sn = &sn_tab[i];
833 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
834 }
835 qemu_free(sn_tab);
836}
837
bellardea2384d2004-08-01 21:59:26 +0000838static int img_info(int argc, char **argv)
839{
840 int c;
841 const char *filename, *fmt;
842 BlockDriver *drv;
843 BlockDriverState *bs;
844 char fmt_name[128], size_buf[128], dsize_buf[128];
ths96b8f132007-12-17 01:35:20 +0000845 uint64_t total_sectors;
846 int64_t allocated_size;
bellard93b6b2a2006-08-01 15:51:11 +0000847 char backing_filename[1024];
848 char backing_filename2[1024];
bellardfaea38e2006-08-05 21:31:00 +0000849 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +0000850
851 fmt = NULL;
852 for(;;) {
853 c = getopt(argc, argv, "f:h");
854 if (c == -1)
855 break;
856 switch(c) {
857 case 'h':
858 help();
859 break;
860 case 'f':
861 fmt = optarg;
862 break;
863 }
864 }
ths5fafdf22007-09-16 21:08:06 +0000865 if (optind >= argc)
bellardea2384d2004-08-01 21:59:26 +0000866 help();
867 filename = argv[optind++];
868
869 bs = bdrv_new("");
870 if (!bs)
871 error("Not enough memory");
872 if (fmt) {
873 drv = bdrv_find_format(fmt);
874 if (!drv)
875 error("Unknown file format '%s'", fmt);
876 } else {
877 drv = NULL;
878 }
aurel32137519c2008-11-30 19:12:49 +0000879 if (bdrv_open2(bs, filename, BRDV_O_FLAGS, drv) < 0) {
bellardea2384d2004-08-01 21:59:26 +0000880 error("Could not open '%s'", filename);
881 }
882 bdrv_get_format(bs, fmt_name, sizeof(fmt_name));
883 bdrv_get_geometry(bs, &total_sectors);
884 get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512);
bellard57d1a2b2004-08-03 21:15:11 +0000885 allocated_size = get_allocated_file_size(filename);
886 if (allocated_size < 0)
blueswir1a10ea302008-08-24 10:30:33 +0000887 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
bellardde167e42005-04-28 21:15:08 +0000888 else
ths5fafdf22007-09-16 21:08:06 +0000889 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
bellardde167e42005-04-28 21:15:08 +0000890 allocated_size);
bellardea2384d2004-08-01 21:59:26 +0000891 printf("image: %s\n"
892 "file format: %s\n"
bellardec3757d2006-06-14 15:50:07 +0000893 "virtual size: %s (%" PRId64 " bytes)\n"
bellardea2384d2004-08-01 21:59:26 +0000894 "disk size: %s\n",
ths5fafdf22007-09-16 21:08:06 +0000895 filename, fmt_name, size_buf,
bellardec3757d2006-06-14 15:50:07 +0000896 (total_sectors * 512),
bellardea2384d2004-08-01 21:59:26 +0000897 dsize_buf);
898 if (bdrv_is_encrypted(bs))
899 printf("encrypted: yes\n");
bellardfaea38e2006-08-05 21:31:00 +0000900 if (bdrv_get_info(bs, &bdi) >= 0) {
ths5fafdf22007-09-16 21:08:06 +0000901 if (bdi.cluster_size != 0)
bellardfaea38e2006-08-05 21:31:00 +0000902 printf("cluster_size: %d\n", bdi.cluster_size);
903 }
bellard93b6b2a2006-08-01 15:51:11 +0000904 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
bellardfaea38e2006-08-05 21:31:00 +0000905 if (backing_filename[0] != '\0') {
bellard93b6b2a2006-08-01 15:51:11 +0000906 path_combine(backing_filename2, sizeof(backing_filename2),
907 filename, backing_filename);
ths5fafdf22007-09-16 21:08:06 +0000908 printf("backing file: %s (actual path: %s)\n",
bellard93b6b2a2006-08-01 15:51:11 +0000909 backing_filename,
910 backing_filename2);
bellardfaea38e2006-08-05 21:31:00 +0000911 }
912 dump_snapshots(bs);
bellardea2384d2004-08-01 21:59:26 +0000913 bdrv_delete(bs);
914 return 0;
915}
916
aliguorif7b4a942009-01-07 17:40:15 +0000917#define SNAPSHOT_LIST 1
918#define SNAPSHOT_CREATE 2
919#define SNAPSHOT_APPLY 3
920#define SNAPSHOT_DELETE 4
921
922static void img_snapshot(int argc, char **argv)
923{
924 BlockDriverState *bs;
925 QEMUSnapshotInfo sn;
926 char *filename, *snapshot_name = NULL;
aliguori40a45392009-01-15 21:42:12 +0000927 int c, ret;
aliguorif7b4a942009-01-07 17:40:15 +0000928 int action = 0;
929 qemu_timeval tv;
930
931 /* Parse commandline parameters */
932 for(;;) {
933 c = getopt(argc, argv, "la:c:d:h");
934 if (c == -1)
935 break;
936 switch(c) {
937 case 'h':
938 help();
939 return;
940 case 'l':
941 if (action) {
942 help();
943 return;
944 }
945 action = SNAPSHOT_LIST;
946 break;
947 case 'a':
948 if (action) {
949 help();
950 return;
951 }
952 action = SNAPSHOT_APPLY;
953 snapshot_name = optarg;
954 break;
955 case 'c':
956 if (action) {
957 help();
958 return;
959 }
960 action = SNAPSHOT_CREATE;
961 snapshot_name = optarg;
962 break;
963 case 'd':
964 if (action) {
965 help();
966 return;
967 }
968 action = SNAPSHOT_DELETE;
969 snapshot_name = optarg;
970 break;
971 }
972 }
973
974 if (optind >= argc)
975 help();
976 filename = argv[optind++];
977
978 /* Open the image */
979 bs = bdrv_new("");
980 if (!bs)
981 error("Not enough memory");
982
983 if (bdrv_open2(bs, filename, 0, NULL) < 0) {
984 error("Could not open '%s'", filename);
985 }
986
987 /* Perform the requested action */
988 switch(action) {
989 case SNAPSHOT_LIST:
990 dump_snapshots(bs);
991 break;
992
993 case SNAPSHOT_CREATE:
994 memset(&sn, 0, sizeof(sn));
995 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
996
997 qemu_gettimeofday(&tv);
998 sn.date_sec = tv.tv_sec;
999 sn.date_nsec = tv.tv_usec * 1000;
1000
1001 ret = bdrv_snapshot_create(bs, &sn);
1002 if (ret)
1003 error("Could not create snapshot '%s': %d (%s)",
1004 snapshot_name, ret, strerror(-ret));
1005 break;
1006
1007 case SNAPSHOT_APPLY:
1008 ret = bdrv_snapshot_goto(bs, snapshot_name);
1009 if (ret)
1010 error("Could not apply snapshot '%s': %d (%s)",
1011 snapshot_name, ret, strerror(-ret));
1012 break;
1013
1014 case SNAPSHOT_DELETE:
1015 ret = bdrv_snapshot_delete(bs, snapshot_name);
1016 if (ret)
1017 error("Could not delete snapshot '%s': %d (%s)",
1018 snapshot_name, ret, strerror(-ret));
1019 break;
1020 }
1021
1022 /* Cleanup */
1023 bdrv_delete(bs);
1024}
1025
bellardea2384d2004-08-01 21:59:26 +00001026int main(int argc, char **argv)
1027{
1028 const char *cmd;
1029
1030 bdrv_init();
1031 if (argc < 2)
1032 help();
1033 cmd = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00001034 argc--; argv++;
bellardea2384d2004-08-01 21:59:26 +00001035 if (!strcmp(cmd, "create")) {
1036 img_create(argc, argv);
aliguori15859692009-04-21 23:11:53 +00001037 } else if (!strcmp(cmd, "check")) {
1038 img_check(argc, argv);
bellardea2384d2004-08-01 21:59:26 +00001039 } else if (!strcmp(cmd, "commit")) {
1040 img_commit(argc, argv);
1041 } else if (!strcmp(cmd, "convert")) {
1042 img_convert(argc, argv);
1043 } else if (!strcmp(cmd, "info")) {
1044 img_info(argc, argv);
aliguorif7b4a942009-01-07 17:40:15 +00001045 } else if (!strcmp(cmd, "snapshot")) {
1046 img_snapshot(argc, argv);
bellardea2384d2004-08-01 21:59:26 +00001047 } else {
1048 help();
1049 }
1050 return 0;
1051}