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