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