blob: c98896b281a049472b9e21fa93e31e31ef8e558e [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"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080033#include "block/qapi.h"
Benoît Canetc054b3f2012-09-05 13:09:02 +020034#include <getopt.h>
Mike Day1a443c12014-05-05 12:53:34 -040035#include <glib.h>
bellarde8445332006-06-14 15:32:10 +000036
Jeff Cody5f6979c2014-04-28 14:37:18 -040037#define QEMU_IMG_VERSION "qemu-img version " QEMU_VERSION \
38 ", Copyright (c) 2004-2008 Fabrice Bellard\n"
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
Mike Day1a443c12014-05-05 12:53:34 -040059static gint compare_data(gconstpointer a, gconstpointer b, gpointer user)
bellardea2384d2004-08-01 21:59:26 +000060{
Mike Day1a443c12014-05-05 12:53:34 -040061 return g_strcmp0(a, b);
62}
63
64static void print_format(gpointer data, gpointer user)
65{
66 printf(" %s", (char *)data);
67}
68
69static void add_format_to_seq(void *opaque, const char *fmt_name)
70{
71 GSequence *seq = opaque;
72
Mike Day395071a2014-05-13 17:11:06 -040073 g_sequence_insert_sorted(seq, (gpointer)fmt_name,
74 compare_data, NULL);
bellardea2384d2004-08-01 21:59:26 +000075}
76
Fam Zhengac1307a2014-04-22 13:36:11 +080077static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...)
78{
79 va_list ap;
80
81 error_printf("qemu-img: ");
82
83 va_start(ap, fmt);
84 error_vprintf(fmt, ap);
85 va_end(ap);
86
87 error_printf("\nTry 'qemu-img --help' for more information\n");
88 exit(EXIT_FAILURE);
89}
90
blueswir1d2c639d2009-01-24 18:19:25 +000091/* Please keep in synch with qemu-img.texi */
Fam Zhengac1307a2014-04-22 13:36:11 +080092static void QEMU_NORETURN help(void)
bellardea2384d2004-08-01 21:59:26 +000093{
Paolo Bonzinie00291c2010-02-04 16:49:56 +010094 const char *help_msg =
Jeff Cody5f6979c2014-04-28 14:37:18 -040095 QEMU_IMG_VERSION
malc3f020d72010-02-08 12:04:56 +030096 "usage: qemu-img command [command options]\n"
97 "QEMU disk image utility\n"
98 "\n"
99 "Command syntax:\n"
Stuart Brady153859b2009-06-07 00:42:17 +0100100#define DEF(option, callback, arg_string) \
101 " " arg_string "\n"
102#include "qemu-img-cmds.h"
103#undef DEF
104#undef GEN_DOCS
malc3f020d72010-02-08 12:04:56 +0300105 "\n"
106 "Command parameters:\n"
107 " 'filename' is a disk image filename\n"
108 " 'fmt' is the disk image format. It is guessed automatically in most cases\n"
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400109 " 'cache' is the cache mode used to write the output disk image, the valid\n"
Liu Yuan80ccf932012-04-20 17:10:56 +0800110 " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n"
111 " 'directsync' and 'unsafe' (default for convert)\n"
malc3f020d72010-02-08 12:04:56 +0300112 " 'size' is the disk image size in bytes. Optional suffixes\n"
Kevin Wolf5e009842013-06-05 14:19:27 +0200113 " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n"
114 " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n"
115 " supported. 'b' is ignored.\n"
malc3f020d72010-02-08 12:04:56 +0300116 " 'output_filename' is the destination disk image filename\n"
117 " 'output_fmt' is the destination format\n"
118 " 'options' is a comma separated list of format specific options in a\n"
119 " name=value format. Use -o ? for an overview of the options supported by the\n"
120 " used format\n"
Wenchao Xiaef806542013-12-04 17:10:57 +0800121 " 'snapshot_param' is param used for internal snapshot, format\n"
122 " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
123 " '[ID_OR_NAME]'\n"
124 " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n"
125 " instead\n"
malc3f020d72010-02-08 12:04:56 +0300126 " '-c' indicates that target image must be compressed (qcow format only)\n"
127 " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n"
128 " match exactly. The image doesn't need a working backing file before\n"
129 " rebasing in this case (useful for renaming the backing file)\n"
130 " '-h' with or without a command shows this help and lists the supported formats\n"
Jes Sorensen6b837bc2011-03-30 14:16:25 +0200131 " '-p' show progress of command (only certain commands)\n"
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100132 " '-q' use Quiet mode - do not print any output (except errors)\n"
Peter Lieven11b66992013-10-24 12:07:05 +0200133 " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n"
134 " contain only zeros for qemu-img to create a sparse image during\n"
135 " conversion. If the number of bytes is 0, the source will not be scanned for\n"
136 " unallocated or zero sectors, and the destination image will always be\n"
137 " fully allocated\n"
Benoît Canetc054b3f2012-09-05 13:09:02 +0200138 " '--output' takes the format in which the output must be done (human or json)\n"
Alexandre Derumierb2e10492013-09-02 19:07:24 +0100139 " '-n' skips the target volume creation (useful if the volume is created\n"
140 " prior to running qemu-img)\n"
malc3f020d72010-02-08 12:04:56 +0300141 "\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200142 "Parameters to check subcommand:\n"
143 " '-r' tries to repair any inconsistencies that are found during the check.\n"
144 " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n"
145 " kinds of errors, with a higher risk of choosing the wrong fix or\n"
Stefan Weil0546b8c2012-08-10 22:03:25 +0200146 " hiding corruption that has already occurred.\n"
Kevin Wolf4534ff52012-05-11 16:07:02 +0200147 "\n"
malc3f020d72010-02-08 12:04:56 +0300148 "Parameters to snapshot subcommand:\n"
149 " 'snapshot' is the name of the snapshot to create, apply or delete\n"
150 " '-a' applies a snapshot (revert disk to saved state)\n"
151 " '-c' creates a snapshot\n"
152 " '-d' deletes a snapshot\n"
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100153 " '-l' lists all snapshots in the given image\n"
154 "\n"
155 "Parameters to compare subcommand:\n"
156 " '-f' first image format\n"
157 " '-F' second image format\n"
158 " '-s' run in Strict mode - fail on different image size or sector allocation\n";
Mike Day1a443c12014-05-05 12:53:34 -0400159 GSequence *seq;
Paolo Bonzinie00291c2010-02-04 16:49:56 +0100160
161 printf("%s\nSupported formats:", help_msg);
Mike Day1a443c12014-05-05 12:53:34 -0400162 seq = g_sequence_new(NULL);
163 bdrv_iterate_format(add_format_to_seq, seq);
164 g_sequence_foreach(seq, print_format, NULL);
bellardea2384d2004-08-01 21:59:26 +0000165 printf("\n");
Mike Day1a443c12014-05-05 12:53:34 -0400166 g_sequence_free(seq);
167
Fam Zhengac1307a2014-04-22 13:36:11 +0800168 exit(EXIT_SUCCESS);
bellardea2384d2004-08-01 21:59:26 +0000169}
170
Stefan Weil7c30f652013-06-16 17:01:05 +0200171static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...)
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100172{
173 int ret = 0;
174 if (!quiet) {
175 va_list args;
176 va_start(args, fmt);
177 ret = vprintf(fmt, args);
178 va_end(args);
179 }
180 return ret;
181}
182
bellardea2384d2004-08-01 21:59:26 +0000183#if defined(WIN32)
184/* XXX: put correct support for win32 */
185static int read_password(char *buf, int buf_size)
186{
187 int c, i;
188 printf("Password: ");
189 fflush(stdout);
190 i = 0;
191 for(;;) {
192 c = getchar();
193 if (c == '\n')
194 break;
195 if (i < (buf_size - 1))
196 buf[i++] = c;
197 }
198 buf[i] = '\0';
199 return 0;
200}
201
202#else
203
204#include <termios.h>
205
206static struct termios oldtty;
207
208static void term_exit(void)
209{
210 tcsetattr (0, TCSANOW, &oldtty);
211}
212
213static void term_init(void)
214{
215 struct termios tty;
216
217 tcgetattr (0, &tty);
218 oldtty = tty;
219
220 tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP
221 |INLCR|IGNCR|ICRNL|IXON);
222 tty.c_oflag |= OPOST;
223 tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN);
224 tty.c_cflag &= ~(CSIZE|PARENB);
225 tty.c_cflag |= CS8;
226 tty.c_cc[VMIN] = 1;
227 tty.c_cc[VTIME] = 0;
ths3b46e622007-09-17 08:09:54 +0000228
bellardea2384d2004-08-01 21:59:26 +0000229 tcsetattr (0, TCSANOW, &tty);
230
231 atexit(term_exit);
232}
233
pbrook3f379ab2007-11-11 03:33:13 +0000234static int read_password(char *buf, int buf_size)
bellardea2384d2004-08-01 21:59:26 +0000235{
236 uint8_t ch;
237 int i, ret;
238
239 printf("password: ");
240 fflush(stdout);
241 term_init();
242 i = 0;
243 for(;;) {
244 ret = read(0, &ch, 1);
245 if (ret == -1) {
246 if (errno == EAGAIN || errno == EINTR) {
247 continue;
248 } else {
249 ret = -1;
250 break;
251 }
252 } else if (ret == 0) {
253 ret = -1;
254 break;
255 } else {
256 if (ch == '\r') {
257 ret = 0;
258 break;
259 }
260 if (i < (buf_size - 1))
261 buf[i++] = ch;
262 }
263 }
264 term_exit();
265 buf[i] = '\0';
266 printf("\n");
267 return ret;
268}
269#endif
270
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100271static int print_block_option_help(const char *filename, const char *fmt)
272{
273 BlockDriver *drv, *proto_drv;
Chunyan Liu83d05212014-06-05 17:20:51 +0800274 QemuOptsList *create_opts = NULL;
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100275
276 /* Find driver and parse its options */
277 drv = bdrv_find_format(fmt);
278 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100279 error_report("Unknown file format '%s'", fmt);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100280 return 1;
281 }
282
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800283 create_opts = qemu_opts_append(create_opts, drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100284 if (filename) {
285 proto_drv = bdrv_find_protocol(filename, true);
286 if (!proto_drv) {
287 error_report("Unknown protocol '%s'", filename);
Chunyan Liu83d05212014-06-05 17:20:51 +0800288 qemu_opts_free(create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100289 return 1;
290 }
Chunyan Liuc282e1f2014-06-05 17:21:11 +0800291 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfa283cb62014-02-21 16:24:07 +0100292 }
293
Chunyan Liu83d05212014-06-05 17:20:51 +0800294 qemu_opts_print_help(create_opts);
295 qemu_opts_free(create_opts);
Jes Sorensen4ac8aac2010-12-06 15:25:38 +0100296 return 0;
297}
298
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200299static BlockDriverState *bdrv_new_open(const char *id,
300 const char *filename,
Sheng Yang9bc378c2010-01-29 10:15:06 +0800301 const char *fmt,
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100302 int flags,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100303 bool require_io,
304 bool quiet)
bellard75c23802004-08-27 21:28:58 +0000305{
306 BlockDriverState *bs;
307 BlockDriver *drv;
308 char password[256];
Max Reitz34b5d2c2013-09-05 14:45:29 +0200309 Error *local_err = NULL;
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100310 int ret;
bellard75c23802004-08-27 21:28:58 +0000311
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200312 bs = bdrv_new(id, &error_abort);
Kevin Wolfad717132010-12-16 15:37:41 +0100313
bellard75c23802004-08-27 21:28:58 +0000314 if (fmt) {
315 drv = bdrv_find_format(fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900316 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100317 error_report("Unknown file format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900318 goto fail;
319 }
bellard75c23802004-08-27 21:28:58 +0000320 } else {
321 drv = NULL;
322 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100323
Max Reitzddf56362014-02-18 18:33:06 +0100324 ret = bdrv_open(&bs, filename, NULL, NULL, flags, drv, &local_err);
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100325 if (ret < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +0200326 error_report("Could not open '%s': %s", filename,
327 error_get_pretty(local_err));
328 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900329 goto fail;
bellard75c23802004-08-27 21:28:58 +0000330 }
Kevin Wolfb9eaf9e2011-02-09 11:25:53 +0100331
Daniel P. Berrangef0536bb2012-09-10 12:11:31 +0100332 if (bdrv_is_encrypted(bs) && require_io) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100333 qprintf(quiet, "Disk image '%s' is encrypted.\n", filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900334 if (read_password(password, sizeof(password)) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100335 error_report("No password given");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900336 goto fail;
337 }
338 if (bdrv_set_key(bs, password) < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100339 error_report("invalid password");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900340 goto fail;
341 }
bellard75c23802004-08-27 21:28:58 +0000342 }
343 return bs;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900344fail:
Max Reitzf67503e2014-02-18 18:33:05 +0100345 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900346 return NULL;
bellard75c23802004-08-27 21:28:58 +0000347}
348
Chunyan Liu83d05212014-06-05 17:20:51 +0800349static int add_old_style_options(const char *fmt, QemuOpts *opts,
Jes Sorenseneec77d92010-12-07 17:44:34 +0100350 const char *base_filename,
351 const char *base_fmt)
Kevin Wolfefa84d42009-05-18 16:42:12 +0200352{
Kevin Wolfefa84d42009-05-18 16:42:12 +0200353 if (base_filename) {
Chunyan Liu83d05212014-06-05 17:20:51 +0800354 if (qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100355 error_report("Backing file not supported for file format '%s'",
356 fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900357 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200358 }
359 }
360 if (base_fmt) {
Chunyan Liu83d05212014-06-05 17:20:51 +0800361 if (qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt)) {
Jes Sorensen15654a62010-12-16 14:31:53 +0100362 error_report("Backing file format not supported for file "
363 "format '%s'", fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900364 return -1;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200365 }
366 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900367 return 0;
Kevin Wolfefa84d42009-05-18 16:42:12 +0200368}
369
bellardea2384d2004-08-01 21:59:26 +0000370static int img_create(int argc, char **argv)
371{
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200372 int c;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100373 uint64_t img_size = -1;
bellardea2384d2004-08-01 21:59:26 +0000374 const char *fmt = "raw";
aliguori9230eaf2009-03-28 17:55:19 +0000375 const char *base_fmt = NULL;
bellardea2384d2004-08-01 21:59:26 +0000376 const char *filename;
377 const char *base_filename = NULL;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200378 char *options = NULL;
Luiz Capitulino9b375252012-11-30 10:52:05 -0200379 Error *local_err = NULL;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100380 bool quiet = false;
ths3b46e622007-09-17 08:09:54 +0000381
bellardea2384d2004-08-01 21:59:26 +0000382 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100383 c = getopt(argc, argv, "F:b:f:he6o:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100384 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000385 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100386 }
bellardea2384d2004-08-01 21:59:26 +0000387 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100388 case '?':
bellardea2384d2004-08-01 21:59:26 +0000389 case 'h':
390 help();
391 break;
aliguori9230eaf2009-03-28 17:55:19 +0000392 case 'F':
393 base_fmt = optarg;
394 break;
bellardea2384d2004-08-01 21:59:26 +0000395 case 'b':
396 base_filename = optarg;
397 break;
398 case 'f':
399 fmt = optarg;
400 break;
401 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200402 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100403 "encryption\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100404 goto fail;
thsd8871c52007-10-24 16:11:42 +0000405 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +0200406 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +0100407 "compat6\' instead!");
Kevin Wolf77386bf2014-02-21 16:24:04 +0100408 goto fail;
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200409 case 'o':
Kevin Wolf77386bf2014-02-21 16:24:04 +0100410 if (!is_valid_option_list(optarg)) {
411 error_report("Invalid option list: %s", optarg);
412 goto fail;
413 }
414 if (!options) {
415 options = g_strdup(optarg);
416 } else {
417 char *old_options = options;
418 options = g_strdup_printf("%s,%s", options, optarg);
419 g_free(old_options);
420 }
Kevin Wolf9ea2ea72009-05-18 16:42:11 +0200421 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100422 case 'q':
423 quiet = true;
424 break;
bellardea2384d2004-08-01 21:59:26 +0000425 }
426 }
aliguori9230eaf2009-03-28 17:55:19 +0000427
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900428 /* Get the filename */
Kevin Wolfa283cb62014-02-21 16:24:07 +0100429 filename = (optind < argc) ? argv[optind] : NULL;
430 if (options && has_help_option(options)) {
431 g_free(options);
432 return print_block_option_help(filename, fmt);
433 }
434
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100435 if (optind >= argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800436 error_exit("Expecting image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100437 }
Kevin Wolfa283cb62014-02-21 16:24:07 +0100438 optind++;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +0900439
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100440 /* Get image size, if specified */
441 if (optind < argc) {
Jes Sorensen70b4f4b2011-01-05 11:41:02 +0100442 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +0100443 char *end;
444 sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B);
445 if (sval < 0 || *end) {
liguang79443392012-12-17 09:49:23 +0800446 if (sval == -ERANGE) {
447 error_report("Image size must be less than 8 EiB!");
448 } else {
449 error_report("Invalid image size specified! You may use k, M, "
Kevin Wolf5e009842013-06-05 14:19:27 +0200450 "G, T, P or E suffixes for ");
451 error_report("kilobytes, megabytes, gigabytes, terabytes, "
452 "petabytes and exabytes.");
liguang79443392012-12-17 09:49:23 +0800453 }
Kevin Wolf77386bf2014-02-21 16:24:04 +0100454 goto fail;
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100455 }
456 img_size = (uint64_t)sval;
457 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200458 if (optind != argc) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800459 error_exit("Unexpected argument: %s", argv[optind]);
Kevin Wolffc11eb22013-08-05 10:53:04 +0200460 }
Jes Sorensen1da7cfb2010-12-09 14:17:25 +0100461
Luiz Capitulino9b375252012-11-30 10:52:05 -0200462 bdrv_img_create(filename, fmt, base_filename, base_fmt,
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100463 options, img_size, BDRV_O_FLAGS, &local_err, quiet);
Markus Armbruster84d18f02014-01-30 15:07:28 +0100464 if (local_err) {
Max Reitzb70d8c22013-09-06 16:51:03 +0200465 error_report("%s: %s", filename, error_get_pretty(local_err));
Luiz Capitulino9b375252012-11-30 10:52:05 -0200466 error_free(local_err);
Kevin Wolf77386bf2014-02-21 16:24:04 +0100467 goto fail;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900468 }
Luiz Capitulinoa9300912012-11-30 10:52:06 -0200469
Kevin Wolf77386bf2014-02-21 16:24:04 +0100470 g_free(options);
bellardea2384d2004-08-01 21:59:26 +0000471 return 0;
Kevin Wolf77386bf2014-02-21 16:24:04 +0100472
473fail:
474 g_free(options);
475 return 1;
bellardea2384d2004-08-01 21:59:26 +0000476}
477
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100478static void dump_json_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500479{
Markus Armbruster4399c432014-04-25 16:50:32 +0200480 Error *local_err = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500481 QString *str;
482 QmpOutputVisitor *ov = qmp_output_visitor_new();
483 QObject *obj;
484 visit_type_ImageCheck(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +0200485 &check, NULL, &local_err);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500486 obj = qmp_output_get_qobject(ov);
487 str = qobject_to_json_pretty(obj);
488 assert(str != NULL);
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100489 qprintf(quiet, "%s\n", qstring_get_str(str));
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500490 qobject_decref(obj);
491 qmp_output_visitor_cleanup(ov);
492 QDECREF(str);
493}
494
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100495static void dump_human_image_check(ImageCheck *check, bool quiet)
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500496{
497 if (!(check->corruptions || check->leaks || check->check_errors)) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100498 qprintf(quiet, "No errors were found on the image.\n");
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500499 } else {
500 if (check->corruptions) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100501 qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n"
502 "Data may be corrupted, or further writes to the image "
503 "may corrupt it.\n",
504 check->corruptions);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500505 }
506
507 if (check->leaks) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100508 qprintf(quiet,
509 "\n%" PRId64 " leaked clusters were found on the image.\n"
510 "This means waste of disk space, but no harm to data.\n",
511 check->leaks);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500512 }
513
514 if (check->check_errors) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100515 qprintf(quiet,
516 "\n%" PRId64
517 " internal errors have occurred during the check.\n",
518 check->check_errors);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500519 }
520 }
521
522 if (check->total_clusters != 0 && check->allocated_clusters != 0) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100523 qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, "
524 "%0.2f%% fragmented, %0.2f%% compressed clusters\n",
525 check->allocated_clusters, check->total_clusters,
526 check->allocated_clusters * 100.0 / check->total_clusters,
527 check->fragmented_clusters * 100.0 / check->allocated_clusters,
528 check->compressed_clusters * 100.0 /
529 check->allocated_clusters);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500530 }
531
532 if (check->image_end_offset) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100533 qprintf(quiet,
534 "Image end offset: %" PRId64 "\n", check->image_end_offset);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500535 }
536}
537
538static int collect_image_check(BlockDriverState *bs,
539 ImageCheck *check,
540 const char *filename,
541 const char *fmt,
542 int fix)
543{
544 int ret;
545 BdrvCheckResult result;
546
547 ret = bdrv_check(bs, &result, fix);
548 if (ret < 0) {
549 return ret;
550 }
551
552 check->filename = g_strdup(filename);
553 check->format = g_strdup(bdrv_get_format_name(bs));
554 check->check_errors = result.check_errors;
555 check->corruptions = result.corruptions;
556 check->has_corruptions = result.corruptions != 0;
557 check->leaks = result.leaks;
558 check->has_leaks = result.leaks != 0;
559 check->corruptions_fixed = result.corruptions_fixed;
560 check->has_corruptions_fixed = result.corruptions != 0;
561 check->leaks_fixed = result.leaks_fixed;
562 check->has_leaks_fixed = result.leaks != 0;
563 check->image_end_offset = result.image_end_offset;
564 check->has_image_end_offset = result.image_end_offset != 0;
565 check->total_clusters = result.bfi.total_clusters;
566 check->has_total_clusters = result.bfi.total_clusters != 0;
567 check->allocated_clusters = result.bfi.allocated_clusters;
568 check->has_allocated_clusters = result.bfi.allocated_clusters != 0;
569 check->fragmented_clusters = result.bfi.fragmented_clusters;
570 check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0;
Stefan Hajnoczie6439d72013-02-07 17:15:04 +0100571 check->compressed_clusters = result.bfi.compressed_clusters;
572 check->has_compressed_clusters = result.bfi.compressed_clusters != 0;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500573
574 return 0;
575}
576
Kevin Wolfe076f332010-06-29 11:43:13 +0200577/*
578 * Checks an image for consistency. Exit codes:
579 *
Max Reitzd6635c42014-06-02 22:15:21 +0200580 * 0 - Check completed, image is good
581 * 1 - Check not completed because of internal errors
582 * 2 - Check completed, image is corrupted
583 * 3 - Check completed, image has leaked clusters, but is good otherwise
584 * 63 - Checks are not supported by the image format
Kevin Wolfe076f332010-06-29 11:43:13 +0200585 */
aliguori15859692009-04-21 23:11:53 +0000586static int img_check(int argc, char **argv)
587{
588 int c, ret;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500589 OutputFormat output_format = OFORMAT_HUMAN;
590 const char *filename, *fmt, *output;
aliguori15859692009-04-21 23:11:53 +0000591 BlockDriverState *bs;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200592 int fix = 0;
Stefan Hajnoczi058f8f12012-08-09 13:05:56 +0100593 int flags = BDRV_O_FLAGS | BDRV_O_CHECK;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500594 ImageCheck *check;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100595 bool quiet = false;
aliguori15859692009-04-21 23:11:53 +0000596
597 fmt = NULL;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500598 output = NULL;
aliguori15859692009-04-21 23:11:53 +0000599 for(;;) {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500600 int option_index = 0;
601 static const struct option long_options[] = {
602 {"help", no_argument, 0, 'h'},
603 {"format", required_argument, 0, 'f'},
Prasad Joshi4fd6a982014-03-25 00:08:54 +0530604 {"repair", required_argument, 0, 'r'},
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500605 {"output", required_argument, 0, OPTION_OUTPUT},
606 {0, 0, 0, 0}
607 };
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100608 c = getopt_long(argc, argv, "f:hr:q",
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500609 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100610 if (c == -1) {
aliguori15859692009-04-21 23:11:53 +0000611 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100612 }
aliguori15859692009-04-21 23:11:53 +0000613 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100614 case '?':
aliguori15859692009-04-21 23:11:53 +0000615 case 'h':
616 help();
617 break;
618 case 'f':
619 fmt = optarg;
620 break;
Kevin Wolf4534ff52012-05-11 16:07:02 +0200621 case 'r':
622 flags |= BDRV_O_RDWR;
623
624 if (!strcmp(optarg, "leaks")) {
625 fix = BDRV_FIX_LEAKS;
626 } else if (!strcmp(optarg, "all")) {
627 fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS;
628 } else {
Fam Zhengac1307a2014-04-22 13:36:11 +0800629 error_exit("Unknown option value for -r "
630 "(expecting 'leaks' or 'all'): %s", optarg);
Kevin Wolf4534ff52012-05-11 16:07:02 +0200631 }
632 break;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500633 case OPTION_OUTPUT:
634 output = optarg;
635 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100636 case 'q':
637 quiet = true;
638 break;
aliguori15859692009-04-21 23:11:53 +0000639 }
640 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200641 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800642 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100643 }
aliguori15859692009-04-21 23:11:53 +0000644 filename = argv[optind++];
645
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500646 if (output && !strcmp(output, "json")) {
647 output_format = OFORMAT_JSON;
648 } else if (output && !strcmp(output, "human")) {
649 output_format = OFORMAT_HUMAN;
650 } else if (output) {
651 error_report("--output must be used with human or json as argument.");
652 return 1;
653 }
654
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200655 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900656 if (!bs) {
657 return 1;
658 }
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500659
660 check = g_new0(ImageCheck, 1);
661 ret = collect_image_check(bs, check, filename, fmt, fix);
Kevin Wolfe076f332010-06-29 11:43:13 +0200662
663 if (ret == -ENOTSUP) {
Max Reitz55d492d2014-05-31 21:33:30 +0200664 error_report("This image format does not support checks");
Peter Lievenfefddf92013-10-24 08:53:34 +0200665 ret = 63;
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500666 goto fail;
Kevin Wolfe076f332010-06-29 11:43:13 +0200667 }
668
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500669 if (check->corruptions_fixed || check->leaks_fixed) {
670 int corruptions_fixed, leaks_fixed;
671
672 leaks_fixed = check->leaks_fixed;
673 corruptions_fixed = check->corruptions_fixed;
674
675 if (output_format == OFORMAT_HUMAN) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100676 qprintf(quiet,
677 "The following inconsistencies were found and repaired:\n\n"
678 " %" PRId64 " leaked clusters\n"
679 " %" PRId64 " corruptions\n\n"
680 "Double checking the fixed image now...\n",
681 check->leaks_fixed,
682 check->corruptions_fixed);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500683 }
684
685 ret = collect_image_check(bs, check, filename, fmt, 0);
686
687 check->leaks_fixed = leaks_fixed;
688 check->corruptions_fixed = corruptions_fixed;
Kevin Wolfccf34712012-05-11 18:16:54 +0200689 }
690
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500691 switch (output_format) {
692 case OFORMAT_HUMAN:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100693 dump_human_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500694 break;
695 case OFORMAT_JSON:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100696 dump_json_image_check(check, quiet);
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500697 break;
698 }
699
700 if (ret || check->check_errors) {
701 ret = 1;
702 goto fail;
703 }
704
705 if (check->corruptions) {
706 ret = 2;
707 } else if (check->leaks) {
708 ret = 3;
Kevin Wolfe076f332010-06-29 11:43:13 +0200709 } else {
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500710 ret = 0;
aliguori15859692009-04-21 23:11:53 +0000711 }
712
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500713fail:
714 qapi_free_ImageCheck(check);
Fam Zheng4f6fd342013-08-23 09:14:47 +0800715 bdrv_unref(bs);
Kevin Wolfe076f332010-06-29 11:43:13 +0200716
Federico Simoncelli8599ea42013-01-28 06:59:47 -0500717 return ret;
aliguori15859692009-04-21 23:11:53 +0000718}
719
bellardea2384d2004-08-01 21:59:26 +0000720static int img_commit(int argc, char **argv)
721{
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400722 int c, ret, flags;
723 const char *filename, *fmt, *cache;
bellardea2384d2004-08-01 21:59:26 +0000724 BlockDriverState *bs;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100725 bool quiet = false;
bellardea2384d2004-08-01 21:59:26 +0000726
727 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400728 cache = BDRV_DEFAULT_CACHE;
bellardea2384d2004-08-01 21:59:26 +0000729 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100730 c = getopt(argc, argv, "f:ht:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100731 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +0000732 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100733 }
bellardea2384d2004-08-01 21:59:26 +0000734 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +0100735 case '?':
bellardea2384d2004-08-01 21:59:26 +0000736 case 'h':
737 help();
738 break;
739 case 'f':
740 fmt = optarg;
741 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400742 case 't':
743 cache = optarg;
744 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100745 case 'q':
746 quiet = true;
747 break;
bellardea2384d2004-08-01 21:59:26 +0000748 }
749 }
Kevin Wolffc11eb22013-08-05 10:53:04 +0200750 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800751 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +0100752 }
bellardea2384d2004-08-01 21:59:26 +0000753 filename = argv[optind++];
754
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400755 flags = BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +0100756 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -0400757 if (ret < 0) {
758 error_report("Invalid cache option: %s", cache);
759 return -1;
760 }
761
Kevin Wolf9ffe3332014-04-17 16:57:13 +0200762 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900763 if (!bs) {
764 return 1;
765 }
bellardea2384d2004-08-01 21:59:26 +0000766 ret = bdrv_commit(bs);
767 switch(ret) {
768 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +0100769 qprintf(quiet, "Image committed.\n");
bellardea2384d2004-08-01 21:59:26 +0000770 break;
771 case -ENOENT:
Jes Sorensen15654a62010-12-16 14:31:53 +0100772 error_report("No disk inserted");
bellardea2384d2004-08-01 21:59:26 +0000773 break;
774 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +0100775 error_report("Image is read-only");
bellardea2384d2004-08-01 21:59:26 +0000776 break;
777 case -ENOTSUP:
Jes Sorensen15654a62010-12-16 14:31:53 +0100778 error_report("Image is already committed");
bellardea2384d2004-08-01 21:59:26 +0000779 break;
780 default:
Jes Sorensen15654a62010-12-16 14:31:53 +0100781 error_report("Error while committing image");
bellardea2384d2004-08-01 21:59:26 +0000782 break;
783 }
784
Fam Zheng4f6fd342013-08-23 09:14:47 +0800785 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +0900786 if (ret) {
787 return 1;
788 }
bellardea2384d2004-08-01 21:59:26 +0000789 return 0;
790}
791
Dmitry Konishchevf6a00aa2011-05-18 15:03:59 +0400792/*
thsf58c7b32008-06-05 21:53:49 +0000793 * Returns true iff the first sector pointed to by 'buf' contains at least
794 * a non-NUL byte.
795 *
796 * 'pnum' is set to the number of sectors (including and immediately following
797 * the first one) that are known to be in the same allocated/unallocated state.
798 */
bellardea2384d2004-08-01 21:59:26 +0000799static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum)
800{
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000801 bool is_zero;
802 int i;
bellardea2384d2004-08-01 21:59:26 +0000803
804 if (n <= 0) {
805 *pnum = 0;
806 return 0;
807 }
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000808 is_zero = buffer_is_zero(buf, 512);
bellardea2384d2004-08-01 21:59:26 +0000809 for(i = 1; i < n; i++) {
810 buf += 512;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000811 if (is_zero != buffer_is_zero(buf, 512)) {
bellardea2384d2004-08-01 21:59:26 +0000812 break;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000813 }
bellardea2384d2004-08-01 21:59:26 +0000814 }
815 *pnum = i;
Stefan Hajnoczi1a6d39f2012-02-07 13:27:24 +0000816 return !is_zero;
bellardea2384d2004-08-01 21:59:26 +0000817}
818
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100819/*
Kevin Wolfa22f1232011-08-26 15:27:13 +0200820 * Like is_allocated_sectors, but if the buffer starts with a used sector,
821 * up to 'min' consecutive sectors containing zeros are ignored. This avoids
822 * breaking up write requests for only small sparse areas.
823 */
824static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum,
825 int min)
826{
827 int ret;
828 int num_checked, num_used;
829
830 if (n < min) {
831 min = n;
832 }
833
834 ret = is_allocated_sectors(buf, n, pnum);
835 if (!ret) {
836 return ret;
837 }
838
839 num_used = *pnum;
840 buf += BDRV_SECTOR_SIZE * *pnum;
841 n -= *pnum;
842 num_checked = num_used;
843
844 while (n > 0) {
845 ret = is_allocated_sectors(buf, n, pnum);
846
847 buf += BDRV_SECTOR_SIZE * *pnum;
848 n -= *pnum;
849 num_checked += *pnum;
850 if (ret) {
851 num_used = num_checked;
852 } else if (*pnum >= min) {
853 break;
854 }
855 }
856
857 *pnum = num_used;
858 return 1;
859}
860
861/*
Kevin Wolf3e85c6f2010-01-12 12:55:18 +0100862 * Compares two buffers sector by sector. Returns 0 if the first sector of both
863 * buffers matches, non-zero otherwise.
864 *
865 * pnum is set to the number of sectors (including and immediately following
866 * the first one) that are known to have the same comparison result
867 */
868static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n,
869 int *pnum)
870{
871 int res, i;
872
873 if (n <= 0) {
874 *pnum = 0;
875 return 0;
876 }
877
878 res = !!memcmp(buf1, buf2, 512);
879 for(i = 1; i < n; i++) {
880 buf1 += 512;
881 buf2 += 512;
882
883 if (!!memcmp(buf1, buf2, 512) != res) {
884 break;
885 }
886 }
887
888 *pnum = i;
889 return res;
890}
891
Kevin Wolf80ee15a2009-09-15 12:30:43 +0200892#define IO_BUF_SIZE (2 * 1024 * 1024)
bellardea2384d2004-08-01 21:59:26 +0000893
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100894static int64_t sectors_to_bytes(int64_t sectors)
895{
896 return sectors << BDRV_SECTOR_BITS;
897}
898
899static int64_t sectors_to_process(int64_t total, int64_t from)
900{
901 return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS);
902}
903
904/*
905 * Check if passed sectors are empty (not allocated or contain only 0 bytes)
906 *
907 * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero
908 * data and negative value on error.
909 *
910 * @param bs: Driver used for accessing file
911 * @param sect_num: Number of first sector to check
912 * @param sect_count: Number of sectors to check
913 * @param filename: Name of disk file we are checking (logging purpose)
914 * @param buffer: Allocated buffer for storing read data
915 * @param quiet: Flag for quiet mode
916 */
917static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num,
918 int sect_count, const char *filename,
919 uint8_t *buffer, bool quiet)
920{
921 int pnum, ret = 0;
922 ret = bdrv_read(bs, sect_num, buffer, sect_count);
923 if (ret < 0) {
924 error_report("Error while reading offset %" PRId64 " of %s: %s",
925 sectors_to_bytes(sect_num), filename, strerror(-ret));
926 return ret;
927 }
928 ret = is_allocated_sectors(buffer, sect_count, &pnum);
929 if (ret || pnum != sect_count) {
930 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
931 sectors_to_bytes(ret ? sect_num : sect_num + pnum));
932 return 1;
933 }
934
935 return 0;
936}
937
938/*
939 * Compares two images. Exit codes:
940 *
941 * 0 - Images are identical
942 * 1 - Images differ
943 * >1 - Error occurred
944 */
945static int img_compare(int argc, char **argv)
946{
947 const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2;
948 BlockDriverState *bs1, *bs2;
949 int64_t total_sectors1, total_sectors2;
950 uint8_t *buf1 = NULL, *buf2 = NULL;
951 int pnum1, pnum2;
952 int allocated1, allocated2;
953 int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */
954 bool progress = false, quiet = false, strict = false;
955 int64_t total_sectors;
956 int64_t sector_num = 0;
957 int64_t nb_sectors;
958 int c, pnum;
959 uint64_t bs_sectors;
960 uint64_t progress_base;
961
962 for (;;) {
963 c = getopt(argc, argv, "hpf:F:sq");
964 if (c == -1) {
965 break;
966 }
967 switch (c) {
968 case '?':
969 case 'h':
970 help();
971 break;
972 case 'f':
973 fmt1 = optarg;
974 break;
975 case 'F':
976 fmt2 = optarg;
977 break;
978 case 'p':
979 progress = true;
980 break;
981 case 'q':
982 quiet = true;
983 break;
984 case 's':
985 strict = true;
986 break;
987 }
988 }
989
990 /* Progress is not shown in Quiet mode */
991 if (quiet) {
992 progress = false;
993 }
994
995
Kevin Wolffc11eb22013-08-05 10:53:04 +0200996 if (optind != argc - 2) {
Fam Zhengac1307a2014-04-22 13:36:11 +0800997 error_exit("Expecting two image file names");
Miroslav Rezaninad14ed182013-02-13 09:09:41 +0100998 }
999 filename1 = argv[optind++];
1000 filename2 = argv[optind++];
1001
1002 /* Initialize before goto out */
1003 qemu_progress_init(progress, 2.0);
1004
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001005 bs1 = bdrv_new_open("image 1", filename1, fmt1, BDRV_O_FLAGS, true, quiet);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001006 if (!bs1) {
1007 error_report("Can't open file %s", filename1);
1008 ret = 2;
1009 goto out3;
1010 }
1011
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001012 bs2 = bdrv_new_open("image 2", filename2, fmt2, BDRV_O_FLAGS, true, quiet);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001013 if (!bs2) {
1014 error_report("Can't open file %s", filename2);
1015 ret = 2;
1016 goto out2;
1017 }
1018
1019 buf1 = qemu_blockalign(bs1, IO_BUF_SIZE);
1020 buf2 = qemu_blockalign(bs2, IO_BUF_SIZE);
1021 bdrv_get_geometry(bs1, &bs_sectors);
1022 total_sectors1 = bs_sectors;
1023 bdrv_get_geometry(bs2, &bs_sectors);
1024 total_sectors2 = bs_sectors;
1025 total_sectors = MIN(total_sectors1, total_sectors2);
1026 progress_base = MAX(total_sectors1, total_sectors2);
1027
1028 qemu_progress_print(0, 100);
1029
1030 if (strict && total_sectors1 != total_sectors2) {
1031 ret = 1;
1032 qprintf(quiet, "Strict mode: Image size mismatch!\n");
1033 goto out;
1034 }
1035
1036 for (;;) {
1037 nb_sectors = sectors_to_process(total_sectors, sector_num);
1038 if (nb_sectors <= 0) {
1039 break;
1040 }
1041 allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors,
1042 &pnum1);
1043 if (allocated1 < 0) {
1044 ret = 3;
1045 error_report("Sector allocation test failed for %s", filename1);
1046 goto out;
1047 }
1048
1049 allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors,
1050 &pnum2);
1051 if (allocated2 < 0) {
1052 ret = 3;
1053 error_report("Sector allocation test failed for %s", filename2);
1054 goto out;
1055 }
1056 nb_sectors = MIN(pnum1, pnum2);
1057
1058 if (allocated1 == allocated2) {
1059 if (allocated1) {
1060 ret = bdrv_read(bs1, sector_num, buf1, nb_sectors);
1061 if (ret < 0) {
1062 error_report("Error while reading offset %" PRId64 " of %s:"
1063 " %s", sectors_to_bytes(sector_num), filename1,
1064 strerror(-ret));
1065 ret = 4;
1066 goto out;
1067 }
1068 ret = bdrv_read(bs2, sector_num, buf2, nb_sectors);
1069 if (ret < 0) {
1070 error_report("Error while reading offset %" PRId64
1071 " of %s: %s", sectors_to_bytes(sector_num),
1072 filename2, strerror(-ret));
1073 ret = 4;
1074 goto out;
1075 }
1076 ret = compare_sectors(buf1, buf2, nb_sectors, &pnum);
1077 if (ret || pnum != nb_sectors) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001078 qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n",
1079 sectors_to_bytes(
1080 ret ? sector_num : sector_num + pnum));
Fam Zheng36452f12013-11-13 20:26:49 +08001081 ret = 1;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001082 goto out;
1083 }
1084 }
1085 } else {
1086 if (strict) {
1087 ret = 1;
1088 qprintf(quiet, "Strict mode: Offset %" PRId64
1089 " allocation mismatch!\n",
1090 sectors_to_bytes(sector_num));
1091 goto out;
1092 }
1093
1094 if (allocated1) {
1095 ret = check_empty_sectors(bs1, sector_num, nb_sectors,
1096 filename1, buf1, quiet);
1097 } else {
1098 ret = check_empty_sectors(bs2, sector_num, nb_sectors,
1099 filename2, buf1, quiet);
1100 }
1101 if (ret) {
1102 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001103 error_report("Error while reading offset %" PRId64 ": %s",
1104 sectors_to_bytes(sector_num), strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001105 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001106 }
1107 goto out;
1108 }
1109 }
1110 sector_num += nb_sectors;
1111 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1112 }
1113
1114 if (total_sectors1 != total_sectors2) {
1115 BlockDriverState *bs_over;
1116 int64_t total_sectors_over;
1117 const char *filename_over;
1118
1119 qprintf(quiet, "Warning: Image size mismatch!\n");
1120 if (total_sectors1 > total_sectors2) {
1121 total_sectors_over = total_sectors1;
1122 bs_over = bs1;
1123 filename_over = filename1;
1124 } else {
1125 total_sectors_over = total_sectors2;
1126 bs_over = bs2;
1127 filename_over = filename2;
1128 }
1129
1130 for (;;) {
1131 nb_sectors = sectors_to_process(total_sectors_over, sector_num);
1132 if (nb_sectors <= 0) {
1133 break;
1134 }
1135 ret = bdrv_is_allocated_above(bs_over, NULL, sector_num,
1136 nb_sectors, &pnum);
1137 if (ret < 0) {
1138 ret = 3;
1139 error_report("Sector allocation test failed for %s",
1140 filename_over);
1141 goto out;
1142
1143 }
1144 nb_sectors = pnum;
1145 if (ret) {
1146 ret = check_empty_sectors(bs_over, sector_num, nb_sectors,
1147 filename_over, buf1, quiet);
1148 if (ret) {
1149 if (ret < 0) {
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001150 error_report("Error while reading offset %" PRId64
1151 " of %s: %s", sectors_to_bytes(sector_num),
1152 filename_over, strerror(-ret));
Fam Zheng36452f12013-11-13 20:26:49 +08001153 ret = 4;
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001154 }
1155 goto out;
1156 }
1157 }
1158 sector_num += nb_sectors;
1159 qemu_progress_print(((float) nb_sectors / progress_base)*100, 100);
1160 }
1161 }
1162
1163 qprintf(quiet, "Images are identical.\n");
1164 ret = 0;
1165
1166out:
Fam Zheng4f6fd342013-08-23 09:14:47 +08001167 bdrv_unref(bs2);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001168 qemu_vfree(buf1);
1169 qemu_vfree(buf2);
1170out2:
Fam Zheng4f6fd342013-08-23 09:14:47 +08001171 bdrv_unref(bs1);
Miroslav Rezaninad14ed182013-02-13 09:09:41 +01001172out3:
1173 qemu_progress_end();
1174 return ret;
1175}
1176
bellardea2384d2004-08-01 21:59:26 +00001177static int img_convert(int argc, char **argv)
1178{
Peter Lieven24f833c2013-11-27 11:07:07 +01001179 int c, n, n1, bs_n, bs_i, compress, cluster_sectors, skip_create;
Peter Lieven13c28af2013-11-27 11:07:01 +01001180 int64_t ret = 0;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001181 int progress = 0, flags;
1182 const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename;
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001183 BlockDriver *drv, *proto_drv;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001184 BlockDriverState **bs = NULL, *out_bs = NULL;
Peter Lieven802c3d42013-12-05 15:54:53 +01001185 int64_t total_sectors, nb_sectors, sector_num, bs_offset;
ths96b8f132007-12-17 01:35:20 +00001186 uint64_t bs_sectors;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001187 uint8_t * buf = NULL;
Peter Lievenf2521c92013-11-27 11:07:06 +01001188 size_t bufsectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE;
bellardea2384d2004-08-01 21:59:26 +00001189 const uint8_t *buf1;
bellardfaea38e2006-08-05 21:31:00 +00001190 BlockDriverInfo bdi;
Chunyan Liu83d05212014-06-05 17:20:51 +08001191 QemuOpts *opts = NULL;
1192 QemuOptsList *create_opts = NULL;
1193 const char *out_baseimg_param;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001194 char *options = NULL;
edison51ef6722010-09-21 19:58:41 -07001195 const char *snapshot_name = NULL;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001196 int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001197 bool quiet = false;
Max Reitzcc84d902013-09-06 17:14:26 +02001198 Error *local_err = NULL;
Wenchao Xiaef806542013-12-04 17:10:57 +08001199 QemuOpts *sn_opts = NULL;
bellardea2384d2004-08-01 21:59:26 +00001200
1201 fmt = NULL;
1202 out_fmt = "raw";
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001203 cache = "unsafe";
thsf58c7b32008-06-05 21:53:49 +00001204 out_baseimg = NULL;
Jes Sorenseneec77d92010-12-07 17:44:34 +01001205 compress = 0;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001206 skip_create = 0;
bellardea2384d2004-08-01 21:59:26 +00001207 for(;;) {
Wenchao Xiaef806542013-12-04 17:10:57 +08001208 c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qnl:");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001209 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001210 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001211 }
bellardea2384d2004-08-01 21:59:26 +00001212 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001213 case '?':
bellardea2384d2004-08-01 21:59:26 +00001214 case 'h':
1215 help();
1216 break;
1217 case 'f':
1218 fmt = optarg;
1219 break;
1220 case 'O':
1221 out_fmt = optarg;
1222 break;
thsf58c7b32008-06-05 21:53:49 +00001223 case 'B':
1224 out_baseimg = optarg;
1225 break;
bellardea2384d2004-08-01 21:59:26 +00001226 case 'c':
Jes Sorenseneec77d92010-12-07 17:44:34 +01001227 compress = 1;
bellardea2384d2004-08-01 21:59:26 +00001228 break;
1229 case 'e':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001230 error_report("option -e is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001231 "encryption\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001232 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001233 goto fail_getopt;
thsec36ba12007-09-16 21:59:02 +00001234 case '6':
Markus Armbruster9d42e152011-06-22 14:03:55 +02001235 error_report("option -6 is deprecated, please use \'-o "
Jes Sorenseneec77d92010-12-07 17:44:34 +01001236 "compat6\' instead!");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001237 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001238 goto fail_getopt;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001239 case 'o':
Kevin Wolf2dc83282014-02-21 16:24:05 +01001240 if (!is_valid_option_list(optarg)) {
1241 error_report("Invalid option list: %s", optarg);
1242 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001243 goto fail_getopt;
Kevin Wolf2dc83282014-02-21 16:24:05 +01001244 }
1245 if (!options) {
1246 options = g_strdup(optarg);
1247 } else {
1248 char *old_options = options;
1249 options = g_strdup_printf("%s,%s", options, optarg);
1250 g_free(old_options);
1251 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001252 break;
edison51ef6722010-09-21 19:58:41 -07001253 case 's':
1254 snapshot_name = optarg;
1255 break;
Wenchao Xiaef806542013-12-04 17:10:57 +08001256 case 'l':
1257 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
1258 sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
1259 if (!sn_opts) {
1260 error_report("Failed in parsing snapshot param '%s'",
1261 optarg);
Kevin Wolf2dc83282014-02-21 16:24:05 +01001262 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001263 goto fail_getopt;
Wenchao Xiaef806542013-12-04 17:10:57 +08001264 }
1265 } else {
1266 snapshot_name = optarg;
1267 }
1268 break;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001269 case 'S':
1270 {
1271 int64_t sval;
Markus Armbrustere36b3692011-11-22 09:46:05 +01001272 char *end;
1273 sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B);
1274 if (sval < 0 || *end) {
Kevin Wolfa22f1232011-08-26 15:27:13 +02001275 error_report("Invalid minimum zero buffer size for sparse output specified");
Kevin Wolf2dc83282014-02-21 16:24:05 +01001276 ret = -1;
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001277 goto fail_getopt;
Kevin Wolfa22f1232011-08-26 15:27:13 +02001278 }
1279
1280 min_sparse = sval / BDRV_SECTOR_SIZE;
1281 break;
1282 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001283 case 'p':
1284 progress = 1;
1285 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001286 case 't':
1287 cache = optarg;
1288 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001289 case 'q':
1290 quiet = true;
1291 break;
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001292 case 'n':
1293 skip_create = 1;
1294 break;
bellardea2384d2004-08-01 21:59:26 +00001295 }
1296 }
ths3b46e622007-09-17 08:09:54 +00001297
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001298 /* Initialize before goto out */
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001299 if (quiet) {
1300 progress = 0;
1301 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001302 qemu_progress_init(progress, 1.0);
1303
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01001304
balrog926c2d22007-10-31 01:11:44 +00001305 bs_n = argc - optind - 1;
Kevin Wolfa283cb62014-02-21 16:24:07 +01001306 out_filename = bs_n >= 1 ? argv[argc - 1] : NULL;
thsf58c7b32008-06-05 21:53:49 +00001307
Kevin Wolf2dc83282014-02-21 16:24:05 +01001308 if (options && has_help_option(options)) {
Jes Sorensen4ac8aac2010-12-06 15:25:38 +01001309 ret = print_block_option_help(out_filename, out_fmt);
1310 goto out;
1311 }
1312
bellardea2384d2004-08-01 21:59:26 +00001313 if (bs_n < 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001314 error_exit("Must specify image file name");
ths5fafdf22007-09-16 21:08:06 +00001315 }
bellardea2384d2004-08-01 21:59:26 +00001316
thsf58c7b32008-06-05 21:53:49 +00001317
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001318 if (bs_n > 1 && out_baseimg) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001319 error_report("-B makes no sense when concatenating multiple input "
1320 "images");
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001321 ret = -1;
1322 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001323 }
Dong Xu Wangf8111c22012-03-15 20:13:31 +08001324
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001325 qemu_progress_print(0, 100);
1326
Anthony Liguori7267c092011-08-20 22:09:37 -05001327 bs = g_malloc0(bs_n * sizeof(BlockDriverState *));
balrog926c2d22007-10-31 01:11:44 +00001328
1329 total_sectors = 0;
1330 for (bs_i = 0; bs_i < bs_n; bs_i++) {
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001331 char *id = bs_n > 1 ? g_strdup_printf("source %d", bs_i)
1332 : g_strdup("source");
1333 bs[bs_i] = bdrv_new_open(id, argv[optind + bs_i], fmt, BDRV_O_FLAGS,
1334 true, quiet);
1335 g_free(id);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001336 if (!bs[bs_i]) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001337 error_report("Could not open '%s'", argv[optind + bs_i]);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001338 ret = -1;
1339 goto out;
1340 }
balrog926c2d22007-10-31 01:11:44 +00001341 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1342 total_sectors += bs_sectors;
1343 }
bellardea2384d2004-08-01 21:59:26 +00001344
Wenchao Xiaef806542013-12-04 17:10:57 +08001345 if (sn_opts) {
1346 ret = bdrv_snapshot_load_tmp(bs[0],
1347 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1348 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1349 &local_err);
1350 } else if (snapshot_name != NULL) {
edison51ef6722010-09-21 19:58:41 -07001351 if (bs_n > 1) {
Markus Armbruster6daf1942011-06-22 14:03:54 +02001352 error_report("No support for concatenating multiple snapshot");
edison51ef6722010-09-21 19:58:41 -07001353 ret = -1;
1354 goto out;
1355 }
Wenchao Xia7b4c4782013-12-04 17:10:54 +08001356
1357 bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err);
Wenchao Xiaef806542013-12-04 17:10:57 +08001358 }
Markus Armbruster84d18f02014-01-30 15:07:28 +01001359 if (local_err) {
Wenchao Xiaef806542013-12-04 17:10:57 +08001360 error_report("Failed to load snapshot: %s",
1361 error_get_pretty(local_err));
1362 error_free(local_err);
1363 ret = -1;
1364 goto out;
edison51ef6722010-09-21 19:58:41 -07001365 }
1366
Kevin Wolfefa84d42009-05-18 16:42:12 +02001367 /* Find driver and parse its options */
bellardea2384d2004-08-01 21:59:26 +00001368 drv = bdrv_find_format(out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001369 if (!drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001370 error_report("Unknown file format '%s'", out_fmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001371 ret = -1;
1372 goto out;
1373 }
balrog926c2d22007-10-31 01:11:44 +00001374
Kevin Wolf98289622013-07-10 15:47:39 +02001375 proto_drv = bdrv_find_protocol(out_filename, true);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001376 if (!proto_drv) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001377 error_report("Unknown protocol '%s'", out_filename);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001378 ret = -1;
1379 goto out;
1380 }
MORITA Kazutakab50cbab2010-05-26 11:35:36 +09001381
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001382 create_opts = qemu_opts_append(create_opts, drv->create_opts);
1383 create_opts = qemu_opts_append(create_opts, proto_drv->create_opts);
Kevin Wolfdb08adf2009-06-04 15:39:38 +02001384
Chunyan Liu83d05212014-06-05 17:20:51 +08001385 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
1386 if (options && qemu_opts_do_parse(opts, options, NULL)) {
1387 error_report("Invalid options for file format '%s'", out_fmt);
1388 ret = -1;
1389 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001390 }
1391
Chunyan Liu83d05212014-06-05 17:20:51 +08001392 qemu_opt_set_number(opts, BLOCK_OPT_SIZE, total_sectors * 512);
1393 ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001394 if (ret < 0) {
1395 goto out;
1396 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001397
Kevin Wolfa18953f2010-10-14 15:46:04 +02001398 /* Get backing file name if -o backing_file was used */
Chunyan Liu83d05212014-06-05 17:20:51 +08001399 out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE);
Kevin Wolfa18953f2010-10-14 15:46:04 +02001400 if (out_baseimg_param) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001401 out_baseimg = out_baseimg_param;
Kevin Wolfa18953f2010-10-14 15:46:04 +02001402 }
1403
Kevin Wolfefa84d42009-05-18 16:42:12 +02001404 /* Check if compression is supported */
Jes Sorenseneec77d92010-12-07 17:44:34 +01001405 if (compress) {
Chunyan Liu83d05212014-06-05 17:20:51 +08001406 bool encryption =
1407 qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false);
1408 const char *preallocation =
1409 qemu_opt_get(opts, BLOCK_OPT_PREALLOC);
Kevin Wolfefa84d42009-05-18 16:42:12 +02001410
1411 if (!drv->bdrv_write_compressed) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001412 error_report("Compression not supported for this file format");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001413 ret = -1;
1414 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001415 }
1416
Chunyan Liu83d05212014-06-05 17:20:51 +08001417 if (encryption) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001418 error_report("Compression and encryption not supported at "
1419 "the same time");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001420 ret = -1;
1421 goto out;
Kevin Wolfefa84d42009-05-18 16:42:12 +02001422 }
Kevin Wolf41521fa2011-10-18 16:19:42 +02001423
Chunyan Liu83d05212014-06-05 17:20:51 +08001424 if (preallocation
1425 && strcmp(preallocation, "off"))
Kevin Wolf41521fa2011-10-18 16:19:42 +02001426 {
1427 error_report("Compression and preallocation not supported at "
1428 "the same time");
1429 ret = -1;
1430 goto out;
1431 }
Kevin Wolfefa84d42009-05-18 16:42:12 +02001432 }
1433
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001434 if (!skip_create) {
1435 /* Create the new image */
Chunyan Liuc282e1f2014-06-05 17:21:11 +08001436 ret = bdrv_create(drv, out_filename, opts, &local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001437 if (ret < 0) {
Max Reitzcc84d902013-09-06 17:14:26 +02001438 error_report("%s: error while converting %s: %s",
1439 out_filename, out_fmt, error_get_pretty(local_err));
1440 error_free(local_err);
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001441 goto out;
bellardea2384d2004-08-01 21:59:26 +00001442 }
1443 }
ths3b46e622007-09-17 08:09:54 +00001444
Peter Lieven5a37b602013-10-24 12:07:06 +02001445 flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR;
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01001446 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001447 if (ret < 0) {
1448 error_report("Invalid cache option: %s", cache);
Markus Armbrusterbb9cd2e2014-05-28 11:17:07 +02001449 goto out;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04001450 }
1451
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001452 out_bs = bdrv_new_open("target", out_filename, out_fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001453 if (!out_bs) {
1454 ret = -1;
1455 goto out;
1456 }
bellardea2384d2004-08-01 21:59:26 +00001457
balrog926c2d22007-10-31 01:11:44 +00001458 bs_i = 0;
1459 bs_offset = 0;
1460 bdrv_get_geometry(bs[0], &bs_sectors);
Peter Lievenf2521c92013-11-27 11:07:06 +01001461
1462 /* increase bufsectors from the default 4096 (2M) if opt_transfer_length
1463 * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB)
1464 * as maximum. */
1465 bufsectors = MIN(32768,
1466 MAX(bufsectors, MAX(out_bs->bl.opt_transfer_length,
1467 out_bs->bl.discard_alignment))
1468 );
1469
1470 buf = qemu_blockalign(out_bs, bufsectors * BDRV_SECTOR_SIZE);
balrog926c2d22007-10-31 01:11:44 +00001471
Alexandre Derumierb2e10492013-09-02 19:07:24 +01001472 if (skip_create) {
1473 int64_t output_length = bdrv_getlength(out_bs);
1474 if (output_length < 0) {
1475 error_report("unable to get output image length: %s\n",
1476 strerror(-output_length));
1477 ret = -1;
1478 goto out;
1479 } else if (output_length < total_sectors << BDRV_SECTOR_BITS) {
1480 error_report("output file is smaller than input file");
1481 ret = -1;
1482 goto out;
1483 }
1484 }
1485
Peter Lieven24f833c2013-11-27 11:07:07 +01001486 cluster_sectors = 0;
1487 ret = bdrv_get_info(out_bs, &bdi);
1488 if (ret < 0) {
1489 if (compress) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001490 error_report("could not get block driver info");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001491 goto out;
1492 }
Peter Lieven24f833c2013-11-27 11:07:07 +01001493 } else {
Fam Zheng85f49ca2014-05-06 21:08:43 +08001494 compress = compress || bdi.needs_compressed_writes;
Peter Lieven24f833c2013-11-27 11:07:07 +01001495 cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE;
1496 }
1497
1498 if (compress) {
1499 if (cluster_sectors <= 0 || cluster_sectors > bufsectors) {
Jes Sorensen15654a62010-12-16 14:31:53 +01001500 error_report("invalid cluster size");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001501 ret = -1;
1502 goto out;
1503 }
bellardea2384d2004-08-01 21:59:26 +00001504 sector_num = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001505
1506 nb_sectors = total_sectors;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001507
bellardea2384d2004-08-01 21:59:26 +00001508 for(;;) {
balrog926c2d22007-10-31 01:11:44 +00001509 int64_t bs_num;
1510 int remainder;
1511 uint8_t *buf2;
1512
bellardea2384d2004-08-01 21:59:26 +00001513 nb_sectors = total_sectors - sector_num;
1514 if (nb_sectors <= 0)
1515 break;
1516 if (nb_sectors >= cluster_sectors)
1517 n = cluster_sectors;
1518 else
1519 n = nb_sectors;
balrog926c2d22007-10-31 01:11:44 +00001520
1521 bs_num = sector_num - bs_offset;
1522 assert (bs_num >= 0);
1523 remainder = n;
1524 buf2 = buf;
1525 while (remainder > 0) {
1526 int nlow;
1527 while (bs_num == bs_sectors) {
1528 bs_i++;
1529 assert (bs_i < bs_n);
1530 bs_offset += bs_sectors;
1531 bdrv_get_geometry(bs[bs_i], &bs_sectors);
1532 bs_num = 0;
Blue Swirl0bfcd592010-05-22 08:02:12 +00001533 /* printf("changing part: sector_num=%" PRId64 ", "
1534 "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64
1535 "\n", sector_num, bs_i, bs_offset, bs_sectors); */
balrog926c2d22007-10-31 01:11:44 +00001536 }
1537 assert (bs_num < bs_sectors);
1538
1539 nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder;
1540
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001541 ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow);
1542 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001543 error_report("error while reading sector %" PRId64 ": %s",
1544 bs_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001545 goto out;
1546 }
balrog926c2d22007-10-31 01:11:44 +00001547
1548 buf2 += nlow * 512;
1549 bs_num += nlow;
1550
1551 remainder -= nlow;
1552 }
1553 assert (remainder == 0);
1554
Stefan Hajnoczi54f106d2013-04-15 17:17:33 +02001555 if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) {
1556 ret = bdrv_write_compressed(out_bs, sector_num, buf, n);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001557 if (ret != 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001558 error_report("error while compressing sector %" PRId64
1559 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001560 goto out;
1561 }
bellardea2384d2004-08-01 21:59:26 +00001562 }
1563 sector_num += n;
Peter Lieven13c28af2013-11-27 11:07:01 +01001564 qemu_progress_print(100.0 * sector_num / total_sectors, 0);
bellardea2384d2004-08-01 21:59:26 +00001565 }
bellardfaea38e2006-08-05 21:31:00 +00001566 /* signal EOF to align */
1567 bdrv_write_compressed(out_bs, 0, NULL, 0);
bellardea2384d2004-08-01 21:59:26 +00001568 } else {
Peter Lieven802c3d42013-12-05 15:54:53 +01001569 int64_t sectors_to_read, sectors_read, sector_num_next_status;
1570 bool count_allocated_sectors;
Peter Lieven11b66992013-10-24 12:07:05 +02001571 int has_zero_init = min_sparse ? bdrv_has_zero_init(out_bs) : 0;
Kevin Wolff2feebb2010-04-14 17:30:35 +02001572
Peter Lieven5a37b602013-10-24 12:07:06 +02001573 if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) {
1574 ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP);
1575 if (ret < 0) {
1576 goto out;
1577 }
1578 has_zero_init = 1;
1579 }
1580
Peter Lieven802c3d42013-12-05 15:54:53 +01001581 sectors_to_read = total_sectors;
1582 count_allocated_sectors = progress && (out_baseimg || has_zero_init);
1583restart:
thsf58c7b32008-06-05 21:53:49 +00001584 sector_num = 0; // total number of sectors converted so far
Peter Lieven802c3d42013-12-05 15:54:53 +01001585 sectors_read = 0;
1586 sector_num_next_status = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001587
bellardea2384d2004-08-01 21:59:26 +00001588 for(;;) {
1589 nb_sectors = total_sectors - sector_num;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001590 if (nb_sectors <= 0) {
Peter Lieven802c3d42013-12-05 15:54:53 +01001591 if (count_allocated_sectors) {
1592 sectors_to_read = sectors_read;
1593 count_allocated_sectors = false;
1594 goto restart;
1595 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001596 ret = 0;
bellardea2384d2004-08-01 21:59:26 +00001597 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001598 }
balrog926c2d22007-10-31 01:11:44 +00001599
1600 while (sector_num - bs_offset >= bs_sectors) {
1601 bs_i ++;
1602 assert (bs_i < bs_n);
1603 bs_offset += bs_sectors;
1604 bdrv_get_geometry(bs[bs_i], &bs_sectors);
Blue Swirl0bfcd592010-05-22 08:02:12 +00001605 /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, "
1606 "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n",
balrog926c2d22007-10-31 01:11:44 +00001607 sector_num, bs_i, bs_offset, bs_sectors); */
1608 }
1609
Peter Lieven13c28af2013-11-27 11:07:01 +01001610 if ((out_baseimg || has_zero_init) &&
1611 sector_num >= sector_num_next_status) {
1612 n = nb_sectors > INT_MAX ? INT_MAX : nb_sectors;
1613 ret = bdrv_get_block_status(bs[bs_i], sector_num - bs_offset,
1614 n, &n1);
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001615 if (ret < 0) {
Peter Lieven13c28af2013-11-27 11:07:01 +01001616 error_report("error while reading block status of sector %"
1617 PRId64 ": %s", sector_num - bs_offset,
1618 strerror(-ret));
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001619 goto out;
aliguori93c65b42009-04-05 17:40:43 +00001620 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001621 /* If the output image is zero initialized, we are not working
1622 * on a shared base and the input is zero we can skip the next
1623 * n1 sectors */
1624 if (has_zero_init && !out_baseimg && (ret & BDRV_BLOCK_ZERO)) {
Paolo Bonzinie4a86f82013-09-04 19:00:26 +02001625 sector_num += n1;
1626 continue;
1627 }
Peter Lieven13c28af2013-11-27 11:07:01 +01001628 /* If the output image is being created as a copy on write
1629 * image, assume that sectors which are unallocated in the
1630 * input image are present in both the output's and input's
1631 * base images (no need to copy them). */
1632 if (out_baseimg) {
1633 if (!(ret & BDRV_BLOCK_DATA)) {
1634 sector_num += n1;
1635 continue;
1636 }
1637 /* The next 'n1' sectors are allocated in the input image.
1638 * Copy only those as they may be followed by unallocated
1639 * sectors. */
1640 nb_sectors = n1;
1641 }
1642 /* avoid redundant callouts to get_block_status */
1643 sector_num_next_status = sector_num + n1;
thsf58c7b32008-06-05 21:53:49 +00001644 }
1645
Peter Lievenf2521c92013-11-27 11:07:06 +01001646 n = MIN(nb_sectors, bufsectors);
Peter Lieven24f833c2013-11-27 11:07:07 +01001647
1648 /* round down request length to an aligned sector, but
1649 * do not bother doing this on short requests. They happen
1650 * when we found an all-zero area, and the next sector to
1651 * write will not be sector_num + n. */
1652 if (cluster_sectors > 0 && n >= cluster_sectors) {
1653 int64_t next_aligned_sector = (sector_num + n);
1654 next_aligned_sector -= next_aligned_sector % cluster_sectors;
1655 if (sector_num + n > next_aligned_sector) {
1656 n = next_aligned_sector - sector_num;
1657 }
1658 }
1659
Peter Lieven13c28af2013-11-27 11:07:01 +01001660 n = MIN(n, bs_sectors - (sector_num - bs_offset));
Peter Lieven13c28af2013-11-27 11:07:01 +01001661
Peter Lieven802c3d42013-12-05 15:54:53 +01001662 sectors_read += n;
1663 if (count_allocated_sectors) {
1664 sector_num += n;
1665 continue;
1666 }
1667
1668 n1 = n;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001669 ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n);
1670 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001671 error_report("error while reading sector %" PRId64 ": %s",
1672 sector_num - bs_offset, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001673 goto out;
1674 }
bellardea2384d2004-08-01 21:59:26 +00001675 /* NOTE: at the same time we convert, we do not write zero
1676 sectors to have a chance to compress the image. Ideally, we
1677 should add a specific call to have the info to go faster */
1678 buf1 = buf;
1679 while (n > 0) {
Paolo Bonzini11212d82013-09-04 19:00:27 +02001680 if (!has_zero_init ||
Kevin Wolfa22f1232011-08-26 15:27:13 +02001681 is_allocated_sectors_min(buf1, n, &n1, min_sparse)) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001682 ret = bdrv_write(out_bs, sector_num, buf1, n1);
1683 if (ret < 0) {
Stefan Hajnoczi3fba9d82011-08-17 17:41:09 +01001684 error_report("error while writing sector %" PRId64
1685 ": %s", sector_num, strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001686 goto out;
1687 }
bellardea2384d2004-08-01 21:59:26 +00001688 }
1689 sector_num += n1;
1690 n -= n1;
1691 buf1 += n1 * 512;
1692 }
Peter Lieven802c3d42013-12-05 15:54:53 +01001693 qemu_progress_print(100.0 * sectors_read / sectors_to_read, 0);
bellardea2384d2004-08-01 21:59:26 +00001694 }
1695 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001696out:
Peter Lieven13c28af2013-11-27 11:07:01 +01001697 if (!ret) {
1698 qemu_progress_print(100, 0);
1699 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02001700 qemu_progress_end();
Chunyan Liu83d05212014-06-05 17:20:51 +08001701 qemu_opts_del(opts);
1702 qemu_opts_free(create_opts);
Kevin Wolfbb1c0592011-08-08 14:09:12 +02001703 qemu_vfree(buf);
Wenchao Xiaef806542013-12-04 17:10:57 +08001704 if (sn_opts) {
1705 qemu_opts_del(sn_opts);
1706 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001707 if (out_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001708 bdrv_unref(out_bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001709 }
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001710 if (bs) {
1711 for (bs_i = 0; bs_i < bs_n; bs_i++) {
1712 if (bs[bs_i]) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08001713 bdrv_unref(bs[bs_i]);
Jes Sorensen31ca34b2010-12-06 15:25:36 +01001714 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001715 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001716 g_free(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001717 }
Kevin Wolf64bb01a2014-03-03 14:54:07 +01001718fail_getopt:
1719 g_free(options);
1720
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001721 if (ret) {
1722 return 1;
1723 }
bellardea2384d2004-08-01 21:59:26 +00001724 return 0;
1725}
1726
bellard57d1a2b2004-08-03 21:15:11 +00001727
bellardfaea38e2006-08-05 21:31:00 +00001728static void dump_snapshots(BlockDriverState *bs)
1729{
1730 QEMUSnapshotInfo *sn_tab, *sn;
1731 int nb_sns, i;
bellardfaea38e2006-08-05 21:31:00 +00001732
1733 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1734 if (nb_sns <= 0)
1735 return;
1736 printf("Snapshot list:\n");
Wenchao Xia5b917042013-05-25 11:09:45 +08001737 bdrv_snapshot_dump(fprintf, stdout, NULL);
1738 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00001739 for(i = 0; i < nb_sns; i++) {
1740 sn = &sn_tab[i];
Wenchao Xia5b917042013-05-25 11:09:45 +08001741 bdrv_snapshot_dump(fprintf, stdout, sn);
1742 printf("\n");
bellardfaea38e2006-08-05 21:31:00 +00001743 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001744 g_free(sn_tab);
bellardfaea38e2006-08-05 21:31:00 +00001745}
1746
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001747static void dump_json_image_info_list(ImageInfoList *list)
1748{
Markus Armbruster4399c432014-04-25 16:50:32 +02001749 Error *local_err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001750 QString *str;
1751 QmpOutputVisitor *ov = qmp_output_visitor_new();
1752 QObject *obj;
1753 visit_type_ImageInfoList(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +02001754 &list, NULL, &local_err);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001755 obj = qmp_output_get_qobject(ov);
1756 str = qobject_to_json_pretty(obj);
1757 assert(str != NULL);
1758 printf("%s\n", qstring_get_str(str));
1759 qobject_decref(obj);
1760 qmp_output_visitor_cleanup(ov);
1761 QDECREF(str);
1762}
1763
Benoît Canetc054b3f2012-09-05 13:09:02 +02001764static void dump_json_image_info(ImageInfo *info)
1765{
Markus Armbruster4399c432014-04-25 16:50:32 +02001766 Error *local_err = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001767 QString *str;
1768 QmpOutputVisitor *ov = qmp_output_visitor_new();
1769 QObject *obj;
1770 visit_type_ImageInfo(qmp_output_get_visitor(ov),
Markus Armbruster4399c432014-04-25 16:50:32 +02001771 &info, NULL, &local_err);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001772 obj = qmp_output_get_qobject(ov);
1773 str = qobject_to_json_pretty(obj);
1774 assert(str != NULL);
1775 printf("%s\n", qstring_get_str(str));
1776 qobject_decref(obj);
1777 qmp_output_visitor_cleanup(ov);
1778 QDECREF(str);
1779}
1780
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001781static void dump_human_image_info_list(ImageInfoList *list)
1782{
1783 ImageInfoList *elem;
1784 bool delim = false;
1785
1786 for (elem = list; elem; elem = elem->next) {
1787 if (delim) {
1788 printf("\n");
1789 }
1790 delim = true;
1791
Wenchao Xia5b917042013-05-25 11:09:45 +08001792 bdrv_image_info_dump(fprintf, stdout, elem->value);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001793 }
1794}
1795
1796static gboolean str_equal_func(gconstpointer a, gconstpointer b)
1797{
1798 return strcmp(a, b) == 0;
1799}
1800
1801/**
1802 * Open an image file chain and return an ImageInfoList
1803 *
1804 * @filename: topmost image filename
1805 * @fmt: topmost image format (may be NULL to autodetect)
1806 * @chain: true - enumerate entire backing file chain
1807 * false - only topmost image file
1808 *
1809 * Returns a list of ImageInfo objects or NULL if there was an error opening an
1810 * image file. If there was an error a message will have been printed to
1811 * stderr.
1812 */
1813static ImageInfoList *collect_image_info_list(const char *filename,
1814 const char *fmt,
1815 bool chain)
1816{
1817 ImageInfoList *head = NULL;
1818 ImageInfoList **last = &head;
1819 GHashTable *filenames;
Wenchao Xia43526ec2013-06-06 12:27:58 +08001820 Error *err = NULL;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001821
1822 filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL);
1823
1824 while (filename) {
1825 BlockDriverState *bs;
1826 ImageInfo *info;
1827 ImageInfoList *elem;
1828
1829 if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) {
1830 error_report("Backing file '%s' creates an infinite loop.",
1831 filename);
1832 goto err;
1833 }
1834 g_hash_table_insert(filenames, (gpointer)filename, NULL);
1835
Kevin Wolf9ffe3332014-04-17 16:57:13 +02001836 bs = bdrv_new_open("image", filename, fmt,
1837 BDRV_O_FLAGS | BDRV_O_NO_BACKING, false, false);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001838 if (!bs) {
1839 goto err;
1840 }
1841
Wenchao Xia43526ec2013-06-06 12:27:58 +08001842 bdrv_query_image_info(bs, &info, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01001843 if (err) {
Wenchao Xia43526ec2013-06-06 12:27:58 +08001844 error_report("%s", error_get_pretty(err));
1845 error_free(err);
Prasad Joshibdf866f2014-03-26 01:55:53 +05301846 bdrv_unref(bs);
Wenchao Xia43526ec2013-06-06 12:27:58 +08001847 goto err;
Wenchao Xiafb0ed452013-06-06 12:27:57 +08001848 }
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001849
1850 elem = g_new0(ImageInfoList, 1);
1851 elem->value = info;
1852 *last = elem;
1853 last = &elem->next;
1854
Fam Zheng4f6fd342013-08-23 09:14:47 +08001855 bdrv_unref(bs);
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001856
1857 filename = fmt = NULL;
1858 if (chain) {
1859 if (info->has_full_backing_filename) {
1860 filename = info->full_backing_filename;
1861 } else if (info->has_backing_filename) {
1862 filename = info->backing_filename;
1863 }
1864 if (info->has_backing_filename_format) {
1865 fmt = info->backing_filename_format;
1866 }
1867 }
1868 }
1869 g_hash_table_destroy(filenames);
1870 return head;
1871
1872err:
1873 qapi_free_ImageInfoList(head);
1874 g_hash_table_destroy(filenames);
1875 return NULL;
1876}
1877
Benoît Canetc054b3f2012-09-05 13:09:02 +02001878static int img_info(int argc, char **argv)
1879{
1880 int c;
1881 OutputFormat output_format = OFORMAT_HUMAN;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001882 bool chain = false;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001883 const char *filename, *fmt, *output;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001884 ImageInfoList *list;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001885
bellardea2384d2004-08-01 21:59:26 +00001886 fmt = NULL;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001887 output = NULL;
bellardea2384d2004-08-01 21:59:26 +00001888 for(;;) {
Benoît Canetc054b3f2012-09-05 13:09:02 +02001889 int option_index = 0;
1890 static const struct option long_options[] = {
1891 {"help", no_argument, 0, 'h'},
1892 {"format", required_argument, 0, 'f'},
1893 {"output", required_argument, 0, OPTION_OUTPUT},
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001894 {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN},
Benoît Canetc054b3f2012-09-05 13:09:02 +02001895 {0, 0, 0, 0}
1896 };
1897 c = getopt_long(argc, argv, "f:h",
1898 long_options, &option_index);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001899 if (c == -1) {
bellardea2384d2004-08-01 21:59:26 +00001900 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001901 }
bellardea2384d2004-08-01 21:59:26 +00001902 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01001903 case '?':
bellardea2384d2004-08-01 21:59:26 +00001904 case 'h':
1905 help();
1906 break;
1907 case 'f':
1908 fmt = optarg;
1909 break;
Benoît Canetc054b3f2012-09-05 13:09:02 +02001910 case OPTION_OUTPUT:
1911 output = optarg;
1912 break;
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001913 case OPTION_BACKING_CHAIN:
1914 chain = true;
1915 break;
bellardea2384d2004-08-01 21:59:26 +00001916 }
1917 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02001918 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08001919 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001920 }
bellardea2384d2004-08-01 21:59:26 +00001921 filename = argv[optind++];
1922
Benoît Canetc054b3f2012-09-05 13:09:02 +02001923 if (output && !strcmp(output, "json")) {
1924 output_format = OFORMAT_JSON;
1925 } else if (output && !strcmp(output, "human")) {
1926 output_format = OFORMAT_HUMAN;
1927 } else if (output) {
1928 error_report("--output must be used with human or json as argument.");
1929 return 1;
1930 }
1931
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001932 list = collect_image_info_list(filename, fmt, chain);
1933 if (!list) {
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09001934 return 1;
1935 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001936
Benoît Canetc054b3f2012-09-05 13:09:02 +02001937 switch (output_format) {
1938 case OFORMAT_HUMAN:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001939 dump_human_image_info_list(list);
Benoît Canetc054b3f2012-09-05 13:09:02 +02001940 break;
1941 case OFORMAT_JSON:
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001942 if (chain) {
1943 dump_json_image_info_list(list);
1944 } else {
1945 dump_json_image_info(list->value);
1946 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001947 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01001948 }
Benoît Canetc054b3f2012-09-05 13:09:02 +02001949
Stefan Hajnoczi9699bf02012-10-17 14:02:31 +02001950 qapi_free_ImageInfoList(list);
bellardea2384d2004-08-01 21:59:26 +00001951 return 0;
1952}
1953
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02001954
1955typedef struct MapEntry {
1956 int flags;
1957 int depth;
1958 int64_t start;
1959 int64_t length;
1960 int64_t offset;
1961 BlockDriverState *bs;
1962} MapEntry;
1963
1964static void dump_map_entry(OutputFormat output_format, MapEntry *e,
1965 MapEntry *next)
1966{
1967 switch (output_format) {
1968 case OFORMAT_HUMAN:
1969 if ((e->flags & BDRV_BLOCK_DATA) &&
1970 !(e->flags & BDRV_BLOCK_OFFSET_VALID)) {
1971 error_report("File contains external, encrypted or compressed clusters.");
1972 exit(1);
1973 }
1974 if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) {
1975 printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n",
1976 e->start, e->length, e->offset, e->bs->filename);
1977 }
1978 /* This format ignores the distinction between 0, ZERO and ZERO|DATA.
1979 * Modify the flags here to allow more coalescing.
1980 */
1981 if (next &&
1982 (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) {
1983 next->flags &= ~BDRV_BLOCK_DATA;
1984 next->flags |= BDRV_BLOCK_ZERO;
1985 }
1986 break;
1987 case OFORMAT_JSON:
1988 printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d,"
1989 " \"zero\": %s, \"data\": %s",
1990 (e->start == 0 ? "[" : ",\n"),
1991 e->start, e->length, e->depth,
1992 (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false",
1993 (e->flags & BDRV_BLOCK_DATA) ? "true" : "false");
1994 if (e->flags & BDRV_BLOCK_OFFSET_VALID) {
Paolo Bonzinic745bfb2013-09-11 18:47:52 +02001995 printf(", \"offset\": %"PRId64"", e->offset);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02001996 }
1997 putchar('}');
1998
1999 if (!next) {
2000 printf("]\n");
2001 }
2002 break;
2003 }
2004}
2005
2006static int get_block_status(BlockDriverState *bs, int64_t sector_num,
2007 int nb_sectors, MapEntry *e)
2008{
2009 int64_t ret;
2010 int depth;
2011
2012 /* As an optimization, we could cache the current range of unallocated
2013 * clusters in each file of the chain, and avoid querying the same
2014 * range repeatedly.
2015 */
2016
2017 depth = 0;
2018 for (;;) {
2019 ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors);
2020 if (ret < 0) {
2021 return ret;
2022 }
2023 assert(nb_sectors);
2024 if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) {
2025 break;
2026 }
2027 bs = bs->backing_hd;
2028 if (bs == NULL) {
2029 ret = 0;
2030 break;
2031 }
2032
2033 depth++;
2034 }
2035
2036 e->start = sector_num * BDRV_SECTOR_SIZE;
2037 e->length = nb_sectors * BDRV_SECTOR_SIZE;
2038 e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK;
2039 e->offset = ret & BDRV_BLOCK_OFFSET_MASK;
2040 e->depth = depth;
2041 e->bs = bs;
2042 return 0;
2043}
2044
2045static int img_map(int argc, char **argv)
2046{
2047 int c;
2048 OutputFormat output_format = OFORMAT_HUMAN;
2049 BlockDriverState *bs;
2050 const char *filename, *fmt, *output;
2051 int64_t length;
2052 MapEntry curr = { .length = 0 }, next;
2053 int ret = 0;
2054
2055 fmt = NULL;
2056 output = NULL;
2057 for (;;) {
2058 int option_index = 0;
2059 static const struct option long_options[] = {
2060 {"help", no_argument, 0, 'h'},
2061 {"format", required_argument, 0, 'f'},
2062 {"output", required_argument, 0, OPTION_OUTPUT},
2063 {0, 0, 0, 0}
2064 };
2065 c = getopt_long(argc, argv, "f:h",
2066 long_options, &option_index);
2067 if (c == -1) {
2068 break;
2069 }
2070 switch (c) {
2071 case '?':
2072 case 'h':
2073 help();
2074 break;
2075 case 'f':
2076 fmt = optarg;
2077 break;
2078 case OPTION_OUTPUT:
2079 output = optarg;
2080 break;
2081 }
2082 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002083 if (optind != argc - 1) {
2084 error_exit("Expecting one image file name");
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002085 }
Fam Zhengac1307a2014-04-22 13:36:11 +08002086 filename = argv[optind];
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002087
2088 if (output && !strcmp(output, "json")) {
2089 output_format = OFORMAT_JSON;
2090 } else if (output && !strcmp(output, "human")) {
2091 output_format = OFORMAT_HUMAN;
2092 } else if (output) {
2093 error_report("--output must be used with human or json as argument.");
2094 return 1;
2095 }
2096
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002097 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS, true, false);
Paolo Bonzini4c93a13b2013-09-04 19:00:33 +02002098 if (!bs) {
2099 return 1;
2100 }
2101
2102 if (output_format == OFORMAT_HUMAN) {
2103 printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File");
2104 }
2105
2106 length = bdrv_getlength(bs);
2107 while (curr.start + curr.length < length) {
2108 int64_t nsectors_left;
2109 int64_t sector_num;
2110 int n;
2111
2112 sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS;
2113
2114 /* Probe up to 1 GiB at a time. */
2115 nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num;
2116 n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left);
2117 ret = get_block_status(bs, sector_num, n, &next);
2118
2119 if (ret < 0) {
2120 error_report("Could not read file metadata: %s", strerror(-ret));
2121 goto out;
2122 }
2123
2124 if (curr.length != 0 && curr.flags == next.flags &&
2125 curr.depth == next.depth &&
2126 ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 ||
2127 curr.offset + curr.length == next.offset)) {
2128 curr.length += next.length;
2129 continue;
2130 }
2131
2132 if (curr.length > 0) {
2133 dump_map_entry(output_format, &curr, &next);
2134 }
2135 curr = next;
2136 }
2137
2138 dump_map_entry(output_format, &curr, NULL);
2139
2140out:
2141 bdrv_unref(bs);
2142 return ret < 0;
2143}
2144
aliguorif7b4a942009-01-07 17:40:15 +00002145#define SNAPSHOT_LIST 1
2146#define SNAPSHOT_CREATE 2
2147#define SNAPSHOT_APPLY 3
2148#define SNAPSHOT_DELETE 4
2149
Stuart Brady153859b2009-06-07 00:42:17 +01002150static int img_snapshot(int argc, char **argv)
aliguorif7b4a942009-01-07 17:40:15 +00002151{
2152 BlockDriverState *bs;
2153 QEMUSnapshotInfo sn;
2154 char *filename, *snapshot_name = NULL;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002155 int c, ret = 0, bdrv_oflags;
aliguorif7b4a942009-01-07 17:40:15 +00002156 int action = 0;
2157 qemu_timeval tv;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002158 bool quiet = false;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002159 Error *err = NULL;
aliguorif7b4a942009-01-07 17:40:15 +00002160
Kevin Wolf710da702011-01-10 12:33:02 +01002161 bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR;
aliguorif7b4a942009-01-07 17:40:15 +00002162 /* Parse commandline parameters */
2163 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002164 c = getopt(argc, argv, "la:c:d:hq");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002165 if (c == -1) {
aliguorif7b4a942009-01-07 17:40:15 +00002166 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002167 }
aliguorif7b4a942009-01-07 17:40:15 +00002168 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002169 case '?':
aliguorif7b4a942009-01-07 17:40:15 +00002170 case 'h':
2171 help();
Stuart Brady153859b2009-06-07 00:42:17 +01002172 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002173 case 'l':
2174 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002175 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002176 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002177 }
2178 action = SNAPSHOT_LIST;
Naphtali Spreif5edb012010-01-17 16:48:13 +02002179 bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */
aliguorif7b4a942009-01-07 17:40:15 +00002180 break;
2181 case 'a':
2182 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002183 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002184 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002185 }
2186 action = SNAPSHOT_APPLY;
2187 snapshot_name = optarg;
2188 break;
2189 case 'c':
2190 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002191 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002192 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002193 }
2194 action = SNAPSHOT_CREATE;
2195 snapshot_name = optarg;
2196 break;
2197 case 'd':
2198 if (action) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002199 error_exit("Cannot mix '-l', '-a', '-c', '-d'");
Stuart Brady153859b2009-06-07 00:42:17 +01002200 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002201 }
2202 action = SNAPSHOT_DELETE;
2203 snapshot_name = optarg;
2204 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002205 case 'q':
2206 quiet = true;
2207 break;
aliguorif7b4a942009-01-07 17:40:15 +00002208 }
2209 }
2210
Kevin Wolffc11eb22013-08-05 10:53:04 +02002211 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002212 error_exit("Expecting one image file name");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002213 }
aliguorif7b4a942009-01-07 17:40:15 +00002214 filename = argv[optind++];
2215
2216 /* Open the image */
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002217 bs = bdrv_new_open("image", filename, NULL, bdrv_oflags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002218 if (!bs) {
2219 return 1;
2220 }
aliguorif7b4a942009-01-07 17:40:15 +00002221
2222 /* Perform the requested action */
2223 switch(action) {
2224 case SNAPSHOT_LIST:
2225 dump_snapshots(bs);
2226 break;
2227
2228 case SNAPSHOT_CREATE:
2229 memset(&sn, 0, sizeof(sn));
2230 pstrcpy(sn.name, sizeof(sn.name), snapshot_name);
2231
2232 qemu_gettimeofday(&tv);
2233 sn.date_sec = tv.tv_sec;
2234 sn.date_nsec = tv.tv_usec * 1000;
2235
2236 ret = bdrv_snapshot_create(bs, &sn);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002237 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002238 error_report("Could not create snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002239 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002240 }
aliguorif7b4a942009-01-07 17:40:15 +00002241 break;
2242
2243 case SNAPSHOT_APPLY:
2244 ret = bdrv_snapshot_goto(bs, snapshot_name);
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002245 if (ret) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002246 error_report("Could not apply snapshot '%s': %d (%s)",
aliguorif7b4a942009-01-07 17:40:15 +00002247 snapshot_name, ret, strerror(-ret));
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002248 }
aliguorif7b4a942009-01-07 17:40:15 +00002249 break;
2250
2251 case SNAPSHOT_DELETE:
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002252 bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err);
Markus Armbruster84d18f02014-01-30 15:07:28 +01002253 if (err) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002254 error_report("Could not delete snapshot '%s': (%s)",
2255 snapshot_name, error_get_pretty(err));
2256 error_free(err);
2257 ret = 1;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002258 }
aliguorif7b4a942009-01-07 17:40:15 +00002259 break;
2260 }
2261
2262 /* Cleanup */
Fam Zheng4f6fd342013-08-23 09:14:47 +08002263 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002264 if (ret) {
2265 return 1;
2266 }
Stuart Brady153859b2009-06-07 00:42:17 +01002267 return 0;
aliguorif7b4a942009-01-07 17:40:15 +00002268}
2269
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002270static int img_rebase(int argc, char **argv)
2271{
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002272 BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL;
Stefan Hajnoczif163d072010-04-13 10:29:34 +01002273 BlockDriver *old_backing_drv, *new_backing_drv;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002274 char *filename;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002275 const char *fmt, *cache, *out_basefmt, *out_baseimg;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002276 int c, flags, ret;
2277 int unsafe = 0;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002278 int progress = 0;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002279 bool quiet = false;
Max Reitz34b5d2c2013-09-05 14:45:29 +02002280 Error *local_err = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002281
2282 /* Parse commandline parameters */
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002283 fmt = NULL;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002284 cache = BDRV_DEFAULT_CACHE;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002285 out_baseimg = NULL;
2286 out_basefmt = NULL;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002287 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002288 c = getopt(argc, argv, "uhf:F:b:pt:q");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002289 if (c == -1) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002290 break;
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002291 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002292 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002293 case '?':
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002294 case 'h':
2295 help();
2296 return 0;
Kevin Wolfe53dbee2010-03-02 12:14:31 +01002297 case 'f':
2298 fmt = optarg;
2299 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002300 case 'F':
2301 out_basefmt = optarg;
2302 break;
2303 case 'b':
2304 out_baseimg = optarg;
2305 break;
2306 case 'u':
2307 unsafe = 1;
2308 break;
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002309 case 'p':
2310 progress = 1;
2311 break;
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002312 case 't':
2313 cache = optarg;
2314 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002315 case 'q':
2316 quiet = true;
2317 break;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002318 }
2319 }
2320
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002321 if (quiet) {
2322 progress = 0;
2323 }
2324
Fam Zhengac1307a2014-04-22 13:36:11 +08002325 if (optind != argc - 1) {
2326 error_exit("Expecting one image file name");
2327 }
2328 if (!unsafe && !out_baseimg) {
2329 error_exit("Must specify backing file (-b) or use unsafe mode (-u)");
Jes Sorensenb8fb60d2010-12-06 15:25:39 +01002330 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002331 filename = argv[optind++];
2332
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002333 qemu_progress_init(progress, 2.0);
2334 qemu_progress_print(0, 100);
2335
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002336 flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0);
Stefan Hajnoczic3993cd2011-08-04 12:26:51 +01002337 ret = bdrv_parse_cache_flags(cache, &flags);
Federico Simoncelli661a0f72011-06-20 12:48:19 -04002338 if (ret < 0) {
2339 error_report("Invalid cache option: %s", cache);
2340 return -1;
2341 }
2342
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002343 /*
2344 * Open the images.
2345 *
2346 * Ignore the old backing file for unsafe rebase in case we want to correct
2347 * the reference to a renamed or moved backing file.
2348 */
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002349 bs = bdrv_new_open("image", filename, fmt, flags, true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002350 if (!bs) {
2351 return 1;
2352 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002353
2354 /* Find the right drivers for the backing files */
2355 old_backing_drv = NULL;
2356 new_backing_drv = NULL;
2357
2358 if (!unsafe && bs->backing_format[0] != '\0') {
2359 old_backing_drv = bdrv_find_format(bs->backing_format);
2360 if (old_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002361 error_report("Invalid format name: '%s'", bs->backing_format);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002362 ret = -1;
2363 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002364 }
2365 }
2366
2367 if (out_basefmt != NULL) {
2368 new_backing_drv = bdrv_find_format(out_basefmt);
2369 if (new_backing_drv == NULL) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002370 error_report("Invalid format name: '%s'", out_basefmt);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002371 ret = -1;
2372 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002373 }
2374 }
2375
2376 /* For safe rebasing we need to compare old and new backing file */
2377 if (unsafe) {
2378 /* Make the compiler happy */
2379 bs_old_backing = NULL;
2380 bs_new_backing = NULL;
2381 } else {
2382 char backing_name[1024];
2383
Kevin Wolf98522f62014-04-17 13:16:01 +02002384 bs_old_backing = bdrv_new("old_backing", &error_abort);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002385 bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name));
Max Reitzddf56362014-02-18 18:33:06 +01002386 ret = bdrv_open(&bs_old_backing, backing_name, NULL, NULL, BDRV_O_FLAGS,
Max Reitz34b5d2c2013-09-05 14:45:29 +02002387 old_backing_drv, &local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002388 if (ret) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02002389 error_report("Could not open old backing file '%s': %s",
2390 backing_name, error_get_pretty(local_err));
2391 error_free(local_err);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002392 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002393 }
Alex Bligha6166732012-10-16 13:46:18 +01002394 if (out_baseimg[0]) {
Kevin Wolf98522f62014-04-17 13:16:01 +02002395 bs_new_backing = bdrv_new("new_backing", &error_abort);
Max Reitzddf56362014-02-18 18:33:06 +01002396 ret = bdrv_open(&bs_new_backing, out_baseimg, NULL, NULL,
2397 BDRV_O_FLAGS, new_backing_drv, &local_err);
Alex Bligha6166732012-10-16 13:46:18 +01002398 if (ret) {
Max Reitz34b5d2c2013-09-05 14:45:29 +02002399 error_report("Could not open new backing file '%s': %s",
2400 out_baseimg, error_get_pretty(local_err));
2401 error_free(local_err);
Alex Bligha6166732012-10-16 13:46:18 +01002402 goto out;
2403 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002404 }
2405 }
2406
2407 /*
2408 * Check each unallocated cluster in the COW file. If it is unallocated,
2409 * accesses go to the backing file. We must therefore compare this cluster
2410 * in the old and new backing file, and if they differ we need to copy it
2411 * from the old backing file into the COW file.
2412 *
2413 * If qemu-img crashes during this step, no harm is done. The content of
2414 * the image is the same as the original one at any time.
2415 */
2416 if (!unsafe) {
2417 uint64_t num_sectors;
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002418 uint64_t old_backing_num_sectors;
Alex Bligha6166732012-10-16 13:46:18 +01002419 uint64_t new_backing_num_sectors = 0;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002420 uint64_t sector;
Kevin Wolfcc60e322010-04-29 14:47:48 +02002421 int n;
TeLeMand6771bf2010-02-08 16:20:00 +08002422 uint8_t * buf_old;
2423 uint8_t * buf_new;
Kevin Wolf1f710492012-10-12 14:29:18 +02002424 float local_progress = 0;
TeLeMand6771bf2010-02-08 16:20:00 +08002425
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002426 buf_old = qemu_blockalign(bs, IO_BUF_SIZE);
2427 buf_new = qemu_blockalign(bs, IO_BUF_SIZE);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002428
2429 bdrv_get_geometry(bs, &num_sectors);
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002430 bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors);
Alex Bligha6166732012-10-16 13:46:18 +01002431 if (bs_new_backing) {
2432 bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors);
2433 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002434
Kevin Wolf1f710492012-10-12 14:29:18 +02002435 if (num_sectors != 0) {
2436 local_progress = (float)100 /
2437 (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512));
2438 }
2439
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002440 for (sector = 0; sector < num_sectors; sector += n) {
2441
2442 /* How many sectors can we handle with the next read? */
2443 if (sector + (IO_BUF_SIZE / 512) <= num_sectors) {
2444 n = (IO_BUF_SIZE / 512);
2445 } else {
2446 n = num_sectors - sector;
2447 }
2448
2449 /* If the cluster is allocated, we don't need to take action */
Kevin Wolfcc60e322010-04-29 14:47:48 +02002450 ret = bdrv_is_allocated(bs, sector, n, &n);
Paolo Bonzinid6636402013-09-04 19:00:25 +02002451 if (ret < 0) {
2452 error_report("error while reading image metadata: %s",
2453 strerror(-ret));
2454 goto out;
2455 }
Kevin Wolfcc60e322010-04-29 14:47:48 +02002456 if (ret) {
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002457 continue;
2458 }
2459
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002460 /*
2461 * Read old and new backing file and take into consideration that
2462 * backing files may be smaller than the COW image.
2463 */
2464 if (sector >= old_backing_num_sectors) {
2465 memset(buf_old, 0, n * BDRV_SECTOR_SIZE);
2466 } else {
2467 if (sector + n > old_backing_num_sectors) {
2468 n = old_backing_num_sectors - sector;
2469 }
2470
2471 ret = bdrv_read(bs_old_backing, sector, buf_old, n);
2472 if (ret < 0) {
2473 error_report("error while reading from old backing file");
2474 goto out;
2475 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002476 }
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002477
Alex Bligha6166732012-10-16 13:46:18 +01002478 if (sector >= new_backing_num_sectors || !bs_new_backing) {
Kevin Wolf87a1b3e2011-12-07 12:42:10 +01002479 memset(buf_new, 0, n * BDRV_SECTOR_SIZE);
2480 } else {
2481 if (sector + n > new_backing_num_sectors) {
2482 n = new_backing_num_sectors - sector;
2483 }
2484
2485 ret = bdrv_read(bs_new_backing, sector, buf_new, n);
2486 if (ret < 0) {
2487 error_report("error while reading from new backing file");
2488 goto out;
2489 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002490 }
2491
2492 /* If they differ, we need to write to the COW file */
2493 uint64_t written = 0;
2494
2495 while (written < n) {
2496 int pnum;
2497
2498 if (compare_sectors(buf_old + written * 512,
Kevin Wolf60b1bd42010-02-17 12:32:59 +01002499 buf_new + written * 512, n - written, &pnum))
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002500 {
2501 ret = bdrv_write(bs, sector + written,
2502 buf_old + written * 512, pnum);
2503 if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002504 error_report("Error while writing to COW image: %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002505 strerror(-ret));
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002506 goto out;
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002507 }
2508 }
2509
2510 written += pnum;
2511 }
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002512 qemu_progress_print(local_progress, 100);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002513 }
TeLeMand6771bf2010-02-08 16:20:00 +08002514
Kevin Wolfbb1c0592011-08-08 14:09:12 +02002515 qemu_vfree(buf_old);
2516 qemu_vfree(buf_new);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002517 }
2518
2519 /*
2520 * Change the backing file. All clusters that are different from the old
2521 * backing file are overwritten in the COW file now, so the visible content
2522 * doesn't change when we switch the backing file.
2523 */
Alex Bligha6166732012-10-16 13:46:18 +01002524 if (out_baseimg && *out_baseimg) {
2525 ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt);
2526 } else {
2527 ret = bdrv_change_backing_file(bs, NULL, NULL);
2528 }
2529
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002530 if (ret == -ENOSPC) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002531 error_report("Could not change the backing file to '%s': No "
2532 "space left in the file header", out_baseimg);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002533 } else if (ret < 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002534 error_report("Could not change the backing file to '%s': %s",
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002535 out_baseimg, strerror(-ret));
2536 }
2537
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002538 qemu_progress_print(100, 0);
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002539 /*
2540 * TODO At this point it is possible to check if any clusters that are
2541 * allocated in the COW file are the same in the backing file. If so, they
2542 * could be dropped from the COW file. Don't do this before switching the
2543 * backing file, in case of a crash this would lead to corruption.
2544 */
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002545out:
Jes Sorensen6b837bc2011-03-30 14:16:25 +02002546 qemu_progress_end();
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002547 /* Cleanup */
2548 if (!unsafe) {
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002549 if (bs_old_backing != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002550 bdrv_unref(bs_old_backing);
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002551 }
2552 if (bs_new_backing != NULL) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002553 bdrv_unref(bs_new_backing);
Kevin Wolfeb863ad2011-03-31 12:39:51 +02002554 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002555 }
2556
Fam Zheng4f6fd342013-08-23 09:14:47 +08002557 bdrv_unref(bs);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002558 if (ret) {
2559 return 1;
2560 }
Kevin Wolf3e85c6f2010-01-12 12:55:18 +01002561 return 0;
2562}
2563
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002564static int img_resize(int argc, char **argv)
2565{
2566 int c, ret, relative;
2567 const char *filename, *fmt, *size;
2568 int64_t n, total_size;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002569 bool quiet = false;
Jes Sorensen2a819982010-12-06 17:08:31 +01002570 BlockDriverState *bs = NULL;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002571 QemuOpts *param;
2572 static QemuOptsList resize_options = {
2573 .name = "resize_options",
2574 .head = QTAILQ_HEAD_INITIALIZER(resize_options.head),
2575 .desc = {
2576 {
2577 .name = BLOCK_OPT_SIZE,
2578 .type = QEMU_OPT_SIZE,
2579 .help = "Virtual disk size"
2580 }, {
2581 /* end of list */
2582 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002583 },
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002584 };
2585
Kevin Wolfe80fec72011-04-29 10:58:12 +02002586 /* Remove size from argv manually so that negative numbers are not treated
2587 * as options by getopt. */
2588 if (argc < 3) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002589 error_exit("Not enough arguments");
Kevin Wolfe80fec72011-04-29 10:58:12 +02002590 return 1;
2591 }
2592
2593 size = argv[--argc];
2594
2595 /* Parse getopt arguments */
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002596 fmt = NULL;
2597 for(;;) {
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002598 c = getopt(argc, argv, "f:hq");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002599 if (c == -1) {
2600 break;
2601 }
2602 switch(c) {
Jes Sorensenef873942010-12-06 15:25:40 +01002603 case '?':
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002604 case 'h':
2605 help();
2606 break;
2607 case 'f':
2608 fmt = optarg;
2609 break;
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002610 case 'q':
2611 quiet = true;
2612 break;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002613 }
2614 }
Kevin Wolffc11eb22013-08-05 10:53:04 +02002615 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002616 error_exit("Expecting one image file name");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002617 }
2618 filename = argv[optind++];
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002619
2620 /* Choose grow, shrink, or absolute resize mode */
2621 switch (size[0]) {
2622 case '+':
2623 relative = 1;
2624 size++;
2625 break;
2626 case '-':
2627 relative = -1;
2628 size++;
2629 break;
2630 default:
2631 relative = 0;
2632 break;
2633 }
2634
2635 /* Parse size */
Peter Crosthwaite87ea75d2014-01-01 18:49:17 -08002636 param = qemu_opts_create(&resize_options, NULL, 0, &error_abort);
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002637 if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) {
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002638 /* Error message already printed when size parsing fails */
Jes Sorensen2a819982010-12-06 17:08:31 +01002639 ret = -1;
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002640 qemu_opts_del(param);
Jes Sorensen2a819982010-12-06 17:08:31 +01002641 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002642 }
Dong Xu Wang20caf0f2012-08-06 10:18:42 +08002643 n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0);
2644 qemu_opts_del(param);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002645
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002646 bs = bdrv_new_open("image", filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR,
2647 true, quiet);
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002648 if (!bs) {
Jes Sorensen2a819982010-12-06 17:08:31 +01002649 ret = -1;
2650 goto out;
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002651 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002652
2653 if (relative) {
2654 total_size = bdrv_getlength(bs) + n * relative;
2655 } else {
2656 total_size = n;
2657 }
2658 if (total_size <= 0) {
Jes Sorensen15654a62010-12-16 14:31:53 +01002659 error_report("New image size must be positive");
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002660 ret = -1;
2661 goto out;
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002662 }
2663
2664 ret = bdrv_truncate(bs, total_size);
2665 switch (ret) {
2666 case 0:
Miroslav Rezaninaf382d432013-02-13 09:09:40 +01002667 qprintf(quiet, "Image resized.\n");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002668 break;
2669 case -ENOTSUP:
Kevin Wolf259b2172012-03-06 12:44:45 +01002670 error_report("This image does not support resize");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002671 break;
2672 case -EACCES:
Jes Sorensen15654a62010-12-16 14:31:53 +01002673 error_report("Image is read-only");
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002674 break;
2675 default:
Jes Sorensen15654a62010-12-16 14:31:53 +01002676 error_report("Error resizing image (%d)", -ret);
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002677 break;
2678 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002679out:
Jes Sorensen2a819982010-12-06 17:08:31 +01002680 if (bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +08002681 bdrv_unref(bs);
Jes Sorensen2a819982010-12-06 17:08:31 +01002682 }
MORITA Kazutakac2abcce2010-06-21 04:26:35 +09002683 if (ret) {
2684 return 1;
2685 }
Stefan Hajnocziae6b0ed2010-04-24 09:12:12 +01002686 return 0;
2687}
2688
Max Reitz6f176b42013-09-03 10:09:50 +02002689static int img_amend(int argc, char **argv)
2690{
2691 int c, ret = 0;
2692 char *options = NULL;
Chunyan Liu83d05212014-06-05 17:20:51 +08002693 QemuOptsList *create_opts = NULL;
2694 QemuOpts *opts = NULL;
Max Reitz6f176b42013-09-03 10:09:50 +02002695 const char *fmt = NULL, *filename;
2696 bool quiet = false;
2697 BlockDriverState *bs = NULL;
2698
2699 for (;;) {
2700 c = getopt(argc, argv, "hqf:o:");
2701 if (c == -1) {
2702 break;
2703 }
2704
2705 switch (c) {
2706 case 'h':
2707 case '?':
2708 help();
2709 break;
2710 case 'o':
Kevin Wolf626f84f2014-02-21 16:24:06 +01002711 if (!is_valid_option_list(optarg)) {
2712 error_report("Invalid option list: %s", optarg);
2713 ret = -1;
2714 goto out;
2715 }
2716 if (!options) {
2717 options = g_strdup(optarg);
2718 } else {
2719 char *old_options = options;
2720 options = g_strdup_printf("%s,%s", options, optarg);
2721 g_free(old_options);
2722 }
Max Reitz6f176b42013-09-03 10:09:50 +02002723 break;
2724 case 'f':
2725 fmt = optarg;
2726 break;
2727 case 'q':
2728 quiet = true;
2729 break;
2730 }
2731 }
2732
Max Reitz6f176b42013-09-03 10:09:50 +02002733 if (!options) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002734 error_exit("Must specify options (-o)");
Max Reitz6f176b42013-09-03 10:09:50 +02002735 }
2736
Kevin Wolfa283cb62014-02-21 16:24:07 +01002737 filename = (optind == argc - 1) ? argv[argc - 1] : NULL;
2738 if (fmt && has_help_option(options)) {
2739 /* If a format is explicitly specified (and possibly no filename is
2740 * given), print option help here */
2741 ret = print_block_option_help(filename, fmt);
2742 goto out;
2743 }
2744
2745 if (optind != argc - 1) {
Fam Zhengac1307a2014-04-22 13:36:11 +08002746 error_exit("Expecting one image file name");
Kevin Wolfa283cb62014-02-21 16:24:07 +01002747 }
Max Reitz6f176b42013-09-03 10:09:50 +02002748
Kevin Wolf9ffe3332014-04-17 16:57:13 +02002749 bs = bdrv_new_open("image", filename, fmt,
2750 BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet);
Max Reitz6f176b42013-09-03 10:09:50 +02002751 if (!bs) {
2752 error_report("Could not open image '%s'", filename);
2753 ret = -1;
2754 goto out;
2755 }
2756
2757 fmt = bs->drv->format_name;
2758
Kevin Wolf626f84f2014-02-21 16:24:06 +01002759 if (has_help_option(options)) {
Kevin Wolfa283cb62014-02-21 16:24:07 +01002760 /* If the format was auto-detected, print option help here */
Max Reitz6f176b42013-09-03 10:09:50 +02002761 ret = print_block_option_help(filename, fmt);
2762 goto out;
2763 }
2764
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002765 create_opts = qemu_opts_append(create_opts, bs->drv->create_opts);
Chunyan Liu83d05212014-06-05 17:20:51 +08002766 opts = qemu_opts_create(create_opts, NULL, 0, &error_abort);
2767 if (options && qemu_opts_do_parse(opts, options, NULL)) {
Max Reitz6f176b42013-09-03 10:09:50 +02002768 error_report("Invalid options for file format '%s'", fmt);
2769 ret = -1;
2770 goto out;
2771 }
2772
Chunyan Liuc282e1f2014-06-05 17:21:11 +08002773 ret = bdrv_amend_options(bs, opts);
Max Reitz6f176b42013-09-03 10:09:50 +02002774 if (ret < 0) {
2775 error_report("Error while amending options: %s", strerror(-ret));
2776 goto out;
2777 }
2778
2779out:
2780 if (bs) {
2781 bdrv_unref(bs);
2782 }
Chunyan Liu83d05212014-06-05 17:20:51 +08002783 qemu_opts_del(opts);
2784 qemu_opts_free(create_opts);
Kevin Wolf626f84f2014-02-21 16:24:06 +01002785 g_free(options);
2786
Max Reitz6f176b42013-09-03 10:09:50 +02002787 if (ret) {
2788 return 1;
2789 }
2790 return 0;
2791}
2792
Anthony Liguoric227f092009-10-01 16:12:16 -05002793static const img_cmd_t img_cmds[] = {
Stuart Brady153859b2009-06-07 00:42:17 +01002794#define DEF(option, callback, arg_string) \
2795 { option, callback },
2796#include "qemu-img-cmds.h"
2797#undef DEF
2798#undef GEN_DOCS
2799 { NULL, NULL, },
2800};
2801
bellardea2384d2004-08-01 21:59:26 +00002802int main(int argc, char **argv)
2803{
Anthony Liguoric227f092009-10-01 16:12:16 -05002804 const img_cmd_t *cmd;
Stuart Brady153859b2009-06-07 00:42:17 +01002805 const char *cmdname;
Jeff Cody7db16892014-04-25 17:02:32 -04002806 int c;
Jeff Cody7db16892014-04-25 17:02:32 -04002807 static const struct option long_options[] = {
2808 {"help", no_argument, 0, 'h'},
Jeff Cody5f6979c2014-04-28 14:37:18 -04002809 {"version", no_argument, 0, 'v'},
Jeff Cody7db16892014-04-25 17:02:32 -04002810 {0, 0, 0, 0}
2811 };
bellardea2384d2004-08-01 21:59:26 +00002812
MORITA Kazutaka526eda12013-07-23 17:30:11 +09002813#ifdef CONFIG_POSIX
2814 signal(SIGPIPE, SIG_IGN);
2815#endif
2816
Kevin Wolf53f76e52010-12-16 15:10:32 +01002817 error_set_progname(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +08002818 qemu_init_exec_dir(argv[0]);
Kevin Wolf53f76e52010-12-16 15:10:32 +01002819
Paolo Bonzini2592c592012-11-03 18:10:17 +01002820 qemu_init_main_loop();
bellardea2384d2004-08-01 21:59:26 +00002821 bdrv_init();
Fam Zhengac1307a2014-04-22 13:36:11 +08002822 if (argc < 2) {
2823 error_exit("Not enough arguments");
2824 }
Stuart Brady153859b2009-06-07 00:42:17 +01002825 cmdname = argv[1];
Stuart Brady153859b2009-06-07 00:42:17 +01002826
2827 /* find the command */
Jeff Cody5f6979c2014-04-28 14:37:18 -04002828 for (cmd = img_cmds; cmd->name != NULL; cmd++) {
Stuart Brady153859b2009-06-07 00:42:17 +01002829 if (!strcmp(cmdname, cmd->name)) {
Jeff Cody7db16892014-04-25 17:02:32 -04002830 return cmd->handler(argc - 1, argv + 1);
Stuart Brady153859b2009-06-07 00:42:17 +01002831 }
bellardea2384d2004-08-01 21:59:26 +00002832 }
Stuart Brady153859b2009-06-07 00:42:17 +01002833
Jeff Cody5f6979c2014-04-28 14:37:18 -04002834 c = getopt_long(argc, argv, "h", long_options, NULL);
Jeff Cody7db16892014-04-25 17:02:32 -04002835
2836 if (c == 'h') {
2837 help();
2838 }
Jeff Cody5f6979c2014-04-28 14:37:18 -04002839 if (c == 'v') {
2840 printf(QEMU_IMG_VERSION);
2841 return 0;
2842 }
Jeff Cody7db16892014-04-25 17:02:32 -04002843
Stuart Brady153859b2009-06-07 00:42:17 +01002844 /* not found */
Fam Zhengac1307a2014-04-22 13:36:11 +08002845 error_exit("Command not found: %s", cmdname);
bellardea2384d2004-08-01 21:59:26 +00002846}