]>
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; | |
351c8eff | 1559 | bool unallocated_blocks_are_zero; |
690c7301 | 1560 | bool target_has_backing; |
351c8eff | 1561 | int64_t target_backing_sectors; /* negative if unknown */ |
2d9187bc | 1562 | bool wr_in_order; |
ee5306d0 | 1563 | bool copy_range; |
690c7301 KW |
1564 | int min_sparse; |
1565 | size_t cluster_sectors; | |
1566 | size_t buf_sectors; | |
9fd77f99 | 1567 | long num_coroutines; |
2d9187bc PL |
1568 | int running_coroutines; |
1569 | Coroutine *co[MAX_COROUTINES]; | |
1570 | int64_t wait_sector_num[MAX_COROUTINES]; | |
1571 | CoMutex lock; | |
1572 | int ret; | |
690c7301 KW |
1573 | } ImgConvertState; |
1574 | ||
2d9187bc PL |
1575 | static void convert_select_part(ImgConvertState *s, int64_t sector_num, |
1576 | int *src_cur, int64_t *src_cur_offset) | |
690c7301 | 1577 | { |
2d9187bc PL |
1578 | *src_cur = 0; |
1579 | *src_cur_offset = 0; | |
1580 | while (sector_num - *src_cur_offset >= s->src_sectors[*src_cur]) { | |
1581 | *src_cur_offset += s->src_sectors[*src_cur]; | |
1582 | (*src_cur)++; | |
1583 | assert(*src_cur < s->src_num); | |
690c7301 KW |
1584 | } |
1585 | } | |
1586 | ||
1587 | static int convert_iteration_sectors(ImgConvertState *s, int64_t sector_num) | |
1588 | { | |
31826642 EB |
1589 | int64_t src_cur_offset; |
1590 | int ret, n, src_cur; | |
351c8eff | 1591 | bool post_backing_zero = false; |
690c7301 | 1592 | |
2d9187bc | 1593 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
690c7301 KW |
1594 | |
1595 | assert(s->total_sectors > sector_num); | |
1596 | n = MIN(s->total_sectors - sector_num, BDRV_REQUEST_MAX_SECTORS); | |
1597 | ||
351c8eff HR |
1598 | if (s->target_backing_sectors >= 0) { |
1599 | if (sector_num >= s->target_backing_sectors) { | |
1600 | post_backing_zero = s->unallocated_blocks_are_zero; | |
1601 | } else if (sector_num + n > s->target_backing_sectors) { | |
1602 | /* Split requests around target_backing_sectors (because | |
1603 | * starting from there, zeros are handled differently) */ | |
1604 | n = s->target_backing_sectors - sector_num; | |
1605 | } | |
1606 | } | |
1607 | ||
690c7301 | 1608 | if (s->sector_next_status <= sector_num) { |
31826642 EB |
1609 | int64_t count = n * BDRV_SECTOR_SIZE; |
1610 | ||
9f1b92ad | 1611 | if (s->target_has_backing) { |
237d78f8 EB |
1612 | |
1613 | ret = bdrv_block_status(blk_bs(s->src[src_cur]), | |
1614 | (sector_num - src_cur_offset) * | |
1615 | BDRV_SECTOR_SIZE, | |
1616 | count, &count, NULL, NULL); | |
9f1b92ad | 1617 | } else { |
31826642 EB |
1618 | ret = bdrv_block_status_above(blk_bs(s->src[src_cur]), NULL, |
1619 | (sector_num - src_cur_offset) * | |
1620 | BDRV_SECTOR_SIZE, | |
1621 | count, &count, NULL, NULL); | |
9f1b92ad | 1622 | } |
690c7301 KW |
1623 | if (ret < 0) { |
1624 | return ret; | |
1625 | } | |
31826642 | 1626 | n = DIV_ROUND_UP(count, BDRV_SECTOR_SIZE); |
690c7301 KW |
1627 | |
1628 | if (ret & BDRV_BLOCK_ZERO) { | |
351c8eff | 1629 | s->status = post_backing_zero ? BLK_BACKING_FILE : BLK_ZERO; |
690c7301 KW |
1630 | } else if (ret & BDRV_BLOCK_DATA) { |
1631 | s->status = BLK_DATA; | |
690c7301 | 1632 | } else { |
9f1b92ad | 1633 | s->status = s->target_has_backing ? BLK_BACKING_FILE : BLK_DATA; |
690c7301 KW |
1634 | } |
1635 | ||
1636 | s->sector_next_status = sector_num + n; | |
1637 | } | |
1638 | ||
1639 | n = MIN(n, s->sector_next_status - sector_num); | |
1640 | if (s->status == BLK_DATA) { | |
1641 | n = MIN(n, s->buf_sectors); | |
1642 | } | |
1643 | ||
1644 | /* We need to write complete clusters for compressed images, so if an | |
1645 | * unallocated area is shorter than that, we must consider the whole | |
1646 | * cluster allocated. */ | |
1647 | if (s->compressed) { | |
1648 | if (n < s->cluster_sectors) { | |
1649 | n = MIN(s->cluster_sectors, s->total_sectors - sector_num); | |
1650 | s->status = BLK_DATA; | |
1651 | } else { | |
1652 | n = QEMU_ALIGN_DOWN(n, s->cluster_sectors); | |
1653 | } | |
1654 | } | |
1655 | ||
1656 | return n; | |
1657 | } | |
1658 | ||
2d9187bc PL |
1659 | static int coroutine_fn convert_co_read(ImgConvertState *s, int64_t sector_num, |
1660 | int nb_sectors, uint8_t *buf) | |
690c7301 | 1661 | { |
2d9187bc PL |
1662 | int n, ret; |
1663 | QEMUIOVector qiov; | |
1664 | struct iovec iov; | |
690c7301 | 1665 | |
690c7301 KW |
1666 | assert(nb_sectors <= s->buf_sectors); |
1667 | while (nb_sectors > 0) { | |
1668 | BlockBackend *blk; | |
2d9187bc PL |
1669 | int src_cur; |
1670 | int64_t bs_sectors, src_cur_offset; | |
690c7301 KW |
1671 | |
1672 | /* In the case of compression with multiple source files, we can get a | |
1673 | * nb_sectors that spreads into the next part. So we must be able to | |
1674 | * read across multiple BDSes for one convert_read() call. */ | |
2d9187bc PL |
1675 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); |
1676 | blk = s->src[src_cur]; | |
1677 | bs_sectors = s->src_sectors[src_cur]; | |
1678 | ||
1679 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); | |
1680 | iov.iov_base = buf; | |
1681 | iov.iov_len = n << BDRV_SECTOR_BITS; | |
1682 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1683 | ||
1684 | ret = blk_co_preadv( | |
1685 | blk, (sector_num - src_cur_offset) << BDRV_SECTOR_BITS, | |
1686 | n << BDRV_SECTOR_BITS, &qiov, 0); | |
690c7301 KW |
1687 | if (ret < 0) { |
1688 | return ret; | |
1689 | } | |
1690 | ||
1691 | sector_num += n; | |
1692 | nb_sectors -= n; | |
1693 | buf += n * BDRV_SECTOR_SIZE; | |
1694 | } | |
1695 | ||
1696 | return 0; | |
1697 | } | |
1698 | ||
2d9187bc PL |
1699 | |
1700 | static int coroutine_fn convert_co_write(ImgConvertState *s, int64_t sector_num, | |
1701 | int nb_sectors, uint8_t *buf, | |
1702 | enum ImgConvertBlockStatus status) | |
690c7301 KW |
1703 | { |
1704 | int ret; | |
2d9187bc PL |
1705 | QEMUIOVector qiov; |
1706 | struct iovec iov; | |
690c7301 KW |
1707 | |
1708 | while (nb_sectors > 0) { | |
1709 | int n = nb_sectors; | |
db933fbe LC |
1710 | BdrvRequestFlags flags = s->compressed ? BDRV_REQ_WRITE_COMPRESSED : 0; |
1711 | ||
2d9187bc | 1712 | switch (status) { |
690c7301 KW |
1713 | case BLK_BACKING_FILE: |
1714 | /* If we have a backing file, leave clusters unallocated that are | |
1715 | * unallocated in the source image, so that the backing file is | |
1716 | * visible at the respective offset. */ | |
1717 | assert(s->target_has_backing); | |
1718 | break; | |
1719 | ||
1720 | case BLK_DATA: | |
db933fbe LC |
1721 | /* If we're told to keep the target fully allocated (-S 0) or there |
1722 | * is real non-zero data, we must write it. Otherwise we can treat | |
1723 | * it as zero sectors. | |
1724 | * Compressed clusters need to be written as a whole, so in that | |
1725 | * case we can only save the write if the buffer is completely | |
1726 | * zeroed. */ | |
690c7301 | 1727 | if (!s->min_sparse || |
db933fbe LC |
1728 | (!s->compressed && |
1729 | is_allocated_sectors_min(buf, n, &n, s->min_sparse)) || | |
1730 | (s->compressed && | |
1731 | !buffer_is_zero(buf, n * BDRV_SECTOR_SIZE))) | |
690c7301 | 1732 | { |
2d9187bc PL |
1733 | iov.iov_base = buf; |
1734 | iov.iov_len = n << BDRV_SECTOR_BITS; | |
1735 | qemu_iovec_init_external(&qiov, &iov, 1); | |
1736 | ||
1737 | ret = blk_co_pwritev(s->target, sector_num << BDRV_SECTOR_BITS, | |
db933fbe | 1738 | n << BDRV_SECTOR_BITS, &qiov, flags); |
690c7301 KW |
1739 | if (ret < 0) { |
1740 | return ret; | |
1741 | } | |
1742 | break; | |
1743 | } | |
1744 | /* fall-through */ | |
1745 | ||
1746 | case BLK_ZERO: | |
1747 | if (s->has_zero_init) { | |
db933fbe | 1748 | assert(!s->target_has_backing); |
690c7301 KW |
1749 | break; |
1750 | } | |
2d9187bc PL |
1751 | ret = blk_co_pwrite_zeroes(s->target, |
1752 | sector_num << BDRV_SECTOR_BITS, | |
1753 | n << BDRV_SECTOR_BITS, 0); | |
690c7301 KW |
1754 | if (ret < 0) { |
1755 | return ret; | |
1756 | } | |
1757 | break; | |
1758 | } | |
1759 | ||
1760 | sector_num += n; | |
1761 | nb_sectors -= n; | |
1762 | buf += n * BDRV_SECTOR_SIZE; | |
1763 | } | |
1764 | ||
1765 | return 0; | |
1766 | } | |
1767 | ||
ee5306d0 FZ |
1768 | static int coroutine_fn convert_co_copy_range(ImgConvertState *s, int64_t sector_num, |
1769 | int nb_sectors) | |
1770 | { | |
1771 | int n, ret; | |
1772 | ||
1773 | while (nb_sectors > 0) { | |
1774 | BlockBackend *blk; | |
1775 | int src_cur; | |
1776 | int64_t bs_sectors, src_cur_offset; | |
1777 | int64_t offset; | |
1778 | ||
1779 | convert_select_part(s, sector_num, &src_cur, &src_cur_offset); | |
1780 | offset = (sector_num - src_cur_offset) << BDRV_SECTOR_BITS; | |
1781 | blk = s->src[src_cur]; | |
1782 | bs_sectors = s->src_sectors[src_cur]; | |
1783 | ||
1784 | n = MIN(nb_sectors, bs_sectors - (sector_num - src_cur_offset)); | |
1785 | ||
1786 | ret = blk_co_copy_range(blk, offset, s->target, | |
1787 | sector_num << BDRV_SECTOR_BITS, | |
1788 | n << BDRV_SECTOR_BITS, 0); | |
1789 | if (ret < 0) { | |
1790 | return ret; | |
1791 | } | |
1792 | ||
1793 | sector_num += n; | |
1794 | nb_sectors -= n; | |
1795 | } | |
1796 | return 0; | |
1797 | } | |
1798 | ||
2d9187bc | 1799 | static void coroutine_fn convert_co_do_copy(void *opaque) |
690c7301 | 1800 | { |
2d9187bc | 1801 | ImgConvertState *s = opaque; |
690c7301 | 1802 | uint8_t *buf = NULL; |
2d9187bc PL |
1803 | int ret, i; |
1804 | int index = -1; | |
1805 | ||
1806 | for (i = 0; i < s->num_coroutines; i++) { | |
1807 | if (s->co[i] == qemu_coroutine_self()) { | |
1808 | index = i; | |
1809 | break; | |
1810 | } | |
1811 | } | |
1812 | assert(index >= 0); | |
1813 | ||
1814 | s->running_coroutines++; | |
1815 | buf = blk_blockalign(s->target, s->buf_sectors * BDRV_SECTOR_SIZE); | |
1816 | ||
1817 | while (1) { | |
1818 | int n; | |
1819 | int64_t sector_num; | |
1820 | enum ImgConvertBlockStatus status; | |
ee5306d0 | 1821 | bool copy_range; |
2d9187bc PL |
1822 | |
1823 | qemu_co_mutex_lock(&s->lock); | |
1824 | if (s->ret != -EINPROGRESS || s->sector_num >= s->total_sectors) { | |
1825 | qemu_co_mutex_unlock(&s->lock); | |
b91127ed | 1826 | break; |
2d9187bc PL |
1827 | } |
1828 | n = convert_iteration_sectors(s, s->sector_num); | |
1829 | if (n < 0) { | |
1830 | qemu_co_mutex_unlock(&s->lock); | |
1831 | s->ret = n; | |
b91127ed | 1832 | break; |
2d9187bc PL |
1833 | } |
1834 | /* save current sector and allocation status to local variables */ | |
1835 | sector_num = s->sector_num; | |
1836 | status = s->status; | |
1837 | if (!s->min_sparse && s->status == BLK_ZERO) { | |
1838 | n = MIN(n, s->buf_sectors); | |
1839 | } | |
1840 | /* increment global sector counter so that other coroutines can | |
1841 | * already continue reading beyond this request */ | |
1842 | s->sector_num += n; | |
1843 | qemu_co_mutex_unlock(&s->lock); | |
1844 | ||
1845 | if (status == BLK_DATA || (!s->min_sparse && status == BLK_ZERO)) { | |
1846 | s->allocated_done += n; | |
1847 | qemu_progress_print(100.0 * s->allocated_done / | |
1848 | s->allocated_sectors, 0); | |
1849 | } | |
1850 | ||
ee5306d0 FZ |
1851 | retry: |
1852 | copy_range = s->copy_range && s->status == BLK_DATA; | |
1853 | if (status == BLK_DATA && !copy_range) { | |
2d9187bc PL |
1854 | ret = convert_co_read(s, sector_num, n, buf); |
1855 | if (ret < 0) { | |
1856 | error_report("error while reading sector %" PRId64 | |
1857 | ": %s", sector_num, strerror(-ret)); | |
1858 | s->ret = ret; | |
2d9187bc PL |
1859 | } |
1860 | } else if (!s->min_sparse && status == BLK_ZERO) { | |
1861 | status = BLK_DATA; | |
1862 | memset(buf, 0x00, n * BDRV_SECTOR_SIZE); | |
1863 | } | |
1864 | ||
1865 | if (s->wr_in_order) { | |
1866 | /* keep writes in order */ | |
b91127ed | 1867 | while (s->wr_offs != sector_num && s->ret == -EINPROGRESS) { |
2d9187bc PL |
1868 | s->wait_sector_num[index] = sector_num; |
1869 | qemu_coroutine_yield(); | |
1870 | } | |
1871 | s->wait_sector_num[index] = -1; | |
1872 | } | |
1873 | ||
b91127ed | 1874 | if (s->ret == -EINPROGRESS) { |
ee5306d0 FZ |
1875 | if (copy_range) { |
1876 | ret = convert_co_copy_range(s, sector_num, n); | |
1877 | if (ret) { | |
1878 | s->copy_range = false; | |
1879 | goto retry; | |
1880 | } | |
1881 | } else { | |
1882 | ret = convert_co_write(s, sector_num, n, buf, status); | |
1883 | } | |
b91127ed AN |
1884 | if (ret < 0) { |
1885 | error_report("error while writing sector %" PRId64 | |
1886 | ": %s", sector_num, strerror(-ret)); | |
1887 | s->ret = ret; | |
1888 | } | |
2d9187bc PL |
1889 | } |
1890 | ||
1891 | if (s->wr_in_order) { | |
1892 | /* reenter the coroutine that might have waited | |
1893 | * for this write to complete */ | |
1894 | s->wr_offs = sector_num + n; | |
1895 | for (i = 0; i < s->num_coroutines; i++) { | |
1896 | if (s->co[i] && s->wait_sector_num[i] == s->wr_offs) { | |
1897 | /* | |
1898 | * A -> B -> A cannot occur because A has | |
1899 | * s->wait_sector_num[i] == -1 during A -> B. Therefore | |
1900 | * B will never enter A during this time window. | |
1901 | */ | |
1902 | qemu_coroutine_enter(s->co[i]); | |
1903 | break; | |
1904 | } | |
1905 | } | |
1906 | } | |
1907 | } | |
1908 | ||
2d9187bc PL |
1909 | qemu_vfree(buf); |
1910 | s->co[index] = NULL; | |
1911 | s->running_coroutines--; | |
1912 | if (!s->running_coroutines && s->ret == -EINPROGRESS) { | |
1913 | /* the convert job finished successfully */ | |
1914 | s->ret = 0; | |
1915 | } | |
1916 | } | |
1917 | ||
1918 | static int convert_do_copy(ImgConvertState *s) | |
1919 | { | |
1920 | int ret, i, n; | |
1921 | int64_t sector_num = 0; | |
690c7301 KW |
1922 | |
1923 | /* Check whether we have zero initialisation or can get it efficiently */ | |
1924 | s->has_zero_init = s->min_sparse && !s->target_has_backing | |
1925 | ? bdrv_has_zero_init(blk_bs(s->target)) | |
1926 | : false; | |
1927 | ||
1928 | if (!s->has_zero_init && !s->target_has_backing && | |
1929 | bdrv_can_write_zeroes_with_unmap(blk_bs(s->target))) | |
1930 | { | |
720ff280 | 1931 | ret = blk_make_zero(s->target, BDRV_REQ_MAY_UNMAP); |
690c7301 KW |
1932 | if (ret == 0) { |
1933 | s->has_zero_init = true; | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | /* Allocate buffer for copied data. For compressed images, only one cluster | |
1938 | * can be copied at a time. */ | |
1939 | if (s->compressed) { | |
1940 | if (s->cluster_sectors <= 0 || s->cluster_sectors > s->buf_sectors) { | |
1941 | error_report("invalid cluster size"); | |
2d9187bc | 1942 | return -EINVAL; |
690c7301 KW |
1943 | } |
1944 | s->buf_sectors = s->cluster_sectors; | |
1945 | } | |
690c7301 | 1946 | |
690c7301 KW |
1947 | while (sector_num < s->total_sectors) { |
1948 | n = convert_iteration_sectors(s, sector_num); | |
1949 | if (n < 0) { | |
2d9187bc | 1950 | return n; |
690c7301 | 1951 | } |
aad15de4 HR |
1952 | if (s->status == BLK_DATA || (!s->min_sparse && s->status == BLK_ZERO)) |
1953 | { | |
690c7301 KW |
1954 | s->allocated_sectors += n; |
1955 | } | |
1956 | sector_num += n; | |
1957 | } | |
1958 | ||
1959 | /* Do the copy */ | |
690c7301 | 1960 | s->sector_next_status = 0; |
2d9187bc | 1961 | s->ret = -EINPROGRESS; |
690c7301 | 1962 | |
2d9187bc PL |
1963 | qemu_co_mutex_init(&s->lock); |
1964 | for (i = 0; i < s->num_coroutines; i++) { | |
1965 | s->co[i] = qemu_coroutine_create(convert_co_do_copy, s); | |
1966 | s->wait_sector_num[i] = -1; | |
1967 | qemu_coroutine_enter(s->co[i]); | |
1968 | } | |
690c7301 | 1969 | |
b91127ed | 1970 | while (s->running_coroutines) { |
2d9187bc | 1971 | main_loop_wait(false); |
690c7301 KW |
1972 | } |
1973 | ||
2d9187bc | 1974 | if (s->compressed && !s->ret) { |
690c7301 | 1975 | /* signal EOF to align */ |
fe5c1355 | 1976 | ret = blk_pwrite_compressed(s->target, 0, NULL, 0); |
690c7301 | 1977 | if (ret < 0) { |
2d9187bc | 1978 | return ret; |
690c7301 KW |
1979 | } |
1980 | } | |
1981 | ||
2d9187bc | 1982 | return s->ret; |
690c7301 KW |
1983 | } |
1984 | ||
ea2384d3 FB |
1985 | static int img_convert(int argc, char **argv) |
1986 | { | |
9fd77f99 | 1987 | int c, bs_i, flags, src_flags = 0; |
305b4c60 | 1988 | const char *fmt = NULL, *out_fmt = NULL, *cache = "unsafe", |
9fd77f99 PL |
1989 | *src_cache = BDRV_DEFAULT_CACHE, *out_baseimg = NULL, |
1990 | *out_filename, *out_baseimg_param, *snapshot_name = NULL; | |
305b4c60 | 1991 | BlockDriver *drv = NULL, *proto_drv = NULL; |
faea38e7 | 1992 | BlockDriverInfo bdi; |
9fd77f99 PL |
1993 | BlockDriverState *out_bs; |
1994 | QemuOpts *opts = NULL, *sn_opts = NULL; | |
83d0521a | 1995 | QemuOptsList *create_opts = NULL; |
efa84d43 | 1996 | char *options = NULL; |
cc84d90f | 1997 | Error *local_err = NULL; |
9fd77f99 | 1998 | bool writethrough, src_writethrough, quiet = false, image_opts = false, |
305b4c60 | 1999 | skip_create = false, progress = false, tgt_image_opts = false; |
9fd77f99 | 2000 | int64_t ret = -EINVAL; |
335e9937 | 2001 | bool force_share = false; |
9fd77f99 PL |
2002 | |
2003 | ImgConvertState s = (ImgConvertState) { | |
2004 | /* Need at least 4k of zeros for sparse detection */ | |
2005 | .min_sparse = 8, | |
ee5306d0 | 2006 | .copy_range = true, |
9fd77f99 PL |
2007 | .buf_sectors = IO_BUF_SIZE / BDRV_SECTOR_SIZE, |
2008 | .wr_in_order = true, | |
2009 | .num_coroutines = 8, | |
2010 | }; | |
ea2384d3 | 2011 | |
ea2384d3 | 2012 | for(;;) { |
3babeb15 DB |
2013 | static const struct option long_options[] = { |
2014 | {"help", no_argument, 0, 'h'}, | |
2015 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 2016 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2017 | {"force-share", no_argument, 0, 'U'}, |
305b4c60 | 2018 | {"target-image-opts", no_argument, 0, OPTION_TARGET_IMAGE_OPTS}, |
3babeb15 DB |
2019 | {0, 0, 0, 0} |
2020 | }; | |
6b4df548 | 2021 | c = getopt_long(argc, argv, ":hf:O:B:co:s:l:S:pt:T:qnm:WU", |
3babeb15 | 2022 | long_options, NULL); |
b8fb60da | 2023 | if (c == -1) { |
ea2384d3 | 2024 | break; |
b8fb60da | 2025 | } |
ea2384d3 | 2026 | switch(c) { |
c9192973 SH |
2027 | case ':': |
2028 | missing_argument(argv[optind - 1]); | |
2029 | break; | |
ef87394c | 2030 | case '?': |
c9192973 SH |
2031 | unrecognized_option(argv[optind - 1]); |
2032 | break; | |
ea2384d3 FB |
2033 | case 'h': |
2034 | help(); | |
2035 | break; | |
2036 | case 'f': | |
2037 | fmt = optarg; | |
2038 | break; | |
2039 | case 'O': | |
2040 | out_fmt = optarg; | |
2041 | break; | |
f58c7b35 TS |
2042 | case 'B': |
2043 | out_baseimg = optarg; | |
2044 | break; | |
ea2384d3 | 2045 | case 'c': |
9fd77f99 | 2046 | s.compressed = true; |
ee5306d0 | 2047 | s.copy_range = false; |
ea2384d3 | 2048 | break; |
efa84d43 | 2049 | case 'o': |
2dc8328b KW |
2050 | if (!is_valid_option_list(optarg)) { |
2051 | error_report("Invalid option list: %s", optarg); | |
64bb01aa | 2052 | goto fail_getopt; |
2dc8328b KW |
2053 | } |
2054 | if (!options) { | |
2055 | options = g_strdup(optarg); | |
2056 | } else { | |
2057 | char *old_options = options; | |
2058 | options = g_strdup_printf("%s,%s", options, optarg); | |
2059 | g_free(old_options); | |
2060 | } | |
efa84d43 | 2061 | break; |
51ef6727 | 2062 | case 's': |
2063 | snapshot_name = optarg; | |
2064 | break; | |
ef80654d WX |
2065 | case 'l': |
2066 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
70b94331 MA |
2067 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, |
2068 | optarg, false); | |
ef80654d WX |
2069 | if (!sn_opts) { |
2070 | error_report("Failed in parsing snapshot param '%s'", | |
2071 | optarg); | |
64bb01aa | 2072 | goto fail_getopt; |
ef80654d WX |
2073 | } |
2074 | } else { | |
2075 | snapshot_name = optarg; | |
2076 | } | |
2077 | break; | |
a22f123c KW |
2078 | case 'S': |
2079 | { | |
2080 | int64_t sval; | |
606caa0a MA |
2081 | |
2082 | sval = cvtnum(optarg); | |
2083 | if (sval < 0) { | |
a22f123c | 2084 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
64bb01aa | 2085 | goto fail_getopt; |
a22f123c KW |
2086 | } |
2087 | ||
9fd77f99 | 2088 | s.min_sparse = sval / BDRV_SECTOR_SIZE; |
ee5306d0 | 2089 | s.copy_range = false; |
a22f123c KW |
2090 | break; |
2091 | } | |
6b837bc4 | 2092 | case 'p': |
9fd77f99 | 2093 | progress = true; |
6b837bc4 | 2094 | break; |
661a0f71 FS |
2095 | case 't': |
2096 | cache = optarg; | |
2097 | break; | |
40055951 HR |
2098 | case 'T': |
2099 | src_cache = optarg; | |
2100 | break; | |
f382d43a MR |
2101 | case 'q': |
2102 | quiet = true; | |
2103 | break; | |
b2e10493 | 2104 | case 'n': |
9fd77f99 | 2105 | skip_create = true; |
b2e10493 | 2106 | break; |
2d9187bc | 2107 | case 'm': |
9fd77f99 PL |
2108 | if (qemu_strtol(optarg, NULL, 0, &s.num_coroutines) || |
2109 | s.num_coroutines < 1 || s.num_coroutines > MAX_COROUTINES) { | |
2d9187bc PL |
2110 | error_report("Invalid number of coroutines. Allowed number of" |
2111 | " coroutines is between 1 and %d", MAX_COROUTINES); | |
2d9187bc PL |
2112 | goto fail_getopt; |
2113 | } | |
2114 | break; | |
2115 | case 'W': | |
9fd77f99 | 2116 | s.wr_in_order = false; |
2d9187bc | 2117 | break; |
335e9937 FZ |
2118 | case 'U': |
2119 | force_share = true; | |
2120 | break; | |
3258b911 HR |
2121 | case OPTION_OBJECT: { |
2122 | QemuOpts *object_opts; | |
2123 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2124 | optarg, true); | |
2125 | if (!object_opts) { | |
3babeb15 DB |
2126 | goto fail_getopt; |
2127 | } | |
2128 | break; | |
3258b911 | 2129 | } |
eb769f74 DB |
2130 | case OPTION_IMAGE_OPTS: |
2131 | image_opts = true; | |
2132 | break; | |
305b4c60 DB |
2133 | case OPTION_TARGET_IMAGE_OPTS: |
2134 | tgt_image_opts = true; | |
2135 | break; | |
ea2384d3 FB |
2136 | } |
2137 | } | |
3b46e624 | 2138 | |
305b4c60 DB |
2139 | if (!out_fmt && !tgt_image_opts) { |
2140 | out_fmt = "raw"; | |
2141 | } | |
2142 | ||
3babeb15 DB |
2143 | if (qemu_opts_foreach(&qemu_object_opts, |
2144 | user_creatable_add_opts_foreach, | |
51b9b478 | 2145 | NULL, NULL)) { |
3babeb15 DB |
2146 | goto fail_getopt; |
2147 | } | |
2148 | ||
9fd77f99 | 2149 | if (!s.wr_in_order && s.compressed) { |
2d9187bc | 2150 | error_report("Out of order write and compress are mutually exclusive"); |
2d9187bc PL |
2151 | goto fail_getopt; |
2152 | } | |
2153 | ||
305b4c60 DB |
2154 | if (tgt_image_opts && !skip_create) { |
2155 | error_report("--target-image-opts requires use of -n flag"); | |
2156 | goto fail_getopt; | |
2157 | } | |
2158 | ||
9fd77f99 PL |
2159 | s.src_num = argc - optind - 1; |
2160 | out_filename = s.src_num >= 1 ? argv[argc - 1] : NULL; | |
f58c7b35 | 2161 | |
2dc8328b | 2162 | if (options && has_help_option(options)) { |
305b4c60 DB |
2163 | if (out_fmt) { |
2164 | ret = print_block_option_help(out_filename, out_fmt); | |
2165 | goto fail_getopt; | |
2166 | } else { | |
2167 | error_report("Option help requires a format be specified"); | |
2168 | goto fail_getopt; | |
2169 | } | |
4ac8aacd JS |
2170 | } |
2171 | ||
9fd77f99 PL |
2172 | if (s.src_num < 1) { |
2173 | error_report("Must specify image file name"); | |
2174 | goto fail_getopt; | |
a283cb6e KW |
2175 | } |
2176 | ||
2177 | ||
9fd77f99 | 2178 | /* ret is still -EINVAL until here */ |
ce099547 | 2179 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); |
40055951 HR |
2180 | if (ret < 0) { |
2181 | error_report("Invalid source cache option: %s", src_cache); | |
9fd77f99 | 2182 | goto fail_getopt; |
40055951 HR |
2183 | } |
2184 | ||
9fd77f99 PL |
2185 | /* Initialize before goto out */ |
2186 | if (quiet) { | |
2187 | progress = false; | |
2188 | } | |
2189 | qemu_progress_init(progress, 1.0); | |
6b837bc4 JS |
2190 | qemu_progress_print(0, 100); |
2191 | ||
9fd77f99 PL |
2192 | s.src = g_new0(BlockBackend *, s.src_num); |
2193 | s.src_sectors = g_new(int64_t, s.src_num); | |
926c2d23 | 2194 | |
9fd77f99 PL |
2195 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { |
2196 | s.src[bs_i] = img_open(image_opts, argv[optind + bs_i], | |
335e9937 FZ |
2197 | fmt, src_flags, src_writethrough, quiet, |
2198 | force_share); | |
9fd77f99 | 2199 | if (!s.src[bs_i]) { |
c2abccec MK |
2200 | ret = -1; |
2201 | goto out; | |
2202 | } | |
9fd77f99 PL |
2203 | s.src_sectors[bs_i] = blk_nb_sectors(s.src[bs_i]); |
2204 | if (s.src_sectors[bs_i] < 0) { | |
52bf1e72 | 2205 | error_report("Could not get size of %s: %s", |
9fd77f99 | 2206 | argv[optind + bs_i], strerror(-s.src_sectors[bs_i])); |
52bf1e72 MA |
2207 | ret = -1; |
2208 | goto out; | |
2209 | } | |
9fd77f99 | 2210 | s.total_sectors += s.src_sectors[bs_i]; |
926c2d23 | 2211 | } |
ea2384d3 | 2212 | |
ef80654d | 2213 | if (sn_opts) { |
9fd77f99 | 2214 | bdrv_snapshot_load_tmp(blk_bs(s.src[0]), |
10d6eda1 PM |
2215 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), |
2216 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
2217 | &local_err); | |
ef80654d | 2218 | } else if (snapshot_name != NULL) { |
9fd77f99 | 2219 | if (s.src_num > 1) { |
6daf194d | 2220 | error_report("No support for concatenating multiple snapshot"); |
51ef6727 | 2221 | ret = -1; |
2222 | goto out; | |
2223 | } | |
7b4c4781 | 2224 | |
9fd77f99 PL |
2225 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(s.src[0]), snapshot_name, |
2226 | &local_err); | |
ef80654d | 2227 | } |
84d18f06 | 2228 | if (local_err) { |
c29b77f9 | 2229 | error_reportf_err(local_err, "Failed to load snapshot: "); |
ef80654d WX |
2230 | ret = -1; |
2231 | goto out; | |
51ef6727 | 2232 | } |
2233 | ||
305b4c60 DB |
2234 | if (!skip_create) { |
2235 | /* Find driver and parse its options */ | |
2236 | drv = bdrv_find_format(out_fmt); | |
2237 | if (!drv) { | |
2238 | error_report("Unknown file format '%s'", out_fmt); | |
2239 | ret = -1; | |
2240 | goto out; | |
2241 | } | |
efa84d43 | 2242 | |
305b4c60 DB |
2243 | proto_drv = bdrv_find_protocol(out_filename, true, &local_err); |
2244 | if (!proto_drv) { | |
2245 | error_report_err(local_err); | |
2246 | ret = -1; | |
2247 | goto out; | |
2248 | } | |
b50cbabc | 2249 | |
2e024cde HR |
2250 | if (!drv->create_opts) { |
2251 | error_report("Format driver '%s' does not support image creation", | |
2252 | drv->format_name); | |
2253 | ret = -1; | |
2254 | goto out; | |
2255 | } | |
f75613cf | 2256 | |
2e024cde HR |
2257 | if (!proto_drv->create_opts) { |
2258 | error_report("Protocol driver '%s' does not support image creation", | |
2259 | proto_drv->format_name); | |
2260 | ret = -1; | |
2261 | goto out; | |
2262 | } | |
f75613cf | 2263 | |
2e024cde HR |
2264 | create_opts = qemu_opts_append(create_opts, drv->create_opts); |
2265 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
db08adf5 | 2266 | |
2e024cde | 2267 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
dc523cd3 MA |
2268 | if (options) { |
2269 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
2270 | if (local_err) { | |
97a2ca7a | 2271 | error_report_err(local_err); |
dc523cd3 MA |
2272 | ret = -1; |
2273 | goto out; | |
2274 | } | |
2e024cde | 2275 | } |
efa84d43 | 2276 | |
9fd77f99 | 2277 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, s.total_sectors * 512, |
39101f25 | 2278 | &error_abort); |
2e024cde HR |
2279 | ret = add_old_style_options(out_fmt, opts, out_baseimg, NULL); |
2280 | if (ret < 0) { | |
2281 | goto out; | |
2282 | } | |
c2abccec | 2283 | } |
efa84d43 | 2284 | |
a18953fb | 2285 | /* Get backing file name if -o backing_file was used */ |
83d0521a | 2286 | out_baseimg_param = qemu_opt_get(opts, BLOCK_OPT_BACKING_FILE); |
a18953fb | 2287 | if (out_baseimg_param) { |
83d0521a | 2288 | out_baseimg = out_baseimg_param; |
a18953fb | 2289 | } |
9fd77f99 | 2290 | s.target_has_backing = (bool) out_baseimg; |
a18953fb | 2291 | |
48758a84 HR |
2292 | if (s.src_num > 1 && out_baseimg) { |
2293 | error_report("Having a backing file for the target makes no sense when " | |
2294 | "concatenating multiple input images"); | |
2295 | ret = -1; | |
2296 | goto out; | |
2297 | } | |
2298 | ||
efa84d43 | 2299 | /* Check if compression is supported */ |
9fd77f99 | 2300 | if (s.compressed) { |
83d0521a CL |
2301 | bool encryption = |
2302 | qemu_opt_get_bool(opts, BLOCK_OPT_ENCRYPT, false); | |
0cb8d47b DB |
2303 | const char *encryptfmt = |
2304 | qemu_opt_get(opts, BLOCK_OPT_ENCRYPT_FORMAT); | |
83d0521a CL |
2305 | const char *preallocation = |
2306 | qemu_opt_get(opts, BLOCK_OPT_PREALLOC); | |
efa84d43 | 2307 | |
305b4c60 | 2308 | if (drv && !drv->bdrv_co_pwritev_compressed) { |
15654a6d | 2309 | error_report("Compression not supported for this file format"); |
c2abccec MK |
2310 | ret = -1; |
2311 | goto out; | |
efa84d43 KW |
2312 | } |
2313 | ||
0cb8d47b | 2314 | if (encryption || encryptfmt) { |
15654a6d JS |
2315 | error_report("Compression and encryption not supported at " |
2316 | "the same time"); | |
c2abccec MK |
2317 | ret = -1; |
2318 | goto out; | |
efa84d43 | 2319 | } |
41521fa4 | 2320 | |
83d0521a CL |
2321 | if (preallocation |
2322 | && strcmp(preallocation, "off")) | |
41521fa4 KW |
2323 | { |
2324 | error_report("Compression and preallocation not supported at " | |
2325 | "the same time"); | |
2326 | ret = -1; | |
2327 | goto out; | |
2328 | } | |
efa84d43 KW |
2329 | } |
2330 | ||
b2e10493 AD |
2331 | if (!skip_create) { |
2332 | /* Create the new image */ | |
c282e1fd | 2333 | ret = bdrv_create(drv, out_filename, opts, &local_err); |
b2e10493 | 2334 | if (ret < 0) { |
c29b77f9 MA |
2335 | error_reportf_err(local_err, "%s: error while converting %s: ", |
2336 | out_filename, out_fmt); | |
b2e10493 | 2337 | goto out; |
ea2384d3 FB |
2338 | } |
2339 | } | |
3b46e624 | 2340 | |
9fd77f99 | 2341 | flags = s.min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; |
ce099547 | 2342 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
2343 | if (ret < 0) { |
2344 | error_report("Invalid cache option: %s", cache); | |
bb9cd2ee | 2345 | goto out; |
661a0f71 FS |
2346 | } |
2347 | ||
305b4c60 DB |
2348 | if (skip_create) { |
2349 | s.target = img_open(tgt_image_opts, out_filename, out_fmt, | |
2350 | flags, writethrough, quiet, false); | |
2351 | } else { | |
2352 | /* TODO ultimately we should allow --target-image-opts | |
2353 | * to be used even when -n is not given. | |
2354 | * That has to wait for bdrv_create to be improved | |
2355 | * to allow filenames in option syntax | |
2356 | */ | |
29cf9336 DB |
2357 | s.target = img_open_new_file(out_filename, opts, out_fmt, |
2358 | flags, writethrough, quiet, false); | |
305b4c60 | 2359 | } |
9fd77f99 | 2360 | if (!s.target) { |
c2abccec MK |
2361 | ret = -1; |
2362 | goto out; | |
2363 | } | |
9fd77f99 | 2364 | out_bs = blk_bs(s.target); |
ea2384d3 | 2365 | |
305b4c60 DB |
2366 | if (s.compressed && !out_bs->drv->bdrv_co_pwritev_compressed) { |
2367 | error_report("Compression not supported for this file format"); | |
2368 | ret = -1; | |
2369 | goto out; | |
2370 | } | |
2371 | ||
5def6b80 | 2372 | /* increase bufsectors from the default 4096 (2M) if opt_transfer |
f2521c90 PL |
2373 | * or discard_alignment of the out_bs is greater. Limit to 32768 (16MB) |
2374 | * as maximum. */ | |
9fd77f99 PL |
2375 | s.buf_sectors = MIN(32768, |
2376 | MAX(s.buf_sectors, | |
2377 | MAX(out_bs->bl.opt_transfer >> BDRV_SECTOR_BITS, | |
2378 | out_bs->bl.pdiscard_alignment >> | |
2379 | BDRV_SECTOR_BITS))); | |
f2521c90 | 2380 | |
b2e10493 | 2381 | if (skip_create) { |
9fd77f99 | 2382 | int64_t output_sectors = blk_nb_sectors(s.target); |
43716fa8 | 2383 | if (output_sectors < 0) { |
eec5eb42 | 2384 | error_report("unable to get output image length: %s", |
43716fa8 | 2385 | strerror(-output_sectors)); |
b2e10493 AD |
2386 | ret = -1; |
2387 | goto out; | |
9fd77f99 | 2388 | } else if (output_sectors < s.total_sectors) { |
b2e10493 AD |
2389 | error_report("output file is smaller than input file"); |
2390 | ret = -1; | |
2391 | goto out; | |
2392 | } | |
2393 | } | |
2394 | ||
351c8eff HR |
2395 | if (s.target_has_backing) { |
2396 | /* Errors are treated as "backing length unknown" (which means | |
2397 | * s.target_backing_sectors has to be negative, which it will | |
2398 | * be automatically). The backing file length is used only | |
2399 | * for optimizations, so such a case is not fatal. */ | |
2400 | s.target_backing_sectors = bdrv_nb_sectors(out_bs->backing->bs); | |
2401 | } else { | |
2402 | s.target_backing_sectors = -1; | |
2403 | } | |
2404 | ||
24f833cd PL |
2405 | ret = bdrv_get_info(out_bs, &bdi); |
2406 | if (ret < 0) { | |
9fd77f99 | 2407 | if (s.compressed) { |
15654a6d | 2408 | error_report("could not get block driver info"); |
c2abccec MK |
2409 | goto out; |
2410 | } | |
24f833cd | 2411 | } else { |
9fd77f99 PL |
2412 | s.compressed = s.compressed || bdi.needs_compressed_writes; |
2413 | s.cluster_sectors = bdi.cluster_size / BDRV_SECTOR_SIZE; | |
351c8eff | 2414 | s.unallocated_blocks_are_zero = bdi.unallocated_blocks_are_zero; |
9fd77f99 | 2415 | } |
802c3d4c | 2416 | |
9fd77f99 | 2417 | ret = convert_do_copy(&s); |
c2abccec | 2418 | out: |
13c28af8 PL |
2419 | if (!ret) { |
2420 | qemu_progress_print(100, 0); | |
2421 | } | |
6b837bc4 | 2422 | qemu_progress_end(); |
83d0521a CL |
2423 | qemu_opts_del(opts); |
2424 | qemu_opts_free(create_opts); | |
fbf28a43 | 2425 | qemu_opts_del(sn_opts); |
9fd77f99 PL |
2426 | blk_unref(s.target); |
2427 | if (s.src) { | |
2428 | for (bs_i = 0; bs_i < s.src_num; bs_i++) { | |
2429 | blk_unref(s.src[bs_i]); | |
26f54e9a | 2430 | } |
9fd77f99 | 2431 | g_free(s.src); |
26f54e9a | 2432 | } |
9fd77f99 | 2433 | g_free(s.src_sectors); |
64bb01aa KW |
2434 | fail_getopt: |
2435 | g_free(options); | |
2436 | ||
9fd77f99 | 2437 | return !!ret; |
ea2384d3 FB |
2438 | } |
2439 | ||
57d1a2b6 | 2440 | |
faea38e7 FB |
2441 | static void dump_snapshots(BlockDriverState *bs) |
2442 | { | |
2443 | QEMUSnapshotInfo *sn_tab, *sn; | |
2444 | int nb_sns, i; | |
faea38e7 FB |
2445 | |
2446 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
2447 | if (nb_sns <= 0) | |
2448 | return; | |
2449 | printf("Snapshot list:\n"); | |
5b917044 WX |
2450 | bdrv_snapshot_dump(fprintf, stdout, NULL); |
2451 | printf("\n"); | |
faea38e7 FB |
2452 | for(i = 0; i < nb_sns; i++) { |
2453 | sn = &sn_tab[i]; | |
5b917044 WX |
2454 | bdrv_snapshot_dump(fprintf, stdout, sn); |
2455 | printf("\n"); | |
faea38e7 | 2456 | } |
7267c094 | 2457 | g_free(sn_tab); |
faea38e7 FB |
2458 | } |
2459 | ||
9699bf0d SH |
2460 | static void dump_json_image_info_list(ImageInfoList *list) |
2461 | { | |
9699bf0d | 2462 | QString *str; |
9699bf0d | 2463 | QObject *obj; |
7d5e199a | 2464 | Visitor *v = qobject_output_visitor_new(&obj); |
3b098d56 EB |
2465 | |
2466 | visit_type_ImageInfoList(v, NULL, &list, &error_abort); | |
2467 | visit_complete(v, &obj); | |
9699bf0d SH |
2468 | str = qobject_to_json_pretty(obj); |
2469 | assert(str != NULL); | |
2470 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 2471 | qobject_unref(obj); |
3b098d56 | 2472 | visit_free(v); |
cb3e7f08 | 2473 | qobject_unref(str); |
9699bf0d SH |
2474 | } |
2475 | ||
c054b3fd BC |
2476 | static void dump_json_image_info(ImageInfo *info) |
2477 | { | |
c054b3fd | 2478 | QString *str; |
c054b3fd | 2479 | QObject *obj; |
7d5e199a | 2480 | Visitor *v = qobject_output_visitor_new(&obj); |
3b098d56 EB |
2481 | |
2482 | visit_type_ImageInfo(v, NULL, &info, &error_abort); | |
2483 | visit_complete(v, &obj); | |
c054b3fd BC |
2484 | str = qobject_to_json_pretty(obj); |
2485 | assert(str != NULL); | |
2486 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 2487 | qobject_unref(obj); |
3b098d56 | 2488 | visit_free(v); |
cb3e7f08 | 2489 | qobject_unref(str); |
c054b3fd BC |
2490 | } |
2491 | ||
9699bf0d SH |
2492 | static void dump_human_image_info_list(ImageInfoList *list) |
2493 | { | |
2494 | ImageInfoList *elem; | |
2495 | bool delim = false; | |
2496 | ||
2497 | for (elem = list; elem; elem = elem->next) { | |
2498 | if (delim) { | |
2499 | printf("\n"); | |
2500 | } | |
2501 | delim = true; | |
2502 | ||
5b917044 | 2503 | bdrv_image_info_dump(fprintf, stdout, elem->value); |
9699bf0d SH |
2504 | } |
2505 | } | |
2506 | ||
2507 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) | |
2508 | { | |
2509 | return strcmp(a, b) == 0; | |
2510 | } | |
2511 | ||
2512 | /** | |
2513 | * Open an image file chain and return an ImageInfoList | |
2514 | * | |
2515 | * @filename: topmost image filename | |
2516 | * @fmt: topmost image format (may be NULL to autodetect) | |
2517 | * @chain: true - enumerate entire backing file chain | |
2518 | * false - only topmost image file | |
2519 | * | |
2520 | * Returns a list of ImageInfo objects or NULL if there was an error opening an | |
2521 | * image file. If there was an error a message will have been printed to | |
2522 | * stderr. | |
2523 | */ | |
eb769f74 DB |
2524 | static ImageInfoList *collect_image_info_list(bool image_opts, |
2525 | const char *filename, | |
9699bf0d | 2526 | const char *fmt, |
335e9937 | 2527 | bool chain, bool force_share) |
9699bf0d SH |
2528 | { |
2529 | ImageInfoList *head = NULL; | |
2530 | ImageInfoList **last = &head; | |
2531 | GHashTable *filenames; | |
43526ec8 | 2532 | Error *err = NULL; |
9699bf0d SH |
2533 | |
2534 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); | |
2535 | ||
2536 | while (filename) { | |
26f54e9a | 2537 | BlockBackend *blk; |
9699bf0d SH |
2538 | BlockDriverState *bs; |
2539 | ImageInfo *info; | |
2540 | ImageInfoList *elem; | |
2541 | ||
2542 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { | |
2543 | error_report("Backing file '%s' creates an infinite loop.", | |
2544 | filename); | |
2545 | goto err; | |
2546 | } | |
2547 | g_hash_table_insert(filenames, (gpointer)filename, NULL); | |
2548 | ||
efaa7c4e | 2549 | blk = img_open(image_opts, filename, fmt, |
335e9937 FZ |
2550 | BDRV_O_NO_BACKING | BDRV_O_NO_IO, false, false, |
2551 | force_share); | |
7e7d56d9 | 2552 | if (!blk) { |
9699bf0d SH |
2553 | goto err; |
2554 | } | |
7e7d56d9 | 2555 | bs = blk_bs(blk); |
9699bf0d | 2556 | |
43526ec8 | 2557 | bdrv_query_image_info(bs, &info, &err); |
84d18f06 | 2558 | if (err) { |
565f65d2 | 2559 | error_report_err(err); |
26f54e9a | 2560 | blk_unref(blk); |
43526ec8 | 2561 | goto err; |
fb0ed453 | 2562 | } |
9699bf0d SH |
2563 | |
2564 | elem = g_new0(ImageInfoList, 1); | |
2565 | elem->value = info; | |
2566 | *last = elem; | |
2567 | last = &elem->next; | |
2568 | ||
26f54e9a | 2569 | blk_unref(blk); |
9699bf0d SH |
2570 | |
2571 | filename = fmt = NULL; | |
2572 | if (chain) { | |
2573 | if (info->has_full_backing_filename) { | |
2574 | filename = info->full_backing_filename; | |
2575 | } else if (info->has_backing_filename) { | |
92d617ab JS |
2576 | error_report("Could not determine absolute backing filename," |
2577 | " but backing filename '%s' present", | |
2578 | info->backing_filename); | |
2579 | goto err; | |
9699bf0d SH |
2580 | } |
2581 | if (info->has_backing_filename_format) { | |
2582 | fmt = info->backing_filename_format; | |
2583 | } | |
2584 | } | |
2585 | } | |
2586 | g_hash_table_destroy(filenames); | |
2587 | return head; | |
2588 | ||
2589 | err: | |
2590 | qapi_free_ImageInfoList(head); | |
2591 | g_hash_table_destroy(filenames); | |
2592 | return NULL; | |
2593 | } | |
2594 | ||
c054b3fd BC |
2595 | static int img_info(int argc, char **argv) |
2596 | { | |
2597 | int c; | |
2598 | OutputFormat output_format = OFORMAT_HUMAN; | |
9699bf0d | 2599 | bool chain = false; |
c054b3fd | 2600 | const char *filename, *fmt, *output; |
9699bf0d | 2601 | ImageInfoList *list; |
eb769f74 | 2602 | bool image_opts = false; |
335e9937 | 2603 | bool force_share = false; |
c054b3fd | 2604 | |
ea2384d3 | 2605 | fmt = NULL; |
c054b3fd | 2606 | output = NULL; |
ea2384d3 | 2607 | for(;;) { |
c054b3fd BC |
2608 | int option_index = 0; |
2609 | static const struct option long_options[] = { | |
2610 | {"help", no_argument, 0, 'h'}, | |
2611 | {"format", required_argument, 0, 'f'}, | |
2612 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
9699bf0d | 2613 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
3babeb15 | 2614 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 2615 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2616 | {"force-share", no_argument, 0, 'U'}, |
c054b3fd BC |
2617 | {0, 0, 0, 0} |
2618 | }; | |
335e9937 | 2619 | c = getopt_long(argc, argv, ":f:hU", |
c054b3fd | 2620 | long_options, &option_index); |
b8fb60da | 2621 | if (c == -1) { |
ea2384d3 | 2622 | break; |
b8fb60da | 2623 | } |
ea2384d3 | 2624 | switch(c) { |
c9192973 SH |
2625 | case ':': |
2626 | missing_argument(argv[optind - 1]); | |
2627 | break; | |
ef87394c | 2628 | case '?': |
c9192973 SH |
2629 | unrecognized_option(argv[optind - 1]); |
2630 | break; | |
ea2384d3 FB |
2631 | case 'h': |
2632 | help(); | |
2633 | break; | |
2634 | case 'f': | |
2635 | fmt = optarg; | |
2636 | break; | |
335e9937 FZ |
2637 | case 'U': |
2638 | force_share = true; | |
2639 | break; | |
c054b3fd BC |
2640 | case OPTION_OUTPUT: |
2641 | output = optarg; | |
2642 | break; | |
9699bf0d SH |
2643 | case OPTION_BACKING_CHAIN: |
2644 | chain = true; | |
2645 | break; | |
3babeb15 DB |
2646 | case OPTION_OBJECT: { |
2647 | QemuOpts *opts; | |
2648 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2649 | optarg, true); | |
2650 | if (!opts) { | |
2651 | return 1; | |
2652 | } | |
2653 | } break; | |
eb769f74 DB |
2654 | case OPTION_IMAGE_OPTS: |
2655 | image_opts = true; | |
2656 | break; | |
ea2384d3 FB |
2657 | } |
2658 | } | |
fc11eb26 | 2659 | if (optind != argc - 1) { |
ac1307ab | 2660 | error_exit("Expecting one image file name"); |
b8fb60da | 2661 | } |
ea2384d3 FB |
2662 | filename = argv[optind++]; |
2663 | ||
c054b3fd BC |
2664 | if (output && !strcmp(output, "json")) { |
2665 | output_format = OFORMAT_JSON; | |
2666 | } else if (output && !strcmp(output, "human")) { | |
2667 | output_format = OFORMAT_HUMAN; | |
2668 | } else if (output) { | |
2669 | error_report("--output must be used with human or json as argument."); | |
c2abccec MK |
2670 | return 1; |
2671 | } | |
c054b3fd | 2672 | |
3babeb15 DB |
2673 | if (qemu_opts_foreach(&qemu_object_opts, |
2674 | user_creatable_add_opts_foreach, | |
51b9b478 | 2675 | NULL, NULL)) { |
3babeb15 DB |
2676 | return 1; |
2677 | } | |
2678 | ||
335e9937 FZ |
2679 | list = collect_image_info_list(image_opts, filename, fmt, chain, |
2680 | force_share); | |
9699bf0d | 2681 | if (!list) { |
c2abccec | 2682 | return 1; |
faea38e7 | 2683 | } |
c054b3fd | 2684 | |
c054b3fd BC |
2685 | switch (output_format) { |
2686 | case OFORMAT_HUMAN: | |
9699bf0d | 2687 | dump_human_image_info_list(list); |
c054b3fd BC |
2688 | break; |
2689 | case OFORMAT_JSON: | |
9699bf0d SH |
2690 | if (chain) { |
2691 | dump_json_image_info_list(list); | |
2692 | } else { | |
2693 | dump_json_image_info(list->value); | |
2694 | } | |
c054b3fd | 2695 | break; |
faea38e7 | 2696 | } |
c054b3fd | 2697 | |
9699bf0d | 2698 | qapi_free_ImageInfoList(list); |
ea2384d3 FB |
2699 | return 0; |
2700 | } | |
2701 | ||
4c93a13b PB |
2702 | static void dump_map_entry(OutputFormat output_format, MapEntry *e, |
2703 | MapEntry *next) | |
2704 | { | |
2705 | switch (output_format) { | |
2706 | case OFORMAT_HUMAN: | |
16b0d555 | 2707 | if (e->data && !e->has_offset) { |
4c93a13b PB |
2708 | error_report("File contains external, encrypted or compressed clusters."); |
2709 | exit(1); | |
2710 | } | |
16b0d555 | 2711 | if (e->data && !e->zero) { |
4c93a13b | 2712 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", |
16b0d555 FZ |
2713 | e->start, e->length, |
2714 | e->has_offset ? e->offset : 0, | |
2715 | e->has_filename ? e->filename : ""); | |
4c93a13b PB |
2716 | } |
2717 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. | |
2718 | * Modify the flags here to allow more coalescing. | |
2719 | */ | |
16b0d555 FZ |
2720 | if (next && (!next->data || next->zero)) { |
2721 | next->data = false; | |
2722 | next->zero = true; | |
4c93a13b PB |
2723 | } |
2724 | break; | |
2725 | case OFORMAT_JSON: | |
16b0d555 FZ |
2726 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64"," |
2727 | " \"depth\": %"PRId64", \"zero\": %s, \"data\": %s", | |
4c93a13b PB |
2728 | (e->start == 0 ? "[" : ",\n"), |
2729 | e->start, e->length, e->depth, | |
16b0d555 FZ |
2730 | e->zero ? "true" : "false", |
2731 | e->data ? "true" : "false"); | |
2732 | if (e->has_offset) { | |
c745bfb4 | 2733 | printf(", \"offset\": %"PRId64"", e->offset); |
4c93a13b PB |
2734 | } |
2735 | putchar('}'); | |
2736 | ||
2737 | if (!next) { | |
2738 | printf("]\n"); | |
2739 | } | |
2740 | break; | |
2741 | } | |
2742 | } | |
2743 | ||
5e344dd8 EB |
2744 | static int get_block_status(BlockDriverState *bs, int64_t offset, |
2745 | int64_t bytes, MapEntry *e) | |
4c93a13b | 2746 | { |
237d78f8 | 2747 | int ret; |
4c93a13b | 2748 | int depth; |
67a0fd2a | 2749 | BlockDriverState *file; |
2875645b | 2750 | bool has_offset; |
237d78f8 | 2751 | int64_t map; |
4c93a13b PB |
2752 | |
2753 | /* As an optimization, we could cache the current range of unallocated | |
2754 | * clusters in each file of the chain, and avoid querying the same | |
2755 | * range repeatedly. | |
2756 | */ | |
2757 | ||
2758 | depth = 0; | |
2759 | for (;;) { | |
237d78f8 | 2760 | ret = bdrv_block_status(bs, offset, bytes, &bytes, &map, &file); |
4c93a13b PB |
2761 | if (ret < 0) { |
2762 | return ret; | |
2763 | } | |
237d78f8 | 2764 | assert(bytes); |
4c93a13b PB |
2765 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { |
2766 | break; | |
2767 | } | |
760e0063 | 2768 | bs = backing_bs(bs); |
4c93a13b PB |
2769 | if (bs == NULL) { |
2770 | ret = 0; | |
2771 | break; | |
2772 | } | |
2773 | ||
2774 | depth++; | |
2775 | } | |
2776 | ||
2875645b JS |
2777 | has_offset = !!(ret & BDRV_BLOCK_OFFSET_VALID); |
2778 | ||
2779 | *e = (MapEntry) { | |
5e344dd8 | 2780 | .start = offset, |
237d78f8 | 2781 | .length = bytes, |
2875645b JS |
2782 | .data = !!(ret & BDRV_BLOCK_DATA), |
2783 | .zero = !!(ret & BDRV_BLOCK_ZERO), | |
237d78f8 | 2784 | .offset = map, |
2875645b JS |
2785 | .has_offset = has_offset, |
2786 | .depth = depth, | |
2787 | .has_filename = file && has_offset, | |
2788 | .filename = file && has_offset ? file->filename : NULL, | |
2789 | }; | |
2790 | ||
4c93a13b PB |
2791 | return 0; |
2792 | } | |
2793 | ||
16b0d555 FZ |
2794 | static inline bool entry_mergeable(const MapEntry *curr, const MapEntry *next) |
2795 | { | |
2796 | if (curr->length == 0) { | |
2797 | return false; | |
2798 | } | |
2799 | if (curr->zero != next->zero || | |
2800 | curr->data != next->data || | |
2801 | curr->depth != next->depth || | |
2802 | curr->has_filename != next->has_filename || | |
2803 | curr->has_offset != next->has_offset) { | |
2804 | return false; | |
2805 | } | |
2806 | if (curr->has_filename && strcmp(curr->filename, next->filename)) { | |
2807 | return false; | |
2808 | } | |
2809 | if (curr->has_offset && curr->offset + curr->length != next->offset) { | |
2810 | return false; | |
2811 | } | |
2812 | return true; | |
2813 | } | |
2814 | ||
4c93a13b PB |
2815 | static int img_map(int argc, char **argv) |
2816 | { | |
2817 | int c; | |
2818 | OutputFormat output_format = OFORMAT_HUMAN; | |
26f54e9a | 2819 | BlockBackend *blk; |
4c93a13b PB |
2820 | BlockDriverState *bs; |
2821 | const char *filename, *fmt, *output; | |
2822 | int64_t length; | |
2823 | MapEntry curr = { .length = 0 }, next; | |
2824 | int ret = 0; | |
eb769f74 | 2825 | bool image_opts = false; |
335e9937 | 2826 | bool force_share = false; |
4c93a13b PB |
2827 | |
2828 | fmt = NULL; | |
2829 | output = NULL; | |
2830 | for (;;) { | |
2831 | int option_index = 0; | |
2832 | static const struct option long_options[] = { | |
2833 | {"help", no_argument, 0, 'h'}, | |
2834 | {"format", required_argument, 0, 'f'}, | |
2835 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
3babeb15 | 2836 | {"object", required_argument, 0, OPTION_OBJECT}, |
eb769f74 | 2837 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2838 | {"force-share", no_argument, 0, 'U'}, |
4c93a13b PB |
2839 | {0, 0, 0, 0} |
2840 | }; | |
335e9937 | 2841 | c = getopt_long(argc, argv, ":f:hU", |
4c93a13b PB |
2842 | long_options, &option_index); |
2843 | if (c == -1) { | |
2844 | break; | |
2845 | } | |
2846 | switch (c) { | |
c9192973 SH |
2847 | case ':': |
2848 | missing_argument(argv[optind - 1]); | |
2849 | break; | |
4c93a13b | 2850 | case '?': |
c9192973 SH |
2851 | unrecognized_option(argv[optind - 1]); |
2852 | break; | |
4c93a13b PB |
2853 | case 'h': |
2854 | help(); | |
2855 | break; | |
2856 | case 'f': | |
2857 | fmt = optarg; | |
2858 | break; | |
335e9937 FZ |
2859 | case 'U': |
2860 | force_share = true; | |
2861 | break; | |
4c93a13b PB |
2862 | case OPTION_OUTPUT: |
2863 | output = optarg; | |
2864 | break; | |
3babeb15 DB |
2865 | case OPTION_OBJECT: { |
2866 | QemuOpts *opts; | |
2867 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
2868 | optarg, true); | |
2869 | if (!opts) { | |
2870 | return 1; | |
2871 | } | |
2872 | } break; | |
eb769f74 DB |
2873 | case OPTION_IMAGE_OPTS: |
2874 | image_opts = true; | |
2875 | break; | |
4c93a13b PB |
2876 | } |
2877 | } | |
ac1307ab FZ |
2878 | if (optind != argc - 1) { |
2879 | error_exit("Expecting one image file name"); | |
4c93a13b | 2880 | } |
ac1307ab | 2881 | filename = argv[optind]; |
4c93a13b PB |
2882 | |
2883 | if (output && !strcmp(output, "json")) { | |
2884 | output_format = OFORMAT_JSON; | |
2885 | } else if (output && !strcmp(output, "human")) { | |
2886 | output_format = OFORMAT_HUMAN; | |
2887 | } else if (output) { | |
2888 | error_report("--output must be used with human or json as argument."); | |
2889 | return 1; | |
2890 | } | |
2891 | ||
3babeb15 DB |
2892 | if (qemu_opts_foreach(&qemu_object_opts, |
2893 | user_creatable_add_opts_foreach, | |
51b9b478 | 2894 | NULL, NULL)) { |
3babeb15 DB |
2895 | return 1; |
2896 | } | |
2897 | ||
335e9937 | 2898 | blk = img_open(image_opts, filename, fmt, 0, false, false, force_share); |
7e7d56d9 MA |
2899 | if (!blk) { |
2900 | return 1; | |
4c93a13b | 2901 | } |
7e7d56d9 | 2902 | bs = blk_bs(blk); |
4c93a13b PB |
2903 | |
2904 | if (output_format == OFORMAT_HUMAN) { | |
2905 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); | |
2906 | } | |
2907 | ||
f1d3cd79 | 2908 | length = blk_getlength(blk); |
4c93a13b | 2909 | while (curr.start + curr.length < length) { |
5e344dd8 EB |
2910 | int64_t offset = curr.start + curr.length; |
2911 | int64_t n; | |
4c93a13b PB |
2912 | |
2913 | /* Probe up to 1 GiB at a time. */ | |
5e344dd8 EB |
2914 | n = QEMU_ALIGN_DOWN(MIN(1 << 30, length - offset), BDRV_SECTOR_SIZE); |
2915 | ret = get_block_status(bs, offset, n, &next); | |
4c93a13b PB |
2916 | |
2917 | if (ret < 0) { | |
2918 | error_report("Could not read file metadata: %s", strerror(-ret)); | |
2919 | goto out; | |
2920 | } | |
2921 | ||
16b0d555 | 2922 | if (entry_mergeable(&curr, &next)) { |
4c93a13b PB |
2923 | curr.length += next.length; |
2924 | continue; | |
2925 | } | |
2926 | ||
2927 | if (curr.length > 0) { | |
2928 | dump_map_entry(output_format, &curr, &next); | |
2929 | } | |
2930 | curr = next; | |
2931 | } | |
2932 | ||
2933 | dump_map_entry(output_format, &curr, NULL); | |
2934 | ||
2935 | out: | |
26f54e9a | 2936 | blk_unref(blk); |
4c93a13b PB |
2937 | return ret < 0; |
2938 | } | |
2939 | ||
f7b4a940 AL |
2940 | #define SNAPSHOT_LIST 1 |
2941 | #define SNAPSHOT_CREATE 2 | |
2942 | #define SNAPSHOT_APPLY 3 | |
2943 | #define SNAPSHOT_DELETE 4 | |
2944 | ||
153859be | 2945 | static int img_snapshot(int argc, char **argv) |
f7b4a940 | 2946 | { |
26f54e9a | 2947 | BlockBackend *blk; |
f7b4a940 AL |
2948 | BlockDriverState *bs; |
2949 | QEMUSnapshotInfo sn; | |
2950 | char *filename, *snapshot_name = NULL; | |
c2abccec | 2951 | int c, ret = 0, bdrv_oflags; |
f7b4a940 AL |
2952 | int action = 0; |
2953 | qemu_timeval tv; | |
f382d43a | 2954 | bool quiet = false; |
a89d89d3 | 2955 | Error *err = NULL; |
eb769f74 | 2956 | bool image_opts = false; |
335e9937 | 2957 | bool force_share = false; |
f7b4a940 | 2958 | |
ce099547 | 2959 | bdrv_oflags = BDRV_O_RDWR; |
f7b4a940 AL |
2960 | /* Parse commandline parameters */ |
2961 | for(;;) { | |
3babeb15 DB |
2962 | static const struct option long_options[] = { |
2963 | {"help", no_argument, 0, 'h'}, | |
2964 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 2965 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 2966 | {"force-share", no_argument, 0, 'U'}, |
3babeb15 DB |
2967 | {0, 0, 0, 0} |
2968 | }; | |
335e9937 | 2969 | c = getopt_long(argc, argv, ":la:c:d:hqU", |
3babeb15 | 2970 | long_options, NULL); |
b8fb60da | 2971 | if (c == -1) { |
f7b4a940 | 2972 | break; |
b8fb60da | 2973 | } |
f7b4a940 | 2974 | switch(c) { |
c9192973 SH |
2975 | case ':': |
2976 | missing_argument(argv[optind - 1]); | |
2977 | break; | |
ef87394c | 2978 | case '?': |
c9192973 SH |
2979 | unrecognized_option(argv[optind - 1]); |
2980 | break; | |
f7b4a940 AL |
2981 | case 'h': |
2982 | help(); | |
153859be | 2983 | return 0; |
f7b4a940 AL |
2984 | case 'l': |
2985 | if (action) { | |
ac1307ab | 2986 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 2987 | return 0; |
f7b4a940 AL |
2988 | } |
2989 | action = SNAPSHOT_LIST; | |
f5edb014 | 2990 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
f7b4a940 AL |
2991 | break; |
2992 | case 'a': | |
2993 | if (action) { | |
ac1307ab | 2994 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 2995 | return 0; |
f7b4a940 AL |
2996 | } |
2997 | action = SNAPSHOT_APPLY; | |
2998 | snapshot_name = optarg; | |
2999 | break; | |
3000 | case 'c': | |
3001 | if (action) { | |
ac1307ab | 3002 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3003 | return 0; |
f7b4a940 AL |
3004 | } |
3005 | action = SNAPSHOT_CREATE; | |
3006 | snapshot_name = optarg; | |
3007 | break; | |
3008 | case 'd': | |
3009 | if (action) { | |
ac1307ab | 3010 | error_exit("Cannot mix '-l', '-a', '-c', '-d'"); |
153859be | 3011 | return 0; |
f7b4a940 AL |
3012 | } |
3013 | action = SNAPSHOT_DELETE; | |
3014 | snapshot_name = optarg; | |
3015 | break; | |
f382d43a MR |
3016 | case 'q': |
3017 | quiet = true; | |
3018 | break; | |
335e9937 FZ |
3019 | case 'U': |
3020 | force_share = true; | |
3021 | break; | |
3babeb15 DB |
3022 | case OPTION_OBJECT: { |
3023 | QemuOpts *opts; | |
3024 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3025 | optarg, true); | |
3026 | if (!opts) { | |
3027 | return 1; | |
3028 | } | |
3029 | } break; | |
eb769f74 DB |
3030 | case OPTION_IMAGE_OPTS: |
3031 | image_opts = true; | |
3032 | break; | |
f7b4a940 AL |
3033 | } |
3034 | } | |
3035 | ||
fc11eb26 | 3036 | if (optind != argc - 1) { |
ac1307ab | 3037 | error_exit("Expecting one image file name"); |
b8fb60da | 3038 | } |
f7b4a940 AL |
3039 | filename = argv[optind++]; |
3040 | ||
3babeb15 DB |
3041 | if (qemu_opts_foreach(&qemu_object_opts, |
3042 | user_creatable_add_opts_foreach, | |
51b9b478 | 3043 | NULL, NULL)) { |
3babeb15 DB |
3044 | return 1; |
3045 | } | |
3046 | ||
f7b4a940 | 3047 | /* Open the image */ |
335e9937 FZ |
3048 | blk = img_open(image_opts, filename, NULL, bdrv_oflags, false, quiet, |
3049 | force_share); | |
7e7d56d9 MA |
3050 | if (!blk) { |
3051 | return 1; | |
c2abccec | 3052 | } |
7e7d56d9 | 3053 | bs = blk_bs(blk); |
f7b4a940 AL |
3054 | |
3055 | /* Perform the requested action */ | |
3056 | switch(action) { | |
3057 | case SNAPSHOT_LIST: | |
3058 | dump_snapshots(bs); | |
3059 | break; | |
3060 | ||
3061 | case SNAPSHOT_CREATE: | |
3062 | memset(&sn, 0, sizeof(sn)); | |
3063 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); | |
3064 | ||
3065 | qemu_gettimeofday(&tv); | |
3066 | sn.date_sec = tv.tv_sec; | |
3067 | sn.date_nsec = tv.tv_usec * 1000; | |
3068 | ||
3069 | ret = bdrv_snapshot_create(bs, &sn); | |
b8fb60da | 3070 | if (ret) { |
15654a6d | 3071 | error_report("Could not create snapshot '%s': %d (%s)", |
f7b4a940 | 3072 | snapshot_name, ret, strerror(-ret)); |
b8fb60da | 3073 | } |
f7b4a940 AL |
3074 | break; |
3075 | ||
3076 | case SNAPSHOT_APPLY: | |
0b62bcbc | 3077 | ret = bdrv_snapshot_goto(bs, snapshot_name, &err); |
b8fb60da | 3078 | if (ret) { |
0b62bcbc KW |
3079 | error_reportf_err(err, "Could not apply snapshot '%s': ", |
3080 | snapshot_name); | |
b8fb60da | 3081 | } |
f7b4a940 AL |
3082 | break; |
3083 | ||
3084 | case SNAPSHOT_DELETE: | |
a89d89d3 | 3085 | bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); |
84d18f06 | 3086 | if (err) { |
c29b77f9 MA |
3087 | error_reportf_err(err, "Could not delete snapshot '%s': ", |
3088 | snapshot_name); | |
a89d89d3 | 3089 | ret = 1; |
b8fb60da | 3090 | } |
f7b4a940 AL |
3091 | break; |
3092 | } | |
3093 | ||
3094 | /* Cleanup */ | |
26f54e9a | 3095 | blk_unref(blk); |
c2abccec MK |
3096 | if (ret) { |
3097 | return 1; | |
3098 | } | |
153859be | 3099 | return 0; |
f7b4a940 AL |
3100 | } |
3101 | ||
3e85c6fd KW |
3102 | static int img_rebase(int argc, char **argv) |
3103 | { | |
26f54e9a | 3104 | BlockBackend *blk = NULL, *blk_old_backing = NULL, *blk_new_backing = NULL; |
396374ca PB |
3105 | uint8_t *buf_old = NULL; |
3106 | uint8_t *buf_new = NULL; | |
f1d3cd79 | 3107 | BlockDriverState *bs = NULL; |
3e85c6fd | 3108 | char *filename; |
40055951 HR |
3109 | const char *fmt, *cache, *src_cache, *out_basefmt, *out_baseimg; |
3110 | int c, flags, src_flags, ret; | |
ce099547 | 3111 | bool writethrough, src_writethrough; |
3e85c6fd | 3112 | int unsafe = 0; |
335e9937 | 3113 | bool force_share = false; |
6b837bc4 | 3114 | int progress = 0; |
f382d43a | 3115 | bool quiet = false; |
34b5d2c6 | 3116 | Error *local_err = NULL; |
eb769f74 | 3117 | bool image_opts = false; |
3e85c6fd KW |
3118 | |
3119 | /* Parse commandline parameters */ | |
e53dbee0 | 3120 | fmt = NULL; |
661a0f71 | 3121 | cache = BDRV_DEFAULT_CACHE; |
40055951 | 3122 | src_cache = BDRV_DEFAULT_CACHE; |
3e85c6fd KW |
3123 | out_baseimg = NULL; |
3124 | out_basefmt = NULL; | |
3e85c6fd | 3125 | for(;;) { |
3babeb15 DB |
3126 | static const struct option long_options[] = { |
3127 | {"help", no_argument, 0, 'h'}, | |
3128 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3129 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 3130 | {"force-share", no_argument, 0, 'U'}, |
3babeb15 DB |
3131 | {0, 0, 0, 0} |
3132 | }; | |
335e9937 | 3133 | c = getopt_long(argc, argv, ":hf:F:b:upt:T:qU", |
3babeb15 | 3134 | long_options, NULL); |
b8fb60da | 3135 | if (c == -1) { |
3e85c6fd | 3136 | break; |
b8fb60da | 3137 | } |
3e85c6fd | 3138 | switch(c) { |
c9192973 SH |
3139 | case ':': |
3140 | missing_argument(argv[optind - 1]); | |
3141 | break; | |
ef87394c | 3142 | case '?': |
c9192973 SH |
3143 | unrecognized_option(argv[optind - 1]); |
3144 | break; | |
3e85c6fd KW |
3145 | case 'h': |
3146 | help(); | |
3147 | return 0; | |
e53dbee0 KW |
3148 | case 'f': |
3149 | fmt = optarg; | |
3150 | break; | |
3e85c6fd KW |
3151 | case 'F': |
3152 | out_basefmt = optarg; | |
3153 | break; | |
3154 | case 'b': | |
3155 | out_baseimg = optarg; | |
3156 | break; | |
3157 | case 'u': | |
3158 | unsafe = 1; | |
3159 | break; | |
6b837bc4 JS |
3160 | case 'p': |
3161 | progress = 1; | |
3162 | break; | |
661a0f71 FS |
3163 | case 't': |
3164 | cache = optarg; | |
3165 | break; | |
40055951 HR |
3166 | case 'T': |
3167 | src_cache = optarg; | |
3168 | break; | |
f382d43a MR |
3169 | case 'q': |
3170 | quiet = true; | |
3171 | break; | |
3babeb15 DB |
3172 | case OPTION_OBJECT: { |
3173 | QemuOpts *opts; | |
3174 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3175 | optarg, true); | |
3176 | if (!opts) { | |
3177 | return 1; | |
3178 | } | |
3179 | } break; | |
eb769f74 DB |
3180 | case OPTION_IMAGE_OPTS: |
3181 | image_opts = true; | |
3182 | break; | |
335e9937 FZ |
3183 | case 'U': |
3184 | force_share = true; | |
3185 | break; | |
3e85c6fd KW |
3186 | } |
3187 | } | |
3188 | ||
f382d43a MR |
3189 | if (quiet) { |
3190 | progress = 0; | |
3191 | } | |
3192 | ||
ac1307ab FZ |
3193 | if (optind != argc - 1) { |
3194 | error_exit("Expecting one image file name"); | |
3195 | } | |
3196 | if (!unsafe && !out_baseimg) { | |
3197 | error_exit("Must specify backing file (-b) or use unsafe mode (-u)"); | |
b8fb60da | 3198 | } |
3e85c6fd KW |
3199 | filename = argv[optind++]; |
3200 | ||
3babeb15 DB |
3201 | if (qemu_opts_foreach(&qemu_object_opts, |
3202 | user_creatable_add_opts_foreach, | |
51b9b478 | 3203 | NULL, NULL)) { |
3babeb15 DB |
3204 | return 1; |
3205 | } | |
3206 | ||
6b837bc4 JS |
3207 | qemu_progress_init(progress, 2.0); |
3208 | qemu_progress_print(0, 100); | |
3209 | ||
661a0f71 | 3210 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
ce099547 | 3211 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); |
661a0f71 FS |
3212 | if (ret < 0) { |
3213 | error_report("Invalid cache option: %s", cache); | |
40ed35a3 | 3214 | goto out; |
661a0f71 FS |
3215 | } |
3216 | ||
ce099547 KW |
3217 | src_flags = 0; |
3218 | ret = bdrv_parse_cache_mode(src_cache, &src_flags, &src_writethrough); | |
40055951 HR |
3219 | if (ret < 0) { |
3220 | error_report("Invalid source cache option: %s", src_cache); | |
40ed35a3 | 3221 | goto out; |
40055951 HR |
3222 | } |
3223 | ||
ce099547 KW |
3224 | /* The source files are opened read-only, don't care about WCE */ |
3225 | assert((src_flags & BDRV_O_RDWR) == 0); | |
3226 | (void) src_writethrough; | |
3227 | ||
3e85c6fd KW |
3228 | /* |
3229 | * Open the images. | |
3230 | * | |
3231 | * Ignore the old backing file for unsafe rebase in case we want to correct | |
3232 | * the reference to a renamed or moved backing file. | |
3233 | */ | |
335e9937 FZ |
3234 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
3235 | false); | |
7e7d56d9 | 3236 | if (!blk) { |
40ed35a3 SH |
3237 | ret = -1; |
3238 | goto out; | |
c2abccec | 3239 | } |
7e7d56d9 | 3240 | bs = blk_bs(blk); |
3e85c6fd | 3241 | |
3e85c6fd | 3242 | if (out_basefmt != NULL) { |
644483d9 | 3243 | if (bdrv_find_format(out_basefmt) == NULL) { |
15654a6d | 3244 | error_report("Invalid format name: '%s'", out_basefmt); |
c2abccec MK |
3245 | ret = -1; |
3246 | goto out; | |
3e85c6fd KW |
3247 | } |
3248 | } | |
3249 | ||
3250 | /* For safe rebasing we need to compare old and new backing file */ | |
40ed35a3 | 3251 | if (!unsafe) { |
9a29e18f | 3252 | char backing_name[PATH_MAX]; |
644483d9 HR |
3253 | QDict *options = NULL; |
3254 | ||
3255 | if (bs->backing_format[0] != '\0') { | |
3256 | options = qdict_new(); | |
46f5ac20 | 3257 | qdict_put_str(options, "driver", bs->backing_format); |
644483d9 | 3258 | } |
3e85c6fd | 3259 | |
335e9937 FZ |
3260 | if (force_share) { |
3261 | if (!options) { | |
3262 | options = qdict_new(); | |
3263 | } | |
579cf1d1 | 3264 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); |
335e9937 | 3265 | } |
3e85c6fd | 3266 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
efaa7c4e | 3267 | blk_old_backing = blk_new_open(backing_name, NULL, |
644483d9 HR |
3268 | options, src_flags, &local_err); |
3269 | if (!blk_old_backing) { | |
c29b77f9 MA |
3270 | error_reportf_err(local_err, |
3271 | "Could not open old backing file '%s': ", | |
3272 | backing_name); | |
e84a0dd5 | 3273 | ret = -1; |
c2abccec | 3274 | goto out; |
3e85c6fd | 3275 | } |
644483d9 | 3276 | |
a616673d | 3277 | if (out_baseimg[0]) { |
d16699b6 HR |
3278 | const char *overlay_filename; |
3279 | char *out_real_path; | |
3280 | ||
335e9937 | 3281 | options = qdict_new(); |
644483d9 | 3282 | if (out_basefmt) { |
46f5ac20 | 3283 | qdict_put_str(options, "driver", out_basefmt); |
335e9937 FZ |
3284 | } |
3285 | if (force_share) { | |
3286 | qdict_put_bool(options, BDRV_OPT_FORCE_SHARE, true); | |
644483d9 HR |
3287 | } |
3288 | ||
d16699b6 HR |
3289 | overlay_filename = bs->exact_filename[0] ? bs->exact_filename |
3290 | : bs->filename; | |
3291 | out_real_path = g_malloc(PATH_MAX); | |
3292 | ||
3293 | bdrv_get_full_backing_filename_from_filename(overlay_filename, | |
3294 | out_baseimg, | |
3295 | out_real_path, | |
3296 | PATH_MAX, | |
3297 | &local_err); | |
3298 | if (local_err) { | |
3299 | error_reportf_err(local_err, | |
3300 | "Could not resolve backing filename: "); | |
3301 | ret = -1; | |
3302 | g_free(out_real_path); | |
3303 | goto out; | |
3304 | } | |
3305 | ||
3306 | blk_new_backing = blk_new_open(out_real_path, NULL, | |
644483d9 | 3307 | options, src_flags, &local_err); |
d16699b6 | 3308 | g_free(out_real_path); |
644483d9 | 3309 | if (!blk_new_backing) { |
c29b77f9 MA |
3310 | error_reportf_err(local_err, |
3311 | "Could not open new backing file '%s': ", | |
3312 | out_baseimg); | |
e84a0dd5 | 3313 | ret = -1; |
a616673d AB |
3314 | goto out; |
3315 | } | |
3e85c6fd KW |
3316 | } |
3317 | } | |
3318 | ||
3319 | /* | |
3320 | * Check each unallocated cluster in the COW file. If it is unallocated, | |
3321 | * accesses go to the backing file. We must therefore compare this cluster | |
3322 | * in the old and new backing file, and if they differ we need to copy it | |
3323 | * from the old backing file into the COW file. | |
3324 | * | |
3325 | * If qemu-img crashes during this step, no harm is done. The content of | |
3326 | * the image is the same as the original one at any time. | |
3327 | */ | |
3328 | if (!unsafe) { | |
41536287 EB |
3329 | int64_t size; |
3330 | int64_t old_backing_size; | |
3331 | int64_t new_backing_size = 0; | |
3332 | uint64_t offset; | |
3333 | int64_t n; | |
1f710495 | 3334 | float local_progress = 0; |
d6771bfa | 3335 | |
f1d3cd79 HR |
3336 | buf_old = blk_blockalign(blk, IO_BUF_SIZE); |
3337 | buf_new = blk_blockalign(blk, IO_BUF_SIZE); | |
3e85c6fd | 3338 | |
41536287 EB |
3339 | size = blk_getlength(blk); |
3340 | if (size < 0) { | |
52bf1e72 | 3341 | error_report("Could not get size of '%s': %s", |
41536287 | 3342 | filename, strerror(-size)); |
52bf1e72 MA |
3343 | ret = -1; |
3344 | goto out; | |
3345 | } | |
41536287 EB |
3346 | old_backing_size = blk_getlength(blk_old_backing); |
3347 | if (old_backing_size < 0) { | |
9a29e18f | 3348 | char backing_name[PATH_MAX]; |
52bf1e72 MA |
3349 | |
3350 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); | |
3351 | error_report("Could not get size of '%s': %s", | |
41536287 | 3352 | backing_name, strerror(-old_backing_size)); |
52bf1e72 MA |
3353 | ret = -1; |
3354 | goto out; | |
3355 | } | |
f1d3cd79 | 3356 | if (blk_new_backing) { |
41536287 EB |
3357 | new_backing_size = blk_getlength(blk_new_backing); |
3358 | if (new_backing_size < 0) { | |
52bf1e72 | 3359 | error_report("Could not get size of '%s': %s", |
41536287 | 3360 | out_baseimg, strerror(-new_backing_size)); |
52bf1e72 MA |
3361 | ret = -1; |
3362 | goto out; | |
3363 | } | |
a616673d | 3364 | } |
3e85c6fd | 3365 | |
41536287 EB |
3366 | if (size != 0) { |
3367 | local_progress = (float)100 / (size / MIN(size, IO_BUF_SIZE)); | |
1f710495 KW |
3368 | } |
3369 | ||
41536287 EB |
3370 | for (offset = 0; offset < size; offset += n) { |
3371 | /* How many bytes can we handle with the next read? */ | |
3372 | n = MIN(IO_BUF_SIZE, size - offset); | |
3e85c6fd KW |
3373 | |
3374 | /* If the cluster is allocated, we don't need to take action */ | |
41536287 | 3375 | ret = bdrv_is_allocated(bs, offset, n, &n); |
d663640c PB |
3376 | if (ret < 0) { |
3377 | error_report("error while reading image metadata: %s", | |
3378 | strerror(-ret)); | |
3379 | goto out; | |
3380 | } | |
cc60e327 | 3381 | if (ret) { |
3e85c6fd KW |
3382 | continue; |
3383 | } | |
3384 | ||
87a1b3e3 KW |
3385 | /* |
3386 | * Read old and new backing file and take into consideration that | |
3387 | * backing files may be smaller than the COW image. | |
3388 | */ | |
41536287 EB |
3389 | if (offset >= old_backing_size) { |
3390 | memset(buf_old, 0, n); | |
87a1b3e3 | 3391 | } else { |
41536287 EB |
3392 | if (offset + n > old_backing_size) { |
3393 | n = old_backing_size - offset; | |
87a1b3e3 KW |
3394 | } |
3395 | ||
41536287 | 3396 | ret = blk_pread(blk_old_backing, offset, buf_old, n); |
87a1b3e3 KW |
3397 | if (ret < 0) { |
3398 | error_report("error while reading from old backing file"); | |
3399 | goto out; | |
3400 | } | |
3e85c6fd | 3401 | } |
87a1b3e3 | 3402 | |
41536287 EB |
3403 | if (offset >= new_backing_size || !blk_new_backing) { |
3404 | memset(buf_new, 0, n); | |
87a1b3e3 | 3405 | } else { |
41536287 EB |
3406 | if (offset + n > new_backing_size) { |
3407 | n = new_backing_size - offset; | |
87a1b3e3 KW |
3408 | } |
3409 | ||
41536287 | 3410 | ret = blk_pread(blk_new_backing, offset, buf_new, n); |
87a1b3e3 KW |
3411 | if (ret < 0) { |
3412 | error_report("error while reading from new backing file"); | |
3413 | goto out; | |
3414 | } | |
3e85c6fd KW |
3415 | } |
3416 | ||
3417 | /* If they differ, we need to write to the COW file */ | |
3418 | uint64_t written = 0; | |
3419 | ||
41536287 | 3420 | while (written < n) { |
dc61cd3b | 3421 | int64_t pnum; |
3e85c6fd | 3422 | |
41536287 EB |
3423 | if (compare_buffers(buf_old + written, buf_new + written, |
3424 | n - written, &pnum)) | |
3e85c6fd | 3425 | { |
41536287 | 3426 | ret = blk_pwrite(blk, offset + written, |
dc61cd3b | 3427 | buf_old + written, pnum, 0); |
3e85c6fd | 3428 | if (ret < 0) { |
15654a6d | 3429 | error_report("Error while writing to COW image: %s", |
3e85c6fd | 3430 | strerror(-ret)); |
c2abccec | 3431 | goto out; |
3e85c6fd KW |
3432 | } |
3433 | } | |
3434 | ||
3435 | written += pnum; | |
3436 | } | |
6b837bc4 | 3437 | qemu_progress_print(local_progress, 100); |
3e85c6fd KW |
3438 | } |
3439 | } | |
3440 | ||
3441 | /* | |
3442 | * Change the backing file. All clusters that are different from the old | |
3443 | * backing file are overwritten in the COW file now, so the visible content | |
3444 | * doesn't change when we switch the backing file. | |
3445 | */ | |
a616673d AB |
3446 | if (out_baseimg && *out_baseimg) { |
3447 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); | |
3448 | } else { | |
3449 | ret = bdrv_change_backing_file(bs, NULL, NULL); | |
3450 | } | |
3451 | ||
3e85c6fd | 3452 | if (ret == -ENOSPC) { |
15654a6d JS |
3453 | error_report("Could not change the backing file to '%s': No " |
3454 | "space left in the file header", out_baseimg); | |
3e85c6fd | 3455 | } else if (ret < 0) { |
15654a6d | 3456 | error_report("Could not change the backing file to '%s': %s", |
3e85c6fd KW |
3457 | out_baseimg, strerror(-ret)); |
3458 | } | |
3459 | ||
6b837bc4 | 3460 | qemu_progress_print(100, 0); |
3e85c6fd KW |
3461 | /* |
3462 | * TODO At this point it is possible to check if any clusters that are | |
3463 | * allocated in the COW file are the same in the backing file. If so, they | |
3464 | * could be dropped from the COW file. Don't do this before switching the | |
3465 | * backing file, in case of a crash this would lead to corruption. | |
3466 | */ | |
c2abccec | 3467 | out: |
6b837bc4 | 3468 | qemu_progress_end(); |
3e85c6fd KW |
3469 | /* Cleanup */ |
3470 | if (!unsafe) { | |
26f54e9a | 3471 | blk_unref(blk_old_backing); |
26f54e9a | 3472 | blk_unref(blk_new_backing); |
3e85c6fd | 3473 | } |
396374ca PB |
3474 | qemu_vfree(buf_old); |
3475 | qemu_vfree(buf_new); | |
3e85c6fd | 3476 | |
26f54e9a | 3477 | blk_unref(blk); |
c2abccec MK |
3478 | if (ret) { |
3479 | return 1; | |
3480 | } | |
3e85c6fd KW |
3481 | return 0; |
3482 | } | |
3483 | ||
ae6b0ed6 SH |
3484 | static int img_resize(int argc, char **argv) |
3485 | { | |
6750e795 | 3486 | Error *err = NULL; |
ae6b0ed6 SH |
3487 | int c, ret, relative; |
3488 | const char *filename, *fmt, *size; | |
5279b303 | 3489 | int64_t n, total_size, current_size, new_size; |
f382d43a | 3490 | bool quiet = false; |
26f54e9a | 3491 | BlockBackend *blk = NULL; |
dc5f690b | 3492 | PreallocMode prealloc = PREALLOC_MODE_OFF; |
20caf0f7 | 3493 | QemuOpts *param; |
3babeb15 | 3494 | |
20caf0f7 DXW |
3495 | static QemuOptsList resize_options = { |
3496 | .name = "resize_options", | |
3497 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), | |
3498 | .desc = { | |
3499 | { | |
3500 | .name = BLOCK_OPT_SIZE, | |
3501 | .type = QEMU_OPT_SIZE, | |
3502 | .help = "Virtual disk size" | |
3503 | }, { | |
3504 | /* end of list */ | |
3505 | } | |
ae6b0ed6 | 3506 | }, |
ae6b0ed6 | 3507 | }; |
eb769f74 | 3508 | bool image_opts = false; |
4ffca890 | 3509 | bool shrink = false; |
ae6b0ed6 | 3510 | |
e80fec7f KW |
3511 | /* Remove size from argv manually so that negative numbers are not treated |
3512 | * as options by getopt. */ | |
3513 | if (argc < 3) { | |
ac1307ab | 3514 | error_exit("Not enough arguments"); |
e80fec7f KW |
3515 | return 1; |
3516 | } | |
3517 | ||
3518 | size = argv[--argc]; | |
3519 | ||
3520 | /* Parse getopt arguments */ | |
ae6b0ed6 SH |
3521 | fmt = NULL; |
3522 | for(;;) { | |
3babeb15 DB |
3523 | static const struct option long_options[] = { |
3524 | {"help", no_argument, 0, 'h'}, | |
3525 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3526 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
dc5f690b | 3527 | {"preallocation", required_argument, 0, OPTION_PREALLOCATION}, |
4ffca890 | 3528 | {"shrink", no_argument, 0, OPTION_SHRINK}, |
3babeb15 DB |
3529 | {0, 0, 0, 0} |
3530 | }; | |
c9192973 | 3531 | c = getopt_long(argc, argv, ":f:hq", |
3babeb15 | 3532 | long_options, NULL); |
ae6b0ed6 SH |
3533 | if (c == -1) { |
3534 | break; | |
3535 | } | |
3536 | switch(c) { | |
c9192973 SH |
3537 | case ':': |
3538 | missing_argument(argv[optind - 1]); | |
3539 | break; | |
ef87394c | 3540 | case '?': |
c9192973 SH |
3541 | unrecognized_option(argv[optind - 1]); |
3542 | break; | |
ae6b0ed6 SH |
3543 | case 'h': |
3544 | help(); | |
3545 | break; | |
3546 | case 'f': | |
3547 | fmt = optarg; | |
3548 | break; | |
f382d43a MR |
3549 | case 'q': |
3550 | quiet = true; | |
3551 | break; | |
3babeb15 DB |
3552 | case OPTION_OBJECT: { |
3553 | QemuOpts *opts; | |
3554 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3555 | optarg, true); | |
3556 | if (!opts) { | |
3557 | return 1; | |
3558 | } | |
3559 | } break; | |
eb769f74 DB |
3560 | case OPTION_IMAGE_OPTS: |
3561 | image_opts = true; | |
3562 | break; | |
dc5f690b | 3563 | case OPTION_PREALLOCATION: |
f7abe0ec | 3564 | prealloc = qapi_enum_parse(&PreallocMode_lookup, optarg, |
06c60b6c | 3565 | PREALLOC_MODE__MAX, NULL); |
dc5f690b HR |
3566 | if (prealloc == PREALLOC_MODE__MAX) { |
3567 | error_report("Invalid preallocation mode '%s'", optarg); | |
3568 | return 1; | |
3569 | } | |
3570 | break; | |
4ffca890 PB |
3571 | case OPTION_SHRINK: |
3572 | shrink = true; | |
3573 | break; | |
ae6b0ed6 SH |
3574 | } |
3575 | } | |
fc11eb26 | 3576 | if (optind != argc - 1) { |
be8fbd47 | 3577 | error_exit("Expecting image file name and size"); |
ae6b0ed6 SH |
3578 | } |
3579 | filename = argv[optind++]; | |
ae6b0ed6 | 3580 | |
3babeb15 DB |
3581 | if (qemu_opts_foreach(&qemu_object_opts, |
3582 | user_creatable_add_opts_foreach, | |
51b9b478 | 3583 | NULL, NULL)) { |
3babeb15 DB |
3584 | return 1; |
3585 | } | |
3586 | ||
ae6b0ed6 SH |
3587 | /* Choose grow, shrink, or absolute resize mode */ |
3588 | switch (size[0]) { | |
3589 | case '+': | |
3590 | relative = 1; | |
3591 | size++; | |
3592 | break; | |
3593 | case '-': | |
3594 | relative = -1; | |
3595 | size++; | |
3596 | break; | |
3597 | default: | |
3598 | relative = 0; | |
3599 | break; | |
3600 | } | |
3601 | ||
3602 | /* Parse size */ | |
87ea75d5 | 3603 | param = qemu_opts_create(&resize_options, NULL, 0, &error_abort); |
f43e47db | 3604 | qemu_opt_set(param, BLOCK_OPT_SIZE, size, &err); |
6750e795 MA |
3605 | if (err) { |
3606 | error_report_err(err); | |
2a81998a | 3607 | ret = -1; |
20caf0f7 | 3608 | qemu_opts_del(param); |
2a81998a | 3609 | goto out; |
ae6b0ed6 | 3610 | } |
20caf0f7 DXW |
3611 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
3612 | qemu_opts_del(param); | |
ae6b0ed6 | 3613 | |
efaa7c4e | 3614 | blk = img_open(image_opts, filename, fmt, |
335e9937 FZ |
3615 | BDRV_O_RDWR | BDRV_O_RESIZE, false, quiet, |
3616 | false); | |
7e7d56d9 | 3617 | if (!blk) { |
2a81998a JS |
3618 | ret = -1; |
3619 | goto out; | |
c2abccec | 3620 | } |
ae6b0ed6 | 3621 | |
dc5f690b HR |
3622 | current_size = blk_getlength(blk); |
3623 | if (current_size < 0) { | |
3624 | error_report("Failed to inquire current image length: %s", | |
3625 | strerror(-current_size)); | |
3626 | ret = -1; | |
3627 | goto out; | |
3628 | } | |
3629 | ||
ae6b0ed6 | 3630 | if (relative) { |
dc5f690b | 3631 | total_size = current_size + n * relative; |
ae6b0ed6 SH |
3632 | } else { |
3633 | total_size = n; | |
3634 | } | |
3635 | if (total_size <= 0) { | |
15654a6d | 3636 | error_report("New image size must be positive"); |
c2abccec MK |
3637 | ret = -1; |
3638 | goto out; | |
ae6b0ed6 SH |
3639 | } |
3640 | ||
dc5f690b HR |
3641 | if (total_size <= current_size && prealloc != PREALLOC_MODE_OFF) { |
3642 | error_report("Preallocation can only be used for growing images"); | |
3643 | ret = -1; | |
3644 | goto out; | |
3645 | } | |
3646 | ||
4ffca890 PB |
3647 | if (total_size < current_size && !shrink) { |
3648 | warn_report("Shrinking an image will delete all data beyond the " | |
3649 | "shrunken image's end. Before performing such an " | |
3650 | "operation, make sure there is no important data there."); | |
3651 | ||
3652 | if (g_strcmp0(bdrv_get_format_name(blk_bs(blk)), "raw") != 0) { | |
3653 | error_report( | |
3654 | "Use the --shrink option to perform a shrink operation."); | |
3655 | ret = -1; | |
3656 | goto out; | |
3657 | } else { | |
3658 | warn_report("Using the --shrink option will suppress this message. " | |
3659 | "Note that future versions of qemu-img may refuse to " | |
3660 | "shrink images without this option."); | |
3661 | } | |
3662 | } | |
3663 | ||
dc5f690b | 3664 | ret = blk_truncate(blk, total_size, prealloc, &err); |
5279b303 | 3665 | if (ret < 0) { |
ed3d2ec9 | 3666 | error_report_err(err); |
5279b303 HR |
3667 | goto out; |
3668 | } | |
3669 | ||
3670 | new_size = blk_getlength(blk); | |
3671 | if (new_size < 0) { | |
3672 | error_report("Failed to verify truncated image length: %s", | |
3673 | strerror(-new_size)); | |
3674 | ret = -1; | |
3675 | goto out; | |
ae6b0ed6 | 3676 | } |
5279b303 HR |
3677 | |
3678 | /* Some block drivers implement a truncation method, but only so | |
3679 | * the user can cause qemu to refresh the image's size from disk. | |
3680 | * The idea is that the user resizes the image outside of qemu and | |
3681 | * then invokes block_resize to inform qemu about it. | |
3682 | * (This includes iscsi and file-posix for device files.) | |
3683 | * Of course, that is not the behavior someone invoking | |
3684 | * qemu-img resize would find useful, so we catch that behavior | |
3685 | * here and tell the user. */ | |
3686 | if (new_size != total_size && new_size == current_size) { | |
3687 | error_report("Image was not resized; resizing may not be supported " | |
3688 | "for this image"); | |
3689 | ret = -1; | |
3690 | goto out; | |
3691 | } | |
3692 | ||
3693 | if (new_size != total_size) { | |
3694 | warn_report("Image should have been resized to %" PRIi64 | |
3695 | " bytes, but was resized to %" PRIi64 " bytes", | |
3696 | total_size, new_size); | |
3697 | } | |
3698 | ||
3699 | qprintf(quiet, "Image resized.\n"); | |
3700 | ||
c2abccec | 3701 | out: |
26f54e9a | 3702 | blk_unref(blk); |
c2abccec MK |
3703 | if (ret) { |
3704 | return 1; | |
3705 | } | |
ae6b0ed6 SH |
3706 | return 0; |
3707 | } | |
3708 | ||
76a3a34d | 3709 | static void amend_status_cb(BlockDriverState *bs, |
8b13976d HR |
3710 | int64_t offset, int64_t total_work_size, |
3711 | void *opaque) | |
76a3a34d HR |
3712 | { |
3713 | qemu_progress_print(100.f * offset / total_work_size, 0); | |
3714 | } | |
3715 | ||
51641351 HR |
3716 | static int print_amend_option_help(const char *format) |
3717 | { | |
3718 | BlockDriver *drv; | |
3719 | ||
3720 | /* Find driver and parse its options */ | |
3721 | drv = bdrv_find_format(format); | |
3722 | if (!drv) { | |
3723 | error_report("Unknown file format '%s'", format); | |
3724 | return 1; | |
3725 | } | |
3726 | ||
3727 | if (!drv->bdrv_amend_options) { | |
3728 | error_report("Format driver '%s' does not support option amendment", | |
3729 | format); | |
3730 | return 1; | |
3731 | } | |
3732 | ||
3733 | /* Every driver supporting amendment must have create_opts */ | |
3734 | assert(drv->create_opts); | |
3735 | ||
3736 | printf("Creation options for '%s':\n", format); | |
3737 | qemu_opts_print_help(drv->create_opts); | |
3738 | printf("\nNote that not all of these options may be amendable.\n"); | |
3739 | return 0; | |
3740 | } | |
3741 | ||
6f176b48 HR |
3742 | static int img_amend(int argc, char **argv) |
3743 | { | |
dc523cd3 | 3744 | Error *err = NULL; |
6f176b48 HR |
3745 | int c, ret = 0; |
3746 | char *options = NULL; | |
83d0521a CL |
3747 | QemuOptsList *create_opts = NULL; |
3748 | QemuOpts *opts = NULL; | |
bd39e6ed HR |
3749 | const char *fmt = NULL, *filename, *cache; |
3750 | int flags; | |
ce099547 | 3751 | bool writethrough; |
76a3a34d | 3752 | bool quiet = false, progress = false; |
26f54e9a | 3753 | BlockBackend *blk = NULL; |
6f176b48 | 3754 | BlockDriverState *bs = NULL; |
eb769f74 | 3755 | bool image_opts = false; |
6f176b48 | 3756 | |
bd39e6ed | 3757 | cache = BDRV_DEFAULT_CACHE; |
6f176b48 | 3758 | for (;;) { |
3babeb15 DB |
3759 | static const struct option long_options[] = { |
3760 | {"help", no_argument, 0, 'h'}, | |
3761 | {"object", required_argument, 0, OPTION_OBJECT}, | |
eb769f74 | 3762 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
3babeb15 DB |
3763 | {0, 0, 0, 0} |
3764 | }; | |
c9192973 | 3765 | c = getopt_long(argc, argv, ":ho:f:t:pq", |
3babeb15 | 3766 | long_options, NULL); |
6f176b48 HR |
3767 | if (c == -1) { |
3768 | break; | |
3769 | } | |
3770 | ||
3771 | switch (c) { | |
c9192973 SH |
3772 | case ':': |
3773 | missing_argument(argv[optind - 1]); | |
3774 | break; | |
f7077624 | 3775 | case '?': |
c9192973 SH |
3776 | unrecognized_option(argv[optind - 1]); |
3777 | break; | |
3778 | case 'h': | |
f7077624 SH |
3779 | help(); |
3780 | break; | |
3781 | case 'o': | |
3782 | if (!is_valid_option_list(optarg)) { | |
3783 | error_report("Invalid option list: %s", optarg); | |
3784 | ret = -1; | |
3785 | goto out_no_progress; | |
3786 | } | |
3787 | if (!options) { | |
3788 | options = g_strdup(optarg); | |
3789 | } else { | |
3790 | char *old_options = options; | |
3791 | options = g_strdup_printf("%s,%s", options, optarg); | |
3792 | g_free(old_options); | |
3793 | } | |
3794 | break; | |
3795 | case 'f': | |
3796 | fmt = optarg; | |
3797 | break; | |
3798 | case 't': | |
3799 | cache = optarg; | |
3800 | break; | |
3801 | case 'p': | |
3802 | progress = true; | |
3803 | break; | |
3804 | case 'q': | |
3805 | quiet = true; | |
3806 | break; | |
3807 | case OPTION_OBJECT: | |
3808 | opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
3809 | optarg, true); | |
3810 | if (!opts) { | |
3811 | ret = -1; | |
3812 | goto out_no_progress; | |
3813 | } | |
3814 | break; | |
3815 | case OPTION_IMAGE_OPTS: | |
3816 | image_opts = true; | |
3817 | break; | |
6f176b48 HR |
3818 | } |
3819 | } | |
3820 | ||
a283cb6e | 3821 | if (!options) { |
ac1307ab | 3822 | error_exit("Must specify options (-o)"); |
6f176b48 HR |
3823 | } |
3824 | ||
3babeb15 DB |
3825 | if (qemu_opts_foreach(&qemu_object_opts, |
3826 | user_creatable_add_opts_foreach, | |
51b9b478 | 3827 | NULL, NULL)) { |
3babeb15 DB |
3828 | ret = -1; |
3829 | goto out_no_progress; | |
3830 | } | |
3831 | ||
76a3a34d HR |
3832 | if (quiet) { |
3833 | progress = false; | |
3834 | } | |
3835 | qemu_progress_init(progress, 1.0); | |
3836 | ||
a283cb6e KW |
3837 | filename = (optind == argc - 1) ? argv[argc - 1] : NULL; |
3838 | if (fmt && has_help_option(options)) { | |
3839 | /* If a format is explicitly specified (and possibly no filename is | |
3840 | * given), print option help here */ | |
51641351 | 3841 | ret = print_amend_option_help(fmt); |
a283cb6e | 3842 | goto out; |
6f176b48 HR |
3843 | } |
3844 | ||
a283cb6e | 3845 | if (optind != argc - 1) { |
b2f27e44 HR |
3846 | error_report("Expecting one image file name"); |
3847 | ret = -1; | |
3848 | goto out; | |
a283cb6e | 3849 | } |
6f176b48 | 3850 | |
ce099547 KW |
3851 | flags = BDRV_O_RDWR; |
3852 | ret = bdrv_parse_cache_mode(cache, &flags, &writethrough); | |
bd39e6ed HR |
3853 | if (ret < 0) { |
3854 | error_report("Invalid cache option: %s", cache); | |
3855 | goto out; | |
3856 | } | |
3857 | ||
335e9937 FZ |
3858 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
3859 | false); | |
7e7d56d9 | 3860 | if (!blk) { |
6f176b48 HR |
3861 | ret = -1; |
3862 | goto out; | |
3863 | } | |
7e7d56d9 | 3864 | bs = blk_bs(blk); |
6f176b48 HR |
3865 | |
3866 | fmt = bs->drv->format_name; | |
3867 | ||
626f84f3 | 3868 | if (has_help_option(options)) { |
a283cb6e | 3869 | /* If the format was auto-detected, print option help here */ |
51641351 | 3870 | ret = print_amend_option_help(fmt); |
6f176b48 HR |
3871 | goto out; |
3872 | } | |
3873 | ||
1f996683 HR |
3874 | if (!bs->drv->bdrv_amend_options) { |
3875 | error_report("Format driver '%s' does not support option amendment", | |
b2439d26 HR |
3876 | fmt); |
3877 | ret = -1; | |
3878 | goto out; | |
3879 | } | |
3880 | ||
1f996683 HR |
3881 | /* Every driver supporting amendment must have create_opts */ |
3882 | assert(bs->drv->create_opts); | |
3883 | ||
c282e1fd | 3884 | create_opts = qemu_opts_append(create_opts, bs->drv->create_opts); |
83d0521a | 3885 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); |
ece9086e PB |
3886 | qemu_opts_do_parse(opts, options, NULL, &err); |
3887 | if (err) { | |
3888 | error_report_err(err); | |
3889 | ret = -1; | |
3890 | goto out; | |
6f176b48 HR |
3891 | } |
3892 | ||
76a3a34d HR |
3893 | /* In case the driver does not call amend_status_cb() */ |
3894 | qemu_progress_print(0.f, 0); | |
d1402b50 | 3895 | ret = bdrv_amend_options(bs, opts, &amend_status_cb, NULL, &err); |
76a3a34d | 3896 | qemu_progress_print(100.f, 0); |
6f176b48 | 3897 | if (ret < 0) { |
d1402b50 | 3898 | error_report_err(err); |
6f176b48 HR |
3899 | goto out; |
3900 | } | |
3901 | ||
3902 | out: | |
76a3a34d HR |
3903 | qemu_progress_end(); |
3904 | ||
e814dffc | 3905 | out_no_progress: |
26f54e9a | 3906 | blk_unref(blk); |
83d0521a CL |
3907 | qemu_opts_del(opts); |
3908 | qemu_opts_free(create_opts); | |
626f84f3 KW |
3909 | g_free(options); |
3910 | ||
6f176b48 HR |
3911 | if (ret) { |
3912 | return 1; | |
3913 | } | |
3914 | return 0; | |
3915 | } | |
3916 | ||
b6133b8c KW |
3917 | typedef struct BenchData { |
3918 | BlockBackend *blk; | |
3919 | uint64_t image_size; | |
b6495fa8 | 3920 | bool write; |
b6133b8c | 3921 | int bufsize; |
83de9be0 | 3922 | int step; |
b6133b8c KW |
3923 | int nrreq; |
3924 | int n; | |
55d539c8 KW |
3925 | int flush_interval; |
3926 | bool drain_on_flush; | |
b6133b8c KW |
3927 | uint8_t *buf; |
3928 | QEMUIOVector *qiov; | |
3929 | ||
3930 | int in_flight; | |
55d539c8 | 3931 | bool in_flush; |
b6133b8c KW |
3932 | uint64_t offset; |
3933 | } BenchData; | |
3934 | ||
55d539c8 KW |
3935 | static void bench_undrained_flush_cb(void *opaque, int ret) |
3936 | { | |
3937 | if (ret < 0) { | |
df3c286c | 3938 | error_report("Failed flush request: %s", strerror(-ret)); |
55d539c8 KW |
3939 | exit(EXIT_FAILURE); |
3940 | } | |
3941 | } | |
3942 | ||
b6133b8c KW |
3943 | static void bench_cb(void *opaque, int ret) |
3944 | { | |
3945 | BenchData *b = opaque; | |
3946 | BlockAIOCB *acb; | |
3947 | ||
3948 | if (ret < 0) { | |
df3c286c | 3949 | error_report("Failed request: %s", strerror(-ret)); |
b6133b8c KW |
3950 | exit(EXIT_FAILURE); |
3951 | } | |
55d539c8 KW |
3952 | |
3953 | if (b->in_flush) { | |
3954 | /* Just finished a flush with drained queue: Start next requests */ | |
3955 | assert(b->in_flight == 0); | |
3956 | b->in_flush = false; | |
3957 | } else if (b->in_flight > 0) { | |
3958 | int remaining = b->n - b->in_flight; | |
3959 | ||
b6133b8c KW |
3960 | b->n--; |
3961 | b->in_flight--; | |
55d539c8 KW |
3962 | |
3963 | /* Time for flush? Drain queue if requested, then flush */ | |
3964 | if (b->flush_interval && remaining % b->flush_interval == 0) { | |
3965 | if (!b->in_flight || !b->drain_on_flush) { | |
3966 | BlockCompletionFunc *cb; | |
3967 | ||
3968 | if (b->drain_on_flush) { | |
3969 | b->in_flush = true; | |
3970 | cb = bench_cb; | |
3971 | } else { | |
3972 | cb = bench_undrained_flush_cb; | |
3973 | } | |
3974 | ||
3975 | acb = blk_aio_flush(b->blk, cb, b); | |
3976 | if (!acb) { | |
3977 | error_report("Failed to issue flush request"); | |
3978 | exit(EXIT_FAILURE); | |
3979 | } | |
3980 | } | |
3981 | if (b->drain_on_flush) { | |
3982 | return; | |
3983 | } | |
3984 | } | |
b6133b8c KW |
3985 | } |
3986 | ||
3987 | while (b->n > b->in_flight && b->in_flight < b->nrreq) { | |
4baaa8c3 PB |
3988 | int64_t offset = b->offset; |
3989 | /* blk_aio_* might look for completed I/Os and kick bench_cb | |
3990 | * again, so make sure this operation is counted by in_flight | |
3991 | * and b->offset is ready for the next submission. | |
3992 | */ | |
3993 | b->in_flight++; | |
3994 | b->offset += b->step; | |
3995 | b->offset %= b->image_size; | |
b6495fa8 | 3996 | if (b->write) { |
4baaa8c3 | 3997 | acb = blk_aio_pwritev(b->blk, offset, b->qiov, 0, bench_cb, b); |
b6495fa8 | 3998 | } else { |
4baaa8c3 | 3999 | acb = blk_aio_preadv(b->blk, offset, b->qiov, 0, bench_cb, b); |
b6495fa8 | 4000 | } |
b6133b8c KW |
4001 | if (!acb) { |
4002 | error_report("Failed to issue request"); | |
4003 | exit(EXIT_FAILURE); | |
4004 | } | |
b6133b8c KW |
4005 | } |
4006 | } | |
4007 | ||
4008 | static int img_bench(int argc, char **argv) | |
4009 | { | |
4010 | int c, ret = 0; | |
4011 | const char *fmt = NULL, *filename; | |
4012 | bool quiet = false; | |
4013 | bool image_opts = false; | |
b6495fa8 | 4014 | bool is_write = false; |
b6133b8c KW |
4015 | int count = 75000; |
4016 | int depth = 64; | |
d3199a31 | 4017 | int64_t offset = 0; |
b6133b8c | 4018 | size_t bufsize = 4096; |
b6495fa8 | 4019 | int pattern = 0; |
83de9be0 | 4020 | size_t step = 0; |
55d539c8 KW |
4021 | int flush_interval = 0; |
4022 | bool drain_on_flush = true; | |
b6133b8c KW |
4023 | int64_t image_size; |
4024 | BlockBackend *blk = NULL; | |
4025 | BenchData data = {}; | |
4026 | int flags = 0; | |
604e8613 | 4027 | bool writethrough = false; |
b6133b8c KW |
4028 | struct timeval t1, t2; |
4029 | int i; | |
335e9937 | 4030 | bool force_share = false; |
79d46583 | 4031 | size_t buf_size; |
b6133b8c KW |
4032 | |
4033 | for (;;) { | |
4034 | static const struct option long_options[] = { | |
4035 | {"help", no_argument, 0, 'h'}, | |
55d539c8 | 4036 | {"flush-interval", required_argument, 0, OPTION_FLUSH_INTERVAL}, |
b6133b8c | 4037 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
b6495fa8 | 4038 | {"pattern", required_argument, 0, OPTION_PATTERN}, |
55d539c8 | 4039 | {"no-drain", no_argument, 0, OPTION_NO_DRAIN}, |
335e9937 | 4040 | {"force-share", no_argument, 0, 'U'}, |
b6133b8c KW |
4041 | {0, 0, 0, 0} |
4042 | }; | |
335e9937 | 4043 | c = getopt_long(argc, argv, ":hc:d:f:no:qs:S:t:wU", long_options, NULL); |
b6133b8c KW |
4044 | if (c == -1) { |
4045 | break; | |
4046 | } | |
4047 | ||
4048 | switch (c) { | |
c9192973 SH |
4049 | case ':': |
4050 | missing_argument(argv[optind - 1]); | |
4051 | break; | |
b6133b8c | 4052 | case '?': |
c9192973 SH |
4053 | unrecognized_option(argv[optind - 1]); |
4054 | break; | |
4055 | case 'h': | |
b6133b8c KW |
4056 | help(); |
4057 | break; | |
4058 | case 'c': | |
4059 | { | |
8b3c6792 PM |
4060 | unsigned long res; |
4061 | ||
4062 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
b6133b8c KW |
4063 | error_report("Invalid request count specified"); |
4064 | return 1; | |
4065 | } | |
8b3c6792 | 4066 | count = res; |
b6133b8c KW |
4067 | break; |
4068 | } | |
4069 | case 'd': | |
4070 | { | |
8b3c6792 PM |
4071 | unsigned long res; |
4072 | ||
4073 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
b6133b8c KW |
4074 | error_report("Invalid queue depth specified"); |
4075 | return 1; | |
4076 | } | |
8b3c6792 | 4077 | depth = res; |
b6133b8c KW |
4078 | break; |
4079 | } | |
4080 | case 'f': | |
4081 | fmt = optarg; | |
4082 | break; | |
4083 | case 'n': | |
4084 | flags |= BDRV_O_NATIVE_AIO; | |
4085 | break; | |
d3199a31 KW |
4086 | case 'o': |
4087 | { | |
606caa0a MA |
4088 | offset = cvtnum(optarg); |
4089 | if (offset < 0) { | |
d3199a31 KW |
4090 | error_report("Invalid offset specified"); |
4091 | return 1; | |
4092 | } | |
4093 | break; | |
4094 | } | |
4095 | break; | |
b6133b8c KW |
4096 | case 'q': |
4097 | quiet = true; | |
4098 | break; | |
4099 | case 's': | |
4100 | { | |
4101 | int64_t sval; | |
b6133b8c | 4102 | |
606caa0a MA |
4103 | sval = cvtnum(optarg); |
4104 | if (sval < 0 || sval > INT_MAX) { | |
b6133b8c KW |
4105 | error_report("Invalid buffer size specified"); |
4106 | return 1; | |
4107 | } | |
4108 | ||
4109 | bufsize = sval; | |
4110 | break; | |
4111 | } | |
83de9be0 KW |
4112 | case 'S': |
4113 | { | |
4114 | int64_t sval; | |
83de9be0 | 4115 | |
606caa0a MA |
4116 | sval = cvtnum(optarg); |
4117 | if (sval < 0 || sval > INT_MAX) { | |
83de9be0 KW |
4118 | error_report("Invalid step size specified"); |
4119 | return 1; | |
4120 | } | |
4121 | ||
4122 | step = sval; | |
4123 | break; | |
4124 | } | |
b6133b8c KW |
4125 | case 't': |
4126 | ret = bdrv_parse_cache_mode(optarg, &flags, &writethrough); | |
4127 | if (ret < 0) { | |
4128 | error_report("Invalid cache mode"); | |
4129 | ret = -1; | |
4130 | goto out; | |
4131 | } | |
4132 | break; | |
b6495fa8 KW |
4133 | case 'w': |
4134 | flags |= BDRV_O_RDWR; | |
4135 | is_write = true; | |
4136 | break; | |
335e9937 FZ |
4137 | case 'U': |
4138 | force_share = true; | |
4139 | break; | |
b6495fa8 KW |
4140 | case OPTION_PATTERN: |
4141 | { | |
8b3c6792 PM |
4142 | unsigned long res; |
4143 | ||
4144 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > 0xff) { | |
b6495fa8 KW |
4145 | error_report("Invalid pattern byte specified"); |
4146 | return 1; | |
4147 | } | |
8b3c6792 | 4148 | pattern = res; |
b6495fa8 KW |
4149 | break; |
4150 | } | |
55d539c8 KW |
4151 | case OPTION_FLUSH_INTERVAL: |
4152 | { | |
8b3c6792 PM |
4153 | unsigned long res; |
4154 | ||
4155 | if (qemu_strtoul(optarg, NULL, 0, &res) < 0 || res > INT_MAX) { | |
55d539c8 KW |
4156 | error_report("Invalid flush interval specified"); |
4157 | return 1; | |
4158 | } | |
8b3c6792 | 4159 | flush_interval = res; |
55d539c8 KW |
4160 | break; |
4161 | } | |
4162 | case OPTION_NO_DRAIN: | |
4163 | drain_on_flush = false; | |
4164 | break; | |
b6133b8c KW |
4165 | case OPTION_IMAGE_OPTS: |
4166 | image_opts = true; | |
4167 | break; | |
4168 | } | |
4169 | } | |
4170 | ||
4171 | if (optind != argc - 1) { | |
4172 | error_exit("Expecting one image file name"); | |
4173 | } | |
4174 | filename = argv[argc - 1]; | |
4175 | ||
55d539c8 KW |
4176 | if (!is_write && flush_interval) { |
4177 | error_report("--flush-interval is only available in write tests"); | |
4178 | ret = -1; | |
4179 | goto out; | |
4180 | } | |
4181 | if (flush_interval && flush_interval < depth) { | |
4182 | error_report("Flush interval can't be smaller than depth"); | |
4183 | ret = -1; | |
4184 | goto out; | |
4185 | } | |
4186 | ||
335e9937 FZ |
4187 | blk = img_open(image_opts, filename, fmt, flags, writethrough, quiet, |
4188 | force_share); | |
b6133b8c KW |
4189 | if (!blk) { |
4190 | ret = -1; | |
4191 | goto out; | |
4192 | } | |
4193 | ||
4194 | image_size = blk_getlength(blk); | |
4195 | if (image_size < 0) { | |
4196 | ret = image_size; | |
4197 | goto out; | |
4198 | } | |
4199 | ||
4200 | data = (BenchData) { | |
55d539c8 KW |
4201 | .blk = blk, |
4202 | .image_size = image_size, | |
4203 | .bufsize = bufsize, | |
4204 | .step = step ?: bufsize, | |
4205 | .nrreq = depth, | |
4206 | .n = count, | |
4207 | .offset = offset, | |
4208 | .write = is_write, | |
4209 | .flush_interval = flush_interval, | |
4210 | .drain_on_flush = drain_on_flush, | |
b6133b8c | 4211 | }; |
d3199a31 | 4212 | printf("Sending %d %s requests, %d bytes each, %d in parallel " |
83de9be0 | 4213 | "(starting at offset %" PRId64 ", step size %d)\n", |
d3199a31 | 4214 | data.n, data.write ? "write" : "read", data.bufsize, data.nrreq, |
83de9be0 | 4215 | data.offset, data.step); |
55d539c8 KW |
4216 | if (flush_interval) { |
4217 | printf("Sending flush every %d requests\n", flush_interval); | |
4218 | } | |
b6133b8c | 4219 | |
79d46583 FZ |
4220 | buf_size = data.nrreq * data.bufsize; |
4221 | data.buf = blk_blockalign(blk, buf_size); | |
b6495fa8 KW |
4222 | memset(data.buf, pattern, data.nrreq * data.bufsize); |
4223 | ||
79d46583 FZ |
4224 | blk_register_buf(blk, data.buf, buf_size); |
4225 | ||
b6133b8c KW |
4226 | data.qiov = g_new(QEMUIOVector, data.nrreq); |
4227 | for (i = 0; i < data.nrreq; i++) { | |
4228 | qemu_iovec_init(&data.qiov[i], 1); | |
4229 | qemu_iovec_add(&data.qiov[i], | |
4230 | data.buf + i * data.bufsize, data.bufsize); | |
4231 | } | |
4232 | ||
4233 | gettimeofday(&t1, NULL); | |
4234 | bench_cb(&data, 0); | |
4235 | ||
4236 | while (data.n > 0) { | |
4237 | main_loop_wait(false); | |
4238 | } | |
4239 | gettimeofday(&t2, NULL); | |
4240 | ||
4241 | printf("Run completed in %3.3f seconds.\n", | |
4242 | (t2.tv_sec - t1.tv_sec) | |
4243 | + ((double)(t2.tv_usec - t1.tv_usec) / 1000000)); | |
4244 | ||
4245 | out: | |
79d46583 FZ |
4246 | if (data.buf) { |
4247 | blk_unregister_buf(blk, data.buf); | |
4248 | } | |
b6133b8c KW |
4249 | qemu_vfree(data.buf); |
4250 | blk_unref(blk); | |
4251 | ||
4252 | if (ret) { | |
86ce1f6e RS |
4253 | return 1; |
4254 | } | |
4255 | return 0; | |
4256 | } | |
4257 | ||
4258 | #define C_BS 01 | |
4259 | #define C_COUNT 02 | |
4260 | #define C_IF 04 | |
4261 | #define C_OF 010 | |
f7c15533 | 4262 | #define C_SKIP 020 |
86ce1f6e RS |
4263 | |
4264 | struct DdInfo { | |
4265 | unsigned int flags; | |
4266 | int64_t count; | |
4267 | }; | |
4268 | ||
4269 | struct DdIo { | |
4270 | int bsz; /* Block size */ | |
4271 | char *filename; | |
4272 | uint8_t *buf; | |
f7c15533 | 4273 | int64_t offset; |
86ce1f6e RS |
4274 | }; |
4275 | ||
4276 | struct DdOpts { | |
4277 | const char *name; | |
4278 | int (*f)(const char *, struct DdIo *, struct DdIo *, struct DdInfo *); | |
4279 | unsigned int flag; | |
4280 | }; | |
4281 | ||
4282 | static int img_dd_bs(const char *arg, | |
4283 | struct DdIo *in, struct DdIo *out, | |
4284 | struct DdInfo *dd) | |
4285 | { | |
86ce1f6e RS |
4286 | int64_t res; |
4287 | ||
606caa0a | 4288 | res = cvtnum(arg); |
86ce1f6e | 4289 | |
606caa0a | 4290 | if (res <= 0 || res > INT_MAX) { |
86ce1f6e RS |
4291 | error_report("invalid number: '%s'", arg); |
4292 | return 1; | |
4293 | } | |
4294 | in->bsz = out->bsz = res; | |
4295 | ||
4296 | return 0; | |
4297 | } | |
4298 | ||
4299 | static int img_dd_count(const char *arg, | |
4300 | struct DdIo *in, struct DdIo *out, | |
4301 | struct DdInfo *dd) | |
4302 | { | |
606caa0a | 4303 | dd->count = cvtnum(arg); |
86ce1f6e | 4304 | |
606caa0a | 4305 | if (dd->count < 0) { |
86ce1f6e RS |
4306 | error_report("invalid number: '%s'", arg); |
4307 | return 1; | |
4308 | } | |
4309 | ||
4310 | return 0; | |
4311 | } | |
4312 | ||
4313 | static int img_dd_if(const char *arg, | |
4314 | struct DdIo *in, struct DdIo *out, | |
4315 | struct DdInfo *dd) | |
4316 | { | |
4317 | in->filename = g_strdup(arg); | |
4318 | ||
4319 | return 0; | |
4320 | } | |
4321 | ||
4322 | static int img_dd_of(const char *arg, | |
4323 | struct DdIo *in, struct DdIo *out, | |
4324 | struct DdInfo *dd) | |
4325 | { | |
4326 | out->filename = g_strdup(arg); | |
4327 | ||
4328 | return 0; | |
4329 | } | |
4330 | ||
f7c15533 RS |
4331 | static int img_dd_skip(const char *arg, |
4332 | struct DdIo *in, struct DdIo *out, | |
4333 | struct DdInfo *dd) | |
4334 | { | |
606caa0a | 4335 | in->offset = cvtnum(arg); |
f7c15533 | 4336 | |
606caa0a | 4337 | if (in->offset < 0) { |
f7c15533 RS |
4338 | error_report("invalid number: '%s'", arg); |
4339 | return 1; | |
4340 | } | |
4341 | ||
4342 | return 0; | |
4343 | } | |
4344 | ||
86ce1f6e RS |
4345 | static int img_dd(int argc, char **argv) |
4346 | { | |
4347 | int ret = 0; | |
4348 | char *arg = NULL; | |
4349 | char *tmp; | |
4350 | BlockDriver *drv = NULL, *proto_drv = NULL; | |
4351 | BlockBackend *blk1 = NULL, *blk2 = NULL; | |
4352 | QemuOpts *opts = NULL; | |
4353 | QemuOptsList *create_opts = NULL; | |
4354 | Error *local_err = NULL; | |
4355 | bool image_opts = false; | |
4356 | int c, i; | |
4357 | const char *out_fmt = "raw"; | |
4358 | const char *fmt = NULL; | |
4359 | int64_t size = 0; | |
4360 | int64_t block_count = 0, out_pos, in_pos; | |
335e9937 | 4361 | bool force_share = false; |
86ce1f6e RS |
4362 | struct DdInfo dd = { |
4363 | .flags = 0, | |
4364 | .count = 0, | |
4365 | }; | |
4366 | struct DdIo in = { | |
4367 | .bsz = 512, /* Block size is by default 512 bytes */ | |
4368 | .filename = NULL, | |
f7c15533 RS |
4369 | .buf = NULL, |
4370 | .offset = 0 | |
86ce1f6e RS |
4371 | }; |
4372 | struct DdIo out = { | |
4373 | .bsz = 512, | |
4374 | .filename = NULL, | |
f7c15533 RS |
4375 | .buf = NULL, |
4376 | .offset = 0 | |
86ce1f6e RS |
4377 | }; |
4378 | ||
4379 | const struct DdOpts options[] = { | |
4380 | { "bs", img_dd_bs, C_BS }, | |
4381 | { "count", img_dd_count, C_COUNT }, | |
4382 | { "if", img_dd_if, C_IF }, | |
4383 | { "of", img_dd_of, C_OF }, | |
f7c15533 | 4384 | { "skip", img_dd_skip, C_SKIP }, |
86ce1f6e RS |
4385 | { NULL, NULL, 0 } |
4386 | }; | |
4387 | const struct option long_options[] = { | |
4388 | { "help", no_argument, 0, 'h'}, | |
83d4bf94 | 4389 | { "object", required_argument, 0, OPTION_OBJECT}, |
86ce1f6e | 4390 | { "image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, |
335e9937 | 4391 | { "force-share", no_argument, 0, 'U'}, |
86ce1f6e RS |
4392 | { 0, 0, 0, 0 } |
4393 | }; | |
4394 | ||
335e9937 | 4395 | while ((c = getopt_long(argc, argv, ":hf:O:U", long_options, NULL))) { |
86ce1f6e RS |
4396 | if (c == EOF) { |
4397 | break; | |
4398 | } | |
4399 | switch (c) { | |
4400 | case 'O': | |
4401 | out_fmt = optarg; | |
4402 | break; | |
4403 | case 'f': | |
4404 | fmt = optarg; | |
4405 | break; | |
c9192973 SH |
4406 | case ':': |
4407 | missing_argument(argv[optind - 1]); | |
4408 | break; | |
86ce1f6e | 4409 | case '?': |
c9192973 SH |
4410 | unrecognized_option(argv[optind - 1]); |
4411 | break; | |
86ce1f6e RS |
4412 | case 'h': |
4413 | help(); | |
4414 | break; | |
335e9937 FZ |
4415 | case 'U': |
4416 | force_share = true; | |
4417 | break; | |
2a245709 SH |
4418 | case OPTION_OBJECT: |
4419 | if (!qemu_opts_parse_noisily(&qemu_object_opts, optarg, true)) { | |
83d4bf94 DB |
4420 | ret = -1; |
4421 | goto out; | |
4422 | } | |
2a245709 | 4423 | break; |
86ce1f6e RS |
4424 | case OPTION_IMAGE_OPTS: |
4425 | image_opts = true; | |
4426 | break; | |
4427 | } | |
4428 | } | |
4429 | ||
4430 | for (i = optind; i < argc; i++) { | |
4431 | int j; | |
4432 | arg = g_strdup(argv[i]); | |
4433 | ||
4434 | tmp = strchr(arg, '='); | |
4435 | if (tmp == NULL) { | |
4436 | error_report("unrecognized operand %s", arg); | |
4437 | ret = -1; | |
4438 | goto out; | |
4439 | } | |
4440 | ||
4441 | *tmp++ = '\0'; | |
4442 | ||
4443 | for (j = 0; options[j].name != NULL; j++) { | |
4444 | if (!strcmp(arg, options[j].name)) { | |
4445 | break; | |
4446 | } | |
4447 | } | |
4448 | if (options[j].name == NULL) { | |
4449 | error_report("unrecognized operand %s", arg); | |
4450 | ret = -1; | |
4451 | goto out; | |
4452 | } | |
4453 | ||
4454 | if (options[j].f(tmp, &in, &out, &dd) != 0) { | |
4455 | ret = -1; | |
4456 | goto out; | |
4457 | } | |
4458 | dd.flags |= options[j].flag; | |
4459 | g_free(arg); | |
4460 | arg = NULL; | |
4461 | } | |
4462 | ||
4463 | if (!(dd.flags & C_IF && dd.flags & C_OF)) { | |
4464 | error_report("Must specify both input and output files"); | |
4465 | ret = -1; | |
4466 | goto out; | |
4467 | } | |
83d4bf94 DB |
4468 | |
4469 | if (qemu_opts_foreach(&qemu_object_opts, | |
4470 | user_creatable_add_opts_foreach, | |
4471 | NULL, NULL)) { | |
4472 | ret = -1; | |
4473 | goto out; | |
4474 | } | |
4475 | ||
335e9937 FZ |
4476 | blk1 = img_open(image_opts, in.filename, fmt, 0, false, false, |
4477 | force_share); | |
86ce1f6e RS |
4478 | |
4479 | if (!blk1) { | |
4480 | ret = -1; | |
4481 | goto out; | |
4482 | } | |
4483 | ||
4484 | drv = bdrv_find_format(out_fmt); | |
4485 | if (!drv) { | |
4486 | error_report("Unknown file format"); | |
4487 | ret = -1; | |
4488 | goto out; | |
4489 | } | |
4490 | proto_drv = bdrv_find_protocol(out.filename, true, &local_err); | |
4491 | ||
4492 | if (!proto_drv) { | |
4493 | error_report_err(local_err); | |
4494 | ret = -1; | |
4495 | goto out; | |
4496 | } | |
4497 | if (!drv->create_opts) { | |
4498 | error_report("Format driver '%s' does not support image creation", | |
4499 | drv->format_name); | |
4500 | ret = -1; | |
4501 | goto out; | |
4502 | } | |
4503 | if (!proto_drv->create_opts) { | |
4504 | error_report("Protocol driver '%s' does not support image creation", | |
4505 | proto_drv->format_name); | |
4506 | ret = -1; | |
4507 | goto out; | |
4508 | } | |
4509 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
4510 | create_opts = qemu_opts_append(create_opts, proto_drv->create_opts); | |
4511 | ||
4512 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
4513 | ||
4514 | size = blk_getlength(blk1); | |
4515 | if (size < 0) { | |
4516 | error_report("Failed to get size for '%s'", in.filename); | |
4517 | ret = -1; | |
4518 | goto out; | |
4519 | } | |
4520 | ||
4521 | if (dd.flags & C_COUNT && dd.count <= INT64_MAX / in.bsz && | |
4522 | dd.count * in.bsz < size) { | |
4523 | size = dd.count * in.bsz; | |
4524 | } | |
4525 | ||
f7c15533 RS |
4526 | /* Overflow means the specified offset is beyond input image's size */ |
4527 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || | |
4528 | size < in.bsz * in.offset)) { | |
4529 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, 0, &error_abort); | |
4530 | } else { | |
4531 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, | |
4532 | size - in.bsz * in.offset, &error_abort); | |
4533 | } | |
86ce1f6e RS |
4534 | |
4535 | ret = bdrv_create(drv, out.filename, opts, &local_err); | |
4536 | if (ret < 0) { | |
4537 | error_reportf_err(local_err, | |
4538 | "%s: error while creating output image: ", | |
4539 | out.filename); | |
4540 | ret = -1; | |
4541 | goto out; | |
4542 | } | |
4543 | ||
ea204dda DB |
4544 | /* TODO, we can't honour --image-opts for the target, |
4545 | * since it needs to be given in a format compatible | |
4546 | * with the bdrv_create() call above which does not | |
4547 | * support image-opts style. | |
4548 | */ | |
29cf9336 | 4549 | blk2 = img_open_file(out.filename, NULL, out_fmt, BDRV_O_RDWR, |
ea204dda | 4550 | false, false, false); |
86ce1f6e RS |
4551 | |
4552 | if (!blk2) { | |
4553 | ret = -1; | |
4554 | goto out; | |
4555 | } | |
4556 | ||
f7c15533 RS |
4557 | if (dd.flags & C_SKIP && (in.offset > INT64_MAX / in.bsz || |
4558 | size < in.offset * in.bsz)) { | |
4559 | /* We give a warning if the skip option is bigger than the input | |
4560 | * size and create an empty output disk image (i.e. like dd(1)). | |
4561 | */ | |
4562 | error_report("%s: cannot skip to specified offset", in.filename); | |
4563 | in_pos = size; | |
4564 | } else { | |
4565 | in_pos = in.offset * in.bsz; | |
4566 | } | |
4567 | ||
86ce1f6e RS |
4568 | in.buf = g_new(uint8_t, in.bsz); |
4569 | ||
f7c15533 | 4570 | for (out_pos = 0; in_pos < size; block_count++) { |
86ce1f6e RS |
4571 | int in_ret, out_ret; |
4572 | ||
4573 | if (in_pos + in.bsz > size) { | |
4574 | in_ret = blk_pread(blk1, in_pos, in.buf, size - in_pos); | |
4575 | } else { | |
4576 | in_ret = blk_pread(blk1, in_pos, in.buf, in.bsz); | |
4577 | } | |
4578 | if (in_ret < 0) { | |
4579 | error_report("error while reading from input image file: %s", | |
4580 | strerror(-in_ret)); | |
4581 | ret = -1; | |
4582 | goto out; | |
4583 | } | |
4584 | in_pos += in_ret; | |
4585 | ||
4586 | out_ret = blk_pwrite(blk2, out_pos, in.buf, in_ret, 0); | |
4587 | ||
4588 | if (out_ret < 0) { | |
4589 | error_report("error while writing to output image file: %s", | |
4590 | strerror(-out_ret)); | |
4591 | ret = -1; | |
4592 | goto out; | |
4593 | } | |
4594 | out_pos += out_ret; | |
4595 | } | |
4596 | ||
4597 | out: | |
4598 | g_free(arg); | |
4599 | qemu_opts_del(opts); | |
4600 | qemu_opts_free(create_opts); | |
4601 | blk_unref(blk1); | |
4602 | blk_unref(blk2); | |
4603 | g_free(in.filename); | |
4604 | g_free(out.filename); | |
4605 | g_free(in.buf); | |
4606 | g_free(out.buf); | |
4607 | ||
4608 | if (ret) { | |
b6133b8c KW |
4609 | return 1; |
4610 | } | |
4611 | return 0; | |
4612 | } | |
4613 | ||
fd03c2b8 SH |
4614 | static void dump_json_block_measure_info(BlockMeasureInfo *info) |
4615 | { | |
4616 | QString *str; | |
4617 | QObject *obj; | |
4618 | Visitor *v = qobject_output_visitor_new(&obj); | |
4619 | ||
4620 | visit_type_BlockMeasureInfo(v, NULL, &info, &error_abort); | |
4621 | visit_complete(v, &obj); | |
4622 | str = qobject_to_json_pretty(obj); | |
4623 | assert(str != NULL); | |
4624 | printf("%s\n", qstring_get_str(str)); | |
cb3e7f08 | 4625 | qobject_unref(obj); |
fd03c2b8 | 4626 | visit_free(v); |
cb3e7f08 | 4627 | qobject_unref(str); |
fd03c2b8 SH |
4628 | } |
4629 | ||
4630 | static int img_measure(int argc, char **argv) | |
4631 | { | |
4632 | static const struct option long_options[] = { | |
4633 | {"help", no_argument, 0, 'h'}, | |
4634 | {"image-opts", no_argument, 0, OPTION_IMAGE_OPTS}, | |
4635 | {"object", required_argument, 0, OPTION_OBJECT}, | |
4636 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
4637 | {"size", required_argument, 0, OPTION_SIZE}, | |
4638 | {"force-share", no_argument, 0, 'U'}, | |
4639 | {0, 0, 0, 0} | |
4640 | }; | |
4641 | OutputFormat output_format = OFORMAT_HUMAN; | |
4642 | BlockBackend *in_blk = NULL; | |
4643 | BlockDriver *drv; | |
4644 | const char *filename = NULL; | |
4645 | const char *fmt = NULL; | |
4646 | const char *out_fmt = "raw"; | |
4647 | char *options = NULL; | |
4648 | char *snapshot_name = NULL; | |
4649 | bool force_share = false; | |
4650 | QemuOpts *opts = NULL; | |
4651 | QemuOpts *object_opts = NULL; | |
4652 | QemuOpts *sn_opts = NULL; | |
4653 | QemuOptsList *create_opts = NULL; | |
4654 | bool image_opts = false; | |
4655 | uint64_t img_size = UINT64_MAX; | |
4656 | BlockMeasureInfo *info = NULL; | |
4657 | Error *local_err = NULL; | |
4658 | int ret = 1; | |
4659 | int c; | |
4660 | ||
4661 | while ((c = getopt_long(argc, argv, "hf:O:o:l:U", | |
4662 | long_options, NULL)) != -1) { | |
4663 | switch (c) { | |
4664 | case '?': | |
4665 | case 'h': | |
4666 | help(); | |
4667 | break; | |
4668 | case 'f': | |
4669 | fmt = optarg; | |
4670 | break; | |
4671 | case 'O': | |
4672 | out_fmt = optarg; | |
4673 | break; | |
4674 | case 'o': | |
4675 | if (!is_valid_option_list(optarg)) { | |
4676 | error_report("Invalid option list: %s", optarg); | |
4677 | goto out; | |
4678 | } | |
4679 | if (!options) { | |
4680 | options = g_strdup(optarg); | |
4681 | } else { | |
4682 | char *old_options = options; | |
4683 | options = g_strdup_printf("%s,%s", options, optarg); | |
4684 | g_free(old_options); | |
4685 | } | |
4686 | break; | |
4687 | case 'l': | |
4688 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
4689 | sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts, | |
4690 | optarg, false); | |
4691 | if (!sn_opts) { | |
4692 | error_report("Failed in parsing snapshot param '%s'", | |
4693 | optarg); | |
4694 | goto out; | |
4695 | } | |
4696 | } else { | |
4697 | snapshot_name = optarg; | |
4698 | } | |
4699 | break; | |
4700 | case 'U': | |
4701 | force_share = true; | |
4702 | break; | |
4703 | case OPTION_OBJECT: | |
4704 | object_opts = qemu_opts_parse_noisily(&qemu_object_opts, | |
4705 | optarg, true); | |
4706 | if (!object_opts) { | |
4707 | goto out; | |
4708 | } | |
4709 | break; | |
4710 | case OPTION_IMAGE_OPTS: | |
4711 | image_opts = true; | |
4712 | break; | |
4713 | case OPTION_OUTPUT: | |
4714 | if (!strcmp(optarg, "json")) { | |
4715 | output_format = OFORMAT_JSON; | |
4716 | } else if (!strcmp(optarg, "human")) { | |
4717 | output_format = OFORMAT_HUMAN; | |
4718 | } else { | |
4719 | error_report("--output must be used with human or json " | |
4720 | "as argument."); | |
4721 | goto out; | |
4722 | } | |
4723 | break; | |
4724 | case OPTION_SIZE: | |
4725 | { | |
4726 | int64_t sval; | |
4727 | ||
4728 | sval = cvtnum(optarg); | |
4729 | if (sval < 0) { | |
4730 | if (sval == -ERANGE) { | |
4731 | error_report("Image size must be less than 8 EiB!"); | |
4732 | } else { | |
4733 | error_report("Invalid image size specified! You may use " | |
4734 | "k, M, G, T, P or E suffixes for "); | |
4735 | error_report("kilobytes, megabytes, gigabytes, terabytes, " | |
4736 | "petabytes and exabytes."); | |
4737 | } | |
4738 | goto out; | |
4739 | } | |
4740 | img_size = (uint64_t)sval; | |
4741 | } | |
4742 | break; | |
4743 | } | |
4744 | } | |
4745 | ||
4746 | if (qemu_opts_foreach(&qemu_object_opts, | |
4747 | user_creatable_add_opts_foreach, | |
4748 | NULL, NULL)) { | |
4749 | goto out; | |
4750 | } | |
4751 | ||
4752 | if (argc - optind > 1) { | |
4753 | error_report("At most one filename argument is allowed."); | |
4754 | goto out; | |
4755 | } else if (argc - optind == 1) { | |
4756 | filename = argv[optind]; | |
4757 | } | |
4758 | ||
4759 | if (!filename && | |
4760 | (object_opts || image_opts || fmt || snapshot_name || sn_opts)) { | |
4761 | error_report("--object, --image-opts, -f, and -l " | |
4762 | "require a filename argument."); | |
4763 | goto out; | |
4764 | } | |
4765 | if (filename && img_size != UINT64_MAX) { | |
4766 | error_report("--size N cannot be used together with a filename."); | |
4767 | goto out; | |
4768 | } | |
4769 | if (!filename && img_size == UINT64_MAX) { | |
4770 | error_report("Either --size N or one filename must be specified."); | |
4771 | goto out; | |
4772 | } | |
4773 | ||
4774 | if (filename) { | |
4775 | in_blk = img_open(image_opts, filename, fmt, 0, | |
4776 | false, false, force_share); | |
4777 | if (!in_blk) { | |
4778 | goto out; | |
4779 | } | |
4780 | ||
4781 | if (sn_opts) { | |
4782 | bdrv_snapshot_load_tmp(blk_bs(in_blk), | |
4783 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
4784 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
4785 | &local_err); | |
4786 | } else if (snapshot_name != NULL) { | |
4787 | bdrv_snapshot_load_tmp_by_id_or_name(blk_bs(in_blk), | |
4788 | snapshot_name, &local_err); | |
4789 | } | |
4790 | if (local_err) { | |
4791 | error_reportf_err(local_err, "Failed to load snapshot: "); | |
4792 | goto out; | |
4793 | } | |
4794 | } | |
4795 | ||
4796 | drv = bdrv_find_format(out_fmt); | |
4797 | if (!drv) { | |
4798 | error_report("Unknown file format '%s'", out_fmt); | |
4799 | goto out; | |
4800 | } | |
4801 | if (!drv->create_opts) { | |
4802 | error_report("Format driver '%s' does not support image creation", | |
4803 | drv->format_name); | |
4804 | goto out; | |
4805 | } | |
4806 | ||
4807 | create_opts = qemu_opts_append(create_opts, drv->create_opts); | |
4808 | create_opts = qemu_opts_append(create_opts, bdrv_file.create_opts); | |
4809 | opts = qemu_opts_create(create_opts, NULL, 0, &error_abort); | |
4810 | if (options) { | |
4811 | qemu_opts_do_parse(opts, options, NULL, &local_err); | |
4812 | if (local_err) { | |
4813 | error_report_err(local_err); | |
4814 | error_report("Invalid options for file format '%s'", out_fmt); | |
4815 | goto out; | |
4816 | } | |
4817 | } | |
4818 | if (img_size != UINT64_MAX) { | |
4819 | qemu_opt_set_number(opts, BLOCK_OPT_SIZE, img_size, &error_abort); | |
4820 | } | |
4821 | ||
4822 | info = bdrv_measure(drv, opts, in_blk ? blk_bs(in_blk) : NULL, &local_err); | |
4823 | if (local_err) { | |
4824 | error_report_err(local_err); | |
4825 | goto out; | |
4826 | } | |
4827 | ||
4828 | if (output_format == OFORMAT_HUMAN) { | |
4829 | printf("required size: %" PRIu64 "\n", info->required); | |
4830 | printf("fully allocated size: %" PRIu64 "\n", info->fully_allocated); | |
4831 | } else { | |
4832 | dump_json_block_measure_info(info); | |
4833 | } | |
4834 | ||
4835 | ret = 0; | |
4836 | ||
4837 | out: | |
4838 | qapi_free_BlockMeasureInfo(info); | |
4839 | qemu_opts_del(object_opts); | |
4840 | qemu_opts_del(opts); | |
4841 | qemu_opts_del(sn_opts); | |
4842 | qemu_opts_free(create_opts); | |
4843 | g_free(options); | |
4844 | blk_unref(in_blk); | |
4845 | return ret; | |
4846 | } | |
b6133b8c | 4847 | |
c227f099 | 4848 | static const img_cmd_t img_cmds[] = { |
153859be SB |
4849 | #define DEF(option, callback, arg_string) \ |
4850 | { option, callback }, | |
4851 | #include "qemu-img-cmds.h" | |
4852 | #undef DEF | |
153859be SB |
4853 | { NULL, NULL, }, |
4854 | }; | |
4855 | ||
ea2384d3 FB |
4856 | int main(int argc, char **argv) |
4857 | { | |
c227f099 | 4858 | const img_cmd_t *cmd; |
153859be | 4859 | const char *cmdname; |
2f78e491 | 4860 | Error *local_error = NULL; |
06a1e0c1 | 4861 | char *trace_file = NULL; |
7db1689c | 4862 | int c; |
7db1689c JC |
4863 | static const struct option long_options[] = { |
4864 | {"help", no_argument, 0, 'h'}, | |
10985131 | 4865 | {"version", no_argument, 0, 'V'}, |
06a1e0c1 | 4866 | {"trace", required_argument, NULL, 'T'}, |
7db1689c JC |
4867 | {0, 0, 0, 0} |
4868 | }; | |
ea2384d3 | 4869 | |
526eda14 MK |
4870 | #ifdef CONFIG_POSIX |
4871 | signal(SIGPIPE, SIG_IGN); | |
4872 | #endif | |
4873 | ||
fe4db84d | 4874 | module_call_init(MODULE_INIT_TRACE); |
53f76e58 | 4875 | error_set_progname(argv[0]); |
10f5bff6 | 4876 | qemu_init_exec_dir(argv[0]); |
53f76e58 | 4877 | |
2f78e491 | 4878 | if (qemu_init_main_loop(&local_error)) { |
565f65d2 | 4879 | error_report_err(local_error); |
2f78e491 CN |
4880 | exit(EXIT_FAILURE); |
4881 | } | |
4882 | ||
e8f2d272 | 4883 | qcrypto_init(&error_fatal); |
c2297088 | 4884 | |
064097d9 | 4885 | module_call_init(MODULE_INIT_QOM); |
ea2384d3 | 4886 | bdrv_init(); |
ac1307ab FZ |
4887 | if (argc < 2) { |
4888 | error_exit("Not enough arguments"); | |
4889 | } | |
153859be | 4890 | |
3babeb15 | 4891 | qemu_add_opts(&qemu_object_opts); |
eb769f74 | 4892 | qemu_add_opts(&qemu_source_opts); |
06a1e0c1 | 4893 | qemu_add_opts(&qemu_trace_opts); |
3babeb15 | 4894 | |
c9192973 | 4895 | while ((c = getopt_long(argc, argv, "+:hVT:", long_options, NULL)) != -1) { |
10985131 | 4896 | switch (c) { |
c9192973 SH |
4897 | case ':': |
4898 | missing_argument(argv[optind - 1]); | |
4899 | return 0; | |
4581c16f | 4900 | case '?': |
c9192973 SH |
4901 | unrecognized_option(argv[optind - 1]); |
4902 | return 0; | |
4903 | case 'h': | |
10985131 DL |
4904 | help(); |
4905 | return 0; | |
4906 | case 'V': | |
4907 | printf(QEMU_IMG_VERSION); | |
4908 | return 0; | |
06a1e0c1 DL |
4909 | case 'T': |
4910 | g_free(trace_file); | |
4911 | trace_file = trace_opt_parse(optarg); | |
4912 | break; | |
153859be | 4913 | } |
ea2384d3 | 4914 | } |
153859be | 4915 | |
10985131 | 4916 | cmdname = argv[optind]; |
7db1689c | 4917 | |
10985131 DL |
4918 | /* reset getopt_long scanning */ |
4919 | argc -= optind; | |
4920 | if (argc < 1) { | |
5f6979cb JC |
4921 | return 0; |
4922 | } | |
10985131 | 4923 | argv += optind; |
cfef6a45 | 4924 | optind = 0; |
10985131 | 4925 | |
06a1e0c1 DL |
4926 | if (!trace_init_backends()) { |
4927 | exit(1); | |
4928 | } | |
4929 | trace_init_file(trace_file); | |
4930 | qemu_set_log(LOG_TRACE); | |
4931 | ||
10985131 DL |
4932 | /* find the command */ |
4933 | for (cmd = img_cmds; cmd->name != NULL; cmd++) { | |
4934 | if (!strcmp(cmdname, cmd->name)) { | |
4935 | return cmd->handler(argc, argv); | |
4936 | } | |
4937 | } | |
7db1689c | 4938 | |
153859be | 4939 | /* not found */ |
ac1307ab | 4940 | error_exit("Command not found: %s", cmdname); |
ea2384d3 | 4941 | } |