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