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