]>
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 | */ | |
c054b3fd BC |
24 | #include "qapi-visit.h" |
25 | #include "qapi/qmp-output-visitor.h" | |
7b1b5d19 | 26 | #include "qapi/qmp/qjson.h" |
faf07963 | 27 | #include "qemu-common.h" |
1de7afc9 PB |
28 | #include "qemu/option.h" |
29 | #include "qemu/error-report.h" | |
30 | #include "qemu/osdep.h" | |
9c17d615 | 31 | #include "sysemu/sysemu.h" |
737e150e | 32 | #include "block/block_int.h" |
f364ec65 | 33 | #include "block/qapi.h" |
c054b3fd | 34 | #include <getopt.h> |
9230eaf6 | 35 | #include <stdio.h> |
f382d43a | 36 | #include <stdarg.h> |
ea2384d3 | 37 | |
e8445331 FB |
38 | #ifdef _WIN32 |
39 | #include <windows.h> | |
40 | #endif | |
41 | ||
c227f099 | 42 | typedef struct img_cmd_t { |
153859be SB |
43 | const char *name; |
44 | int (*handler)(int argc, char **argv); | |
c227f099 | 45 | } img_cmd_t; |
153859be | 46 | |
8599ea4c FS |
47 | enum { |
48 | OPTION_OUTPUT = 256, | |
49 | OPTION_BACKING_CHAIN = 257, | |
50 | }; | |
51 | ||
52 | typedef enum OutputFormat { | |
53 | OFORMAT_JSON, | |
54 | OFORMAT_HUMAN, | |
55 | } OutputFormat; | |
56 | ||
137519ce | 57 | /* Default to cache=writeback as data integrity is not important for qemu-tcg. */ |
adfe078e | 58 | #define BDRV_O_FLAGS BDRV_O_CACHE_WB |
661a0f71 | 59 | #define BDRV_DEFAULT_CACHE "writeback" |
137519ce | 60 | |
ea2384d3 FB |
61 | static void format_print(void *opaque, const char *name) |
62 | { | |
63 | printf(" %s", name); | |
64 | } | |
65 | ||
d2c639d6 | 66 | /* Please keep in synch with qemu-img.texi */ |
3f379ab1 | 67 | static void help(void) |
ea2384d3 | 68 | { |
e00291c0 PB |
69 | const char *help_msg = |
70 | "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n" | |
3f020d70 | 71 | "usage: qemu-img command [command options]\n" |
72 | "QEMU disk image utility\n" | |
73 | "\n" | |
74 | "Command syntax:\n" | |
153859be SB |
75 | #define DEF(option, callback, arg_string) \ |
76 | " " arg_string "\n" | |
77 | #include "qemu-img-cmds.h" | |
78 | #undef DEF | |
79 | #undef GEN_DOCS | |
3f020d70 | 80 | "\n" |
81 | "Command parameters:\n" | |
82 | " 'filename' is a disk image filename\n" | |
83 | " 'fmt' is the disk image format. It is guessed automatically in most cases\n" | |
661a0f71 | 84 | " 'cache' is the cache mode used to write the output disk image, the valid\n" |
80ccf93b LY |
85 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
86 | " 'directsync' and 'unsafe' (default for convert)\n" | |
3f020d70 | 87 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
5e00984a KW |
88 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M),\n" |
89 | " 'T' (terabyte, 1024G), 'P' (petabyte, 1024T) and 'E' (exabyte, 1024P) are\n" | |
90 | " supported. 'b' is ignored.\n" | |
3f020d70 | 91 | " 'output_filename' is the destination disk image filename\n" |
92 | " 'output_fmt' is the destination format\n" | |
93 | " 'options' is a comma separated list of format specific options in a\n" | |
94 | " name=value format. Use -o ? for an overview of the options supported by the\n" | |
95 | " used format\n" | |
ef80654d WX |
96 | " 'snapshot_param' is param used for internal snapshot, format\n" |
97 | " is 'snapshot.id=[ID],snapshot.name=[NAME]', or\n" | |
98 | " '[ID_OR_NAME]'\n" | |
99 | " 'snapshot_id_or_name' is deprecated, use 'snapshot_param'\n" | |
100 | " instead\n" | |
3f020d70 | 101 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
102 | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n" | |
103 | " match exactly. The image doesn't need a working backing file before\n" | |
104 | " rebasing in this case (useful for renaming the backing file)\n" | |
105 | " '-h' with or without a command shows this help and lists the supported formats\n" | |
6b837bc4 | 106 | " '-p' show progress of command (only certain commands)\n" |
f382d43a | 107 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
11b6699a PL |
108 | " '-S' indicates the consecutive number of bytes (defaults to 4k) that must\n" |
109 | " contain only zeros for qemu-img to create a sparse image during\n" | |
110 | " conversion. If the number of bytes is 0, the source will not be scanned for\n" | |
111 | " unallocated or zero sectors, and the destination image will always be\n" | |
112 | " fully allocated\n" | |
c054b3fd | 113 | " '--output' takes the format in which the output must be done (human or json)\n" |
b2e10493 AD |
114 | " '-n' skips the target volume creation (useful if the volume is created\n" |
115 | " prior to running qemu-img)\n" | |
3f020d70 | 116 | "\n" |
4534ff54 KW |
117 | "Parameters to check subcommand:\n" |
118 | " '-r' tries to repair any inconsistencies that are found during the check.\n" | |
119 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" | |
120 | " kinds of errors, with a higher risk of choosing the wrong fix or\n" | |
0546b8c2 | 121 | " hiding corruption that has already occurred.\n" |
4534ff54 | 122 | "\n" |
3f020d70 | 123 | "Parameters to snapshot subcommand:\n" |
124 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" | |
125 | " '-a' applies a snapshot (revert disk to saved state)\n" | |
126 | " '-c' creates a snapshot\n" | |
127 | " '-d' deletes a snapshot\n" | |
d14ed18c MR |
128 | " '-l' lists all snapshots in the given image\n" |
129 | "\n" | |
130 | "Parameters to compare subcommand:\n" | |
131 | " '-f' first image format\n" | |
132 | " '-F' second image format\n" | |
133 | " '-s' run in Strict mode - fail on different image size or sector allocation\n"; | |
e00291c0 PB |
134 | |
135 | printf("%s\nSupported formats:", help_msg); | |
ea2384d3 FB |
136 | bdrv_iterate_format(format_print, NULL); |
137 | printf("\n"); | |
138 | exit(1); | |
139 | } | |
140 | ||
7c30f657 | 141 | static int GCC_FMT_ATTR(2, 3) qprintf(bool quiet, const char *fmt, ...) |
f382d43a MR |
142 | { |
143 | int ret = 0; | |
144 | if (!quiet) { | |
145 | va_list args; | |
146 | va_start(args, fmt); | |
147 | ret = vprintf(fmt, args); | |
148 | va_end(args); | |
149 | } | |
150 | return ret; | |
151 | } | |
152 | ||
ea2384d3 FB |
153 | #if defined(WIN32) |
154 | /* XXX: put correct support for win32 */ | |
155 | static int read_password(char *buf, int buf_size) | |
156 | { | |
157 | int c, i; | |
158 | printf("Password: "); | |
159 | fflush(stdout); | |
160 | i = 0; | |
161 | for(;;) { | |
162 | c = getchar(); | |
163 | if (c == '\n') | |
164 | break; | |
165 | if (i < (buf_size - 1)) | |
166 | buf[i++] = c; | |
167 | } | |
168 | buf[i] = '\0'; | |
169 | return 0; | |
170 | } | |
171 | ||
172 | #else | |
173 | ||
174 | #include <termios.h> | |
175 | ||
176 | static struct termios oldtty; | |
177 | ||
178 | static void term_exit(void) | |
179 | { | |
180 | tcsetattr (0, TCSANOW, &oldtty); | |
181 | } | |
182 | ||
183 | static void term_init(void) | |
184 | { | |
185 | struct termios tty; | |
186 | ||
187 | tcgetattr (0, &tty); | |
188 | oldtty = tty; | |
189 | ||
190 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP | |
191 | |INLCR|IGNCR|ICRNL|IXON); | |
192 | tty.c_oflag |= OPOST; | |
193 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); | |
194 | tty.c_cflag &= ~(CSIZE|PARENB); | |
195 | tty.c_cflag |= CS8; | |
196 | tty.c_cc[VMIN] = 1; | |
197 | tty.c_cc[VTIME] = 0; | |
3b46e624 | 198 | |
ea2384d3 FB |
199 | tcsetattr (0, TCSANOW, &tty); |
200 | ||
201 | atexit(term_exit); | |
202 | } | |
203 | ||
3f379ab1 | 204 | static int read_password(char *buf, int buf_size) |
ea2384d3 FB |
205 | { |
206 | uint8_t ch; | |
207 | int i, ret; | |
208 | ||
209 | printf("password: "); | |
210 | fflush(stdout); | |
211 | term_init(); | |
212 | i = 0; | |
213 | for(;;) { | |
214 | ret = read(0, &ch, 1); | |
215 | if (ret == -1) { | |
216 | if (errno == EAGAIN || errno == EINTR) { | |
217 | continue; | |
218 | } else { | |
219 | ret = -1; | |
220 | break; | |
221 | } | |
222 | } else if (ret == 0) { | |
223 | ret = -1; | |
224 | break; | |
225 | } else { | |
226 | if (ch == '\r') { | |
227 | ret = 0; | |
228 | break; | |
229 | } | |
230 | if (i < (buf_size - 1)) | |
231 | buf[i++] = ch; | |
232 | } | |
233 | } | |
234 | term_exit(); | |
235 | buf[i] = '\0'; | |
236 | printf("\n"); | |
237 | return ret; | |
238 | } | |
239 | #endif | |
240 | ||
4ac8aacd JS |
241 | static int print_block_option_help(const char *filename, const char *fmt) |
242 | { | |
243 | BlockDriver *drv, *proto_drv; | |
244 | QEMUOptionParameter *create_options = NULL; | |
245 | ||
246 | /* Find driver and parse its options */ | |
247 | drv = bdrv_find_format(fmt); | |
248 | if (!drv) { | |
15654a6d | 249 | error_report("Unknown file format '%s'", fmt); |
4ac8aacd JS |
250 | return 1; |
251 | } | |
252 | ||
98289620 | 253 | proto_drv = bdrv_find_protocol(filename, true); |
4ac8aacd | 254 | if (!proto_drv) { |
15654a6d | 255 | error_report("Unknown protocol '%s'", filename); |
4ac8aacd JS |
256 | return 1; |
257 | } | |
258 | ||
259 | create_options = append_option_parameters(create_options, | |
260 | drv->create_options); | |
261 | create_options = append_option_parameters(create_options, | |
262 | proto_drv->create_options); | |
263 | print_option_help(create_options); | |
264 | free_option_parameters(create_options); | |
265 | return 0; | |
266 | } | |
267 | ||
75c23805 | 268 | static BlockDriverState *bdrv_new_open(const char *filename, |
9bc378c1 | 269 | const char *fmt, |
f0536bb8 | 270 | int flags, |
f382d43a MR |
271 | bool require_io, |
272 | bool quiet) | |
75c23805 FB |
273 | { |
274 | BlockDriverState *bs; | |
275 | BlockDriver *drv; | |
276 | char password[256]; | |
34b5d2c6 | 277 | Error *local_err = NULL; |
b9eaf9ec | 278 | int ret; |
75c23805 | 279 | |
b9eaf9ec | 280 | bs = bdrv_new("image"); |
ad717139 | 281 | |
75c23805 FB |
282 | if (fmt) { |
283 | drv = bdrv_find_format(fmt); | |
c2abccec | 284 | if (!drv) { |
15654a6d | 285 | error_report("Unknown file format '%s'", fmt); |
c2abccec MK |
286 | goto fail; |
287 | } | |
75c23805 FB |
288 | } else { |
289 | drv = NULL; | |
290 | } | |
b9eaf9ec | 291 | |
34b5d2c6 | 292 | ret = bdrv_open(bs, filename, NULL, flags, drv, &local_err); |
b9eaf9ec | 293 | if (ret < 0) { |
34b5d2c6 HR |
294 | error_report("Could not open '%s': %s", filename, |
295 | error_get_pretty(local_err)); | |
296 | error_free(local_err); | |
c2abccec | 297 | goto fail; |
75c23805 | 298 | } |
b9eaf9ec | 299 | |
f0536bb8 | 300 | if (bdrv_is_encrypted(bs) && require_io) { |
f382d43a | 301 | qprintf(quiet, "Disk image '%s' is encrypted.\n", filename); |
c2abccec | 302 | if (read_password(password, sizeof(password)) < 0) { |
15654a6d | 303 | error_report("No password given"); |
c2abccec MK |
304 | goto fail; |
305 | } | |
306 | if (bdrv_set_key(bs, password) < 0) { | |
15654a6d | 307 | error_report("invalid password"); |
c2abccec MK |
308 | goto fail; |
309 | } | |
75c23805 FB |
310 | } |
311 | return bs; | |
c2abccec MK |
312 | fail: |
313 | if (bs) { | |
4f6fd349 | 314 | bdrv_unref(bs); |
c2abccec MK |
315 | } |
316 | return NULL; | |
75c23805 FB |
317 | } |
318 | ||
c2abccec | 319 | static int add_old_style_options(const char *fmt, QEMUOptionParameter *list, |
eec77d9e JS |
320 | const char *base_filename, |
321 | const char *base_fmt) | |
efa84d43 | 322 | { |
efa84d43 KW |
323 | if (base_filename) { |
324 | if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) { | |
15654a6d JS |
325 | error_report("Backing file not supported for file format '%s'", |
326 | fmt); | |
c2abccec | 327 | return -1; |
efa84d43 KW |
328 | } |
329 | } | |
330 | if (base_fmt) { | |
331 | if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) { | |
15654a6d JS |
332 | error_report("Backing file format not supported for file " |
333 | "format '%s'", fmt); | |
c2abccec | 334 | return -1; |
efa84d43 KW |
335 | } |
336 | } | |
c2abccec | 337 | return 0; |
efa84d43 KW |
338 | } |
339 | ||
ea2384d3 FB |
340 | static int img_create(int argc, char **argv) |
341 | { | |
a9300911 | 342 | int c; |
1da7cfbd | 343 | uint64_t img_size = -1; |
ea2384d3 | 344 | const char *fmt = "raw"; |
9230eaf6 | 345 | const char *base_fmt = NULL; |
ea2384d3 FB |
346 | const char *filename; |
347 | const char *base_filename = NULL; | |
9ea2ea71 | 348 | char *options = NULL; |
9b37525a | 349 | Error *local_err = NULL; |
f382d43a | 350 | bool quiet = false; |
3b46e624 | 351 | |
ea2384d3 | 352 | for(;;) { |
f382d43a | 353 | c = getopt(argc, argv, "F:b:f:he6o:q"); |
b8fb60da | 354 | if (c == -1) { |
ea2384d3 | 355 | break; |
b8fb60da | 356 | } |
ea2384d3 | 357 | switch(c) { |
ef87394c | 358 | case '?': |
ea2384d3 FB |
359 | case 'h': |
360 | help(); | |
361 | break; | |
9230eaf6 AL |
362 | case 'F': |
363 | base_fmt = optarg; | |
364 | break; | |
ea2384d3 FB |
365 | case 'b': |
366 | base_filename = optarg; | |
367 | break; | |
368 | case 'f': | |
369 | fmt = optarg; | |
370 | break; | |
371 | case 'e': | |
9d42e15d | 372 | error_report("option -e is deprecated, please use \'-o " |
eec77d9e JS |
373 | "encryption\' instead!"); |
374 | return 1; | |
d8871c5a | 375 | case '6': |
9d42e15d | 376 | error_report("option -6 is deprecated, please use \'-o " |
eec77d9e JS |
377 | "compat6\' instead!"); |
378 | return 1; | |
9ea2ea71 KW |
379 | case 'o': |
380 | options = optarg; | |
381 | break; | |
f382d43a MR |
382 | case 'q': |
383 | quiet = true; | |
384 | break; | |
ea2384d3 FB |
385 | } |
386 | } | |
9230eaf6 | 387 | |
b50cbabc | 388 | /* Get the filename */ |
b8fb60da | 389 | if (optind >= argc) { |
b50cbabc | 390 | help(); |
b8fb60da | 391 | } |
b50cbabc MK |
392 | filename = argv[optind++]; |
393 | ||
1da7cfbd JS |
394 | /* Get image size, if specified */ |
395 | if (optind < argc) { | |
70b4f4bb | 396 | int64_t sval; |
e36b3695 MA |
397 | char *end; |
398 | sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B); | |
399 | if (sval < 0 || *end) { | |
79443397 LG |
400 | if (sval == -ERANGE) { |
401 | error_report("Image size must be less than 8 EiB!"); | |
402 | } else { | |
403 | error_report("Invalid image size specified! You may use k, M, " | |
5e00984a KW |
404 | "G, T, P or E suffixes for "); |
405 | error_report("kilobytes, megabytes, gigabytes, terabytes, " | |
406 | "petabytes and exabytes."); | |
79443397 | 407 | } |
a9300911 | 408 | return 1; |
1da7cfbd JS |
409 | } |
410 | img_size = (uint64_t)sval; | |
411 | } | |
fc11eb26 KW |
412 | if (optind != argc) { |
413 | help(); | |
414 | } | |
1da7cfbd | 415 | |
c8057f95 | 416 | if (options && is_help_option(options)) { |
a9300911 | 417 | return print_block_option_help(filename, fmt); |
4ac8aacd JS |
418 | } |
419 | ||
9b37525a | 420 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
f382d43a | 421 | options, img_size, BDRV_O_FLAGS, &local_err, quiet); |
9b37525a | 422 | if (error_is_set(&local_err)) { |
b70d8c23 | 423 | error_report("%s: %s", filename, error_get_pretty(local_err)); |
9b37525a | 424 | error_free(local_err); |
c2abccec MK |
425 | return 1; |
426 | } | |
a9300911 | 427 | |
ea2384d3 FB |
428 | return 0; |
429 | } | |
430 | ||
f382d43a | 431 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
8599ea4c FS |
432 | { |
433 | Error *errp = NULL; | |
434 | QString *str; | |
435 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
436 | QObject *obj; | |
437 | visit_type_ImageCheck(qmp_output_get_visitor(ov), | |
438 | &check, NULL, &errp); | |
439 | obj = qmp_output_get_qobject(ov); | |
440 | str = qobject_to_json_pretty(obj); | |
441 | assert(str != NULL); | |
f382d43a | 442 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
8599ea4c FS |
443 | qobject_decref(obj); |
444 | qmp_output_visitor_cleanup(ov); | |
445 | QDECREF(str); | |
446 | } | |
447 | ||
f382d43a | 448 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
8599ea4c FS |
449 | { |
450 | if (!(check->corruptions || check->leaks || check->check_errors)) { | |
f382d43a | 451 | qprintf(quiet, "No errors were found on the image.\n"); |
8599ea4c FS |
452 | } else { |
453 | if (check->corruptions) { | |
f382d43a MR |
454 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
455 | "Data may be corrupted, or further writes to the image " | |
456 | "may corrupt it.\n", | |
457 | check->corruptions); | |
8599ea4c FS |
458 | } |
459 | ||
460 | if (check->leaks) { | |
f382d43a MR |
461 | qprintf(quiet, |
462 | "\n%" PRId64 " leaked clusters were found on the image.\n" | |
463 | "This means waste of disk space, but no harm to data.\n", | |
464 | check->leaks); | |
8599ea4c FS |
465 | } |
466 | ||
467 | if (check->check_errors) { | |
f382d43a MR |
468 | qprintf(quiet, |
469 | "\n%" PRId64 | |
470 | " internal errors have occurred during the check.\n", | |
471 | check->check_errors); | |
8599ea4c FS |
472 | } |
473 | } | |
474 | ||
475 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { | |
f382d43a MR |
476 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
477 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", | |
478 | check->allocated_clusters, check->total_clusters, | |
479 | check->allocated_clusters * 100.0 / check->total_clusters, | |
480 | check->fragmented_clusters * 100.0 / check->allocated_clusters, | |
481 | check->compressed_clusters * 100.0 / | |
482 | check->allocated_clusters); | |
8599ea4c FS |
483 | } |
484 | ||
485 | if (check->image_end_offset) { | |
f382d43a MR |
486 | qprintf(quiet, |
487 | "Image end offset: %" PRId64 "\n", check->image_end_offset); | |
8599ea4c FS |
488 | } |
489 | } | |
490 | ||
491 | static int collect_image_check(BlockDriverState *bs, | |
492 | ImageCheck *check, | |
493 | const char *filename, | |
494 | const char *fmt, | |
495 | int fix) | |
496 | { | |
497 | int ret; | |
498 | BdrvCheckResult result; | |
499 | ||
500 | ret = bdrv_check(bs, &result, fix); | |
501 | if (ret < 0) { | |
502 | return ret; | |
503 | } | |
504 | ||
505 | check->filename = g_strdup(filename); | |
506 | check->format = g_strdup(bdrv_get_format_name(bs)); | |
507 | check->check_errors = result.check_errors; | |
508 | check->corruptions = result.corruptions; | |
509 | check->has_corruptions = result.corruptions != 0; | |
510 | check->leaks = result.leaks; | |
511 | check->has_leaks = result.leaks != 0; | |
512 | check->corruptions_fixed = result.corruptions_fixed; | |
513 | check->has_corruptions_fixed = result.corruptions != 0; | |
514 | check->leaks_fixed = result.leaks_fixed; | |
515 | check->has_leaks_fixed = result.leaks != 0; | |
516 | check->image_end_offset = result.image_end_offset; | |
517 | check->has_image_end_offset = result.image_end_offset != 0; | |
518 | check->total_clusters = result.bfi.total_clusters; | |
519 | check->has_total_clusters = result.bfi.total_clusters != 0; | |
520 | check->allocated_clusters = result.bfi.allocated_clusters; | |
521 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; | |
522 | check->fragmented_clusters = result.bfi.fragmented_clusters; | |
523 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; | |
e6439d78 SH |
524 | check->compressed_clusters = result.bfi.compressed_clusters; |
525 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; | |
8599ea4c FS |
526 | |
527 | return 0; | |
528 | } | |
529 | ||
e076f338 KW |
530 | /* |
531 | * Checks an image for consistency. Exit codes: | |
532 | * | |
533 | * 0 - Check completed, image is good | |
534 | * 1 - Check not completed because of internal errors | |
535 | * 2 - Check completed, image is corrupted | |
536 | * 3 - Check completed, image has leaked clusters, but is good otherwise | |
537 | */ | |
1585969c AL |
538 | static int img_check(int argc, char **argv) |
539 | { | |
540 | int c, ret; | |
8599ea4c FS |
541 | OutputFormat output_format = OFORMAT_HUMAN; |
542 | const char *filename, *fmt, *output; | |
1585969c | 543 | BlockDriverState *bs; |
4534ff54 | 544 | int fix = 0; |
058f8f16 | 545 | int flags = BDRV_O_FLAGS | BDRV_O_CHECK; |
8599ea4c | 546 | ImageCheck *check; |
f382d43a | 547 | bool quiet = false; |
1585969c AL |
548 | |
549 | fmt = NULL; | |
8599ea4c | 550 | output = NULL; |
1585969c | 551 | for(;;) { |
8599ea4c FS |
552 | int option_index = 0; |
553 | static const struct option long_options[] = { | |
554 | {"help", no_argument, 0, 'h'}, | |
555 | {"format", required_argument, 0, 'f'}, | |
556 | {"repair", no_argument, 0, 'r'}, | |
557 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
558 | {0, 0, 0, 0} | |
559 | }; | |
f382d43a | 560 | c = getopt_long(argc, argv, "f:hr:q", |
8599ea4c | 561 | long_options, &option_index); |
b8fb60da | 562 | if (c == -1) { |
1585969c | 563 | break; |
b8fb60da | 564 | } |
1585969c | 565 | switch(c) { |
ef87394c | 566 | case '?': |
1585969c AL |
567 | case 'h': |
568 | help(); | |
569 | break; | |
570 | case 'f': | |
571 | fmt = optarg; | |
572 | break; | |
4534ff54 KW |
573 | case 'r': |
574 | flags |= BDRV_O_RDWR; | |
575 | ||
576 | if (!strcmp(optarg, "leaks")) { | |
577 | fix = BDRV_FIX_LEAKS; | |
578 | } else if (!strcmp(optarg, "all")) { | |
579 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; | |
580 | } else { | |
581 | help(); | |
582 | } | |
583 | break; | |
8599ea4c FS |
584 | case OPTION_OUTPUT: |
585 | output = optarg; | |
586 | break; | |
f382d43a MR |
587 | case 'q': |
588 | quiet = true; | |
589 | break; | |
1585969c AL |
590 | } |
591 | } | |
fc11eb26 | 592 | if (optind != argc - 1) { |
1585969c | 593 | help(); |
b8fb60da | 594 | } |
1585969c AL |
595 | filename = argv[optind++]; |
596 | ||
8599ea4c FS |
597 | if (output && !strcmp(output, "json")) { |
598 | output_format = OFORMAT_JSON; | |
599 | } else if (output && !strcmp(output, "human")) { | |
600 | output_format = OFORMAT_HUMAN; | |
601 | } else if (output) { | |
602 | error_report("--output must be used with human or json as argument."); | |
603 | return 1; | |
604 | } | |
605 | ||
f382d43a | 606 | bs = bdrv_new_open(filename, fmt, flags, true, quiet); |
c2abccec MK |
607 | if (!bs) { |
608 | return 1; | |
609 | } | |
8599ea4c FS |
610 | |
611 | check = g_new0(ImageCheck, 1); | |
612 | ret = collect_image_check(bs, check, filename, fmt, fix); | |
e076f338 KW |
613 | |
614 | if (ret == -ENOTSUP) { | |
8599ea4c FS |
615 | if (output_format == OFORMAT_HUMAN) { |
616 | error_report("This image format does not support checks"); | |
617 | } | |
fefddf95 | 618 | ret = 63; |
8599ea4c | 619 | goto fail; |
e076f338 KW |
620 | } |
621 | ||
8599ea4c FS |
622 | if (check->corruptions_fixed || check->leaks_fixed) { |
623 | int corruptions_fixed, leaks_fixed; | |
ccf34716 | 624 | |
8599ea4c FS |
625 | leaks_fixed = check->leaks_fixed; |
626 | corruptions_fixed = check->corruptions_fixed; | |
e076f338 | 627 | |
8599ea4c | 628 | if (output_format == OFORMAT_HUMAN) { |
f382d43a MR |
629 | qprintf(quiet, |
630 | "The following inconsistencies were found and repaired:\n\n" | |
631 | " %" PRId64 " leaked clusters\n" | |
632 | " %" PRId64 " corruptions\n\n" | |
633 | "Double checking the fixed image now...\n", | |
634 | check->leaks_fixed, | |
635 | check->corruptions_fixed); | |
e076f338 KW |
636 | } |
637 | ||
8599ea4c | 638 | ret = collect_image_check(bs, check, filename, fmt, 0); |
1585969c | 639 | |
8599ea4c FS |
640 | check->leaks_fixed = leaks_fixed; |
641 | check->corruptions_fixed = corruptions_fixed; | |
f8111c24 DXW |
642 | } |
643 | ||
8599ea4c FS |
644 | switch (output_format) { |
645 | case OFORMAT_HUMAN: | |
f382d43a | 646 | dump_human_image_check(check, quiet); |
8599ea4c FS |
647 | break; |
648 | case OFORMAT_JSON: | |
f382d43a | 649 | dump_json_image_check(check, quiet); |
8599ea4c | 650 | break; |
c6bb9ad1 FS |
651 | } |
652 | ||
8599ea4c FS |
653 | if (ret || check->check_errors) { |
654 | ret = 1; | |
655 | goto fail; | |
c2abccec | 656 | } |
e076f338 | 657 | |
8599ea4c FS |
658 | if (check->corruptions) { |
659 | ret = 2; | |
660 | } else if (check->leaks) { | |
661 | ret = 3; | |
e076f338 | 662 | } else { |
8599ea4c | 663 | ret = 0; |
e076f338 | 664 | } |
8599ea4c FS |
665 | |
666 | fail: | |
667 | qapi_free_ImageCheck(check); | |
4f6fd349 | 668 | bdrv_unref(bs); |
8599ea4c FS |
669 | |
670 | return ret; | |
1585969c AL |
671 | } |
672 | ||
ea2384d3 FB |
673 | static int img_commit(int argc, char **argv) |
674 | { | |
661a0f71 FS |
675 | int c, ret, flags; |
676 | const char *filename, *fmt, *cache; | |
ea2384d3 | 677 | BlockDriverState *bs; |
f382d43a | 678 | bool quiet = false; |
ea2384d3 FB |
679 | |
680 | fmt = NULL; | |
661a0f71 | 681 | cache = BDRV_DEFAULT_CACHE; |
ea2384d3 | 682 | for(;;) { |
f382d43a | 683 | c = getopt(argc, argv, "f:ht:q"); |
b8fb60da | 684 | if (c == -1) { |
ea2384d3 | 685 | break; |
b8fb60da | 686 | } |
ea2384d3 | 687 | switch(c) { |
ef87394c | 688 | case '?': |
ea2384d3 FB |
689 | case 'h': |
690 | help(); | |
691 | break; | |
692 | case 'f': | |
693 | fmt = optarg; | |
694 | break; | |
661a0f71 FS |
695 | case 't': |
696 | cache = optarg; | |
697 | break; | |
f382d43a MR |
698 | case 'q': |
699 | quiet = true; | |
700 | break; | |
ea2384d3 FB |
701 | } |
702 | } | |
fc11eb26 | 703 | if (optind != argc - 1) { |
ea2384d3 | 704 | help(); |
b8fb60da | 705 | } |
ea2384d3 FB |
706 | filename = argv[optind++]; |
707 | ||
661a0f71 | 708 | flags = BDRV_O_RDWR; |
c3993cdc | 709 | ret = bdrv_parse_cache_flags(cache, &flags); |
661a0f71 FS |
710 | if (ret < 0) { |
711 | error_report("Invalid cache option: %s", cache); | |
712 | return -1; | |
713 | } | |
714 | ||
f382d43a | 715 | bs = bdrv_new_open(filename, fmt, flags, true, quiet); |
c2abccec MK |
716 | if (!bs) { |
717 | return 1; | |
718 | } | |
ea2384d3 FB |
719 | ret = bdrv_commit(bs); |
720 | switch(ret) { | |
721 | case 0: | |
f382d43a | 722 | qprintf(quiet, "Image committed.\n"); |
ea2384d3 FB |
723 | break; |
724 | case -ENOENT: | |
15654a6d | 725 | error_report("No disk inserted"); |
ea2384d3 FB |
726 | break; |
727 | case -EACCES: | |
15654a6d | 728 | error_report("Image is read-only"); |
ea2384d3 FB |
729 | break; |
730 | case -ENOTSUP: | |
15654a6d | 731 | error_report("Image is already committed"); |
ea2384d3 FB |
732 | break; |
733 | default: | |
15654a6d | 734 | error_report("Error while committing image"); |
ea2384d3 FB |
735 | break; |
736 | } | |
737 | ||
4f6fd349 | 738 | bdrv_unref(bs); |
c2abccec MK |
739 | if (ret) { |
740 | return 1; | |
741 | } | |
ea2384d3 FB |
742 | return 0; |
743 | } | |
744 | ||
f58c7b35 TS |
745 | /* |
746 | * Returns true iff the first sector pointed to by 'buf' contains at least | |
747 | * a non-NUL byte. | |
748 | * | |
749 | * 'pnum' is set to the number of sectors (including and immediately following | |
750 | * the first one) that are known to be in the same allocated/unallocated state. | |
751 | */ | |
ea2384d3 FB |
752 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
753 | { | |
1a6d39fd SH |
754 | bool is_zero; |
755 | int i; | |
ea2384d3 FB |
756 | |
757 | if (n <= 0) { | |
758 | *pnum = 0; | |
759 | return 0; | |
760 | } | |
1a6d39fd | 761 | is_zero = buffer_is_zero(buf, 512); |
ea2384d3 FB |
762 | for(i = 1; i < n; i++) { |
763 | buf += 512; | |
1a6d39fd | 764 | if (is_zero != buffer_is_zero(buf, 512)) { |
ea2384d3 | 765 | break; |
1a6d39fd | 766 | } |
ea2384d3 FB |
767 | } |
768 | *pnum = i; | |
1a6d39fd | 769 | return !is_zero; |
ea2384d3 FB |
770 | } |
771 | ||
a22f123c KW |
772 | /* |
773 | * Like is_allocated_sectors, but if the buffer starts with a used sector, | |
774 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids | |
775 | * breaking up write requests for only small sparse areas. | |
776 | */ | |
777 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, | |
778 | int min) | |
779 | { | |
780 | int ret; | |
781 | int num_checked, num_used; | |
782 | ||
783 | if (n < min) { | |
784 | min = n; | |
785 | } | |
786 | ||
787 | ret = is_allocated_sectors(buf, n, pnum); | |
788 | if (!ret) { | |
789 | return ret; | |
790 | } | |
791 | ||
792 | num_used = *pnum; | |
793 | buf += BDRV_SECTOR_SIZE * *pnum; | |
794 | n -= *pnum; | |
795 | num_checked = num_used; | |
796 | ||
797 | while (n > 0) { | |
798 | ret = is_allocated_sectors(buf, n, pnum); | |
799 | ||
800 | buf += BDRV_SECTOR_SIZE * *pnum; | |
801 | n -= *pnum; | |
802 | num_checked += *pnum; | |
803 | if (ret) { | |
804 | num_used = num_checked; | |
805 | } else if (*pnum >= min) { | |
806 | break; | |
807 | } | |
808 | } | |
809 | ||
810 | *pnum = num_used; | |
811 | return 1; | |
812 | } | |
813 | ||
3e85c6fd KW |
814 | /* |
815 | * Compares two buffers sector by sector. Returns 0 if the first sector of both | |
816 | * buffers matches, non-zero otherwise. | |
817 | * | |
818 | * pnum is set to the number of sectors (including and immediately following | |
819 | * the first one) that are known to have the same comparison result | |
820 | */ | |
821 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, | |
822 | int *pnum) | |
823 | { | |
824 | int res, i; | |
825 | ||
826 | if (n <= 0) { | |
827 | *pnum = 0; | |
828 | return 0; | |
829 | } | |
830 | ||
831 | res = !!memcmp(buf1, buf2, 512); | |
832 | for(i = 1; i < n; i++) { | |
833 | buf1 += 512; | |
834 | buf2 += 512; | |
835 | ||
836 | if (!!memcmp(buf1, buf2, 512) != res) { | |
837 | break; | |
838 | } | |
839 | } | |
840 | ||
841 | *pnum = i; | |
842 | return res; | |
843 | } | |
844 | ||
80ee15a6 | 845 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
ea2384d3 | 846 | |
d14ed18c MR |
847 | static int64_t sectors_to_bytes(int64_t sectors) |
848 | { | |
849 | return sectors << BDRV_SECTOR_BITS; | |
850 | } | |
851 | ||
852 | static int64_t sectors_to_process(int64_t total, int64_t from) | |
853 | { | |
854 | return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS); | |
855 | } | |
856 | ||
857 | /* | |
858 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) | |
859 | * | |
860 | * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero | |
861 | * data and negative value on error. | |
862 | * | |
863 | * @param bs: Driver used for accessing file | |
864 | * @param sect_num: Number of first sector to check | |
865 | * @param sect_count: Number of sectors to check | |
866 | * @param filename: Name of disk file we are checking (logging purpose) | |
867 | * @param buffer: Allocated buffer for storing read data | |
868 | * @param quiet: Flag for quiet mode | |
869 | */ | |
870 | static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num, | |
871 | int sect_count, const char *filename, | |
872 | uint8_t *buffer, bool quiet) | |
873 | { | |
874 | int pnum, ret = 0; | |
875 | ret = bdrv_read(bs, sect_num, buffer, sect_count); | |
876 | if (ret < 0) { | |
877 | error_report("Error while reading offset %" PRId64 " of %s: %s", | |
878 | sectors_to_bytes(sect_num), filename, strerror(-ret)); | |
879 | return ret; | |
880 | } | |
881 | ret = is_allocated_sectors(buffer, sect_count, &pnum); | |
882 | if (ret || pnum != sect_count) { | |
883 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", | |
884 | sectors_to_bytes(ret ? sect_num : sect_num + pnum)); | |
885 | return 1; | |
886 | } | |
887 | ||
888 | return 0; | |
889 | } | |
890 | ||
891 | /* | |
892 | * Compares two images. Exit codes: | |
893 | * | |
894 | * 0 - Images are identical | |
895 | * 1 - Images differ | |
896 | * >1 - Error occurred | |
897 | */ | |
898 | static int img_compare(int argc, char **argv) | |
899 | { | |
900 | const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2; | |
901 | BlockDriverState *bs1, *bs2; | |
902 | int64_t total_sectors1, total_sectors2; | |
903 | uint8_t *buf1 = NULL, *buf2 = NULL; | |
904 | int pnum1, pnum2; | |
905 | int allocated1, allocated2; | |
906 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ | |
907 | bool progress = false, quiet = false, strict = false; | |
908 | int64_t total_sectors; | |
909 | int64_t sector_num = 0; | |
910 | int64_t nb_sectors; | |
911 | int c, pnum; | |
912 | uint64_t bs_sectors; | |
913 | uint64_t progress_base; | |
914 | ||
915 | for (;;) { | |
916 | c = getopt(argc, argv, "hpf:F:sq"); | |
917 | if (c == -1) { | |
918 | break; | |
919 | } | |
920 | switch (c) { | |
921 | case '?': | |
922 | case 'h': | |
923 | help(); | |
924 | break; | |
925 | case 'f': | |
926 | fmt1 = optarg; | |
927 | break; | |
928 | case 'F': | |
929 | fmt2 = optarg; | |
930 | break; | |
931 | case 'p': | |
932 | progress = true; | |
933 | break; | |
934 | case 'q': | |
935 | quiet = true; | |
936 | break; | |
937 | case 's': | |
938 | strict = true; | |
939 | break; | |
940 | } | |
941 | } | |
942 | ||
943 | /* Progress is not shown in Quiet mode */ | |
944 | if (quiet) { | |
945 | progress = false; | |
946 | } | |
947 | ||
948 | ||
fc11eb26 | 949 | if (optind != argc - 2) { |
d14ed18c MR |
950 | help(); |
951 | } | |
952 | filename1 = argv[optind++]; | |
953 | filename2 = argv[optind++]; | |
954 | ||
955 | /* Initialize before goto out */ | |
956 | qemu_progress_init(progress, 2.0); | |
957 | ||
958 | bs1 = bdrv_new_open(filename1, fmt1, BDRV_O_FLAGS, true, quiet); | |
959 | if (!bs1) { | |
960 | error_report("Can't open file %s", filename1); | |
961 | ret = 2; | |
962 | goto out3; | |
963 | } | |
964 | ||
965 | bs2 = bdrv_new_open(filename2, fmt2, BDRV_O_FLAGS, true, quiet); | |
966 | if (!bs2) { | |
967 | error_report("Can't open file %s", filename2); | |
968 | ret = 2; | |
969 | goto out2; | |
970 | } | |
971 | ||
972 | buf1 = qemu_blockalign(bs1, IO_BUF_SIZE); | |
973 | buf2 = qemu_blockalign(bs2, IO_BUF_SIZE); | |
974 | bdrv_get_geometry(bs1, &bs_sectors); | |
975 | total_sectors1 = bs_sectors; | |
976 | bdrv_get_geometry(bs2, &bs_sectors); | |
977 | total_sectors2 = bs_sectors; | |
978 | total_sectors = MIN(total_sectors1, total_sectors2); | |
979 | progress_base = MAX(total_sectors1, total_sectors2); | |
980 | ||
981 | qemu_progress_print(0, 100); | |
982 | ||
983 | if (strict && total_sectors1 != total_sectors2) { | |
984 | ret = 1; | |
985 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); | |
986 | goto out; | |
987 | } | |
988 | ||
989 | for (;;) { | |
990 | nb_sectors = sectors_to_process(total_sectors, sector_num); | |
991 | if (nb_sectors <= 0) { | |
992 | break; | |
993 | } | |
994 | allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors, | |
995 | &pnum1); | |
996 | if (allocated1 < 0) { | |
997 | ret = 3; | |
998 | error_report("Sector allocation test failed for %s", filename1); | |
999 | goto out; | |
1000 | } | |
1001 | ||
1002 | allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors, | |
1003 | &pnum2); | |
1004 | if (allocated2 < 0) { | |
1005 | ret = 3; | |
1006 | error_report("Sector allocation test failed for %s", filename2); | |
1007 | goto out; | |
1008 | } | |
1009 | nb_sectors = MIN(pnum1, pnum2); | |
1010 | ||
1011 | if (allocated1 == allocated2) { | |
1012 | if (allocated1) { | |
1013 | ret = bdrv_read(bs1, sector_num, buf1, nb_sectors); | |
1014 | if (ret < 0) { | |
1015 | error_report("Error while reading offset %" PRId64 " of %s:" | |
1016 | " %s", sectors_to_bytes(sector_num), filename1, | |
1017 | strerror(-ret)); | |
1018 | ret = 4; | |
1019 | goto out; | |
1020 | } | |
1021 | ret = bdrv_read(bs2, sector_num, buf2, nb_sectors); | |
1022 | if (ret < 0) { | |
1023 | error_report("Error while reading offset %" PRId64 | |
1024 | " of %s: %s", sectors_to_bytes(sector_num), | |
1025 | filename2, strerror(-ret)); | |
1026 | ret = 4; | |
1027 | goto out; | |
1028 | } | |
1029 | ret = compare_sectors(buf1, buf2, nb_sectors, &pnum); | |
1030 | if (ret || pnum != nb_sectors) { | |
d14ed18c MR |
1031 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
1032 | sectors_to_bytes( | |
1033 | ret ? sector_num : sector_num + pnum)); | |
36452f12 | 1034 | ret = 1; |
d14ed18c MR |
1035 | goto out; |
1036 | } | |
1037 | } | |
1038 | } else { | |
1039 | if (strict) { | |
1040 | ret = 1; | |
1041 | qprintf(quiet, "Strict mode: Offset %" PRId64 | |
1042 | " allocation mismatch!\n", | |
1043 | sectors_to_bytes(sector_num)); | |
1044 | goto out; | |
1045 | } | |
1046 | ||
1047 | if (allocated1) { | |
1048 | ret = check_empty_sectors(bs1, sector_num, nb_sectors, | |
1049 | filename1, buf1, quiet); | |
1050 | } else { | |
1051 | ret = check_empty_sectors(bs2, sector_num, nb_sectors, | |
1052 | filename2, buf1, quiet); | |
1053 | } | |
1054 | if (ret) { | |
1055 | if (ret < 0) { | |
d14ed18c MR |
1056 | error_report("Error while reading offset %" PRId64 ": %s", |
1057 | sectors_to_bytes(sector_num), strerror(-ret)); | |
36452f12 | 1058 | ret = 4; |
d14ed18c MR |
1059 | } |
1060 | goto out; | |
1061 | } | |
1062 | } | |
1063 | sector_num += nb_sectors; | |
1064 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); | |
1065 | } | |
1066 | ||
1067 | if (total_sectors1 != total_sectors2) { | |
1068 | BlockDriverState *bs_over; | |
1069 | int64_t total_sectors_over; | |
1070 | const char *filename_over; | |
1071 | ||
1072 | qprintf(quiet, "Warning: Image size mismatch!\n"); | |
1073 | if (total_sectors1 > total_sectors2) { | |
1074 | total_sectors_over = total_sectors1; | |
1075 | bs_over = bs1; | |
1076 | filename_over = filename1; | |
1077 | } else { | |
1078 | total_sectors_over = total_sectors2; | |
1079 | bs_over = bs2; | |
1080 | filename_over = filename2; | |
1081 | } | |
1082 | ||
1083 | for (;;) { | |
1084 | nb_sectors = sectors_to_process(total_sectors_over, sector_num); | |
1085 | if (nb_sectors <= 0) { | |
1086 | break; | |
1087 | } | |
1088 | ret = bdrv_is_allocated_above(bs_over, NULL, sector_num, | |
1089 | nb_sectors, &pnum); | |
1090 | if (ret < 0) { | |
1091 | ret = 3; | |
1092 | error_report("Sector allocation test failed for %s", | |
1093 | filename_over); | |
1094 | goto out; | |
1095 | ||
1096 | } | |
1097 | nb_sectors = pnum; | |
1098 | if (ret) { | |
1099 | ret = check_empty_sectors(bs_over, sector_num, nb_sectors, | |
1100 | filename_over, buf1, quiet); | |
1101 | if (ret) { | |
1102 | if (ret < 0) { | |
d14ed18c MR |
1103 | error_report("Error while reading offset %" PRId64 |
1104 | " of %s: %s", sectors_to_bytes(sector_num), | |
1105 | filename_over, strerror(-ret)); | |
36452f12 | 1106 | ret = 4; |
d14ed18c MR |
1107 | } |
1108 | goto out; | |
1109 | } | |
1110 | } | |
1111 | sector_num += nb_sectors; | |
1112 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); | |
1113 | } | |
1114 | } | |
1115 | ||
1116 | qprintf(quiet, "Images are identical.\n"); | |
1117 | ret = 0; | |
1118 | ||
1119 | out: | |
4f6fd349 | 1120 | bdrv_unref(bs2); |
d14ed18c MR |
1121 | qemu_vfree(buf1); |
1122 | qemu_vfree(buf2); | |
1123 | out2: | |
4f6fd349 | 1124 | bdrv_unref(bs1); |
d14ed18c MR |
1125 | out3: |
1126 | qemu_progress_end(); | |
1127 | return ret; | |
1128 | } | |
1129 | ||
ea2384d3 FB |
1130 | static int img_convert(int argc, char **argv) |
1131 | { | |
13c28af8 | 1132 | int c, n, n1, bs_n, bs_i, compress, cluster_size, |
b2e10493 | 1133 | cluster_sectors, skip_create; |
13c28af8 | 1134 | int64_t ret = 0; |
661a0f71 FS |
1135 | int progress = 0, flags; |
1136 | const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename; | |
b50cbabc | 1137 | BlockDriver *drv, *proto_drv; |
c2abccec | 1138 | BlockDriverState **bs = NULL, *out_bs = NULL; |
13c28af8 PL |
1139 | int64_t total_sectors, nb_sectors, sector_num, bs_offset, |
1140 | sector_num_next_status = 0; | |
96b8f136 | 1141 | uint64_t bs_sectors; |
c2abccec | 1142 | uint8_t * buf = NULL; |
ea2384d3 | 1143 | const uint8_t *buf1; |
faea38e7 | 1144 | BlockDriverInfo bdi; |
b50cbabc | 1145 | QEMUOptionParameter *param = NULL, *create_options = NULL; |
a18953fb | 1146 | QEMUOptionParameter *out_baseimg_param; |
efa84d43 | 1147 | char *options = NULL; |
51ef6727 | 1148 | const char *snapshot_name = NULL; |
a22f123c | 1149 | int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ |
f382d43a | 1150 | bool quiet = false; |
cc84d90f | 1151 | Error *local_err = NULL; |
ef80654d | 1152 | QemuOpts *sn_opts = NULL; |
ea2384d3 FB |
1153 | |
1154 | fmt = NULL; | |
1155 | out_fmt = "raw"; | |
661a0f71 | 1156 | cache = "unsafe"; |
f58c7b35 | 1157 | out_baseimg = NULL; |
eec77d9e | 1158 | compress = 0; |
b2e10493 | 1159 | skip_create = 0; |
ea2384d3 | 1160 | for(;;) { |
ef80654d | 1161 | c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:qnl:"); |
b8fb60da | 1162 | if (c == -1) { |
ea2384d3 | 1163 | break; |
b8fb60da | 1164 | } |
ea2384d3 | 1165 | switch(c) { |
ef87394c | 1166 | case '?': |
ea2384d3 FB |
1167 | case 'h': |
1168 | help(); | |
1169 | break; | |
1170 | case 'f': | |
1171 | fmt = optarg; | |
1172 | break; | |
1173 | case 'O': | |
1174 | out_fmt = optarg; | |
1175 | break; | |
f58c7b35 TS |
1176 | case 'B': |
1177 | out_baseimg = optarg; | |
1178 | break; | |
ea2384d3 | 1179 | case 'c': |
eec77d9e | 1180 | compress = 1; |
ea2384d3 FB |
1181 | break; |
1182 | case 'e': | |
9d42e15d | 1183 | error_report("option -e is deprecated, please use \'-o " |
eec77d9e JS |
1184 | "encryption\' instead!"); |
1185 | return 1; | |
ec36ba14 | 1186 | case '6': |
9d42e15d | 1187 | error_report("option -6 is deprecated, please use \'-o " |
eec77d9e JS |
1188 | "compat6\' instead!"); |
1189 | return 1; | |
efa84d43 KW |
1190 | case 'o': |
1191 | options = optarg; | |
1192 | break; | |
51ef6727 | 1193 | case 's': |
1194 | snapshot_name = optarg; | |
1195 | break; | |
ef80654d WX |
1196 | case 'l': |
1197 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
1198 | sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0); | |
1199 | if (!sn_opts) { | |
1200 | error_report("Failed in parsing snapshot param '%s'", | |
1201 | optarg); | |
1202 | return 1; | |
1203 | } | |
1204 | } else { | |
1205 | snapshot_name = optarg; | |
1206 | } | |
1207 | break; | |
a22f123c KW |
1208 | case 'S': |
1209 | { | |
1210 | int64_t sval; | |
e36b3695 MA |
1211 | char *end; |
1212 | sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B); | |
1213 | if (sval < 0 || *end) { | |
a22f123c KW |
1214 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
1215 | return 1; | |
1216 | } | |
1217 | ||
1218 | min_sparse = sval / BDRV_SECTOR_SIZE; | |
1219 | break; | |
1220 | } | |
6b837bc4 JS |
1221 | case 'p': |
1222 | progress = 1; | |
1223 | break; | |
661a0f71 FS |
1224 | case 't': |
1225 | cache = optarg; | |
1226 | break; | |
f382d43a MR |
1227 | case 'q': |
1228 | quiet = true; | |
1229 | break; | |
b2e10493 AD |
1230 | case 'n': |
1231 | skip_create = 1; | |
1232 | break; | |
ea2384d3 FB |
1233 | } |
1234 | } | |
3b46e624 | 1235 | |
f382d43a MR |
1236 | if (quiet) { |
1237 | progress = 0; | |
1238 | } | |
1239 | ||
926c2d23 | 1240 | bs_n = argc - optind - 1; |
b8fb60da JS |
1241 | if (bs_n < 1) { |
1242 | help(); | |
1243 | } | |
926c2d23 AZ |
1244 | |
1245 | out_filename = argv[argc - 1]; | |
f58c7b35 | 1246 | |
fa170c14 CA |
1247 | /* Initialize before goto out */ |
1248 | qemu_progress_init(progress, 2.0); | |
1249 | ||
c8057f95 | 1250 | if (options && is_help_option(options)) { |
4ac8aacd JS |
1251 | ret = print_block_option_help(out_filename, out_fmt); |
1252 | goto out; | |
1253 | } | |
1254 | ||
c2abccec | 1255 | if (bs_n > 1 && out_baseimg) { |
15654a6d JS |
1256 | error_report("-B makes no sense when concatenating multiple input " |
1257 | "images"); | |
31ca34b8 JS |
1258 | ret = -1; |
1259 | goto out; | |
c2abccec | 1260 | } |
f8111c24 | 1261 | |
6b837bc4 JS |
1262 | qemu_progress_print(0, 100); |
1263 | ||
7267c094 | 1264 | bs = g_malloc0(bs_n * sizeof(BlockDriverState *)); |
926c2d23 AZ |
1265 | |
1266 | total_sectors = 0; | |
1267 | for (bs_i = 0; bs_i < bs_n; bs_i++) { | |
f382d43a MR |
1268 | bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true, |
1269 | quiet); | |
c2abccec | 1270 | if (!bs[bs_i]) { |
15654a6d | 1271 | error_report("Could not open '%s'", argv[optind + bs_i]); |
c2abccec MK |
1272 | ret = -1; |
1273 | goto out; | |
1274 | } | |
926c2d23 AZ |
1275 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
1276 | total_sectors += bs_sectors; | |
1277 | } | |
ea2384d3 | 1278 | |
ef80654d WX |
1279 | if (sn_opts) { |
1280 | ret = bdrv_snapshot_load_tmp(bs[0], | |
1281 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
1282 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
1283 | &local_err); | |
1284 | } else if (snapshot_name != NULL) { | |
51ef6727 | 1285 | if (bs_n > 1) { |
6daf194d | 1286 | error_report("No support for concatenating multiple snapshot"); |
51ef6727 | 1287 | ret = -1; |
1288 | goto out; | |
1289 | } | |
7b4c4781 WX |
1290 | |
1291 | bdrv_snapshot_load_tmp_by_id_or_name(bs[0], snapshot_name, &local_err); | |
ef80654d WX |
1292 | } |
1293 | if (error_is_set(&local_err)) { | |
1294 | error_report("Failed to load snapshot: %s", | |
1295 | error_get_pretty(local_err)); | |
1296 | error_free(local_err); | |
1297 | ret = -1; | |
1298 | goto out; | |
51ef6727 | 1299 | } |
1300 | ||
efa84d43 | 1301 | /* Find driver and parse its options */ |
ea2384d3 | 1302 | drv = bdrv_find_format(out_fmt); |
c2abccec | 1303 | if (!drv) { |
15654a6d | 1304 | error_report("Unknown file format '%s'", out_fmt); |
c2abccec MK |
1305 | ret = -1; |
1306 | goto out; | |
1307 | } | |
efa84d43 | 1308 | |
98289620 | 1309 | proto_drv = bdrv_find_protocol(out_filename, true); |
c2abccec | 1310 | if (!proto_drv) { |
15654a6d | 1311 | error_report("Unknown protocol '%s'", out_filename); |
c2abccec MK |
1312 | ret = -1; |
1313 | goto out; | |
1314 | } | |
b50cbabc MK |
1315 | |
1316 | create_options = append_option_parameters(create_options, | |
1317 | drv->create_options); | |
1318 | create_options = append_option_parameters(create_options, | |
1319 | proto_drv->create_options); | |
db08adf5 | 1320 | |
efa84d43 | 1321 | if (options) { |
b50cbabc | 1322 | param = parse_option_parameters(options, create_options, param); |
efa84d43 | 1323 | if (param == NULL) { |
15654a6d | 1324 | error_report("Invalid options for file format '%s'.", out_fmt); |
c2abccec MK |
1325 | ret = -1; |
1326 | goto out; | |
efa84d43 KW |
1327 | } |
1328 | } else { | |
b50cbabc | 1329 | param = parse_option_parameters("", create_options, param); |
efa84d43 KW |
1330 | } |
1331 | ||
1332 | set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512); | |
eec77d9e | 1333 | ret = add_old_style_options(out_fmt, param, out_baseimg, NULL); |
c2abccec MK |
1334 | if (ret < 0) { |
1335 | goto out; | |
1336 | } | |
efa84d43 | 1337 | |
a18953fb KW |
1338 | /* Get backing file name if -o backing_file was used */ |
1339 | out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); | |
1340 | if (out_baseimg_param) { | |
1341 | out_baseimg = out_baseimg_param->value.s; | |
1342 | } | |
1343 | ||
efa84d43 | 1344 | /* Check if compression is supported */ |
eec77d9e | 1345 | if (compress) { |
efa84d43 KW |
1346 | QEMUOptionParameter *encryption = |
1347 | get_option_parameter(param, BLOCK_OPT_ENCRYPT); | |
41521fa4 KW |
1348 | QEMUOptionParameter *preallocation = |
1349 | get_option_parameter(param, BLOCK_OPT_PREALLOC); | |
efa84d43 KW |
1350 | |
1351 | if (!drv->bdrv_write_compressed) { | |
15654a6d | 1352 | error_report("Compression not supported for this file format"); |
c2abccec MK |
1353 | ret = -1; |
1354 | goto out; | |
efa84d43 KW |
1355 | } |
1356 | ||
1357 | if (encryption && encryption->value.n) { | |
15654a6d JS |
1358 | error_report("Compression and encryption not supported at " |
1359 | "the same time"); | |
c2abccec MK |
1360 | ret = -1; |
1361 | goto out; | |
efa84d43 | 1362 | } |
41521fa4 KW |
1363 | |
1364 | if (preallocation && preallocation->value.s | |
1365 | && strcmp(preallocation->value.s, "off")) | |
1366 | { | |
1367 | error_report("Compression and preallocation not supported at " | |
1368 | "the same time"); | |
1369 | ret = -1; | |
1370 | goto out; | |
1371 | } | |
efa84d43 KW |
1372 | } |
1373 | ||
b2e10493 AD |
1374 | if (!skip_create) { |
1375 | /* Create the new image */ | |
cc84d90f | 1376 | ret = bdrv_create(drv, out_filename, param, &local_err); |
b2e10493 | 1377 | if (ret < 0) { |
cc84d90f HR |
1378 | error_report("%s: error while converting %s: %s", |
1379 | out_filename, out_fmt, error_get_pretty(local_err)); | |
1380 | error_free(local_err); | |
b2e10493 | 1381 | goto out; |
ea2384d3 FB |
1382 | } |
1383 | } | |
3b46e624 | 1384 | |
5a37b60a | 1385 | flags = min_sparse ? (BDRV_O_RDWR | BDRV_O_UNMAP) : BDRV_O_RDWR; |
c3993cdc | 1386 | ret = bdrv_parse_cache_flags(cache, &flags); |
661a0f71 FS |
1387 | if (ret < 0) { |
1388 | error_report("Invalid cache option: %s", cache); | |
1389 | return -1; | |
1390 | } | |
1391 | ||
f382d43a | 1392 | out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet); |
c2abccec MK |
1393 | if (!out_bs) { |
1394 | ret = -1; | |
1395 | goto out; | |
1396 | } | |
ea2384d3 | 1397 | |
926c2d23 AZ |
1398 | bs_i = 0; |
1399 | bs_offset = 0; | |
1400 | bdrv_get_geometry(bs[0], &bs_sectors); | |
bb1c0597 | 1401 | buf = qemu_blockalign(out_bs, IO_BUF_SIZE); |
926c2d23 | 1402 | |
b2e10493 AD |
1403 | if (skip_create) { |
1404 | int64_t output_length = bdrv_getlength(out_bs); | |
1405 | if (output_length < 0) { | |
1406 | error_report("unable to get output image length: %s\n", | |
1407 | strerror(-output_length)); | |
1408 | ret = -1; | |
1409 | goto out; | |
1410 | } else if (output_length < total_sectors << BDRV_SECTOR_BITS) { | |
1411 | error_report("output file is smaller than input file"); | |
1412 | ret = -1; | |
1413 | goto out; | |
1414 | } | |
1415 | } | |
1416 | ||
eec77d9e | 1417 | if (compress) { |
c2abccec MK |
1418 | ret = bdrv_get_info(out_bs, &bdi); |
1419 | if (ret < 0) { | |
15654a6d | 1420 | error_report("could not get block driver info"); |
c2abccec MK |
1421 | goto out; |
1422 | } | |
faea38e7 | 1423 | cluster_size = bdi.cluster_size; |
c2abccec | 1424 | if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) { |
15654a6d | 1425 | error_report("invalid cluster size"); |
c2abccec MK |
1426 | ret = -1; |
1427 | goto out; | |
1428 | } | |
ea2384d3 FB |
1429 | cluster_sectors = cluster_size >> 9; |
1430 | sector_num = 0; | |
6b837bc4 JS |
1431 | |
1432 | nb_sectors = total_sectors; | |
6b837bc4 | 1433 | |
ea2384d3 | 1434 | for(;;) { |
926c2d23 AZ |
1435 | int64_t bs_num; |
1436 | int remainder; | |
1437 | uint8_t *buf2; | |
1438 | ||
ea2384d3 FB |
1439 | nb_sectors = total_sectors - sector_num; |
1440 | if (nb_sectors <= 0) | |
1441 | break; | |
1442 | if (nb_sectors >= cluster_sectors) | |
1443 | n = cluster_sectors; | |
1444 | else | |
1445 | n = nb_sectors; | |
926c2d23 AZ |
1446 | |
1447 | bs_num = sector_num - bs_offset; | |
1448 | assert (bs_num >= 0); | |
1449 | remainder = n; | |
1450 | buf2 = buf; | |
1451 | while (remainder > 0) { | |
1452 | int nlow; | |
1453 | while (bs_num == bs_sectors) { | |
1454 | bs_i++; | |
1455 | assert (bs_i < bs_n); | |
1456 | bs_offset += bs_sectors; | |
1457 | bdrv_get_geometry(bs[bs_i], &bs_sectors); | |
1458 | bs_num = 0; | |
0bfcd599 BS |
1459 | /* printf("changing part: sector_num=%" PRId64 ", " |
1460 | "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64 | |
1461 | "\n", sector_num, bs_i, bs_offset, bs_sectors); */ | |
926c2d23 AZ |
1462 | } |
1463 | assert (bs_num < bs_sectors); | |
1464 | ||
1465 | nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder; | |
1466 | ||
c2abccec MK |
1467 | ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow); |
1468 | if (ret < 0) { | |
3fba9d81 SH |
1469 | error_report("error while reading sector %" PRId64 ": %s", |
1470 | bs_num, strerror(-ret)); | |
c2abccec MK |
1471 | goto out; |
1472 | } | |
926c2d23 AZ |
1473 | |
1474 | buf2 += nlow * 512; | |
1475 | bs_num += nlow; | |
1476 | ||
1477 | remainder -= nlow; | |
1478 | } | |
1479 | assert (remainder == 0); | |
1480 | ||
54f106d5 SH |
1481 | if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) { |
1482 | ret = bdrv_write_compressed(out_bs, sector_num, buf, n); | |
c2abccec | 1483 | if (ret != 0) { |
3fba9d81 SH |
1484 | error_report("error while compressing sector %" PRId64 |
1485 | ": %s", sector_num, strerror(-ret)); | |
c2abccec MK |
1486 | goto out; |
1487 | } | |
ea2384d3 FB |
1488 | } |
1489 | sector_num += n; | |
13c28af8 | 1490 | qemu_progress_print(100.0 * sector_num / total_sectors, 0); |
ea2384d3 | 1491 | } |
faea38e7 FB |
1492 | /* signal EOF to align */ |
1493 | bdrv_write_compressed(out_bs, 0, NULL, 0); | |
ea2384d3 | 1494 | } else { |
11b6699a | 1495 | int has_zero_init = min_sparse ? bdrv_has_zero_init(out_bs) : 0; |
f2feebbd | 1496 | |
5a37b60a PL |
1497 | if (!has_zero_init && bdrv_can_write_zeroes_with_unmap(out_bs)) { |
1498 | ret = bdrv_make_zero(out_bs, BDRV_REQ_MAY_UNMAP); | |
1499 | if (ret < 0) { | |
1500 | goto out; | |
1501 | } | |
1502 | has_zero_init = 1; | |
1503 | } | |
1504 | ||
f58c7b35 | 1505 | sector_num = 0; // total number of sectors converted so far |
6b837bc4 | 1506 | nb_sectors = total_sectors - sector_num; |
6b837bc4 | 1507 | |
ea2384d3 FB |
1508 | for(;;) { |
1509 | nb_sectors = total_sectors - sector_num; | |
b8fb60da | 1510 | if (nb_sectors <= 0) { |
13c28af8 | 1511 | ret = 0; |
ea2384d3 | 1512 | break; |
b8fb60da | 1513 | } |
926c2d23 AZ |
1514 | |
1515 | while (sector_num - bs_offset >= bs_sectors) { | |
1516 | bs_i ++; | |
1517 | assert (bs_i < bs_n); | |
1518 | bs_offset += bs_sectors; | |
1519 | bdrv_get_geometry(bs[bs_i], &bs_sectors); | |
0bfcd599 BS |
1520 | /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, " |
1521 | "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n", | |
926c2d23 AZ |
1522 | sector_num, bs_i, bs_offset, bs_sectors); */ |
1523 | } | |
1524 | ||
13c28af8 PL |
1525 | if ((out_baseimg || has_zero_init) && |
1526 | sector_num >= sector_num_next_status) { | |
1527 | n = nb_sectors > INT_MAX ? INT_MAX : nb_sectors; | |
1528 | ret = bdrv_get_block_status(bs[bs_i], sector_num - bs_offset, | |
1529 | n, &n1); | |
e4a86f88 | 1530 | if (ret < 0) { |
13c28af8 PL |
1531 | error_report("error while reading block status of sector %" |
1532 | PRId64 ": %s", sector_num - bs_offset, | |
1533 | strerror(-ret)); | |
e4a86f88 PB |
1534 | goto out; |
1535 | } | |
13c28af8 PL |
1536 | /* If the output image is zero initialized, we are not working |
1537 | * on a shared base and the input is zero we can skip the next | |
1538 | * n1 sectors */ | |
1539 | if (has_zero_init && !out_baseimg && (ret & BDRV_BLOCK_ZERO)) { | |
e4a86f88 PB |
1540 | sector_num += n1; |
1541 | continue; | |
93c65b47 | 1542 | } |
13c28af8 PL |
1543 | /* If the output image is being created as a copy on write |
1544 | * image, assume that sectors which are unallocated in the | |
1545 | * input image are present in both the output's and input's | |
1546 | * base images (no need to copy them). */ | |
1547 | if (out_baseimg) { | |
1548 | if (!(ret & BDRV_BLOCK_DATA)) { | |
1549 | sector_num += n1; | |
1550 | continue; | |
1551 | } | |
1552 | /* The next 'n1' sectors are allocated in the input image. | |
1553 | * Copy only those as they may be followed by unallocated | |
1554 | * sectors. */ | |
1555 | nb_sectors = n1; | |
1556 | } | |
1557 | /* avoid redundant callouts to get_block_status */ | |
1558 | sector_num_next_status = sector_num + n1; | |
f58c7b35 TS |
1559 | } |
1560 | ||
13c28af8 PL |
1561 | n = MIN(nb_sectors, IO_BUF_SIZE / 512); |
1562 | n = MIN(n, bs_sectors - (sector_num - bs_offset)); | |
1563 | n1 = n; | |
1564 | ||
c2abccec MK |
1565 | ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n); |
1566 | if (ret < 0) { | |
3fba9d81 SH |
1567 | error_report("error while reading sector %" PRId64 ": %s", |
1568 | sector_num - bs_offset, strerror(-ret)); | |
c2abccec MK |
1569 | goto out; |
1570 | } | |
ea2384d3 FB |
1571 | /* NOTE: at the same time we convert, we do not write zero |
1572 | sectors to have a chance to compress the image. Ideally, we | |
1573 | should add a specific call to have the info to go faster */ | |
1574 | buf1 = buf; | |
1575 | while (n > 0) { | |
11212d8f | 1576 | if (!has_zero_init || |
a22f123c | 1577 | is_allocated_sectors_min(buf1, n, &n1, min_sparse)) { |
c2abccec MK |
1578 | ret = bdrv_write(out_bs, sector_num, buf1, n1); |
1579 | if (ret < 0) { | |
3fba9d81 SH |
1580 | error_report("error while writing sector %" PRId64 |
1581 | ": %s", sector_num, strerror(-ret)); | |
c2abccec MK |
1582 | goto out; |
1583 | } | |
ea2384d3 FB |
1584 | } |
1585 | sector_num += n1; | |
1586 | n -= n1; | |
1587 | buf1 += n1 * 512; | |
1588 | } | |
13c28af8 | 1589 | qemu_progress_print(100.0 * sector_num / total_sectors, 0); |
ea2384d3 FB |
1590 | } |
1591 | } | |
c2abccec | 1592 | out: |
13c28af8 PL |
1593 | if (!ret) { |
1594 | qemu_progress_print(100, 0); | |
1595 | } | |
6b837bc4 | 1596 | qemu_progress_end(); |
c2abccec MK |
1597 | free_option_parameters(create_options); |
1598 | free_option_parameters(param); | |
bb1c0597 | 1599 | qemu_vfree(buf); |
ef80654d WX |
1600 | if (sn_opts) { |
1601 | qemu_opts_del(sn_opts); | |
1602 | } | |
c2abccec | 1603 | if (out_bs) { |
4f6fd349 | 1604 | bdrv_unref(out_bs); |
c2abccec | 1605 | } |
31ca34b8 JS |
1606 | if (bs) { |
1607 | for (bs_i = 0; bs_i < bs_n; bs_i++) { | |
1608 | if (bs[bs_i]) { | |
4f6fd349 | 1609 | bdrv_unref(bs[bs_i]); |
31ca34b8 | 1610 | } |
c2abccec | 1611 | } |
7267c094 | 1612 | g_free(bs); |
c2abccec | 1613 | } |
c2abccec MK |
1614 | if (ret) { |
1615 | return 1; | |
1616 | } | |
ea2384d3 FB |
1617 | return 0; |
1618 | } | |
1619 | ||
57d1a2b6 | 1620 | |
faea38e7 FB |
1621 | static void dump_snapshots(BlockDriverState *bs) |
1622 | { | |
1623 | QEMUSnapshotInfo *sn_tab, *sn; | |
1624 | int nb_sns, i; | |
faea38e7 FB |
1625 | |
1626 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); | |
1627 | if (nb_sns <= 0) | |
1628 | return; | |
1629 | printf("Snapshot list:\n"); | |
5b917044 WX |
1630 | bdrv_snapshot_dump(fprintf, stdout, NULL); |
1631 | printf("\n"); | |
faea38e7 FB |
1632 | for(i = 0; i < nb_sns; i++) { |
1633 | sn = &sn_tab[i]; | |
5b917044 WX |
1634 | bdrv_snapshot_dump(fprintf, stdout, sn); |
1635 | printf("\n"); | |
faea38e7 | 1636 | } |
7267c094 | 1637 | g_free(sn_tab); |
faea38e7 FB |
1638 | } |
1639 | ||
9699bf0d SH |
1640 | static void dump_json_image_info_list(ImageInfoList *list) |
1641 | { | |
1642 | Error *errp = NULL; | |
1643 | QString *str; | |
1644 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
1645 | QObject *obj; | |
1646 | visit_type_ImageInfoList(qmp_output_get_visitor(ov), | |
1647 | &list, NULL, &errp); | |
1648 | obj = qmp_output_get_qobject(ov); | |
1649 | str = qobject_to_json_pretty(obj); | |
1650 | assert(str != NULL); | |
1651 | printf("%s\n", qstring_get_str(str)); | |
1652 | qobject_decref(obj); | |
1653 | qmp_output_visitor_cleanup(ov); | |
1654 | QDECREF(str); | |
1655 | } | |
1656 | ||
c054b3fd BC |
1657 | static void dump_json_image_info(ImageInfo *info) |
1658 | { | |
1659 | Error *errp = NULL; | |
1660 | QString *str; | |
1661 | QmpOutputVisitor *ov = qmp_output_visitor_new(); | |
1662 | QObject *obj; | |
1663 | visit_type_ImageInfo(qmp_output_get_visitor(ov), | |
1664 | &info, NULL, &errp); | |
1665 | obj = qmp_output_get_qobject(ov); | |
1666 | str = qobject_to_json_pretty(obj); | |
1667 | assert(str != NULL); | |
1668 | printf("%s\n", qstring_get_str(str)); | |
1669 | qobject_decref(obj); | |
1670 | qmp_output_visitor_cleanup(ov); | |
1671 | QDECREF(str); | |
1672 | } | |
1673 | ||
9699bf0d SH |
1674 | static void dump_human_image_info_list(ImageInfoList *list) |
1675 | { | |
1676 | ImageInfoList *elem; | |
1677 | bool delim = false; | |
1678 | ||
1679 | for (elem = list; elem; elem = elem->next) { | |
1680 | if (delim) { | |
1681 | printf("\n"); | |
1682 | } | |
1683 | delim = true; | |
1684 | ||
5b917044 | 1685 | bdrv_image_info_dump(fprintf, stdout, elem->value); |
9699bf0d SH |
1686 | } |
1687 | } | |
1688 | ||
1689 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) | |
1690 | { | |
1691 | return strcmp(a, b) == 0; | |
1692 | } | |
1693 | ||
1694 | /** | |
1695 | * Open an image file chain and return an ImageInfoList | |
1696 | * | |
1697 | * @filename: topmost image filename | |
1698 | * @fmt: topmost image format (may be NULL to autodetect) | |
1699 | * @chain: true - enumerate entire backing file chain | |
1700 | * false - only topmost image file | |
1701 | * | |
1702 | * Returns a list of ImageInfo objects or NULL if there was an error opening an | |
1703 | * image file. If there was an error a message will have been printed to | |
1704 | * stderr. | |
1705 | */ | |
1706 | static ImageInfoList *collect_image_info_list(const char *filename, | |
1707 | const char *fmt, | |
1708 | bool chain) | |
1709 | { | |
1710 | ImageInfoList *head = NULL; | |
1711 | ImageInfoList **last = &head; | |
1712 | GHashTable *filenames; | |
43526ec8 | 1713 | Error *err = NULL; |
9699bf0d SH |
1714 | |
1715 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); | |
1716 | ||
1717 | while (filename) { | |
1718 | BlockDriverState *bs; | |
1719 | ImageInfo *info; | |
1720 | ImageInfoList *elem; | |
1721 | ||
1722 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { | |
1723 | error_report("Backing file '%s' creates an infinite loop.", | |
1724 | filename); | |
1725 | goto err; | |
1726 | } | |
1727 | g_hash_table_insert(filenames, (gpointer)filename, NULL); | |
1728 | ||
1729 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING, | |
f382d43a | 1730 | false, false); |
9699bf0d SH |
1731 | if (!bs) { |
1732 | goto err; | |
1733 | } | |
1734 | ||
43526ec8 WX |
1735 | bdrv_query_image_info(bs, &info, &err); |
1736 | if (error_is_set(&err)) { | |
1737 | error_report("%s", error_get_pretty(err)); | |
1738 | error_free(err); | |
1739 | goto err; | |
fb0ed453 | 1740 | } |
9699bf0d SH |
1741 | |
1742 | elem = g_new0(ImageInfoList, 1); | |
1743 | elem->value = info; | |
1744 | *last = elem; | |
1745 | last = &elem->next; | |
1746 | ||
4f6fd349 | 1747 | bdrv_unref(bs); |
9699bf0d SH |
1748 | |
1749 | filename = fmt = NULL; | |
1750 | if (chain) { | |
1751 | if (info->has_full_backing_filename) { | |
1752 | filename = info->full_backing_filename; | |
1753 | } else if (info->has_backing_filename) { | |
1754 | filename = info->backing_filename; | |
1755 | } | |
1756 | if (info->has_backing_filename_format) { | |
1757 | fmt = info->backing_filename_format; | |
1758 | } | |
1759 | } | |
1760 | } | |
1761 | g_hash_table_destroy(filenames); | |
1762 | return head; | |
1763 | ||
1764 | err: | |
1765 | qapi_free_ImageInfoList(head); | |
1766 | g_hash_table_destroy(filenames); | |
1767 | return NULL; | |
1768 | } | |
1769 | ||
c054b3fd BC |
1770 | static int img_info(int argc, char **argv) |
1771 | { | |
1772 | int c; | |
1773 | OutputFormat output_format = OFORMAT_HUMAN; | |
9699bf0d | 1774 | bool chain = false; |
c054b3fd | 1775 | const char *filename, *fmt, *output; |
9699bf0d | 1776 | ImageInfoList *list; |
c054b3fd | 1777 | |
ea2384d3 | 1778 | fmt = NULL; |
c054b3fd | 1779 | output = NULL; |
ea2384d3 | 1780 | for(;;) { |
c054b3fd BC |
1781 | int option_index = 0; |
1782 | static const struct option long_options[] = { | |
1783 | {"help", no_argument, 0, 'h'}, | |
1784 | {"format", required_argument, 0, 'f'}, | |
1785 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
9699bf0d | 1786 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
c054b3fd BC |
1787 | {0, 0, 0, 0} |
1788 | }; | |
1789 | c = getopt_long(argc, argv, "f:h", | |
1790 | long_options, &option_index); | |
b8fb60da | 1791 | if (c == -1) { |
ea2384d3 | 1792 | break; |
b8fb60da | 1793 | } |
ea2384d3 | 1794 | switch(c) { |
ef87394c | 1795 | case '?': |
ea2384d3 FB |
1796 | case 'h': |
1797 | help(); | |
1798 | break; | |
1799 | case 'f': | |
1800 | fmt = optarg; | |
1801 | break; | |
c054b3fd BC |
1802 | case OPTION_OUTPUT: |
1803 | output = optarg; | |
1804 | break; | |
9699bf0d SH |
1805 | case OPTION_BACKING_CHAIN: |
1806 | chain = true; | |
1807 | break; | |
ea2384d3 FB |
1808 | } |
1809 | } | |
fc11eb26 | 1810 | if (optind != argc - 1) { |
ea2384d3 | 1811 | help(); |
b8fb60da | 1812 | } |
ea2384d3 FB |
1813 | filename = argv[optind++]; |
1814 | ||
c054b3fd BC |
1815 | if (output && !strcmp(output, "json")) { |
1816 | output_format = OFORMAT_JSON; | |
1817 | } else if (output && !strcmp(output, "human")) { | |
1818 | output_format = OFORMAT_HUMAN; | |
1819 | } else if (output) { | |
1820 | error_report("--output must be used with human or json as argument."); | |
c2abccec MK |
1821 | return 1; |
1822 | } | |
c054b3fd | 1823 | |
9699bf0d SH |
1824 | list = collect_image_info_list(filename, fmt, chain); |
1825 | if (!list) { | |
c2abccec | 1826 | return 1; |
faea38e7 | 1827 | } |
c054b3fd | 1828 | |
c054b3fd BC |
1829 | switch (output_format) { |
1830 | case OFORMAT_HUMAN: | |
9699bf0d | 1831 | dump_human_image_info_list(list); |
c054b3fd BC |
1832 | break; |
1833 | case OFORMAT_JSON: | |
9699bf0d SH |
1834 | if (chain) { |
1835 | dump_json_image_info_list(list); | |
1836 | } else { | |
1837 | dump_json_image_info(list->value); | |
1838 | } | |
c054b3fd | 1839 | break; |
faea38e7 | 1840 | } |
c054b3fd | 1841 | |
9699bf0d | 1842 | qapi_free_ImageInfoList(list); |
ea2384d3 FB |
1843 | return 0; |
1844 | } | |
1845 | ||
4c93a13b PB |
1846 | |
1847 | typedef struct MapEntry { | |
1848 | int flags; | |
1849 | int depth; | |
1850 | int64_t start; | |
1851 | int64_t length; | |
1852 | int64_t offset; | |
1853 | BlockDriverState *bs; | |
1854 | } MapEntry; | |
1855 | ||
1856 | static void dump_map_entry(OutputFormat output_format, MapEntry *e, | |
1857 | MapEntry *next) | |
1858 | { | |
1859 | switch (output_format) { | |
1860 | case OFORMAT_HUMAN: | |
1861 | if ((e->flags & BDRV_BLOCK_DATA) && | |
1862 | !(e->flags & BDRV_BLOCK_OFFSET_VALID)) { | |
1863 | error_report("File contains external, encrypted or compressed clusters."); | |
1864 | exit(1); | |
1865 | } | |
1866 | if ((e->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) == BDRV_BLOCK_DATA) { | |
1867 | printf("%#-16"PRIx64"%#-16"PRIx64"%#-16"PRIx64"%s\n", | |
1868 | e->start, e->length, e->offset, e->bs->filename); | |
1869 | } | |
1870 | /* This format ignores the distinction between 0, ZERO and ZERO|DATA. | |
1871 | * Modify the flags here to allow more coalescing. | |
1872 | */ | |
1873 | if (next && | |
1874 | (next->flags & (BDRV_BLOCK_DATA|BDRV_BLOCK_ZERO)) != BDRV_BLOCK_DATA) { | |
1875 | next->flags &= ~BDRV_BLOCK_DATA; | |
1876 | next->flags |= BDRV_BLOCK_ZERO; | |
1877 | } | |
1878 | break; | |
1879 | case OFORMAT_JSON: | |
1880 | printf("%s{ \"start\": %"PRId64", \"length\": %"PRId64", \"depth\": %d," | |
1881 | " \"zero\": %s, \"data\": %s", | |
1882 | (e->start == 0 ? "[" : ",\n"), | |
1883 | e->start, e->length, e->depth, | |
1884 | (e->flags & BDRV_BLOCK_ZERO) ? "true" : "false", | |
1885 | (e->flags & BDRV_BLOCK_DATA) ? "true" : "false"); | |
1886 | if (e->flags & BDRV_BLOCK_OFFSET_VALID) { | |
c745bfb4 | 1887 | printf(", \"offset\": %"PRId64"", e->offset); |
4c93a13b PB |
1888 | } |
1889 | putchar('}'); | |
1890 | ||
1891 | if (!next) { | |
1892 | printf("]\n"); | |
1893 | } | |
1894 | break; | |
1895 | } | |
1896 | } | |
1897 | ||
1898 | static int get_block_status(BlockDriverState *bs, int64_t sector_num, | |
1899 | int nb_sectors, MapEntry *e) | |
1900 | { | |
1901 | int64_t ret; | |
1902 | int depth; | |
1903 | ||
1904 | /* As an optimization, we could cache the current range of unallocated | |
1905 | * clusters in each file of the chain, and avoid querying the same | |
1906 | * range repeatedly. | |
1907 | */ | |
1908 | ||
1909 | depth = 0; | |
1910 | for (;;) { | |
1911 | ret = bdrv_get_block_status(bs, sector_num, nb_sectors, &nb_sectors); | |
1912 | if (ret < 0) { | |
1913 | return ret; | |
1914 | } | |
1915 | assert(nb_sectors); | |
1916 | if (ret & (BDRV_BLOCK_ZERO|BDRV_BLOCK_DATA)) { | |
1917 | break; | |
1918 | } | |
1919 | bs = bs->backing_hd; | |
1920 | if (bs == NULL) { | |
1921 | ret = 0; | |
1922 | break; | |
1923 | } | |
1924 | ||
1925 | depth++; | |
1926 | } | |
1927 | ||
1928 | e->start = sector_num * BDRV_SECTOR_SIZE; | |
1929 | e->length = nb_sectors * BDRV_SECTOR_SIZE; | |
1930 | e->flags = ret & ~BDRV_BLOCK_OFFSET_MASK; | |
1931 | e->offset = ret & BDRV_BLOCK_OFFSET_MASK; | |
1932 | e->depth = depth; | |
1933 | e->bs = bs; | |
1934 | return 0; | |
1935 | } | |
1936 | ||
1937 | static int img_map(int argc, char **argv) | |
1938 | { | |
1939 | int c; | |
1940 | OutputFormat output_format = OFORMAT_HUMAN; | |
1941 | BlockDriverState *bs; | |
1942 | const char *filename, *fmt, *output; | |
1943 | int64_t length; | |
1944 | MapEntry curr = { .length = 0 }, next; | |
1945 | int ret = 0; | |
1946 | ||
1947 | fmt = NULL; | |
1948 | output = NULL; | |
1949 | for (;;) { | |
1950 | int option_index = 0; | |
1951 | static const struct option long_options[] = { | |
1952 | {"help", no_argument, 0, 'h'}, | |
1953 | {"format", required_argument, 0, 'f'}, | |
1954 | {"output", required_argument, 0, OPTION_OUTPUT}, | |
1955 | {0, 0, 0, 0} | |
1956 | }; | |
1957 | c = getopt_long(argc, argv, "f:h", | |
1958 | long_options, &option_index); | |
1959 | if (c == -1) { | |
1960 | break; | |
1961 | } | |
1962 | switch (c) { | |
1963 | case '?': | |
1964 | case 'h': | |
1965 | help(); | |
1966 | break; | |
1967 | case 'f': | |
1968 | fmt = optarg; | |
1969 | break; | |
1970 | case OPTION_OUTPUT: | |
1971 | output = optarg; | |
1972 | break; | |
1973 | } | |
1974 | } | |
1975 | if (optind >= argc) { | |
1976 | help(); | |
1977 | } | |
1978 | filename = argv[optind++]; | |
1979 | ||
1980 | if (output && !strcmp(output, "json")) { | |
1981 | output_format = OFORMAT_JSON; | |
1982 | } else if (output && !strcmp(output, "human")) { | |
1983 | output_format = OFORMAT_HUMAN; | |
1984 | } else if (output) { | |
1985 | error_report("--output must be used with human or json as argument."); | |
1986 | return 1; | |
1987 | } | |
1988 | ||
1989 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS, true, false); | |
1990 | if (!bs) { | |
1991 | return 1; | |
1992 | } | |
1993 | ||
1994 | if (output_format == OFORMAT_HUMAN) { | |
1995 | printf("%-16s%-16s%-16s%s\n", "Offset", "Length", "Mapped to", "File"); | |
1996 | } | |
1997 | ||
1998 | length = bdrv_getlength(bs); | |
1999 | while (curr.start + curr.length < length) { | |
2000 | int64_t nsectors_left; | |
2001 | int64_t sector_num; | |
2002 | int n; | |
2003 | ||
2004 | sector_num = (curr.start + curr.length) >> BDRV_SECTOR_BITS; | |
2005 | ||
2006 | /* Probe up to 1 GiB at a time. */ | |
2007 | nsectors_left = DIV_ROUND_UP(length, BDRV_SECTOR_SIZE) - sector_num; | |
2008 | n = MIN(1 << (30 - BDRV_SECTOR_BITS), nsectors_left); | |
2009 | ret = get_block_status(bs, sector_num, n, &next); | |
2010 | ||
2011 | if (ret < 0) { | |
2012 | error_report("Could not read file metadata: %s", strerror(-ret)); | |
2013 | goto out; | |
2014 | } | |
2015 | ||
2016 | if (curr.length != 0 && curr.flags == next.flags && | |
2017 | curr.depth == next.depth && | |
2018 | ((curr.flags & BDRV_BLOCK_OFFSET_VALID) == 0 || | |
2019 | curr.offset + curr.length == next.offset)) { | |
2020 | curr.length += next.length; | |
2021 | continue; | |
2022 | } | |
2023 | ||
2024 | if (curr.length > 0) { | |
2025 | dump_map_entry(output_format, &curr, &next); | |
2026 | } | |
2027 | curr = next; | |
2028 | } | |
2029 | ||
2030 | dump_map_entry(output_format, &curr, NULL); | |
2031 | ||
2032 | out: | |
2033 | bdrv_unref(bs); | |
2034 | return ret < 0; | |
2035 | } | |
2036 | ||
f7b4a940 AL |
2037 | #define SNAPSHOT_LIST 1 |
2038 | #define SNAPSHOT_CREATE 2 | |
2039 | #define SNAPSHOT_APPLY 3 | |
2040 | #define SNAPSHOT_DELETE 4 | |
2041 | ||
153859be | 2042 | static int img_snapshot(int argc, char **argv) |
f7b4a940 AL |
2043 | { |
2044 | BlockDriverState *bs; | |
2045 | QEMUSnapshotInfo sn; | |
2046 | char *filename, *snapshot_name = NULL; | |
c2abccec | 2047 | int c, ret = 0, bdrv_oflags; |
f7b4a940 AL |
2048 | int action = 0; |
2049 | qemu_timeval tv; | |
f382d43a | 2050 | bool quiet = false; |
a89d89d3 | 2051 | Error *err = NULL; |
f7b4a940 | 2052 | |
710da702 | 2053 | bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR; |
f7b4a940 AL |
2054 | /* Parse commandline parameters */ |
2055 | for(;;) { | |
f382d43a | 2056 | c = getopt(argc, argv, "la:c:d:hq"); |
b8fb60da | 2057 | if (c == -1) { |
f7b4a940 | 2058 | break; |
b8fb60da | 2059 | } |
f7b4a940 | 2060 | switch(c) { |
ef87394c | 2061 | case '?': |
f7b4a940 AL |
2062 | case 'h': |
2063 | help(); | |
153859be | 2064 | return 0; |
f7b4a940 AL |
2065 | case 'l': |
2066 | if (action) { | |
2067 | help(); | |
153859be | 2068 | return 0; |
f7b4a940 AL |
2069 | } |
2070 | action = SNAPSHOT_LIST; | |
f5edb014 | 2071 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
f7b4a940 AL |
2072 | break; |
2073 | case 'a': | |
2074 | if (action) { | |
2075 | help(); | |
153859be | 2076 | return 0; |
f7b4a940 AL |
2077 | } |
2078 | action = SNAPSHOT_APPLY; | |
2079 | snapshot_name = optarg; | |
2080 | break; | |
2081 | case 'c': | |
2082 | if (action) { | |
2083 | help(); | |
153859be | 2084 | return 0; |
f7b4a940 AL |
2085 | } |
2086 | action = SNAPSHOT_CREATE; | |
2087 | snapshot_name = optarg; | |
2088 | break; | |
2089 | case 'd': | |
2090 | if (action) { | |
2091 | help(); | |
153859be | 2092 | return 0; |
f7b4a940 AL |
2093 | } |
2094 | action = SNAPSHOT_DELETE; | |
2095 | snapshot_name = optarg; | |
2096 | break; | |
f382d43a MR |
2097 | case 'q': |
2098 | quiet = true; | |
2099 | break; | |
f7b4a940 AL |
2100 | } |
2101 | } | |
2102 | ||
fc11eb26 | 2103 | if (optind != argc - 1) { |
f7b4a940 | 2104 | help(); |
b8fb60da | 2105 | } |
f7b4a940 AL |
2106 | filename = argv[optind++]; |
2107 | ||
2108 | /* Open the image */ | |
f382d43a | 2109 | bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet); |
c2abccec MK |
2110 | if (!bs) { |
2111 | return 1; | |
2112 | } | |
f7b4a940 AL |
2113 | |
2114 | /* Perform the requested action */ | |
2115 | switch(action) { | |
2116 | case SNAPSHOT_LIST: | |
2117 | dump_snapshots(bs); | |
2118 | break; | |
2119 | ||
2120 | case SNAPSHOT_CREATE: | |
2121 | memset(&sn, 0, sizeof(sn)); | |
2122 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); | |
2123 | ||
2124 | qemu_gettimeofday(&tv); | |
2125 | sn.date_sec = tv.tv_sec; | |
2126 | sn.date_nsec = tv.tv_usec * 1000; | |
2127 | ||
2128 | ret = bdrv_snapshot_create(bs, &sn); | |
b8fb60da | 2129 | if (ret) { |
15654a6d | 2130 | error_report("Could not create snapshot '%s': %d (%s)", |
f7b4a940 | 2131 | snapshot_name, ret, strerror(-ret)); |
b8fb60da | 2132 | } |
f7b4a940 AL |
2133 | break; |
2134 | ||
2135 | case SNAPSHOT_APPLY: | |
2136 | ret = bdrv_snapshot_goto(bs, snapshot_name); | |
b8fb60da | 2137 | if (ret) { |
15654a6d | 2138 | error_report("Could not apply snapshot '%s': %d (%s)", |
f7b4a940 | 2139 | snapshot_name, ret, strerror(-ret)); |
b8fb60da | 2140 | } |
f7b4a940 AL |
2141 | break; |
2142 | ||
2143 | case SNAPSHOT_DELETE: | |
a89d89d3 WX |
2144 | bdrv_snapshot_delete_by_id_or_name(bs, snapshot_name, &err); |
2145 | if (error_is_set(&err)) { | |
2146 | error_report("Could not delete snapshot '%s': (%s)", | |
2147 | snapshot_name, error_get_pretty(err)); | |
2148 | error_free(err); | |
2149 | ret = 1; | |
b8fb60da | 2150 | } |
f7b4a940 AL |
2151 | break; |
2152 | } | |
2153 | ||
2154 | /* Cleanup */ | |
4f6fd349 | 2155 | bdrv_unref(bs); |
c2abccec MK |
2156 | if (ret) { |
2157 | return 1; | |
2158 | } | |
153859be | 2159 | return 0; |
f7b4a940 AL |
2160 | } |
2161 | ||
3e85c6fd KW |
2162 | static int img_rebase(int argc, char **argv) |
2163 | { | |
c2abccec | 2164 | BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL; |
f163d073 | 2165 | BlockDriver *old_backing_drv, *new_backing_drv; |
3e85c6fd | 2166 | char *filename; |
661a0f71 | 2167 | const char *fmt, *cache, *out_basefmt, *out_baseimg; |
3e85c6fd KW |
2168 | int c, flags, ret; |
2169 | int unsafe = 0; | |
6b837bc4 | 2170 | int progress = 0; |
f382d43a | 2171 | bool quiet = false; |
34b5d2c6 | 2172 | Error *local_err = NULL; |
3e85c6fd KW |
2173 | |
2174 | /* Parse commandline parameters */ | |
e53dbee0 | 2175 | fmt = NULL; |
661a0f71 | 2176 | cache = BDRV_DEFAULT_CACHE; |
3e85c6fd KW |
2177 | out_baseimg = NULL; |
2178 | out_basefmt = NULL; | |
3e85c6fd | 2179 | for(;;) { |
f382d43a | 2180 | c = getopt(argc, argv, "uhf:F:b:pt:q"); |
b8fb60da | 2181 | if (c == -1) { |
3e85c6fd | 2182 | break; |
b8fb60da | 2183 | } |
3e85c6fd | 2184 | switch(c) { |
ef87394c | 2185 | case '?': |
3e85c6fd KW |
2186 | case 'h': |
2187 | help(); | |
2188 | return 0; | |
e53dbee0 KW |
2189 | case 'f': |
2190 | fmt = optarg; | |
2191 | break; | |
3e85c6fd KW |
2192 | case 'F': |
2193 | out_basefmt = optarg; | |
2194 | break; | |
2195 | case 'b': | |
2196 | out_baseimg = optarg; | |
2197 | break; | |
2198 | case 'u': | |
2199 | unsafe = 1; | |
2200 | break; | |
6b837bc4 JS |
2201 | case 'p': |
2202 | progress = 1; | |
2203 | break; | |
661a0f71 FS |
2204 | case 't': |
2205 | cache = optarg; | |
2206 | break; | |
f382d43a MR |
2207 | case 'q': |
2208 | quiet = true; | |
2209 | break; | |
3e85c6fd KW |
2210 | } |
2211 | } | |
2212 | ||
f382d43a MR |
2213 | if (quiet) { |
2214 | progress = 0; | |
2215 | } | |
2216 | ||
fc11eb26 | 2217 | if ((optind != argc - 1) || (!unsafe && !out_baseimg)) { |
3e85c6fd | 2218 | help(); |
b8fb60da | 2219 | } |
3e85c6fd KW |
2220 | filename = argv[optind++]; |
2221 | ||
6b837bc4 JS |
2222 | qemu_progress_init(progress, 2.0); |
2223 | qemu_progress_print(0, 100); | |
2224 | ||
661a0f71 | 2225 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
c3993cdc | 2226 | ret = bdrv_parse_cache_flags(cache, &flags); |
661a0f71 FS |
2227 | if (ret < 0) { |
2228 | error_report("Invalid cache option: %s", cache); | |
2229 | return -1; | |
2230 | } | |
2231 | ||
3e85c6fd KW |
2232 | /* |
2233 | * Open the images. | |
2234 | * | |
2235 | * Ignore the old backing file for unsafe rebase in case we want to correct | |
2236 | * the reference to a renamed or moved backing file. | |
2237 | */ | |
f382d43a | 2238 | bs = bdrv_new_open(filename, fmt, flags, true, quiet); |
c2abccec MK |
2239 | if (!bs) { |
2240 | return 1; | |
2241 | } | |
3e85c6fd KW |
2242 | |
2243 | /* Find the right drivers for the backing files */ | |
2244 | old_backing_drv = NULL; | |
2245 | new_backing_drv = NULL; | |
2246 | ||
2247 | if (!unsafe && bs->backing_format[0] != '\0') { | |
2248 | old_backing_drv = bdrv_find_format(bs->backing_format); | |
2249 | if (old_backing_drv == NULL) { | |
15654a6d | 2250 | error_report("Invalid format name: '%s'", bs->backing_format); |
c2abccec MK |
2251 | ret = -1; |
2252 | goto out; | |
3e85c6fd KW |
2253 | } |
2254 | } | |
2255 | ||
2256 | if (out_basefmt != NULL) { | |
2257 | new_backing_drv = bdrv_find_format(out_basefmt); | |
2258 | if (new_backing_drv == NULL) { | |
15654a6d | 2259 | error_report("Invalid format name: '%s'", out_basefmt); |
c2abccec MK |
2260 | ret = -1; |
2261 | goto out; | |
3e85c6fd KW |
2262 | } |
2263 | } | |
2264 | ||
2265 | /* For safe rebasing we need to compare old and new backing file */ | |
2266 | if (unsafe) { | |
2267 | /* Make the compiler happy */ | |
2268 | bs_old_backing = NULL; | |
2269 | bs_new_backing = NULL; | |
2270 | } else { | |
2271 | char backing_name[1024]; | |
2272 | ||
2273 | bs_old_backing = bdrv_new("old_backing"); | |
2274 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); | |
de9c0cec | 2275 | ret = bdrv_open(bs_old_backing, backing_name, NULL, BDRV_O_FLAGS, |
34b5d2c6 | 2276 | old_backing_drv, &local_err); |
c2abccec | 2277 | if (ret) { |
34b5d2c6 HR |
2278 | error_report("Could not open old backing file '%s': %s", |
2279 | backing_name, error_get_pretty(local_err)); | |
2280 | error_free(local_err); | |
c2abccec | 2281 | goto out; |
3e85c6fd | 2282 | } |
a616673d AB |
2283 | if (out_baseimg[0]) { |
2284 | bs_new_backing = bdrv_new("new_backing"); | |
de9c0cec | 2285 | ret = bdrv_open(bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS, |
34b5d2c6 | 2286 | new_backing_drv, &local_err); |
a616673d | 2287 | if (ret) { |
34b5d2c6 HR |
2288 | error_report("Could not open new backing file '%s': %s", |
2289 | out_baseimg, error_get_pretty(local_err)); | |
2290 | error_free(local_err); | |
a616673d AB |
2291 | goto out; |
2292 | } | |
3e85c6fd KW |
2293 | } |
2294 | } | |
2295 | ||
2296 | /* | |
2297 | * Check each unallocated cluster in the COW file. If it is unallocated, | |
2298 | * accesses go to the backing file. We must therefore compare this cluster | |
2299 | * in the old and new backing file, and if they differ we need to copy it | |
2300 | * from the old backing file into the COW file. | |
2301 | * | |
2302 | * If qemu-img crashes during this step, no harm is done. The content of | |
2303 | * the image is the same as the original one at any time. | |
2304 | */ | |
2305 | if (!unsafe) { | |
2306 | uint64_t num_sectors; | |
87a1b3e3 | 2307 | uint64_t old_backing_num_sectors; |
a616673d | 2308 | uint64_t new_backing_num_sectors = 0; |
3e85c6fd | 2309 | uint64_t sector; |
cc60e327 | 2310 | int n; |
d6771bfa T |
2311 | uint8_t * buf_old; |
2312 | uint8_t * buf_new; | |
1f710495 | 2313 | float local_progress = 0; |
d6771bfa | 2314 | |
bb1c0597 KW |
2315 | buf_old = qemu_blockalign(bs, IO_BUF_SIZE); |
2316 | buf_new = qemu_blockalign(bs, IO_BUF_SIZE); | |
3e85c6fd KW |
2317 | |
2318 | bdrv_get_geometry(bs, &num_sectors); | |
87a1b3e3 | 2319 | bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors); |
a616673d AB |
2320 | if (bs_new_backing) { |
2321 | bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors); | |
2322 | } | |
3e85c6fd | 2323 | |
1f710495 KW |
2324 | if (num_sectors != 0) { |
2325 | local_progress = (float)100 / | |
2326 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); | |
2327 | } | |
2328 | ||
3e85c6fd KW |
2329 | for (sector = 0; sector < num_sectors; sector += n) { |
2330 | ||
2331 | /* How many sectors can we handle with the next read? */ | |
2332 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { | |
2333 | n = (IO_BUF_SIZE / 512); | |
2334 | } else { | |
2335 | n = num_sectors - sector; | |
2336 | } | |
2337 | ||
2338 | /* If the cluster is allocated, we don't need to take action */ | |
cc60e327 | 2339 | ret = bdrv_is_allocated(bs, sector, n, &n); |
d663640c PB |
2340 | if (ret < 0) { |
2341 | error_report("error while reading image metadata: %s", | |
2342 | strerror(-ret)); | |
2343 | goto out; | |
2344 | } | |
cc60e327 | 2345 | if (ret) { |
3e85c6fd KW |
2346 | continue; |
2347 | } | |
2348 | ||
87a1b3e3 KW |
2349 | /* |
2350 | * Read old and new backing file and take into consideration that | |
2351 | * backing files may be smaller than the COW image. | |
2352 | */ | |
2353 | if (sector >= old_backing_num_sectors) { | |
2354 | memset(buf_old, 0, n * BDRV_SECTOR_SIZE); | |
2355 | } else { | |
2356 | if (sector + n > old_backing_num_sectors) { | |
2357 | n = old_backing_num_sectors - sector; | |
2358 | } | |
2359 | ||
2360 | ret = bdrv_read(bs_old_backing, sector, buf_old, n); | |
2361 | if (ret < 0) { | |
2362 | error_report("error while reading from old backing file"); | |
2363 | goto out; | |
2364 | } | |
3e85c6fd | 2365 | } |
87a1b3e3 | 2366 | |
a616673d | 2367 | if (sector >= new_backing_num_sectors || !bs_new_backing) { |
87a1b3e3 KW |
2368 | memset(buf_new, 0, n * BDRV_SECTOR_SIZE); |
2369 | } else { | |
2370 | if (sector + n > new_backing_num_sectors) { | |
2371 | n = new_backing_num_sectors - sector; | |
2372 | } | |
2373 | ||
2374 | ret = bdrv_read(bs_new_backing, sector, buf_new, n); | |
2375 | if (ret < 0) { | |
2376 | error_report("error while reading from new backing file"); | |
2377 | goto out; | |
2378 | } | |
3e85c6fd KW |
2379 | } |
2380 | ||
2381 | /* If they differ, we need to write to the COW file */ | |
2382 | uint64_t written = 0; | |
2383 | ||
2384 | while (written < n) { | |
2385 | int pnum; | |
2386 | ||
2387 | if (compare_sectors(buf_old + written * 512, | |
60b1bd4f | 2388 | buf_new + written * 512, n - written, &pnum)) |
3e85c6fd KW |
2389 | { |
2390 | ret = bdrv_write(bs, sector + written, | |
2391 | buf_old + written * 512, pnum); | |
2392 | if (ret < 0) { | |
15654a6d | 2393 | error_report("Error while writing to COW image: %s", |
3e85c6fd | 2394 | strerror(-ret)); |
c2abccec | 2395 | goto out; |
3e85c6fd KW |
2396 | } |
2397 | } | |
2398 | ||
2399 | written += pnum; | |
2400 | } | |
6b837bc4 | 2401 | qemu_progress_print(local_progress, 100); |
3e85c6fd | 2402 | } |
d6771bfa | 2403 | |
bb1c0597 KW |
2404 | qemu_vfree(buf_old); |
2405 | qemu_vfree(buf_new); | |
3e85c6fd KW |
2406 | } |
2407 | ||
2408 | /* | |
2409 | * Change the backing file. All clusters that are different from the old | |
2410 | * backing file are overwritten in the COW file now, so the visible content | |
2411 | * doesn't change when we switch the backing file. | |
2412 | */ | |
a616673d AB |
2413 | if (out_baseimg && *out_baseimg) { |
2414 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); | |
2415 | } else { | |
2416 | ret = bdrv_change_backing_file(bs, NULL, NULL); | |
2417 | } | |
2418 | ||
3e85c6fd | 2419 | if (ret == -ENOSPC) { |
15654a6d JS |
2420 | error_report("Could not change the backing file to '%s': No " |
2421 | "space left in the file header", out_baseimg); | |
3e85c6fd | 2422 | } else if (ret < 0) { |
15654a6d | 2423 | error_report("Could not change the backing file to '%s': %s", |
3e85c6fd KW |
2424 | out_baseimg, strerror(-ret)); |
2425 | } | |
2426 | ||
6b837bc4 | 2427 | qemu_progress_print(100, 0); |
3e85c6fd KW |
2428 | /* |
2429 | * TODO At this point it is possible to check if any clusters that are | |
2430 | * allocated in the COW file are the same in the backing file. If so, they | |
2431 | * could be dropped from the COW file. Don't do this before switching the | |
2432 | * backing file, in case of a crash this would lead to corruption. | |
2433 | */ | |
c2abccec | 2434 | out: |
6b837bc4 | 2435 | qemu_progress_end(); |
3e85c6fd KW |
2436 | /* Cleanup */ |
2437 | if (!unsafe) { | |
eb863add | 2438 | if (bs_old_backing != NULL) { |
4f6fd349 | 2439 | bdrv_unref(bs_old_backing); |
eb863add KW |
2440 | } |
2441 | if (bs_new_backing != NULL) { | |
4f6fd349 | 2442 | bdrv_unref(bs_new_backing); |
eb863add | 2443 | } |
3e85c6fd KW |
2444 | } |
2445 | ||
4f6fd349 | 2446 | bdrv_unref(bs); |
c2abccec MK |
2447 | if (ret) { |
2448 | return 1; | |
2449 | } | |
3e85c6fd KW |
2450 | return 0; |
2451 | } | |
2452 | ||
ae6b0ed6 SH |
2453 | static int img_resize(int argc, char **argv) |
2454 | { | |
2455 | int c, ret, relative; | |
2456 | const char *filename, *fmt, *size; | |
2457 | int64_t n, total_size; | |
f382d43a | 2458 | bool quiet = false; |
2a81998a | 2459 | BlockDriverState *bs = NULL; |
20caf0f7 DXW |
2460 | QemuOpts *param; |
2461 | static QemuOptsList resize_options = { | |
2462 | .name = "resize_options", | |
2463 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), | |
2464 | .desc = { | |
2465 | { | |
2466 | .name = BLOCK_OPT_SIZE, | |
2467 | .type = QEMU_OPT_SIZE, | |
2468 | .help = "Virtual disk size" | |
2469 | }, { | |
2470 | /* end of list */ | |
2471 | } | |
ae6b0ed6 | 2472 | }, |
ae6b0ed6 SH |
2473 | }; |
2474 | ||
e80fec7f KW |
2475 | /* Remove size from argv manually so that negative numbers are not treated |
2476 | * as options by getopt. */ | |
2477 | if (argc < 3) { | |
2478 | help(); | |
2479 | return 1; | |
2480 | } | |
2481 | ||
2482 | size = argv[--argc]; | |
2483 | ||
2484 | /* Parse getopt arguments */ | |
ae6b0ed6 SH |
2485 | fmt = NULL; |
2486 | for(;;) { | |
f382d43a | 2487 | c = getopt(argc, argv, "f:hq"); |
ae6b0ed6 SH |
2488 | if (c == -1) { |
2489 | break; | |
2490 | } | |
2491 | switch(c) { | |
ef87394c | 2492 | case '?': |
ae6b0ed6 SH |
2493 | case 'h': |
2494 | help(); | |
2495 | break; | |
2496 | case 'f': | |
2497 | fmt = optarg; | |
2498 | break; | |
f382d43a MR |
2499 | case 'q': |
2500 | quiet = true; | |
2501 | break; | |
ae6b0ed6 SH |
2502 | } |
2503 | } | |
fc11eb26 | 2504 | if (optind != argc - 1) { |
ae6b0ed6 SH |
2505 | help(); |
2506 | } | |
2507 | filename = argv[optind++]; | |
ae6b0ed6 SH |
2508 | |
2509 | /* Choose grow, shrink, or absolute resize mode */ | |
2510 | switch (size[0]) { | |
2511 | case '+': | |
2512 | relative = 1; | |
2513 | size++; | |
2514 | break; | |
2515 | case '-': | |
2516 | relative = -1; | |
2517 | size++; | |
2518 | break; | |
2519 | default: | |
2520 | relative = 0; | |
2521 | break; | |
2522 | } | |
2523 | ||
2524 | /* Parse size */ | |
e478b448 | 2525 | param = qemu_opts_create_nofail(&resize_options); |
20caf0f7 | 2526 | if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) { |
ae6b0ed6 | 2527 | /* Error message already printed when size parsing fails */ |
2a81998a | 2528 | ret = -1; |
20caf0f7 | 2529 | qemu_opts_del(param); |
2a81998a | 2530 | goto out; |
ae6b0ed6 | 2531 | } |
20caf0f7 DXW |
2532 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
2533 | qemu_opts_del(param); | |
ae6b0ed6 | 2534 | |
f382d43a | 2535 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet); |
c2abccec | 2536 | if (!bs) { |
2a81998a JS |
2537 | ret = -1; |
2538 | goto out; | |
c2abccec | 2539 | } |
ae6b0ed6 SH |
2540 | |
2541 | if (relative) { | |
2542 | total_size = bdrv_getlength(bs) + n * relative; | |
2543 | } else { | |
2544 | total_size = n; | |
2545 | } | |
2546 | if (total_size <= 0) { | |
15654a6d | 2547 | error_report("New image size must be positive"); |
c2abccec MK |
2548 | ret = -1; |
2549 | goto out; | |
ae6b0ed6 SH |
2550 | } |
2551 | ||
2552 | ret = bdrv_truncate(bs, total_size); | |
2553 | switch (ret) { | |
2554 | case 0: | |
f382d43a | 2555 | qprintf(quiet, "Image resized.\n"); |
ae6b0ed6 SH |
2556 | break; |
2557 | case -ENOTSUP: | |
259b2173 | 2558 | error_report("This image does not support resize"); |
ae6b0ed6 SH |
2559 | break; |
2560 | case -EACCES: | |
15654a6d | 2561 | error_report("Image is read-only"); |
ae6b0ed6 SH |
2562 | break; |
2563 | default: | |
15654a6d | 2564 | error_report("Error resizing image (%d)", -ret); |
ae6b0ed6 SH |
2565 | break; |
2566 | } | |
c2abccec | 2567 | out: |
2a81998a | 2568 | if (bs) { |
4f6fd349 | 2569 | bdrv_unref(bs); |
2a81998a | 2570 | } |
c2abccec MK |
2571 | if (ret) { |
2572 | return 1; | |
2573 | } | |
ae6b0ed6 SH |
2574 | return 0; |
2575 | } | |
2576 | ||
6f176b48 HR |
2577 | static int img_amend(int argc, char **argv) |
2578 | { | |
2579 | int c, ret = 0; | |
2580 | char *options = NULL; | |
2581 | QEMUOptionParameter *create_options = NULL, *options_param = NULL; | |
2582 | const char *fmt = NULL, *filename; | |
2583 | bool quiet = false; | |
2584 | BlockDriverState *bs = NULL; | |
2585 | ||
2586 | for (;;) { | |
2587 | c = getopt(argc, argv, "hqf:o:"); | |
2588 | if (c == -1) { | |
2589 | break; | |
2590 | } | |
2591 | ||
2592 | switch (c) { | |
2593 | case 'h': | |
2594 | case '?': | |
2595 | help(); | |
2596 | break; | |
2597 | case 'o': | |
2598 | options = optarg; | |
2599 | break; | |
2600 | case 'f': | |
2601 | fmt = optarg; | |
2602 | break; | |
2603 | case 'q': | |
2604 | quiet = true; | |
2605 | break; | |
2606 | } | |
2607 | } | |
2608 | ||
2609 | if (optind != argc - 1) { | |
2610 | help(); | |
2611 | } | |
2612 | ||
2613 | if (!options) { | |
2614 | help(); | |
2615 | } | |
2616 | ||
2617 | filename = argv[argc - 1]; | |
2618 | ||
2619 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet); | |
2620 | if (!bs) { | |
2621 | error_report("Could not open image '%s'", filename); | |
2622 | ret = -1; | |
2623 | goto out; | |
2624 | } | |
2625 | ||
2626 | fmt = bs->drv->format_name; | |
2627 | ||
2628 | if (is_help_option(options)) { | |
2629 | ret = print_block_option_help(filename, fmt); | |
2630 | goto out; | |
2631 | } | |
2632 | ||
2633 | create_options = append_option_parameters(create_options, | |
2634 | bs->drv->create_options); | |
2635 | options_param = parse_option_parameters(options, create_options, | |
2636 | options_param); | |
2637 | if (options_param == NULL) { | |
2638 | error_report("Invalid options for file format '%s'", fmt); | |
2639 | ret = -1; | |
2640 | goto out; | |
2641 | } | |
2642 | ||
2643 | ret = bdrv_amend_options(bs, options_param); | |
2644 | if (ret < 0) { | |
2645 | error_report("Error while amending options: %s", strerror(-ret)); | |
2646 | goto out; | |
2647 | } | |
2648 | ||
2649 | out: | |
2650 | if (bs) { | |
2651 | bdrv_unref(bs); | |
2652 | } | |
2653 | free_option_parameters(create_options); | |
2654 | free_option_parameters(options_param); | |
2655 | if (ret) { | |
2656 | return 1; | |
2657 | } | |
2658 | return 0; | |
2659 | } | |
2660 | ||
c227f099 | 2661 | static const img_cmd_t img_cmds[] = { |
153859be SB |
2662 | #define DEF(option, callback, arg_string) \ |
2663 | { option, callback }, | |
2664 | #include "qemu-img-cmds.h" | |
2665 | #undef DEF | |
2666 | #undef GEN_DOCS | |
2667 | { NULL, NULL, }, | |
2668 | }; | |
2669 | ||
ea2384d3 FB |
2670 | int main(int argc, char **argv) |
2671 | { | |
c227f099 | 2672 | const img_cmd_t *cmd; |
153859be | 2673 | const char *cmdname; |
ea2384d3 | 2674 | |
526eda14 MK |
2675 | #ifdef CONFIG_POSIX |
2676 | signal(SIGPIPE, SIG_IGN); | |
2677 | #endif | |
2678 | ||
53f76e58 KW |
2679 | error_set_progname(argv[0]); |
2680 | ||
2592c59a | 2681 | qemu_init_main_loop(); |
ea2384d3 FB |
2682 | bdrv_init(); |
2683 | if (argc < 2) | |
2684 | help(); | |
153859be | 2685 | cmdname = argv[1]; |
8f9b157e | 2686 | argc--; argv++; |
153859be SB |
2687 | |
2688 | /* find the command */ | |
2689 | for(cmd = img_cmds; cmd->name != NULL; cmd++) { | |
2690 | if (!strcmp(cmdname, cmd->name)) { | |
2691 | return cmd->handler(argc, argv); | |
2692 | } | |
ea2384d3 | 2693 | } |
153859be SB |
2694 | |
2695 | /* not found */ | |
2696 | help(); | |
ea2384d3 FB |
2697 | return 0; |
2698 | } |