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