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