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