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