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