bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1 | /* |
bellard | fb43f4d | 2006-08-07 21:34:46 +0000 | [diff] [blame] | 2 | * QEMU disk image utility |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | 68d0f70 | 2008-01-06 17:21:48 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 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 | */ |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 24 | #include "qapi-visit.h" |
| 25 | #include "qapi/qmp-output-visitor.h" |
Paolo Bonzini | 7b1b5d1 | 2012-12-17 18:19:43 +0100 | [diff] [blame] | 26 | #include "qapi/qmp/qjson.h" |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 27 | #include "qemu-common.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 28 | #include "qemu/option.h" |
| 29 | #include "qemu/error-report.h" |
| 30 | #include "qemu/osdep.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 31 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 737e150 | 2012-12-17 18:19:44 +0100 | [diff] [blame] | 32 | #include "block/block_int.h" |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 33 | #include "block/qapi.h" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 34 | #include <getopt.h> |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 35 | #include <stdio.h> |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 36 | #include <stdarg.h> |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 37 | |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 38 | #ifdef _WIN32 |
| 39 | #include <windows.h> |
| 40 | #endif |
| 41 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 42 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 43 | const char *name; |
| 44 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 45 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 46 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 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 | |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 57 | /* Default to cache=writeback as data integrity is not important for qemu-tcg. */ |
Stefan Hajnoczi | adfe078 | 2010-04-13 10:29:35 +0100 | [diff] [blame] | 58 | #define BDRV_O_FLAGS BDRV_O_CACHE_WB |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 59 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 60 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 61 | static void format_print(void *opaque, const char *name) |
| 62 | { |
| 63 | printf(" %s", name); |
| 64 | } |
| 65 | |
blueswir1 | d2c639d | 2009-01-24 18:19:25 +0000 | [diff] [blame] | 66 | /* Please keep in synch with qemu-img.texi */ |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 67 | static void help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 68 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 69 | const char *help_msg = |
| 70 | "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 71 | "usage: qemu-img command [command options]\n" |
| 72 | "QEMU disk image utility\n" |
| 73 | "\n" |
| 74 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 75 | #define DEF(option, callback, arg_string) \ |
| 76 | " " arg_string "\n" |
| 77 | #include "qemu-img-cmds.h" |
| 78 | #undef DEF |
| 79 | #undef GEN_DOCS |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 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" |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 84 | " 'cache' is the cache mode used to write the output disk image, the valid\n" |
Liu Yuan | 80ccf93 | 2012-04-20 17:10:56 +0800 | [diff] [blame] | 85 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 86 | " 'directsync' and 'unsafe' (default for convert)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 87 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
| 88 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n" |
| 89 | " and T (terabyte, 1024G) are supported. 'b' is ignored.\n" |
| 90 | " 'output_filename' is the destination disk image filename\n" |
| 91 | " 'output_fmt' is the destination format\n" |
| 92 | " 'options' is a comma separated list of format specific options in a\n" |
| 93 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 94 | " used format\n" |
| 95 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
| 96 | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n" |
| 97 | " match exactly. The image doesn't need a working backing file before\n" |
| 98 | " rebasing in this case (useful for renaming the backing file)\n" |
| 99 | " '-h' with or without a command shows this help and lists the supported formats\n" |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 100 | " '-p' show progress of command (only certain commands)\n" |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 101 | " '-q' use Quiet mode - do not print any output (except errors)\n" |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 102 | " '-S' indicates the consecutive number of bytes that must contain only zeros\n" |
| 103 | " for qemu-img to create a sparse image during conversion\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 104 | " '--output' takes the format in which the output must be done (human or json)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 105 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 106 | "Parameters to check subcommand:\n" |
| 107 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 108 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 109 | " kinds of errors, with a higher risk of choosing the wrong fix or\n" |
Stefan Weil | 0546b8c | 2012-08-10 22:03:25 +0200 | [diff] [blame] | 110 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 111 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 112 | "Parameters to snapshot subcommand:\n" |
| 113 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 114 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 115 | " '-c' creates a snapshot\n" |
| 116 | " '-d' deletes a snapshot\n" |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 117 | " '-l' lists all snapshots in the given image\n" |
| 118 | "\n" |
| 119 | "Parameters to compare subcommand:\n" |
| 120 | " '-f' first image format\n" |
| 121 | " '-F' second image format\n" |
| 122 | " '-s' run in Strict mode - fail on different image size or sector allocation\n"; |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 123 | |
| 124 | printf("%s\nSupported formats:", help_msg); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 125 | bdrv_iterate_format(format_print, NULL); |
| 126 | printf("\n"); |
| 127 | exit(1); |
| 128 | } |
| 129 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 130 | static int qprintf(bool quiet, const char *fmt, ...) |
| 131 | { |
| 132 | int ret = 0; |
| 133 | if (!quiet) { |
| 134 | va_list args; |
| 135 | va_start(args, fmt); |
| 136 | ret = vprintf(fmt, args); |
| 137 | va_end(args); |
| 138 | } |
| 139 | return ret; |
| 140 | } |
| 141 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 142 | #if defined(WIN32) |
| 143 | /* XXX: put correct support for win32 */ |
| 144 | static int read_password(char *buf, int buf_size) |
| 145 | { |
| 146 | int c, i; |
| 147 | printf("Password: "); |
| 148 | fflush(stdout); |
| 149 | i = 0; |
| 150 | for(;;) { |
| 151 | c = getchar(); |
| 152 | if (c == '\n') |
| 153 | break; |
| 154 | if (i < (buf_size - 1)) |
| 155 | buf[i++] = c; |
| 156 | } |
| 157 | buf[i] = '\0'; |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | #else |
| 162 | |
| 163 | #include <termios.h> |
| 164 | |
| 165 | static struct termios oldtty; |
| 166 | |
| 167 | static void term_exit(void) |
| 168 | { |
| 169 | tcsetattr (0, TCSANOW, &oldtty); |
| 170 | } |
| 171 | |
| 172 | static void term_init(void) |
| 173 | { |
| 174 | struct termios tty; |
| 175 | |
| 176 | tcgetattr (0, &tty); |
| 177 | oldtty = tty; |
| 178 | |
| 179 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 180 | |INLCR|IGNCR|ICRNL|IXON); |
| 181 | tty.c_oflag |= OPOST; |
| 182 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 183 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 184 | tty.c_cflag |= CS8; |
| 185 | tty.c_cc[VMIN] = 1; |
| 186 | tty.c_cc[VTIME] = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 187 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 188 | tcsetattr (0, TCSANOW, &tty); |
| 189 | |
| 190 | atexit(term_exit); |
| 191 | } |
| 192 | |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 193 | static int read_password(char *buf, int buf_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 194 | { |
| 195 | uint8_t ch; |
| 196 | int i, ret; |
| 197 | |
| 198 | printf("password: "); |
| 199 | fflush(stdout); |
| 200 | term_init(); |
| 201 | i = 0; |
| 202 | for(;;) { |
| 203 | ret = read(0, &ch, 1); |
| 204 | if (ret == -1) { |
| 205 | if (errno == EAGAIN || errno == EINTR) { |
| 206 | continue; |
| 207 | } else { |
| 208 | ret = -1; |
| 209 | break; |
| 210 | } |
| 211 | } else if (ret == 0) { |
| 212 | ret = -1; |
| 213 | break; |
| 214 | } else { |
| 215 | if (ch == '\r') { |
| 216 | ret = 0; |
| 217 | break; |
| 218 | } |
| 219 | if (i < (buf_size - 1)) |
| 220 | buf[i++] = ch; |
| 221 | } |
| 222 | } |
| 223 | term_exit(); |
| 224 | buf[i] = '\0'; |
| 225 | printf("\n"); |
| 226 | return ret; |
| 227 | } |
| 228 | #endif |
| 229 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 230 | static int print_block_option_help(const char *filename, const char *fmt) |
| 231 | { |
| 232 | BlockDriver *drv, *proto_drv; |
| 233 | QEMUOptionParameter *create_options = NULL; |
| 234 | |
| 235 | /* Find driver and parse its options */ |
| 236 | drv = bdrv_find_format(fmt); |
| 237 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 238 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 239 | return 1; |
| 240 | } |
| 241 | |
| 242 | proto_drv = bdrv_find_protocol(filename); |
| 243 | if (!proto_drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 244 | error_report("Unknown protocol '%s'", filename); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 245 | return 1; |
| 246 | } |
| 247 | |
| 248 | create_options = append_option_parameters(create_options, |
| 249 | drv->create_options); |
| 250 | create_options = append_option_parameters(create_options, |
| 251 | proto_drv->create_options); |
| 252 | print_option_help(create_options); |
| 253 | free_option_parameters(create_options); |
| 254 | return 0; |
| 255 | } |
| 256 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 257 | static BlockDriverState *bdrv_new_open(const char *filename, |
Sheng Yang | 9bc378c | 2010-01-29 10:15:06 +0800 | [diff] [blame] | 258 | const char *fmt, |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 259 | int flags, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 260 | bool require_io, |
| 261 | bool quiet) |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 262 | { |
| 263 | BlockDriverState *bs; |
| 264 | BlockDriver *drv; |
| 265 | char password[256]; |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 266 | int ret; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 267 | |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 268 | bs = bdrv_new("image"); |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 269 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 270 | if (fmt) { |
| 271 | drv = bdrv_find_format(fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 272 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 273 | error_report("Unknown file format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 274 | goto fail; |
| 275 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 276 | } else { |
| 277 | drv = NULL; |
| 278 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 279 | |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 280 | ret = bdrv_open(bs, filename, NULL, flags, drv); |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 281 | if (ret < 0) { |
| 282 | error_report("Could not open '%s': %s", filename, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 283 | goto fail; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 284 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 285 | |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 286 | if (bdrv_is_encrypted(bs) && require_io) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 287 | qprintf(quiet, "Disk image '%s' is encrypted.\n", filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 288 | if (read_password(password, sizeof(password)) < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 289 | error_report("No password given"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 290 | goto fail; |
| 291 | } |
| 292 | if (bdrv_set_key(bs, password) < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 293 | error_report("invalid password"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 294 | goto fail; |
| 295 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 296 | } |
| 297 | return bs; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 298 | fail: |
| 299 | if (bs) { |
| 300 | bdrv_delete(bs); |
| 301 | } |
| 302 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 303 | } |
| 304 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 305 | static int add_old_style_options(const char *fmt, QEMUOptionParameter *list, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 306 | const char *base_filename, |
| 307 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 308 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 309 | if (base_filename) { |
| 310 | if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 311 | error_report("Backing file not supported for file format '%s'", |
| 312 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 313 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | if (base_fmt) { |
| 317 | if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 318 | error_report("Backing file format not supported for file " |
| 319 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 320 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 321 | } |
| 322 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 323 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 324 | } |
| 325 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 326 | static int img_create(int argc, char **argv) |
| 327 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 328 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 329 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 330 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 331 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 332 | const char *filename; |
| 333 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 334 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 335 | Error *local_err = NULL; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 336 | bool quiet = false; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 337 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 338 | for(;;) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 339 | c = getopt(argc, argv, "F:b:f:he6o:q"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 340 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 341 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 342 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 343 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 344 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 345 | case 'h': |
| 346 | help(); |
| 347 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 348 | case 'F': |
| 349 | base_fmt = optarg; |
| 350 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 351 | case 'b': |
| 352 | base_filename = optarg; |
| 353 | break; |
| 354 | case 'f': |
| 355 | fmt = optarg; |
| 356 | break; |
| 357 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 358 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 359 | "encryption\' instead!"); |
| 360 | return 1; |
ths | d8871c5 | 2007-10-24 16:11:42 +0000 | [diff] [blame] | 361 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 362 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 363 | "compat6\' instead!"); |
| 364 | return 1; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 365 | case 'o': |
| 366 | options = optarg; |
| 367 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 368 | case 'q': |
| 369 | quiet = true; |
| 370 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 371 | } |
| 372 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 373 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 374 | /* Get the filename */ |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 375 | if (optind >= argc) { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 376 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 377 | } |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 378 | filename = argv[optind++]; |
| 379 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 380 | /* Get image size, if specified */ |
| 381 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 382 | int64_t sval; |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 383 | char *end; |
| 384 | sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B); |
| 385 | if (sval < 0 || *end) { |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 386 | if (sval == -ERANGE) { |
| 387 | error_report("Image size must be less than 8 EiB!"); |
| 388 | } else { |
| 389 | error_report("Invalid image size specified! You may use k, M, " |
| 390 | "G or T suffixes for "); |
| 391 | error_report("kilobytes, megabytes, gigabytes and terabytes."); |
| 392 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 393 | return 1; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 394 | } |
| 395 | img_size = (uint64_t)sval; |
| 396 | } |
| 397 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 398 | if (options && is_help_option(options)) { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 399 | return print_block_option_help(filename, fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 402 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 403 | options, img_size, BDRV_O_FLAGS, &local_err, quiet); |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 404 | if (error_is_set(&local_err)) { |
| 405 | error_report("%s", error_get_pretty(local_err)); |
| 406 | error_free(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 407 | return 1; |
| 408 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 409 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 410 | return 0; |
| 411 | } |
| 412 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 413 | static void dump_json_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 414 | { |
| 415 | Error *errp = NULL; |
| 416 | QString *str; |
| 417 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 418 | QObject *obj; |
| 419 | visit_type_ImageCheck(qmp_output_get_visitor(ov), |
| 420 | &check, NULL, &errp); |
| 421 | obj = qmp_output_get_qobject(ov); |
| 422 | str = qobject_to_json_pretty(obj); |
| 423 | assert(str != NULL); |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 424 | qprintf(quiet, "%s\n", qstring_get_str(str)); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 425 | qobject_decref(obj); |
| 426 | qmp_output_visitor_cleanup(ov); |
| 427 | QDECREF(str); |
| 428 | } |
| 429 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 430 | static void dump_human_image_check(ImageCheck *check, bool quiet) |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 431 | { |
| 432 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 433 | qprintf(quiet, "No errors were found on the image.\n"); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 434 | } else { |
| 435 | if (check->corruptions) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 436 | qprintf(quiet, "\n%" PRId64 " errors were found on the image.\n" |
| 437 | "Data may be corrupted, or further writes to the image " |
| 438 | "may corrupt it.\n", |
| 439 | check->corruptions); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | if (check->leaks) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 443 | qprintf(quiet, |
| 444 | "\n%" PRId64 " leaked clusters were found on the image.\n" |
| 445 | "This means waste of disk space, but no harm to data.\n", |
| 446 | check->leaks); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | if (check->check_errors) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 450 | qprintf(quiet, |
| 451 | "\n%" PRId64 |
| 452 | " internal errors have occurred during the check.\n", |
| 453 | check->check_errors); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 454 | } |
| 455 | } |
| 456 | |
| 457 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 458 | qprintf(quiet, "%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
| 459 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
| 460 | check->allocated_clusters, check->total_clusters, |
| 461 | check->allocated_clusters * 100.0 / check->total_clusters, |
| 462 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 463 | check->compressed_clusters * 100.0 / |
| 464 | check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | if (check->image_end_offset) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 468 | qprintf(quiet, |
| 469 | "Image end offset: %" PRId64 "\n", check->image_end_offset); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 470 | } |
| 471 | } |
| 472 | |
| 473 | static int collect_image_check(BlockDriverState *bs, |
| 474 | ImageCheck *check, |
| 475 | const char *filename, |
| 476 | const char *fmt, |
| 477 | int fix) |
| 478 | { |
| 479 | int ret; |
| 480 | BdrvCheckResult result; |
| 481 | |
| 482 | ret = bdrv_check(bs, &result, fix); |
| 483 | if (ret < 0) { |
| 484 | return ret; |
| 485 | } |
| 486 | |
| 487 | check->filename = g_strdup(filename); |
| 488 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 489 | check->check_errors = result.check_errors; |
| 490 | check->corruptions = result.corruptions; |
| 491 | check->has_corruptions = result.corruptions != 0; |
| 492 | check->leaks = result.leaks; |
| 493 | check->has_leaks = result.leaks != 0; |
| 494 | check->corruptions_fixed = result.corruptions_fixed; |
| 495 | check->has_corruptions_fixed = result.corruptions != 0; |
| 496 | check->leaks_fixed = result.leaks_fixed; |
| 497 | check->has_leaks_fixed = result.leaks != 0; |
| 498 | check->image_end_offset = result.image_end_offset; |
| 499 | check->has_image_end_offset = result.image_end_offset != 0; |
| 500 | check->total_clusters = result.bfi.total_clusters; |
| 501 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 502 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 503 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 504 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 505 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame] | 506 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 507 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 508 | |
| 509 | return 0; |
| 510 | } |
| 511 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 512 | /* |
| 513 | * Checks an image for consistency. Exit codes: |
| 514 | * |
| 515 | * 0 - Check completed, image is good |
| 516 | * 1 - Check not completed because of internal errors |
| 517 | * 2 - Check completed, image is corrupted |
| 518 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 519 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 520 | static int img_check(int argc, char **argv) |
| 521 | { |
| 522 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 523 | OutputFormat output_format = OFORMAT_HUMAN; |
| 524 | const char *filename, *fmt, *output; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 525 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 526 | int fix = 0; |
Stefan Hajnoczi | 058f8f1 | 2012-08-09 13:05:56 +0100 | [diff] [blame] | 527 | int flags = BDRV_O_FLAGS | BDRV_O_CHECK; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 528 | ImageCheck *check; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 529 | bool quiet = false; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 530 | |
| 531 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 532 | output = NULL; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 533 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 534 | int option_index = 0; |
| 535 | static const struct option long_options[] = { |
| 536 | {"help", no_argument, 0, 'h'}, |
| 537 | {"format", required_argument, 0, 'f'}, |
| 538 | {"repair", no_argument, 0, 'r'}, |
| 539 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 540 | {0, 0, 0, 0} |
| 541 | }; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 542 | c = getopt_long(argc, argv, "f:hr:q", |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 543 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 544 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 545 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 546 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 547 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 548 | case '?': |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 549 | case 'h': |
| 550 | help(); |
| 551 | break; |
| 552 | case 'f': |
| 553 | fmt = optarg; |
| 554 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 555 | case 'r': |
| 556 | flags |= BDRV_O_RDWR; |
| 557 | |
| 558 | if (!strcmp(optarg, "leaks")) { |
| 559 | fix = BDRV_FIX_LEAKS; |
| 560 | } else if (!strcmp(optarg, "all")) { |
| 561 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 562 | } else { |
| 563 | help(); |
| 564 | } |
| 565 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 566 | case OPTION_OUTPUT: |
| 567 | output = optarg; |
| 568 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 569 | case 'q': |
| 570 | quiet = true; |
| 571 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 572 | } |
| 573 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 574 | if (optind >= argc) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 575 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 576 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 577 | filename = argv[optind++]; |
| 578 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 579 | if (output && !strcmp(output, "json")) { |
| 580 | output_format = OFORMAT_JSON; |
| 581 | } else if (output && !strcmp(output, "human")) { |
| 582 | output_format = OFORMAT_HUMAN; |
| 583 | } else if (output) { |
| 584 | error_report("--output must be used with human or json as argument."); |
| 585 | return 1; |
| 586 | } |
| 587 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 588 | bs = bdrv_new_open(filename, fmt, flags, true, quiet); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 589 | if (!bs) { |
| 590 | return 1; |
| 591 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 592 | |
| 593 | check = g_new0(ImageCheck, 1); |
| 594 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 595 | |
| 596 | if (ret == -ENOTSUP) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 597 | if (output_format == OFORMAT_HUMAN) { |
| 598 | error_report("This image format does not support checks"); |
| 599 | } |
| 600 | ret = 1; |
| 601 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 602 | } |
| 603 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 604 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 605 | int corruptions_fixed, leaks_fixed; |
| 606 | |
| 607 | leaks_fixed = check->leaks_fixed; |
| 608 | corruptions_fixed = check->corruptions_fixed; |
| 609 | |
| 610 | if (output_format == OFORMAT_HUMAN) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 611 | qprintf(quiet, |
| 612 | "The following inconsistencies were found and repaired:\n\n" |
| 613 | " %" PRId64 " leaked clusters\n" |
| 614 | " %" PRId64 " corruptions\n\n" |
| 615 | "Double checking the fixed image now...\n", |
| 616 | check->leaks_fixed, |
| 617 | check->corruptions_fixed); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 621 | |
| 622 | check->leaks_fixed = leaks_fixed; |
| 623 | check->corruptions_fixed = corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 624 | } |
| 625 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 626 | switch (output_format) { |
| 627 | case OFORMAT_HUMAN: |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 628 | dump_human_image_check(check, quiet); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 629 | break; |
| 630 | case OFORMAT_JSON: |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 631 | dump_json_image_check(check, quiet); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 632 | break; |
| 633 | } |
| 634 | |
| 635 | if (ret || check->check_errors) { |
| 636 | ret = 1; |
| 637 | goto fail; |
| 638 | } |
| 639 | |
| 640 | if (check->corruptions) { |
| 641 | ret = 2; |
| 642 | } else if (check->leaks) { |
| 643 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 644 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 645 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 646 | } |
| 647 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 648 | fail: |
| 649 | qapi_free_ImageCheck(check); |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 650 | bdrv_delete(bs); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 651 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 652 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 653 | } |
| 654 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 655 | static int img_commit(int argc, char **argv) |
| 656 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 657 | int c, ret, flags; |
| 658 | const char *filename, *fmt, *cache; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 659 | BlockDriverState *bs; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 660 | bool quiet = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 661 | |
| 662 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 663 | cache = BDRV_DEFAULT_CACHE; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 664 | for(;;) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 665 | c = getopt(argc, argv, "f:ht:q"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 666 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 667 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 668 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 669 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 670 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 671 | case 'h': |
| 672 | help(); |
| 673 | break; |
| 674 | case 'f': |
| 675 | fmt = optarg; |
| 676 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 677 | case 't': |
| 678 | cache = optarg; |
| 679 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 680 | case 'q': |
| 681 | quiet = true; |
| 682 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 683 | } |
| 684 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 685 | if (optind >= argc) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 686 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 687 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 688 | filename = argv[optind++]; |
| 689 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 690 | flags = BDRV_O_RDWR; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 691 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 692 | if (ret < 0) { |
| 693 | error_report("Invalid cache option: %s", cache); |
| 694 | return -1; |
| 695 | } |
| 696 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 697 | bs = bdrv_new_open(filename, fmt, flags, true, quiet); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 698 | if (!bs) { |
| 699 | return 1; |
| 700 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 701 | ret = bdrv_commit(bs); |
| 702 | switch(ret) { |
| 703 | case 0: |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 704 | qprintf(quiet, "Image committed.\n"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 705 | break; |
| 706 | case -ENOENT: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 707 | error_report("No disk inserted"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 708 | break; |
| 709 | case -EACCES: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 710 | error_report("Image is read-only"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 711 | break; |
| 712 | case -ENOTSUP: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 713 | error_report("Image is already committed"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 714 | break; |
| 715 | default: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 716 | error_report("Error while committing image"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 717 | break; |
| 718 | } |
| 719 | |
| 720 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 721 | if (ret) { |
| 722 | return 1; |
| 723 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 724 | return 0; |
| 725 | } |
| 726 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 727 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 728 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 729 | * a non-NUL byte. |
| 730 | * |
| 731 | * 'pnum' is set to the number of sectors (including and immediately following |
| 732 | * the first one) that are known to be in the same allocated/unallocated state. |
| 733 | */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 734 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
| 735 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 736 | bool is_zero; |
| 737 | int i; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 738 | |
| 739 | if (n <= 0) { |
| 740 | *pnum = 0; |
| 741 | return 0; |
| 742 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 743 | is_zero = buffer_is_zero(buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 744 | for(i = 1; i < n; i++) { |
| 745 | buf += 512; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 746 | if (is_zero != buffer_is_zero(buf, 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 747 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 748 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 749 | } |
| 750 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 751 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 754 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 755 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 756 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 757 | * breaking up write requests for only small sparse areas. |
| 758 | */ |
| 759 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, |
| 760 | int min) |
| 761 | { |
| 762 | int ret; |
| 763 | int num_checked, num_used; |
| 764 | |
| 765 | if (n < min) { |
| 766 | min = n; |
| 767 | } |
| 768 | |
| 769 | ret = is_allocated_sectors(buf, n, pnum); |
| 770 | if (!ret) { |
| 771 | return ret; |
| 772 | } |
| 773 | |
| 774 | num_used = *pnum; |
| 775 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 776 | n -= *pnum; |
| 777 | num_checked = num_used; |
| 778 | |
| 779 | while (n > 0) { |
| 780 | ret = is_allocated_sectors(buf, n, pnum); |
| 781 | |
| 782 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 783 | n -= *pnum; |
| 784 | num_checked += *pnum; |
| 785 | if (ret) { |
| 786 | num_used = num_checked; |
| 787 | } else if (*pnum >= min) { |
| 788 | break; |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | *pnum = num_used; |
| 793 | return 1; |
| 794 | } |
| 795 | |
| 796 | /* |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 797 | * Compares two buffers sector by sector. Returns 0 if the first sector of both |
| 798 | * buffers matches, non-zero otherwise. |
| 799 | * |
| 800 | * pnum is set to the number of sectors (including and immediately following |
| 801 | * the first one) that are known to have the same comparison result |
| 802 | */ |
| 803 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, |
| 804 | int *pnum) |
| 805 | { |
| 806 | int res, i; |
| 807 | |
| 808 | if (n <= 0) { |
| 809 | *pnum = 0; |
| 810 | return 0; |
| 811 | } |
| 812 | |
| 813 | res = !!memcmp(buf1, buf2, 512); |
| 814 | for(i = 1; i < n; i++) { |
| 815 | buf1 += 512; |
| 816 | buf2 += 512; |
| 817 | |
| 818 | if (!!memcmp(buf1, buf2, 512) != res) { |
| 819 | break; |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | *pnum = i; |
| 824 | return res; |
| 825 | } |
| 826 | |
Kevin Wolf | 80ee15a | 2009-09-15 12:30:43 +0200 | [diff] [blame] | 827 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 828 | |
Miroslav Rezanina | d14ed18 | 2013-02-13 09:09:41 +0100 | [diff] [blame] | 829 | static int64_t sectors_to_bytes(int64_t sectors) |
| 830 | { |
| 831 | return sectors << BDRV_SECTOR_BITS; |
| 832 | } |
| 833 | |
| 834 | static int64_t sectors_to_process(int64_t total, int64_t from) |
| 835 | { |
| 836 | return MIN(total - from, IO_BUF_SIZE >> BDRV_SECTOR_BITS); |
| 837 | } |
| 838 | |
| 839 | /* |
| 840 | * Check if passed sectors are empty (not allocated or contain only 0 bytes) |
| 841 | * |
| 842 | * Returns 0 in case sectors are filled with 0, 1 if sectors contain non-zero |
| 843 | * data and negative value on error. |
| 844 | * |
| 845 | * @param bs: Driver used for accessing file |
| 846 | * @param sect_num: Number of first sector to check |
| 847 | * @param sect_count: Number of sectors to check |
| 848 | * @param filename: Name of disk file we are checking (logging purpose) |
| 849 | * @param buffer: Allocated buffer for storing read data |
| 850 | * @param quiet: Flag for quiet mode |
| 851 | */ |
| 852 | static int check_empty_sectors(BlockDriverState *bs, int64_t sect_num, |
| 853 | int sect_count, const char *filename, |
| 854 | uint8_t *buffer, bool quiet) |
| 855 | { |
| 856 | int pnum, ret = 0; |
| 857 | ret = bdrv_read(bs, sect_num, buffer, sect_count); |
| 858 | if (ret < 0) { |
| 859 | error_report("Error while reading offset %" PRId64 " of %s: %s", |
| 860 | sectors_to_bytes(sect_num), filename, strerror(-ret)); |
| 861 | return ret; |
| 862 | } |
| 863 | ret = is_allocated_sectors(buffer, sect_count, &pnum); |
| 864 | if (ret || pnum != sect_count) { |
| 865 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
| 866 | sectors_to_bytes(ret ? sect_num : sect_num + pnum)); |
| 867 | return 1; |
| 868 | } |
| 869 | |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | /* |
| 874 | * Compares two images. Exit codes: |
| 875 | * |
| 876 | * 0 - Images are identical |
| 877 | * 1 - Images differ |
| 878 | * >1 - Error occurred |
| 879 | */ |
| 880 | static int img_compare(int argc, char **argv) |
| 881 | { |
| 882 | const char *fmt1 = NULL, *fmt2 = NULL, *filename1, *filename2; |
| 883 | BlockDriverState *bs1, *bs2; |
| 884 | int64_t total_sectors1, total_sectors2; |
| 885 | uint8_t *buf1 = NULL, *buf2 = NULL; |
| 886 | int pnum1, pnum2; |
| 887 | int allocated1, allocated2; |
| 888 | int ret = 0; /* return value - 0 Ident, 1 Different, >1 Error */ |
| 889 | bool progress = false, quiet = false, strict = false; |
| 890 | int64_t total_sectors; |
| 891 | int64_t sector_num = 0; |
| 892 | int64_t nb_sectors; |
| 893 | int c, pnum; |
| 894 | uint64_t bs_sectors; |
| 895 | uint64_t progress_base; |
| 896 | |
| 897 | for (;;) { |
| 898 | c = getopt(argc, argv, "hpf:F:sq"); |
| 899 | if (c == -1) { |
| 900 | break; |
| 901 | } |
| 902 | switch (c) { |
| 903 | case '?': |
| 904 | case 'h': |
| 905 | help(); |
| 906 | break; |
| 907 | case 'f': |
| 908 | fmt1 = optarg; |
| 909 | break; |
| 910 | case 'F': |
| 911 | fmt2 = optarg; |
| 912 | break; |
| 913 | case 'p': |
| 914 | progress = true; |
| 915 | break; |
| 916 | case 'q': |
| 917 | quiet = true; |
| 918 | break; |
| 919 | case 's': |
| 920 | strict = true; |
| 921 | break; |
| 922 | } |
| 923 | } |
| 924 | |
| 925 | /* Progress is not shown in Quiet mode */ |
| 926 | if (quiet) { |
| 927 | progress = false; |
| 928 | } |
| 929 | |
| 930 | |
| 931 | if (optind > argc - 2) { |
| 932 | help(); |
| 933 | } |
| 934 | filename1 = argv[optind++]; |
| 935 | filename2 = argv[optind++]; |
| 936 | |
| 937 | /* Initialize before goto out */ |
| 938 | qemu_progress_init(progress, 2.0); |
| 939 | |
| 940 | bs1 = bdrv_new_open(filename1, fmt1, BDRV_O_FLAGS, true, quiet); |
| 941 | if (!bs1) { |
| 942 | error_report("Can't open file %s", filename1); |
| 943 | ret = 2; |
| 944 | goto out3; |
| 945 | } |
| 946 | |
| 947 | bs2 = bdrv_new_open(filename2, fmt2, BDRV_O_FLAGS, true, quiet); |
| 948 | if (!bs2) { |
| 949 | error_report("Can't open file %s", filename2); |
| 950 | ret = 2; |
| 951 | goto out2; |
| 952 | } |
| 953 | |
| 954 | buf1 = qemu_blockalign(bs1, IO_BUF_SIZE); |
| 955 | buf2 = qemu_blockalign(bs2, IO_BUF_SIZE); |
| 956 | bdrv_get_geometry(bs1, &bs_sectors); |
| 957 | total_sectors1 = bs_sectors; |
| 958 | bdrv_get_geometry(bs2, &bs_sectors); |
| 959 | total_sectors2 = bs_sectors; |
| 960 | total_sectors = MIN(total_sectors1, total_sectors2); |
| 961 | progress_base = MAX(total_sectors1, total_sectors2); |
| 962 | |
| 963 | qemu_progress_print(0, 100); |
| 964 | |
| 965 | if (strict && total_sectors1 != total_sectors2) { |
| 966 | ret = 1; |
| 967 | qprintf(quiet, "Strict mode: Image size mismatch!\n"); |
| 968 | goto out; |
| 969 | } |
| 970 | |
| 971 | for (;;) { |
| 972 | nb_sectors = sectors_to_process(total_sectors, sector_num); |
| 973 | if (nb_sectors <= 0) { |
| 974 | break; |
| 975 | } |
| 976 | allocated1 = bdrv_is_allocated_above(bs1, NULL, sector_num, nb_sectors, |
| 977 | &pnum1); |
| 978 | if (allocated1 < 0) { |
| 979 | ret = 3; |
| 980 | error_report("Sector allocation test failed for %s", filename1); |
| 981 | goto out; |
| 982 | } |
| 983 | |
| 984 | allocated2 = bdrv_is_allocated_above(bs2, NULL, sector_num, nb_sectors, |
| 985 | &pnum2); |
| 986 | if (allocated2 < 0) { |
| 987 | ret = 3; |
| 988 | error_report("Sector allocation test failed for %s", filename2); |
| 989 | goto out; |
| 990 | } |
| 991 | nb_sectors = MIN(pnum1, pnum2); |
| 992 | |
| 993 | if (allocated1 == allocated2) { |
| 994 | if (allocated1) { |
| 995 | ret = bdrv_read(bs1, sector_num, buf1, nb_sectors); |
| 996 | if (ret < 0) { |
| 997 | error_report("Error while reading offset %" PRId64 " of %s:" |
| 998 | " %s", sectors_to_bytes(sector_num), filename1, |
| 999 | strerror(-ret)); |
| 1000 | ret = 4; |
| 1001 | goto out; |
| 1002 | } |
| 1003 | ret = bdrv_read(bs2, sector_num, buf2, nb_sectors); |
| 1004 | if (ret < 0) { |
| 1005 | error_report("Error while reading offset %" PRId64 |
| 1006 | " of %s: %s", sectors_to_bytes(sector_num), |
| 1007 | filename2, strerror(-ret)); |
| 1008 | ret = 4; |
| 1009 | goto out; |
| 1010 | } |
| 1011 | ret = compare_sectors(buf1, buf2, nb_sectors, &pnum); |
| 1012 | if (ret || pnum != nb_sectors) { |
| 1013 | ret = 1; |
| 1014 | qprintf(quiet, "Content mismatch at offset %" PRId64 "!\n", |
| 1015 | sectors_to_bytes( |
| 1016 | ret ? sector_num : sector_num + pnum)); |
| 1017 | goto out; |
| 1018 | } |
| 1019 | } |
| 1020 | } else { |
| 1021 | if (strict) { |
| 1022 | ret = 1; |
| 1023 | qprintf(quiet, "Strict mode: Offset %" PRId64 |
| 1024 | " allocation mismatch!\n", |
| 1025 | sectors_to_bytes(sector_num)); |
| 1026 | goto out; |
| 1027 | } |
| 1028 | |
| 1029 | if (allocated1) { |
| 1030 | ret = check_empty_sectors(bs1, sector_num, nb_sectors, |
| 1031 | filename1, buf1, quiet); |
| 1032 | } else { |
| 1033 | ret = check_empty_sectors(bs2, sector_num, nb_sectors, |
| 1034 | filename2, buf1, quiet); |
| 1035 | } |
| 1036 | if (ret) { |
| 1037 | if (ret < 0) { |
| 1038 | ret = 4; |
| 1039 | error_report("Error while reading offset %" PRId64 ": %s", |
| 1040 | sectors_to_bytes(sector_num), strerror(-ret)); |
| 1041 | } |
| 1042 | goto out; |
| 1043 | } |
| 1044 | } |
| 1045 | sector_num += nb_sectors; |
| 1046 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); |
| 1047 | } |
| 1048 | |
| 1049 | if (total_sectors1 != total_sectors2) { |
| 1050 | BlockDriverState *bs_over; |
| 1051 | int64_t total_sectors_over; |
| 1052 | const char *filename_over; |
| 1053 | |
| 1054 | qprintf(quiet, "Warning: Image size mismatch!\n"); |
| 1055 | if (total_sectors1 > total_sectors2) { |
| 1056 | total_sectors_over = total_sectors1; |
| 1057 | bs_over = bs1; |
| 1058 | filename_over = filename1; |
| 1059 | } else { |
| 1060 | total_sectors_over = total_sectors2; |
| 1061 | bs_over = bs2; |
| 1062 | filename_over = filename2; |
| 1063 | } |
| 1064 | |
| 1065 | for (;;) { |
| 1066 | nb_sectors = sectors_to_process(total_sectors_over, sector_num); |
| 1067 | if (nb_sectors <= 0) { |
| 1068 | break; |
| 1069 | } |
| 1070 | ret = bdrv_is_allocated_above(bs_over, NULL, sector_num, |
| 1071 | nb_sectors, &pnum); |
| 1072 | if (ret < 0) { |
| 1073 | ret = 3; |
| 1074 | error_report("Sector allocation test failed for %s", |
| 1075 | filename_over); |
| 1076 | goto out; |
| 1077 | |
| 1078 | } |
| 1079 | nb_sectors = pnum; |
| 1080 | if (ret) { |
| 1081 | ret = check_empty_sectors(bs_over, sector_num, nb_sectors, |
| 1082 | filename_over, buf1, quiet); |
| 1083 | if (ret) { |
| 1084 | if (ret < 0) { |
| 1085 | ret = 4; |
| 1086 | error_report("Error while reading offset %" PRId64 |
| 1087 | " of %s: %s", sectors_to_bytes(sector_num), |
| 1088 | filename_over, strerror(-ret)); |
| 1089 | } |
| 1090 | goto out; |
| 1091 | } |
| 1092 | } |
| 1093 | sector_num += nb_sectors; |
| 1094 | qemu_progress_print(((float) nb_sectors / progress_base)*100, 100); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | qprintf(quiet, "Images are identical.\n"); |
| 1099 | ret = 0; |
| 1100 | |
| 1101 | out: |
| 1102 | bdrv_delete(bs2); |
| 1103 | qemu_vfree(buf1); |
| 1104 | qemu_vfree(buf2); |
| 1105 | out2: |
| 1106 | bdrv_delete(bs1); |
| 1107 | out3: |
| 1108 | qemu_progress_end(); |
| 1109 | return ret; |
| 1110 | } |
| 1111 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1112 | static int img_convert(int argc, char **argv) |
| 1113 | { |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1114 | int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1115 | int progress = 0, flags; |
| 1116 | const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1117 | BlockDriver *drv, *proto_drv; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1118 | BlockDriverState **bs = NULL, *out_bs = NULL; |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 1119 | int64_t total_sectors, nb_sectors, sector_num, bs_offset; |
| 1120 | uint64_t bs_sectors; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1121 | uint8_t * buf = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1122 | const uint8_t *buf1; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1123 | BlockDriverInfo bdi; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1124 | QEMUOptionParameter *param = NULL, *create_options = NULL; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 1125 | QEMUOptionParameter *out_baseimg_param; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1126 | char *options = NULL; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1127 | const char *snapshot_name = NULL; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 1128 | float local_progress = 0; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1129 | int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1130 | bool quiet = false; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1131 | |
| 1132 | fmt = NULL; |
| 1133 | out_fmt = "raw"; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1134 | cache = "unsafe"; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1135 | out_baseimg = NULL; |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1136 | compress = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1137 | for(;;) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1138 | c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:q"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1139 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1140 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1141 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1142 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1143 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1144 | case 'h': |
| 1145 | help(); |
| 1146 | break; |
| 1147 | case 'f': |
| 1148 | fmt = optarg; |
| 1149 | break; |
| 1150 | case 'O': |
| 1151 | out_fmt = optarg; |
| 1152 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1153 | case 'B': |
| 1154 | out_baseimg = optarg; |
| 1155 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1156 | case 'c': |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1157 | compress = 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1158 | break; |
| 1159 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 1160 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1161 | "encryption\' instead!"); |
| 1162 | return 1; |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 1163 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 1164 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1165 | "compat6\' instead!"); |
| 1166 | return 1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1167 | case 'o': |
| 1168 | options = optarg; |
| 1169 | break; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1170 | case 's': |
| 1171 | snapshot_name = optarg; |
| 1172 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1173 | case 'S': |
| 1174 | { |
| 1175 | int64_t sval; |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 1176 | char *end; |
| 1177 | sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B); |
| 1178 | if (sval < 0 || *end) { |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1179 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
| 1180 | return 1; |
| 1181 | } |
| 1182 | |
| 1183 | min_sparse = sval / BDRV_SECTOR_SIZE; |
| 1184 | break; |
| 1185 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1186 | case 'p': |
| 1187 | progress = 1; |
| 1188 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1189 | case 't': |
| 1190 | cache = optarg; |
| 1191 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1192 | case 'q': |
| 1193 | quiet = true; |
| 1194 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1195 | } |
| 1196 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1197 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1198 | if (quiet) { |
| 1199 | progress = 0; |
| 1200 | } |
| 1201 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1202 | bs_n = argc - optind - 1; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1203 | if (bs_n < 1) { |
| 1204 | help(); |
| 1205 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1206 | |
| 1207 | out_filename = argv[argc - 1]; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1208 | |
Charles Arnold | fa170c1 | 2012-05-11 10:57:54 -0600 | [diff] [blame] | 1209 | /* Initialize before goto out */ |
| 1210 | qemu_progress_init(progress, 2.0); |
| 1211 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 1212 | if (options && is_help_option(options)) { |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 1213 | ret = print_block_option_help(out_filename, out_fmt); |
| 1214 | goto out; |
| 1215 | } |
| 1216 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1217 | if (bs_n > 1 && out_baseimg) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1218 | error_report("-B makes no sense when concatenating multiple input " |
| 1219 | "images"); |
Jes Sorensen | 31ca34b | 2010-12-06 15:25:36 +0100 | [diff] [blame] | 1220 | ret = -1; |
| 1221 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1222 | } |
Dong Xu Wang | f8111c2 | 2012-03-15 20:13:31 +0800 | [diff] [blame] | 1223 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1224 | qemu_progress_print(0, 100); |
| 1225 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1226 | bs = g_malloc0(bs_n * sizeof(BlockDriverState *)); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1227 | |
| 1228 | total_sectors = 0; |
| 1229 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1230 | bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true, |
| 1231 | quiet); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1232 | if (!bs[bs_i]) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1233 | error_report("Could not open '%s'", argv[optind + bs_i]); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1234 | ret = -1; |
| 1235 | goto out; |
| 1236 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1237 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 1238 | total_sectors += bs_sectors; |
| 1239 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1240 | |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1241 | if (snapshot_name != NULL) { |
| 1242 | if (bs_n > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1243 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1244 | ret = -1; |
| 1245 | goto out; |
| 1246 | } |
| 1247 | if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 1248 | error_report("Failed to load snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 1249 | ret = -1; |
| 1250 | goto out; |
| 1251 | } |
| 1252 | } |
| 1253 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1254 | /* Find driver and parse its options */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1255 | drv = bdrv_find_format(out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1256 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1257 | error_report("Unknown file format '%s'", out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1258 | ret = -1; |
| 1259 | goto out; |
| 1260 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1261 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1262 | proto_drv = bdrv_find_protocol(out_filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1263 | if (!proto_drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1264 | error_report("Unknown protocol '%s'", out_filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1265 | ret = -1; |
| 1266 | goto out; |
| 1267 | } |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1268 | |
| 1269 | create_options = append_option_parameters(create_options, |
| 1270 | drv->create_options); |
| 1271 | create_options = append_option_parameters(create_options, |
| 1272 | proto_drv->create_options); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 1273 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1274 | if (options) { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1275 | param = parse_option_parameters(options, create_options, param); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1276 | if (param == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1277 | error_report("Invalid options for file format '%s'.", out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1278 | ret = -1; |
| 1279 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1280 | } |
| 1281 | } else { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 1282 | param = parse_option_parameters("", create_options, param); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1283 | } |
| 1284 | |
| 1285 | set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512); |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1286 | ret = add_old_style_options(out_fmt, param, out_baseimg, NULL); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1287 | if (ret < 0) { |
| 1288 | goto out; |
| 1289 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1290 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 1291 | /* Get backing file name if -o backing_file was used */ |
| 1292 | out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
| 1293 | if (out_baseimg_param) { |
| 1294 | out_baseimg = out_baseimg_param->value.s; |
| 1295 | } |
| 1296 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1297 | /* Check if compression is supported */ |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1298 | if (compress) { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1299 | QEMUOptionParameter *encryption = |
| 1300 | get_option_parameter(param, BLOCK_OPT_ENCRYPT); |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 1301 | QEMUOptionParameter *preallocation = |
| 1302 | get_option_parameter(param, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1303 | |
| 1304 | if (!drv->bdrv_write_compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1305 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1306 | ret = -1; |
| 1307 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1308 | } |
| 1309 | |
| 1310 | if (encryption && encryption->value.n) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1311 | error_report("Compression and encryption not supported at " |
| 1312 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1313 | ret = -1; |
| 1314 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1315 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 1316 | |
| 1317 | if (preallocation && preallocation->value.s |
| 1318 | && strcmp(preallocation->value.s, "off")) |
| 1319 | { |
| 1320 | error_report("Compression and preallocation not supported at " |
| 1321 | "the same time"); |
| 1322 | ret = -1; |
| 1323 | goto out; |
| 1324 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 1325 | } |
| 1326 | |
| 1327 | /* Create the new image */ |
| 1328 | ret = bdrv_create(drv, out_filename, param); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1329 | if (ret < 0) { |
| 1330 | if (ret == -ENOTSUP) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1331 | error_report("Formatting not supported for file format '%s'", |
| 1332 | out_fmt); |
aurel32 | 6e9ea0c | 2009-04-15 14:42:46 +0000 | [diff] [blame] | 1333 | } else if (ret == -EFBIG) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1334 | error_report("The image size is too large for file format '%s'", |
| 1335 | out_fmt); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1336 | } else { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1337 | error_report("%s: error while converting %s: %s", |
| 1338 | out_filename, out_fmt, strerror(-ret)); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1339 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1340 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1341 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1342 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1343 | flags = BDRV_O_RDWR; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 1344 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1345 | if (ret < 0) { |
| 1346 | error_report("Invalid cache option: %s", cache); |
| 1347 | return -1; |
| 1348 | } |
| 1349 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1350 | out_bs = bdrv_new_open(out_filename, out_fmt, flags, true, quiet); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1351 | if (!out_bs) { |
| 1352 | ret = -1; |
| 1353 | goto out; |
| 1354 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1355 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1356 | bs_i = 0; |
| 1357 | bs_offset = 0; |
| 1358 | bdrv_get_geometry(bs[0], &bs_sectors); |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 1359 | buf = qemu_blockalign(out_bs, IO_BUF_SIZE); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1360 | |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1361 | if (compress) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1362 | ret = bdrv_get_info(out_bs, &bdi); |
| 1363 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1364 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1365 | goto out; |
| 1366 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1367 | cluster_size = bdi.cluster_size; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1368 | if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1369 | error_report("invalid cluster size"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1370 | ret = -1; |
| 1371 | goto out; |
| 1372 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1373 | cluster_sectors = cluster_size >> 9; |
| 1374 | sector_num = 0; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1375 | |
| 1376 | nb_sectors = total_sectors; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 1377 | if (nb_sectors != 0) { |
| 1378 | local_progress = (float)100 / |
| 1379 | (nb_sectors / MIN(nb_sectors, cluster_sectors)); |
| 1380 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1381 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1382 | for(;;) { |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1383 | int64_t bs_num; |
| 1384 | int remainder; |
| 1385 | uint8_t *buf2; |
| 1386 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1387 | nb_sectors = total_sectors - sector_num; |
| 1388 | if (nb_sectors <= 0) |
| 1389 | break; |
| 1390 | if (nb_sectors >= cluster_sectors) |
| 1391 | n = cluster_sectors; |
| 1392 | else |
| 1393 | n = nb_sectors; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1394 | |
| 1395 | bs_num = sector_num - bs_offset; |
| 1396 | assert (bs_num >= 0); |
| 1397 | remainder = n; |
| 1398 | buf2 = buf; |
| 1399 | while (remainder > 0) { |
| 1400 | int nlow; |
| 1401 | while (bs_num == bs_sectors) { |
| 1402 | bs_i++; |
| 1403 | assert (bs_i < bs_n); |
| 1404 | bs_offset += bs_sectors; |
| 1405 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 1406 | bs_num = 0; |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 1407 | /* printf("changing part: sector_num=%" PRId64 ", " |
| 1408 | "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64 |
| 1409 | "\n", sector_num, bs_i, bs_offset, bs_sectors); */ |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1410 | } |
| 1411 | assert (bs_num < bs_sectors); |
| 1412 | |
| 1413 | nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder; |
| 1414 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1415 | ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow); |
| 1416 | if (ret < 0) { |
Stefan Hajnoczi | 3fba9d8 | 2011-08-17 17:41:09 +0100 | [diff] [blame] | 1417 | error_report("error while reading sector %" PRId64 ": %s", |
| 1418 | bs_num, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1419 | goto out; |
| 1420 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1421 | |
| 1422 | buf2 += nlow * 512; |
| 1423 | bs_num += nlow; |
| 1424 | |
| 1425 | remainder -= nlow; |
| 1426 | } |
| 1427 | assert (remainder == 0); |
| 1428 | |
Stefan Hajnoczi | 54f106d | 2013-04-15 17:17:33 +0200 | [diff] [blame] | 1429 | if (!buffer_is_zero(buf, n * BDRV_SECTOR_SIZE)) { |
| 1430 | ret = bdrv_write_compressed(out_bs, sector_num, buf, n); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1431 | if (ret != 0) { |
Stefan Hajnoczi | 3fba9d8 | 2011-08-17 17:41:09 +0100 | [diff] [blame] | 1432 | error_report("error while compressing sector %" PRId64 |
| 1433 | ": %s", sector_num, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1434 | goto out; |
| 1435 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1436 | } |
| 1437 | sector_num += n; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1438 | qemu_progress_print(local_progress, 100); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1439 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1440 | /* signal EOF to align */ |
| 1441 | bdrv_write_compressed(out_bs, 0, NULL, 0); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1442 | } else { |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1443 | int has_zero_init = bdrv_has_zero_init(out_bs); |
| 1444 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1445 | sector_num = 0; // total number of sectors converted so far |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1446 | nb_sectors = total_sectors - sector_num; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 1447 | if (nb_sectors != 0) { |
| 1448 | local_progress = (float)100 / |
| 1449 | (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512)); |
| 1450 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1451 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1452 | for(;;) { |
| 1453 | nb_sectors = total_sectors - sector_num; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1454 | if (nb_sectors <= 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1455 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1456 | } |
| 1457 | if (nb_sectors >= (IO_BUF_SIZE / 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1458 | n = (IO_BUF_SIZE / 512); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1459 | } else { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1460 | n = nb_sectors; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1461 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1462 | |
| 1463 | while (sector_num - bs_offset >= bs_sectors) { |
| 1464 | bs_i ++; |
| 1465 | assert (bs_i < bs_n); |
| 1466 | bs_offset += bs_sectors; |
| 1467 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 1468 | /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, " |
| 1469 | "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n", |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1470 | sector_num, bs_i, bs_offset, bs_sectors); */ |
| 1471 | } |
| 1472 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1473 | if (n > bs_offset + bs_sectors - sector_num) { |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1474 | n = bs_offset + bs_sectors - sector_num; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1475 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1476 | |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1477 | if (has_zero_init) { |
Akkarit Sangpetch | d032044 | 2009-07-17 10:02:15 +0200 | [diff] [blame] | 1478 | /* If the output image is being created as a copy on write image, |
| 1479 | assume that sectors which are unallocated in the input image |
| 1480 | are present in both the output's and input's base images (no |
| 1481 | need to copy them). */ |
| 1482 | if (out_baseimg) { |
| 1483 | if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, |
| 1484 | n, &n1)) { |
| 1485 | sector_num += n1; |
| 1486 | continue; |
| 1487 | } |
| 1488 | /* The next 'n1' sectors are allocated in the input image. Copy |
| 1489 | only those as they may be followed by unallocated sectors. */ |
| 1490 | n = n1; |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1491 | } |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1492 | } else { |
| 1493 | n1 = n; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1494 | } |
| 1495 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1496 | ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n); |
| 1497 | if (ret < 0) { |
Stefan Hajnoczi | 3fba9d8 | 2011-08-17 17:41:09 +0100 | [diff] [blame] | 1498 | error_report("error while reading sector %" PRId64 ": %s", |
| 1499 | sector_num - bs_offset, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1500 | goto out; |
| 1501 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1502 | /* NOTE: at the same time we convert, we do not write zero |
| 1503 | sectors to have a chance to compress the image. Ideally, we |
| 1504 | should add a specific call to have the info to go faster */ |
| 1505 | buf1 = buf; |
| 1506 | while (n > 0) { |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1507 | /* If the output image is being created as a copy on write image, |
| 1508 | copy all sectors even the ones containing only NUL bytes, |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1509 | because they may differ from the sectors in the base image. |
| 1510 | |
| 1511 | If the output is to a host device, we also write out |
| 1512 | sectors that are entirely 0, since whatever data was |
| 1513 | already there is garbage, not 0s. */ |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1514 | if (!has_zero_init || out_baseimg || |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1515 | is_allocated_sectors_min(buf1, n, &n1, min_sparse)) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1516 | ret = bdrv_write(out_bs, sector_num, buf1, n1); |
| 1517 | if (ret < 0) { |
Stefan Hajnoczi | 3fba9d8 | 2011-08-17 17:41:09 +0100 | [diff] [blame] | 1518 | error_report("error while writing sector %" PRId64 |
| 1519 | ": %s", sector_num, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1520 | goto out; |
| 1521 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1522 | } |
| 1523 | sector_num += n1; |
| 1524 | n -= n1; |
| 1525 | buf1 += n1 * 512; |
| 1526 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1527 | qemu_progress_print(local_progress, 100); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1528 | } |
| 1529 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1530 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1531 | qemu_progress_end(); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1532 | free_option_parameters(create_options); |
| 1533 | free_option_parameters(param); |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 1534 | qemu_vfree(buf); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1535 | if (out_bs) { |
| 1536 | bdrv_delete(out_bs); |
| 1537 | } |
Jes Sorensen | 31ca34b | 2010-12-06 15:25:36 +0100 | [diff] [blame] | 1538 | if (bs) { |
| 1539 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
| 1540 | if (bs[bs_i]) { |
| 1541 | bdrv_delete(bs[bs_i]); |
| 1542 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1543 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1544 | g_free(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1545 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1546 | if (ret) { |
| 1547 | return 1; |
| 1548 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1549 | return 0; |
| 1550 | } |
| 1551 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 1552 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1553 | static void dump_snapshots(BlockDriverState *bs) |
| 1554 | { |
| 1555 | QEMUSnapshotInfo *sn_tab, *sn; |
| 1556 | int nb_sns, i; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1557 | |
| 1558 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 1559 | if (nb_sns <= 0) |
| 1560 | return; |
| 1561 | printf("Snapshot list:\n"); |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame^] | 1562 | bdrv_snapshot_dump(fprintf, stdout, NULL); |
| 1563 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1564 | for(i = 0; i < nb_sns; i++) { |
| 1565 | sn = &sn_tab[i]; |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame^] | 1566 | bdrv_snapshot_dump(fprintf, stdout, sn); |
| 1567 | printf("\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1568 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1569 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1572 | static void dump_json_image_info_list(ImageInfoList *list) |
| 1573 | { |
| 1574 | Error *errp = NULL; |
| 1575 | QString *str; |
| 1576 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 1577 | QObject *obj; |
| 1578 | visit_type_ImageInfoList(qmp_output_get_visitor(ov), |
| 1579 | &list, NULL, &errp); |
| 1580 | obj = qmp_output_get_qobject(ov); |
| 1581 | str = qobject_to_json_pretty(obj); |
| 1582 | assert(str != NULL); |
| 1583 | printf("%s\n", qstring_get_str(str)); |
| 1584 | qobject_decref(obj); |
| 1585 | qmp_output_visitor_cleanup(ov); |
| 1586 | QDECREF(str); |
| 1587 | } |
| 1588 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1589 | static void dump_json_image_info(ImageInfo *info) |
| 1590 | { |
| 1591 | Error *errp = NULL; |
| 1592 | QString *str; |
| 1593 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 1594 | QObject *obj; |
| 1595 | visit_type_ImageInfo(qmp_output_get_visitor(ov), |
| 1596 | &info, NULL, &errp); |
| 1597 | obj = qmp_output_get_qobject(ov); |
| 1598 | str = qobject_to_json_pretty(obj); |
| 1599 | assert(str != NULL); |
| 1600 | printf("%s\n", qstring_get_str(str)); |
| 1601 | qobject_decref(obj); |
| 1602 | qmp_output_visitor_cleanup(ov); |
| 1603 | QDECREF(str); |
| 1604 | } |
| 1605 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1606 | static void dump_human_image_info_list(ImageInfoList *list) |
| 1607 | { |
| 1608 | ImageInfoList *elem; |
| 1609 | bool delim = false; |
| 1610 | |
| 1611 | for (elem = list; elem; elem = elem->next) { |
| 1612 | if (delim) { |
| 1613 | printf("\n"); |
| 1614 | } |
| 1615 | delim = true; |
| 1616 | |
Wenchao Xia | 5b91704 | 2013-05-25 11:09:45 +0800 | [diff] [blame^] | 1617 | bdrv_image_info_dump(fprintf, stdout, elem->value); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1618 | } |
| 1619 | } |
| 1620 | |
| 1621 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 1622 | { |
| 1623 | return strcmp(a, b) == 0; |
| 1624 | } |
| 1625 | |
| 1626 | /** |
| 1627 | * Open an image file chain and return an ImageInfoList |
| 1628 | * |
| 1629 | * @filename: topmost image filename |
| 1630 | * @fmt: topmost image format (may be NULL to autodetect) |
| 1631 | * @chain: true - enumerate entire backing file chain |
| 1632 | * false - only topmost image file |
| 1633 | * |
| 1634 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 1635 | * image file. If there was an error a message will have been printed to |
| 1636 | * stderr. |
| 1637 | */ |
| 1638 | static ImageInfoList *collect_image_info_list(const char *filename, |
| 1639 | const char *fmt, |
| 1640 | bool chain) |
| 1641 | { |
| 1642 | ImageInfoList *head = NULL; |
| 1643 | ImageInfoList **last = &head; |
| 1644 | GHashTable *filenames; |
| 1645 | |
| 1646 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 1647 | |
| 1648 | while (filename) { |
| 1649 | BlockDriverState *bs; |
| 1650 | ImageInfo *info; |
| 1651 | ImageInfoList *elem; |
| 1652 | |
| 1653 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 1654 | error_report("Backing file '%s' creates an infinite loop.", |
| 1655 | filename); |
| 1656 | goto err; |
| 1657 | } |
| 1658 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 1659 | |
| 1660 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING, |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1661 | false, false); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1662 | if (!bs) { |
| 1663 | goto err; |
| 1664 | } |
| 1665 | |
| 1666 | info = g_new0(ImageInfo, 1); |
Wenchao Xia | f364ec6 | 2013-05-25 11:09:44 +0800 | [diff] [blame] | 1667 | bdrv_collect_image_info(bs, info, filename); |
| 1668 | bdrv_collect_snapshots(bs, info); |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1669 | |
| 1670 | elem = g_new0(ImageInfoList, 1); |
| 1671 | elem->value = info; |
| 1672 | *last = elem; |
| 1673 | last = &elem->next; |
| 1674 | |
| 1675 | bdrv_delete(bs); |
| 1676 | |
| 1677 | filename = fmt = NULL; |
| 1678 | if (chain) { |
| 1679 | if (info->has_full_backing_filename) { |
| 1680 | filename = info->full_backing_filename; |
| 1681 | } else if (info->has_backing_filename) { |
| 1682 | filename = info->backing_filename; |
| 1683 | } |
| 1684 | if (info->has_backing_filename_format) { |
| 1685 | fmt = info->backing_filename_format; |
| 1686 | } |
| 1687 | } |
| 1688 | } |
| 1689 | g_hash_table_destroy(filenames); |
| 1690 | return head; |
| 1691 | |
| 1692 | err: |
| 1693 | qapi_free_ImageInfoList(head); |
| 1694 | g_hash_table_destroy(filenames); |
| 1695 | return NULL; |
| 1696 | } |
| 1697 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1698 | static int img_info(int argc, char **argv) |
| 1699 | { |
| 1700 | int c; |
| 1701 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1702 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1703 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1704 | ImageInfoList *list; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1705 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1706 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1707 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1708 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1709 | int option_index = 0; |
| 1710 | static const struct option long_options[] = { |
| 1711 | {"help", no_argument, 0, 'h'}, |
| 1712 | {"format", required_argument, 0, 'f'}, |
| 1713 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1714 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1715 | {0, 0, 0, 0} |
| 1716 | }; |
| 1717 | c = getopt_long(argc, argv, "f:h", |
| 1718 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1719 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1720 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1721 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1722 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1723 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1724 | case 'h': |
| 1725 | help(); |
| 1726 | break; |
| 1727 | case 'f': |
| 1728 | fmt = optarg; |
| 1729 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1730 | case OPTION_OUTPUT: |
| 1731 | output = optarg; |
| 1732 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1733 | case OPTION_BACKING_CHAIN: |
| 1734 | chain = true; |
| 1735 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1736 | } |
| 1737 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1738 | if (optind >= argc) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1739 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1740 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1741 | filename = argv[optind++]; |
| 1742 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1743 | if (output && !strcmp(output, "json")) { |
| 1744 | output_format = OFORMAT_JSON; |
| 1745 | } else if (output && !strcmp(output, "human")) { |
| 1746 | output_format = OFORMAT_HUMAN; |
| 1747 | } else if (output) { |
| 1748 | error_report("--output must be used with human or json as argument."); |
| 1749 | return 1; |
| 1750 | } |
| 1751 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1752 | list = collect_image_info_list(filename, fmt, chain); |
| 1753 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1754 | return 1; |
| 1755 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1756 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1757 | switch (output_format) { |
| 1758 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1759 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1760 | break; |
| 1761 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1762 | if (chain) { |
| 1763 | dump_json_image_info_list(list); |
| 1764 | } else { |
| 1765 | dump_json_image_info(list->value); |
| 1766 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1767 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1768 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1769 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1770 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1771 | return 0; |
| 1772 | } |
| 1773 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1774 | #define SNAPSHOT_LIST 1 |
| 1775 | #define SNAPSHOT_CREATE 2 |
| 1776 | #define SNAPSHOT_APPLY 3 |
| 1777 | #define SNAPSHOT_DELETE 4 |
| 1778 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1779 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1780 | { |
| 1781 | BlockDriverState *bs; |
| 1782 | QEMUSnapshotInfo sn; |
| 1783 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1784 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1785 | int action = 0; |
| 1786 | qemu_timeval tv; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1787 | bool quiet = false; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1788 | |
Kevin Wolf | 710da70 | 2011-01-10 12:33:02 +0100 | [diff] [blame] | 1789 | bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1790 | /* Parse commandline parameters */ |
| 1791 | for(;;) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1792 | c = getopt(argc, argv, "la:c:d:hq"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1793 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1794 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1795 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1796 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1797 | case '?': |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1798 | case 'h': |
| 1799 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1800 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1801 | case 'l': |
| 1802 | if (action) { |
| 1803 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1804 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1805 | } |
| 1806 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 1807 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1808 | break; |
| 1809 | case 'a': |
| 1810 | if (action) { |
| 1811 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1812 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1813 | } |
| 1814 | action = SNAPSHOT_APPLY; |
| 1815 | snapshot_name = optarg; |
| 1816 | break; |
| 1817 | case 'c': |
| 1818 | if (action) { |
| 1819 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1820 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1821 | } |
| 1822 | action = SNAPSHOT_CREATE; |
| 1823 | snapshot_name = optarg; |
| 1824 | break; |
| 1825 | case 'd': |
| 1826 | if (action) { |
| 1827 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1828 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1829 | } |
| 1830 | action = SNAPSHOT_DELETE; |
| 1831 | snapshot_name = optarg; |
| 1832 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1833 | case 'q': |
| 1834 | quiet = true; |
| 1835 | break; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1836 | } |
| 1837 | } |
| 1838 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1839 | if (optind >= argc) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1840 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1841 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1842 | filename = argv[optind++]; |
| 1843 | |
| 1844 | /* Open the image */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1845 | bs = bdrv_new_open(filename, NULL, bdrv_oflags, true, quiet); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1846 | if (!bs) { |
| 1847 | return 1; |
| 1848 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1849 | |
| 1850 | /* Perform the requested action */ |
| 1851 | switch(action) { |
| 1852 | case SNAPSHOT_LIST: |
| 1853 | dump_snapshots(bs); |
| 1854 | break; |
| 1855 | |
| 1856 | case SNAPSHOT_CREATE: |
| 1857 | memset(&sn, 0, sizeof(sn)); |
| 1858 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 1859 | |
| 1860 | qemu_gettimeofday(&tv); |
| 1861 | sn.date_sec = tv.tv_sec; |
| 1862 | sn.date_nsec = tv.tv_usec * 1000; |
| 1863 | |
| 1864 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1865 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1866 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1867 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1868 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1869 | break; |
| 1870 | |
| 1871 | case SNAPSHOT_APPLY: |
| 1872 | ret = bdrv_snapshot_goto(bs, snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1873 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1874 | error_report("Could not apply snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1875 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1876 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1877 | break; |
| 1878 | |
| 1879 | case SNAPSHOT_DELETE: |
| 1880 | ret = bdrv_snapshot_delete(bs, snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1881 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1882 | error_report("Could not delete snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1883 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1884 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1885 | break; |
| 1886 | } |
| 1887 | |
| 1888 | /* Cleanup */ |
| 1889 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1890 | if (ret) { |
| 1891 | return 1; |
| 1892 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1893 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1896 | static int img_rebase(int argc, char **argv) |
| 1897 | { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1898 | BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL; |
Stefan Hajnoczi | f163d07 | 2010-04-13 10:29:34 +0100 | [diff] [blame] | 1899 | BlockDriver *old_backing_drv, *new_backing_drv; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1900 | char *filename; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1901 | const char *fmt, *cache, *out_basefmt, *out_baseimg; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1902 | int c, flags, ret; |
| 1903 | int unsafe = 0; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1904 | int progress = 0; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1905 | bool quiet = false; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1906 | |
| 1907 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 1908 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1909 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1910 | out_baseimg = NULL; |
| 1911 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1912 | for(;;) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1913 | c = getopt(argc, argv, "uhf:F:b:pt:q"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1914 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1915 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1916 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1917 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1918 | case '?': |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1919 | case 'h': |
| 1920 | help(); |
| 1921 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 1922 | case 'f': |
| 1923 | fmt = optarg; |
| 1924 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1925 | case 'F': |
| 1926 | out_basefmt = optarg; |
| 1927 | break; |
| 1928 | case 'b': |
| 1929 | out_baseimg = optarg; |
| 1930 | break; |
| 1931 | case 'u': |
| 1932 | unsafe = 1; |
| 1933 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1934 | case 'p': |
| 1935 | progress = 1; |
| 1936 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1937 | case 't': |
| 1938 | cache = optarg; |
| 1939 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1940 | case 'q': |
| 1941 | quiet = true; |
| 1942 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1943 | } |
| 1944 | } |
| 1945 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1946 | if (quiet) { |
| 1947 | progress = 0; |
| 1948 | } |
| 1949 | |
Anthony Liguori | 9a9d9db | 2011-04-13 15:51:47 +0100 | [diff] [blame] | 1950 | if ((optind >= argc) || (!unsafe && !out_baseimg)) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1951 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1952 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1953 | filename = argv[optind++]; |
| 1954 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1955 | qemu_progress_init(progress, 2.0); |
| 1956 | qemu_progress_print(0, 100); |
| 1957 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1958 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 1959 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1960 | if (ret < 0) { |
| 1961 | error_report("Invalid cache option: %s", cache); |
| 1962 | return -1; |
| 1963 | } |
| 1964 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1965 | /* |
| 1966 | * Open the images. |
| 1967 | * |
| 1968 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 1969 | * the reference to a renamed or moved backing file. |
| 1970 | */ |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 1971 | bs = bdrv_new_open(filename, fmt, flags, true, quiet); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1972 | if (!bs) { |
| 1973 | return 1; |
| 1974 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1975 | |
| 1976 | /* Find the right drivers for the backing files */ |
| 1977 | old_backing_drv = NULL; |
| 1978 | new_backing_drv = NULL; |
| 1979 | |
| 1980 | if (!unsafe && bs->backing_format[0] != '\0') { |
| 1981 | old_backing_drv = bdrv_find_format(bs->backing_format); |
| 1982 | if (old_backing_drv == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1983 | error_report("Invalid format name: '%s'", bs->backing_format); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1984 | ret = -1; |
| 1985 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1986 | } |
| 1987 | } |
| 1988 | |
| 1989 | if (out_basefmt != NULL) { |
| 1990 | new_backing_drv = bdrv_find_format(out_basefmt); |
| 1991 | if (new_backing_drv == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1992 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1993 | ret = -1; |
| 1994 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1995 | } |
| 1996 | } |
| 1997 | |
| 1998 | /* For safe rebasing we need to compare old and new backing file */ |
| 1999 | if (unsafe) { |
| 2000 | /* Make the compiler happy */ |
| 2001 | bs_old_backing = NULL; |
| 2002 | bs_new_backing = NULL; |
| 2003 | } else { |
| 2004 | char backing_name[1024]; |
| 2005 | |
| 2006 | bs_old_backing = bdrv_new("old_backing"); |
| 2007 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 2008 | ret = bdrv_open(bs_old_backing, backing_name, NULL, BDRV_O_FLAGS, |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2009 | old_backing_drv); |
| 2010 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2011 | error_report("Could not open old backing file '%s'", backing_name); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2012 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2013 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2014 | if (out_baseimg[0]) { |
| 2015 | bs_new_backing = bdrv_new("new_backing"); |
Kevin Wolf | de9c0ce | 2013-03-15 10:35:02 +0100 | [diff] [blame] | 2016 | ret = bdrv_open(bs_new_backing, out_baseimg, NULL, BDRV_O_FLAGS, |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2017 | new_backing_drv); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2018 | if (ret) { |
| 2019 | error_report("Could not open new backing file '%s'", |
| 2020 | out_baseimg); |
| 2021 | goto out; |
| 2022 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2023 | } |
| 2024 | } |
| 2025 | |
| 2026 | /* |
| 2027 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 2028 | * accesses go to the backing file. We must therefore compare this cluster |
| 2029 | * in the old and new backing file, and if they differ we need to copy it |
| 2030 | * from the old backing file into the COW file. |
| 2031 | * |
| 2032 | * If qemu-img crashes during this step, no harm is done. The content of |
| 2033 | * the image is the same as the original one at any time. |
| 2034 | */ |
| 2035 | if (!unsafe) { |
| 2036 | uint64_t num_sectors; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 2037 | uint64_t old_backing_num_sectors; |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2038 | uint64_t new_backing_num_sectors = 0; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2039 | uint64_t sector; |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 2040 | int n; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 2041 | uint8_t * buf_old; |
| 2042 | uint8_t * buf_new; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 2043 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 2044 | |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 2045 | buf_old = qemu_blockalign(bs, IO_BUF_SIZE); |
| 2046 | buf_new = qemu_blockalign(bs, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2047 | |
| 2048 | bdrv_get_geometry(bs, &num_sectors); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 2049 | bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2050 | if (bs_new_backing) { |
| 2051 | bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors); |
| 2052 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2053 | |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 2054 | if (num_sectors != 0) { |
| 2055 | local_progress = (float)100 / |
| 2056 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); |
| 2057 | } |
| 2058 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2059 | for (sector = 0; sector < num_sectors; sector += n) { |
| 2060 | |
| 2061 | /* How many sectors can we handle with the next read? */ |
| 2062 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { |
| 2063 | n = (IO_BUF_SIZE / 512); |
| 2064 | } else { |
| 2065 | n = num_sectors - sector; |
| 2066 | } |
| 2067 | |
| 2068 | /* If the cluster is allocated, we don't need to take action */ |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 2069 | ret = bdrv_is_allocated(bs, sector, n, &n); |
| 2070 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2071 | continue; |
| 2072 | } |
| 2073 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 2074 | /* |
| 2075 | * Read old and new backing file and take into consideration that |
| 2076 | * backing files may be smaller than the COW image. |
| 2077 | */ |
| 2078 | if (sector >= old_backing_num_sectors) { |
| 2079 | memset(buf_old, 0, n * BDRV_SECTOR_SIZE); |
| 2080 | } else { |
| 2081 | if (sector + n > old_backing_num_sectors) { |
| 2082 | n = old_backing_num_sectors - sector; |
| 2083 | } |
| 2084 | |
| 2085 | ret = bdrv_read(bs_old_backing, sector, buf_old, n); |
| 2086 | if (ret < 0) { |
| 2087 | error_report("error while reading from old backing file"); |
| 2088 | goto out; |
| 2089 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2090 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 2091 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2092 | if (sector >= new_backing_num_sectors || !bs_new_backing) { |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 2093 | memset(buf_new, 0, n * BDRV_SECTOR_SIZE); |
| 2094 | } else { |
| 2095 | if (sector + n > new_backing_num_sectors) { |
| 2096 | n = new_backing_num_sectors - sector; |
| 2097 | } |
| 2098 | |
| 2099 | ret = bdrv_read(bs_new_backing, sector, buf_new, n); |
| 2100 | if (ret < 0) { |
| 2101 | error_report("error while reading from new backing file"); |
| 2102 | goto out; |
| 2103 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2104 | } |
| 2105 | |
| 2106 | /* If they differ, we need to write to the COW file */ |
| 2107 | uint64_t written = 0; |
| 2108 | |
| 2109 | while (written < n) { |
| 2110 | int pnum; |
| 2111 | |
| 2112 | if (compare_sectors(buf_old + written * 512, |
Kevin Wolf | 60b1bd4 | 2010-02-17 12:32:59 +0100 | [diff] [blame] | 2113 | buf_new + written * 512, n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2114 | { |
| 2115 | ret = bdrv_write(bs, sector + written, |
| 2116 | buf_old + written * 512, pnum); |
| 2117 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2118 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2119 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2120 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | written += pnum; |
| 2125 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2126 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2127 | } |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 2128 | |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 2129 | qemu_vfree(buf_old); |
| 2130 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2131 | } |
| 2132 | |
| 2133 | /* |
| 2134 | * Change the backing file. All clusters that are different from the old |
| 2135 | * backing file are overwritten in the COW file now, so the visible content |
| 2136 | * doesn't change when we switch the backing file. |
| 2137 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 2138 | if (out_baseimg && *out_baseimg) { |
| 2139 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 2140 | } else { |
| 2141 | ret = bdrv_change_backing_file(bs, NULL, NULL); |
| 2142 | } |
| 2143 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2144 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2145 | error_report("Could not change the backing file to '%s': No " |
| 2146 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2147 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2148 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2149 | out_baseimg, strerror(-ret)); |
| 2150 | } |
| 2151 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2152 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2153 | /* |
| 2154 | * TODO At this point it is possible to check if any clusters that are |
| 2155 | * allocated in the COW file are the same in the backing file. If so, they |
| 2156 | * could be dropped from the COW file. Don't do this before switching the |
| 2157 | * backing file, in case of a crash this would lead to corruption. |
| 2158 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2159 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 2160 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2161 | /* Cleanup */ |
| 2162 | if (!unsafe) { |
Kevin Wolf | eb863ad | 2011-03-31 12:39:51 +0200 | [diff] [blame] | 2163 | if (bs_old_backing != NULL) { |
| 2164 | bdrv_delete(bs_old_backing); |
| 2165 | } |
| 2166 | if (bs_new_backing != NULL) { |
| 2167 | bdrv_delete(bs_new_backing); |
| 2168 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2172 | if (ret) { |
| 2173 | return 1; |
| 2174 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 2175 | return 0; |
| 2176 | } |
| 2177 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2178 | static int img_resize(int argc, char **argv) |
| 2179 | { |
| 2180 | int c, ret, relative; |
| 2181 | const char *filename, *fmt, *size; |
| 2182 | int64_t n, total_size; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2183 | bool quiet = false; |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2184 | BlockDriverState *bs = NULL; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 2185 | QemuOpts *param; |
| 2186 | static QemuOptsList resize_options = { |
| 2187 | .name = "resize_options", |
| 2188 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 2189 | .desc = { |
| 2190 | { |
| 2191 | .name = BLOCK_OPT_SIZE, |
| 2192 | .type = QEMU_OPT_SIZE, |
| 2193 | .help = "Virtual disk size" |
| 2194 | }, { |
| 2195 | /* end of list */ |
| 2196 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2197 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2198 | }; |
| 2199 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 2200 | /* Remove size from argv manually so that negative numbers are not treated |
| 2201 | * as options by getopt. */ |
| 2202 | if (argc < 3) { |
| 2203 | help(); |
| 2204 | return 1; |
| 2205 | } |
| 2206 | |
| 2207 | size = argv[--argc]; |
| 2208 | |
| 2209 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2210 | fmt = NULL; |
| 2211 | for(;;) { |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2212 | c = getopt(argc, argv, "f:hq"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2213 | if (c == -1) { |
| 2214 | break; |
| 2215 | } |
| 2216 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2217 | case '?': |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2218 | case 'h': |
| 2219 | help(); |
| 2220 | break; |
| 2221 | case 'f': |
| 2222 | fmt = optarg; |
| 2223 | break; |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2224 | case 'q': |
| 2225 | quiet = true; |
| 2226 | break; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2227 | } |
| 2228 | } |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 2229 | if (optind >= argc) { |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2230 | help(); |
| 2231 | } |
| 2232 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2233 | |
| 2234 | /* Choose grow, shrink, or absolute resize mode */ |
| 2235 | switch (size[0]) { |
| 2236 | case '+': |
| 2237 | relative = 1; |
| 2238 | size++; |
| 2239 | break; |
| 2240 | case '-': |
| 2241 | relative = -1; |
| 2242 | size++; |
| 2243 | break; |
| 2244 | default: |
| 2245 | relative = 0; |
| 2246 | break; |
| 2247 | } |
| 2248 | |
| 2249 | /* Parse size */ |
Dong Xu Wang | e478b44 | 2012-12-06 14:47:22 +0800 | [diff] [blame] | 2250 | param = qemu_opts_create_nofail(&resize_options); |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 2251 | if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) { |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2252 | /* Error message already printed when size parsing fails */ |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2253 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 2254 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2255 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2256 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 2257 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 2258 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2259 | |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2260 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true, quiet); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2261 | if (!bs) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2262 | ret = -1; |
| 2263 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2264 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2265 | |
| 2266 | if (relative) { |
| 2267 | total_size = bdrv_getlength(bs) + n * relative; |
| 2268 | } else { |
| 2269 | total_size = n; |
| 2270 | } |
| 2271 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2272 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2273 | ret = -1; |
| 2274 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | ret = bdrv_truncate(bs, total_size); |
| 2278 | switch (ret) { |
| 2279 | case 0: |
Miroslav Rezanina | f382d43 | 2013-02-13 09:09:40 +0100 | [diff] [blame] | 2280 | qprintf(quiet, "Image resized.\n"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2281 | break; |
| 2282 | case -ENOTSUP: |
Kevin Wolf | 259b217 | 2012-03-06 12:44:45 +0100 | [diff] [blame] | 2283 | error_report("This image does not support resize"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2284 | break; |
| 2285 | case -EACCES: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2286 | error_report("Image is read-only"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2287 | break; |
| 2288 | default: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2289 | error_report("Error resizing image (%d)", -ret); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2290 | break; |
| 2291 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2292 | out: |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2293 | if (bs) { |
| 2294 | bdrv_delete(bs); |
| 2295 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2296 | if (ret) { |
| 2297 | return 1; |
| 2298 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2299 | return 0; |
| 2300 | } |
| 2301 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2302 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2303 | #define DEF(option, callback, arg_string) \ |
| 2304 | { option, callback }, |
| 2305 | #include "qemu-img-cmds.h" |
| 2306 | #undef DEF |
| 2307 | #undef GEN_DOCS |
| 2308 | { NULL, NULL, }, |
| 2309 | }; |
| 2310 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2311 | int main(int argc, char **argv) |
| 2312 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2313 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2314 | const char *cmdname; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2315 | |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 2316 | error_set_progname(argv[0]); |
| 2317 | |
Paolo Bonzini | 2592c59 | 2012-11-03 18:10:17 +0100 | [diff] [blame] | 2318 | qemu_init_main_loop(); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2319 | bdrv_init(); |
| 2320 | if (argc < 2) |
| 2321 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2322 | cmdname = argv[1]; |
aurel32 | 8f9b157 | 2009-02-09 18:14:31 +0000 | [diff] [blame] | 2323 | argc--; argv++; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2324 | |
| 2325 | /* find the command */ |
| 2326 | for(cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 2327 | if (!strcmp(cmdname, cmd->name)) { |
| 2328 | return cmd->handler(argc, argv); |
| 2329 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2330 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2331 | |
| 2332 | /* not found */ |
| 2333 | help(); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2334 | return 0; |
| 2335 | } |