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