blob: 3e2996e3c3e6909c1ab4bc914d64a480ad0c5cce [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>
Miroslav Rezaninaf382d432013-02-13 09:09:40 +010035#include <stdarg.h>
bellardea2384d2004-08-01 21:59:26 +000036
bellarde8445332006-06-14 15:32:10 +000037#ifdef _WIN32
38#include <windows.h>
39#endif
40
Anthony Liguoric227f092009-10-01 16:12:16 -050041typedef struct img_cmd_t {
Stuart Brady153859b2009-06-07 00:42:17 +010042 const char *name;
43 int (*handler)(int argc, char **argv);
Anthony Liguoric227f092009-10-01 16:12:16 -050044} img_cmd_t;
Stuart Brady153859b2009-06-07 00:42:17 +010045
Federico Simoncelli8599ea42013-01-28 06:59:47 -050046enum {
47 OPTION_OUTPUT = 256,
48 OPTION_BACKING_CHAIN = 257,
49};
50
51typedef enum OutputFormat {
52 OFORMAT_JSON,
53 OFORMAT_HUMAN,
54} OutputFormat;
55
aurel32137519c2008-11-30 19:12:49 +000056/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
Stefan Hajnocziadfe0782010-04-13 10:29:35 +010057#define BDRV_O_FLAGS BDRV_O_CACHE_WB
Federico Simoncelli661a0f72011-06-20 12:48:19 -040058#define BDRV_DEFAULT_CACHE "writeback"
aurel32137519c2008-11-30 19:12:49 +000059
bellardea2384d2004-08-01 21:59:26 +000060static void format_print(void *opaque, const char *name)
61{
62 printf(" %s", name);
63}
64
blueswir1d2c639d2009-01-24 18:19:25 +000065/* Please keep in synch with qemu-img.texi */
pbrook3f379ab2007-11-11 03:33:13 +000066static void help(void)
bellardea2384d2004-08-01 21:59:26 +000067{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010068 const char *help_msg =
69 "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n"
malc3f020d72010-02-08 12:04:56 +030070 "usage: qemu-img command [command options]\n"
71 "QEMU disk image utility\n"
72 "\n"
73 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +010074#define DEF(option, callback, arg_string) \
75 " " arg_string "\n"
76#include "qemu-img-cmds.h"
77#undef DEF
78#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +030079 "\n"
80 "Command parameters:\n"
81 " 'filename' is a disk image filename\n"
82 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -040083 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +080084 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
85 " 'directsync' and 'unsafe' (default for convert)\n"
malc3f020d72010-02-08 12:04:56 +030086 " 'size' is the disk image size in bytes. Optional suffixes\n"
87 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n"
88 " and T (terabyte, 1024G) are supported. 'b' is ignored.\n"
89 " 'output_filename' is the destination disk image filename\n"
90 " 'output_fmt' is the destination format\n"
91 " 'options' is a comma separated list of format specific options in a\n"
92 " name=value format. Use -o ? for an overview of the options supported by the\n"
93 " used format\n"
94 " '-c' indicates that target image must be compressed (qcow format only)\n"
95 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
96 " match exactly. The image doesn't need a working backing file before\n"
97 " rebasing in this case (useful for renaming the backing file)\n"
98 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +020099 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100100 " '-q' use Quiet mode - do not print any output (except errors)\n"
Kevin Wolfa22f1232011-08-26 15:27:13 +0200101 " '-S' indicates the consecutive number of bytes that must contain only zeros\n"
102 " for qemu-img to create a sparse image during conversion\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200103 " '--output' takes the format in which the output must be done (human or json)\n"
malc3f020d72010-02-08 12:04:56 +0300104 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200105 "Parameters to check subcommand:\n"
106 " '-r' tries to repair any inconsistencies that are found during the check.\n"
107 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
108 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200109 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200110 "\n"
malc3f020d72010-02-08 12:04:56 +0300111 "Parameters to snapshot subcommand:\n"
112 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
113 " '-a' applies a snapshot (revert disk to saved state)\n"
114 " '-c' creates a snapshot\n"
115 " '-d' deletes a snapshot\n"
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100116 " '-l' lists all snapshots in the given image\n";
117
118 printf("%s\nSupported formats:", help_msg);
bellardea2384d2004-08-01 21:59:26 +0000119 bdrv_iterate_format(format_print, NULL);
120 printf("\n");
121 exit(1);
122}
123
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100124static int qprintf(bool quiet, const char *fmt, ...)
125{
126 int ret = 0;
127 if (!quiet) {
128 va_list args;
129 va_start(args, fmt);
130 ret = vprintf(fmt, args);
131 va_end(args);
132 }
133 return ret;
134}
135
bellardea2384d2004-08-01 21:59:26 +0000136#if defined(WIN32)
137/* XXX: put correct support for win32 */
138static int read_password(char *buf, int buf_size)
139{
140 int c, i;
141 printf("Password: ");
142 fflush(stdout);
143 i = 0;
144 for(;;) {
145 c = getchar();
146 if (c == '\n')
147 break;
148 if (i < (buf_size - 1))
149 buf[i++] = c;
150 }
151 buf[i] = '\0';
152 return 0;
153}
154
155#else
156
157#include <termios.h>
158
159static struct termios oldtty;
160
161static void term_exit(void)
162{
163 tcsetattr (0, TCSANOW, &oldtty);
164}
165
166static void term_init(void)
167{
168 struct termios tty;
169
170 tcgetattr (0, &tty);
171 oldtty = tty;
172
173 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
174 |INLCR|IGNCR|ICRNL|IXON);
175 tty.c_oflag |= OPOST;
176 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
177 tty.c_cflag &= ~(CSIZE|PARENB);
178 tty.c_cflag |= CS8;
179 tty.c_cc[VMIN] = 1;
180 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000181
bellardea2384d2004-08-01 21:59:26 +0000182 tcsetattr (0, TCSANOW, &tty);
183
184 atexit(term_exit);
185}
186
pbrook3f379ab2007-11-11 03:33:13 +0000187static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000188{
189 uint8_t ch;
190 int i, ret;
191
192 printf("password: ");
193 fflush(stdout);
194 term_init();
195 i = 0;
196 for(;;) {
197 ret = read(0, &ch, 1);
198 if (ret == -1) {
199 if (errno == EAGAIN || errno == EINTR) {
200 continue;
201 } else {
202 ret = -1;
203 break;
204 }
205 } else if (ret == 0) {
206 ret = -1;
207 break;
208 } else {
209 if (ch == '\r') {
210 ret = 0;
211 break;
212 }
213 if (i < (buf_size - 1))
214 buf[i++] = ch;
215 }
216 }
217 term_exit();
218 buf[i] = '\0';
219 printf("\n");
220 return ret;
221}
222#endif
223
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100224static int print_block_option_help(const char *filename, const char *fmt)
225{
226 BlockDriver *drv, *proto_drv;
227 QEMUOptionParameter *create_options = NULL;
228
229 /* Find driver and parse its options */
230 drv = bdrv_find_format(fmt);
231 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100232 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100233 return 1;
234 }
235
236 proto_drv = bdrv_find_protocol(filename);
237 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100238 error_report("Unknown protocol '%s'", filename);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100239 return 1;
240 }
241
242 create_options = append_option_parameters(create_options,
243 drv->create_options);
244 create_options = append_option_parameters(create_options,
245 proto_drv->create_options);
246 print_option_help(create_options);
247 free_option_parameters(create_options);
248 return 0;
249}
250
bellard75c23802004-08-27 21:28:58 +0000251static BlockDriverState *bdrv_new_open(const char *filename,
Sheng Yang9bc378c2010-01-29 10:15:06 +0800252 const char *fmt,
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100253 int flags,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100254 bool require_io,
255 bool quiet)
bellard75c23802004-08-27 21:28:58 +0000256{
257 BlockDriverState *bs;
258 BlockDriver *drv;
259 char password[256];
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100260 int ret;
bellard75c23802004-08-27 21:28:58 +0000261
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100262 bs = bdrv_new("image");
Kevin Wolfad717132010-12-16 15:37:41 +0100263
bellard75c23802004-08-27 21:28:58 +0000264 if (fmt) {
265 drv = bdrv_find_format(fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900266 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100267 error_report("Unknown file format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900268 goto fail;
269 }
bellard75c23802004-08-27 21:28:58 +0000270 } else {
271 drv = NULL;
272 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100273
274 ret = bdrv_open(bs, filename, flags, drv);
275 if (ret < 0) {
276 error_report("Could not open '%s': %s", filename, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900277 goto fail;
bellard75c23802004-08-27 21:28:58 +0000278 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100279
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100280 if (bdrv_is_encrypted(bs) && require_io) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100281 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900282 if (read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100283 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900284 goto fail;
285 }
286 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100287 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900288 goto fail;
289 }
bellard75c23802004-08-27 21:28:58 +0000290 }
291 return bs;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900292fail:
293 if (bs) {
294 bdrv_delete(bs);
295 }
296 return NULL;
bellard75c23802004-08-27 21:28:58 +0000297}
298
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900299static int add_old_style_options(const char *fmt, QEMUOptionParameter *list,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100300 const char *base_filename,
301 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200302{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200303 if (base_filename) {
304 if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100305 error_report("Backing file not supported for file format '%s'",
306 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900307 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200308 }
309 }
310 if (base_fmt) {
311 if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100312 error_report("Backing file format not supported for file "
313 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900314 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200315 }
316 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900317 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200318}
319
bellardea2384d2004-08-01 21:59:26 +0000320static int img_create(int argc, char **argv)
321{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200322 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100323 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000324 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000325 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000326 const char *filename;
327 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200328 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200329 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100330 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000331
bellardea2384d2004-08-01 21:59:26 +0000332 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100333 c = getopt(argc, argv, "F:b:f:he6o:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100334 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000335 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100336 }
bellardea2384d2004-08-01 21:59:26 +0000337 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100338 case '?':
bellardea2384d2004-08-01 21:59:26 +0000339 case 'h':
340 help();
341 break;
aliguori9230eaf2009-03-28 17:55:19 +0000342 case 'F':
343 base_fmt = optarg;
344 break;
bellardea2384d2004-08-01 21:59:26 +0000345 case 'b':
346 base_filename = optarg;
347 break;
348 case 'f':
349 fmt = optarg;
350 break;
351 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200352 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100353 "encryption\' instead!");
354 return 1;
thsd8871c52007-10-24 16:11:42 +0000355 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200356 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100357 "compat6\' instead!");
358 return 1;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200359 case 'o':
360 options = optarg;
361 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100362 case 'q':
363 quiet = true;
364 break;
bellardea2384d2004-08-01 21:59:26 +0000365 }
366 }
aliguori9230eaf2009-03-28 17:55:19 +0000367
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900368 /* Get the filename */
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100369 if (optind >= argc) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900370 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100371 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900372 filename = argv[optind++];
373
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100374 /* Get image size, if specified */
375 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100376 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100377 char *end;
378 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
379 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800380 if (sval == -ERANGE) {
381 error_report("Image size must be less than 8 EiB!");
382 } else {
383 error_report("Invalid image size specified! You may use k, M, "
384 "G or T suffixes for ");
385 error_report("kilobytes, megabytes, gigabytes and terabytes.");
386 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200387 return 1;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100388 }
389 img_size = (uint64_t)sval;
390 }
391
Peter Maydellc8057f92012-08-02 13:45:54 +0100392 if (options && is_help_option(options)) {
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200393 return print_block_option_help(filename, fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100394 }
395
Luiz Capitulino9b375252012-11-30 10:52:05 -0200396 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100397 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Luiz Capitulino9b375252012-11-30 10:52:05 -0200398 if (error_is_set(&local_err)) {
399 error_report("%s", error_get_pretty(local_err));
400 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900401 return 1;
402 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200403
bellardea2384d2004-08-01 21:59:26 +0000404 return 0;
405}
406
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100407static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500408{
409 Error *errp = NULL;
410 QString *str;
411 QmpOutputVisitor *ov = qmp_output_visitor_new();
412 QObject *obj;
413 visit_type_ImageCheck(qmp_output_get_visitor(ov),
414 &check, NULL, &errp);
415 obj = qmp_output_get_qobject(ov);
416 str = qobject_to_json_pretty(obj);
417 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100418 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500419 qobject_decref(obj);
420 qmp_output_visitor_cleanup(ov);
421 QDECREF(str);
422}
423
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100424static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500425{
426 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100427 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500428 } else {
429 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100430 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
431 "Data may be corrupted, or further writes to the image "
432 "may corrupt it.\n",
433 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500434 }
435
436 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100437 qprintf(quiet,
438 "\n%" PRId64 " leaked clusters were found on the image.\n"
439 "This means waste of disk space, but no harm to data.\n",
440 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500441 }
442
443 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100444 qprintf(quiet,
445 "\n%" PRId64
446 " internal errors have occurred during the check.\n",
447 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500448 }
449 }
450
451 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100452 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
453 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
454 check->allocated_clusters, check->total_clusters,
455 check->allocated_clusters * 100.0 / check->total_clusters,
456 check->fragmented_clusters * 100.0 / check->allocated_clusters,
457 check->compressed_clusters * 100.0 /
458 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500459 }
460
461 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100462 qprintf(quiet,
463 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500464 }
465}
466
467static int collect_image_check(BlockDriverState *bs,
468 ImageCheck *check,
469 const char *filename,
470 const char *fmt,
471 int fix)
472{
473 int ret;
474 BdrvCheckResult result;
475
476 ret = bdrv_check(bs, &result, fix);
477 if (ret < 0) {
478 return ret;
479 }
480
481 check->filename = g_strdup(filename);
482 check->format = g_strdup(bdrv_get_format_name(bs));
483 check->check_errors = result.check_errors;
484 check->corruptions = result.corruptions;
485 check->has_corruptions = result.corruptions != 0;
486 check->leaks = result.leaks;
487 check->has_leaks = result.leaks != 0;
488 check->corruptions_fixed = result.corruptions_fixed;
489 check->has_corruptions_fixed = result.corruptions != 0;
490 check->leaks_fixed = result.leaks_fixed;
491 check->has_leaks_fixed = result.leaks != 0;
492 check->image_end_offset = result.image_end_offset;
493 check->has_image_end_offset = result.image_end_offset != 0;
494 check->total_clusters = result.bfi.total_clusters;
495 check->has_total_clusters = result.bfi.total_clusters != 0;
496 check->allocated_clusters = result.bfi.allocated_clusters;
497 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
498 check->fragmented_clusters = result.bfi.fragmented_clusters;
499 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100500 check->compressed_clusters = result.bfi.compressed_clusters;
501 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500502
503 return 0;
504}
505
Kevin Wolfe076f332010-06-29 11:43:13 +0200506/*
507 * Checks an image for consistency. Exit codes:
508 *
509 * 0 - Check completed, image is good
510 * 1 - Check not completed because of internal errors
511 * 2 - Check completed, image is corrupted
512 * 3 - Check completed, image has leaked clusters, but is good otherwise
513 */
aliguori15859692009-04-21 23:11:53 +0000514static int img_check(int argc, char **argv)
515{
516 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500517 OutputFormat output_format = OFORMAT_HUMAN;
518 const char *filename, *fmt, *output;
aliguori15859692009-04-21 23:11:53 +0000519 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200520 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100521 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500522 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100523 bool quiet = false;
aliguori15859692009-04-21 23:11:53 +0000524
525 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500526 output = NULL;
aliguori15859692009-04-21 23:11:53 +0000527 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500528 int option_index = 0;
529 static const struct option long_options[] = {
530 {"help", no_argument, 0, 'h'},
531 {"format", required_argument, 0, 'f'},
532 {"repair", no_argument, 0, 'r'},
533 {"output", required_argument, 0, OPTION_OUTPUT},
534 {0, 0, 0, 0}
535 };
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100536 c = getopt_long(argc, argv, "f:hr:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500537 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100538 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000539 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100540 }
aliguori15859692009-04-21 23:11:53 +0000541 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100542 case '?':
aliguori15859692009-04-21 23:11:53 +0000543 case 'h':
544 help();
545 break;
546 case 'f':
547 fmt = optarg;
548 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200549 case 'r':
550 flags |= BDRV_O_RDWR;
551
552 if (!strcmp(optarg, "leaks")) {
553 fix = BDRV_FIX_LEAKS;
554 } else if (!strcmp(optarg, "all")) {
555 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
556 } else {
557 help();
558 }
559 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500560 case OPTION_OUTPUT:
561 output = optarg;
562 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100563 case 'q':
564 quiet = true;
565 break;
aliguori15859692009-04-21 23:11:53 +0000566 }
567 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100568 if (optind >= argc) {
aliguori15859692009-04-21 23:11:53 +0000569 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100570 }
aliguori15859692009-04-21 23:11:53 +0000571 filename = argv[optind++];
572
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500573 if (output && !strcmp(output, "json")) {
574 output_format = OFORMAT_JSON;
575 } else if (output && !strcmp(output, "human")) {
576 output_format = OFORMAT_HUMAN;
577 } else if (output) {
578 error_report("--output must be used with human or json as argument.");
579 return 1;
580 }
581
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100582 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900583 if (!bs) {
584 return 1;
585 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500586
587 check = g_new0(ImageCheck, 1);
588 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200589
590 if (ret == -ENOTSUP) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500591 if (output_format == OFORMAT_HUMAN) {
592 error_report("This image format does not support checks");
593 }
594 ret = 1;
595 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200596 }
597
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500598 if (check->corruptions_fixed || check->leaks_fixed) {
599 int corruptions_fixed, leaks_fixed;
600
601 leaks_fixed = check->leaks_fixed;
602 corruptions_fixed = check->corruptions_fixed;
603
604 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100605 qprintf(quiet,
606 "The following inconsistencies were found and repaired:\n\n"
607 " %" PRId64 " leaked clusters\n"
608 " %" PRId64 " corruptions\n\n"
609 "Double checking the fixed image now...\n",
610 check->leaks_fixed,
611 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500612 }
613
614 ret = collect_image_check(bs, check, filename, fmt, 0);
615
616 check->leaks_fixed = leaks_fixed;
617 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200618 }
619
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500620 switch (output_format) {
621 case OFORMAT_HUMAN:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100622 dump_human_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500623 break;
624 case OFORMAT_JSON:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100625 dump_json_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500626 break;
627 }
628
629 if (ret || check->check_errors) {
630 ret = 1;
631 goto fail;
632 }
633
634 if (check->corruptions) {
635 ret = 2;
636 } else if (check->leaks) {
637 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200638 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500639 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000640 }
641
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500642fail:
643 qapi_free_ImageCheck(check);
aliguori15859692009-04-21 23:11:53 +0000644 bdrv_delete(bs);
Kevin Wolfe076f332010-06-29 11:43:13 +0200645
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500646 return ret;
aliguori15859692009-04-21 23:11:53 +0000647}
648
bellardea2384d2004-08-01 21:59:26 +0000649static int img_commit(int argc, char **argv)
650{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400651 int c, ret, flags;
652 const char *filename, *fmt, *cache;
bellardea2384d2004-08-01 21:59:26 +0000653 BlockDriverState *bs;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100654 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +0000655
656 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400657 cache = BDRV_DEFAULT_CACHE;
bellardea2384d2004-08-01 21:59:26 +0000658 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100659 c = getopt(argc, argv, "f:ht:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100660 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000661 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100662 }
bellardea2384d2004-08-01 21:59:26 +0000663 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100664 case '?':
bellardea2384d2004-08-01 21:59:26 +0000665 case 'h':
666 help();
667 break;
668 case 'f':
669 fmt = optarg;
670 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400671 case 't':
672 cache = optarg;
673 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100674 case 'q':
675 quiet = true;
676 break;
bellardea2384d2004-08-01 21:59:26 +0000677 }
678 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100679 if (optind >= argc) {
bellardea2384d2004-08-01 21:59:26 +0000680 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100681 }
bellardea2384d2004-08-01 21:59:26 +0000682 filename = argv[optind++];
683
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400684 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100685 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400686 if (ret < 0) {
687 error_report("Invalid cache option: %s", cache);
688 return -1;
689 }
690
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100691 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900692 if (!bs) {
693 return 1;
694 }
bellardea2384d2004-08-01 21:59:26 +0000695 ret = bdrv_commit(bs);
696 switch(ret) {
697 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100698 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000699 break;
700 case -ENOENT:
Jes Sorensen15654a62010-12-16 14:31:53 +0100701 error_report("No disk inserted");
bellardea2384d2004-08-01 21:59:26 +0000702 break;
703 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +0100704 error_report("Image is read-only");
bellardea2384d2004-08-01 21:59:26 +0000705 break;
706 case -ENOTSUP:
Jes Sorensen15654a62010-12-16 14:31:53 +0100707 error_report("Image is already committed");
bellardea2384d2004-08-01 21:59:26 +0000708 break;
709 default:
Jes Sorensen15654a62010-12-16 14:31:53 +0100710 error_report("Error while committing image");
bellardea2384d2004-08-01 21:59:26 +0000711 break;
712 }
713
714 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900715 if (ret) {
716 return 1;
717 }
bellardea2384d2004-08-01 21:59:26 +0000718 return 0;
719}
720
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400721/*
thsf58c7b32008-06-05 21:53:49 +0000722 * Returns true iff the first sector pointed to by 'buf' contains at least
723 * a non-NUL byte.
724 *
725 * 'pnum' is set to the number of sectors (including and immediately following
726 * the first one) that are known to be in the same allocated/unallocated state.
727 */
bellardea2384d2004-08-01 21:59:26 +0000728static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
729{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000730 bool is_zero;
731 int i;
bellardea2384d2004-08-01 21:59:26 +0000732
733 if (n <= 0) {
734 *pnum = 0;
735 return 0;
736 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000737 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000738 for(i = 1; i < n; i++) {
739 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000740 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000741 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000742 }
bellardea2384d2004-08-01 21:59:26 +0000743 }
744 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000745 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000746}
747
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100748/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200749 * Like is_allocated_sectors, but if the buffer starts with a used sector,
750 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
751 * breaking up write requests for only small sparse areas.
752 */
753static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
754 int min)
755{
756 int ret;
757 int num_checked, num_used;
758
759 if (n < min) {
760 min = n;
761 }
762
763 ret = is_allocated_sectors(buf, n, pnum);
764 if (!ret) {
765 return ret;
766 }
767
768 num_used = *pnum;
769 buf += BDRV_SECTOR_SIZE * *pnum;
770 n -= *pnum;
771 num_checked = num_used;
772
773 while (n > 0) {
774 ret = is_allocated_sectors(buf, n, pnum);
775
776 buf += BDRV_SECTOR_SIZE * *pnum;
777 n -= *pnum;
778 num_checked += *pnum;
779 if (ret) {
780 num_used = num_checked;
781 } else if (*pnum >= min) {
782 break;
783 }
784 }
785
786 *pnum = num_used;
787 return 1;
788}
789
790/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100791 * Compares two buffers sector by sector. Returns 0 if the first sector of both
792 * buffers matches, non-zero otherwise.
793 *
794 * pnum is set to the number of sectors (including and immediately following
795 * the first one) that are known to have the same comparison result
796 */
797static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
798 int *pnum)
799{
800 int res, i;
801
802 if (n <= 0) {
803 *pnum = 0;
804 return 0;
805 }
806
807 res = !!memcmp(buf1, buf2, 512);
808 for(i = 1; i < n; i++) {
809 buf1 += 512;
810 buf2 += 512;
811
812 if (!!memcmp(buf1, buf2, 512) != res) {
813 break;
814 }
815 }
816
817 *pnum = i;
818 return res;
819}
820
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200821#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000822
823static int img_convert(int argc, char **argv)
824{
Jes Sorenseneec77d92010-12-07 17:44:34 +0100825 int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400826 int progress = 0, flags;
827 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900828 BlockDriver *drv, *proto_drv;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900829 BlockDriverState **bs = NULL, *out_bs = NULL;
ths96b8f132007-12-17 01:35:20 +0000830 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
831 uint64_t bs_sectors;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900832 uint8_t * buf = NULL;
bellardea2384d2004-08-01 21:59:26 +0000833 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +0000834 BlockDriverInfo bdi;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900835 QEMUOptionParameter *param = NULL, *create_options = NULL;
Kevin Wolfa18953f2010-10-14 15:46:04 +0200836 QEMUOptionParameter *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200837 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -0700838 const char *snapshot_name = NULL;
Kevin Wolf1f710492012-10-12 14:29:18 +0200839 float local_progress = 0;
Kevin Wolfa22f1232011-08-26 15:27:13 +0200840 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100841 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +0000842
843 fmt = NULL;
844 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400845 cache = "unsafe";
thsf58c7b32008-06-05 21:53:49 +0000846 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +0100847 compress = 0;
bellardea2384d2004-08-01 21:59:26 +0000848 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100849 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100850 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000851 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100852 }
bellardea2384d2004-08-01 21:59:26 +0000853 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100854 case '?':
bellardea2384d2004-08-01 21:59:26 +0000855 case 'h':
856 help();
857 break;
858 case 'f':
859 fmt = optarg;
860 break;
861 case 'O':
862 out_fmt = optarg;
863 break;
thsf58c7b32008-06-05 21:53:49 +0000864 case 'B':
865 out_baseimg = optarg;
866 break;
bellardea2384d2004-08-01 21:59:26 +0000867 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +0100868 compress = 1;
bellardea2384d2004-08-01 21:59:26 +0000869 break;
870 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200871 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100872 "encryption\' instead!");
873 return 1;
thsec36ba12007-09-16 21:59:02 +0000874 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200875 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100876 "compat6\' instead!");
877 return 1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200878 case 'o':
879 options = optarg;
880 break;
edison51ef6722010-09-21 19:58:41 -0700881 case 's':
882 snapshot_name = optarg;
883 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +0200884 case 'S':
885 {
886 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100887 char *end;
888 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
889 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +0200890 error_report("Invalid minimum zero buffer size for sparse output specified");
891 return 1;
892 }
893
894 min_sparse = sval / BDRV_SECTOR_SIZE;
895 break;
896 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200897 case 'p':
898 progress = 1;
899 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400900 case 't':
901 cache = optarg;
902 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100903 case 'q':
904 quiet = true;
905 break;
bellardea2384d2004-08-01 21:59:26 +0000906 }
907 }
ths3b46e622007-09-17 08:09:54 +0000908
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100909 if (quiet) {
910 progress = 0;
911 }
912
balrog926c2d22007-10-31 01:11:44 +0000913 bs_n = argc - optind - 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100914 if (bs_n < 1) {
915 help();
916 }
balrog926c2d22007-10-31 01:11:44 +0000917
918 out_filename = argv[argc - 1];
thsf58c7b32008-06-05 21:53:49 +0000919
Charles Arnoldfa170c12012-05-11 10:57:54 -0600920 /* Initialize before goto out */
921 qemu_progress_init(progress, 2.0);
922
Peter Maydellc8057f92012-08-02 13:45:54 +0100923 if (options && is_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100924 ret = print_block_option_help(out_filename, out_fmt);
925 goto out;
926 }
927
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900928 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100929 error_report("-B makes no sense when concatenating multiple input "
930 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +0100931 ret = -1;
932 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900933 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +0800934
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200935 qemu_progress_print(0, 100);
936
Anthony Liguori7267c092011-08-20 22:09:37 -0500937 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
balrog926c2d22007-10-31 01:11:44 +0000938
939 total_sectors = 0;
940 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100941 bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true,
942 quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900943 if (!bs[bs_i]) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100944 error_report("Could not open '%s'", argv[optind + bs_i]);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900945 ret = -1;
946 goto out;
947 }
balrog926c2d22007-10-31 01:11:44 +0000948 bdrv_get_geometry(bs[bs_i], &bs_sectors);
949 total_sectors += bs_sectors;
950 }
bellardea2384d2004-08-01 21:59:26 +0000951
edison51ef6722010-09-21 19:58:41 -0700952 if (snapshot_name != NULL) {
953 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200954 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -0700955 ret = -1;
956 goto out;
957 }
958 if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) {
Markus Armbruster6daf1942011-06-22 14:03:54 +0200959 error_report("Failed to load snapshot");
edison51ef6722010-09-21 19:58:41 -0700960 ret = -1;
961 goto out;
962 }
963 }
964
Kevin Wolfefa84d42009-05-18 16:42:12 +0200965 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +0000966 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900967 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100968 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900969 ret = -1;
970 goto out;
971 }
balrog926c2d22007-10-31 01:11:44 +0000972
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900973 proto_drv = bdrv_find_protocol(out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900974 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100975 error_report("Unknown protocol '%s'", out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900976 ret = -1;
977 goto out;
978 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900979
980 create_options = append_option_parameters(create_options,
981 drv->create_options);
982 create_options = append_option_parameters(create_options,
983 proto_drv->create_options);
Kevin Wolfdb08adf2009-06-04 15:39:38 +0200984
Kevin Wolfefa84d42009-05-18 16:42:12 +0200985 if (options) {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900986 param = parse_option_parameters(options, create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +0200987 if (param == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100988 error_report("Invalid options for file format '%s'.", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900989 ret = -1;
990 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200991 }
992 } else {
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900993 param = parse_option_parameters("", create_options, param);
Kevin Wolfefa84d42009-05-18 16:42:12 +0200994 }
995
996 set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512);
Jes Sorenseneec77d92010-12-07 17:44:34 +0100997 ret = add_old_style_options(out_fmt, param, out_baseimg, NULL);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900998 if (ret < 0) {
999 goto out;
1000 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001001
Kevin Wolfa18953f2010-10-14 15:46:04 +02001002 /* Get backing file name if -o backing_file was used */
1003 out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
1004 if (out_baseimg_param) {
1005 out_baseimg = out_baseimg_param->value.s;
1006 }
1007
Kevin Wolfefa84d42009-05-18 16:42:12 +02001008 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001009 if (compress) {
Kevin Wolfefa84d42009-05-18 16:42:12 +02001010 QEMUOptionParameter *encryption =
1011 get_option_parameter(param, BLOCK_OPT_ENCRYPT);
Kevin Wolf41521fa2011-10-18 16:19:42 +02001012 QEMUOptionParameter *preallocation =
1013 get_option_parameter(param, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001014
1015 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001016 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001017 ret = -1;
1018 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001019 }
1020
1021 if (encryption && encryption->value.n) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001022 error_report("Compression and encryption not supported at "
1023 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001024 ret = -1;
1025 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001026 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02001027
1028 if (preallocation && preallocation->value.s
1029 && strcmp(preallocation->value.s, "off"))
1030 {
1031 error_report("Compression and preallocation not supported at "
1032 "the same time");
1033 ret = -1;
1034 goto out;
1035 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001036 }
1037
1038 /* Create the new image */
1039 ret = bdrv_create(drv, out_filename, param);
bellardea2384d2004-08-01 21:59:26 +00001040 if (ret < 0) {
1041 if (ret == -ENOTSUP) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001042 error_report("Formatting not supported for file format '%s'",
1043 out_fmt);
aurel326e9ea0c2009-04-15 14:42:46 +00001044 } else if (ret == -EFBIG) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001045 error_report("The image size is too large for file format '%s'",
1046 out_fmt);
bellardea2384d2004-08-01 21:59:26 +00001047 } else {
Jes Sorensen15654a62010-12-16 14:31:53 +01001048 error_report("%s: error while converting %s: %s",
1049 out_filename, out_fmt, strerror(-ret));
bellardea2384d2004-08-01 21:59:26 +00001050 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001051 goto out;
bellardea2384d2004-08-01 21:59:26 +00001052 }
ths3b46e622007-09-17 08:09:54 +00001053
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001054 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001055 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001056 if (ret < 0) {
1057 error_report("Invalid cache option: %s", cache);
1058 return -1;
1059 }
1060
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001061 out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001062 if (!out_bs) {
1063 ret = -1;
1064 goto out;
1065 }
bellardea2384d2004-08-01 21:59:26 +00001066
balrog926c2d22007-10-31 01:11:44 +00001067 bs_i = 0;
1068 bs_offset = 0;
1069 bdrv_get_geometry(bs[0], &bs_sectors);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001070 buf = qemu_blockalign(out_bs, IO_BUF_SIZE);
balrog926c2d22007-10-31 01:11:44 +00001071
Jes Sorenseneec77d92010-12-07 17:44:34 +01001072 if (compress) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001073 ret = bdrv_get_info(out_bs, &bdi);
1074 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001075 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001076 goto out;
1077 }
bellardfaea38e2006-08-05 21:31:00 +00001078 cluster_size = bdi.cluster_size;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001079 if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001080 error_report("invalid cluster size");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001081 ret = -1;
1082 goto out;
1083 }
bellardea2384d2004-08-01 21:59:26 +00001084 cluster_sectors = cluster_size >> 9;
1085 sector_num = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001086
1087 nb_sectors = total_sectors;
Kevin Wolf1f710492012-10-12 14:29:18 +02001088 if (nb_sectors != 0) {
1089 local_progress = (float)100 /
1090 (nb_sectors / MIN(nb_sectors, cluster_sectors));
1091 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001092
bellardea2384d2004-08-01 21:59:26 +00001093 for(;;) {
balrog926c2d22007-10-31 01:11:44 +00001094 int64_t bs_num;
1095 int remainder;
1096 uint8_t *buf2;
1097
bellardea2384d2004-08-01 21:59:26 +00001098 nb_sectors = total_sectors - sector_num;
1099 if (nb_sectors <= 0)
1100 break;
1101 if (nb_sectors >= cluster_sectors)
1102 n = cluster_sectors;
1103 else
1104 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +00001105
1106 bs_num = sector_num - bs_offset;
1107 assert (bs_num >= 0);
1108 remainder = n;
1109 buf2 = buf;
1110 while (remainder > 0) {
1111 int nlow;
1112 while (bs_num == bs_sectors) {
1113 bs_i++;
1114 assert (bs_i < bs_n);
1115 bs_offset += bs_sectors;
1116 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1117 bs_num = 0;
Blue Swirl0bfcd592010-05-22 08:02:12 +00001118 /* printf("changing part: sector_num=%" PRId64 ", "
1119 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1120 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
balrog926c2d22007-10-31 01:11:44 +00001121 }
1122 assert (bs_num < bs_sectors);
1123
1124 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1125
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001126 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1127 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001128 error_report("error while reading sector %" PRId64 ": %s",
1129 bs_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001130 goto out;
1131 }
balrog926c2d22007-10-31 01:11:44 +00001132
1133 buf2 += nlow * 512;
1134 bs_num += nlow;
1135
1136 remainder -= nlow;
1137 }
1138 assert (remainder == 0);
1139
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001140 if (n < cluster_sectors) {
bellardea2384d2004-08-01 21:59:26 +00001141 memset(buf + n * 512, 0, cluster_size - n * 512);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001142 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +00001143 if (!buffer_is_zero(buf, cluster_size)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001144 ret = bdrv_write_compressed(out_bs, sector_num, buf,
1145 cluster_sectors);
1146 if (ret != 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001147 error_report("error while compressing sector %" PRId64
1148 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001149 goto out;
1150 }
bellardea2384d2004-08-01 21:59:26 +00001151 }
1152 sector_num += n;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001153 qemu_progress_print(local_progress, 100);
bellardea2384d2004-08-01 21:59:26 +00001154 }
bellardfaea38e2006-08-05 21:31:00 +00001155 /* signal EOF to align */
1156 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +00001157 } else {
Kevin Wolff2feebb2010-04-14 17:30:35 +02001158 int has_zero_init = bdrv_has_zero_init(out_bs);
1159
thsf58c7b32008-06-05 21:53:49 +00001160 sector_num = 0; // total number of sectors converted so far
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001161 nb_sectors = total_sectors - sector_num;
Kevin Wolf1f710492012-10-12 14:29:18 +02001162 if (nb_sectors != 0) {
1163 local_progress = (float)100 /
1164 (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512));
1165 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001166
bellardea2384d2004-08-01 21:59:26 +00001167 for(;;) {
1168 nb_sectors = total_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001169 if (nb_sectors <= 0) {
bellardea2384d2004-08-01 21:59:26 +00001170 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001171 }
1172 if (nb_sectors >= (IO_BUF_SIZE / 512)) {
bellardea2384d2004-08-01 21:59:26 +00001173 n = (IO_BUF_SIZE / 512);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001174 } else {
bellardea2384d2004-08-01 21:59:26 +00001175 n = nb_sectors;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001176 }
balrog926c2d22007-10-31 01:11:44 +00001177
1178 while (sector_num - bs_offset >= bs_sectors) {
1179 bs_i ++;
1180 assert (bs_i < bs_n);
1181 bs_offset += bs_sectors;
1182 bdrv_get_geometry(bs[bs_i], &bs_sectors);
Blue Swirl0bfcd592010-05-22 08:02:12 +00001183 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1184 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
balrog926c2d22007-10-31 01:11:44 +00001185 sector_num, bs_i, bs_offset, bs_sectors); */
1186 }
1187
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001188 if (n > bs_offset + bs_sectors - sector_num) {
balrog926c2d22007-10-31 01:11:44 +00001189 n = bs_offset + bs_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001190 }
balrog926c2d22007-10-31 01:11:44 +00001191
Kevin Wolff2feebb2010-04-14 17:30:35 +02001192 if (has_zero_init) {
Akkarit Sangpetchd0320442009-07-17 10:02:15 +02001193 /* If the output image is being created as a copy on write image,
1194 assume that sectors which are unallocated in the input image
1195 are present in both the output's and input's base images (no
1196 need to copy them). */
1197 if (out_baseimg) {
1198 if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
1199 n, &n1)) {
1200 sector_num += n1;
1201 continue;
1202 }
1203 /* The next 'n1' sectors are allocated in the input image. Copy
1204 only those as they may be followed by unallocated sectors. */
1205 n = n1;
aliguori93c65b42009-04-05 17:40:43 +00001206 }
aliguori93c65b42009-04-05 17:40:43 +00001207 } else {
1208 n1 = n;
thsf58c7b32008-06-05 21:53:49 +00001209 }
1210
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001211 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1212 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001213 error_report("error while reading sector %" PRId64 ": %s",
1214 sector_num - bs_offset, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001215 goto out;
1216 }
bellardea2384d2004-08-01 21:59:26 +00001217 /* NOTE: at the same time we convert, we do not write zero
1218 sectors to have a chance to compress the image. Ideally, we
1219 should add a specific call to have the info to go faster */
1220 buf1 = buf;
1221 while (n > 0) {
thsf58c7b32008-06-05 21:53:49 +00001222 /* If the output image is being created as a copy on write image,
1223 copy all sectors even the ones containing only NUL bytes,
aliguori93c65b42009-04-05 17:40:43 +00001224 because they may differ from the sectors in the base image.
1225
1226 If the output is to a host device, we also write out
1227 sectors that are entirely 0, since whatever data was
1228 already there is garbage, not 0s. */
Kevin Wolff2feebb2010-04-14 17:30:35 +02001229 if (!has_zero_init || out_baseimg ||
Kevin Wolfa22f1232011-08-26 15:27:13 +02001230 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001231 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1232 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001233 error_report("error while writing sector %" PRId64
1234 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001235 goto out;
1236 }
bellardea2384d2004-08-01 21:59:26 +00001237 }
1238 sector_num += n1;
1239 n -= n1;
1240 buf1 += n1 * 512;
1241 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001242 qemu_progress_print(local_progress, 100);
bellardea2384d2004-08-01 21:59:26 +00001243 }
1244 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001245out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001246 qemu_progress_end();
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001247 free_option_parameters(create_options);
1248 free_option_parameters(param);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001249 qemu_vfree(buf);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001250 if (out_bs) {
1251 bdrv_delete(out_bs);
1252 }
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001253 if (bs) {
1254 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1255 if (bs[bs_i]) {
1256 bdrv_delete(bs[bs_i]);
1257 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001258 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001259 g_free(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001260 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001261 if (ret) {
1262 return 1;
1263 }
bellardea2384d2004-08-01 21:59:26 +00001264 return 0;
1265}
1266
bellard57d1a2b2004-08-03 21:15:11 +00001267
bellardfaea38e2006-08-05 21:31:00 +00001268static void dump_snapshots(BlockDriverState *bs)
1269{
1270 QEMUSnapshotInfo *sn_tab, *sn;
1271 int nb_sns, i;
1272 char buf[256];
1273
1274 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1275 if (nb_sns <= 0)
1276 return;
1277 printf("Snapshot list:\n");
1278 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1279 for(i = 0; i < nb_sns; i++) {
1280 sn = &sn_tab[i];
1281 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1282 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001283 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00001284}
1285
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001286static void dump_json_image_info_list(ImageInfoList *list)
1287{
1288 Error *errp = NULL;
1289 QString *str;
1290 QmpOutputVisitor *ov = qmp_output_visitor_new();
1291 QObject *obj;
1292 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
1293 &list, NULL, &errp);
1294 obj = qmp_output_get_qobject(ov);
1295 str = qobject_to_json_pretty(obj);
1296 assert(str != NULL);
1297 printf("%s\n", qstring_get_str(str));
1298 qobject_decref(obj);
1299 qmp_output_visitor_cleanup(ov);
1300 QDECREF(str);
1301}
1302
Benoît Canetc054b3f2012-09-05 13:09:02 +02001303static void collect_snapshots(BlockDriverState *bs , ImageInfo *info)
bellardea2384d2004-08-01 21:59:26 +00001304{
Benoît Canetc054b3f2012-09-05 13:09:02 +02001305 int i, sn_count;
1306 QEMUSnapshotInfo *sn_tab = NULL;
1307 SnapshotInfoList *info_list, *cur_item = NULL;
1308 sn_count = bdrv_snapshot_list(bs, &sn_tab);
1309
1310 for (i = 0; i < sn_count; i++) {
1311 info->has_snapshots = true;
1312 info_list = g_new0(SnapshotInfoList, 1);
1313
1314 info_list->value = g_new0(SnapshotInfo, 1);
1315 info_list->value->id = g_strdup(sn_tab[i].id_str);
1316 info_list->value->name = g_strdup(sn_tab[i].name);
1317 info_list->value->vm_state_size = sn_tab[i].vm_state_size;
1318 info_list->value->date_sec = sn_tab[i].date_sec;
1319 info_list->value->date_nsec = sn_tab[i].date_nsec;
1320 info_list->value->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000;
1321 info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000;
1322
1323 /* XXX: waiting for the qapi to support qemu-queue.h types */
1324 if (!cur_item) {
1325 info->snapshots = cur_item = info_list;
1326 } else {
1327 cur_item->next = info_list;
1328 cur_item = info_list;
1329 }
1330
1331 }
1332
1333 g_free(sn_tab);
1334}
1335
1336static void dump_json_image_info(ImageInfo *info)
1337{
1338 Error *errp = NULL;
1339 QString *str;
1340 QmpOutputVisitor *ov = qmp_output_visitor_new();
1341 QObject *obj;
1342 visit_type_ImageInfo(qmp_output_get_visitor(ov),
1343 &info, NULL, &errp);
1344 obj = qmp_output_get_qobject(ov);
1345 str = qobject_to_json_pretty(obj);
1346 assert(str != NULL);
1347 printf("%s\n", qstring_get_str(str));
1348 qobject_decref(obj);
1349 qmp_output_visitor_cleanup(ov);
1350 QDECREF(str);
1351}
1352
1353static void collect_image_info(BlockDriverState *bs,
1354 ImageInfo *info,
1355 const char *filename,
1356 const char *fmt)
1357{
ths96b8f132007-12-17 01:35:20 +00001358 uint64_t total_sectors;
bellard93b6b2a2006-08-01 15:51:11 +00001359 char backing_filename[1024];
1360 char backing_filename2[1024];
bellardfaea38e2006-08-05 21:31:00 +00001361 BlockDriverInfo bdi;
bellardea2384d2004-08-01 21:59:26 +00001362
Benoît Canetc054b3f2012-09-05 13:09:02 +02001363 bdrv_get_geometry(bs, &total_sectors);
1364
1365 info->filename = g_strdup(filename);
1366 info->format = g_strdup(bdrv_get_format_name(bs));
1367 info->virtual_size = total_sectors * 512;
1368 info->actual_size = bdrv_get_allocated_file_size(bs);
1369 info->has_actual_size = info->actual_size >= 0;
1370 if (bdrv_is_encrypted(bs)) {
1371 info->encrypted = true;
1372 info->has_encrypted = true;
1373 }
1374 if (bdrv_get_info(bs, &bdi) >= 0) {
1375 if (bdi.cluster_size != 0) {
1376 info->cluster_size = bdi.cluster_size;
1377 info->has_cluster_size = true;
1378 }
1379 info->dirty_flag = bdi.is_dirty;
1380 info->has_dirty_flag = true;
1381 }
1382 bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename));
1383 if (backing_filename[0] != '\0') {
1384 info->backing_filename = g_strdup(backing_filename);
1385 info->has_backing_filename = true;
1386 bdrv_get_full_backing_filename(bs, backing_filename2,
1387 sizeof(backing_filename2));
1388
1389 if (strcmp(backing_filename, backing_filename2) != 0) {
1390 info->full_backing_filename =
1391 g_strdup(backing_filename2);
1392 info->has_full_backing_filename = true;
1393 }
1394
1395 if (bs->backing_format[0]) {
1396 info->backing_filename_format = g_strdup(bs->backing_format);
1397 info->has_backing_filename_format = true;
1398 }
1399 }
1400}
1401
1402static void dump_human_image_info(ImageInfo *info)
1403{
1404 char size_buf[128], dsize_buf[128];
1405 if (!info->has_actual_size) {
1406 snprintf(dsize_buf, sizeof(dsize_buf), "unavailable");
1407 } else {
1408 get_human_readable_size(dsize_buf, sizeof(dsize_buf),
1409 info->actual_size);
1410 }
1411 get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size);
1412 printf("image: %s\n"
1413 "file format: %s\n"
1414 "virtual size: %s (%" PRId64 " bytes)\n"
1415 "disk size: %s\n",
1416 info->filename, info->format, size_buf,
1417 info->virtual_size,
1418 dsize_buf);
1419
1420 if (info->has_encrypted && info->encrypted) {
1421 printf("encrypted: yes\n");
1422 }
1423
1424 if (info->has_cluster_size) {
1425 printf("cluster_size: %" PRId64 "\n", info->cluster_size);
1426 }
1427
1428 if (info->has_dirty_flag && info->dirty_flag) {
1429 printf("cleanly shut down: no\n");
1430 }
1431
1432 if (info->has_backing_filename) {
1433 printf("backing file: %s", info->backing_filename);
1434 if (info->has_full_backing_filename) {
1435 printf(" (actual path: %s)", info->full_backing_filename);
1436 }
1437 putchar('\n');
1438 if (info->has_backing_filename_format) {
1439 printf("backing file format: %s\n", info->backing_filename_format);
1440 }
1441 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001442
1443 if (info->has_snapshots) {
1444 SnapshotInfoList *elem;
1445 char buf[256];
1446
1447 printf("Snapshot list:\n");
1448 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1449
1450 /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but
1451 * we convert to the block layer's native QEMUSnapshotInfo for now.
1452 */
1453 for (elem = info->snapshots; elem; elem = elem->next) {
1454 QEMUSnapshotInfo sn = {
1455 .vm_state_size = elem->value->vm_state_size,
1456 .date_sec = elem->value->date_sec,
1457 .date_nsec = elem->value->date_nsec,
1458 .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL +
1459 elem->value->vm_clock_nsec,
1460 };
1461
1462 pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id);
1463 pstrcpy(sn.name, sizeof(sn.name), elem->value->name);
1464 printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn));
1465 }
1466 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001467}
1468
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001469static void dump_human_image_info_list(ImageInfoList *list)
1470{
1471 ImageInfoList *elem;
1472 bool delim = false;
1473
1474 for (elem = list; elem; elem = elem->next) {
1475 if (delim) {
1476 printf("\n");
1477 }
1478 delim = true;
1479
1480 dump_human_image_info(elem->value);
1481 }
1482}
1483
1484static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1485{
1486 return strcmp(a, b) == 0;
1487}
1488
1489/**
1490 * Open an image file chain and return an ImageInfoList
1491 *
1492 * @filename: topmost image filename
1493 * @fmt: topmost image format (may be NULL to autodetect)
1494 * @chain: true - enumerate entire backing file chain
1495 * false - only topmost image file
1496 *
1497 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1498 * image file. If there was an error a message will have been printed to
1499 * stderr.
1500 */
1501static ImageInfoList *collect_image_info_list(const char *filename,
1502 const char *fmt,
1503 bool chain)
1504{
1505 ImageInfoList *head = NULL;
1506 ImageInfoList **last = &head;
1507 GHashTable *filenames;
1508
1509 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1510
1511 while (filename) {
1512 BlockDriverState *bs;
1513 ImageInfo *info;
1514 ImageInfoList *elem;
1515
1516 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1517 error_report("Backing file '%s' creates an infinite loop.",
1518 filename);
1519 goto err;
1520 }
1521 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1522
1523 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001524 false, false);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001525 if (!bs) {
1526 goto err;
1527 }
1528
1529 info = g_new0(ImageInfo, 1);
1530 collect_image_info(bs, info, filename, fmt);
1531 collect_snapshots(bs, info);
1532
1533 elem = g_new0(ImageInfoList, 1);
1534 elem->value = info;
1535 *last = elem;
1536 last = &elem->next;
1537
1538 bdrv_delete(bs);
1539
1540 filename = fmt = NULL;
1541 if (chain) {
1542 if (info->has_full_backing_filename) {
1543 filename = info->full_backing_filename;
1544 } else if (info->has_backing_filename) {
1545 filename = info->backing_filename;
1546 }
1547 if (info->has_backing_filename_format) {
1548 fmt = info->backing_filename_format;
1549 }
1550 }
1551 }
1552 g_hash_table_destroy(filenames);
1553 return head;
1554
1555err:
1556 qapi_free_ImageInfoList(head);
1557 g_hash_table_destroy(filenames);
1558 return NULL;
1559}
1560
Benoît Canetc054b3f2012-09-05 13:09:02 +02001561static int img_info(int argc, char **argv)
1562{
1563 int c;
1564 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001565 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001566 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001567 ImageInfoList *list;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001568
bellardea2384d2004-08-01 21:59:26 +00001569 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001570 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00001571 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02001572 int option_index = 0;
1573 static const struct option long_options[] = {
1574 {"help", no_argument, 0, 'h'},
1575 {"format", required_argument, 0, 'f'},
1576 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001577 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Benoît Canetc054b3f2012-09-05 13:09:02 +02001578 {0, 0, 0, 0}
1579 };
1580 c = getopt_long(argc, argv, "f:h",
1581 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001582 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001583 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001584 }
bellardea2384d2004-08-01 21:59:26 +00001585 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001586 case '?':
bellardea2384d2004-08-01 21:59:26 +00001587 case 'h':
1588 help();
1589 break;
1590 case 'f':
1591 fmt = optarg;
1592 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001593 case OPTION_OUTPUT:
1594 output = optarg;
1595 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001596 case OPTION_BACKING_CHAIN:
1597 chain = true;
1598 break;
bellardea2384d2004-08-01 21:59:26 +00001599 }
1600 }
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001601 if (optind >= argc) {
bellardea2384d2004-08-01 21:59:26 +00001602 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001603 }
bellardea2384d2004-08-01 21:59:26 +00001604 filename = argv[optind++];
1605
Benoît Canetc054b3f2012-09-05 13:09:02 +02001606 if (output && !strcmp(output, "json")) {
1607 output_format = OFORMAT_JSON;
1608 } else if (output && !strcmp(output, "human")) {
1609 output_format = OFORMAT_HUMAN;
1610 } else if (output) {
1611 error_report("--output must be used with human or json as argument.");
1612 return 1;
1613 }
1614
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001615 list = collect_image_info_list(filename, fmt, chain);
1616 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001617 return 1;
1618 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001619
Benoît Canetc054b3f2012-09-05 13:09:02 +02001620 switch (output_format) {
1621 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001622 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001623 break;
1624 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001625 if (chain) {
1626 dump_json_image_info_list(list);
1627 } else {
1628 dump_json_image_info(list->value);
1629 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001630 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001631 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001632
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001633 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00001634 return 0;
1635}
1636
aliguorif7b4a942009-01-07 17:40:15 +00001637#define SNAPSHOT_LIST 1
1638#define SNAPSHOT_CREATE 2
1639#define SNAPSHOT_APPLY 3
1640#define SNAPSHOT_DELETE 4
1641
Stuart Brady153859b2009-06-07 00:42:17 +01001642static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00001643{
1644 BlockDriverState *bs;
1645 QEMUSnapshotInfo sn;
1646 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001647 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00001648 int action = 0;
1649 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001650 bool quiet = false;
aliguorif7b4a942009-01-07 17:40:15 +00001651
Kevin Wolf710da702011-01-10 12:33:02 +01001652 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00001653 /* Parse commandline parameters */
1654 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001655 c = getopt(argc, argv, "la:c:d:hq");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001656 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00001657 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001658 }
aliguorif7b4a942009-01-07 17:40:15 +00001659 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001660 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00001661 case 'h':
1662 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001663 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001664 case 'l':
1665 if (action) {
1666 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001667 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001668 }
1669 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02001670 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00001671 break;
1672 case 'a':
1673 if (action) {
1674 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001675 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001676 }
1677 action = SNAPSHOT_APPLY;
1678 snapshot_name = optarg;
1679 break;
1680 case 'c':
1681 if (action) {
1682 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001683 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001684 }
1685 action = SNAPSHOT_CREATE;
1686 snapshot_name = optarg;
1687 break;
1688 case 'd':
1689 if (action) {
1690 help();
Stuart Brady153859b2009-06-07 00:42:17 +01001691 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001692 }
1693 action = SNAPSHOT_DELETE;
1694 snapshot_name = optarg;
1695 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001696 case 'q':
1697 quiet = true;
1698 break;
aliguorif7b4a942009-01-07 17:40:15 +00001699 }
1700 }
1701
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001702 if (optind >= argc) {
aliguorif7b4a942009-01-07 17:40:15 +00001703 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001704 }
aliguorif7b4a942009-01-07 17:40:15 +00001705 filename = argv[optind++];
1706
1707 /* Open the image */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001708 bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001709 if (!bs) {
1710 return 1;
1711 }
aliguorif7b4a942009-01-07 17:40:15 +00001712
1713 /* Perform the requested action */
1714 switch(action) {
1715 case SNAPSHOT_LIST:
1716 dump_snapshots(bs);
1717 break;
1718
1719 case SNAPSHOT_CREATE:
1720 memset(&sn, 0, sizeof(sn));
1721 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
1722
1723 qemu_gettimeofday(&tv);
1724 sn.date_sec = tv.tv_sec;
1725 sn.date_nsec = tv.tv_usec * 1000;
1726
1727 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001728 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001729 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001730 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001731 }
aliguorif7b4a942009-01-07 17:40:15 +00001732 break;
1733
1734 case SNAPSHOT_APPLY:
1735 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001736 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001737 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001738 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001739 }
aliguorif7b4a942009-01-07 17:40:15 +00001740 break;
1741
1742 case SNAPSHOT_DELETE:
1743 ret = bdrv_snapshot_delete(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001744 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001745 error_report("Could not delete snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00001746 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001747 }
aliguorif7b4a942009-01-07 17:40:15 +00001748 break;
1749 }
1750
1751 /* Cleanup */
1752 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001753 if (ret) {
1754 return 1;
1755 }
Stuart Brady153859b2009-06-07 00:42:17 +01001756 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00001757}
1758
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001759static int img_rebase(int argc, char **argv)
1760{
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001761 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
Stefan Hajnoczif163d072010-04-13 10:29:34 +01001762 BlockDriver *old_backing_drv, *new_backing_drv;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001763 char *filename;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001764 const char *fmt, *cache, *out_basefmt, *out_baseimg;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001765 int c, flags, ret;
1766 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001767 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001768 bool quiet = false;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001769
1770 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01001771 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001772 cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001773 out_baseimg = NULL;
1774 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001775 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001776 c = getopt(argc, argv, "uhf:F:b:pt:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001777 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001778 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001779 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001780 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001781 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001782 case 'h':
1783 help();
1784 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01001785 case 'f':
1786 fmt = optarg;
1787 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001788 case 'F':
1789 out_basefmt = optarg;
1790 break;
1791 case 'b':
1792 out_baseimg = optarg;
1793 break;
1794 case 'u':
1795 unsafe = 1;
1796 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001797 case 'p':
1798 progress = 1;
1799 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001800 case 't':
1801 cache = optarg;
1802 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001803 case 'q':
1804 quiet = true;
1805 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001806 }
1807 }
1808
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001809 if (quiet) {
1810 progress = 0;
1811 }
1812
Anthony Liguori9a9d9db2011-04-13 15:51:47 +01001813 if ((optind >= argc) || (!unsafe && !out_baseimg)) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001814 help();
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001815 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001816 filename = argv[optind++];
1817
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001818 qemu_progress_init(progress, 2.0);
1819 qemu_progress_print(0, 100);
1820
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001821 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001822 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001823 if (ret < 0) {
1824 error_report("Invalid cache option: %s", cache);
1825 return -1;
1826 }
1827
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001828 /*
1829 * Open the images.
1830 *
1831 * Ignore the old backing file for unsafe rebase in case we want to correct
1832 * the reference to a renamed or moved backing file.
1833 */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001834 bs = bdrv_new_open(filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001835 if (!bs) {
1836 return 1;
1837 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001838
1839 /* Find the right drivers for the backing files */
1840 old_backing_drv = NULL;
1841 new_backing_drv = NULL;
1842
1843 if (!unsafe && bs->backing_format[0] != '\0') {
1844 old_backing_drv = bdrv_find_format(bs->backing_format);
1845 if (old_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001846 error_report("Invalid format name: '%s'", bs->backing_format);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001847 ret = -1;
1848 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001849 }
1850 }
1851
1852 if (out_basefmt != NULL) {
1853 new_backing_drv = bdrv_find_format(out_basefmt);
1854 if (new_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001855 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001856 ret = -1;
1857 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001858 }
1859 }
1860
1861 /* For safe rebasing we need to compare old and new backing file */
1862 if (unsafe) {
1863 /* Make the compiler happy */
1864 bs_old_backing = NULL;
1865 bs_new_backing = NULL;
1866 } else {
1867 char backing_name[1024];
1868
1869 bs_old_backing = bdrv_new("old_backing");
1870 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001871 ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS,
1872 old_backing_drv);
1873 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001874 error_report("Could not open old backing file '%s'", backing_name);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001875 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001876 }
Alex Bligha6166732012-10-16 13:46:18 +01001877 if (out_baseimg[0]) {
1878 bs_new_backing = bdrv_new("new_backing");
1879 ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS,
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001880 new_backing_drv);
Alex Bligha6166732012-10-16 13:46:18 +01001881 if (ret) {
1882 error_report("Could not open new backing file '%s'",
1883 out_baseimg);
1884 goto out;
1885 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001886 }
1887 }
1888
1889 /*
1890 * Check each unallocated cluster in the COW file. If it is unallocated,
1891 * accesses go to the backing file. We must therefore compare this cluster
1892 * in the old and new backing file, and if they differ we need to copy it
1893 * from the old backing file into the COW file.
1894 *
1895 * If qemu-img crashes during this step, no harm is done. The content of
1896 * the image is the same as the original one at any time.
1897 */
1898 if (!unsafe) {
1899 uint64_t num_sectors;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001900 uint64_t old_backing_num_sectors;
Alex Bligha6166732012-10-16 13:46:18 +01001901 uint64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001902 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02001903 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08001904 uint8_t * buf_old;
1905 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02001906 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08001907
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001908 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
1909 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001910
1911 bdrv_get_geometry(bs, &num_sectors);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001912 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
Alex Bligha6166732012-10-16 13:46:18 +01001913 if (bs_new_backing) {
1914 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
1915 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001916
Kevin Wolf1f710492012-10-12 14:29:18 +02001917 if (num_sectors != 0) {
1918 local_progress = (float)100 /
1919 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
1920 }
1921
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001922 for (sector = 0; sector < num_sectors; sector += n) {
1923
1924 /* How many sectors can we handle with the next read? */
1925 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
1926 n = (IO_BUF_SIZE / 512);
1927 } else {
1928 n = num_sectors - sector;
1929 }
1930
1931 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02001932 ret = bdrv_is_allocated(bs, sector, n, &n);
1933 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001934 continue;
1935 }
1936
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001937 /*
1938 * Read old and new backing file and take into consideration that
1939 * backing files may be smaller than the COW image.
1940 */
1941 if (sector >= old_backing_num_sectors) {
1942 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
1943 } else {
1944 if (sector + n > old_backing_num_sectors) {
1945 n = old_backing_num_sectors - sector;
1946 }
1947
1948 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
1949 if (ret < 0) {
1950 error_report("error while reading from old backing file");
1951 goto out;
1952 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001953 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001954
Alex Bligha6166732012-10-16 13:46:18 +01001955 if (sector >= new_backing_num_sectors || !bs_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01001956 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
1957 } else {
1958 if (sector + n > new_backing_num_sectors) {
1959 n = new_backing_num_sectors - sector;
1960 }
1961
1962 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
1963 if (ret < 0) {
1964 error_report("error while reading from new backing file");
1965 goto out;
1966 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001967 }
1968
1969 /* If they differ, we need to write to the COW file */
1970 uint64_t written = 0;
1971
1972 while (written < n) {
1973 int pnum;
1974
1975 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01001976 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001977 {
1978 ret = bdrv_write(bs, sector + written,
1979 buf_old + written * 512, pnum);
1980 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001981 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001982 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001983 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001984 }
1985 }
1986
1987 written += pnum;
1988 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001989 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001990 }
TeLeMand6771bf2010-02-08 16:20:00 +08001991
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001992 qemu_vfree(buf_old);
1993 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01001994 }
1995
1996 /*
1997 * Change the backing file. All clusters that are different from the old
1998 * backing file are overwritten in the COW file now, so the visible content
1999 * doesn't change when we switch the backing file.
2000 */
Alex Bligha6166732012-10-16 13:46:18 +01002001 if (out_baseimg && *out_baseimg) {
2002 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2003 } else {
2004 ret = bdrv_change_backing_file(bs, NULL, NULL);
2005 }
2006
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002007 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002008 error_report("Could not change the backing file to '%s': No "
2009 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002010 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002011 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002012 out_baseimg, strerror(-ret));
2013 }
2014
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002015 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002016 /*
2017 * TODO At this point it is possible to check if any clusters that are
2018 * allocated in the COW file are the same in the backing file. If so, they
2019 * could be dropped from the COW file. Don't do this before switching the
2020 * backing file, in case of a crash this would lead to corruption.
2021 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002022out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002023 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002024 /* Cleanup */
2025 if (!unsafe) {
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002026 if (bs_old_backing != NULL) {
2027 bdrv_delete(bs_old_backing);
2028 }
2029 if (bs_new_backing != NULL) {
2030 bdrv_delete(bs_new_backing);
2031 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002032 }
2033
2034 bdrv_delete(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002035 if (ret) {
2036 return 1;
2037 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002038 return 0;
2039}
2040
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002041static int img_resize(int argc, char **argv)
2042{
2043 int c, ret, relative;
2044 const char *filename, *fmt, *size;
2045 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002046 bool quiet = false;
Jes Sorensen2a819982010-12-06 17:08:31 +01002047 BlockDriverState *bs = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002048 QemuOpts *param;
2049 static QemuOptsList resize_options = {
2050 .name = "resize_options",
2051 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2052 .desc = {
2053 {
2054 .name = BLOCK_OPT_SIZE,
2055 .type = QEMU_OPT_SIZE,
2056 .help = "Virtual disk size"
2057 }, {
2058 /* end of list */
2059 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002060 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002061 };
2062
Kevin Wolfe80fec72011-04-29 10:58:12 +02002063 /* Remove size from argv manually so that negative numbers are not treated
2064 * as options by getopt. */
2065 if (argc < 3) {
2066 help();
2067 return 1;
2068 }
2069
2070 size = argv[--argc];
2071
2072 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002073 fmt = NULL;
2074 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002075 c = getopt(argc, argv, "f:hq");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002076 if (c == -1) {
2077 break;
2078 }
2079 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002080 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002081 case 'h':
2082 help();
2083 break;
2084 case 'f':
2085 fmt = optarg;
2086 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002087 case 'q':
2088 quiet = true;
2089 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002090 }
2091 }
Kevin Wolfe80fec72011-04-29 10:58:12 +02002092 if (optind >= argc) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002093 help();
2094 }
2095 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002096
2097 /* Choose grow, shrink, or absolute resize mode */
2098 switch (size[0]) {
2099 case '+':
2100 relative = 1;
2101 size++;
2102 break;
2103 case '-':
2104 relative = -1;
2105 size++;
2106 break;
2107 default:
2108 relative = 0;
2109 break;
2110 }
2111
2112 /* Parse size */
Dong Xu Wange478b442012-12-06 14:47:22 +08002113 param = qemu_opts_create_nofail(&resize_options);
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002114 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002115 /* Error message already printed when size parsing fails */
Jes Sorensen2a819982010-12-06 17:08:31 +01002116 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002117 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01002118 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002119 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002120 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2121 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002122
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002123 bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002124 if (!bs) {
Jes Sorensen2a819982010-12-06 17:08:31 +01002125 ret = -1;
2126 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002127 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002128
2129 if (relative) {
2130 total_size = bdrv_getlength(bs) + n * relative;
2131 } else {
2132 total_size = n;
2133 }
2134 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002135 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002136 ret = -1;
2137 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002138 }
2139
2140 ret = bdrv_truncate(bs, total_size);
2141 switch (ret) {
2142 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002143 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002144 break;
2145 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01002146 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002147 break;
2148 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01002149 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002150 break;
2151 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01002152 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002153 break;
2154 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002155out:
Jes Sorensen2a819982010-12-06 17:08:31 +01002156 if (bs) {
2157 bdrv_delete(bs);
2158 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002159 if (ret) {
2160 return 1;
2161 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002162 return 0;
2163}
2164
Anthony Liguoric227f092009-10-01 16:12:16 -05002165static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01002166#define DEF(option, callback, arg_string) \
2167 { option, callback },
2168#include "qemu-img-cmds.h"
2169#undef DEF
2170#undef GEN_DOCS
2171 { NULL, NULL, },
2172};
2173
bellardea2384d2004-08-01 21:59:26 +00002174int main(int argc, char **argv)
2175{
Anthony Liguoric227f092009-10-01 16:12:16 -05002176 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01002177 const char *cmdname;
bellardea2384d2004-08-01 21:59:26 +00002178
Kevin Wolf53f76e52010-12-16 15:10:32 +01002179 error_set_progname(argv[0]);
2180
Paolo Bonzini2592c592012-11-03 18:10:17 +01002181 qemu_init_main_loop();
bellardea2384d2004-08-01 21:59:26 +00002182 bdrv_init();
2183 if (argc < 2)
2184 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002185 cmdname = argv[1];
aurel328f9b1572009-02-09 18:14:31 +00002186 argc--; argv++;
Stuart Brady153859b2009-06-07 00:42:17 +01002187
2188 /* find the command */
2189 for(cmd = img_cmds; cmd->name != NULL; cmd++) {
2190 if (!strcmp(cmdname, cmd->name)) {
2191 return cmd->handler(argc, argv);
2192 }
bellardea2384d2004-08-01 21:59:26 +00002193 }
Stuart Brady153859b2009-06-07 00:42:17 +01002194
2195 /* not found */
2196 help();
bellardea2384d2004-08-01 21:59:26 +00002197 return 0;
2198}