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