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