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