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