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