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