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