blob: 0e34bf0c90bc5ddb1707d300296c04e99fd98340 [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 */
Benoît Canetc054b3f2012-09-05 13:09:02 +020024#include "qapi-visit.h"
25#include "qapi/qmp-output-visitor.h"
Paolo Bonzini7b1b5d12012-12-17 18:19:43 +010026#include "qapi/qmp/qjson.h"
pbrookfaf07962007-11-11 02:51:17 +000027#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010028#include "qemu/option.h"
29#include "qemu/error-report.h"
30#include "qemu/osdep.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010032#include "block/block_int.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020033#include <getopt.h>
aliguori9230eaf2009-03-28 17:55:19 +000034#include <stdio.h>
bellardea2384d2004-08-01 21:59:26 +000035
bellarde8445332006-06-14 15:32:10 +000036#ifdef _WIN32
37#include <windows.h>
38#endif
39
Anthony Liguoric227f092009-10-01 16:12:16 -050040typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010041 const char *name;
42 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050043} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010044
Federico Simoncelli8599ea42013-01-28 06:59:47 -050045enum {
46 OPTION_OUTPUT = 256,
47 OPTION_BACKING_CHAIN = 257,
48};
49
50typedef enum OutputFormat {
51 OFORMAT_JSON,
52 OFORMAT_HUMAN,
53} OutputFormat;
54
aurel32137519c2008-11-30 19:12:49 +000055/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010056#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040057#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000058
bellardea2384d2004-08-01 21:59:26 +000059static void format_print(void *opaque, const char *name)
60{
61 printf(" %s", name);
62}
63
blueswir1d2c639d2009-01-24 18:19:25 +000064/* Please keep in synch with qemu-img.texi */
pbrook3f379ab2007-11-11 03:33:13 +000065static void help(void)
bellardea2384d2004-08-01 21:59:26 +000066{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010067 const char *help_msg =
68 "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
malc3f020d72010-02-08 12:04:56 +030069 "usage: qemu-img command [command options]\n"
70 "QEMU disk image utility\n"
71 "\n"
72 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010073#define DEF(option, callback, arg_string) \
74 " " arg_string "\n"
75#include "qemu-img-cmds.h"
76#undef DEF
77#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030078 "\n"
79 "Command parameters:\n"
80 " 'filename' is a disk image filename\n"
81 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -040082 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +080083 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
84 " 'directsync' and 'unsafe' (default for convert)\n"
malc3f020d72010-02-08 12:04:56 +030085 " 'size' is the disk image size in bytes. Optional suffixes\n"
86 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
87 " and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
88 " 'output_filename' is the destination disk image filename\n"
89 " 'output_fmt' is the destination format\n"
90 " 'options' is a comma separated list of format specific options in a\n"
91 " name=value format. Use -o ? for an overview of the options supported by the\n"
92 " used format\n"
93 " '-c' indicates that target image must be compressed (qcow format only)\n"
94 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
95 " match exactly. The image doesn't need a working backing file before\n"
96 " rebasing in this case (useful for renaming the backing file)\n"
97 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +020098 " '-p' show progress of command (only certain commands)\n"
Kevin Wolfa22f1232011-08-26 15:27:13 +020099 " '-S' indicates the consecutive number of bytes that must contain only zeros\n"
100 " for qemu-img to create a sparse image during conversion\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200101 " '--output' takes the format in which the output must be done (human or json)\n"
malc3f020d72010-02-08 12:04:56 +0300102 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200103 "Parameters to check subcommand:\n"
104 " '-r' tries to repair any inconsistencies that are found during the check.\n"
105 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
106 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200107 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200108 "\n"
malc3f020d72010-02-08 12:04:56 +0300109 "Parameters to snapshot subcommand:\n"
110 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
111 " '-a' applies a snapshot (revert disk to saved state)\n"
112 " '-c' creates a snapshot\n"
113 " '-d' deletes a snapshot\n"
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100114 " '-l' lists all snapshots in the given image\n";
115
116 printf("%s\nSupported formats:", help_msg);
bellardea2384d2004-08-01 21:59:26 +0000117 bdrv_iterate_format(format_print, NULL);
118 printf("\n");
119 exit(1);
120}
121
bellardea2384d2004-08-01 21:59:26 +0000122#if defined(WIN32)
123/* XXX: put correct support for win32 */
124static int read_password(char *buf, int buf_size)
125{
126 int c, i;
127 printf("Password: ");
128 fflush(stdout);
129 i = 0;
130 for(;;) {
131 c = getchar();
132 if (c == '\n')
133 break;
134 if (i < (buf_size - 1))
135 buf[i++] = c;
136 }
137 buf[i] = '\0';
138 return 0;
139}
140
141#else
142
143#include <termios.h>
144
145static struct termios oldtty;
146
147static void term_exit(void)
148{
149 tcsetattr (0, TCSANOW, &oldtty);
150}
151
152static void term_init(void)
153{
154 struct termios tty;
155
156 tcgetattr (0, &tty);
157 oldtty = tty;
158
159 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
160 |INLCR|IGNCR|ICRNL|IXON);
161 tty.c_oflag |= OPOST;
162 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
163 tty.c_cflag &= ~(CSIZE|PARENB);
164 tty.c_cflag |= CS8;
165 tty.c_cc[VMIN] = 1;
166 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000167
bellardea2384d2004-08-01 21:59:26 +0000168 tcsetattr (0, TCSANOW, &tty);
169
170 atexit(term_exit);
171}
172
pbrook3f379ab2007-11-11 03:33:13 +0000173static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000174{
175 uint8_t ch;
176 int i, ret;
177
178 printf("password: ");
179 fflush(stdout);
180 term_init();
181 i = 0;
182 for(;;) {
183 ret = read(0, &ch, 1);
184 if (ret == -1) {
185 if (errno == EAGAIN || errno == EINTR) {
186 continue;
187 } else {
188 ret = -1;
189 break;
190 }
191 } else if (ret == 0) {
192 ret = -1;
193 break;
194 } else {
195 if (ch == '\r') {
196 ret = 0;
197 break;
198 }
199 if (i < (buf_size - 1))
200 buf[i++] = ch;
201 }
202 }
203 term_exit();
204 buf[i] = '\0';
205 printf("\n");
206 return ret;
207}
208#endif
209
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100210static int print_block_option_help(const char *filename, const char *fmt)
211{
212 BlockDriver *drv, *proto_drv;
213 QEMUOptionParameter *create_options = NULL;
214
215 /* Find driver and parse its options */
216 drv = bdrv_find_format(fmt);
217 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100218 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100219 return 1;
220 }
221
222 proto_drv = bdrv_find_protocol(filename);
223 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100224 error_report("Unknown protocol '%s'", filename);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100225 return 1;
226 }
227
228 create_options = append_option_parameters(create_options,
229 drv->create_options);
230 create_options = append_option_parameters(create_options,
231 proto_drv->create_options);
232 print_option_help(create_options);
233 free_option_parameters(create_options);
234 return 0;
235}
236
bellard75c23802004-08-27 21:28:58 +0000237static BlockDriverState *bdrv_new_open(const char *filename,
Sheng Yang9bc378c2010-01-29 10:15:06 +0800238 const char *fmt,
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100239 int flags,
240 bool require_io)
bellard75c23802004-08-27 21:28:58 +0000241{
242 BlockDriverState *bs;
243 BlockDriver *drv;
244 char password[256];
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100245 int ret;
bellard75c23802004-08-27 21:28:58 +0000246
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100247 bs = bdrv_new("image");
Kevin Wolfad717132010-12-16 15:37:41 +0100248
bellard75c23802004-08-27 21:28:58 +0000249 if (fmt) {
250 drv = bdrv_find_format(fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900251 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100252 error_report("Unknown file format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900253 goto fail;
254 }
bellard75c23802004-08-27 21:28:58 +0000255 } else {
256 drv = NULL;
257 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100258
259 ret = bdrv_open(bs, filename, flags, drv);
260 if (ret < 0) {
261 error_report("Could not open '%s': %s", filename, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900262 goto fail;
bellard75c23802004-08-27 21:28:58 +0000263 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100264
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100265 if (bdrv_is_encrypted(bs) && require_io) {
bellard75c23802004-08-27 21:28:58 +0000266 printf("Disk image '%s' is encrypted.\n", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900267 if (read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100268 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900269 goto fail;
270 }
271 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100272 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900273 goto fail;
274 }
bellard75c23802004-08-27 21:28:58 +0000275 }
276 return bs;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900277fail:
278 if (bs) {
279 bdrv_delete(bs);
280 }
281 return NULL;
bellard75c23802004-08-27 21:28:58 +0000282}
283
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900284static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100285 const char *base_filename,
286 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200287{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200288 if (base_filename) {
289 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100290 error_report("Backing file not supported for file format '%s'",
291 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900292 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200293 }
294 }
295 if (base_fmt) {
296 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100297 error_report("Backing file format not supported for file "
298 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900299 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200300 }
301 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900302 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200303}
304
bellardea2384d2004-08-01 21:59:26 +0000305static int img_create(int argc, char **argv)
306{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200307 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100308 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000309 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000310 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000311 const char *filename;
312 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200313 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200314 Error *local_err = NULL;
ths3b46e622007-09-17 08:09:54 +0000315
bellardea2384d2004-08-01 21:59:26 +0000316 for(;;) {
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200317 c = getopt(argc, argv, "F:b:f:he6o:");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100318 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000319 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100320 }
bellardea2384d2004-08-01 21:59:26 +0000321 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100322 case '?':
bellardea2384d2004-08-01 21:59:26 +0000323 case 'h':
324 help();
325 break;
aliguori9230eaf2009-03-28 17:55:19 +0000326 case 'F':
327 base_fmt = optarg;
328 break;
bellardea2384d2004-08-01 21:59:26 +0000329 case 'b':
330 base_filename = optarg;
331 break;
332 case 'f':
333 fmt = optarg;
334 break;
335 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200336 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100337 "encryption\' instead!");
338 return 1;
thsd8871c52007-10-24 16:11:42 +0000339 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200340 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100341 "compat6\' instead!");
342 return 1;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200343 case 'o':
344 options = optarg;
345 break;
bellardea2384d2004-08-01 21:59:26 +0000346 }
347 }
aliguori9230eaf2009-03-28 17:55:19 +0000348
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900349 /* Get the filename */
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100350 if (optind >= argc) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900351 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100352 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900353 filename = argv[optind++];
354
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100355 /* Get image size, if specified */
356 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100357 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100358 char *end;
359 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
360 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800361 if (sval == -ERANGE) {
362 error_report("Image size must be less than 8 EiB!");
363 } else {
364 error_report("Invalid image size specified! You may use k, M, "
365 "G or T suffixes for ");
366 error_report("kilobytes, megabytes, gigabytes and terabytes.");
367 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200368 return 1;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100369 }
370 img_size = (uint64_t)sval;
371 }
372
Peter Maydellc8057f92012-08-02 13:45:54 +0100373 if (options && is_help_option(options)) {
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200374 return print_block_option_help(filename, fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100375 }
376
Luiz Capitulino9b375252012-11-30 10:52:05 -0200377 bdrv_img_create(filename, fmt, base_filename, base_fmt,
378 options, img_size, BDRV_O_FLAGS, &local_err);
379 if (error_is_set(&local_err)) {
380 error_report("%s", error_get_pretty(local_err));
381 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900382 return 1;
383 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200384
bellardea2384d2004-08-01 21:59:26 +0000385 return 0;
386}
387
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500388static void dump_json_image_check(ImageCheck *check)
389{
390 Error *errp = NULL;
391 QString *str;
392 QmpOutputVisitor *ov = qmp_output_visitor_new();
393 QObject *obj;
394 visit_type_ImageCheck(qmp_output_get_visitor(ov),
395 &check, NULL, &errp);
396 obj = qmp_output_get_qobject(ov);
397 str = qobject_to_json_pretty(obj);
398 assert(str != NULL);
399 printf("%s\n", qstring_get_str(str));
400 qobject_decref(obj);
401 qmp_output_visitor_cleanup(ov);
402 QDECREF(str);
403}
404
405static void dump_human_image_check(ImageCheck *check)
406{
407 if (!(check->corruptions || check->leaks || check->check_errors)) {
408 printf("No errors were found on the image.\n");
409 } else {
410 if (check->corruptions) {
411 printf("\n%" PRId64 " errors were found on the image.\n"
412 "Data may be corrupted, or further writes to the image "
413 "may corrupt it.\n",
414 check->corruptions);
415 }
416
417 if (check->leaks) {
418 printf("\n%" PRId64 " leaked clusters were found on the image.\n"
419 "This means waste of disk space, but no harm to data.\n",
420 check->leaks);
421 }
422
423 if (check->check_errors) {
424 printf("\n%" PRId64 " internal errors have occurred during the check.\n",
425 check->check_errors);
426 }
427 }
428
429 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Stefan Hajnoczic9fc5082013-02-07 17:15:03 +0100430 printf("%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
431 "%0.2f%% fragmented\n",
432 check->allocated_clusters, check->total_clusters,
433 check->allocated_clusters * 100.0 / check->total_clusters,
434 check->fragmented_clusters * 100.0 / check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500435 }
436
437 if (check->image_end_offset) {
438 printf("Image end offset: %" PRId64 "\n", check->image_end_offset);
439 }
440}
441
442static int collect_image_check(BlockDriverState *bs,
443 ImageCheck *check,
444 const char *filename,
445 const char *fmt,
446 int fix)
447{
448 int ret;
449 BdrvCheckResult result;
450
451 ret = bdrv_check(bs, &result, fix);
452 if (ret < 0) {
453 return ret;
454 }
455
456 check->filename = g_strdup(filename);
457 check->format = g_strdup(bdrv_get_format_name(bs));
458 check->check_errors = result.check_errors;
459 check->corruptions = result.corruptions;
460 check->has_corruptions = result.corruptions != 0;
461 check->leaks = result.leaks;
462 check->has_leaks = result.leaks != 0;
463 check->corruptions_fixed = result.corruptions_fixed;
464 check->has_corruptions_fixed = result.corruptions != 0;
465 check->leaks_fixed = result.leaks_fixed;
466 check->has_leaks_fixed = result.leaks != 0;
467 check->image_end_offset = result.image_end_offset;
468 check->has_image_end_offset = result.image_end_offset != 0;
469 check->total_clusters = result.bfi.total_clusters;
470 check->has_total_clusters = result.bfi.total_clusters != 0;
471 check->allocated_clusters = result.bfi.allocated_clusters;
472 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
473 check->fragmented_clusters = result.bfi.fragmented_clusters;
474 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
475
476 return 0;
477}
478
Kevin Wolfe076f332010-06-29 11:43:13 +0200479/*
480 * Checks an image for consistency. Exit codes:
481 *
482 * 0 - Check completed, image is good
483 * 1 - Check not completed because of internal errors
484 * 2 - Check completed, image is corrupted
485 * 3 - Check completed, image has leaked clusters, but is good otherwise
486 */
aliguori15859692009-04-21 23:11:53 +0000487static int img_check(int argc, char **argv)
488{
489 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500490 OutputFormat output_format = OFORMAT_HUMAN;
491 const char *filename, *fmt, *output;
aliguori15859692009-04-21 23:11:53 +0000492 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200493 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100494 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500495 ImageCheck *check;
aliguori15859692009-04-21 23:11:53 +0000496
497 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500498 output = NULL;
aliguori15859692009-04-21 23:11:53 +0000499 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500500 int option_index = 0;
501 static const struct option long_options[] = {
502 {"help", no_argument, 0, 'h'},
503 {"format", required_argument, 0, 'f'},
504 {"repair", no_argument, 0, 'r'},
505 {"output", required_argument, 0, OPTION_OUTPUT},
506 {0, 0, 0, 0}
507 };
508 c = getopt_long(argc, argv, "f:hr:",
509 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100510 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000511 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100512 }
aliguori15859692009-04-21 23:11:53 +0000513 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100514 case '?':
aliguori15859692009-04-21 23:11:53 +0000515 case 'h':
516 help();
517 break;
518 case 'f':
519 fmt = optarg;
520 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200521 case 'r':
522 flags |= BDRV_O_RDWR;
523
524 if (!strcmp(optarg, "leaks")) {
525 fix = BDRV_FIX_LEAKS;
526 } else if (!strcmp(optarg, "all")) {
527 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
528 } else {
529 help();
530 }
531 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500532 case OPTION_OUTPUT:
533 output = optarg;
534 break;
aliguori15859692009-04-21 23:11:53 +0000535 }
536 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100537 if (optind >= argc) {
aliguori15859692009-04-21 23:11:53 +0000538 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100539 }
aliguori15859692009-04-21 23:11:53 +0000540 filename = argv[optind++];
541
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500542 if (output && !strcmp(output, "json")) {
543 output_format = OFORMAT_JSON;
544 } else if (output && !strcmp(output, "human")) {
545 output_format = OFORMAT_HUMAN;
546 } else if (output) {
547 error_report("--output must be used with human or json as argument.");
548 return 1;
549 }
550
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100551 bs = bdrv_new_open(filename, fmt, flags, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900552 if (!bs) {
553 return 1;
554 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500555
556 check = g_new0(ImageCheck, 1);
557 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200558
559 if (ret == -ENOTSUP) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500560 if (output_format == OFORMAT_HUMAN) {
561 error_report("This image format does not support checks");
562 }
563 ret = 1;
564 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200565 }
566
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500567 if (check->corruptions_fixed || check->leaks_fixed) {
568 int corruptions_fixed, leaks_fixed;
569
570 leaks_fixed = check->leaks_fixed;
571 corruptions_fixed = check->corruptions_fixed;
572
573 if (output_format == OFORMAT_HUMAN) {
574 printf("The following inconsistencies were found and repaired:\n\n"
575 " %" PRId64 " leaked clusters\n"
576 " %" PRId64 " corruptions\n\n"
577 "Double checking the fixed image now...\n",
578 check->leaks_fixed,
579 check->corruptions_fixed);
580 }
581
582 ret = collect_image_check(bs, check, filename, fmt, 0);
583
584 check->leaks_fixed = leaks_fixed;
585 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200586 }
587
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500588 switch (output_format) {
589 case OFORMAT_HUMAN:
590 dump_human_image_check(check);
591 break;
592 case OFORMAT_JSON:
593 dump_json_image_check(check);
594 break;
595 }
596
597 if (ret || check->check_errors) {
598 ret = 1;
599 goto fail;
600 }
601
602 if (check->corruptions) {
603 ret = 2;
604 } else if (check->leaks) {
605 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200606 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500607 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000608 }
609
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500610fail:
611 qapi_free_ImageCheck(check);
aliguori15859692009-04-21 23:11:53 +0000612 bdrv_delete(bs);
Kevin Wolfe076f332010-06-29 11:43:13 +0200613
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500614 return ret;
aliguori15859692009-04-21 23:11:53 +0000615}
616
bellardea2384d2004-08-01 21:59:26 +0000617static int img_commit(int argc, char **argv)
618{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400619 int c, ret, flags;
620 const char *filename, *fmt, *cache;
bellardea2384d2004-08-01 21:59:26 +0000621 BlockDriverState *bs;
622
623 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400624 cache = BDRV_DEFAULT_CACHE;
bellardea2384d2004-08-01 21:59:26 +0000625 for(;;) {
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400626 c = getopt(argc, argv, "f:ht:");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100627 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000628 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100629 }
bellardea2384d2004-08-01 21:59:26 +0000630 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100631 case '?':
bellardea2384d2004-08-01 21:59:26 +0000632 case 'h':
633 help();
634 break;
635 case 'f':
636 fmt = optarg;
637 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400638 case 't':
639 cache = optarg;
640 break;
bellardea2384d2004-08-01 21:59:26 +0000641 }
642 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100643 if (optind >= argc) {
bellardea2384d2004-08-01 21:59:26 +0000644 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100645 }
bellardea2384d2004-08-01 21:59:26 +0000646 filename = argv[optind++];
647
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400648 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100649 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400650 if (ret < 0) {
651 error_report("Invalid cache option: %s", cache);
652 return -1;
653 }
654
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100655 bs = bdrv_new_open(filename, fmt, flags, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900656 if (!bs) {
657 return 1;
658 }
bellardea2384d2004-08-01 21:59:26 +0000659 ret = bdrv_commit(bs);
660 switch(ret) {
661 case 0:
662 printf("Image committed.\n");
663 break;
664 case -ENOENT:
Jes Sorensen15654a62010-12-16 14:31:53 +0100665 error_report("No disk inserted");
bellardea2384d2004-08-01 21:59:26 +0000666 break;
667 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +0100668 error_report("Image is read-only");
bellardea2384d2004-08-01 21:59:26 +0000669 break;
670 case -ENOTSUP:
Jes Sorensen15654a62010-12-16 14:31:53 +0100671 error_report("Image is already committed");
bellardea2384d2004-08-01 21:59:26 +0000672 break;
673 default:
Jes Sorensen15654a62010-12-16 14:31:53 +0100674 error_report("Error while committing image");
bellardea2384d2004-08-01 21:59:26 +0000675 break;
676 }
677
678 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900679 if (ret) {
680 return 1;
681 }
bellardea2384d2004-08-01 21:59:26 +0000682 return 0;
683}
684
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400685/*
thsf58c7b32008-06-05 21:53:49 +0000686 * Returns true iff the first sector pointed to by 'buf' contains at least
687 * a non-NUL byte.
688 *
689 * 'pnum' is set to the number of sectors (including and immediately following
690 * the first one) that are known to be in the same allocated/unallocated state.
691 */
bellardea2384d2004-08-01 21:59:26 +0000692static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
693{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000694 bool is_zero;
695 int i;
bellardea2384d2004-08-01 21:59:26 +0000696
697 if (n <= 0) {
698 *pnum = 0;
699 return 0;
700 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000701 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000702 for(i = 1; i < n; i++) {
703 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000704 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000705 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000706 }
bellardea2384d2004-08-01 21:59:26 +0000707 }
708 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000709 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000710}
711
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100712/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200713 * Like is_allocated_sectors, but if the buffer starts with a used sector,
714 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
715 * breaking up write requests for only small sparse areas.
716 */
717static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
718 int min)
719{
720 int ret;
721 int num_checked, num_used;
722
723 if (n < min) {
724 min = n;
725 }
726
727 ret = is_allocated_sectors(buf, n, pnum);
728 if (!ret) {
729 return ret;
730 }
731
732 num_used = *pnum;
733 buf += BDRV_SECTOR_SIZE * *pnum;
734 n -= *pnum;
735 num_checked = num_used;
736
737 while (n > 0) {
738 ret = is_allocated_sectors(buf, n, pnum);
739
740 buf += BDRV_SECTOR_SIZE * *pnum;
741 n -= *pnum;
742 num_checked += *pnum;
743 if (ret) {
744 num_used = num_checked;
745 } else if (*pnum >= min) {
746 break;
747 }
748 }
749
750 *pnum = num_used;
751 return 1;
752}
753
754/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100755 * Compares two buffers sector by sector. Returns 0 if the first sector of both
756 * buffers matches, non-zero otherwise.
757 *
758 * pnum is set to the number of sectors (including and immediately following
759 * the first one) that are known to have the same comparison result
760 */
761static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
762 int *pnum)
763{
764 int res, i;
765
766 if (n <= 0) {
767 *pnum = 0;
768 return 0;
769 }
770
771 res = !!memcmp(buf1, buf2, 512);
772 for(i = 1; i < n; i++) {
773 buf1 += 512;
774 buf2 += 512;
775
776 if (!!memcmp(buf1, buf2, 512) != res) {
777 break;
778 }
779 }
780
781 *pnum = i;
782 return res;
783}
784
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200785#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000786
787static int img_convert(int argc, char **argv)
788{
Jes Sorenseneec77d92010-12-07 17:44:34 +0100789 int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400790 int progress = 0, flags;
791 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900792 BlockDriver *drv, *proto_drv;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900793 BlockDriverState **bs = NULL, *out_bs = NULL;
ths96b8f132007-12-17 01:35:20 +0000794 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
795 uint64_t bs_sectors;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900796 uint8_t * buf = NULL;
bellardea2384d2004-08-01 21:59:26 +0000797 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +0000798 BlockDriverInfo bdi;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900799 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfa18953f2010-10-14 15:46:04 +0200800 QEMUOptionParameter *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200801 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -0700802 const char *snapshot_name = NULL;
Kevin Wolf1f710492012-10-12 14:29:18 +0200803 float local_progress = 0;
Kevin Wolfa22f1232011-08-26 15:27:13 +0200804 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
bellardea2384d2004-08-01 21:59:26 +0000805
806 fmt = NULL;
807 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400808 cache = "unsafe";
thsf58c7b32008-06-05 21:53:49 +0000809 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +0100810 compress = 0;
bellardea2384d2004-08-01 21:59:26 +0000811 for(;;) {
Kevin Wolfa22f1232011-08-26 15:27:13 +0200812 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100813 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000814 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100815 }
bellardea2384d2004-08-01 21:59:26 +0000816 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100817 case '?':
bellardea2384d2004-08-01 21:59:26 +0000818 case 'h':
819 help();
820 break;
821 case 'f':
822 fmt = optarg;
823 break;
824 case 'O':
825 out_fmt = optarg;
826 break;
thsf58c7b32008-06-05 21:53:49 +0000827 case 'B':
828 out_baseimg = optarg;
829 break;
bellardea2384d2004-08-01 21:59:26 +0000830 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +0100831 compress = 1;
bellardea2384d2004-08-01 21:59:26 +0000832 break;
833 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200834 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100835 "encryption\' instead!");
836 return 1;
thsec36ba12007-09-16 21:59:02 +0000837 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200838 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100839 "compat6\' instead!");
840 return 1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200841 case 'o':
842 options = optarg;
843 break;
edison51ef6722010-09-21 19:58:41 -0700844 case 's':
845 snapshot_name = optarg;
846 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +0200847 case 'S':
848 {
849 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100850 char *end;
851 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
852 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +0200853 error_report("Invalid minimum zero buffer size for sparse output specified");
854 return 1;
855 }
856
857 min_sparse = sval / BDRV_SECTOR_SIZE;
858 break;
859 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200860 case 'p':
861 progress = 1;
862 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400863 case 't':
864 cache = optarg;
865 break;
bellardea2384d2004-08-01 21:59:26 +0000866 }
867 }
ths3b46e622007-09-17 08:09:54 +0000868
balrog926c2d22007-10-31 01:11:44 +0000869 bs_n = argc - optind - 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100870 if (bs_n < 1) {
871 help();
872 }
balrog926c2d22007-10-31 01:11:44 +0000873
874 out_filename = argv[argc - 1];
thsf58c7b32008-06-05 21:53:49 +0000875
Charles Arnoldfa170c12012-05-11 10:57:54 -0600876 /* Initialize before goto out */
877 qemu_progress_init(progress, 2.0);
878
Peter Maydellc8057f92012-08-02 13:45:54 +0100879 if (options && is_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100880 ret = print_block_option_help(out_filename, out_fmt);
881 goto out;
882 }
883
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900884 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100885 error_report("-B makes no sense when concatenating multiple input "
886 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +0100887 ret = -1;
888 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900889 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +0800890
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200891 qemu_progress_print(0, 100);
892
Anthony Liguori7267c092011-08-20 22:09:37 -0500893 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
balrog926c2d22007-10-31 01:11:44 +0000894
895 total_sectors = 0;
896 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100897 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900898 if (!bs[bs_i]) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100899 error_report("Could not open '%s'", argv[optind + bs_i]);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900900 ret = -1;
901 goto out;
902 }
balrog926c2d22007-10-31 01:11:44 +0000903 bdrv_get_geometry(bs[bs_i], &bs_sectors);
904 total_sectors += bs_sectors;
905 }
bellardea2384d2004-08-01 21:59:26 +0000906
edison51ef6722010-09-21 19:58:41 -0700907 if (snapshot_name != NULL) {
908 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200909 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -0700910 ret = -1;
911 goto out;
912 }
913 if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200914 error_report("Failed to load snapshot");
edison51ef6722010-09-21 19:58:41 -0700915 ret = -1;
916 goto out;
917 }
918 }
919
Kevin Wolfefa84d42009-05-18 16:42:12 +0200920 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000921 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900922 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100923 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900924 ret = -1;
925 goto out;
926 }
balrog926c2d22007-10-31 01:11:44 +0000927
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900928 proto_drv = bdrv_find_protocol(out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900929 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100930 error_report("Unknown protocol '%s'", out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900931 ret = -1;
932 goto out;
933 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900934
935 create_options = append_option_parameters(create_options,
936 drv->create_options);
937 create_options = append_option_parameters(create_options,
938 proto_drv->create_options);
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200939
Kevin Wolfefa84d42009-05-18 16:42:12 +0200940 if (options) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900941 param = parse_option_parameters(options, create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +0200942 if (param == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100943 error_report("Invalid options for file format '%s'.", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900944 ret = -1;
945 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200946 }
947 } else {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900948 param = parse_option_parameters("", create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +0200949 }
950
951 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
Jes Sorenseneec77d92010-12-07 17:44:34 +0100952 ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900953 if (ret < 0) {
954 goto out;
955 }
Kevin Wolfefa84d42009-05-18 16:42:12 +0200956
Kevin Wolfa18953f2010-10-14 15:46:04 +0200957 /* Get backing file name if -o backing_file was used */
958 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
959 if (out_baseimg_param) {
960 out_baseimg = out_baseimg_param->value.s;
961 }
962
Kevin Wolfefa84d42009-05-18 16:42:12 +0200963 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +0100964 if (compress) {
Kevin Wolfefa84d42009-05-18 16:42:12 +0200965 QEMUOptionParameter *encryption =
966 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
Kevin Wolf41521fa2011-10-18 16:19:42 +0200967 QEMUOptionParameter *preallocation =
968 get_option_parameter(param, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +0200969
970 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100971 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900972 ret = -1;
973 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200974 }
975
976 if (encryption && encryption->value.n) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100977 error_report("Compression and encryption not supported at "
978 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900979 ret = -1;
980 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200981 }
Kevin Wolf41521fa2011-10-18 16:19:42 +0200982
983 if (preallocation && preallocation->value.s
984 && strcmp(preallocation->value.s, "off"))
985 {
986 error_report("Compression and preallocation not supported at "
987 "the same time");
988 ret = -1;
989 goto out;
990 }
Kevin Wolfefa84d42009-05-18 16:42:12 +0200991 }
992
993 /* Create the new image */
994 ret = bdrv_create(drv, out_filename, param);
bellardea2384d2004-08-01 21:59:26 +0000995 if (ret < 0) {
996 if (ret == -ENOTSUP) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100997 error_report("Formatting not supported for file format '%s'",
998 out_fmt);
aurel326e9ea0c2009-04-15 14:42:46 +0000999 } else if (ret == -EFBIG) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001000 error_report("The image size is too large for file format '%s'",
1001 out_fmt);
bellardea2384d2004-08-01 21:59:26 +00001002 } else {
Jes Sorensen15654a62010-12-16 14:31:53 +01001003 error_report("%s: error while converting %s: %s",
1004 out_filename, out_fmt, strerror(-ret));
bellardea2384d2004-08-01 21:59:26 +00001005 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001006 goto out;
bellardea2384d2004-08-01 21:59:26 +00001007 }
ths3b46e622007-09-17 08:09:54 +00001008
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001009 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001010 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001011 if (ret < 0) {
1012 error_report("Invalid cache option: %s", cache);
1013 return -1;
1014 }
1015
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +01001016 out_bs = bdrv_new_open(out_filename, out_fmt, flags, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001017 if (!out_bs) {
1018 ret = -1;
1019 goto out;
1020 }
bellardea2384d2004-08-01 21:59:26 +00001021
balrog926c2d22007-10-31 01:11:44 +00001022 bs_i = 0;
1023 bs_offset = 0;
1024 bdrv_get_geometry(bs[0], &bs_sectors);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001025 buf = qemu_blockalign(out_bs, IO_BUF_SIZE);
balrog926c2d22007-10-31 01:11:44 +00001026
Jes Sorenseneec77d92010-12-07 17:44:34 +01001027 if (compress) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001028 ret = bdrv_get_info(out_bs, &bdi);
1029 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001030 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001031 goto out;
1032 }
bellardfaea38e2006-08-05 21:31:00 +00001033 cluster_size = bdi.cluster_size;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001034 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001035 error_report("invalid cluster size");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001036 ret = -1;
1037 goto out;
1038 }
bellardea2384d2004-08-01 21:59:26 +00001039 cluster_sectors = cluster_size >> 9;
1040 sector_num = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001041
1042 nb_sectors = total_sectors;
Kevin Wolf1f710492012-10-12 14:29:18 +02001043 if (nb_sectors != 0) {
1044 local_progress = (float)100 /
1045 (nb_sectors / MIN(nb_sectors, cluster_sectors));
1046 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001047
bellardea2384d2004-08-01 21:59:26 +00001048 for(;;) {
balrog926c2d22007-10-31 01:11:44 +00001049 int64_t bs_num;
1050 int remainder;
1051 uint8_t *buf2;
1052
bellardea2384d2004-08-01 21:59:26 +00001053 nb_sectors = total_sectors - sector_num;
1054 if (nb_sectors <= 0)
1055 break;
1056 if (nb_sectors >= cluster_sectors)
1057 n = cluster_sectors;
1058 else
1059 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +00001060
1061 bs_num = sector_num - bs_offset;
1062 assert (bs_num >= 0);
1063 remainder = n;
1064 buf2 = buf;
1065 while (remainder > 0) {
1066 int nlow;
1067 while (bs_num == bs_sectors) {
1068 bs_i++;
1069 assert (bs_i < bs_n);
1070 bs_offset += bs_sectors;
1071 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1072 bs_num = 0;
Blue Swirl0bfcd592010-05-22 08:02:12 +00001073 /* printf("changing part: sector_num=%" PRId64 ", "
1074 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1075 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
balrog926c2d22007-10-31 01:11:44 +00001076 }
1077 assert (bs_num < bs_sectors);
1078
1079 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1080
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001081 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1082 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001083 error_report("error while reading sector %" PRId64 ": %s",
1084 bs_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001085 goto out;
1086 }
balrog926c2d22007-10-31 01:11:44 +00001087
1088 buf2 += nlow * 512;
1089 bs_num += nlow;
1090
1091 remainder -= nlow;
1092 }
1093 assert (remainder == 0);
1094
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001095 if (n < cluster_sectors) {
bellardea2384d2004-08-01 21:59:26 +00001096 memset(buf + n * 512, 0, cluster_size - n * 512);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001097 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001098 if (!buffer_is_zero(buf, cluster_size)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001099 ret = bdrv_write_compressed(out_bs, sector_num, buf,
1100 cluster_sectors);
1101 if (ret != 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001102 error_report("error while compressing sector %" PRId64
1103 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001104 goto out;
1105 }
bellardea2384d2004-08-01 21:59:26 +00001106 }
1107 sector_num += n;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001108 qemu_progress_print(local_progress, 100);
bellardea2384d2004-08-01 21:59:26 +00001109 }
bellardfaea38e2006-08-05 21:31:00 +00001110 /* signal EOF to align */
1111 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +00001112 } else {
Kevin Wolff2feebb2010-04-14 17:30:35 +02001113 int has_zero_init = bdrv_has_zero_init(out_bs);
1114
thsf58c7b32008-06-05 21:53:49 +00001115 sector_num = 0; // total number of sectors converted so far
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001116 nb_sectors = total_sectors - sector_num;
Kevin Wolf1f710492012-10-12 14:29:18 +02001117 if (nb_sectors != 0) {
1118 local_progress = (float)100 /
1119 (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512));
1120 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001121
bellardea2384d2004-08-01 21:59:26 +00001122 for(;;) {
1123 nb_sectors = total_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001124 if (nb_sectors <= 0) {
bellardea2384d2004-08-01 21:59:26 +00001125 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001126 }
1127 if (nb_sectors >= (IO_BUF_SIZE / 512)) {
bellardea2384d2004-08-01 21:59:26 +00001128 n = (IO_BUF_SIZE / 512);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001129 } else {
bellardea2384d2004-08-01 21:59:26 +00001130 n = nb_sectors;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001131 }
balrog926c2d22007-10-31 01:11:44 +00001132
1133 while (sector_num - bs_offset >= bs_sectors) {
1134 bs_i ++;
1135 assert (bs_i < bs_n);
1136 bs_offset += bs_sectors;
1137 bdrv_get_geometry(bs[bs_i], &bs_sectors);
Blue Swirl0bfcd592010-05-22 08:02:12 +00001138 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1139 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
balrog926c2d22007-10-31 01:11:44 +00001140 sector_num, bs_i, bs_offset, bs_sectors); */
1141 }
1142
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001143 if (n > bs_offset + bs_sectors - sector_num) {
balrog926c2d22007-10-31 01:11:44 +00001144 n = bs_offset + bs_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001145 }
balrog926c2d22007-10-31 01:11:44 +00001146
Kevin Wolff2feebb2010-04-14 17:30:35 +02001147 if (has_zero_init) {
Akkarit Sangpetchd0320442009-07-17 10:02:15 +02001148 /* If the output image is being created as a copy on write image,
1149 assume that sectors which are unallocated in the input image
1150 are present in both the output's and input's base images (no
1151 need to copy them). */
1152 if (out_baseimg) {
1153 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
1154 n, &n1)) {
1155 sector_num += n1;
1156 continue;
1157 }
1158 /* The next 'n1' sectors are allocated in the input image. Copy
1159 only those as they may be followed by unallocated sectors. */
1160 n = n1;
aliguori93c65b42009-04-05 17:40:43 +00001161 }
aliguori93c65b42009-04-05 17:40:43 +00001162 } else {
1163 n1 = n;
thsf58c7b32008-06-05 21:53:49 +00001164 }
1165
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001166 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1167 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001168 error_report("error while reading sector %" PRId64 ": %s",
1169 sector_num - bs_offset, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001170 goto out;
1171 }
bellardea2384d2004-08-01 21:59:26 +00001172 /* NOTE: at the same time we convert, we do not write zero
1173 sectors to have a chance to compress the image. Ideally, we
1174 should add a specific call to have the info to go faster */
1175 buf1 = buf;
1176 while (n > 0) {
thsf58c7b32008-06-05 21:53:49 +00001177 /* If the output image is being created as a copy on write image,
1178 copy all sectors even the ones containing only NUL bytes,
aliguori93c65b42009-04-05 17:40:43 +00001179 because they may differ from the sectors in the base image.
1180
1181 If the output is to a host device, we also write out
1182 sectors that are entirely 0, since whatever data was
1183 already there is garbage, not 0s. */
Kevin Wolff2feebb2010-04-14 17:30:35 +02001184 if (!has_zero_init || out_baseimg ||
Kevin Wolfa22f1232011-08-26 15:27:13 +02001185 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001186 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1187 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001188 error_report("error while writing sector %" PRId64
1189 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001190 goto out;
1191 }
bellardea2384d2004-08-01 21:59:26 +00001192 }
1193 sector_num += n1;
1194 n -= n1;
1195 buf1 += n1 * 512;
1196 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001197 qemu_progress_print(local_progress, 100);
bellardea2384d2004-08-01 21:59:26 +00001198 }
1199 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001200out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001201 qemu_progress_end();
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001202 free_option_parameters(create_options);
1203 free_option_parameters(param);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001204 qemu_vfree(buf);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001205 if (out_bs) {
1206 bdrv_delete(out_bs);
1207 }
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001208 if (bs) {
1209 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1210 if (bs[bs_i]) {
1211 bdrv_delete(bs[bs_i]);
1212 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001213 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001214 g_free(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001215 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001216 if (ret) {
1217 return 1;
1218 }
bellardea2384d2004-08-01 21:59:26 +00001219 return 0;
1220}
1221
bellard57d1a2b2004-08-03 21:15:11 +00001222
bellardfaea38e2006-08-05 21:31:00 +00001223static void dump_snapshots(BlockDriverState *bs)
1224{
1225 QEMUSnapshotInfo *sn_tab, *sn;
1226 int nb_sns, i;
1227 char buf[256];
1228
1229 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1230 if (nb_sns <= 0)
1231 return;
1232 printf("Snapshot list:\n");
1233 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1234 for(i = 0; i < nb_sns; i++) {
1235 sn = &sn_tab[i];
1236 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1237 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001238 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00001239}
1240
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001241static void dump_json_image_info_list(ImageInfoList *list)
1242{
1243 Error *errp = NULL;
1244 QString *str;
1245 QmpOutputVisitor *ov = qmp_output_visitor_new();
1246 QObject *obj;
1247 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
1248 &list, NULL, &errp);
1249 obj = qmp_output_get_qobject(ov);
1250 str = qobject_to_json_pretty(obj);
1251 assert(str != NULL);
1252 printf("%s\n", qstring_get_str(str));
1253 qobject_decref(obj);
1254 qmp_output_visitor_cleanup(ov);
1255 QDECREF(str);
1256}
1257
Benoît Canetc054b3f2012-09-05 13:09:02 +02001258static void collect_snapshots(BlockDriverState *bs , ImageInfo *info)
bellardea2384d2004-08-01 21:59:26 +00001259{
Benoît Canetc054b3f2012-09-05 13:09:02 +02001260 int i, sn_count;
1261 QEMUSnapshotInfo *sn_tab = NULL;
1262 SnapshotInfoList *info_list, *cur_item = NULL;
1263 sn_count = bdrv_snapshot_list(bs, &sn_tab);
1264
1265 for (i = 0; i < sn_count; i++) {
1266 info->has_snapshots = true;
1267 info_list = g_new0(SnapshotInfoList, 1);
1268
1269 info_list->value = g_new0(SnapshotInfo, 1);
1270 info_list->value->id = g_strdup(sn_tab[i].id_str);
1271 info_list->value->name = g_strdup(sn_tab[i].name);
1272 info_list->value->vm_state_size = sn_tab[i].vm_state_size;
1273 info_list->value->date_sec = sn_tab[i].date_sec;
1274 info_list->value->date_nsec = sn_tab[i].date_nsec;
1275 info_list->value->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
1276 info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
1277
1278 /* XXX: waiting for the qapi to support qemu-queue.h types */
1279 if (!cur_item) {
1280 info->snapshots = cur_item = info_list;
1281 } else {
1282 cur_item->next = info_list;
1283 cur_item = info_list;
1284 }
1285
1286 }
1287
1288 g_free(sn_tab);
1289}
1290
1291static void dump_json_image_info(ImageInfo *info)
1292{
1293 Error *errp = NULL;
1294 QString *str;
1295 QmpOutputVisitor *ov = qmp_output_visitor_new();
1296 QObject *obj;
1297 visit_type_ImageInfo(qmp_output_get_visitor(ov),
1298 &info, NULL, &errp);
1299 obj = qmp_output_get_qobject(ov);
1300 str = qobject_to_json_pretty(obj);
1301 assert(str != NULL);
1302 printf("%s\n", qstring_get_str(str));
1303 qobject_decref(obj);
1304 qmp_output_visitor_cleanup(ov);
1305 QDECREF(str);
1306}
1307
1308static void collect_image_info(BlockDriverState *bs,
1309 ImageInfo *info,
1310 const char *filename,
1311 const char *fmt)
1312{
ths96b8f132007-12-17 01:35:20 +00001313 uint64_t total_sectors;
bellard93b6b2a2006-08-01 15:51:11 +00001314 char backing_filename[1024];
1315 char backing_filename2[1024];
bellardfaea38e2006-08-05 21:31:00 +00001316 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +00001317
Benoît Canetc054b3f2012-09-05 13:09:02 +02001318 bdrv_get_geometry(bs, &total_sectors);
1319
1320 info->filename = g_strdup(filename);
1321 info->format = g_strdup(bdrv_get_format_name(bs));
1322 info->virtual_size = total_sectors * 512;
1323 info->actual_size = bdrv_get_allocated_file_size(bs);
1324 info->has_actual_size = info->actual_size >= 0;
1325 if (bdrv_is_encrypted(bs)) {
1326 info->encrypted = true;
1327 info->has_encrypted = true;
1328 }
1329 if (bdrv_get_info(bs, &bdi) >= 0) {
1330 if (bdi.cluster_size != 0) {
1331 info->cluster_size = bdi.cluster_size;
1332 info->has_cluster_size = true;
1333 }
1334 info->dirty_flag = bdi.is_dirty;
1335 info->has_dirty_flag = true;
1336 }
1337 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
1338 if (backing_filename[0] != '\0') {
1339 info->backing_filename = g_strdup(backing_filename);
1340 info->has_backing_filename = true;
1341 bdrv_get_full_backing_filename(bs, backing_filename2,
1342 sizeof(backing_filename2));
1343
1344 if (strcmp(backing_filename, backing_filename2) != 0) {
1345 info->full_backing_filename =
1346 g_strdup(backing_filename2);
1347 info->has_full_backing_filename = true;
1348 }
1349
1350 if (bs->backing_format[0]) {
1351 info->backing_filename_format = g_strdup(bs->backing_format);
1352 info->has_backing_filename_format = true;
1353 }
1354 }
1355}
1356
1357static void dump_human_image_info(ImageInfo *info)
1358{
1359 char size_buf[128], dsize_buf[128];
1360 if (!info->has_actual_size) {
1361 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
1362 } else {
1363 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
1364 info->actual_size);
1365 }
1366 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
1367 printf("image: %s\n"
1368 "file format: %s\n"
1369 "virtual size: %s (%" PRId64 " bytes)\n"
1370 "disk size: %s\n",
1371 info->filename, info->format, size_buf,
1372 info->virtual_size,
1373 dsize_buf);
1374
1375 if (info->has_encrypted && info->encrypted) {
1376 printf("encrypted: yes\n");
1377 }
1378
1379 if (info->has_cluster_size) {
1380 printf("cluster_size: %" PRId64 "\n", info->cluster_size);
1381 }
1382
1383 if (info->has_dirty_flag && info->dirty_flag) {
1384 printf("cleanly shut down: no\n");
1385 }
1386
1387 if (info->has_backing_filename) {
1388 printf("backing file: %s", info->backing_filename);
1389 if (info->has_full_backing_filename) {
1390 printf(" (actual path: %s)", info->full_backing_filename);
1391 }
1392 putchar('\n');
1393 if (info->has_backing_filename_format) {
1394 printf("backing file format: %s\n", info->backing_filename_format);
1395 }
1396 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001397
1398 if (info->has_snapshots) {
1399 SnapshotInfoList *elem;
1400 char buf[256];
1401
1402 printf("Snapshot list:\n");
1403 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1404
1405 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
1406 * we convert to the block layer's native QEMUSnapshotInfo for now.
1407 */
1408 for (elem = info->snapshots; elem; elem = elem->next) {
1409 QEMUSnapshotInfo sn = {
1410 .vm_state_size = elem->value->vm_state_size,
1411 .date_sec = elem->value->date_sec,
1412 .date_nsec = elem->value->date_nsec,
1413 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
1414 elem->value->vm_clock_nsec,
1415 };
1416
1417 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
1418 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
1419 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
1420 }
1421 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001422}
1423
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001424static void dump_human_image_info_list(ImageInfoList *list)
1425{
1426 ImageInfoList *elem;
1427 bool delim = false;
1428
1429 for (elem = list; elem; elem = elem->next) {
1430 if (delim) {
1431 printf("\n");
1432 }
1433 delim = true;
1434
1435 dump_human_image_info(elem->value);
1436 }
1437}
1438
1439static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1440{
1441 return strcmp(a, b) == 0;
1442}
1443
1444/**
1445 * Open an image file chain and return an ImageInfoList
1446 *
1447 * @filename: topmost image filename
1448 * @fmt: topmost image format (may be NULL to autodetect)
1449 * @chain: true - enumerate entire backing file chain
1450 * false - only topmost image file
1451 *
1452 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1453 * image file. If there was an error a message will have been printed to
1454 * stderr.
1455 */
1456static ImageInfoList *collect_image_info_list(const char *filename,
1457 const char *fmt,
1458 bool chain)
1459{
1460 ImageInfoList *head = NULL;
1461 ImageInfoList **last = &head;
1462 GHashTable *filenames;
1463
1464 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1465
1466 while (filename) {
1467 BlockDriverState *bs;
1468 ImageInfo *info;
1469 ImageInfoList *elem;
1470
1471 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1472 error_report("Backing file '%s' creates an infinite loop.",
1473 filename);
1474 goto err;
1475 }
1476 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1477
1478 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
1479 false);
1480 if (!bs) {
1481 goto err;
1482 }
1483
1484 info = g_new0(ImageInfo, 1);
1485 collect_image_info(bs, info, filename, fmt);
1486 collect_snapshots(bs, info);
1487
1488 elem = g_new0(ImageInfoList, 1);
1489 elem->value = info;
1490 *last = elem;
1491 last = &elem->next;
1492
1493 bdrv_delete(bs);
1494
1495 filename = fmt = NULL;
1496 if (chain) {
1497 if (info->has_full_backing_filename) {
1498 filename = info->full_backing_filename;
1499 } else if (info->has_backing_filename) {
1500 filename = info->backing_filename;
1501 }
1502 if (info->has_backing_filename_format) {
1503 fmt = info->backing_filename_format;
1504 }
1505 }
1506 }
1507 g_hash_table_destroy(filenames);
1508 return head;
1509
1510err:
1511 qapi_free_ImageInfoList(head);
1512 g_hash_table_destroy(filenames);
1513 return NULL;
1514}
1515
Benoît Canetc054b3f2012-09-05 13:09:02 +02001516static int img_info(int argc, char **argv)
1517{
1518 int c;
1519 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001520 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001521 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001522 ImageInfoList *list;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001523
bellardea2384d2004-08-01 21:59:26 +00001524 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001525 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00001526 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02001527 int option_index = 0;
1528 static const struct option long_options[] = {
1529 {"help", no_argument, 0, 'h'},
1530 {"format", required_argument, 0, 'f'},
1531 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001532 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Benoît Canetc054b3f2012-09-05 13:09:02 +02001533 {0, 0, 0, 0}
1534 };
1535 c = getopt_long(argc, argv, "f:h",
1536 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001537 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001538 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001539 }
bellardea2384d2004-08-01 21:59:26 +00001540 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001541 case '?':
bellardea2384d2004-08-01 21:59:26 +00001542 case 'h':
1543 help();
1544 break;
1545 case 'f':
1546 fmt = optarg;
1547 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001548 case OPTION_OUTPUT:
1549 output = optarg;
1550 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001551 case OPTION_BACKING_CHAIN:
1552 chain = true;
1553 break;
bellardea2384d2004-08-01 21:59:26 +00001554 }
1555 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001556 if (optind >= argc) {
bellardea2384d2004-08-01 21:59:26 +00001557 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001558 }
bellardea2384d2004-08-01 21:59:26 +00001559 filename = argv[optind++];
1560
Benoît Canetc054b3f2012-09-05 13:09:02 +02001561 if (output && !strcmp(output, "json")) {
1562 output_format = OFORMAT_JSON;
1563 } else if (output && !strcmp(output, "human")) {
1564 output_format = OFORMAT_HUMAN;
1565 } else if (output) {
1566 error_report("--output must be used with human or json as argument.");
1567 return 1;
1568 }
1569
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001570 list = collect_image_info_list(filename, fmt, chain);
1571 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001572 return 1;
1573 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001574
Benoît Canetc054b3f2012-09-05 13:09:02 +02001575 switch (output_format) {
1576 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001577 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001578 break;
1579 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001580 if (chain) {
1581 dump_json_image_info_list(list);
1582 } else {
1583 dump_json_image_info(list->value);
1584 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001585 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001586 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001587
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001588 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00001589 return 0;
1590}
1591
aliguorif7b4a942009-01-07 17:40:15 +00001592#define SNAPSHOT_LIST 1
1593#define SNAPSHOT_CREATE 2
1594#define SNAPSHOT_APPLY 3
1595#define SNAPSHOT_DELETE 4
1596
Stuart Brady153859b2009-06-07 00:42:17 +01001597static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00001598{
1599 BlockDriverState *bs;
1600 QEMUSnapshotInfo sn;
1601 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001602 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00001603 int action = 0;
1604 qemu_timeval tv;
1605
Kevin Wolf710da702011-01-10 12:33:02 +01001606 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00001607 /* Parse commandline parameters */
1608 for(;;) {
1609 c = getopt(argc, argv, "la:c:d:h");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001610 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00001611 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001612 }
aliguorif7b4a942009-01-07 17:40:15 +00001613 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001614 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00001615 case 'h':
1616 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001617 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001618 case 'l':
1619 if (action) {
1620 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001621 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001622 }
1623 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001624 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00001625 break;
1626 case 'a':
1627 if (action) {
1628 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001629 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001630 }
1631 action = SNAPSHOT_APPLY;
1632 snapshot_name = optarg;
1633 break;
1634 case 'c':
1635 if (action) {
1636 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001637 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001638 }
1639 action = SNAPSHOT_CREATE;
1640 snapshot_name = optarg;
1641 break;
1642 case 'd':
1643 if (action) {
1644 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001645 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001646 }
1647 action = SNAPSHOT_DELETE;
1648 snapshot_name = optarg;
1649 break;
1650 }
1651 }
1652
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001653 if (optind >= argc) {
aliguorif7b4a942009-01-07 17:40:15 +00001654 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001655 }
aliguorif7b4a942009-01-07 17:40:15 +00001656 filename = argv[optind++];
1657
1658 /* Open the image */
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +01001659 bs = bdrv_new_open(filename, NULL, bdrv_oflags, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001660 if (!bs) {
1661 return 1;
1662 }
aliguorif7b4a942009-01-07 17:40:15 +00001663
1664 /* Perform the requested action */
1665 switch(action) {
1666 case SNAPSHOT_LIST:
1667 dump_snapshots(bs);
1668 break;
1669
1670 case SNAPSHOT_CREATE:
1671 memset(&sn, 0, sizeof(sn));
1672 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
1673
1674 qemu_gettimeofday(&tv);
1675 sn.date_sec = tv.tv_sec;
1676 sn.date_nsec = tv.tv_usec * 1000;
1677
1678 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001679 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001680 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001681 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001682 }
aliguorif7b4a942009-01-07 17:40:15 +00001683 break;
1684
1685 case SNAPSHOT_APPLY:
1686 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001687 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001688 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001689 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001690 }
aliguorif7b4a942009-01-07 17:40:15 +00001691 break;
1692
1693 case SNAPSHOT_DELETE:
1694 ret = bdrv_snapshot_delete(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001695 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001696 error_report("Could not delete snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001697 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001698 }
aliguorif7b4a942009-01-07 17:40:15 +00001699 break;
1700 }
1701
1702 /* Cleanup */
1703 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001704 if (ret) {
1705 return 1;
1706 }
Stuart Brady153859b2009-06-07 00:42:17 +01001707 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001708}
1709
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001710static int img_rebase(int argc, char **argv)
1711{
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001712 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
Stefan Hajnoczif163d072010-04-13 10:29:34 +01001713 BlockDriver *old_backing_drv, *new_backing_drv;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001714 char *filename;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001715 const char *fmt, *cache, *out_basefmt, *out_baseimg;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001716 int c, flags, ret;
1717 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001718 int progress = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001719
1720 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01001721 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001722 cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001723 out_baseimg = NULL;
1724 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001725 for(;;) {
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001726 c = getopt(argc, argv, "uhf:F:b:pt:");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001727 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001728 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001729 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001730 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001731 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001732 case 'h':
1733 help();
1734 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01001735 case 'f':
1736 fmt = optarg;
1737 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001738 case 'F':
1739 out_basefmt = optarg;
1740 break;
1741 case 'b':
1742 out_baseimg = optarg;
1743 break;
1744 case 'u':
1745 unsafe = 1;
1746 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001747 case 'p':
1748 progress = 1;
1749 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001750 case 't':
1751 cache = optarg;
1752 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001753 }
1754 }
1755
Anthony Liguori9a9d9db2011-04-13 15:51:47 +01001756 if ((optind >= argc) || (!unsafe && !out_baseimg)) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001757 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001758 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001759 filename = argv[optind++];
1760
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001761 qemu_progress_init(progress, 2.0);
1762 qemu_progress_print(0, 100);
1763
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001764 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001765 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001766 if (ret < 0) {
1767 error_report("Invalid cache option: %s", cache);
1768 return -1;
1769 }
1770
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001771 /*
1772 * Open the images.
1773 *
1774 * Ignore the old backing file for unsafe rebase in case we want to correct
1775 * the reference to a renamed or moved backing file.
1776 */
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +01001777 bs = bdrv_new_open(filename, fmt, flags, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001778 if (!bs) {
1779 return 1;
1780 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001781
1782 /* Find the right drivers for the backing files */
1783 old_backing_drv = NULL;
1784 new_backing_drv = NULL;
1785
1786 if (!unsafe && bs->backing_format[0] != '\0') {
1787 old_backing_drv = bdrv_find_format(bs->backing_format);
1788 if (old_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001789 error_report("Invalid format name: '%s'", bs->backing_format);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001790 ret = -1;
1791 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001792 }
1793 }
1794
1795 if (out_basefmt != NULL) {
1796 new_backing_drv = bdrv_find_format(out_basefmt);
1797 if (new_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001798 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001799 ret = -1;
1800 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001801 }
1802 }
1803
1804 /* For safe rebasing we need to compare old and new backing file */
1805 if (unsafe) {
1806 /* Make the compiler happy */
1807 bs_old_backing = NULL;
1808 bs_new_backing = NULL;
1809 } else {
1810 char backing_name[1024];
1811
1812 bs_old_backing = bdrv_new("old_backing");
1813 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001814 ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS,
1815 old_backing_drv);
1816 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001817 error_report("Could not open old backing file '%s'", backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001818 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001819 }
Alex Bligha6166732012-10-16 13:46:18 +01001820 if (out_baseimg[0]) {
1821 bs_new_backing = bdrv_new("new_backing");
1822 ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS,
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001823 new_backing_drv);
Alex Bligha6166732012-10-16 13:46:18 +01001824 if (ret) {
1825 error_report("Could not open new backing file '%s'",
1826 out_baseimg);
1827 goto out;
1828 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001829 }
1830 }
1831
1832 /*
1833 * Check each unallocated cluster in the COW file. If it is unallocated,
1834 * accesses go to the backing file. We must therefore compare this cluster
1835 * in the old and new backing file, and if they differ we need to copy it
1836 * from the old backing file into the COW file.
1837 *
1838 * If qemu-img crashes during this step, no harm is done. The content of
1839 * the image is the same as the original one at any time.
1840 */
1841 if (!unsafe) {
1842 uint64_t num_sectors;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001843 uint64_t old_backing_num_sectors;
Alex Bligha6166732012-10-16 13:46:18 +01001844 uint64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001845 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02001846 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08001847 uint8_t * buf_old;
1848 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02001849 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08001850
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001851 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
1852 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001853
1854 bdrv_get_geometry(bs, &num_sectors);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001855 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
Alex Bligha6166732012-10-16 13:46:18 +01001856 if (bs_new_backing) {
1857 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
1858 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001859
Kevin Wolf1f710492012-10-12 14:29:18 +02001860 if (num_sectors != 0) {
1861 local_progress = (float)100 /
1862 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
1863 }
1864
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001865 for (sector = 0; sector < num_sectors; sector += n) {
1866
1867 /* How many sectors can we handle with the next read? */
1868 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
1869 n = (IO_BUF_SIZE / 512);
1870 } else {
1871 n = num_sectors - sector;
1872 }
1873
1874 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02001875 ret = bdrv_is_allocated(bs, sector, n, &n);
1876 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001877 continue;
1878 }
1879
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001880 /*
1881 * Read old and new backing file and take into consideration that
1882 * backing files may be smaller than the COW image.
1883 */
1884 if (sector >= old_backing_num_sectors) {
1885 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
1886 } else {
1887 if (sector + n > old_backing_num_sectors) {
1888 n = old_backing_num_sectors - sector;
1889 }
1890
1891 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
1892 if (ret < 0) {
1893 error_report("error while reading from old backing file");
1894 goto out;
1895 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001896 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001897
Alex Bligha6166732012-10-16 13:46:18 +01001898 if (sector >= new_backing_num_sectors || !bs_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001899 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
1900 } else {
1901 if (sector + n > new_backing_num_sectors) {
1902 n = new_backing_num_sectors - sector;
1903 }
1904
1905 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
1906 if (ret < 0) {
1907 error_report("error while reading from new backing file");
1908 goto out;
1909 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001910 }
1911
1912 /* If they differ, we need to write to the COW file */
1913 uint64_t written = 0;
1914
1915 while (written < n) {
1916 int pnum;
1917
1918 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01001919 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001920 {
1921 ret = bdrv_write(bs, sector + written,
1922 buf_old + written * 512, pnum);
1923 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001924 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001925 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001926 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001927 }
1928 }
1929
1930 written += pnum;
1931 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001932 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001933 }
TeLeMand6771bf2010-02-08 16:20:00 +08001934
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001935 qemu_vfree(buf_old);
1936 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001937 }
1938
1939 /*
1940 * Change the backing file. All clusters that are different from the old
1941 * backing file are overwritten in the COW file now, so the visible content
1942 * doesn't change when we switch the backing file.
1943 */
Alex Bligha6166732012-10-16 13:46:18 +01001944 if (out_baseimg && *out_baseimg) {
1945 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
1946 } else {
1947 ret = bdrv_change_backing_file(bs, NULL, NULL);
1948 }
1949
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001950 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001951 error_report("Could not change the backing file to '%s': No "
1952 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001953 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001954 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001955 out_baseimg, strerror(-ret));
1956 }
1957
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001958 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001959 /*
1960 * TODO At this point it is possible to check if any clusters that are
1961 * allocated in the COW file are the same in the backing file. If so, they
1962 * could be dropped from the COW file. Don't do this before switching the
1963 * backing file, in case of a crash this would lead to corruption.
1964 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001965out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001966 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001967 /* Cleanup */
1968 if (!unsafe) {
Kevin Wolfeb863ad2011-03-31 12:39:51 +02001969 if (bs_old_backing != NULL) {
1970 bdrv_delete(bs_old_backing);
1971 }
1972 if (bs_new_backing != NULL) {
1973 bdrv_delete(bs_new_backing);
1974 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001975 }
1976
1977 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001978 if (ret) {
1979 return 1;
1980 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001981 return 0;
1982}
1983
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01001984static int img_resize(int argc, char **argv)
1985{
1986 int c, ret, relative;
1987 const char *filename, *fmt, *size;
1988 int64_t n, total_size;
Jes Sorensen2a819982010-12-06 17:08:31 +01001989 BlockDriverState *bs = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08001990 QemuOpts *param;
1991 static QemuOptsList resize_options = {
1992 .name = "resize_options",
1993 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
1994 .desc = {
1995 {
1996 .name = BLOCK_OPT_SIZE,
1997 .type = QEMU_OPT_SIZE,
1998 .help = "Virtual disk size"
1999 }, {
2000 /* end of list */
2001 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002002 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002003 };
2004
Kevin Wolfe80fec72011-04-29 10:58:12 +02002005 /* Remove size from argv manually so that negative numbers are not treated
2006 * as options by getopt. */
2007 if (argc < 3) {
2008 help();
2009 return 1;
2010 }
2011
2012 size = argv[--argc];
2013
2014 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002015 fmt = NULL;
2016 for(;;) {
2017 c = getopt(argc, argv, "f:h");
2018 if (c == -1) {
2019 break;
2020 }
2021 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002022 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002023 case 'h':
2024 help();
2025 break;
2026 case 'f':
2027 fmt = optarg;
2028 break;
2029 }
2030 }
Kevin Wolfe80fec72011-04-29 10:58:12 +02002031 if (optind >= argc) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002032 help();
2033 }
2034 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002035
2036 /* Choose grow, shrink, or absolute resize mode */
2037 switch (size[0]) {
2038 case '+':
2039 relative = 1;
2040 size++;
2041 break;
2042 case '-':
2043 relative = -1;
2044 size++;
2045 break;
2046 default:
2047 relative = 0;
2048 break;
2049 }
2050
2051 /* Parse size */
Dong Xu Wange478b442012-12-06 14:47:22 +08002052 param = qemu_opts_create_nofail(&resize_options);
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002053 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002054 /* Error message already printed when size parsing fails */
Jes Sorensen2a819982010-12-06 17:08:31 +01002055 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002056 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01002057 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002058 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002059 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2060 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002061
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +01002062 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002063 if (!bs) {
Jes Sorensen2a819982010-12-06 17:08:31 +01002064 ret = -1;
2065 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002066 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002067
2068 if (relative) {
2069 total_size = bdrv_getlength(bs) + n * relative;
2070 } else {
2071 total_size = n;
2072 }
2073 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002074 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002075 ret = -1;
2076 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002077 }
2078
2079 ret = bdrv_truncate(bs, total_size);
2080 switch (ret) {
2081 case 0:
2082 printf("Image resized.\n");
2083 break;
2084 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01002085 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002086 break;
2087 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01002088 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002089 break;
2090 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01002091 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002092 break;
2093 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002094out:
Jes Sorensen2a819982010-12-06 17:08:31 +01002095 if (bs) {
2096 bdrv_delete(bs);
2097 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002098 if (ret) {
2099 return 1;
2100 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002101 return 0;
2102}
2103
Anthony Liguoric227f092009-10-01 16:12:16 -05002104static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01002105#define DEF(option, callback, arg_string) \
2106 { option, callback },
2107#include "qemu-img-cmds.h"
2108#undef DEF
2109#undef GEN_DOCS
2110 { NULL, NULL, },
2111};
2112
bellardea2384d2004-08-01 21:59:26 +00002113int main(int argc, char **argv)
2114{
Anthony Liguoric227f092009-10-01 16:12:16 -05002115 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01002116 const char *cmdname;
bellardea2384d2004-08-01 21:59:26 +00002117
Kevin Wolf53f76e52010-12-16 15:10:32 +01002118 error_set_progname(argv[0]);
2119
Paolo Bonzini2592c592012-11-03 18:10:17 +01002120 qemu_init_main_loop();
bellardea2384d2004-08-01 21:59:26 +00002121 bdrv_init();
2122 if (argc < 2)
2123 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002124 cmdname = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00002125 argc--; argv++;
Stuart Brady153859b2009-06-07 00:42:17 +01002126
2127 /* find the command */
2128 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
2129 if (!strcmp(cmdname, cmd->name)) {
2130 return cmd->handler(argc, argv);
2131 }
bellardea2384d2004-08-01 21:59:26 +00002132 }
Stuart Brady153859b2009-06-07 00:42:17 +01002133
2134 /* not found */
2135 help();
bellardea2384d2004-08-01 21:59:26 +00002136 return 0;
2137}