]>
Commit | Line | Data |
---|---|---|
ea2384d3 | 1 | /* |
fb43f4dd | 2 | * QEMU disk image utility |
5fafdf24 | 3 | * |
68d0f70e | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
5fafdf24 | 5 | * |
ea2384d3 FB |
6 | * 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 | */ | |
452fcdbc | 24 | |
80c71a24 | 25 | #include "qemu/osdep.h" |
c2a3d7da EB |
26 | #include <getopt.h> |
27 | ||
67a1de0d | 28 | #include "qemu-version.h" |
da34e65c | 29 | #include "qapi/error.h" |
9af23989 | 30 | #include "qapi/qapi-visit-block-core.h" |
b3db211f | 31 | #include "qapi/qobject-output-visitor.h" |
7b1b5d19 | 32 | #include "qapi/qmp/qjson.h" |
452fcdbc | 33 | #include "qapi/qmp/qdict.h" |
fc81fa1e | 34 | #include "qapi/qmp/qstring.h" |
f348b6d1 | 35 | #include "qemu/cutils.h" |
3babeb15 | 36 | #include "qemu/config-file.h" |
1de7afc9 PB |
37 | #include "qemu/option.h" |
38 | #include "qemu/error-report.h" | |
06a1e0c1 | 39 | #include "qemu/log.h" |
3babeb15 | 40 | #include "qom/object_interfaces.h" |
9c17d615 | 41 | #include "sysemu/sysemu.h" |
26f54e9a | 42 | #include "sysemu/block-backend.h" |
737e150e | 43 | #include "block/block_int.h" |
d4a3238a | 44 | #include "block/blockjob.h" |
f364ec65 | 45 | #include "block/qapi.h" |
c2297088 | 46 | #include "crypto/init.h" |
06a1e0c1 | 47 | #include "trace/control.h" |
e8445331 | 48 | |
7e563bfb | 49 | #define QEMU_IMG_VERSION "qemu-img version " QEMU_FULL_VERSION \ |
0781dd6e | 50 | "\n" QEMU_COPYRIGHT "\n" |
5f6979cb | 51 | |
c227f099 | 52 | typedef struct img_cmd_t { |
153859be SB |
53 | const char *name; |
54 | int (*handler)(int argc, char **argv); | |
c227f099 | 55 | } img_cmd_t; |
153859be | 56 | |
8599ea4c FS |
57 | enum { |
58 | OPTION_OUTPUT = 256, | |
59 | OPTION_BACKING_CHAIN = 257, | |
3babeb15 | 60 | OPTION_OBJECT = 258, |
eb769f74 | 61 | OPTION_IMAGE_OPTS = 259, |
b6495fa8 | 62 | OPTION_PATTERN = 260, |
55d539c8 KW |
63 | OPTION_FLUSH_INTERVAL = 261, |
64 | OPTION_NO_DRAIN = 262, | |
305b4c60 | 65 | OPTION_TARGET_IMAGE_OPTS = 263, |
fd03c2b8 | 66 | OPTION_SIZE = 264, |
dc5f690b | 67 | OPTION_PREALLOCATION = 265, |
4ffca890 | 68 | OPTION_SHRINK = 266, |
8599ea4c FS |
69 | }; |
70 | ||
71 | typedef enum OutputFormat { | |
72 | OFORMAT_JSON, | |
73 | OFORMAT_HUMAN, | |
74 | } OutputFormat; | |
75 | ||
e6996143 | 76 | /* Default to cache=writeback as data integrity is not important for qemu-img */ |
661a0f71 | 77 | #define BDRV_DEFAULT_CACHE "writeback" |
137519ce | 78 | |
00c6d403 | 79 | static void format_print(void *opaque, const char *name) |
ea2384d3 | 80 | { |
00c6d403 | 81 | printf(" %s", name); |
ea2384d3 FB |
82 | } |
83 | ||
ac1307ab FZ |
84 | static void QEMU_NORETURN GCC_FMT_ATTR(1, 2) error_exit(const char *fmt, ...) |
85 | { | |
86 | va_list ap; | |
87 | ||
88 | error_printf("qemu-img: "); | |
89 | ||
90 | va_start(ap, fmt); | |
91 | error_vprintf(fmt, ap); | |
92 | va_end(ap); | |
93 | ||
94 | error_printf("\nTry 'qemu-img --help' for more information\n"); | |
95 | exit(EXIT_FAILURE); | |
96 | } | |
97 | ||
c9192973 SH |
98 | static void QEMU_NORETURN missing_argument(const char *option) |
99 | { | |
100 | error_exit("missing argument for option '%s'", option); | |
101 | } | |
102 | ||
103 | static void QEMU_NORETURN unrecognized_option(const char *option) | |
104 | { | |
105 | error_exit("unrecognized option '%s'", option); | |
106 | } | |
107 | ||
d2c639d6 | 108 | /* Please keep in synch with qemu-img.texi */ |
ac1307ab | 109 | static void QEMU_NORETURN help(void) |
ea2384d3 | 110 | { |
e00291c0 | 111 | const char *help_msg = |
5f6979cb | 112 | QEMU_IMG_VERSION |
10985131 | 113 | "usage: qemu-img [standard options] command [command options]\n" |
3f020d70 | 114 | "QEMU disk image utility\n" |
115 | "\n" | |
10985131 DL |
116 | " '-h', '--help' display this help and exit\n" |
117 | " '-V', '--version' output version information and exit\n" | |
06a1e0c1 DL |
118 | " '-T', '--trace' [[enable=]<pattern>][,events=<file>][,file=<file>]\n" |
119 | " specify tracing options\n" | |
10985131 | 120 | "\n" |
3f020d70 | 121 | "Command syntax:\n" |
153859be SB |
122 | #define DEF(option, callback, arg_string) \ |
123 | " " arg_string "\n" | |
124 | #include "qemu-img-cmds.h" | |
125 | #undef DEF | |
3f020d70 | 126 | "\n" |
127 | "Command parameters:\n" | |
128 | " 'filename' is a disk image filename\n" | |
3babeb15 DB |
129 | " 'objectdef' is a QEMU user creatable object definition. See the qemu(1)\n" |
130 | " manual page for a description of the object properties. The most common\n" | |
131 | " object type is a 'secret', which is used to supply passwords and/or\n" | |
132 | " encryption keys.\n" | |
3f020d70 | 133 | " 'fmt' is the disk image format. It is guessed automatically in most cases\n" |
661a0f71 | 134 | " 'cache' is the cache mode used to write the output disk image, the valid\n" |
80ccf93b LY |
135 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
136 | " 'directsync' and 'unsafe' (default for convert)\n" | |
bb87fdf8 SH |
137 | " 'src_cache' is the cache mode used to read input disk images, the valid\n" |
138 | " options are the same as for the 'cache' option\n" | |
3f020d70 | 139 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
5e00984a KW |
140 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
141 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" | |
142 | " supported. 'b' is ignored.\n" | |
3f020d70 | 143 | " 'output_filename' is the destination disk image filename\n" |
144 | " 'output_fmt' is the destination format\n" | |
145 | " 'options' is a comma separated list of format specific options in a\n" | |
146 | " name=value format. Use -o ? for an overview of the options supported by the\n" | |
147 | " used format\n" | |
ef80654d WX |
148 | " 'snapshot_param' is param used for internal snapshot, format\n" |
149 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" | |
150 | " '[ID_OR_NAME]'\n" | |
3f020d70 | 151 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
6e6e55f5 JS |
152 | " '-u' allows unsafe backing chains. For rebasing, it is assumed that old and\n" |
153 | " new backing file match exactly. The image doesn't need a working\n" | |
154 | " backing file before rebasing in this case (useful for renaming the\n" | |
155 | " backing file). For image creation, allow creating without attempting\n" | |
156 | " to open the backing file.\n" | |
3f020d70 | 157 | " '-h' with or without a command shows this help and lists the supported formats\n" |
6b837bc4 | 158 | " '-p' show progress of command (only certain commands)\n" |
f382d43a | 159 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
11b6699a PL |
160 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
161 | " contain only zeros for qemu-img to create a sparse image during\n" | |
162 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" | |
163 | " unallocated or zero sectors, and the destination image will always be\n" | |
164 | " fully allocated\n" | |
c054b3fd | 165 | " '--output' takes the format in which the output must be done (human or json)\n" |
b2e10493 AD |
166 | " '-n' skips the target volume creation (useful if the volume is created\n" |
167 | " prior to running qemu-img)\n" | |
3f020d70 | 168 | "\n" |
4534ff54 KW |
169 | "Parameters to check subcommand:\n" |
170 | " '-r' tries to repair any inconsistencies that are found during the check.\n" | |
171 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" | |
172 | " kinds of errors, with a higher risk of choosing the wrong fix or\n" | |
0546b8c2 | 173 | " hiding corruption that has already occurred.\n" |
4534ff54 | 174 | "\n" |
2d9187bc PL |
175 | "Parameters to convert subcommand:\n" |
176 | " '-m' specifies how many coroutines work in parallel during the convert\n" | |
177 | " process (defaults to 8)\n" | |
178 | " '-W' allow to write to the target out of order rather than sequential\n" | |
179 | "\n" | |
3f020d70 | 180 | "Parameters to snapshot subcommand:\n" |
181 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" | |
182 | " '-a' applies a snapshot (revert disk to saved state)\n" | |
183 | " '-c' creates a snapshot\n" | |
184 | " '-d' deletes a snapshot\n" | |
d14ed18c MR |
185 | " '-l' lists all snapshots in the given image\n" |
186 | "\n" | |
187 | "Parameters to compare subcommand:\n" | |
188 | " '-f' first image format\n" | |
189 | " '-F' second image format\n" | |
86ce1f6e RS |
190 | " '-s' run in Strict mode - fail on different image size or sector allocation\n" |
191 | "\n" | |
192 | "Parameters to dd subcommand:\n" | |
193 | " 'bs=BYTES' read and write up to BYTES bytes at a time " | |
194 | "(default: 512)\n" | |
195 | " 'count=N' copy only N input blocks\n" | |
196 | " 'if=FILE' read from FILE\n" | |
f7c15533 RS |
197 | " 'of=FILE' write to FILE\n" |
198 | " 'skip=N' skip N bs-sized blocks at the start of input\n"; | |
e00291c0 PB |
199 | |
200 | printf("%s\nSupported formats:", help_msg); | |
00c6d403 | 201 | bdrv_iterate_format(format_print, NULL); |
f5048cb7 | 202 | printf("\n\n" QEMU_HELP_BOTTOM "\n"); |
ac1307ab | 203 | exit(EXIT_SUCCESS); |
ea2384d3 FB |
204 | } |
205 | ||
3babeb15 DB |
206 | static QemuOptsList qemu_object_opts = { |
207 | .name = "object", | |
208 | .implied_opt_name = "qom-type", | |
209 | .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head), | |
210 | .desc = { | |
211 | { } | |
212 | }, | |
213 | }; | |
214 | ||
eb769f74 DB |
215 | static QemuOptsList qemu_source_opts = { |
216 | .name = "source", | |
217 | .implied_opt_name = "file", | |
218 | .head = QTAILQ_HEAD_INITIALIZER(qemu_source_opts.head), | |
219 | .desc = { | |
220 | { } | |
221 | }, | |
222 | }; | |
223 | ||
7c30f657 | 224 | static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) |
f382d43a MR |
225 | { |
226 | int ret = 0; | |
227 | if (!quiet) { | |
228 | va_list args; | |
229 | va_start(args, fmt); | |
230 | ret = vprintf(fmt, args); | |
231 | va_end(args); | |
232 | } | |
233 | return ret; | |
234 | } | |
235 | ||
ea2384d3 | 236 | |
4ac8aacd JS |
237 | static int print_block_option_help(const char *filename, const char *fmt) |
238 | { | |
239 | BlockDriver *drv, *proto_drv; | |
83d0521a | 240 | QemuOptsList *create_opts = NULL; |
b65a5e12 | 241 | Error *local_err = NULL; |
4ac8aacd JS |
242 | |
243 | /* Find driver and parse its options */ | |
244 | drv = bdrv_find_format(fmt); | |
245 | if (!drv) { | |
15654a6d | 246 | error_report("Unknown file format '%s'", fmt); |
4ac8aacd JS |
247 | return 1; |
248 | } | |
249 | ||
d402b6a2 HR |
250 | if (!drv->create_opts) { |
251 | error_report("Format driver '%s' does not support image creation", fmt); | |
252 | return 1; | |
253 | } | |
254 | ||
c282e1fd | 255 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
a283cb6e | 256 | if (filename) { |
b65a5e12 | 257 | proto_drv = bdrv_find_protocol(filename, true, &local_err); |
a283cb6e | 258 | if (!proto_drv) { |
2867ce4a | 259 | error_report_err(local_err); |
83d0521a | 260 | qemu_opts_free(create_opts); |
a283cb6e KW |
261 | return 1; |
262 | } | |
d402b6a2 HR |
263 | if (!proto_drv->create_opts) { |
264 | error_report("Protocal driver '%s' does not support image creation", | |
265 | proto_drv->format_name); | |
266 | return 1; | |
267 | } | |
c282e1fd | 268 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); |
a283cb6e KW |
269 | } |
270 | ||
7f3fb001 | 271 | printf("Supported options:\n"); |
83d0521a CL |
272 | qemu_opts_print_help(create_opts); |
273 | qemu_opts_free(create_opts); | |
4ac8aacd JS |
274 | return 0; |
275 | } | |
276 | ||
eb769f74 | 277 | |
efaa7c4e | 278 | static BlockBackend *img_open_opts(const char *optstr, |
ce099547 | 279 | QemuOpts *opts, int flags, bool writethrough, |
335e9937 | 280 | bool quiet, bool force_share) |
eb769f74 DB |
281 | { |
282 | QDict *options; | |
283 | Error *local_err = NULL; | |
284 | BlockBackend *blk; | |
285 | options = qemu_opts_to_qdict(opts, NULL); | |
335e9937 FZ |
286 | if (force_share) { |
287 | if (qdict_haskey(options, BDRV_OPT_FORCE_SHARE) | |
4615f878 | 288 | && strcmp(qdict_get_str(options, BDRV_OPT_FORCE_SHARE), "on")) { |
335e9937 | 289 | error_report("--force-share/-U conflicts with image options"); |
cb3e7f08 | 290 | qobject_unref(options); |
335e9937 FZ |
291 | return NULL; |
292 | } | |
4615f878 | 293 | qdict_put_str(options, BDRV_OPT_FORCE_SHARE, "on"); |
335e9937 | 294 | } |
efaa7c4e | 295 | blk = blk_new_open(NULL, NULL, options, flags, &local_err); |
eb769f74 | 296 | if (!blk) { |
143605a2 | 297 | error_reportf_err(local_err, "Could not open '%s': ", optstr); |
eb769f74 DB |
298 | return NULL; |
299 | } | |
ce099547 | 300 | blk_set_enable_write_cache(blk, !writethrough); |
eb769f74 | 301 | |
eb769f74 DB |
302 | return blk; |
303 | } | |
304 | ||
efaa7c4e | 305 | static BlockBackend *img_open_file(const char *filename, |
29cf9336 | 306 | QDict *options, |
eb769f74 | 307 | const char *fmt, int flags, |
335e9937 FZ |
308 | bool writethrough, bool quiet, |
309 | bool force_share) | |
eb769f74 DB |
310 | { |
311 | BlockBackend *blk; | |
34b5d2c6 | 312 | Error *local_err = NULL; |
ad717139 | 313 | |
29cf9336 DB |
314 | if (!options) { |
315 | options = qdict_new(); | |
316 | } | |
75c23805 | 317 | if (fmt) { |
46f5ac20 | 318 | qdict_put_str(options, "driver", fmt); |
75c23805 | 319 | } |
b9eaf9ec | 320 | |
335e9937 | 321 | if (force_share) { |
579cf1d1 | 322 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
335e9937 | 323 | } |
efaa7c4e | 324 | blk = blk_new_open(filename, NULL, options, flags, &local_err); |
5bd31326 | 325 | if (!blk) { |
c29b77f9 | 326 | error_reportf_err(local_err, "Could not open '%s': ", filename); |
eb769f74 | 327 | return NULL; |
75c23805 | 328 | } |
ce099547 | 329 | blk_set_enable_write_cache(blk, !writethrough); |
b9eaf9ec | 330 | |
eb769f74 DB |
331 | return blk; |
332 | } | |
333 | ||
334 | ||
29cf9336 DB |
335 | static int img_add_key_secrets(void *opaque, |
336 | const char *name, const char *value, | |
337 | Error **errp) | |
338 | { | |
339 | QDict *options = opaque; | |
340 | ||
341 | if (g_str_has_suffix(name, "key-secret")) { | |
187f47e9 | 342 | qdict_put_str(options, name, value); |
29cf9336 DB |
343 | } |
344 | ||
345 | return 0; | |
346 | } | |
347 | ||
348 | static BlockBackend *img_open_new_file(const char *filename, | |
349 | QemuOpts *create_opts, | |
350 | const char *fmt, int flags, | |
351 | bool writethrough, bool quiet, | |
352 | bool force_share) | |
353 | { | |
354 | QDict *options = NULL; | |
355 | ||
356 | options = qdict_new(); | |
357 | qemu_opt_foreach(create_opts, img_add_key_secrets, options, &error_abort); | |
358 | ||
359 | return img_open_file(filename, options, fmt, flags, writethrough, quiet, | |
360 | force_share); | |
361 | } | |
362 | ||
363 | ||
efaa7c4e | 364 | static BlockBackend *img_open(bool image_opts, |
eb769f74 | 365 | const char *filename, |
ce099547 | 366 | const char *fmt, int flags, bool writethrough, |
335e9937 | 367 | bool quiet, bool force_share) |
eb769f74 DB |
368 | { |
369 | BlockBackend *blk; | |
370 | if (image_opts) { | |
371 | QemuOpts *opts; | |
372 | if (fmt) { | |
373 | error_report("--image-opts and --format are mutually exclusive"); | |
374 | return NULL; | |
375 | } | |
376 | opts = qemu_opts_parse_noisily(qemu_find_opts("source"), | |
377 | filename, true); | |
378 | if (!opts) { | |
379 | return NULL; | |
380 | } | |
335e9937 FZ |
381 | blk = img_open_opts(filename, opts, flags, writethrough, quiet, |
382 | force_share); | |
eb769f74 | 383 | } else { |
29cf9336 | 384 | blk = img_open_file(filename, NULL, fmt, flags, writethrough, quiet, |
335e9937 | 385 | force_share); |
75c23805 | 386 | } |
7e7d56d9 | 387 | return blk; |
75c23805 FB |
388 | } |
389 | ||
eb769f74 | 390 | |
83d0521a | 391 | static int add_old_style_options(const char *fmt, QemuOpts *opts, |
eec77d9e JS |
392 | const char *base_filename, |
393 | const char *base_fmt) | |
efa84d43 | 394 | { |
6750e795 MA |
395 | Error *err = NULL; |
396 | ||
efa84d43 | 397 | if (base_filename) { |
f43e47db | 398 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, base_filename, &err); |
6750e795 | 399 | if (err) { |
15654a6d JS |
400 | error_report("Backing file not supported for file format '%s'", |
401 | fmt); | |
6750e795 | 402 | error_free(err); |
c2abccec | 403 | return -1; |
efa84d43 KW |
404 | } |
405 | } | |
406 | if (base_fmt) { | |
f43e47db | 407 | qemu_opt_set(opts, BLOCK_OPT_BACKING_FMT, base_fmt, &err); |
6750e795 | 408 | if (err) { |
15654a6d JS |
409 | error_report("Backing file format not supported for file " |
410 | "format '%s'", fmt); | |
6750e795 | 411 | error_free(err); |
c2abccec | 412 | return -1; |
efa84d43 KW |
413 | } |
414 | } | |
c2abccec | 415 | return 0; |
efa84d43 KW |
416 | } |
417 | ||
606caa0a MA |
418 | static int64_t cvtnum(const char *s) |
419 | { | |
f17fd4fd | 420 | int err; |
f46bfdbf | 421 | uint64_t value; |
606caa0a | 422 | |
f17fd4fd MA |
423 | err = qemu_strtosz(s, NULL, &value); |
424 | if (err < 0) { | |
425 | return err; | |
426 | } | |
f46bfdbf MA |
427 | if (value > INT64_MAX) { |
428 | return -ERANGE; | |
429 | } | |
f17fd4fd | 430 | return value; |
606caa0a MA |
431 | } |
432 | ||
ea2384d3 FB |
433 | static int img_create(int argc, char **argv) |
434 | { | |
a9300911 | 435 | int c; |
1da7cfbd | 436 | uint64_t img_size = -1; |
ea2384d3 | 437 | const char *fmt = "raw"; |
9230eaf6 | 438 | const char *base_fmt = NULL; |
ea2384d3 FB |
439 | const char *filename; |
440 | const char *base_filename = NULL; | |
9ea2ea71 | 441 | char *options = NULL; |
9b37525a | 442 | Error *local_err = NULL; |
f382d43a | 443 | bool quiet = false; |
6e6e55f5 | 444 | int flags = 0; |
3b46e624 | 445 | |
ea2384d3 | 446 | for(;;) { |
3babeb15 DB |
447 | static const struct option long_options[] = { |
448 | {"help", no_argument, 0, 'h'}, | |
449 | {"object", required_argument, 0, OPTION_OBJECT}, | |
450 | {0, 0, 0, 0} | |
451 | }; | |
6e6e55f5 | 452 | c = getopt_long(argc, argv, ":F:b:f:ho:qu", |
3babeb15 | 453 | long_options, NULL); |
b8fb60da | 454 | if (c == -1) { |
ea2384d3 | 455 | break; |
b8fb60da | 456 | } |
ea2384d3 | 457 | switch(c) { |
c9192973 SH |
458 | case ':': |
459 | missing_argument(argv[optind - 1]); | |
460 | break; | |
ef87394c | 461 | case '?': |
c9192973 SH |
462 | unrecognized_option(argv[optind - 1]); |
463 | break; | |
ea2384d3 FB |
464 | case 'h': |
465 | help(); | |
466 | break; | |
9230eaf6 AL |
467 | case 'F': |
468 | base_fmt = optarg; | |
469 | break; | |
ea2384d3 FB |
470 | case 'b': |
471 | base_filename = optarg; | |
472 | break; | |
473 | case 'f': | |
474 | fmt = optarg; | |
475 | break; | |
9ea2ea71 | 476 | case 'o': |
77386bf6 KW |
477 | if (!is_valid_option_list(optarg)) { |
478 | error_report("Invalid option list: %s", optarg); | |
479 | goto fail; | |
480 | } | |
481 | if (!options) { | |
482 | options = g_strdup(optarg); | |
483 | } else { | |
484 | char *old_options = options; | |
485 | options = g_strdup_printf("%s,%s", options, optarg); | |
486 | g_free(old_options); | |
487 | } | |
9ea2ea71 | 488 | break; |
f382d43a MR |
489 | case 'q': |
490 | quiet = true; | |
491 | break; | |
6e6e55f5 JS |
492 | case 'u': |
493 | flags |= BDRV_O_NO_BACKING; | |
494 | break; | |
3babeb15 DB |
495 | case OPTION_OBJECT: { |
496 | QemuOpts *opts; | |
497 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
498 | optarg, true); | |
499 | if (!opts) { | |
500 | goto fail; | |
501 | } | |
502 | } break; | |
ea2384d3 FB |
503 | } |
504 | } | |
9230eaf6 | 505 | |
b50cbabc | 506 | /* Get the filename */ |
a283cb6e KW |
507 | filename = (optind < argc) ? argv[optind] : NULL; |
508 | if (options && has_help_option(options)) { | |
509 | g_free(options); | |
510 | return print_block_option_help(filename, fmt); | |
511 | } | |
512 | ||
b8fb60da | 513 | if (optind >= argc) { |
ac1307ab | 514 | error_exit("Expecting image file name"); |
b8fb60da | 515 | } |
a283cb6e | 516 | optind++; |
b50cbabc | 517 | |
3babeb15 DB |
518 | if (qemu_opts_foreach(&qemu_object_opts, |
519 | user_creatable_add_opts_foreach, | |
51b9b478 | 520 | NULL, NULL)) { |
3babeb15 DB |
521 | goto fail; |
522 | } | |
523 | ||
1da7cfbd JS |
524 | /* Get image size, if specified */ |
525 | if (optind < argc) { | |
70b4f4bb | 526 | int64_t sval; |
606caa0a MA |
527 | |
528 | sval = cvtnum(argv[optind++]); | |
529 | if (sval < 0) { | |
79443397 LG |
530 | if (sval == -ERANGE) { |
531 | error_report("Image size must be less than 8 EiB!"); | |
532 | } else { | |
533 | error_report("Invalid image size specified! You may use k, M, " | |
5e00984a KW |
534 | "G, T, P or E suffixes for "); |
535 | error_report("kilobytes, megabytes, gigabytes, terabytes, " | |
536 | "petabytes and exabytes."); | |
79443397 | 537 | } |
77386bf6 | 538 | goto fail; |
1da7cfbd JS |
539 | } |
540 | img_size = (uint64_t)sval; | |
541 | } | |
fc11eb26 | 542 | if (optind != argc) { |
ac1307ab | 543 | error_exit("Unexpected argument: %s", argv[optind]); |
fc11eb26 | 544 | } |
1da7cfbd | 545 | |
9b37525a | 546 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
6e6e55f5 | 547 | options, img_size, flags, quiet, &local_err); |
84d18f06 | 548 | if (local_err) { |
c29b77f9 | 549 | error_reportf_err(local_err, "%s: ", filename); |
77386bf6 | 550 | goto fail; |
c2abccec | 551 | } |
a9300911 | 552 | |
77386bf6 | 553 | g_free(options); |
ea2384d3 | 554 | return 0; |
77386bf6 KW |
555 | |
556 | fail: | |
557 | g_free(options); | |
558 | return 1; | |
ea2384d3 FB |
559 | } |
560 | ||
f382d43a | 561 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
8599ea4c | 562 | { |
8599ea4c | 563 | QString *str; |
8599ea4c | 564 | QObject *obj; |
7d5e199a | 565 | Visitor *v = qobject_output_visitor_new(&obj); |
3b098d56 EB |
566 | |
567 | visit_type_ImageCheck(v, NULL, &check, &error_abort); | |
568 | visit_complete(v, &obj); | |
8599ea4c FS |
569 | str = qobject_to_json_pretty(obj); |
570 | assert(str != NULL); | |
f382d43a | 571 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
cb3e7f08 | 572 | qobject_unref(obj); |
3b098d56 | 573 | visit_free(v); |
cb3e7f08 | 574 | qobject_unref(str); |
8599ea4c FS |
575 | } |
576 | ||
f382d43a | 577 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
8599ea4c FS |
578 | { |
579 | if (!(check->corruptions || check->leaks || check->check_errors)) { | |
f382d43a | 580 | qprintf(quiet, "No errors were found on the image.\n"); |
8599ea4c FS |
581 | } else { |
582 | if (check->corruptions) { | |
f382d43a MR |
583 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
584 | "Data may be corrupted, or further writes to the image " | |
585 | "may corrupt it.\n", | |
586 | check->corruptions); | |
8599ea4c FS |
587 | } |
588 | ||
589 | if (check->leaks) { | |
f382d43a MR |
590 | qprintf(quiet, |
591 | "\n%" PRId64 " leaked clusters were found on the image.\n" | |
592 | "This means waste of disk space, but no harm to data.\n", | |
593 | check->leaks); | |
8599ea4c FS |
594 | } |
595 | ||
596 | if (check->check_errors) { | |
f382d43a MR |
597 | qprintf(quiet, |
598 | "\n%" PRId64 | |
599 | " internal errors have occurred during the check.\n", | |
600 | check->check_errors); | |
8599ea4c FS |
601 | } |
602 | } | |
603 | ||
604 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { | |
f382d43a MR |
605 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
606 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", | |
607 | check->allocated_clusters, check->total_clusters, | |
608 | check->allocated_clusters * 100.0 / check->total_clusters, | |
609 | check->fragmented_clusters * 100.0 / check->allocated_clusters, | |
610 | check->compressed_clusters * 100.0 / | |
611 | check->allocated_clusters); | |
8599ea4c FS |
612 | } |
613 | ||
614 | if (check->image_end_offset) { | |
f382d43a MR |
615 | qprintf(quiet, |
616 | "Image end offset: %" PRId64 "\n", check->image_end_offset); | |
8599ea4c FS |
617 | } |
618 | } | |
619 | ||
620 | static int collect_image_check(BlockDriverState *bs, | |
621 | ImageCheck *check, | |
622 | const char *filename, | |
623 | const char *fmt, | |
624 | int fix) | |
625 | { | |
626 | int ret; | |
627 | BdrvCheckResult result; | |
628 | ||
629 | ret = bdrv_check(bs, &result, fix); | |
630 | if (ret < 0) { | |
631 | return ret; | |
632 | } | |
633 | ||
634 | check->filename = g_strdup(filename); | |
635 | check->format = g_strdup(bdrv_get_format_name(bs)); | |
636 | check->check_errors = result.check_errors; | |
637 | check->corruptions = result.corruptions; | |
638 | check->has_corruptions = result.corruptions != 0; | |
639 | check->leaks = result.leaks; | |
640 | check->has_leaks = result.leaks != 0; | |
641 | check->corruptions_fixed = result.corruptions_fixed; | |
642 | check->has_corruptions_fixed = result.corruptions != 0; | |
643 | check->leaks_fixed = result.leaks_fixed; | |
644 | check->has_leaks_fixed = result.leaks != 0; | |
645 | check->image_end_offset = result.image_end_offset; | |
646 | check->has_image_end_offset = result.image_end_offset != 0; | |
647 | check->total_clusters = result.bfi.total_clusters; | |
648 | check->has_total_clusters = result.bfi.total_clusters != 0; | |
649 | check->allocated_clusters = result.bfi.allocated_clusters; | |
650 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; | |
651 | check->fragmented_clusters = result.bfi.fragmented_clusters; | |
652 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; | |
e6439d78 SH |
653 | check->compressed_clusters = result.bfi.compressed_clusters; |
654 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; | |
8599ea4c FS |
655 | |
656 | return 0; | |
657 | } | |
658 | ||
e076f338 KW |
659 | /* |
660 | * Checks an image for consistency. Exit codes: | |
661 | * | |
d6635c4d HR |
662 | * 0 - Check completed, image is good |
663 | * 1 - Check not completed because of internal errors | |
664 | * 2 - Check completed, image is corrupted | |
665 | * 3 - Check completed, image has leaked clusters, but is good otherwise | |
666 | * 63 - Checks are not supported by the image format | |
e076f338 | 667 | */ |
1585969c AL |
668 | static int img_check(int argc, char **argv) |
669 | { | |
670 | int c, ret; | |
8599ea4c | 671 | OutputFormat output_format = OFORMAT_HUMAN; |
40055951 | 672 | const char *filename, *fmt, *output, *cache; |
26f54e9a | 673 | BlockBackend *blk; |
1585969c | 674 | BlockDriverState *bs; |
4534ff54 | 675 | int fix = 0; |
ce099547 KW |
676 | int flags = BDRV_O_CHECK; |
677 | bool writethrough; | |
7e7d56d9 | 678 | ImageCheck *check; |
f382d43a | 679 | bool quiet = false; |
eb769f74 | 680 | bool image_opts = false; |
335e9937 | 681 | bool force_share = false; |
1585969c AL |
682 | |
683 | fmt = NULL; | |
8599ea4c | 684 | output = NULL; |
40055951 | 685 | cache = BDRV_DEFAULT_CACHE; |
ce099547 | 686 | |
1585969c | 687 | for(;;) { |
8599ea4c FS |
688 | int option_index = 0; |
689 | static const struct option long_options[] = { | |
690 | {"help", no_argument, 0, 'h'}, | |
691 | {"format", required_argument, 0, 'f'}, | |
4fd6a984 | 692 | {"repair", required_argument, 0, 'r'}, |
8599ea4c | 693 | {"output", required_argument, 0, OPTION_OUTPUT}, |
3babeb15 | 694 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 695 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 696 | {"force-share", no_argument, 0, 'U'}, |
8599ea4c FS |
697 | {0, 0, 0, 0} |
698 | }; | |
335e9937 | 699 | c = getopt_long(argc, argv, ":hf:r:T:qU", |
8599ea4c | 700 | long_options, &option_index); |
b8fb60da | 701 | if (c == -1) { |
1585969c | 702 | break; |
b8fb60da | 703 | } |
1585969c | 704 | switch(c) { |
c9192973 SH |
705 | case ':': |
706 | missing_argument(argv[optind - 1]); | |
707 | break; | |
ef87394c | 708 | case '?': |
c9192973 SH |
709 | unrecognized_option(argv[optind - 1]); |
710 | break; | |
1585969c AL |
711 | case 'h': |
712 | help(); | |
713 | break; | |
714 | case 'f': | |
715 | fmt = optarg; | |
716 | break; | |
4534ff54 KW |
717 | case 'r': |
718 | flags |= BDRV_O_RDWR; | |
719 | ||
720 | if (!strcmp(optarg, "leaks")) { | |
721 | fix = BDRV_FIX_LEAKS; | |
722 | } else if (!strcmp(optarg, "all")) { | |
723 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; | |
724 | } else { | |
ac1307ab FZ |
725 | error_exit("Unknown option value for -r " |
726 | "(expecting 'leaks' or 'all'): %s", optarg); | |
4534ff54 KW |
727 | } |
728 | break; | |
8599ea4c FS |
729 | case OPTION_OUTPUT: |
730 | output = optarg; | |
731 | break; | |
40055951 HR |
732 | case 'T': |
733 | cache = optarg; | |
734 | break; | |
f382d43a MR |
735 | case 'q': |
736 | quiet = true; | |
737 | break; | |
335e9937 FZ |
738 | case 'U': |
739 | force_share = true; | |
740 | break; | |
3babeb15 DB |
741 | case OPTION_OBJECT: { |
742 | QemuOpts *opts; | |
743 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
744 | optarg, true); | |
745 | if (!opts) { | |
746 | return 1; | |
747 | } | |
748 | } break; | |
eb769f74 DB |
749 | case OPTION_IMAGE_OPTS: |
750 | image_opts = true; | |
751 | break; | |
1585969c AL |
752 | } |
753 | } | |
fc11eb26 | 754 | if (optind != argc - 1) { |
ac1307ab | 755 | error_exit("Expecting one image file name"); |
b8fb60da | 756 | } |
1585969c AL |
757 | filename = argv[optind++]; |
758 | ||
8599ea4c FS |
759 | if (output && !strcmp(output, "json")) { |
760 | output_format = OFORMAT_JSON; | |
761 | } else if (output && !strcmp(output, "human")) { | |
762 | output_format = OFORMAT_HUMAN; | |
763 | } else if (output) { | |
764 | error_report("--output must be used with human or json as argument."); | |
765 | return 1; | |
766 | } | |
767 | ||
3babeb15 DB |
768 | if (qemu_opts_foreach(&qemu_object_opts, |
769 | user_creatable_add_opts_foreach, | |
51b9b478 | 770 | NULL, NULL)) { |
3babeb15 DB |
771 | return 1; |
772 | } | |
773 | ||
ce099547 | 774 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
40055951 HR |
775 | if (ret < 0) { |
776 | error_report("Invalid source cache option: %s", cache); | |
777 | return 1; | |
778 | } | |
779 | ||
335e9937 FZ |
780 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
781 | force_share); | |
7e7d56d9 MA |
782 | if (!blk) { |
783 | return 1; | |
c2abccec | 784 | } |
7e7d56d9 | 785 | bs = blk_bs(blk); |
8599ea4c FS |
786 | |
787 | check = g_new0(ImageCheck, 1); | |
788 | ret = collect_image_check(bs, check, filename, fmt, fix); | |
e076f338 KW |
789 | |
790 | if (ret == -ENOTSUP) { | |
55d492d7 | 791 | error_report("This image format does not support checks"); |
fefddf95 | 792 | ret = 63; |
8599ea4c | 793 | goto fail; |
e076f338 KW |
794 | } |
795 | ||
8599ea4c FS |
796 | if (check->corruptions_fixed || check->leaks_fixed) { |
797 | int corruptions_fixed, leaks_fixed; | |
ccf34716 | 798 | |
8599ea4c FS |
799 | leaks_fixed = check->leaks_fixed; |
800 | corruptions_fixed = check->corruptions_fixed; | |
e076f338 | 801 | |
8599ea4c | 802 | if (output_format == OFORMAT_HUMAN) { |
f382d43a MR |
803 | qprintf(quiet, |
804 | "The following inconsistencies were found and repaired:\n\n" | |
805 | " %" PRId64 " leaked clusters\n" | |
806 | " %" PRId64 " corruptions\n\n" | |
807 | "Double checking the fixed image now...\n", | |
808 | check->leaks_fixed, | |
809 | check->corruptions_fixed); | |
e076f338 KW |
810 | } |
811 | ||
8599ea4c | 812 | ret = collect_image_check(bs, check, filename, fmt, 0); |
1585969c | 813 | |
8599ea4c FS |
814 | check->leaks_fixed = leaks_fixed; |
815 | check->corruptions_fixed = corruptions_fixed; | |
f8111c24 DXW |
816 | } |
817 | ||
832390a5 HR |
818 | if (!ret) { |
819 | switch (output_format) { | |
820 | case OFORMAT_HUMAN: | |
821 | dump_human_image_check(check, quiet); | |
822 | break; | |
823 | case OFORMAT_JSON: | |
824 | dump_json_image_check(check, quiet); | |
825 | break; | |
826 | } | |
c6bb9ad1 FS |
827 | } |
828 | ||
8599ea4c | 829 | if (ret || check->check_errors) { |
832390a5 HR |
830 | if (ret) { |
831 | error_report("Check failed: %s", strerror(-ret)); | |
832 | } else { | |
833 | error_report("Check failed"); | |
834 | } | |
8599ea4c FS |
835 | ret = 1; |
836 | goto fail; | |
c2abccec | 837 | } |
e076f338 | 838 | |
8599ea4c FS |
839 | if (check->corruptions) { |
840 | ret = 2; | |
841 | } else if (check->leaks) { | |
842 | ret = 3; | |
e076f338 | 843 | } else { |
8599ea4c | 844 | ret = 0; |
e076f338 | 845 | } |
8599ea4c FS |
846 | |
847 | fail: | |
848 | qapi_free_ImageCheck(check); | |
26f54e9a | 849 | blk_unref(blk); |
8599ea4c | 850 | return ret; |
1585969c AL |
851 | } |
852 | ||
d4a3238a HR |
853 | typedef struct CommonBlockJobCBInfo { |
854 | BlockDriverState *bs; | |
855 | Error **errp; | |
856 | } CommonBlockJobCBInfo; | |
857 | ||
858 | static void common_block_job_cb(void *opaque, int ret) | |
859 | { | |
860 | CommonBlockJobCBInfo *cbi = opaque; | |
861 | ||
862 | if (ret < 0) { | |
863 | error_setg_errno(cbi->errp, -ret, "Block job failed"); | |
864 | } | |
d4a3238a HR |
865 | } |
866 | ||
867 | static void run_block_job(BlockJob *job, Error **errp) | |
868 | { | |
b75536c9 | 869 | AioContext *aio_context = blk_get_aio_context(job->blk); |
4172a003 | 870 | int ret = 0; |
d4a3238a | 871 | |
9e944cb4 | 872 | aio_context_acquire(aio_context); |
80fa2c75 | 873 | job_ref(&job->job); |
d4a3238a | 874 | do { |
30a5c887 | 875 | float progress = 0.0f; |
d4a3238a | 876 | aio_poll(aio_context, true); |
30a5c887 KW |
877 | if (job->job.progress_total) { |
878 | progress = (float)job->job.progress_current / | |
879 | job->job.progress_total * 100.f; | |
880 | } | |
881 | qemu_progress_print(progress, 0); | |
df956ae2 | 882 | } while (!job_is_ready(&job->job) && !job_is_completed(&job->job)); |
d4a3238a | 883 | |
dbe5e6c1 | 884 | if (!job_is_completed(&job->job)) { |
3d70ff53 | 885 | ret = job_complete_sync(&job->job, errp); |
4172a003 | 886 | } else { |
4ad35181 | 887 | ret = job->job.ret; |
4172a003 | 888 | } |
80fa2c75 | 889 | job_unref(&job->job); |
9e944cb4 | 890 | aio_context_release(aio_context); |
687fa1d8 | 891 | |
4172a003 SJ |
892 | /* publish completion progress only when success */ |
893 | if (!ret) { | |
894 | qemu_progress_print(100.f, 0); | |
895 | } | |
d4a3238a HR |
896 | } |
897 | ||
ea2384d3 FB |
898 | static int img_commit(int argc, char **argv) |
899 | { | |
661a0f71 | 900 | int c, ret, flags; |
1b22bffd | 901 | const char *filename, *fmt, *cache, *base; |
26f54e9a | 902 | BlockBackend *blk; |
d4a3238a | 903 | BlockDriverState *bs, *base_bs; |
4ef85a9c | 904 | BlockJob *job; |
687fa1d8 | 905 | bool progress = false, quiet = false, drop = false; |
ce099547 | 906 | bool writethrough; |
d4a3238a HR |
907 | Error *local_err = NULL; |
908 | CommonBlockJobCBInfo cbi; | |
eb769f74 | 909 | bool image_opts = false; |
9e944cb4 | 910 | AioContext *aio_context; |
ea2384d3 FB |
911 | |
912 | fmt = NULL; | |
661a0f71 | 913 | cache = BDRV_DEFAULT_CACHE; |
1b22bffd | 914 | base = NULL; |
ea2384d3 | 915 | for(;;) { |
3babeb15 DB |
916 | static const struct option long_options[] = { |
917 | {"help", no_argument, 0, 'h'}, | |
918 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 919 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
920 | {0, 0, 0, 0} |
921 | }; | |
c9192973 | 922 | c = getopt_long(argc, argv, ":f:ht:b:dpq", |
3babeb15 | 923 | long_options, NULL); |
b8fb60da | 924 | if (c == -1) { |
ea2384d3 | 925 | break; |
b8fb60da | 926 | } |
ea2384d3 | 927 | switch(c) { |
c9192973 SH |
928 | case ':': |
929 | missing_argument(argv[optind - 1]); | |
930 | break; | |
ef87394c | 931 | case '?': |
c9192973 SH |
932 | unrecognized_option(argv[optind - 1]); |
933 | break; | |
ea2384d3 FB |
934 | case 'h': |
935 | help(); | |
936 | break; | |
937 | case 'f': | |
938 | fmt = optarg; | |
939 | break; | |
661a0f71 FS |
940 | case 't': |
941 | cache = optarg; | |
942 | break; | |
1b22bffd HR |
943 | case 'b': |
944 | base = optarg; | |
945 | /* -b implies -d */ | |
946 | drop = true; | |
947 | break; | |
9a86fe48 HR |
948 | case 'd': |
949 | drop = true; | |
950 | break; | |
687fa1d8 HR |
951 | case 'p': |
952 | progress = true; | |
953 | break; | |
f382d43a MR |
954 | case 'q': |
955 | quiet = true; | |
956 | break; | |
3babeb15 DB |
957 | case OPTION_OBJECT: { |
958 | QemuOpts *opts; | |
959 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
960 | optarg, true); | |
961 | if (!opts) { | |
962 | return 1; | |
963 | } | |
964 | } break; | |
eb769f74 DB |
965 | case OPTION_IMAGE_OPTS: |
966 | image_opts = true; | |
967 | break; | |
ea2384d3 FB |
968 | } |
969 | } | |
687fa1d8 HR |
970 | |
971 | /* Progress is not shown in Quiet mode */ | |
972 | if (quiet) { | |
973 | progress = false; | |
974 | } | |
975 | ||
fc11eb26 | 976 | if (optind != argc - 1) { |
ac1307ab | 977 | error_exit("Expecting one image file name"); |
b8fb60da | 978 | } |
ea2384d3 FB |
979 | filename = argv[optind++]; |
980 | ||
3babeb15 DB |
981 | if (qemu_opts_foreach(&qemu_object_opts, |
982 | user_creatable_add_opts_foreach, | |
51b9b478 | 983 | NULL, NULL)) { |
3babeb15 DB |
984 | return 1; |
985 | } | |
986 | ||
9a86fe48 | 987 | flags = BDRV_O_RDWR | BDRV_O_UNMAP; |
ce099547 | 988 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
989 | if (ret < 0) { |
990 | error_report("Invalid cache option: %s", cache); | |
a3981eb9 | 991 | return 1; |
661a0f71 FS |
992 | } |
993 | ||
335e9937 FZ |
994 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
995 | false); | |
7e7d56d9 MA |
996 | if (!blk) { |
997 | return 1; | |
c2abccec | 998 | } |
7e7d56d9 MA |
999 | bs = blk_bs(blk); |
1000 | ||
687fa1d8 HR |
1001 | qemu_progress_init(progress, 1.f); |
1002 | qemu_progress_print(0.f, 100); | |
1003 | ||
1b22bffd HR |
1004 | if (base) { |
1005 | base_bs = bdrv_find_backing_image(bs, base); | |
1006 | if (!base_bs) { | |
6b33f3ae HR |
1007 | error_setg(&local_err, |
1008 | "Did not find '%s' in the backing chain of '%s'", | |
1009 | base, filename); | |
1b22bffd HR |
1010 | goto done; |
1011 | } | |
1012 | } else { | |
1013 | /* This is different from QMP, which by default uses the deepest file in | |
1014 | * the backing chain (i.e., the very base); however, the traditional | |
1015 | * behavior of qemu-img commit is using the immediate backing file. */ | |
760e0063 | 1016 | base_bs = backing_bs(bs); |
1b22bffd HR |
1017 | if (!base_bs) { |
1018 | error_setg(&local_err, "Image does not have a backing file"); | |
1019 | goto done; | |
1020 | } | |
d4a3238a HR |
1021 | } |
1022 | ||
1023 | cbi = (CommonBlockJobCBInfo){ | |
1024 | .errp = &local_err, | |
1025 | .bs = bs, | |
1026 | }; | |
1027 | ||
9e944cb4 PB |
1028 | aio_context = bdrv_get_aio_context(bs); |
1029 | aio_context_acquire(aio_context); | |
bb02b65c | 1030 | commit_active_start("commit", bs, base_bs, JOB_DEFAULT, 0, |
0db832f4 | 1031 | BLOCKDEV_ON_ERROR_REPORT, NULL, common_block_job_cb, |
78bbd910 | 1032 | &cbi, false, &local_err); |
9e944cb4 | 1033 | aio_context_release(aio_context); |
d4a3238a HR |
1034 | if (local_err) { |
1035 | goto done; | |
ea2384d3 FB |
1036 | } |
1037 | ||
3f09bfbc KW |
1038 | /* When the block job completes, the BlockBackend reference will point to |
1039 | * the old backing file. In order to avoid that the top image is already | |
1040 | * deleted, so we can still empty it afterwards, increment the reference | |
1041 | * counter here preemptively. */ | |
9a86fe48 | 1042 | if (!drop) { |
3f09bfbc | 1043 | bdrv_ref(bs); |
9a86fe48 HR |
1044 | } |
1045 | ||
4ef85a9c KW |
1046 | job = block_job_get("commit"); |
1047 | run_block_job(job, &local_err); | |
9a86fe48 HR |
1048 | if (local_err) { |
1049 | goto unref_backing; | |
1050 | } | |
1051 | ||
3f09bfbc KW |
1052 | if (!drop && bs->drv->bdrv_make_empty) { |
1053 | ret = bs->drv->bdrv_make_empty(bs); | |
9a86fe48 HR |
1054 | if (ret) { |
1055 | error_setg_errno(&local_err, -ret, "Could not empty %s", | |
1056 | filename); | |
1057 | goto unref_backing; | |
1058 | } | |
1059 | } | |
1060 | ||
1061 | unref_backing: | |
1062 | if (!drop) { | |
3f09bfbc | 1063 | bdrv_unref(bs); |
9a86fe48 | 1064 | } |
d4a3238a HR |
1065 | |
1066 | done: | |
687fa1d8 HR |
1067 | qemu_progress_end(); |
1068 | ||
26f54e9a | 1069 | blk_unref(blk); |
d4a3238a HR |
1070 | |
1071 | if (local_err) { | |
6936f299 | 1072 | error_report_err(local_err); |
c2abccec MK |
1073 | return 1; |
1074 | } | |
d4a3238a HR |
1075 | |
1076 | qprintf(quiet, "Image committed.\n"); | |
ea2384d3 FB |
1077 | return 0; |
1078 | } | |
1079 | ||
debb38a4 EB |
1080 | /* |
1081 | * Returns -1 if 'buf' contains only zeroes, otherwise the byte index | |
1082 | * of the first sector boundary within buf where the sector contains a | |
1083 | * non-zero byte. This function is robust to a buffer that is not | |
1084 | * sector-aligned. | |
1085 | */ | |
1086 | static int64_t find_nonzero(const uint8_t *buf, int64_t n) | |
1087 | { | |
1088 | int64_t i; | |
1089 | int64_t end = QEMU_ALIGN_DOWN(n, BDRV_SECTOR_SIZE); | |
1090 | ||
1091 | for (i = 0; i < end; i += BDRV_SECTOR_SIZE) { | |
1092 | if (!buffer_is_zero(buf + i, BDRV_SECTOR_SIZE)) { | |
1093 | return i; | |
1094 | } | |
1095 | } | |
1096 | if (i < n && !buffer_is_zero(buf + i, n - end)) { | |
1097 | return i; | |
1098 | } | |
1099 | return -1; | |
1100 | } | |
1101 | ||
f58c7b35 TS |
1102 | /* |
1103 | * Returns true iff the first sector pointed to by 'buf' contains at least | |
1104 | * a non-NUL byte. | |
1105 | * | |
1106 | * 'pnum' is set to the number of sectors (including and immediately following | |
1107 | * the first one) that are known to be in the same allocated/unallocated state. | |
8dcd3c9b PL |
1108 | * The function will try to align the end offset to alignment boundaries so |
1109 | * that the request will at least end aligned and consequtive requests will | |
1110 | * also start at an aligned offset. | |
f58c7b35 | 1111 | */ |
8dcd3c9b PL |
1112 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum, |
1113 | int64_t sector_num, int alignment) | |
ea2384d3 | 1114 | { |
1a6d39fd | 1115 | bool is_zero; |
8dcd3c9b | 1116 | int i, tail; |
ea2384d3 FB |
1117 | |
1118 | if (n <= 0) { | |
1119 | *pnum = 0; | |
1120 | return 0; | |
1121 | } | |
1a6d39fd | 1122 | is_zero = buffer_is_zero(buf, 512); |
ea2384d3 FB |
1123 | for(i = 1; i < n; i++) { |
1124 | buf += 512; | |
1a6d39fd | 1125 | if (is_zero != buffer_is_zero(buf, 512)) { |
ea2384d3 | 1126 | break; |
1a6d39fd | 1127 | } |
ea2384d3 | 1128 | } |
8dcd3c9b PL |
1129 | |
1130 | tail = (sector_num + i) & (alignment - 1); | |
1131 | if (tail) { | |
1132 | if (is_zero && i <= tail) { | |
1133 | /* treat unallocated areas which only consist | |
1134 | * of a small tail as allocated. */ | |
1135 | is_zero = false; | |
1136 | } | |
1137 | if (!is_zero) { | |
1138 | /* align up end offset of allocated areas. */ | |
1139 | i += alignment - tail; | |
1140 | i = MIN(i, n); | |
1141 | } else { | |
1142 | /* align down end offset of zero areas. */ | |
1143 | i -= tail; | |
1144 | } | |
1145 | } | |
ea2384d3 | 1146 | *pnum = i; |
1a6d39fd | 1147 | return !is_zero; |
ea2384d3 FB |
1148 | } |
1149 | ||
a22f123c KW |
1150 | /* |
1151 | * Like is_allocated_sectors, but if the buffer starts with a used sector, | |
1152 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids | |
1153 | * breaking up write requests for only small sparse areas. | |
1154 | */ | |
1155 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, | |
8dcd3c9b | 1156 | int min, int64_t sector_num, int alignment) |
a22f123c KW |
1157 | { |
1158 | int ret; | |
1159 | int num_checked, num_used; | |
1160 | ||
1161 | if (n < min) { | |
1162 | min = n; | |
1163 | } | |
1164 | ||
8dcd3c9b | 1165 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
a22f123c KW |
1166 | if (!ret) { |
1167 | return ret; | |
1168 | } | |
1169 | ||
1170 | num_used = *pnum; | |
1171 | buf += BDRV_SECTOR_SIZE * *pnum; | |
1172 | n -= *pnum; | |
8dcd3c9b | 1173 | sector_num += *pnum; |
a22f123c KW |
1174 | num_checked = num_used; |
1175 | ||
1176 | while (n > 0) { | |
8dcd3c9b | 1177 | ret = is_allocated_sectors(buf, n, pnum, sector_num, alignment); |
a22f123c KW |
1178 | |
1179 | buf += BDRV_SECTOR_SIZE * *pnum; | |
1180 | n -= *pnum; | |
8dcd3c9b | 1181 | sector_num += *pnum; |
a22f123c KW |
1182 | num_checked += *pnum; |
1183 | if (ret) { | |
1184 | num_used = num_checked; | |
1185 | } else if (*pnum >= min) { | |
1186 | break; | |
1187 | } | |
1188 | } | |
1189 | ||
1190 | *pnum = num_used; | |
1191 | return 1; | |
1192 | } | |
1193 | ||
3e85c6fd | 1194 | /* |
dc61cd3b EB |
1195 | * Compares two buffers sector by sector. Returns 0 if the first |
1196 | * sector of each buffer matches, non-zero otherwise. | |
3e85c6fd | 1197 | * |
dc61cd3b EB |
1198 | * pnum is set to the sector-aligned size of the buffer prefix that |
1199 | * has the same matching status as the first sector. | |
3e85c6fd | 1200 | */ |
dc61cd3b EB |
1201 | static int compare_buffers(const uint8_t *buf1, const uint8_t *buf2, |
1202 | int64_t bytes, int64_t *pnum) | |
3e85c6fd | 1203 | { |
8c1ac475 | 1204 | bool res; |
dc61cd3b | 1205 | int64_t i = MIN(bytes, BDRV_SECTOR_SIZE); |
3e85c6fd | 1206 | |
dc61cd3b | 1207 | assert(bytes > 0); |
3e85c6fd | 1208 | |
dc61cd3b EB |
1209 | res = !!memcmp(buf1, buf2, i); |
1210 | while (i < bytes) { | |
1211 | int64_t len = MIN(bytes - i, BDRV_SECTOR_SIZE); | |
3e85c6fd | 1212 | |
dc61cd3b | 1213 | if (!!memcmp(buf1 + i, buf2 + i, len) != res) { |
3e85c6fd KW |
1214 | break; |
1215 | } | |
dc61cd3b | 1216 | i += len; |
3e85c6fd KW |
1217 | } |
1218 | ||
1219 | *pnum = i; | |
1220 | return res; | |
1221 | } | |
1222 | ||
80ee15a6 | 1223 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
ea2384d3 | 1224 | |
d14ed18c MR |
1225 | /* |
1226 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) | |
1227 | * | |
0608e40e EB |
1228 | * Intended for use by 'qemu-img compare': Returns 0 in case sectors are |
1229 | * filled with 0, 1 if sectors contain non-zero data (this is a comparison | |
1230 | * failure), and 4 on error (the exit status for read errors), after emitting | |
1231 | * an error message. | |
d14ed18c | 1232 | * |
f1d3cd79 | 1233 | * @param blk: BlockBackend for the image |
c41508ed EB |
1234 | * @param offset: Starting offset to check |
1235 | * @param bytes: Number of bytes to check | |
d14ed18c MR |
1236 | * @param filename: Name of disk file we are checking (logging purpose) |
1237 | * @param buffer: Allocated buffer for storing read data | |
1238 | * @param quiet: Flag for quiet mode | |
1239 | */ | |
c41508ed EB |
1240 | static int check_empty_sectors(BlockBackend *blk, int64_t offset, |
1241 | int64_t bytes, const char *filename, | |
d14ed18c MR |
1242 | uint8_t *buffer, bool quiet) |
1243 | { | |
debb38a4 EB |
1244 | int ret = 0; |
1245 | int64_t idx; | |
1246 | ||
c41508ed | 1247 | ret = blk_pread(blk, offset, buffer, bytes); |
d14ed18c MR |
1248 | if (ret < 0) { |
1249 | error_report("Error while reading offset %" PRId64 " of %s: %s", | |
c41508ed | 1250 | offset, filename, strerror(-ret)); |
0608e40e | 1251 | return 4; |
d14ed18c | 1252 | } |
c41508ed | 1253 | idx = find_nonzero(buffer, bytes); |
debb38a4 | 1254 | if (idx >= 0) { |
d14ed18c | 1255 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
c41508ed | 1256 | offset + idx); |
d14ed18c MR |
1257 | return 1; |
1258 | } | |
1259 | ||
1260 | return 0; | |
1261 | } | |
1262 | ||
1263 | /* | |
1264 | * Compares two images. Exit codes: | |
1265 | * | |
1266 | * 0 - Images are identical | |
1267 | * 1 - Images differ | |
1268 | * >1 - Error occurred | |
1269 | */ | |
1270 | static int img_compare(int argc, char **argv) | |
1271 | { | |
40055951 | 1272 | const char *fmt1 = NULL, *fmt2 = NULL, *cache, *filename1, *filename2; |
26f54e9a | 1273 | BlockBackend *blk1, *blk2; |
d14ed18c | 1274 | BlockDriverState *bs1, *bs2; |
033d9fc2 | 1275 | int64_t total_size1, total_size2; |
d14ed18c | 1276 | uint8_t *buf1 = NULL, *buf2 = NULL; |
31826642 | 1277 | int64_t pnum1, pnum2; |
d14ed18c MR |
1278 | int allocated1, allocated2; |
1279 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ | |
1280 | bool progress = false, quiet = false, strict = false; | |
40055951 | 1281 | int flags; |
ce099547 | 1282 | bool writethrough; |
033d9fc2 EB |
1283 | int64_t total_size; |
1284 | int64_t offset = 0; | |
1285 | int64_t chunk; | |
dc61cd3b | 1286 | int c; |
d14ed18c | 1287 | uint64_t progress_base; |
eb769f74 | 1288 | bool image_opts = false; |
335e9937 | 1289 | bool force_share = false; |
d14ed18c | 1290 | |
40055951 | 1291 | cache = BDRV_DEFAULT_CACHE; |
d14ed18c | 1292 | for (;;) { |
3babeb15 DB |
1293 | static const struct option long_options[] = { |
1294 | {"help", no_argument, 0, 'h'}, | |
1295 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 1296 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 1297 | {"force-share", no_argument, 0, 'U'}, |
3babeb15 DB |
1298 | {0, 0, 0, 0} |
1299 | }; | |
335e9937 | 1300 | c = getopt_long(argc, argv, ":hf:F:T:pqsU", |
3babeb15 | 1301 | long_options, NULL); |
d14ed18c MR |
1302 | if (c == -1) { |
1303 | break; | |
1304 | } | |
1305 | switch (c) { | |
c9192973 SH |
1306 | case ':': |
1307 | missing_argument(argv[optind - 1]); | |
1308 | break; | |
d14ed18c | 1309 | case '?': |
c9192973 SH |
1310 | unrecognized_option(argv[optind - 1]); |
1311 | break; | |
d14ed18c MR |
1312 | case 'h': |
1313 | help(); | |
1314 | break; | |
1315 | case 'f': | |
1316 | fmt1 = optarg; | |
1317 | break; | |
1318 | case 'F': | |
1319 | fmt2 = optarg; | |
1320 | break; | |
40055951 HR |
1321 | case 'T': |
1322 | cache = optarg; | |
1323 | break; | |
d14ed18c MR |
1324 | case 'p': |
1325 | progress = true; | |
1326 | break; | |
1327 | case 'q': | |
1328 | quiet = true; | |
1329 | break; | |
1330 | case 's': | |
1331 | strict = true; | |
1332 | break; | |
335e9937 FZ |
1333 | case 'U': |
1334 | force_share = true; | |
1335 | break; | |
3babeb15 DB |
1336 | case OPTION_OBJECT: { |
1337 | QemuOpts *opts; | |
1338 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
1339 | optarg, true); | |
1340 | if (!opts) { | |
1341 | ret = 2; | |
1342 | goto out4; | |
1343 | } | |
1344 | } break; | |
eb769f74 DB |
1345 | case OPTION_IMAGE_OPTS: |
1346 | image_opts = true; | |
1347 | break; | |
d14ed18c MR |
1348 | } |
1349 | } | |
1350 | ||
1351 | /* Progress is not shown in Quiet mode */ | |
1352 | if (quiet) { | |
1353 | progress = false; | |
1354 | } | |
1355 | ||
1356 | ||
fc11eb26 | 1357 | if (optind != argc - 2) { |
ac1307ab | 1358 | error_exit("Expecting two image file names"); |
d14ed18c MR |
1359 | } |
1360 | filename1 = argv[optind++]; | |
1361 | filename2 = argv[optind++]; | |
1362 | ||
3babeb15 DB |
1363 | if (qemu_opts_foreach(&qemu_object_opts, |
1364 | user_creatable_add_opts_foreach, | |
51b9b478 | 1365 | NULL, NULL)) { |
3babeb15 DB |
1366 | ret = 2; |
1367 | goto out4; | |
1368 | } | |
1369 | ||
cbda016d SH |
1370 | /* Initialize before goto out */ |
1371 | qemu_progress_init(progress, 2.0); | |
1372 | ||
ce099547 KW |
1373 | flags = 0; |
1374 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
40055951 HR |
1375 | if (ret < 0) { |
1376 | error_report("Invalid source cache option: %s", cache); | |
1377 | ret = 2; | |
1378 | goto out3; | |
1379 | } | |
1380 | ||
335e9937 FZ |
1381 | blk1 = img_open(image_opts, filename1, fmt1, flags, writethrough, quiet, |
1382 | force_share); | |
7e7d56d9 | 1383 | if (!blk1) { |
d14ed18c | 1384 | ret = 2; |
7e7d56d9 | 1385 | goto out3; |
d14ed18c MR |
1386 | } |
1387 | ||
335e9937 FZ |
1388 | blk2 = img_open(image_opts, filename2, fmt2, flags, writethrough, quiet, |
1389 | force_share); | |
7e7d56d9 | 1390 | if (!blk2) { |
d14ed18c | 1391 | ret = 2; |
7e7d56d9 | 1392 | goto out2; |
d14ed18c | 1393 | } |
eb769f74 | 1394 | bs1 = blk_bs(blk1); |
7e7d56d9 | 1395 | bs2 = blk_bs(blk2); |
d14ed18c | 1396 | |
f1d3cd79 HR |
1397 | buf1 = blk_blockalign(blk1, IO_BUF_SIZE); |
1398 | buf2 = blk_blockalign(blk2, IO_BUF_SIZE); | |
033d9fc2 EB |
1399 | total_size1 = blk_getlength(blk1); |
1400 | if (total_size1 < 0) { | |
52bf1e72 | 1401 | error_report("Can't get size of %s: %s", |
033d9fc2 | 1402 | filename1, strerror(-total_size1)); |
52bf1e72 MA |
1403 | ret = 4; |
1404 | goto out; | |
1405 | } | |
033d9fc2 EB |
1406 | total_size2 = blk_getlength(blk2); |
1407 | if (total_size2 < 0) { | |
52bf1e72 | 1408 | error_report("Can't get size of %s: %s", |
033d9fc2 | 1409 | filename2, strerror(-total_size2)); |
52bf1e72 MA |
1410 | ret = 4; |
1411 | goto out; | |
1412 | } | |
033d9fc2 EB |
1413 | total_size = MIN(total_size1, total_size2); |
1414 | progress_base = MAX(total_size1, total_size2); | |
d14ed18c MR |
1415 | |
1416 | qemu_progress_print(0, 100); | |
1417 | ||
033d9fc2 | 1418 | if (strict && total_size1 != total_size2) { |
d14ed18c MR |
1419 | ret = 1; |
1420 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); | |
1421 | goto out; | |
1422 | } | |
1423 | ||
033d9fc2 | 1424 | while (offset < total_size) { |
31826642 | 1425 | int status1, status2; |
67a0fd2a | 1426 | |
033d9fc2 EB |
1427 | status1 = bdrv_block_status_above(bs1, NULL, offset, |
1428 | total_size1 - offset, &pnum1, NULL, | |
1429 | NULL); | |
25ad8e6e | 1430 | if (status1 < 0) { |
d14ed18c MR |
1431 | ret = 3; |
1432 | error_report("Sector allocation test failed for %s", filename1); | |
1433 | goto out; | |
1434 | } | |
25ad8e6e | 1435 | allocated1 = status1 & BDRV_BLOCK_ALLOCATED; |
d14ed18c | 1436 | |
033d9fc2 EB |
1437 | status2 = bdrv_block_status_above(bs2, NULL, offset, |
1438 | total_size2 - offset, &pnum2, NULL, | |
1439 | NULL); | |
25ad8e6e | 1440 | if (status2 < 0) { |
d14ed18c MR |
1441 | ret = 3; |
1442 | error_report("Sector allocation test failed for %s", filename2); | |
1443 | goto out; | |
1444 | } | |
25ad8e6e | 1445 | allocated2 = status2 & BDRV_BLOCK_ALLOCATED; |
7daddc61 EB |
1446 | |
1447 | assert(pnum1 && pnum2); | |
033d9fc2 | 1448 | chunk = MIN(pnum1, pnum2); |
d14ed18c | 1449 | |
25ad8e6e | 1450 | if (strict) { |
31826642 | 1451 | if (status1 != status2) { |
25ad8e6e FZ |
1452 | ret = 1; |
1453 | qprintf(quiet, "Strict mode: Offset %" PRId64 | |
033d9fc2 | 1454 | " block status mismatch!\n", offset); |
25ad8e6e FZ |
1455 | goto out; |
1456 | } | |
1457 | } | |
1458 | if ((status1 & BDRV_BLOCK_ZERO) && (status2 & BDRV_BLOCK_ZERO)) { | |
7daddc61 | 1459 | /* nothing to do */ |
25ad8e6e | 1460 | } else if (allocated1 == allocated2) { |
d14ed18c | 1461 | if (allocated1) { |
dc61cd3b EB |
1462 | int64_t pnum; |
1463 | ||
033d9fc2 EB |
1464 | chunk = MIN(chunk, IO_BUF_SIZE); |
1465 | ret = blk_pread(blk1, offset, buf1, chunk); | |
d14ed18c | 1466 | if (ret < 0) { |
033d9fc2 EB |
1467 | error_report("Error while reading offset %" PRId64 |
1468 | " of %s: %s", | |
1469 | offset, filename1, strerror(-ret)); | |
d14ed18c MR |
1470 | ret = 4; |
1471 | goto out; | |
1472 | } | |
033d9fc2 | 1473 | ret = blk_pread(blk2, offset, buf2, chunk); |
d14ed18c MR |
1474 | if (ret < 0) { |
1475 | error_report("Error while reading offset %" PRId64 | |
033d9fc2 EB |
1476 | " of %s: %s", |
1477 | offset, filename2, strerror(-ret)); | |
d14ed18c MR |
1478 | ret = 4; |
1479 | goto out; | |
1480 | } | |
033d9fc2 EB |
1481 | ret = compare_buffers(buf1, buf2, chunk, &pnum); |
1482 | if (ret || pnum != chunk) { | |
d14ed18c | 1483 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
033d9fc2 | 1484 | offset + (ret ? 0 : pnum)); |
36452f12 | 1485 | ret = 1; |
d14ed18c MR |
1486 | goto out; |
1487 | } | |
1488 | } | |
1489 | } else { | |
033d9fc2 | 1490 | chunk = MIN(chunk, IO_BUF_SIZE); |
d14ed18c | 1491 | if (allocated1) { |
033d9fc2 | 1492 | ret = check_empty_sectors(blk1, offset, chunk, |
d14ed18c MR |
1493 | filename1, buf1, quiet); |
1494 | } else { | |
033d9fc2 | 1495 | ret = check_empty_sectors(blk2, offset, chunk, |
d14ed18c MR |
1496 | filename2, buf1, quiet); |
1497 | } | |
1498 | if (ret) { | |
d14ed18c MR |
1499 | goto out; |
1500 | } | |
1501 | } | |
033d9fc2 EB |
1502 | offset += chunk; |
1503 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); | |
d14ed18c MR |
1504 | } |
1505 | ||
033d9fc2 | 1506 | if (total_size1 != total_size2) { |
f1d3cd79 | 1507 | BlockBackend *blk_over; |
d14ed18c MR |
1508 | const char *filename_over; |
1509 | ||
1510 | qprintf(quiet, "Warning: Image size mismatch!\n"); | |
033d9fc2 | 1511 | if (total_size1 > total_size2) { |
f1d3cd79 | 1512 | blk_over = blk1; |
d14ed18c MR |
1513 | filename_over = filename1; |
1514 | } else { | |
f1d3cd79 | 1515 | blk_over = blk2; |
d14ed18c MR |
1516 | filename_over = filename2; |
1517 | } | |
1518 | ||
033d9fc2 EB |
1519 | while (offset < progress_base) { |
1520 | ret = bdrv_block_status_above(blk_bs(blk_over), NULL, offset, | |
1521 | progress_base - offset, &chunk, | |
1522 | NULL, NULL); | |
d14ed18c MR |
1523 | if (ret < 0) { |
1524 | ret = 3; | |
1525 | error_report("Sector allocation test failed for %s", | |
1526 | filename_over); | |
1527 | goto out; | |
1528 | ||
1529 | } | |
391cb1aa | 1530 | if (ret & BDRV_BLOCK_ALLOCATED && !(ret & BDRV_BLOCK_ZERO)) { |
033d9fc2 EB |
1531 | chunk = MIN(chunk, IO_BUF_SIZE); |
1532 | ret = check_empty_sectors(blk_over, offset, chunk, | |
d14ed18c MR |
1533 | filename_over, buf1, quiet); |
1534 | if (ret) { | |
d14ed18c MR |
1535 | goto out; |
1536 | } | |
1537 | } | |
033d9fc2 EB |
1538 | offset += chunk; |
1539 | qemu_progress_print(((float) chunk / progress_base) * 100, 100); | |
d14ed18c MR |
1540 | } |
1541 | } | |
1542 | ||
1543 | qprintf(quiet, "Images are identical.\n"); | |
1544 | ret = 0; | |
1545 | ||
1546 | out: | |
d14ed18c MR |
1547 | qemu_vfree(buf1); |
1548 | qemu_vfree(buf2); | |
26f54e9a | 1549 | blk_unref(blk2); |
d14ed18c | 1550 | out2: |
26f54e9a | 1551 | blk_unref(blk1); |
d14ed18c MR |
1552 | out3: |
1553 | qemu_progress_end(); | |
3babeb15 | 1554 | out4: |
d14ed18c MR |
1555 | return ret; |
1556 | } | |
1557 | ||
690c7301 KW |
1558 | enum ImgConvertBlockStatus { |
1559 | BLK_DATA, | |
1560 | BLK_ZERO, | |
1561 | BLK_BACKING_FILE, | |
1562 | }; | |
1563 | ||
2d9187bc PL |
1564 | #define MAX_COROUTINES 16 |
1565 | ||
690c7301 KW |
1566 | typedef struct ImgConvertState { |
1567 | BlockBackend **src; | |
1568 | int64_t *src_sectors; | |
2d9187bc | 1569 | int src_num; |
690c7301 KW |
1570 | int64_t total_sectors; |
1571 | int64_t allocated_sectors; | |
2d9187bc PL |
1572 | int64_t allocated_done; |
1573 | int64_t sector_num; | |
1574 | int64_t wr_offs; | |
690c7301 KW |
1575 | enum ImgConvertBlockStatus status; |
1576 | int64_t sector_next_status; | |
1577 | BlockBackend *target; | |
1578 | bool has_zero_init; | |
1579 | bool compressed; | |
351c8eff | 1580 | bool unallocated_blocks_are_zero; |
690c7301 | 1581 | bool target_has_backing; |
351c8eff | 1582 | int64_t target_backing_sectors; /* negative if unknown */ |
2d9187bc | 1583 | bool wr_in_order; |
ee5306d0 | 1584 | bool copy_range; |
690c7301 | 1585 | int min_sparse; |
8dcd3c9b | 1586 | int alignment; |
690c7301 KW |
1587 | size_t cluster_sectors; |
1588 | size_t buf_sectors; | |
9fd77f99 | 1589 | long num_coroutines; |
2d9187bc PL |
1590 | int running_coroutines; |
1591 | Coroutine *co[MAX_COROUTINES]; | |
1592 | int64_t wait_sector_num[MAX_COROUTINES]; | |
1593 | CoMutex lock; | |
1594 | int ret; | |
690c7301 KW |
1595 | } ImgConvertState; |
1596 | ||
2d9187bc PL |
1597 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
1598 | int *src_cur, int64_t *src_cur_offset) | |
690c7301 | 1599 | { |
2d9187bc PL |
1600 | *src_cur = 0; |
1601 | *src_cur_offset = 0; | |
1602 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { | |
1603 | *src_cur_offset += s->src_sectors[*src_cur]; | |
1604 | (*src_cur)++; | |
1605 | assert(*src_cur < s->src_num); | |
690c7301 KW |
1606 | } |
1607 | } | |
1608 | ||
1609 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) | |
1610 | { | |
31826642 EB |
1611 | int64_t src_cur_offset; |
1612 | int ret, n, src_cur; | |
351c8eff | 1613 | bool post_backing_zero = false; |
690c7301 | 1614 | |
2d9187bc | 1615 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
690c7301 KW |
1616 | |
1617 | assert(s->total_sectors > sector_num); | |
1618 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); | |
1619 | ||
351c8eff HR |
1620 | if (s->target_backing_sectors >= 0) { |
1621 | if (sector_num >= s->target_backing_sectors) { | |
1622 | post_backing_zero = s->unallocated_blocks_are_zero; | |
1623 | } else if (sector_num + n > s->target_backing_sectors) { | |
1624 | /* Split requests around target_backing_sectors (because | |
1625 | * starting from there, zeros are handled differently) */ | |
1626 | n = s->target_backing_sectors - sector_num; | |
1627 | } | |
1628 | } | |
1629 | ||
690c7301 | 1630 | if (s->sector_next_status <= sector_num) { |
31826642 EB |
1631 | int64_t count = n * BDRV_SECTOR_SIZE; |
1632 | ||
9f1b92ad | 1633 | if (s->target_has_backing) { |
237d78f8 EB |
1634 | |
1635 | ret = bdrv_block_status(blk_bs(s->src[src_cur]), | |
1636 | (sector_num - src_cur_offset) * | |
1637 | BDRV_SECTOR_SIZE, | |
1638 | count, &count, NULL, NULL); | |
9f1b92ad | 1639 | } else { |
31826642 EB |
1640 | ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL, |
1641 | (sector_num - src_cur_offset) * | |
1642 | BDRV_SECTOR_SIZE, | |
1643 | count, &count, NULL, NULL); | |
9f1b92ad | 1644 | } |
690c7301 KW |
1645 | if (ret < 0) { |
1646 | return ret; | |
1647 | } | |
31826642 | 1648 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
690c7301 KW |
1649 | |
1650 | if (ret & BDRV_BLOCK_ZERO) { | |
351c8eff | 1651 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
690c7301 KW |
1652 | } else if (ret & BDRV_BLOCK_DATA) { |
1653 | s->status = BLK_DATA; | |
690c7301 | 1654 | } else { |
9f1b92ad | 1655 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
690c7301 KW |
1656 | } |
1657 | ||
1658 | s->sector_next_status = sector_num + n; | |
1659 | } | |
1660 | ||
1661 | n = MIN(n, s->sector_next_status - sector_num); | |
1662 | if (s->status == BLK_DATA) { | |
1663 | n = MIN(n, s->buf_sectors); | |
1664 | } | |
1665 | ||
1666 | /* We need to write complete clusters for compressed images, so if an | |
1667 | * unallocated area is shorter than that, we must consider the whole | |
1668 | * cluster allocated. */ | |
1669 | if (s->compressed) { | |
1670 | if (n < s->cluster_sectors) { | |
1671 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); | |
1672 | s->status = BLK_DATA; | |
1673 | } else { | |
1674 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); | |
1675 | } | |
1676 | } | |
1677 | ||
1678 | return n; | |
1679 | } | |
1680 | ||
2d9187bc PL |
1681 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
1682 | int nb_sectors, uint8_t *buf) | |
690c7301 | 1683 | { |
2d9187bc PL |
1684 | int n, ret; |
1685 | QEMUIOVector qiov; | |
1686 | struct iovec iov; | |
690c7301 | 1687 | |
690c7301 KW |
1688 | assert(nb_sectors <= s->buf_sectors); |
1689 | while (nb_sectors > 0) { | |
1690 | BlockBackend *blk; | |
2d9187bc PL |
1691 | int src_cur; |
1692 | int64_t bs_sectors, src_cur_offset; | |
690c7301 KW |
1693 | |
1694 | /* In the case of compression with multiple source files, we can get a | |
1695 | * nb_sectors that spreads into the next part. So we must be able to | |
1696 | * read across multiple BDSes for one convert_read() call. */ | |
2d9187bc PL |
1697 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
1698 | blk = s->src[src_cur]; | |
1699 | bs_sectors = s->src_sectors[src_cur]; | |
1700 | ||
1701 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); | |
1702 | iov.iov_base = buf; | |
1703 | iov.iov_len = n << BDRV_SECTOR_BITS; | |
1704 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1705 | ||
1706 | ret = blk_co_preadv( | |
1707 | blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS, | |
1708 | n << BDRV_SECTOR_BITS, &qiov, 0); | |
690c7301 KW |
1709 | if (ret < 0) { |
1710 | return ret; | |
1711 | } | |
1712 | ||
1713 | sector_num += n; | |
1714 | nb_sectors -= n; | |
1715 | buf += n * BDRV_SECTOR_SIZE; | |
1716 | } | |
1717 | ||
1718 | return 0; | |
1719 | } | |
1720 | ||
2d9187bc PL |
1721 | |
1722 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, | |
1723 | int nb_sectors, uint8_t *buf, | |
1724 | enum ImgConvertBlockStatus status) | |
690c7301 KW |
1725 | { |
1726 | int ret; | |
2d9187bc PL |
1727 | QEMUIOVector qiov; |
1728 | struct iovec iov; | |
690c7301 KW |
1729 | |
1730 | while (nb_sectors > 0) { | |
1731 | int n = nb_sectors; | |
db933fbe LC |
1732 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
1733 | ||
2d9187bc | 1734 | switch (status) { |
690c7301 KW |
1735 | case BLK_BACKING_FILE: |
1736 | /* If we have a backing file, leave clusters unallocated that are | |
1737 | * unallocated in the source image, so that the backing file is | |
1738 | * visible at the respective offset. */ | |
1739 | assert(s->target_has_backing); | |
1740 | break; | |
1741 | ||
1742 | case BLK_DATA: | |
db933fbe LC |
1743 | /* If we're told to keep the target fully allocated (-S 0) or there |
1744 | * is real non-zero data, we must write it. Otherwise we can treat | |
1745 | * it as zero sectors. | |
1746 | * Compressed clusters need to be written as a whole, so in that | |
1747 | * case we can only save the write if the buffer is completely | |
1748 | * zeroed. */ | |
690c7301 | 1749 | if (!s->min_sparse || |
db933fbe | 1750 | (!s->compressed && |
8dcd3c9b PL |
1751 | is_allocated_sectors_min(buf, n, &n, s->min_sparse, |
1752 | sector_num, s->alignment)) || | |
db933fbe LC |
1753 | (s->compressed && |
1754 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) | |
690c7301 | 1755 | { |
2d9187bc PL |
1756 | iov.iov_base = buf; |
1757 | iov.iov_len = n << BDRV_SECTOR_BITS; | |
1758 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1759 | ||
1760 | ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS, | |
db933fbe | 1761 | n << BDRV_SECTOR_BITS, &qiov, flags); |
690c7301 KW |
1762 | if (ret < 0) { |
1763 | return ret; | |
1764 | } | |
1765 | break; | |
1766 | } | |
1767 | /* fall-through */ | |
1768 | ||
1769 | case BLK_ZERO: | |
1770 | if (s->has_zero_init) { | |
db933fbe | 1771 | assert(!s->target_has_backing); |
690c7301 KW |
1772 | break; |
1773 | } | |
2d9187bc PL |
1774 | ret = blk_co_pwrite_zeroes(s->target, |
1775 | sector_num << BDRV_SECTOR_BITS, | |
1776 | n << BDRV_SECTOR_BITS, 0); | |
690c7301 KW |
1777 | if (ret < 0) { |
1778 | return ret; | |
1779 | } | |
1780 | break; | |
1781 | } | |
1782 | ||
1783 | sector_num += n; | |
1784 | nb_sectors -= n; | |
1785 | buf += n * BDRV_SECTOR_SIZE; | |
1786 | } | |
1787 | ||
1788 | return 0; | |
1789 | } | |
1790 | ||
ee5306d0 FZ |
1791 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
1792 | int nb_sectors) | |
1793 | { | |
1794 | int n, ret; | |
1795 | ||
1796 | while (nb_sectors > 0) { | |
1797 | BlockBackend *blk; | |
1798 | int src_cur; | |
1799 | int64_t bs_sectors, src_cur_offset; | |
1800 | int64_t offset; | |
1801 | ||
1802 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); | |
1803 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; | |
1804 | blk = s->src[src_cur]; | |
1805 | bs_sectors = s->src_sectors[src_cur]; | |
1806 | ||
1807 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); | |
1808 | ||
1809 | ret = blk_co_copy_range(blk, offset, s->target, | |
1810 | sector_num << BDRV_SECTOR_BITS, | |
67b51fb9 | 1811 | n << BDRV_SECTOR_BITS, 0, 0); |
ee5306d0 FZ |
1812 | if (ret < 0) { |
1813 | return ret; | |
1814 | } | |
1815 | ||
1816 | sector_num += n; | |
1817 | nb_sectors -= n; | |
1818 | } | |
1819 | return 0; | |
1820 | } | |
1821 | ||
2d9187bc | 1822 | static void coroutine_fn convert_co_do_copy(void *opaque) |
690c7301 | 1823 | { |
2d9187bc | 1824 | ImgConvertState *s = opaque; |
690c7301 | 1825 | uint8_t *buf = NULL; |
2d9187bc PL |
1826 | int ret, i; |
1827 | int index = -1; | |
1828 | ||
1829 | for (i = 0; i < s->num_coroutines; i++) { | |
1830 | if (s->co[i] == qemu_coroutine_self()) { | |
1831 | index = i; | |
1832 | break; | |
1833 | } | |
1834 | } | |
1835 | assert(index >= 0); | |
1836 | ||
1837 | s->running_coroutines++; | |
1838 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); | |
1839 | ||
1840 | while (1) { | |
1841 | int n; | |
1842 | int64_t sector_num; | |
1843 | enum ImgConvertBlockStatus status; | |
ee5306d0 | 1844 | bool copy_range; |
2d9187bc PL |
1845 | |
1846 | qemu_co_mutex_lock(&s->lock); | |
1847 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { | |
1848 | qemu_co_mutex_unlock(&s->lock); | |
b91127ed | 1849 | break; |
2d9187bc PL |
1850 | } |
1851 | n = convert_iteration_sectors(s, s->sector_num); | |
1852 | if (n < 0) { | |
1853 | qemu_co_mutex_unlock(&s->lock); | |
1854 | s->ret = n; | |
b91127ed | 1855 | break; |
2d9187bc PL |
1856 | } |
1857 | /* save current sector and allocation status to local variables */ | |
1858 | sector_num = s->sector_num; | |
1859 | status = s->status; | |
1860 | if (!s->min_sparse && s->status == BLK_ZERO) { | |
1861 | n = MIN(n, s->buf_sectors); | |
1862 | } | |
1863 | /* increment global sector counter so that other coroutines can | |
1864 | * already continue reading beyond this request */ | |
1865 | s->sector_num += n; | |
1866 | qemu_co_mutex_unlock(&s->lock); | |
1867 | ||
1868 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { | |
1869 | s->allocated_done += n; | |
1870 | qemu_progress_print(100.0 * s->allocated_done / | |
1871 | s->allocated_sectors, 0); | |
1872 | } | |
1873 | ||
ee5306d0 FZ |
1874 | retry: |
1875 | copy_range = s->copy_range && s->status == BLK_DATA; | |
1876 | if (status == BLK_DATA && !copy_range) { | |
2d9187bc PL |
1877 | ret = convert_co_read(s, sector_num, n, buf); |
1878 | if (ret < 0) { | |
1879 | error_report("error while reading sector %" PRId64 | |
1880 | ": %s", sector_num, strerror(-ret)); | |
1881 | s->ret = ret; | |
2d9187bc PL |
1882 | } |
1883 | } else if (!s->min_sparse && status == BLK_ZERO) { | |
1884 | status = BLK_DATA; | |
1885 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); | |
1886 | } | |
1887 | ||
1888 | if (s->wr_in_order) { | |
1889 | /* keep writes in order */ | |
b91127ed | 1890 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
2d9187bc PL |
1891 | s->wait_sector_num[index] = sector_num; |
1892 | qemu_coroutine_yield(); | |
1893 | } | |
1894 | s->wait_sector_num[index] = -1; | |
1895 | } | |
1896 | ||
b91127ed | 1897 | if (s->ret == -EINPROGRESS) { |
ee5306d0 FZ |
1898 | if (copy_range) { |
1899 | ret = convert_co_copy_range(s, sector_num, n); | |
1900 | if (ret) { | |
1901 | s->copy_range = false; | |
1902 | goto retry; | |
1903 | } | |
1904 | } else { | |
1905 | ret = convert_co_write(s, sector_num, n, buf, status); | |
1906 | } | |
b91127ed AN |
1907 | if (ret < 0) { |
1908 | error_report("error while writing sector %" PRId64 | |
1909 | ": %s", sector_num, strerror(-ret)); | |
1910 | s->ret = ret; | |
1911 | } | |
2d9187bc PL |
1912 | } |
1913 | ||
1914 | if (s->wr_in_order) { | |
1915 | /* reenter the coroutine that might have waited | |
1916 | * for this write to complete */ | |
1917 | s->wr_offs = sector_num + n; | |
1918 | for (i = 0; i < s->num_coroutines; i++) { | |
1919 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { | |
1920 | /* | |
1921 | * A -> B -> A cannot occur because A has | |
1922 | * s->wait_sector_num[i] == -1 during A -> B. Therefore | |
1923 | * B will never enter A during this time window. | |
1924 | */ | |
1925 | qemu_coroutine_enter(s->co[i]); | |
1926 | break; | |
1927 | } | |
1928 | } | |
1929 | } | |
1930 | } | |
1931 | ||
2d9187bc PL |
1932 | qemu_vfree(buf); |
1933 | s->co[index] = NULL; | |
1934 | s->running_coroutines--; | |
1935 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { | |
1936 | /* the convert job finished successfully */ | |
1937 | s->ret = 0; | |
1938 | } | |
1939 | } | |
1940 | ||
1941 | static int convert_do_copy(ImgConvertState *s) | |
1942 | { | |
1943 | int ret, i, n; | |
1944 | int64_t sector_num = 0; | |
690c7301 KW |
1945 | |
1946 | /* Check whether we have zero initialisation or can get it efficiently */ | |
1947 | s->has_zero_init = s->min_sparse && !s->target_has_backing | |
1948 | ? bdrv_has_zero_init(blk_bs(s->target)) | |
1949 | : false; | |
1950 | ||
1951 | if (!s->has_zero_init && !s->target_has_backing && | |
1952 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) | |
1953 | { | |
720ff280 | 1954 | ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP); |
690c7301 KW |
1955 | if (ret == 0) { |
1956 | s->has_zero_init = true; | |
1957 | } | |
1958 | } | |
1959 | ||
1960 | /* Allocate buffer for copied data. For compressed images, only one cluster | |
1961 | * can be copied at a time. */ | |
1962 | if (s->compressed) { | |
1963 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { | |
1964 | error_report("invalid cluster size"); | |
2d9187bc | 1965 | return -EINVAL; |
690c7301 KW |
1966 | } |
1967 | s->buf_sectors = s->cluster_sectors; | |
1968 | } | |
690c7301 | 1969 | |
690c7301 KW |
1970 | while (sector_num < s->total_sectors) { |
1971 | n = convert_iteration_sectors(s, sector_num); | |
1972 | if (n < 0) { | |
2d9187bc | 1973 | return n; |
690c7301 | 1974 | } |
aad15de4 HR |
1975 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
1976 | { | |
690c7301 KW |
1977 | s->allocated_sectors += n; |
1978 | } | |
1979 | sector_num += n; | |
1980 | } | |
1981 | ||
1982 | /* Do the copy */ | |
690c7301 | 1983 | s->sector_next_status = 0; |
2d9187bc | 1984 | s->ret = -EINPROGRESS; |
690c7301 | 1985 | |
2d9187bc PL |
1986 | qemu_co_mutex_init(&s->lock); |
1987 | for (i = 0; i < s->num_coroutines; i++) { | |
1988 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); | |
1989 | s->wait_sector_num[i] = -1; | |
1990 | qemu_coroutine_enter(s->co[i]); | |
1991 | } | |
690c7301 | 1992 | |
b91127ed | 1993 | while (s->running_coroutines) { |
2d9187bc | 1994 | main_loop_wait(false); |
690c7301 KW |
1995 | } |
1996 | ||
2d9187bc | 1997 | if (s->compressed && !s->ret) { |
690c7301 | 1998 | /* signal EOF to align */ |
fe5c1355 | 1999 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
690c7301 | 2000 | if (ret < 0) { |
2d9187bc | 2001 | return ret; |
690c7301 KW |
2002 | } |
2003 | } | |
2004 | ||
2d9187bc | 2005 | return s->ret; |
690c7301 KW |
2006 | } |
2007 | ||
ea2384d3 FB |
2008 | static int img_convert(int argc, char **argv) |
2009 | { | |
9fd77f99 | 2010 | int c, bs_i, flags, src_flags = 0; |
305b4c60 | 2011 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
9fd77f99 PL |
2012 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
2013 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; | |
305b4c60 | 2014 | BlockDriver *drv = NULL, *proto_drv = NULL; |
faea38e7 | 2015 | BlockDriverInfo bdi; |
9fd77f99 PL |
2016 | BlockDriverState *out_bs; |
2017 | QemuOpts *opts = NULL, *sn_opts = NULL; | |
83d0521a | 2018 | QemuOptsList *create_opts = NULL; |
efa84d43 | 2019 | char *options = NULL; |
cc84d90f | 2020 | Error *local_err = NULL; |
9fd77f99 | 2021 | bool writethrough, src_writethrough, quiet = false, image_opts = false, |
305b4c60 | 2022 | skip_create = false, progress = false, tgt_image_opts = false; |
9fd77f99 | 2023 | int64_t ret = -EINVAL; |
335e9937 | 2024 | bool force_share = false; |
9fd77f99 PL |
2025 | |
2026 | ImgConvertState s = (ImgConvertState) { | |
2027 | /* Need at least 4k of zeros for sparse detection */ | |
2028 | .min_sparse = 8, | |
ee5306d0 | 2029 | .copy_range = true, |
9fd77f99 PL |
2030 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
2031 | .wr_in_order = true, | |
2032 | .num_coroutines = 8, | |
2033 | }; | |
ea2384d3 | 2034 | |
ea2384d3 | 2035 | for(;;) { |
3babeb15 DB |
2036 | static const struct option long_options[] = { |
2037 | {"help", no_argument, 0, 'h'}, | |
2038 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 2039 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2040 | {"force-share", no_argument, 0, 'U'}, |
305b4c60 | 2041 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
3babeb15 DB |
2042 | {0, 0, 0, 0} |
2043 | }; | |
46e8d272 | 2044 | c = getopt_long(argc, argv, ":hf:O:B:co:l:S:pt:T:qnm:WU", |
3babeb15 | 2045 | long_options, NULL); |
b8fb60da | 2046 | if (c == -1) { |
ea2384d3 | 2047 | break; |
b8fb60da | 2048 | } |
ea2384d3 | 2049 | switch(c) { |
c9192973 SH |
2050 | case ':': |
2051 | missing_argument(argv[optind - 1]); | |
2052 | break; | |
ef87394c | 2053 | case '?': |
c9192973 SH |
2054 | unrecognized_option(argv[optind - 1]); |
2055 | break; | |
ea2384d3 FB |
2056 | case 'h': |
2057 | help(); | |
2058 | break; | |
2059 | case 'f': | |
2060 | fmt = optarg; | |
2061 | break; | |
2062 | case 'O': | |
2063 | out_fmt = optarg; | |
2064 | break; | |
f58c7b35 TS |
2065 | case 'B': |
2066 | out_baseimg = optarg; | |
2067 | break; | |
ea2384d3 | 2068 | case 'c': |
9fd77f99 | 2069 | s.compressed = true; |
ee5306d0 | 2070 | s.copy_range = false; |
ea2384d3 | 2071 | break; |
efa84d43 | 2072 | case 'o': |
2dc8328b KW |
2073 | if (!is_valid_option_list(optarg)) { |
2074 | error_report("Invalid option list: %s", optarg); | |
64bb01aa | 2075 | goto fail_getopt; |
2dc8328b KW |
2076 | } |
2077 | if (!options) { | |
2078 | options = g_strdup(optarg); | |
2079 | } else { | |
2080 | char *old_options = options; | |
2081 | options = g_strdup_printf("%s,%s", options, optarg); | |
2082 | g_free(old_options); | |
2083 | } | |
efa84d43 | 2084 | break; |
ef80654d WX |
2085 | case 'l': |
2086 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
70b94331 MA |
2087 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
2088 | optarg, false); | |
ef80654d WX |
2089 | if (!sn_opts) { |
2090 | error_report("Failed in parsing snapshot param '%s'", | |
2091 | optarg); | |
64bb01aa | 2092 | goto fail_getopt; |
ef80654d WX |
2093 | } |
2094 | } else { | |
2095 | snapshot_name = optarg; | |
2096 | } | |
2097 | break; | |
a22f123c KW |
2098 | case 'S': |
2099 | { | |
2100 | int64_t sval; | |
606caa0a MA |
2101 | |
2102 | sval = cvtnum(optarg); | |
2103 | if (sval < 0) { | |
a22f123c | 2104 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
64bb01aa | 2105 | goto fail_getopt; |
a22f123c KW |
2106 | } |
2107 | ||
9fd77f99 | 2108 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
ee5306d0 | 2109 | s.copy_range = false; |
a22f123c KW |
2110 | break; |
2111 | } | |
6b837bc4 | 2112 | case 'p': |
9fd77f99 | 2113 | progress = true; |
6b837bc4 | 2114 | break; |
661a0f71 FS |
2115 | case 't': |
2116 | cache = optarg; | |
2117 | break; | |
40055951 HR |
2118 | case 'T': |
2119 | src_cache = optarg; | |
2120 | break; | |
f382d43a MR |
2121 | case 'q': |
2122 | quiet = true; | |
2123 | break; | |
b2e10493 | 2124 | case 'n': |
9fd77f99 | 2125 | skip_create = true; |
b2e10493 | 2126 | break; |
2d9187bc | 2127 | case 'm': |
9fd77f99 PL |
2128 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
2129 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { | |
2d9187bc PL |
2130 | error_report("Invalid number of coroutines. Allowed number of" |
2131 | " coroutines is between 1 and %d", MAX_COROUTINES); | |
2d9187bc PL |
2132 | goto fail_getopt; |
2133 | } | |
2134 | break; | |
2135 | case 'W': | |
9fd77f99 | 2136 | s.wr_in_order = false; |
2d9187bc | 2137 | break; |
335e9937 FZ |
2138 | case 'U': |
2139 | force_share = true; | |
2140 | break; | |
3258b911 HR |
2141 | case OPTION_OBJECT: { |
2142 | QemuOpts *object_opts; | |
2143 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2144 | optarg, true); | |
2145 | if (!object_opts) { | |
3babeb15 DB |
2146 | goto fail_getopt; |
2147 | } | |
2148 | break; | |
3258b911 | 2149 | } |
eb769f74 DB |
2150 | case OPTION_IMAGE_OPTS: |
2151 | image_opts = true; | |
2152 | break; | |
305b4c60 DB |
2153 | case OPTION_TARGET_IMAGE_OPTS: |
2154 | tgt_image_opts = true; | |
2155 | break; | |
ea2384d3 FB |
2156 | } |
2157 | } | |
3b46e624 | 2158 | |
305b4c60 DB |
2159 | if (!out_fmt && !tgt_image_opts) { |
2160 | out_fmt = "raw"; | |
2161 | } | |
2162 | ||
3babeb15 DB |
2163 | if (qemu_opts_foreach(&qemu_object_opts, |
2164 | user_creatable_add_opts_foreach, | |
51b9b478 | 2165 | NULL, NULL)) { |
3babeb15 DB |
2166 | goto fail_getopt; |
2167 | } | |
2168 | ||
305b4c60 DB |
2169 | if (tgt_image_opts && !skip_create) { |
2170 | error_report("--target-image-opts requires use of -n flag"); | |
2171 | goto fail_getopt; | |
2172 | } | |
2173 | ||
9fd77f99 PL |
2174 | s.src_num = argc - optind - 1; |
2175 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; | |
f58c7b35 | 2176 | |
2dc8328b | 2177 | if (options && has_help_option(options)) { |
305b4c60 DB |
2178 | if (out_fmt) { |
2179 | ret = print_block_option_help(out_filename, out_fmt); | |
2180 | goto fail_getopt; | |
2181 | } else { | |
2182 | error_report("Option help requires a format be specified"); | |
2183 | goto fail_getopt; | |
2184 | } | |
4ac8aacd JS |
2185 | } |
2186 | ||
9fd77f99 PL |
2187 | if (s.src_num < 1) { |
2188 | error_report("Must specify image file name"); | |
2189 | goto fail_getopt; | |
a283cb6e KW |
2190 | } |
2191 | ||
2192 | ||
9fd77f99 | 2193 | /* ret is still -EINVAL until here */ |
ce099547 | 2194 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
40055951 HR |
2195 | if (ret < 0) { |
2196 | error_report("Invalid source cache option: %s", src_cache); | |
9fd77f99 | 2197 | goto fail_getopt; |
40055951 HR |
2198 | } |
2199 | ||
9fd77f99 PL |
2200 | /* Initialize before goto out */ |
2201 | if (quiet) { | |
2202 | progress = false; | |
2203 | } | |
2204 | qemu_progress_init(progress, 1.0); | |
6b837bc4 JS |
2205 | qemu_progress_print(0, 100); |
2206 | ||
9fd77f99 PL |
2207 | s.src = g_new0(BlockBackend *, s.src_num); |
2208 | s.src_sectors = g_new(int64_t, s.src_num); | |
926c2d23 | 2209 | |
9fd77f99 PL |
2210 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
2211 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], | |
335e9937 FZ |
2212 | fmt, src_flags, src_writethrough, quiet, |
2213 | force_share); | |
9fd77f99 | 2214 | if (!s.src[bs_i]) { |
c2abccec MK |
2215 | ret = -1; |
2216 | goto out; | |
2217 | } | |
9fd77f99 PL |
2218 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
2219 | if (s.src_sectors[bs_i] < 0) { | |
52bf1e72 | 2220 | error_report("Could not get size of %s: %s", |
9fd77f99 | 2221 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
52bf1e72 MA |
2222 | ret = -1; |
2223 | goto out; | |
2224 | } | |
9fd77f99 | 2225 | s.total_sectors += s.src_sectors[bs_i]; |
926c2d23 | 2226 | } |
ea2384d3 | 2227 | |
ef80654d | 2228 | if (sn_opts) { |
9fd77f99 | 2229 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
10d6eda1 PM |
2230 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
2231 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
2232 | &local_err); | |
ef80654d | 2233 | } else if (snapshot_name != NULL) { |
9fd77f99 | 2234 | if (s.src_num > 1) { |
6daf194d | 2235 | error_report("No support for concatenating multiple snapshot"); |
51ef6727 | 2236 | ret = -1; |
2237 | goto out; | |
2238 | } | |
7b4c4781 | 2239 | |
9fd77f99 PL |
2240 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
2241 | &local_err); | |
ef80654d | 2242 | } |
84d18f06 | 2243 | if (local_err) { |
c29b77f9 | 2244 | error_reportf_err(local_err, "Failed to load snapshot: "); |
ef80654d WX |
2245 | ret = -1; |
2246 | goto out; | |
51ef6727 | 2247 | } |
2248 | ||
305b4c60 DB |
2249 | if (!skip_create) { |
2250 | /* Find driver and parse its options */ | |
2251 | drv = bdrv_find_format(out_fmt); | |
2252 | if (!drv) { | |
2253 | error_report("Unknown file format '%s'", out_fmt); | |
2254 | ret = -1; | |
2255 | goto out; | |
2256 | } | |
efa84d43 | 2257 | |
305b4c60 DB |
2258 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
2259 | if (!proto_drv) { | |
2260 | error_report_err(local_err); | |
2261 | ret = -1; | |
2262 | goto out; | |
2263 | } | |
b50cbabc | 2264 | |
2e024cde HR |
2265 | if (!drv->create_opts) { |
2266 | error_report("Format driver '%s' does not support image creation", | |
2267 | drv->format_name); | |
2268 | ret = -1; | |
2269 | goto out; | |
2270 | } | |
f75613cf | 2271 | |
2e024cde HR |
2272 | if (!proto_drv->create_opts) { |
2273 | error_report("Protocol driver '%s' does not support image creation", | |
2274 | proto_drv->format_name); | |
2275 | ret = -1; | |
2276 | goto out; | |
2277 | } | |
f75613cf | 2278 | |
2e024cde HR |
2279 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
2280 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
db08adf5 | 2281 | |
2e024cde | 2282 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
dc523cd3 MA |
2283 | if (options) { |
2284 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
2285 | if (local_err) { | |
97a2ca7a | 2286 | error_report_err(local_err); |
dc523cd3 MA |
2287 | ret = -1; |
2288 | goto out; | |
2289 | } | |
2e024cde | 2290 | } |
efa84d43 | 2291 | |
9fd77f99 | 2292 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512, |
39101f25 | 2293 | &error_abort); |
2e024cde HR |
2294 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
2295 | if (ret < 0) { | |
2296 | goto out; | |
2297 | } | |
c2abccec | 2298 | } |
efa84d43 | 2299 | |
a18953fb | 2300 | /* Get backing file name if -o backing_file was used */ |
83d0521a | 2301 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
a18953fb | 2302 | if (out_baseimg_param) { |
83d0521a | 2303 | out_baseimg = out_baseimg_param; |
a18953fb | 2304 | } |
9fd77f99 | 2305 | s.target_has_backing = (bool) out_baseimg; |
a18953fb | 2306 | |
48758a84 HR |
2307 | if (s.src_num > 1 && out_baseimg) { |
2308 | error_report("Having a backing file for the target makes no sense when " | |
2309 | "concatenating multiple input images"); | |
2310 | ret = -1; | |
2311 | goto out; | |
2312 | } | |
2313 | ||
efa84d43 | 2314 | /* Check if compression is supported */ |
9fd77f99 | 2315 | if (s.compressed) { |
83d0521a CL |
2316 | bool encryption = |
2317 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); | |
0cb8d47b DB |
2318 | const char *encryptfmt = |
2319 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); | |
83d0521a CL |
2320 | const char *preallocation = |
2321 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); | |
efa84d43 | 2322 | |
305b4c60 | 2323 | if (drv && !drv->bdrv_co_pwritev_compressed) { |
15654a6d | 2324 | error_report("Compression not supported for this file format"); |
c2abccec MK |
2325 | ret = -1; |
2326 | goto out; | |
efa84d43 KW |
2327 | } |
2328 | ||
0cb8d47b | 2329 | if (encryption || encryptfmt) { |
15654a6d JS |
2330 | error_report("Compression and encryption not supported at " |
2331 | "the same time"); | |
c2abccec MK |
2332 | ret = -1; |
2333 | goto out; | |
efa84d43 | 2334 | } |
41521fa4 | 2335 | |
83d0521a CL |
2336 | if (preallocation |
2337 | && strcmp(preallocation, "off")) | |
41521fa4 KW |
2338 | { |
2339 | error_report("Compression and preallocation not supported at " | |
2340 | "the same time"); | |
2341 | ret = -1; | |
2342 | goto out; | |
2343 | } | |
efa84d43 KW |
2344 | } |
2345 | ||
b2e10493 AD |
2346 | if (!skip_create) { |
2347 | /* Create the new image */ | |
c282e1fd | 2348 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
b2e10493 | 2349 | if (ret < 0) { |
c29b77f9 MA |
2350 | error_reportf_err(local_err, "%s: error while converting %s: ", |
2351 | out_filename, out_fmt); | |
b2e10493 | 2352 | goto out; |
ea2384d3 FB |
2353 | } |
2354 | } | |
3b46e624 | 2355 | |
9fd77f99 | 2356 | flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; |
ce099547 | 2357 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
2358 | if (ret < 0) { |
2359 | error_report("Invalid cache option: %s", cache); | |
bb9cd2ee | 2360 | goto out; |
661a0f71 FS |
2361 | } |
2362 | ||
305b4c60 DB |
2363 | if (skip_create) { |
2364 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, | |
2365 | flags, writethrough, quiet, false); | |
2366 | } else { | |
2367 | /* TODO ultimately we should allow --target-image-opts | |
2368 | * to be used even when -n is not given. | |
2369 | * That has to wait for bdrv_create to be improved | |
2370 | * to allow filenames in option syntax | |
2371 | */ | |
29cf9336 DB |
2372 | s.target = img_open_new_file(out_filename, opts, out_fmt, |
2373 | flags, writethrough, quiet, false); | |
305b4c60 | 2374 | } |
9fd77f99 | 2375 | if (!s.target) { |
c2abccec MK |
2376 | ret = -1; |
2377 | goto out; | |
2378 | } | |
9fd77f99 | 2379 | out_bs = blk_bs(s.target); |
ea2384d3 | 2380 | |
305b4c60 DB |
2381 | if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) { |
2382 | error_report("Compression not supported for this file format"); | |
2383 | ret = -1; | |
2384 | goto out; | |
2385 | } | |
2386 | ||
5def6b80 | 2387 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
f2521c90 PL |
2388 | * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB) |
2389 | * as maximum. */ | |
9fd77f99 PL |
2390 | s.buf_sectors = MIN(32768, |
2391 | MAX(s.buf_sectors, | |
2392 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, | |
2393 | out_bs->bl.pdiscard_alignment >> | |
2394 | BDRV_SECTOR_BITS))); | |
f2521c90 | 2395 | |
8dcd3c9b PL |
2396 | /* try to align the write requests to the destination to avoid unnecessary |
2397 | * RMW cycles. */ | |
2398 | s.alignment = MAX(pow2floor(s.min_sparse), | |
2399 | DIV_ROUND_UP(out_bs->bl.request_alignment, | |
2400 | BDRV_SECTOR_SIZE)); | |
2401 | assert(is_power_of_2(s.alignment)); | |
2402 | ||
b2e10493 | 2403 | if (skip_create) { |
9fd77f99 | 2404 | int64_t output_sectors = blk_nb_sectors(s.target); |
43716fa8 | 2405 | if (output_sectors < 0) { |
eec5eb42 | 2406 | error_report("unable to get output image length: %s", |
43716fa8 | 2407 | strerror(-output_sectors)); |
b2e10493 AD |
2408 | ret = -1; |
2409 | goto out; | |
9fd77f99 | 2410 | } else if (output_sectors < s.total_sectors) { |
b2e10493 AD |
2411 | error_report("output file is smaller than input file"); |
2412 | ret = -1; | |
2413 | goto out; | |
2414 | } | |
2415 | } | |
2416 | ||
351c8eff HR |
2417 | if (s.target_has_backing) { |
2418 | /* Errors are treated as "backing length unknown" (which means | |
2419 | * s.target_backing_sectors has to be negative, which it will | |
2420 | * be automatically). The backing file length is used only | |
2421 | * for optimizations, so such a case is not fatal. */ | |
2422 | s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs); | |
2423 | } else { | |
2424 | s.target_backing_sectors = -1; | |
2425 | } | |
2426 | ||
24f833cd PL |
2427 | ret = bdrv_get_info(out_bs, &bdi); |
2428 | if (ret < 0) { | |
9fd77f99 | 2429 | if (s.compressed) { |
15654a6d | 2430 | error_report("could not get block driver info"); |
c2abccec MK |
2431 | goto out; |
2432 | } | |
24f833cd | 2433 | } else { |
9fd77f99 PL |
2434 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
2435 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; | |
351c8eff | 2436 | s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero; |
9fd77f99 | 2437 | } |
802c3d4c | 2438 | |
9fd77f99 | 2439 | ret = convert_do_copy(&s); |
c2abccec | 2440 | out: |
13c28af8 PL |
2441 | if (!ret) { |
2442 | qemu_progress_print(100, 0); | |
2443 | } | |
6b837bc4 | 2444 | qemu_progress_end(); |
83d0521a CL |
2445 | qemu_opts_del(opts); |
2446 | qemu_opts_free(create_opts); | |
fbf28a43 | 2447 | qemu_opts_del(sn_opts); |
9fd77f99 PL |
2448 | blk_unref(s.target); |
2449 | if (s.src) { | |
2450 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { | |
2451 | blk_unref(s.src[bs_i]); | |
26f54e9a | 2452 | } |
9fd77f99 | 2453 | g_free(s.src); |
26f54e9a | 2454 | } |
9fd77f99 | 2455 | g_free(s.src_sectors); |
64bb01aa KW |
2456 | fail_getopt: |
2457 | g_free(options); | |
2458 | ||
9fd77f99 | 2459 | return !!ret; |
ea2384d3 FB |
2460 | } |
2461 | ||
57d1a2b6 | 2462 | |
faea38e7 FB |
2463 | static void dump_snapshots(BlockDriverState *bs) |
2464 | { | |
2465 | QEMUSnapshotInfo *sn_tab, *sn; | |
2466 | int nb_sns, i; | |
faea38e7 FB |
2467 | |
2468 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
2469 | if (nb_sns <= 0) | |
2470 | return; | |
2471 | printf("Snapshot list:\n"); | |
5b917044 WX |
2472 | bdrv_snapshot_dump(fprintf, stdout, NULL); |
2473 | printf("\n"); | |
faea38e7 FB |
2474 | for(i = 0; i < nb_sns; i++) { |
2475 | sn = &sn_tab[i]; | |
5b917044 WX |
2476 | bdrv_snapshot_dump(fprintf, stdout, sn); |
2477 | printf("\n"); | |
faea38e7 | 2478 | } |
7267c094 | 2479 | g_free(sn_tab); |
faea38e7 FB |
2480 | } |
2481 | ||
9699bf0d SH |
2482 | static void dump_json_image_info_list(ImageInfoList *list) |
2483 | { | |
9699bf0d | 2484 | QString *str; |
9699bf0d | 2485 | QObject *obj; |
7d5e199a | 2486 | Visitor *v = qobject_output_visitor_new(&obj); |
3b098d56 EB |
2487 | |
2488 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); | |
2489 | visit_complete(v, &obj); | |
9699bf0d SH |
2490 | str = qobject_to_json_pretty(obj); |
2491 | assert(str != NULL); | |
2492 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 2493 | qobject_unref(obj); |
3b098d56 | 2494 | visit_free(v); |
cb3e7f08 | 2495 | qobject_unref(str); |
9699bf0d SH |
2496 | } |
2497 | ||
c054b3fd BC |
2498 | static void dump_json_image_info(ImageInfo *info) |
2499 | { | |
c054b3fd | 2500 | QString *str; |
c054b3fd | 2501 | QObject *obj; |
7d5e199a | 2502 | Visitor *v = qobject_output_visitor_new(&obj); |
3b098d56 EB |
2503 | |
2504 | visit_type_ImageInfo(v, NULL, &info, &error_abort); | |
2505 | visit_complete(v, &obj); | |
c054b3fd BC |
2506 | str = qobject_to_json_pretty(obj); |
2507 | assert(str != NULL); | |
2508 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 2509 | qobject_unref(obj); |
3b098d56 | 2510 | visit_free(v); |
cb3e7f08 | 2511 | qobject_unref(str); |
c054b3fd BC |
2512 | } |
2513 | ||
9699bf0d SH |
2514 | static void dump_human_image_info_list(ImageInfoList *list) |
2515 | { | |
2516 | ImageInfoList *elem; | |
2517 | bool delim = false; | |
2518 | ||
2519 | for (elem = list; elem; elem = elem->next) { | |
2520 | if (delim) { | |
2521 | printf("\n"); | |
2522 | } | |
2523 | delim = true; | |
2524 | ||
5b917044 | 2525 | bdrv_image_info_dump(fprintf, stdout, elem->value); |
9699bf0d SH |
2526 | } |
2527 | } | |
2528 | ||
2529 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) | |
2530 | { | |
2531 | return strcmp(a, b) == 0; | |
2532 | } | |
2533 | ||
2534 | /** | |
2535 | * Open an image file chain and return an ImageInfoList | |
2536 | * | |
2537 | * @filename: topmost image filename | |
2538 | * @fmt: topmost image format (may be NULL to autodetect) | |
2539 | * @chain: true - enumerate entire backing file chain | |
2540 | * false - only topmost image file | |
2541 | * | |
2542 | * Returns a list of ImageInfo objects or NULL if there was an error opening an | |
2543 | * image file. If there was an error a message will have been printed to | |
2544 | * stderr. | |
2545 | */ | |
eb769f74 DB |
2546 | static ImageInfoList *collect_image_info_list(bool image_opts, |
2547 | const char *filename, | |
9699bf0d | 2548 | const char *fmt, |
335e9937 | 2549 | bool chain, bool force_share) |
9699bf0d SH |
2550 | { |
2551 | ImageInfoList *head = NULL; | |
2552 | ImageInfoList **last = &head; | |
2553 | GHashTable *filenames; | |
43526ec8 | 2554 | Error *err = NULL; |
9699bf0d SH |
2555 | |
2556 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); | |
2557 | ||
2558 | while (filename) { | |
26f54e9a | 2559 | BlockBackend *blk; |
9699bf0d SH |
2560 | BlockDriverState *bs; |
2561 | ImageInfo *info; | |
2562 | ImageInfoList *elem; | |
2563 | ||
2564 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { | |
2565 | error_report("Backing file '%s' creates an infinite loop.", | |
2566 | filename); | |
2567 | goto err; | |
2568 | } | |
2569 | g_hash_table_insert(filenames, (gpointer)filename, NULL); | |
2570 | ||
efaa7c4e | 2571 | blk = img_open(image_opts, filename, fmt, |
335e9937 FZ |
2572 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
2573 | force_share); | |
7e7d56d9 | 2574 | if (!blk) { |
9699bf0d SH |
2575 | goto err; |
2576 | } | |
7e7d56d9 | 2577 | bs = blk_bs(blk); |
9699bf0d | 2578 | |
43526ec8 | 2579 | bdrv_query_image_info(bs, &info, &err); |
84d18f06 | 2580 | if (err) { |
565f65d2 | 2581 | error_report_err(err); |
26f54e9a | 2582 | blk_unref(blk); |
43526ec8 | 2583 | goto err; |
fb0ed453 | 2584 | } |
9699bf0d SH |
2585 | |
2586 | elem = g_new0(ImageInfoList, 1); | |
2587 | elem->value = info; | |
2588 | *last = elem; | |
2589 | last = &elem->next; | |
2590 | ||
26f54e9a | 2591 | blk_unref(blk); |
9699bf0d SH |
2592 | |
2593 | filename = fmt = NULL; | |
2594 | if (chain) { | |
2595 | if (info->has_full_backing_filename) { | |
2596 | filename = info->full_backing_filename; | |
2597 | } else if (info->has_backing_filename) { | |
92d617ab JS |
2598 | error_report("Could not determine absolute backing filename," |
2599 | " but backing filename '%s' present", | |
2600 | info->backing_filename); | |
2601 | goto err; | |
9699bf0d SH |
2602 | } |
2603 | if (info->has_backing_filename_format) { | |
2604 | fmt = info->backing_filename_format; | |
2605 | } | |
2606 | } | |
2607 | } | |
2608 | g_hash_table_destroy(filenames); | |
2609 | return head; | |
2610 | ||
2611 | err: | |
2612 | qapi_free_ImageInfoList(head); | |
2613 | g_hash_table_destroy(filenames); | |
2614 | return NULL; | |
2615 | } | |
2616 | ||
c054b3fd BC |
2617 | static int img_info(int argc, char **argv) |
2618 | { | |
2619 | int c; | |
2620 | OutputFormat output_format = OFORMAT_HUMAN; | |
9699bf0d | 2621 | bool chain = false; |
c054b3fd | 2622 | const char *filename, *fmt, *output; |
9699bf0d | 2623 | ImageInfoList *list; |
eb769f74 | 2624 | bool image_opts = false; |
335e9937 | 2625 | bool force_share = false; |
c054b3fd | 2626 | |
ea2384d3 | 2627 | fmt = NULL; |
c054b3fd | 2628 | output = NULL; |
ea2384d3 | 2629 | for(;;) { |
c054b3fd BC |
2630 | int option_index = 0; |
2631 | static const struct option long_options[] = { | |
2632 | {"help", no_argument, 0, 'h'}, | |
2633 | {"format", required_argument, 0, 'f'}, | |
2634 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
9699bf0d | 2635 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
3babeb15 | 2636 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 2637 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2638 | {"force-share", no_argument, 0, 'U'}, |
c054b3fd BC |
2639 | {0, 0, 0, 0} |
2640 | }; | |
335e9937 | 2641 | c = getopt_long(argc, argv, ":f:hU", |
c054b3fd | 2642 | long_options, &option_index); |
b8fb60da | 2643 | if (c == -1) { |
ea2384d3 | 2644 | break; |
b8fb60da | 2645 | } |
ea2384d3 | 2646 | switch(c) { |
c9192973 SH |
2647 | case ':': |
2648 | missing_argument(argv[optind - 1]); | |
2649 | break; | |
ef87394c | 2650 | case '?': |
c9192973 SH |
2651 | unrecognized_option(argv[optind - 1]); |
2652 | break; | |
ea2384d3 FB |
2653 | case 'h': |
2654 | help(); | |
2655 | break; | |
2656 | case 'f': | |
2657 | fmt = optarg; | |
2658 | break; | |
335e9937 FZ |
2659 | case 'U': |
2660 | force_share = true; | |
2661 | break; | |
c054b3fd BC |
2662 | case OPTION_OUTPUT: |
2663 | output = optarg; | |
2664 | break; | |
9699bf0d SH |
2665 | case OPTION_BACKING_CHAIN: |
2666 | chain = true; | |
2667 | break; | |
3babeb15 DB |
2668 | case OPTION_OBJECT: { |
2669 | QemuOpts *opts; | |
2670 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2671 | optarg, true); | |
2672 | if (!opts) { | |
2673 | return 1; | |
2674 | } | |
2675 | } break; | |
eb769f74 DB |
2676 | case OPTION_IMAGE_OPTS: |
2677 | image_opts = true; | |
2678 | break; | |
ea2384d3 FB |
2679 | } |
2680 | } | |
fc11eb26 | 2681 | if (optind != argc - 1) { |
ac1307ab | 2682 | error_exit("Expecting one image file name"); |
b8fb60da | 2683 | } |
ea2384d3 FB |
2684 | filename = argv[optind++]; |
2685 | ||
c054b3fd BC |
2686 | if (output && !strcmp(output, "json")) { |
2687 | output_format = OFORMAT_JSON; | |
2688 | } else if (output && !strcmp(output, "human")) { | |
2689 | output_format = OFORMAT_HUMAN; | |
2690 | } else if (output) { | |
2691 | error_report("--output must be used with human or json as argument."); | |
c2abccec MK |
2692 | return 1; |
2693 | } | |
c054b3fd | 2694 | |
3babeb15 DB |
2695 | if (qemu_opts_foreach(&qemu_object_opts, |
2696 | user_creatable_add_opts_foreach, | |
51b9b478 | 2697 | NULL, NULL)) { |
3babeb15 DB |
2698 | return 1; |
2699 | } | |
2700 | ||
335e9937 FZ |
2701 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
2702 | force_share); | |
9699bf0d | 2703 | if (!list) { |
c2abccec | 2704 | return 1; |
faea38e7 | 2705 | } |
c054b3fd | 2706 | |
c054b3fd BC |
2707 | switch (output_format) { |
2708 | case OFORMAT_HUMAN: | |
9699bf0d | 2709 | dump_human_image_info_list(list); |
c054b3fd BC |
2710 | break; |
2711 | case OFORMAT_JSON: | |
9699bf0d SH |
2712 | if (chain) { |
2713 | dump_json_image_info_list(list); | |
2714 | } else { | |
2715 | dump_json_image_info(list->value); | |
2716 | } | |
c054b3fd | 2717 | break; |
faea38e7 | 2718 | } |
c054b3fd | 2719 | |
9699bf0d | 2720 | qapi_free_ImageInfoList(list); |
ea2384d3 FB |
2721 | return 0; |
2722 | } | |
2723 | ||
4c93a13b PB |
2724 | static void dump_map_entry(OutputFormat output_format, MapEntry *e, |
2725 | MapEntry *next) | |
2726 | { | |
2727 | switch (output_format) { | |
2728 | case OFORMAT_HUMAN: | |
16b0d555 | 2729 | if (e->data && !e->has_offset) { |
4c93a13b PB |
2730 | error_report("File contains external, encrypted or compressed clusters."); |
2731 | exit(1); | |
2732 | } | |
16b0d555 | 2733 | if (e->data && !e->zero) { |
4c93a13b | 2734 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
16b0d555 FZ |
2735 | e->start, e->length, |
2736 | e->has_offset ? e->offset : 0, | |
2737 | e->has_filename ? e->filename : ""); | |
4c93a13b PB |
2738 | } |
2739 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. | |
2740 | * Modify the flags here to allow more coalescing. | |
2741 | */ | |
16b0d555 FZ |
2742 | if (next && (!next->data || next->zero)) { |
2743 | next->data = false; | |
2744 | next->zero = true; | |
4c93a13b PB |
2745 | } |
2746 | break; | |
2747 | case OFORMAT_JSON: | |
16b0d555 FZ |
2748 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," |
2749 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", | |
4c93a13b PB |
2750 | (e->start == 0 ? "[" : ",\n"), |
2751 | e->start, e->length, e->depth, | |
16b0d555 FZ |
2752 | e->zero ? "true" : "false", |
2753 | e->data ? "true" : "false"); | |
2754 | if (e->has_offset) { | |
c745bfb4 | 2755 | printf(", \"offset\": %"PRId64"", e->offset); |
4c93a13b PB |
2756 | } |
2757 | putchar('}'); | |
2758 | ||
2759 | if (!next) { | |
2760 | printf("]\n"); | |
2761 | } | |
2762 | break; | |
2763 | } | |
2764 | } | |
2765 | ||
5e344dd8 EB |
2766 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
2767 | int64_t bytes, MapEntry *e) | |
4c93a13b | 2768 | { |
237d78f8 | 2769 | int ret; |
4c93a13b | 2770 | int depth; |
67a0fd2a | 2771 | BlockDriverState *file; |
2875645b | 2772 | bool has_offset; |
237d78f8 | 2773 | int64_t map; |
4c93a13b PB |
2774 | |
2775 | /* As an optimization, we could cache the current range of unallocated | |
2776 | * clusters in each file of the chain, and avoid querying the same | |
2777 | * range repeatedly. | |
2778 | */ | |
2779 | ||
2780 | depth = 0; | |
2781 | for (;;) { | |
237d78f8 | 2782 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
4c93a13b PB |
2783 | if (ret < 0) { |
2784 | return ret; | |
2785 | } | |
237d78f8 | 2786 | assert(bytes); |
4c93a13b PB |
2787 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
2788 | break; | |
2789 | } | |
760e0063 | 2790 | bs = backing_bs(bs); |
4c93a13b PB |
2791 | if (bs == NULL) { |
2792 | ret = 0; | |
2793 | break; | |
2794 | } | |
2795 | ||
2796 | depth++; | |
2797 | } | |
2798 | ||
2875645b JS |
2799 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
2800 | ||
2801 | *e = (MapEntry) { | |
5e344dd8 | 2802 | .start = offset, |
237d78f8 | 2803 | .length = bytes, |
2875645b JS |
2804 | .data = !!(ret & BDRV_BLOCK_DATA), |
2805 | .zero = !!(ret & BDRV_BLOCK_ZERO), | |
237d78f8 | 2806 | .offset = map, |
2875645b JS |
2807 | .has_offset = has_offset, |
2808 | .depth = depth, | |
2809 | .has_filename = file && has_offset, | |
2810 | .filename = file && has_offset ? file->filename : NULL, | |
2811 | }; | |
2812 | ||
4c93a13b PB |
2813 | return 0; |
2814 | } | |
2815 | ||
16b0d555 FZ |
2816 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
2817 | { | |
2818 | if (curr->length == 0) { | |
2819 | return false; | |
2820 | } | |
2821 | if (curr->zero != next->zero || | |
2822 | curr->data != next->data || | |
2823 | curr->depth != next->depth || | |
2824 | curr->has_filename != next->has_filename || | |
2825 | curr->has_offset != next->has_offset) { | |
2826 | return false; | |
2827 | } | |
2828 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { | |
2829 | return false; | |
2830 | } | |
2831 | if (curr->has_offset && curr->offset + curr->length != next->offset) { | |
2832 | return false; | |
2833 | } | |
2834 | return true; | |
2835 | } | |
2836 | ||
4c93a13b PB |
2837 | static int img_map(int argc, char **argv) |
2838 | { | |
2839 | int c; | |
2840 | OutputFormat output_format = OFORMAT_HUMAN; | |
26f54e9a | 2841 | BlockBackend *blk; |
4c93a13b PB |
2842 | BlockDriverState *bs; |
2843 | const char *filename, *fmt, *output; | |
2844 | int64_t length; | |
2845 | MapEntry curr = { .length = 0 }, next; | |
2846 | int ret = 0; | |
eb769f74 | 2847 | bool image_opts = false; |
335e9937 | 2848 | bool force_share = false; |
4c93a13b PB |
2849 | |
2850 | fmt = NULL; | |
2851 | output = NULL; | |
2852 | for (;;) { | |
2853 | int option_index = 0; | |
2854 | static const struct option long_options[] = { | |
2855 | {"help", no_argument, 0, 'h'}, | |
2856 | {"format", required_argument, 0, 'f'}, | |
2857 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
3babeb15 | 2858 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 2859 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2860 | {"force-share", no_argument, 0, 'U'}, |
4c93a13b PB |
2861 | {0, 0, 0, 0} |
2862 | }; | |
335e9937 | 2863 | c = getopt_long(argc, argv, ":f:hU", |
4c93a13b PB |
2864 | long_options, &option_index); |
2865 | if (c == -1) { | |
2866 | break; | |
2867 | } | |
2868 | switch (c) { | |
c9192973 SH |
2869 | case ':': |
2870 | missing_argument(argv[optind - 1]); | |
2871 | break; | |
4c93a13b | 2872 | case '?': |
c9192973 SH |
2873 | unrecognized_option(argv[optind - 1]); |
2874 | break; | |
4c93a13b PB |
2875 | case 'h': |
2876 | help(); | |
2877 | break; | |
2878 | case 'f': | |
2879 | fmt = optarg; | |
2880 | break; | |
335e9937 FZ |
2881 | case 'U': |
2882 | force_share = true; | |
2883 | break; | |
4c93a13b PB |
2884 | case OPTION_OUTPUT: |
2885 | output = optarg; | |
2886 | break; | |
3babeb15 DB |
2887 | case OPTION_OBJECT: { |
2888 | QemuOpts *opts; | |
2889 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2890 | optarg, true); | |
2891 | if (!opts) { | |
2892 | return 1; | |
2893 | } | |
2894 | } break; | |
eb769f74 DB |
2895 | case OPTION_IMAGE_OPTS: |
2896 | image_opts = true; | |
2897 | break; | |
4c93a13b PB |
2898 | } |
2899 | } | |
ac1307ab FZ |
2900 | if (optind != argc - 1) { |
2901 | error_exit("Expecting one image file name"); | |
4c93a13b | 2902 | } |
ac1307ab | 2903 | filename = argv[optind]; |
4c93a13b PB |
2904 | |
2905 | if (output && !strcmp(output, "json")) { | |
2906 | output_format = OFORMAT_JSON; | |
2907 | } else if (output && !strcmp(output, "human")) { | |
2908 | output_format = OFORMAT_HUMAN; | |
2909 | } else if (output) { | |
2910 | error_report("--output must be used with human or json as argument."); | |
2911 | return 1; | |
2912 | } | |
2913 | ||
3babeb15 DB |
2914 | if (qemu_opts_foreach(&qemu_object_opts, |
2915 | user_creatable_add_opts_foreach, | |
51b9b478 | 2916 | NULL, NULL)) { |
3babeb15 DB |
2917 | return 1; |
2918 | } | |
2919 | ||
335e9937 | 2920 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
7e7d56d9 MA |
2921 | if (!blk) { |
2922 | return 1; | |
4c93a13b | 2923 | } |
7e7d56d9 | 2924 | bs = blk_bs(blk); |
4c93a13b PB |
2925 | |
2926 | if (output_format == OFORMAT_HUMAN) { | |
2927 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); | |
2928 | } | |
2929 | ||
f1d3cd79 | 2930 | length = blk_getlength(blk); |
4c93a13b | 2931 | while (curr.start + curr.length < length) { |
5e344dd8 EB |
2932 | int64_t offset = curr.start + curr.length; |
2933 | int64_t n; | |
4c93a13b PB |
2934 | |
2935 | /* Probe up to 1 GiB at a time. */ | |
e0b371ed | 2936 | n = MIN(1 << 30, length - offset); |
5e344dd8 | 2937 | ret = get_block_status(bs, offset, n, &next); |
4c93a13b PB |
2938 | |
2939 | if (ret < 0) { | |
2940 | error_report("Could not read file metadata: %s", strerror(-ret)); | |
2941 | goto out; | |
2942 | } | |
2943 | ||
16b0d555 | 2944 | if (entry_mergeable(&curr, &next)) { |
4c93a13b PB |
2945 | curr.length += next.length; |
2946 | continue; | |
2947 | } | |
2948 | ||
2949 | if (curr.length > 0) { | |
2950 | dump_map_entry(output_format, &curr, &next); | |
2951 | } | |
2952 | curr = next; | |
2953 | } | |
2954 | ||
2955 | dump_map_entry(output_format, &curr, NULL); | |
2956 | ||
2957 | out: | |
26f54e9a | 2958 | blk_unref(blk); |
4c93a13b PB |
2959 | return ret < 0; |
2960 | } | |
2961 | ||
f7b4a940 AL |
2962 | #define SNAPSHOT_LIST 1 |
2963 | #define SNAPSHOT_CREATE 2 | |
2964 | #define SNAPSHOT_APPLY 3 | |
2965 | #define SNAPSHOT_DELETE 4 | |
2966 | ||
153859be | 2967 | static int img_snapshot(int argc, char **argv) |
f7b4a940 | 2968 | { |
26f54e9a | 2969 | BlockBackend *blk; |
f7b4a940 AL |
2970 | BlockDriverState *bs; |
2971 | QEMUSnapshotInfo sn; | |
2972 | char *filename, *snapshot_name = NULL; | |
c2abccec | 2973 | int c, ret = 0, bdrv_oflags; |
f7b4a940 AL |
2974 | int action = 0; |
2975 | qemu_timeval tv; | |
f382d43a | 2976 | bool quiet = false; |
a89d89d3 | 2977 | Error *err = NULL; |
eb769f74 | 2978 | bool image_opts = false; |
335e9937 | 2979 | bool force_share = false; |
f7b4a940 | 2980 | |
ce099547 | 2981 | bdrv_oflags = BDRV_O_RDWR; |
f7b4a940 AL |
2982 | /* Parse commandline parameters */ |
2983 | for(;;) { | |
3babeb15 DB |
2984 | static const struct option long_options[] = { |
2985 | {"help", no_argument, 0, 'h'}, | |
2986 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 2987 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2988 | {"force-share", no_argument, 0, 'U'}, |
3babeb15 DB |
2989 | {0, 0, 0, 0} |
2990 | }; | |
335e9937 | 2991 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
3babeb15 | 2992 | long_options, NULL); |
b8fb60da | 2993 | if (c == -1) { |
f7b4a940 | 2994 | break; |
b8fb60da | 2995 | } |
f7b4a940 | 2996 | switch(c) { |
c9192973 SH |
2997 | case ':': |
2998 | missing_argument(argv[optind - 1]); | |
2999 | break; | |
ef87394c | 3000 | case '?': |
c9192973 SH |
3001 | unrecognized_option(argv[optind - 1]); |
3002 | break; | |
f7b4a940 AL |
3003 | case 'h': |
3004 | help(); | |
153859be | 3005 | return 0; |
f7b4a940 AL |
3006 | case 'l': |
3007 | if (action) { | |
ac1307ab | 3008 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3009 | return 0; |
f7b4a940 AL |
3010 | } |
3011 | action = SNAPSHOT_LIST; | |
f5edb014 | 3012 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
f7b4a940 AL |
3013 | break; |
3014 | case 'a': | |
3015 | if (action) { | |
ac1307ab | 3016 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3017 | return 0; |
f7b4a940 AL |
3018 | } |
3019 | action = SNAPSHOT_APPLY; | |
3020 | snapshot_name = optarg; | |
3021 | break; | |
3022 | case 'c': | |
3023 | if (action) { | |
ac1307ab | 3024 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3025 | return 0; |
f7b4a940 AL |
3026 | } |
3027 | action = SNAPSHOT_CREATE; | |
3028 | snapshot_name = optarg; | |
3029 | break; | |
3030 | case 'd': | |
3031 | if (action) { | |
ac1307ab | 3032 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3033 | return 0; |
f7b4a940 AL |
3034 | } |
3035 | action = SNAPSHOT_DELETE; | |
3036 | snapshot_name = optarg; | |
3037 | break; | |
f382d43a MR |
3038 | case 'q': |
3039 | quiet = true; | |
3040 | break; | |
335e9937 FZ |
3041 | case 'U': |
3042 | force_share = true; | |
3043 | break; | |
3babeb15 DB |
3044 | case OPTION_OBJECT: { |
3045 | QemuOpts *opts; | |
3046 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3047 | optarg, true); | |
3048 | if (!opts) { | |
3049 | return 1; | |
3050 | } | |
3051 | } break; | |
eb769f74 DB |
3052 | case OPTION_IMAGE_OPTS: |
3053 | image_opts = true; | |
3054 | break; | |
f7b4a940 AL |
3055 | } |
3056 | } | |
3057 | ||
fc11eb26 | 3058 | if (optind != argc - 1) { |
ac1307ab | 3059 | error_exit("Expecting one image file name"); |
b8fb60da | 3060 | } |
f7b4a940 AL |
3061 | filename = argv[optind++]; |
3062 | ||
3babeb15 DB |
3063 | if (qemu_opts_foreach(&qemu_object_opts, |
3064 | user_creatable_add_opts_foreach, | |
51b9b478 | 3065 | NULL, NULL)) { |
3babeb15 DB |
3066 | return 1; |
3067 | } | |
3068 | ||
f7b4a940 | 3069 | /* Open the image */ |
335e9937 FZ |
3070 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
3071 | force_share); | |
7e7d56d9 MA |
3072 | if (!blk) { |
3073 | return 1; | |
c2abccec | 3074 | } |
7e7d56d9 | 3075 | bs = blk_bs(blk); |
f7b4a940 AL |
3076 | |
3077 | /* Perform the requested action */ | |
3078 | switch(action) { | |
3079 | case SNAPSHOT_LIST: | |
3080 | dump_snapshots(bs); | |
3081 | break; | |
3082 | ||
3083 | case SNAPSHOT_CREATE: | |
3084 | memset(&sn, 0, sizeof(sn)); | |
3085 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); | |
3086 | ||
3087 | qemu_gettimeofday(&tv); | |
3088 | sn.date_sec = tv.tv_sec; | |
3089 | sn.date_nsec = tv.tv_usec * 1000; | |
3090 | ||
3091 | ret = bdrv_snapshot_create(bs, &sn); | |
b8fb60da | 3092 | if (ret) { |
15654a6d | 3093 | error_report("Could not create snapshot '%s': %d (%s)", |
f7b4a940 | 3094 | snapshot_name, ret, strerror(-ret)); |
b8fb60da | 3095 | } |
f7b4a940 AL |
3096 | break; |
3097 | ||
3098 | case SNAPSHOT_APPLY: | |
0b62bcbc | 3099 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
b8fb60da | 3100 | if (ret) { |
0b62bcbc KW |
3101 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
3102 | snapshot_name); | |
b8fb60da | 3103 | } |
f7b4a940 AL |
3104 | break; |
3105 | ||
3106 | case SNAPSHOT_DELETE: | |
a89d89d3 | 3107 | bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); |
84d18f06 | 3108 | if (err) { |
c29b77f9 MA |
3109 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
3110 | snapshot_name); | |
a89d89d3 | 3111 | ret = 1; |
b8fb60da | 3112 | } |
f7b4a940 AL |
3113 | break; |
3114 | } | |
3115 | ||
3116 | /* Cleanup */ | |
26f54e9a | 3117 | blk_unref(blk); |
c2abccec MK |
3118 | if (ret) { |
3119 | return 1; | |
3120 | } | |
153859be | 3121 | return 0; |
f7b4a940 AL |
3122 | } |
3123 | ||
3e85c6fd KW |
3124 | static int img_rebase(int argc, char **argv) |
3125 | { | |
26f54e9a | 3126 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
396374ca PB |
3127 | uint8_t *buf_old = NULL; |
3128 | uint8_t *buf_new = NULL; | |
f1d3cd79 | 3129 | BlockDriverState *bs = NULL; |
3e85c6fd | 3130 | char *filename; |
40055951 HR |
3131 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
3132 | int c, flags, src_flags, ret; | |
ce099547 | 3133 | bool writethrough, src_writethrough; |
3e85c6fd | 3134 | int unsafe = 0; |
335e9937 | 3135 | bool force_share = false; |
6b837bc4 | 3136 | int progress = 0; |
f382d43a | 3137 | bool quiet = false; |
34b5d2c6 | 3138 | Error *local_err = NULL; |
eb769f74 | 3139 | bool image_opts = false; |
3e85c6fd KW |
3140 | |
3141 | /* Parse commandline parameters */ | |
e53dbee0 | 3142 | fmt = NULL; |
661a0f71 | 3143 | cache = BDRV_DEFAULT_CACHE; |
40055951 | 3144 | src_cache = BDRV_DEFAULT_CACHE; |
3e85c6fd KW |
3145 | out_baseimg = NULL; |
3146 | out_basefmt = NULL; | |
3e85c6fd | 3147 | for(;;) { |
3babeb15 DB |
3148 | static const struct option long_options[] = { |
3149 | {"help", no_argument, 0, 'h'}, | |
3150 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3151 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 3152 | {"force-share", no_argument, 0, 'U'}, |
3babeb15 DB |
3153 | {0, 0, 0, 0} |
3154 | }; | |
335e9937 | 3155 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
3babeb15 | 3156 | long_options, NULL); |
b8fb60da | 3157 | if (c == -1) { |
3e85c6fd | 3158 | break; |
b8fb60da | 3159 | } |
3e85c6fd | 3160 | switch(c) { |
c9192973 SH |
3161 | case ':': |
3162 | missing_argument(argv[optind - 1]); | |
3163 | break; | |
ef87394c | 3164 | case '?': |
c9192973 SH |
3165 | unrecognized_option(argv[optind - 1]); |
3166 | break; | |
3e85c6fd KW |
3167 | case 'h': |
3168 | help(); | |
3169 | return 0; | |
e53dbee0 KW |
3170 | case 'f': |
3171 | fmt = optarg; | |
3172 | break; | |
3e85c6fd KW |
3173 | case 'F': |
3174 | out_basefmt = optarg; | |
3175 | break; | |
3176 | case 'b': | |
3177 | out_baseimg = optarg; | |
3178 | break; | |
3179 | case 'u': | |
3180 | unsafe = 1; | |
3181 | break; | |
6b837bc4 JS |
3182 | case 'p': |
3183 | progress = 1; | |
3184 | break; | |
661a0f71 FS |
3185 | case 't': |
3186 | cache = optarg; | |
3187 | break; | |
40055951 HR |
3188 | case 'T': |
3189 | src_cache = optarg; | |
3190 | break; | |
f382d43a MR |
3191 | case 'q': |
3192 | quiet = true; | |
3193 | break; | |
3babeb15 DB |
3194 | case OPTION_OBJECT: { |
3195 | QemuOpts *opts; | |
3196 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3197 | optarg, true); | |
3198 | if (!opts) { | |
3199 | return 1; | |
3200 | } | |
3201 | } break; | |
eb769f74 DB |
3202 | case OPTION_IMAGE_OPTS: |
3203 | image_opts = true; | |
3204 | break; | |
335e9937 FZ |
3205 | case 'U': |
3206 | force_share = true; | |
3207 | break; | |
3e85c6fd KW |
3208 | } |
3209 | } | |
3210 | ||
f382d43a MR |
3211 | if (quiet) { |
3212 | progress = 0; | |
3213 | } | |
3214 | ||
ac1307ab FZ |
3215 | if (optind != argc - 1) { |
3216 | error_exit("Expecting one image file name"); | |
3217 | } | |
3218 | if (!unsafe && !out_baseimg) { | |
3219 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); | |
b8fb60da | 3220 | } |
3e85c6fd KW |
3221 | filename = argv[optind++]; |
3222 | ||
3babeb15 DB |
3223 | if (qemu_opts_foreach(&qemu_object_opts, |
3224 | user_creatable_add_opts_foreach, | |
51b9b478 | 3225 | NULL, NULL)) { |
3babeb15 DB |
3226 | return 1; |
3227 | } | |
3228 | ||
6b837bc4 JS |
3229 | qemu_progress_init(progress, 2.0); |
3230 | qemu_progress_print(0, 100); | |
3231 | ||
661a0f71 | 3232 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
ce099547 | 3233 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
3234 | if (ret < 0) { |
3235 | error_report("Invalid cache option: %s", cache); | |
40ed35a3 | 3236 | goto out; |
661a0f71 FS |
3237 | } |
3238 | ||
ce099547 KW |
3239 | src_flags = 0; |
3240 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); | |
40055951 HR |
3241 | if (ret < 0) { |
3242 | error_report("Invalid source cache option: %s", src_cache); | |
40ed35a3 | 3243 | goto out; |
40055951 HR |
3244 | } |
3245 | ||
ce099547 KW |
3246 | /* The source files are opened read-only, don't care about WCE */ |
3247 | assert((src_flags & BDRV_O_RDWR) == 0); | |
3248 | (void) src_writethrough; | |
3249 | ||
3e85c6fd KW |
3250 | /* |
3251 | * Open the images. | |
3252 | * | |
3253 | * Ignore the old backing file for unsafe rebase in case we want to correct | |
3254 | * the reference to a renamed or moved backing file. | |
3255 | */ | |
335e9937 FZ |
3256 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
3257 | false); | |
7e7d56d9 | 3258 | if (!blk) { |
40ed35a3 SH |
3259 | ret = -1; |
3260 | goto out; | |
c2abccec | 3261 | } |
7e7d56d9 | 3262 | bs = blk_bs(blk); |
3e85c6fd | 3263 | |
3e85c6fd | 3264 | if (out_basefmt != NULL) { |
644483d9 | 3265 | if (bdrv_find_format(out_basefmt) == NULL) { |
15654a6d | 3266 | error_report("Invalid format name: '%s'", out_basefmt); |
c2abccec MK |
3267 | ret = -1; |
3268 | goto out; | |
3e85c6fd KW |
3269 | } |
3270 | } | |
3271 | ||
3272 | /* For safe rebasing we need to compare old and new backing file */ | |
40ed35a3 | 3273 | if (!unsafe) { |
9a29e18f | 3274 | char backing_name[PATH_MAX]; |
644483d9 HR |
3275 | QDict *options = NULL; |
3276 | ||
3277 | if (bs->backing_format[0] != '\0') { | |
3278 | options = qdict_new(); | |
46f5ac20 | 3279 | qdict_put_str(options, "driver", bs->backing_format); |
644483d9 | 3280 | } |
3e85c6fd | 3281 | |
335e9937 FZ |
3282 | if (force_share) { |
3283 | if (!options) { | |
3284 | options = qdict_new(); | |
3285 | } | |
579cf1d1 | 3286 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
335e9937 | 3287 | } |
3e85c6fd | 3288 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
efaa7c4e | 3289 | blk_old_backing = blk_new_open(backing_name, NULL, |
644483d9 HR |
3290 | options, src_flags, &local_err); |
3291 | if (!blk_old_backing) { | |
c29b77f9 MA |
3292 | error_reportf_err(local_err, |
3293 | "Could not open old backing file '%s': ", | |
3294 | backing_name); | |
e84a0dd5 | 3295 | ret = -1; |
c2abccec | 3296 | goto out; |
3e85c6fd | 3297 | } |
644483d9 | 3298 | |
a616673d | 3299 | if (out_baseimg[0]) { |
d16699b6 HR |
3300 | const char *overlay_filename; |
3301 | char *out_real_path; | |
3302 | ||
335e9937 | 3303 | options = qdict_new(); |
644483d9 | 3304 | if (out_basefmt) { |
46f5ac20 | 3305 | qdict_put_str(options, "driver", out_basefmt); |
335e9937 FZ |
3306 | } |
3307 | if (force_share) { | |
3308 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); | |
644483d9 HR |
3309 | } |
3310 | ||
d16699b6 HR |
3311 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
3312 | : bs->filename; | |
3313 | out_real_path = g_malloc(PATH_MAX); | |
3314 | ||
3315 | bdrv_get_full_backing_filename_from_filename(overlay_filename, | |
3316 | out_baseimg, | |
3317 | out_real_path, | |
3318 | PATH_MAX, | |
3319 | &local_err); | |
3320 | if (local_err) { | |
3321 | error_reportf_err(local_err, | |
3322 | "Could not resolve backing filename: "); | |
3323 | ret = -1; | |
3324 | g_free(out_real_path); | |
3325 | goto out; | |
3326 | } | |
3327 | ||
3328 | blk_new_backing = blk_new_open(out_real_path, NULL, | |
644483d9 | 3329 | options, src_flags, &local_err); |
d16699b6 | 3330 | g_free(out_real_path); |
644483d9 | 3331 | if (!blk_new_backing) { |
c29b77f9 MA |
3332 | error_reportf_err(local_err, |
3333 | "Could not open new backing file '%s': ", | |
3334 | out_baseimg); | |
e84a0dd5 | 3335 | ret = -1; |
a616673d AB |
3336 | goto out; |
3337 | } | |
3e85c6fd KW |
3338 | } |
3339 | } | |
3340 | ||
3341 | /* | |
3342 | * Check each unallocated cluster in the COW file. If it is unallocated, | |
3343 | * accesses go to the backing file. We must therefore compare this cluster | |
3344 | * in the old and new backing file, and if they differ we need to copy it | |
3345 | * from the old backing file into the COW file. | |
3346 | * | |
3347 | * If qemu-img crashes during this step, no harm is done. The content of | |
3348 | * the image is the same as the original one at any time. | |
3349 | */ | |
3350 | if (!unsafe) { | |
41536287 EB |
3351 | int64_t size; |
3352 | int64_t old_backing_size; | |
3353 | int64_t new_backing_size = 0; | |
3354 | uint64_t offset; | |
3355 | int64_t n; | |
1f710495 | 3356 | float local_progress = 0; |
d6771bfa | 3357 | |
f1d3cd79 HR |
3358 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
3359 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); | |
3e85c6fd | 3360 | |
41536287 EB |
3361 | size = blk_getlength(blk); |
3362 | if (size < 0) { | |
52bf1e72 | 3363 | error_report("Could not get size of '%s': %s", |
41536287 | 3364 | filename, strerror(-size)); |
52bf1e72 MA |
3365 | ret = -1; |
3366 | goto out; | |
3367 | } | |
41536287 EB |
3368 | old_backing_size = blk_getlength(blk_old_backing); |
3369 | if (old_backing_size < 0) { | |
9a29e18f | 3370 | char backing_name[PATH_MAX]; |
52bf1e72 MA |
3371 | |
3372 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); | |
3373 | error_report("Could not get size of '%s': %s", | |
41536287 | 3374 | backing_name, strerror(-old_backing_size)); |
52bf1e72 MA |
3375 | ret = -1; |
3376 | goto out; | |
3377 | } | |
f1d3cd79 | 3378 | if (blk_new_backing) { |
41536287 EB |
3379 | new_backing_size = blk_getlength(blk_new_backing); |
3380 | if (new_backing_size < 0) { | |
52bf1e72 | 3381 | error_report("Could not get size of '%s': %s", |
41536287 | 3382 | out_baseimg, strerror(-new_backing_size)); |
52bf1e72 MA |
3383 | ret = -1; |
3384 | goto out; | |
3385 | } | |
a616673d | 3386 | } |
3e85c6fd | 3387 | |
41536287 EB |
3388 | if (size != 0) { |
3389 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); | |
1f710495 KW |
3390 | } |
3391 | ||
41536287 EB |
3392 | for (offset = 0; offset < size; offset += n) { |
3393 | /* How many bytes can we handle with the next read? */ | |
3394 | n = MIN(IO_BUF_SIZE, size - offset); | |
3e85c6fd KW |
3395 | |
3396 | /* If the cluster is allocated, we don't need to take action */ | |
41536287 | 3397 | ret = bdrv_is_allocated(bs, offset, n, &n); |
d663640c PB |
3398 | if (ret < 0) { |
3399 | error_report("error while reading image metadata: %s", | |
3400 | strerror(-ret)); | |
3401 | goto out; | |
3402 | } | |
cc60e327 | 3403 | if (ret) { |
3e85c6fd KW |
3404 | continue; |
3405 | } | |
3406 | ||
87a1b3e3 KW |
3407 | /* |
3408 | * Read old and new backing file and take into consideration that | |
3409 | * backing files may be smaller than the COW image. | |
3410 | */ | |
41536287 EB |
3411 | if (offset >= old_backing_size) { |
3412 | memset(buf_old, 0, n); | |
87a1b3e3 | 3413 | } else { |
41536287 EB |
3414 | if (offset + n > old_backing_size) { |
3415 | n = old_backing_size - offset; | |
87a1b3e3 KW |
3416 | } |
3417 | ||
41536287 | 3418 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
87a1b3e3 KW |
3419 | if (ret < 0) { |
3420 | error_report("error while reading from old backing file"); | |
3421 | goto out; | |
3422 | } | |
3e85c6fd | 3423 | } |
87a1b3e3 | 3424 | |
41536287 EB |
3425 | if (offset >= new_backing_size || !blk_new_backing) { |
3426 | memset(buf_new, 0, n); | |
87a1b3e3 | 3427 | } else { |
41536287 EB |
3428 | if (offset + n > new_backing_size) { |
3429 | n = new_backing_size - offset; | |
87a1b3e3 KW |
3430 | } |
3431 | ||
41536287 | 3432 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
87a1b3e3 KW |
3433 | if (ret < 0) { |
3434 | error_report("error while reading from new backing file"); | |
3435 | goto out; | |
3436 | } | |
3e85c6fd KW |
3437 | } |
3438 | ||
3439 | /* If they differ, we need to write to the COW file */ | |
3440 | uint64_t written = 0; | |
3441 | ||
41536287 | 3442 | while (written < n) { |
dc61cd3b | 3443 | int64_t pnum; |
3e85c6fd | 3444 | |
41536287 EB |
3445 | if (compare_buffers(buf_old + written, buf_new + written, |
3446 | n - written, &pnum)) | |
3e85c6fd | 3447 | { |
41536287 | 3448 | ret = blk_pwrite(blk, offset + written, |
dc61cd3b | 3449 | buf_old + written, pnum, 0); |
3e85c6fd | 3450 | if (ret < 0) { |
15654a6d | 3451 | error_report("Error while writing to COW image: %s", |
3e85c6fd | 3452 | strerror(-ret)); |
c2abccec | 3453 | goto out; |
3e85c6fd KW |
3454 | } |
3455 | } | |
3456 | ||
3457 | written += pnum; | |
3458 | } | |
6b837bc4 | 3459 | qemu_progress_print(local_progress, 100); |
3e85c6fd KW |
3460 | } |
3461 | } | |
3462 | ||
3463 | /* | |
3464 | * Change the backing file. All clusters that are different from the old | |
3465 | * backing file are overwritten in the COW file now, so the visible content | |
3466 | * doesn't change when we switch the backing file. | |
3467 | */ | |
a616673d AB |
3468 | if (out_baseimg && *out_baseimg) { |
3469 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); | |
3470 | } else { | |
3471 | ret = bdrv_change_backing_file(bs, NULL, NULL); | |
3472 | } | |
3473 | ||
3e85c6fd | 3474 | if (ret == -ENOSPC) { |
15654a6d JS |
3475 | error_report("Could not change the backing file to '%s': No " |
3476 | "space left in the file header", out_baseimg); | |
3e85c6fd | 3477 | } else if (ret < 0) { |
15654a6d | 3478 | error_report("Could not change the backing file to '%s': %s", |
3e85c6fd KW |
3479 | out_baseimg, strerror(-ret)); |
3480 | } | |
3481 | ||
6b837bc4 | 3482 | qemu_progress_print(100, 0); |
3e85c6fd KW |
3483 | /* |
3484 | * TODO At this point it is possible to check if any clusters that are | |
3485 | * allocated in the COW file are the same in the backing file. If so, they | |
3486 | * could be dropped from the COW file. Don't do this before switching the | |
3487 | * backing file, in case of a crash this would lead to corruption. | |
3488 | */ | |
c2abccec | 3489 | out: |
6b837bc4 | 3490 | qemu_progress_end(); |
3e85c6fd KW |
3491 | /* Cleanup */ |
3492 | if (!unsafe) { | |
26f54e9a | 3493 | blk_unref(blk_old_backing); |
26f54e9a | 3494 | blk_unref(blk_new_backing); |
3e85c6fd | 3495 | } |
396374ca PB |
3496 | qemu_vfree(buf_old); |
3497 | qemu_vfree(buf_new); | |
3e85c6fd | 3498 | |
26f54e9a | 3499 | blk_unref(blk); |
c2abccec MK |
3500 | if (ret) { |
3501 | return 1; | |
3502 | } | |
3e85c6fd KW |
3503 | return 0; |
3504 | } | |
3505 | ||
ae6b0ed6 SH |
3506 | static int img_resize(int argc, char **argv) |
3507 | { | |
6750e795 | 3508 | Error *err = NULL; |
ae6b0ed6 SH |
3509 | int c, ret, relative; |
3510 | const char *filename, *fmt, *size; | |
5279b303 | 3511 | int64_t n, total_size, current_size, new_size; |
f382d43a | 3512 | bool quiet = false; |
26f54e9a | 3513 | BlockBackend *blk = NULL; |
dc5f690b | 3514 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
20caf0f7 | 3515 | QemuOpts *param; |
3babeb15 | 3516 | |
20caf0f7 DXW |
3517 | static QemuOptsList resize_options = { |
3518 | .name = "resize_options", | |
3519 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), | |
3520 | .desc = { | |
3521 | { | |
3522 | .name = BLOCK_OPT_SIZE, | |
3523 | .type = QEMU_OPT_SIZE, | |
3524 | .help = "Virtual disk size" | |
3525 | }, { | |
3526 | /* end of list */ | |
3527 | } | |
ae6b0ed6 | 3528 | }, |
ae6b0ed6 | 3529 | }; |
eb769f74 | 3530 | bool image_opts = false; |
4ffca890 | 3531 | bool shrink = false; |
ae6b0ed6 | 3532 | |
e80fec7f KW |
3533 | /* Remove size from argv manually so that negative numbers are not treated |
3534 | * as options by getopt. */ | |
3535 | if (argc < 3) { | |
ac1307ab | 3536 | error_exit("Not enough arguments"); |
e80fec7f KW |
3537 | return 1; |
3538 | } | |
3539 | ||
3540 | size = argv[--argc]; | |
3541 | ||
3542 | /* Parse getopt arguments */ | |
ae6b0ed6 SH |
3543 | fmt = NULL; |
3544 | for(;;) { | |
3babeb15 DB |
3545 | static const struct option long_options[] = { |
3546 | {"help", no_argument, 0, 'h'}, | |
3547 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3548 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
dc5f690b | 3549 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
4ffca890 | 3550 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
3babeb15 DB |
3551 | {0, 0, 0, 0} |
3552 | }; | |
c9192973 | 3553 | c = getopt_long(argc, argv, ":f:hq", |
3babeb15 | 3554 | long_options, NULL); |
ae6b0ed6 SH |
3555 | if (c == -1) { |
3556 | break; | |
3557 | } | |
3558 | switch(c) { | |
c9192973 SH |
3559 | case ':': |
3560 | missing_argument(argv[optind - 1]); | |
3561 | break; | |
ef87394c | 3562 | case '?': |
c9192973 SH |
3563 | unrecognized_option(argv[optind - 1]); |
3564 | break; | |
ae6b0ed6 SH |
3565 | case 'h': |
3566 | help(); | |
3567 | break; | |
3568 | case 'f': | |
3569 | fmt = optarg; | |
3570 | break; | |
f382d43a MR |
3571 | case 'q': |
3572 | quiet = true; | |
3573 | break; | |
3babeb15 DB |
3574 | case OPTION_OBJECT: { |
3575 | QemuOpts *opts; | |
3576 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3577 | optarg, true); | |
3578 | if (!opts) { | |
3579 | return 1; | |
3580 | } | |
3581 | } break; | |
eb769f74 DB |
3582 | case OPTION_IMAGE_OPTS: |
3583 | image_opts = true; | |
3584 | break; | |
dc5f690b | 3585 | case OPTION_PREALLOCATION: |
f7abe0ec | 3586 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
06c60b6c | 3587 | PREALLOC_MODE__MAX, NULL); |
dc5f690b HR |
3588 | if (prealloc == PREALLOC_MODE__MAX) { |
3589 | error_report("Invalid preallocation mode '%s'", optarg); | |
3590 | return 1; | |
3591 | } | |
3592 | break; | |
4ffca890 PB |
3593 | case OPTION_SHRINK: |
3594 | shrink = true; | |
3595 | break; | |
ae6b0ed6 SH |
3596 | } |
3597 | } | |
fc11eb26 | 3598 | if (optind != argc - 1) { |
be8fbd47 | 3599 | error_exit("Expecting image file name and size"); |
ae6b0ed6 SH |
3600 | } |
3601 | filename = argv[optind++]; | |
ae6b0ed6 | 3602 | |
3babeb15 DB |
3603 | if (qemu_opts_foreach(&qemu_object_opts, |
3604 | user_creatable_add_opts_foreach, | |
51b9b478 | 3605 | NULL, NULL)) { |
3babeb15 DB |
3606 | return 1; |
3607 | } | |
3608 | ||
ae6b0ed6 SH |
3609 | /* Choose grow, shrink, or absolute resize mode */ |
3610 | switch (size[0]) { | |
3611 | case '+': | |
3612 | relative = 1; | |
3613 | size++; | |
3614 | break; | |
3615 | case '-': | |
3616 | relative = -1; | |
3617 | size++; | |
3618 | break; | |
3619 | default: | |
3620 | relative = 0; | |
3621 | break; | |
3622 | } | |
3623 | ||
3624 | /* Parse size */ | |
87ea75d5 | 3625 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
f43e47db | 3626 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
6750e795 MA |
3627 | if (err) { |
3628 | error_report_err(err); | |
2a81998a | 3629 | ret = -1; |
20caf0f7 | 3630 | qemu_opts_del(param); |
2a81998a | 3631 | goto out; |
ae6b0ed6 | 3632 | } |
20caf0f7 DXW |
3633 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
3634 | qemu_opts_del(param); | |
ae6b0ed6 | 3635 | |
efaa7c4e | 3636 | blk = img_open(image_opts, filename, fmt, |
335e9937 FZ |
3637 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
3638 | false); | |
7e7d56d9 | 3639 | if (!blk) { |
2a81998a JS |
3640 | ret = -1; |
3641 | goto out; | |
c2abccec | 3642 | } |
ae6b0ed6 | 3643 | |
dc5f690b HR |
3644 | current_size = blk_getlength(blk); |
3645 | if (current_size < 0) { | |
3646 | error_report("Failed to inquire current image length: %s", | |
3647 | strerror(-current_size)); | |
3648 | ret = -1; | |
3649 | goto out; | |
3650 | } | |
3651 | ||
ae6b0ed6 | 3652 | if (relative) { |
dc5f690b | 3653 | total_size = current_size + n * relative; |
ae6b0ed6 SH |
3654 | } else { |
3655 | total_size = n; | |
3656 | } | |
3657 | if (total_size <= 0) { | |
15654a6d | 3658 | error_report("New image size must be positive"); |
c2abccec MK |
3659 | ret = -1; |
3660 | goto out; | |
ae6b0ed6 SH |
3661 | } |
3662 | ||
dc5f690b HR |
3663 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
3664 | error_report("Preallocation can only be used for growing images"); | |
3665 | ret = -1; | |
3666 | goto out; | |
3667 | } | |
3668 | ||
4ffca890 PB |
3669 | if (total_size < current_size && !shrink) { |
3670 | warn_report("Shrinking an image will delete all data beyond the " | |
3671 | "shrunken image's end. Before performing such an " | |
3672 | "operation, make sure there is no important data there."); | |
3673 | ||
3674 | if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) { | |
3675 | error_report( | |
3676 | "Use the --shrink option to perform a shrink operation."); | |
3677 | ret = -1; | |
3678 | goto out; | |
3679 | } else { | |
3680 | warn_report("Using the --shrink option will suppress this message. " | |
3681 | "Note that future versions of qemu-img may refuse to " | |
3682 | "shrink images without this option."); | |
3683 | } | |
3684 | } | |
3685 | ||
dc5f690b | 3686 | ret = blk_truncate(blk, total_size, prealloc, &err); |
5279b303 | 3687 | if (ret < 0) { |
ed3d2ec9 | 3688 | error_report_err(err); |
5279b303 HR |
3689 | goto out; |
3690 | } | |
3691 | ||
3692 | new_size = blk_getlength(blk); | |
3693 | if (new_size < 0) { | |
3694 | error_report("Failed to verify truncated image length: %s", | |
3695 | strerror(-new_size)); | |
3696 | ret = -1; | |
3697 | goto out; | |
ae6b0ed6 | 3698 | } |
5279b303 HR |
3699 | |
3700 | /* Some block drivers implement a truncation method, but only so | |
3701 | * the user can cause qemu to refresh the image's size from disk. | |
3702 | * The idea is that the user resizes the image outside of qemu and | |
3703 | * then invokes block_resize to inform qemu about it. | |
3704 | * (This includes iscsi and file-posix for device files.) | |
3705 | * Of course, that is not the behavior someone invoking | |
3706 | * qemu-img resize would find useful, so we catch that behavior | |
3707 | * here and tell the user. */ | |
3708 | if (new_size != total_size && new_size == current_size) { | |
3709 | error_report("Image was not resized; resizing may not be supported " | |
3710 | "for this image"); | |
3711 | ret = -1; | |
3712 | goto out; | |
3713 | } | |
3714 | ||
3715 | if (new_size != total_size) { | |
3716 | warn_report("Image should have been resized to %" PRIi64 | |
3717 | " bytes, but was resized to %" PRIi64 " bytes", | |
3718 | total_size, new_size); | |
3719 | } | |
3720 | ||
3721 | qprintf(quiet, "Image resized.\n"); | |
3722 | ||
c2abccec | 3723 | out: |
26f54e9a | 3724 | blk_unref(blk); |
c2abccec MK |
3725 | if (ret) { |
3726 | return 1; | |
3727 | } | |
ae6b0ed6 SH |
3728 | return 0; |
3729 | } | |
3730 | ||
76a3a34d | 3731 | static void amend_status_cb(BlockDriverState *bs, |
8b13976d HR |
3732 | int64_t offset, int64_t total_work_size, |
3733 | void *opaque) | |
76a3a34d HR |
3734 | { |
3735 | qemu_progress_print(100.f * offset / total_work_size, 0); | |
3736 | } | |
3737 | ||
51641351 HR |
3738 | static int print_amend_option_help(const char *format) |
3739 | { | |
3740 | BlockDriver *drv; | |
3741 | ||
3742 | /* Find driver and parse its options */ | |
3743 | drv = bdrv_find_format(format); | |
3744 | if (!drv) { | |
3745 | error_report("Unknown file format '%s'", format); | |
3746 | return 1; | |
3747 | } | |
3748 | ||
3749 | if (!drv->bdrv_amend_options) { | |
3750 | error_report("Format driver '%s' does not support option amendment", | |
3751 | format); | |
3752 | return 1; | |
3753 | } | |
3754 | ||
3755 | /* Every driver supporting amendment must have create_opts */ | |
3756 | assert(drv->create_opts); | |
3757 | ||
3758 | printf("Creation options for '%s':\n", format); | |
3759 | qemu_opts_print_help(drv->create_opts); | |
3760 | printf("\nNote that not all of these options may be amendable.\n"); | |
3761 | return 0; | |
3762 | } | |
3763 | ||
6f176b48 HR |
3764 | static int img_amend(int argc, char **argv) |
3765 | { | |
dc523cd3 | 3766 | Error *err = NULL; |
6f176b48 HR |
3767 | int c, ret = 0; |
3768 | char *options = NULL; | |
83d0521a CL |
3769 | QemuOptsList *create_opts = NULL; |
3770 | QemuOpts *opts = NULL; | |
bd39e6ed HR |
3771 | const char *fmt = NULL, *filename, *cache; |
3772 | int flags; | |
ce099547 | 3773 | bool writethrough; |
76a3a34d | 3774 | bool quiet = false, progress = false; |
26f54e9a | 3775 | BlockBackend *blk = NULL; |
6f176b48 | 3776 | BlockDriverState *bs = NULL; |
eb769f74 | 3777 | bool image_opts = false; |
6f176b48 | 3778 | |
bd39e6ed | 3779 | cache = BDRV_DEFAULT_CACHE; |
6f176b48 | 3780 | for (;;) { |
3babeb15 DB |
3781 | static const struct option long_options[] = { |
3782 | {"help", no_argument, 0, 'h'}, | |
3783 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3784 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
3785 | {0, 0, 0, 0} |
3786 | }; | |
c9192973 | 3787 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
3babeb15 | 3788 | long_options, NULL); |
6f176b48 HR |
3789 | if (c == -1) { |
3790 | break; | |
3791 | } | |
3792 | ||
3793 | switch (c) { | |
c9192973 SH |
3794 | case ':': |
3795 | missing_argument(argv[optind - 1]); | |
3796 | break; | |
f7077624 | 3797 | case '?': |
c9192973 SH |
3798 | unrecognized_option(argv[optind - 1]); |
3799 | break; | |
3800 | case 'h': | |
f7077624 SH |
3801 | help(); |
3802 | break; | |
3803 | case 'o': | |
3804 | if (!is_valid_option_list(optarg)) { | |
3805 | error_report("Invalid option list: %s", optarg); | |
3806 | ret = -1; | |
3807 | goto out_no_progress; | |
3808 | } | |
3809 | if (!options) { | |
3810 | options = g_strdup(optarg); | |
3811 | } else { | |
3812 | char *old_options = options; | |
3813 | options = g_strdup_printf("%s,%s", options, optarg); | |
3814 | g_free(old_options); | |
3815 | } | |
3816 | break; | |
3817 | case 'f': | |
3818 | fmt = optarg; | |
3819 | break; | |
3820 | case 't': | |
3821 | cache = optarg; | |
3822 | break; | |
3823 | case 'p': | |
3824 | progress = true; | |
3825 | break; | |
3826 | case 'q': | |
3827 | quiet = true; | |
3828 | break; | |
3829 | case OPTION_OBJECT: | |
3830 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3831 | optarg, true); | |
3832 | if (!opts) { | |
3833 | ret = -1; | |
3834 | goto out_no_progress; | |
3835 | } | |
3836 | break; | |
3837 | case OPTION_IMAGE_OPTS: | |
3838 | image_opts = true; | |
3839 | break; | |
6f176b48 HR |
3840 | } |
3841 | } | |
3842 | ||
a283cb6e | 3843 | if (!options) { |
ac1307ab | 3844 | error_exit("Must specify options (-o)"); |
6f176b48 HR |
3845 | } |
3846 | ||
3babeb15 DB |
3847 | if (qemu_opts_foreach(&qemu_object_opts, |
3848 | user_creatable_add_opts_foreach, | |
51b9b478 | 3849 | NULL, NULL)) { |
3babeb15 DB |
3850 | ret = -1; |
3851 | goto out_no_progress; | |
3852 | } | |
3853 | ||
76a3a34d HR |
3854 | if (quiet) { |
3855 | progress = false; | |
3856 | } | |
3857 | qemu_progress_init(progress, 1.0); | |
3858 | ||
a283cb6e KW |
3859 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
3860 | if (fmt && has_help_option(options)) { | |
3861 | /* If a format is explicitly specified (and possibly no filename is | |
3862 | * given), print option help here */ | |
51641351 | 3863 | ret = print_amend_option_help(fmt); |
a283cb6e | 3864 | goto out; |
6f176b48 HR |
3865 | } |
3866 | ||
a283cb6e | 3867 | if (optind != argc - 1) { |
b2f27e44 HR |
3868 | error_report("Expecting one image file name"); |
3869 | ret = -1; | |
3870 | goto out; | |
a283cb6e | 3871 | } |
6f176b48 | 3872 | |
ce099547 KW |
3873 | flags = BDRV_O_RDWR; |
3874 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
bd39e6ed HR |
3875 | if (ret < 0) { |
3876 | error_report("Invalid cache option: %s", cache); | |
3877 | goto out; | |
3878 | } | |
3879 | ||
335e9937 FZ |
3880 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
3881 | false); | |
7e7d56d9 | 3882 | if (!blk) { |
6f176b48 HR |
3883 | ret = -1; |
3884 | goto out; | |
3885 | } | |
7e7d56d9 | 3886 | bs = blk_bs(blk); |
6f176b48 HR |
3887 | |
3888 | fmt = bs->drv->format_name; | |
3889 | ||
626f84f3 | 3890 | if (has_help_option(options)) { |
a283cb6e | 3891 | /* If the format was auto-detected, print option help here */ |
51641351 | 3892 | ret = print_amend_option_help(fmt); |
6f176b48 HR |
3893 | goto out; |
3894 | } | |
3895 | ||
1f996683 HR |
3896 | if (!bs->drv->bdrv_amend_options) { |
3897 | error_report("Format driver '%s' does not support option amendment", | |
b2439d26 HR |
3898 | fmt); |
3899 | ret = -1; | |
3900 | goto out; | |
3901 | } | |
3902 | ||
1f996683 HR |
3903 | /* Every driver supporting amendment must have create_opts */ |
3904 | assert(bs->drv->create_opts); | |
3905 | ||
c282e1fd | 3906 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
83d0521a | 3907 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
ece9086e PB |
3908 | qemu_opts_do_parse(opts, options, NULL, &err); |
3909 | if (err) { | |
3910 | error_report_err(err); | |
3911 | ret = -1; | |
3912 | goto out; | |
6f176b48 HR |
3913 | } |
3914 | ||
76a3a34d HR |
3915 | /* In case the driver does not call amend_status_cb() */ |
3916 | qemu_progress_print(0.f, 0); | |
d1402b50 | 3917 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err); |
76a3a34d | 3918 | qemu_progress_print(100.f, 0); |
6f176b48 | 3919 | if (ret < 0) { |
d1402b50 | 3920 | error_report_err(err); |
6f176b48 HR |
3921 | goto out; |
3922 | } | |
3923 | ||
3924 | out: | |
76a3a34d HR |
3925 | qemu_progress_end(); |
3926 | ||
e814dffc | 3927 | out_no_progress: |
26f54e9a | 3928 | blk_unref(blk); |
83d0521a CL |
3929 | qemu_opts_del(opts); |
3930 | qemu_opts_free(create_opts); | |
626f84f3 KW |
3931 | g_free(options); |
3932 | ||
6f176b48 HR |
3933 | if (ret) { |
3934 | return 1; | |
3935 | } | |
3936 | return 0; | |
3937 | } | |
3938 | ||
b6133b8c KW |
3939 | typedef struct BenchData { |
3940 | BlockBackend *blk; | |
3941 | uint64_t image_size; | |
b6495fa8 | 3942 | bool write; |
b6133b8c | 3943 | int bufsize; |
83de9be0 | 3944 | int step; |
b6133b8c KW |
3945 | int nrreq; |
3946 | int n; | |
55d539c8 KW |
3947 | int flush_interval; |
3948 | bool drain_on_flush; | |
b6133b8c KW |
3949 | uint8_t *buf; |
3950 | QEMUIOVector *qiov; | |
3951 | ||
3952 | int in_flight; | |
55d539c8 | 3953 | bool in_flush; |
b6133b8c KW |
3954 | uint64_t offset; |
3955 | } BenchData; | |
3956 | ||
55d539c8 KW |
3957 | static void bench_undrained_flush_cb(void *opaque, int ret) |
3958 | { | |
3959 | if (ret < 0) { | |
df3c286c | 3960 | error_report("Failed flush request: %s", strerror(-ret)); |
55d539c8 KW |
3961 | exit(EXIT_FAILURE); |
3962 | } | |
3963 | } | |
3964 | ||
b6133b8c KW |
3965 | static void bench_cb(void *opaque, int ret) |
3966 | { | |
3967 | BenchData *b = opaque; | |
3968 | BlockAIOCB *acb; | |
3969 | ||
3970 | if (ret < 0) { | |
df3c286c | 3971 | error_report("Failed request: %s", strerror(-ret)); |
b6133b8c KW |
3972 | exit(EXIT_FAILURE); |
3973 | } | |
55d539c8 KW |
3974 | |
3975 | if (b->in_flush) { | |
3976 | /* Just finished a flush with drained queue: Start next requests */ | |
3977 | assert(b->in_flight == 0); | |
3978 | b->in_flush = false; | |
3979 | } else if (b->in_flight > 0) { | |
3980 | int remaining = b->n - b->in_flight; | |
3981 | ||
b6133b8c KW |
3982 | b->n--; |
3983 | b->in_flight--; | |
55d539c8 KW |
3984 | |
3985 | /* Time for flush? Drain queue if requested, then flush */ | |
3986 | if (b->flush_interval && remaining % b->flush_interval == 0) { | |
3987 | if (!b->in_flight || !b->drain_on_flush) { | |
3988 | BlockCompletionFunc *cb; | |
3989 | ||
3990 | if (b->drain_on_flush) { | |
3991 | b->in_flush = true; | |
3992 | cb = bench_cb; | |
3993 | } else { | |
3994 | cb = bench_undrained_flush_cb; | |
3995 | } | |
3996 | ||
3997 | acb = blk_aio_flush(b->blk, cb, b); | |
3998 | if (!acb) { | |
3999 | error_report("Failed to issue flush request"); | |
4000 | exit(EXIT_FAILURE); | |
4001 | } | |
4002 | } | |
4003 | if (b->drain_on_flush) { | |
4004 | return; | |
4005 | } | |
4006 | } | |
b6133b8c KW |
4007 | } |
4008 | ||
4009 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { | |
4baaa8c3 PB |
4010 | int64_t offset = b->offset; |
4011 | /* blk_aio_* might look for completed I/Os and kick bench_cb | |
4012 | * again, so make sure this operation is counted by in_flight | |
4013 | * and b->offset is ready for the next submission. | |
4014 | */ | |
4015 | b->in_flight++; | |
4016 | b->offset += b->step; | |
4017 | b->offset %= b->image_size; | |
b6495fa8 | 4018 | if (b->write) { |
4baaa8c3 | 4019 | acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b); |
b6495fa8 | 4020 | } else { |
4baaa8c3 | 4021 | acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b); |
b6495fa8 | 4022 | } |
b6133b8c KW |
4023 | if (!acb) { |
4024 | error_report("Failed to issue request"); | |
4025 | exit(EXIT_FAILURE); | |
4026 | } | |
b6133b8c KW |
4027 | } |
4028 | } | |
4029 | ||
4030 | static int img_bench(int argc, char **argv) | |
4031 | { | |
4032 | int c, ret = 0; | |
4033 | const char *fmt = NULL, *filename; | |
4034 | bool quiet = false; | |
4035 | bool image_opts = false; | |
b6495fa8 | 4036 | bool is_write = false; |
b6133b8c KW |
4037 | int count = 75000; |
4038 | int depth = 64; | |
d3199a31 | 4039 | int64_t offset = 0; |
b6133b8c | 4040 | size_t bufsize = 4096; |
b6495fa8 | 4041 | int pattern = 0; |
83de9be0 | 4042 | size_t step = 0; |
55d539c8 KW |
4043 | int flush_interval = 0; |
4044 | bool drain_on_flush = true; | |
b6133b8c KW |
4045 | int64_t image_size; |
4046 | BlockBackend *blk = NULL; | |
4047 | BenchData data = {}; | |
4048 | int flags = 0; | |
604e8613 | 4049 | bool writethrough = false; |
b6133b8c KW |
4050 | struct timeval t1, t2; |
4051 | int i; | |
335e9937 | 4052 | bool force_share = false; |
79d46583 | 4053 | size_t buf_size; |
b6133b8c KW |
4054 | |
4055 | for (;;) { | |
4056 | static const struct option long_options[] = { | |
4057 | {"help", no_argument, 0, 'h'}, | |
55d539c8 | 4058 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
b6133b8c | 4059 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
b6495fa8 | 4060 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
55d539c8 | 4061 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
335e9937 | 4062 | {"force-share", no_argument, 0, 'U'}, |
b6133b8c KW |
4063 | {0, 0, 0, 0} |
4064 | }; | |
335e9937 | 4065 | c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL); |
b6133b8c KW |
4066 | if (c == -1) { |
4067 | break; | |
4068 | } | |
4069 | ||
4070 | switch (c) { | |
c9192973 SH |
4071 | case ':': |
4072 | missing_argument(argv[optind - 1]); | |
4073 | break; | |
b6133b8c | 4074 | case '?': |
c9192973 SH |
4075 | unrecognized_option(argv[optind - 1]); |
4076 | break; | |
4077 | case 'h': | |
b6133b8c KW |
4078 | help(); |
4079 | break; | |
4080 | case 'c': | |
4081 | { | |
8b3c6792 PM |
4082 | unsigned long res; |
4083 | ||
4084 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
b6133b8c KW |
4085 | error_report("Invalid request count specified"); |
4086 | return 1; | |
4087 | } | |
8b3c6792 | 4088 | count = res; |
b6133b8c KW |
4089 | break; |
4090 | } | |
4091 | case 'd': | |
4092 | { | |
8b3c6792 PM |
4093 | unsigned long res; |
4094 | ||
4095 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
b6133b8c KW |
4096 | error_report("Invalid queue depth specified"); |
4097 | return 1; | |
4098 | } | |
8b3c6792 | 4099 | depth = res; |
b6133b8c KW |
4100 | break; |
4101 | } | |
4102 | case 'f': | |
4103 | fmt = optarg; | |
4104 | break; | |
4105 | case 'n': | |
4106 | flags |= BDRV_O_NATIVE_AIO; | |
4107 | break; | |
d3199a31 KW |
4108 | case 'o': |
4109 | { | |
606caa0a MA |
4110 | offset = cvtnum(optarg); |
4111 | if (offset < 0) { | |
d3199a31 KW |
4112 | error_report("Invalid offset specified"); |
4113 | return 1; | |
4114 | } | |
4115 | break; | |
4116 | } | |
4117 | break; | |
b6133b8c KW |
4118 | case 'q': |
4119 | quiet = true; | |
4120 | break; | |
4121 | case 's': | |
4122 | { | |
4123 | int64_t sval; | |
b6133b8c | 4124 | |
606caa0a MA |
4125 | sval = cvtnum(optarg); |
4126 | if (sval < 0 || sval > INT_MAX) { | |
b6133b8c KW |
4127 | error_report("Invalid buffer size specified"); |
4128 | return 1; | |
4129 | } | |
4130 | ||
4131 | bufsize = sval; | |
4132 | break; | |
4133 | } | |
83de9be0 KW |
4134 | case 'S': |
4135 | { | |
4136 | int64_t sval; | |
83de9be0 | 4137 | |
606caa0a MA |
4138 | sval = cvtnum(optarg); |
4139 | if (sval < 0 || sval > INT_MAX) { | |
83de9be0 KW |
4140 | error_report("Invalid step size specified"); |
4141 | return 1; | |
4142 | } | |
4143 | ||
4144 | step = sval; | |
4145 | break; | |
4146 | } | |
b6133b8c KW |
4147 | case 't': |
4148 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); | |
4149 | if (ret < 0) { | |
4150 | error_report("Invalid cache mode"); | |
4151 | ret = -1; | |
4152 | goto out; | |
4153 | } | |
4154 | break; | |
b6495fa8 KW |
4155 | case 'w': |
4156 | flags |= BDRV_O_RDWR; | |
4157 | is_write = true; | |
4158 | break; | |
335e9937 FZ |
4159 | case 'U': |
4160 | force_share = true; | |
4161 | break; | |
b6495fa8 KW |
4162 | case OPTION_PATTERN: |
4163 | { | |
8b3c6792 PM |
4164 | unsigned long res; |
4165 | ||
4166 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { | |
b6495fa8 KW |
4167 | error_report("Invalid pattern byte specified"); |
4168 | return 1; | |
4169 | } | |
8b3c6792 | 4170 | pattern = res; |
b6495fa8 KW |
4171 | break; |
4172 | } | |
55d539c8 KW |
4173 | case OPTION_FLUSH_INTERVAL: |
4174 | { | |
8b3c6792 PM |
4175 | unsigned long res; |
4176 | ||
4177 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
55d539c8 KW |
4178 | error_report("Invalid flush interval specified"); |
4179 | return 1; | |
4180 | } | |
8b3c6792 | 4181 | flush_interval = res; |
55d539c8 KW |
4182 | break; |
4183 | } | |
4184 | case OPTION_NO_DRAIN: | |
4185 | drain_on_flush = false; | |
4186 | break; | |
b6133b8c KW |
4187 | case OPTION_IMAGE_OPTS: |
4188 | image_opts = true; | |
4189 | break; | |
4190 | } | |
4191 | } | |
4192 | ||
4193 | if (optind != argc - 1) { | |
4194 | error_exit("Expecting one image file name"); | |
4195 | } | |
4196 | filename = argv[argc - 1]; | |
4197 | ||
55d539c8 KW |
4198 | if (!is_write && flush_interval) { |
4199 | error_report("--flush-interval is only available in write tests"); | |
4200 | ret = -1; | |
4201 | goto out; | |
4202 | } | |
4203 | if (flush_interval && flush_interval < depth) { | |
4204 | error_report("Flush interval can't be smaller than depth"); | |
4205 | ret = -1; | |
4206 | goto out; | |
4207 | } | |
4208 | ||
335e9937 FZ |
4209 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
4210 | force_share); | |
b6133b8c KW |
4211 | if (!blk) { |
4212 | ret = -1; | |
4213 | goto out; | |
4214 | } | |
4215 | ||
4216 | image_size = blk_getlength(blk); | |
4217 | if (image_size < 0) { | |
4218 | ret = image_size; | |
4219 | goto out; | |
4220 | } | |
4221 | ||
4222 | data = (BenchData) { | |
55d539c8 KW |
4223 | .blk = blk, |
4224 | .image_size = image_size, | |
4225 | .bufsize = bufsize, | |
4226 | .step = step ?: bufsize, | |
4227 | .nrreq = depth, | |
4228 | .n = count, | |
4229 | .offset = offset, | |
4230 | .write = is_write, | |
4231 | .flush_interval = flush_interval, | |
4232 | .drain_on_flush = drain_on_flush, | |
b6133b8c | 4233 | }; |
d3199a31 | 4234 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
83de9be0 | 4235 | "(starting at offset %" PRId64 ", step size %d)\n", |
d3199a31 | 4236 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
83de9be0 | 4237 | data.offset, data.step); |
55d539c8 KW |
4238 | if (flush_interval) { |
4239 | printf("Sending flush every %d requests\n", flush_interval); | |
4240 | } | |
b6133b8c | 4241 | |
79d46583 FZ |
4242 | buf_size = data.nrreq * data.bufsize; |
4243 | data.buf = blk_blockalign(blk, buf_size); | |
b6495fa8 KW |
4244 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
4245 | ||
79d46583 FZ |
4246 | blk_register_buf(blk, data.buf, buf_size); |
4247 | ||
b6133b8c KW |
4248 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
4249 | for (i = 0; i < data.nrreq; i++) { | |
4250 | qemu_iovec_init(&data.qiov[i], 1); | |
4251 | qemu_iovec_add(&data.qiov[i], | |
4252 | data.buf + i * data.bufsize, data.bufsize); | |
4253 | } | |
4254 | ||
4255 | gettimeofday(&t1, NULL); | |
4256 | bench_cb(&data, 0); | |
4257 | ||
4258 | while (data.n > 0) { | |
4259 | main_loop_wait(false); | |
4260 | } | |
4261 | gettimeofday(&t2, NULL); | |
4262 | ||
4263 | printf("Run completed in %3.3f seconds.\n", | |
4264 | (t2.tv_sec - t1.tv_sec) | |
4265 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); | |
4266 | ||
4267 | out: | |
79d46583 FZ |
4268 | if (data.buf) { |
4269 | blk_unregister_buf(blk, data.buf); | |
4270 | } | |
b6133b8c KW |
4271 | qemu_vfree(data.buf); |
4272 | blk_unref(blk); | |
4273 | ||
4274 | if (ret) { | |
86ce1f6e RS |
4275 | return 1; |
4276 | } | |
4277 | return 0; | |
4278 | } | |
4279 | ||
4280 | #define C_BS 01 | |
4281 | #define C_COUNT 02 | |
4282 | #define C_IF 04 | |
4283 | #define C_OF 010 | |
f7c15533 | 4284 | #define C_SKIP 020 |
86ce1f6e RS |
4285 | |
4286 | struct DdInfo { | |
4287 | unsigned int flags; | |
4288 | int64_t count; | |
4289 | }; | |
4290 | ||
4291 | struct DdIo { | |
4292 | int bsz; /* Block size */ | |
4293 | char *filename; | |
4294 | uint8_t *buf; | |
f7c15533 | 4295 | int64_t offset; |
86ce1f6e RS |
4296 | }; |
4297 | ||
4298 | struct DdOpts { | |
4299 | const char *name; | |
4300 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); | |
4301 | unsigned int flag; | |
4302 | }; | |
4303 | ||
4304 | static int img_dd_bs(const char *arg, | |
4305 | struct DdIo *in, struct DdIo *out, | |
4306 | struct DdInfo *dd) | |
4307 | { | |
86ce1f6e RS |
4308 | int64_t res; |
4309 | ||
606caa0a | 4310 | res = cvtnum(arg); |
86ce1f6e | 4311 | |
606caa0a | 4312 | if (res <= 0 || res > INT_MAX) { |
86ce1f6e RS |
4313 | error_report("invalid number: '%s'", arg); |
4314 | return 1; | |
4315 | } | |
4316 | in->bsz = out->bsz = res; | |
4317 | ||
4318 | return 0; | |
4319 | } | |
4320 | ||
4321 | static int img_dd_count(const char *arg, | |
4322 | struct DdIo *in, struct DdIo *out, | |
4323 | struct DdInfo *dd) | |
4324 | { | |
606caa0a | 4325 | dd->count = cvtnum(arg); |
86ce1f6e | 4326 | |
606caa0a | 4327 | if (dd->count < 0) { |
86ce1f6e RS |
4328 | error_report("invalid number: '%s'", arg); |
4329 | return 1; | |
4330 | } | |
4331 | ||
4332 | return 0; | |
4333 | } | |
4334 | ||
4335 | static int img_dd_if(const char *arg, | |
4336 | struct DdIo *in, struct DdIo *out, | |
4337 | struct DdInfo *dd) | |
4338 | { | |
4339 | in->filename = g_strdup(arg); | |
4340 | ||
4341 | return 0; | |
4342 | } | |
4343 | ||
4344 | static int img_dd_of(const char *arg, | |
4345 | struct DdIo *in, struct DdIo *out, | |
4346 | struct DdInfo *dd) | |
4347 | { | |
4348 | out->filename = g_strdup(arg); | |
4349 | ||
4350 | return 0; | |
4351 | } | |
4352 | ||
f7c15533 RS |
4353 | static int img_dd_skip(const char *arg, |
4354 | struct DdIo *in, struct DdIo *out, | |
4355 | struct DdInfo *dd) | |
4356 | { | |
606caa0a | 4357 | in->offset = cvtnum(arg); |
f7c15533 | 4358 | |
606caa0a | 4359 | if (in->offset < 0) { |
f7c15533 RS |
4360 | error_report("invalid number: '%s'", arg); |
4361 | return 1; | |
4362 | } | |
4363 | ||
4364 | return 0; | |
4365 | } | |
4366 | ||
86ce1f6e RS |
4367 | static int img_dd(int argc, char **argv) |
4368 | { | |
4369 | int ret = 0; | |
4370 | char *arg = NULL; | |
4371 | char *tmp; | |
4372 | BlockDriver *drv = NULL, *proto_drv = NULL; | |
4373 | BlockBackend *blk1 = NULL, *blk2 = NULL; | |
4374 | QemuOpts *opts = NULL; | |
4375 | QemuOptsList *create_opts = NULL; | |
4376 | Error *local_err = NULL; | |
4377 | bool image_opts = false; | |
4378 | int c, i; | |
4379 | const char *out_fmt = "raw"; | |
4380 | const char *fmt = NULL; | |
4381 | int64_t size = 0; | |
4382 | int64_t block_count = 0, out_pos, in_pos; | |
335e9937 | 4383 | bool force_share = false; |
86ce1f6e RS |
4384 | struct DdInfo dd = { |
4385 | .flags = 0, | |
4386 | .count = 0, | |
4387 | }; | |
4388 | struct DdIo in = { | |
4389 | .bsz = 512, /* Block size is by default 512 bytes */ | |
4390 | .filename = NULL, | |
f7c15533 RS |
4391 | .buf = NULL, |
4392 | .offset = 0 | |
86ce1f6e RS |
4393 | }; |
4394 | struct DdIo out = { | |
4395 | .bsz = 512, | |
4396 | .filename = NULL, | |
f7c15533 RS |
4397 | .buf = NULL, |
4398 | .offset = 0 | |
86ce1f6e RS |
4399 | }; |
4400 | ||
4401 | const struct DdOpts options[] = { | |
4402 | { "bs", img_dd_bs, C_BS }, | |
4403 | { "count", img_dd_count, C_COUNT }, | |
4404 | { "if", img_dd_if, C_IF }, | |
4405 | { "of", img_dd_of, C_OF }, | |
f7c15533 | 4406 | { "skip", img_dd_skip, C_SKIP }, |
86ce1f6e RS |
4407 | { NULL, NULL, 0 } |
4408 | }; | |
4409 | const struct option long_options[] = { | |
4410 | { "help", no_argument, 0, 'h'}, | |
83d4bf94 | 4411 | { "object", required_argument, 0, OPTION_OBJECT}, |
86ce1f6e | 4412 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 4413 | { "force-share", no_argument, 0, 'U'}, |
86ce1f6e RS |
4414 | { 0, 0, 0, 0 } |
4415 | }; | |
4416 | ||
335e9937 | 4417 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
86ce1f6e RS |
4418 | if (c == EOF) { |
4419 | break; | |
4420 | } | |
4421 | switch (c) { | |
4422 | case 'O': | |
4423 | out_fmt = optarg; | |
4424 | break; | |
4425 | case 'f': | |
4426 | fmt = optarg; | |
4427 | break; | |
c9192973 SH |
4428 | case ':': |
4429 | missing_argument(argv[optind - 1]); | |
4430 | break; | |
86ce1f6e | 4431 | case '?': |
c9192973 SH |
4432 | unrecognized_option(argv[optind - 1]); |
4433 | break; | |
86ce1f6e RS |
4434 | case 'h': |
4435 | help(); | |
4436 | break; | |
335e9937 FZ |
4437 | case 'U': |
4438 | force_share = true; | |
4439 | break; | |
2a245709 SH |
4440 | case OPTION_OBJECT: |
4441 | if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) { | |
83d4bf94 DB |
4442 | ret = -1; |
4443 | goto out; | |
4444 | } | |
2a245709 | 4445 | break; |
86ce1f6e RS |
4446 | case OPTION_IMAGE_OPTS: |
4447 | image_opts = true; | |
4448 | break; | |
4449 | } | |
4450 | } | |
4451 | ||
4452 | for (i = optind; i < argc; i++) { | |
4453 | int j; | |
4454 | arg = g_strdup(argv[i]); | |
4455 | ||
4456 | tmp = strchr(arg, '='); | |
4457 | if (tmp == NULL) { | |
4458 | error_report("unrecognized operand %s", arg); | |
4459 | ret = -1; | |
4460 | goto out; | |
4461 | } | |
4462 | ||
4463 | *tmp++ = '\0'; | |
4464 | ||
4465 | for (j = 0; options[j].name != NULL; j++) { | |
4466 | if (!strcmp(arg, options[j].name)) { | |
4467 | break; | |
4468 | } | |
4469 | } | |
4470 | if (options[j].name == NULL) { | |
4471 | error_report("unrecognized operand %s", arg); | |
4472 | ret = -1; | |
4473 | goto out; | |
4474 | } | |
4475 | ||
4476 | if (options[j].f(tmp, &in, &out, &dd) != 0) { | |
4477 | ret = -1; | |
4478 | goto out; | |
4479 | } | |
4480 | dd.flags |= options[j].flag; | |
4481 | g_free(arg); | |
4482 | arg = NULL; | |
4483 | } | |
4484 | ||
4485 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { | |
4486 | error_report("Must specify both input and output files"); | |
4487 | ret = -1; | |
4488 | goto out; | |
4489 | } | |
83d4bf94 DB |
4490 | |
4491 | if (qemu_opts_foreach(&qemu_object_opts, | |
4492 | user_creatable_add_opts_foreach, | |
4493 | NULL, NULL)) { | |
4494 | ret = -1; | |
4495 | goto out; | |
4496 | } | |
4497 | ||
335e9937 FZ |
4498 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
4499 | force_share); | |
86ce1f6e RS |
4500 | |
4501 | if (!blk1) { | |
4502 | ret = -1; | |
4503 | goto out; | |
4504 | } | |
4505 | ||
4506 | drv = bdrv_find_format(out_fmt); | |
4507 | if (!drv) { | |
4508 | error_report("Unknown file format"); | |
4509 | ret = -1; | |
4510 | goto out; | |
4511 | } | |
4512 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); | |
4513 | ||
4514 | if (!proto_drv) { | |
4515 | error_report_err(local_err); | |
4516 | ret = -1; | |
4517 | goto out; | |
4518 | } | |
4519 | if (!drv->create_opts) { | |
4520 | error_report("Format driver '%s' does not support image creation", | |
4521 | drv->format_name); | |
4522 | ret = -1; | |
4523 | goto out; | |
4524 | } | |
4525 | if (!proto_drv->create_opts) { | |
4526 | error_report("Protocol driver '%s' does not support image creation", | |
4527 | proto_drv->format_name); | |
4528 | ret = -1; | |
4529 | goto out; | |
4530 | } | |
4531 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
4532 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
4533 | ||
4534 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
4535 | ||
4536 | size = blk_getlength(blk1); | |
4537 | if (size < 0) { | |
4538 | error_report("Failed to get size for '%s'", in.filename); | |
4539 | ret = -1; | |
4540 | goto out; | |
4541 | } | |
4542 | ||
4543 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && | |
4544 | dd.count * in.bsz < size) { | |
4545 | size = dd.count * in.bsz; | |
4546 | } | |
4547 | ||
f7c15533 RS |
4548 | /* Overflow means the specified offset is beyond input image's size */ |
4549 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || | |
4550 | size < in.bsz * in.offset)) { | |
4551 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); | |
4552 | } else { | |
4553 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, | |
4554 | size - in.bsz * in.offset, &error_abort); | |
4555 | } | |
86ce1f6e RS |
4556 | |
4557 | ret = bdrv_create(drv, out.filename, opts, &local_err); | |
4558 | if (ret < 0) { | |
4559 | error_reportf_err(local_err, | |
4560 | "%s: error while creating output image: ", | |
4561 | out.filename); | |
4562 | ret = -1; | |
4563 | goto out; | |
4564 | } | |
4565 | ||
ea204dda DB |
4566 | /* TODO, we can't honour --image-opts for the target, |
4567 | * since it needs to be given in a format compatible | |
4568 | * with the bdrv_create() call above which does not | |
4569 | * support image-opts style. | |
4570 | */ | |
29cf9336 | 4571 | blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR, |
ea204dda | 4572 | false, false, false); |
86ce1f6e RS |
4573 | |
4574 | if (!blk2) { | |
4575 | ret = -1; | |
4576 | goto out; | |
4577 | } | |
4578 | ||
f7c15533 RS |
4579 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
4580 | size < in.offset * in.bsz)) { | |
4581 | /* We give a warning if the skip option is bigger than the input | |
4582 | * size and create an empty output disk image (i.e. like dd(1)). | |
4583 | */ | |
4584 | error_report("%s: cannot skip to specified offset", in.filename); | |
4585 | in_pos = size; | |
4586 | } else { | |
4587 | in_pos = in.offset * in.bsz; | |
4588 | } | |
4589 | ||
86ce1f6e RS |
4590 | in.buf = g_new(uint8_t, in.bsz); |
4591 | ||
f7c15533 | 4592 | for (out_pos = 0; in_pos < size; block_count++) { |
86ce1f6e RS |
4593 | int in_ret, out_ret; |
4594 | ||
4595 | if (in_pos + in.bsz > size) { | |
4596 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); | |
4597 | } else { | |
4598 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); | |
4599 | } | |
4600 | if (in_ret < 0) { | |
4601 | error_report("error while reading from input image file: %s", | |
4602 | strerror(-in_ret)); | |
4603 | ret = -1; | |
4604 | goto out; | |
4605 | } | |
4606 | in_pos += in_ret; | |
4607 | ||
4608 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); | |
4609 | ||
4610 | if (out_ret < 0) { | |
4611 | error_report("error while writing to output image file: %s", | |
4612 | strerror(-out_ret)); | |
4613 | ret = -1; | |
4614 | goto out; | |
4615 | } | |
4616 | out_pos += out_ret; | |
4617 | } | |
4618 | ||
4619 | out: | |
4620 | g_free(arg); | |
4621 | qemu_opts_del(opts); | |
4622 | qemu_opts_free(create_opts); | |
4623 | blk_unref(blk1); | |
4624 | blk_unref(blk2); | |
4625 | g_free(in.filename); | |
4626 | g_free(out.filename); | |
4627 | g_free(in.buf); | |
4628 | g_free(out.buf); | |
4629 | ||
4630 | if (ret) { | |
b6133b8c KW |
4631 | return 1; |
4632 | } | |
4633 | return 0; | |
4634 | } | |
4635 | ||
fd03c2b8 SH |
4636 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
4637 | { | |
4638 | QString *str; | |
4639 | QObject *obj; | |
4640 | Visitor *v = qobject_output_visitor_new(&obj); | |
4641 | ||
4642 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); | |
4643 | visit_complete(v, &obj); | |
4644 | str = qobject_to_json_pretty(obj); | |
4645 | assert(str != NULL); | |
4646 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 4647 | qobject_unref(obj); |
fd03c2b8 | 4648 | visit_free(v); |
cb3e7f08 | 4649 | qobject_unref(str); |
fd03c2b8 SH |
4650 | } |
4651 | ||
4652 | static int img_measure(int argc, char **argv) | |
4653 | { | |
4654 | static const struct option long_options[] = { | |
4655 | {"help", no_argument, 0, 'h'}, | |
4656 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
4657 | {"object", required_argument, 0, OPTION_OBJECT}, | |
4658 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
4659 | {"size", required_argument, 0, OPTION_SIZE}, | |
4660 | {"force-share", no_argument, 0, 'U'}, | |
4661 | {0, 0, 0, 0} | |
4662 | }; | |
4663 | OutputFormat output_format = OFORMAT_HUMAN; | |
4664 | BlockBackend *in_blk = NULL; | |
4665 | BlockDriver *drv; | |
4666 | const char *filename = NULL; | |
4667 | const char *fmt = NULL; | |
4668 | const char *out_fmt = "raw"; | |
4669 | char *options = NULL; | |
4670 | char *snapshot_name = NULL; | |
4671 | bool force_share = false; | |
4672 | QemuOpts *opts = NULL; | |
4673 | QemuOpts *object_opts = NULL; | |
4674 | QemuOpts *sn_opts = NULL; | |
4675 | QemuOptsList *create_opts = NULL; | |
4676 | bool image_opts = false; | |
4677 | uint64_t img_size = UINT64_MAX; | |
4678 | BlockMeasureInfo *info = NULL; | |
4679 | Error *local_err = NULL; | |
4680 | int ret = 1; | |
4681 | int c; | |
4682 | ||
4683 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", | |
4684 | long_options, NULL)) != -1) { | |
4685 | switch (c) { | |
4686 | case '?': | |
4687 | case 'h': | |
4688 | help(); | |
4689 | break; | |
4690 | case 'f': | |
4691 | fmt = optarg; | |
4692 | break; | |
4693 | case 'O': | |
4694 | out_fmt = optarg; | |
4695 | break; | |
4696 | case 'o': | |
4697 | if (!is_valid_option_list(optarg)) { | |
4698 | error_report("Invalid option list: %s", optarg); | |
4699 | goto out; | |
4700 | } | |
4701 | if (!options) { | |
4702 | options = g_strdup(optarg); | |
4703 | } else { | |
4704 | char *old_options = options; | |
4705 | options = g_strdup_printf("%s,%s", options, optarg); | |
4706 | g_free(old_options); | |
4707 | } | |
4708 | break; | |
4709 | case 'l': | |
4710 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
4711 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, | |
4712 | optarg, false); | |
4713 | if (!sn_opts) { | |
4714 | error_report("Failed in parsing snapshot param '%s'", | |
4715 | optarg); | |
4716 | goto out; | |
4717 | } | |
4718 | } else { | |
4719 | snapshot_name = optarg; | |
4720 | } | |
4721 | break; | |
4722 | case 'U': | |
4723 | force_share = true; | |
4724 | break; | |
4725 | case OPTION_OBJECT: | |
4726 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
4727 | optarg, true); | |
4728 | if (!object_opts) { | |
4729 | goto out; | |
4730 | } | |
4731 | break; | |
4732 | case OPTION_IMAGE_OPTS: | |
4733 | image_opts = true; | |
4734 | break; | |
4735 | case OPTION_OUTPUT: | |
4736 | if (!strcmp(optarg, "json")) { | |
4737 | output_format = OFORMAT_JSON; | |
4738 | } else if (!strcmp(optarg, "human")) { | |
4739 | output_format = OFORMAT_HUMAN; | |
4740 | } else { | |
4741 | error_report("--output must be used with human or json " | |
4742 | "as argument."); | |
4743 | goto out; | |
4744 | } | |
4745 | break; | |
4746 | case OPTION_SIZE: | |
4747 | { | |
4748 | int64_t sval; | |
4749 | ||
4750 | sval = cvtnum(optarg); | |
4751 | if (sval < 0) { | |
4752 | if (sval == -ERANGE) { | |
4753 | error_report("Image size must be less than 8 EiB!"); | |
4754 | } else { | |
4755 | error_report("Invalid image size specified! You may use " | |
4756 | "k, M, G, T, P or E suffixes for "); | |
4757 | error_report("kilobytes, megabytes, gigabytes, terabytes, " | |
4758 | "petabytes and exabytes."); | |
4759 | } | |
4760 | goto out; | |
4761 | } | |
4762 | img_size = (uint64_t)sval; | |
4763 | } | |
4764 | break; | |
4765 | } | |
4766 | } | |
4767 | ||
4768 | if (qemu_opts_foreach(&qemu_object_opts, | |
4769 | user_creatable_add_opts_foreach, | |
4770 | NULL, NULL)) { | |
4771 | goto out; | |
4772 | } | |
4773 | ||
4774 | if (argc - optind > 1) { | |
4775 | error_report("At most one filename argument is allowed."); | |
4776 | goto out; | |
4777 | } else if (argc - optind == 1) { | |
4778 | filename = argv[optind]; | |
4779 | } | |
4780 | ||
4781 | if (!filename && | |
4782 | (object_opts || image_opts || fmt || snapshot_name || sn_opts)) { | |
4783 | error_report("--object, --image-opts, -f, and -l " | |
4784 | "require a filename argument."); | |
4785 | goto out; | |
4786 | } | |
4787 | if (filename && img_size != UINT64_MAX) { | |
4788 | error_report("--size N cannot be used together with a filename."); | |
4789 | goto out; | |
4790 | } | |
4791 | if (!filename && img_size == UINT64_MAX) { | |
4792 | error_report("Either --size N or one filename must be specified."); | |
4793 | goto out; | |
4794 | } | |
4795 | ||
4796 | if (filename) { | |
4797 | in_blk = img_open(image_opts, filename, fmt, 0, | |
4798 | false, false, force_share); | |
4799 | if (!in_blk) { | |
4800 | goto out; | |
4801 | } | |
4802 | ||
4803 | if (sn_opts) { | |
4804 | bdrv_snapshot_load_tmp(blk_bs(in_blk), | |
4805 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
4806 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
4807 | &local_err); | |
4808 | } else if (snapshot_name != NULL) { | |
4809 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), | |
4810 | snapshot_name, &local_err); | |
4811 | } | |
4812 | if (local_err) { | |
4813 | error_reportf_err(local_err, "Failed to load snapshot: "); | |
4814 | goto out; | |
4815 | } | |
4816 | } | |
4817 | ||
4818 | drv = bdrv_find_format(out_fmt); | |
4819 | if (!drv) { | |
4820 | error_report("Unknown file format '%s'", out_fmt); | |
4821 | goto out; | |
4822 | } | |
4823 | if (!drv->create_opts) { | |
4824 | error_report("Format driver '%s' does not support image creation", | |
4825 | drv->format_name); | |
4826 | goto out; | |
4827 | } | |
4828 | ||
4829 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
4830 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); | |
4831 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
4832 | if (options) { | |
4833 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
4834 | if (local_err) { | |
4835 | error_report_err(local_err); | |
4836 | error_report("Invalid options for file format '%s'", out_fmt); | |
4837 | goto out; | |
4838 | } | |
4839 | } | |
4840 | if (img_size != UINT64_MAX) { | |
4841 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); | |
4842 | } | |
4843 | ||
4844 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); | |
4845 | if (local_err) { | |
4846 | error_report_err(local_err); | |
4847 | goto out; | |
4848 | } | |
4849 | ||
4850 | if (output_format == OFORMAT_HUMAN) { | |
4851 | printf("required size: %" PRIu64 "\n", info->required); | |
4852 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); | |
4853 | } else { | |
4854 | dump_json_block_measure_info(info); | |
4855 | } | |
4856 | ||
4857 | ret = 0; | |
4858 | ||
4859 | out: | |
4860 | qapi_free_BlockMeasureInfo(info); | |
4861 | qemu_opts_del(object_opts); | |
4862 | qemu_opts_del(opts); | |
4863 | qemu_opts_del(sn_opts); | |
4864 | qemu_opts_free(create_opts); | |
4865 | g_free(options); | |
4866 | blk_unref(in_blk); | |
4867 | return ret; | |
4868 | } | |
b6133b8c | 4869 | |
c227f099 | 4870 | static const img_cmd_t img_cmds[] = { |
153859be SB |
4871 | #define DEF(option, callback, arg_string) \ |
4872 | { option, callback }, | |
4873 | #include "qemu-img-cmds.h" | |
4874 | #undef DEF | |
153859be SB |
4875 | { NULL, NULL, }, |
4876 | }; | |
4877 | ||
ea2384d3 FB |
4878 | int main(int argc, char **argv) |
4879 | { | |
c227f099 | 4880 | const img_cmd_t *cmd; |
153859be | 4881 | const char *cmdname; |
2f78e491 | 4882 | Error *local_error = NULL; |
06a1e0c1 | 4883 | char *trace_file = NULL; |
7db1689c | 4884 | int c; |
7db1689c JC |
4885 | static const struct option long_options[] = { |
4886 | {"help", no_argument, 0, 'h'}, | |
10985131 | 4887 | {"version", no_argument, 0, 'V'}, |
06a1e0c1 | 4888 | {"trace", required_argument, NULL, 'T'}, |
7db1689c JC |
4889 | {0, 0, 0, 0} |
4890 | }; | |
ea2384d3 | 4891 | |
526eda14 MK |
4892 | #ifdef CONFIG_POSIX |
4893 | signal(SIGPIPE, SIG_IGN); | |
4894 | #endif | |
4895 | ||
fe4db84d | 4896 | module_call_init(MODULE_INIT_TRACE); |
53f76e58 | 4897 | error_set_progname(argv[0]); |
10f5bff6 | 4898 | qemu_init_exec_dir(argv[0]); |
53f76e58 | 4899 | |
2f78e491 | 4900 | if (qemu_init_main_loop(&local_error)) { |
565f65d2 | 4901 | error_report_err(local_error); |
2f78e491 CN |
4902 | exit(EXIT_FAILURE); |
4903 | } | |
4904 | ||
e8f2d272 | 4905 | qcrypto_init(&error_fatal); |
c2297088 | 4906 | |
064097d9 | 4907 | module_call_init(MODULE_INIT_QOM); |
ea2384d3 | 4908 | bdrv_init(); |
ac1307ab FZ |
4909 | if (argc < 2) { |
4910 | error_exit("Not enough arguments"); | |
4911 | } | |
153859be | 4912 | |
3babeb15 | 4913 | qemu_add_opts(&qemu_object_opts); |
eb769f74 | 4914 | qemu_add_opts(&qemu_source_opts); |
06a1e0c1 | 4915 | qemu_add_opts(&qemu_trace_opts); |
3babeb15 | 4916 | |
c9192973 | 4917 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
10985131 | 4918 | switch (c) { |
c9192973 SH |
4919 | case ':': |
4920 | missing_argument(argv[optind - 1]); | |
4921 | return 0; | |
4581c16f | 4922 | case '?': |
c9192973 SH |
4923 | unrecognized_option(argv[optind - 1]); |
4924 | return 0; | |
4925 | case 'h': | |
10985131 DL |
4926 | help(); |
4927 | return 0; | |
4928 | case 'V': | |
4929 | printf(QEMU_IMG_VERSION); | |
4930 | return 0; | |
06a1e0c1 DL |
4931 | case 'T': |
4932 | g_free(trace_file); | |
4933 | trace_file = trace_opt_parse(optarg); | |
4934 | break; | |
153859be | 4935 | } |
ea2384d3 | 4936 | } |
153859be | 4937 | |
10985131 | 4938 | cmdname = argv[optind]; |
7db1689c | 4939 | |
10985131 DL |
4940 | /* reset getopt_long scanning */ |
4941 | argc -= optind; | |
4942 | if (argc < 1) { | |
5f6979cb JC |
4943 | return 0; |
4944 | } | |
10985131 | 4945 | argv += optind; |
cfef6a45 | 4946 | optind = 0; |
10985131 | 4947 | |
06a1e0c1 DL |
4948 | if (!trace_init_backends()) { |
4949 | exit(1); | |
4950 | } | |
4951 | trace_init_file(trace_file); | |
4952 | qemu_set_log(LOG_TRACE); | |
4953 | ||
10985131 DL |
4954 | /* find the command */ |
4955 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { | |
4956 | if (!strcmp(cmdname, cmd->name)) { | |
4957 | return cmd->handler(argc, argv); | |
4958 | } | |
4959 | } | |
7db1689c | 4960 | |
153859be | 4961 | /* not found */ |
ac1307ab | 4962 | error_exit("Command not found: %s", cmdname); |
ea2384d3 | 4963 | } |