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