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" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 33 | #include <getopt.h> |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 34 | #include <stdio.h> |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 35 | |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 36 | #ifdef _WIN32 |
| 37 | #include <windows.h> |
| 38 | #endif |
| 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 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 59 | static void format_print(void *opaque, const char *name) |
| 60 | { |
| 61 | printf(" %s", name); |
| 62 | } |
| 63 | |
blueswir1 | d2c639d | 2009-01-24 18:19:25 +0000 | [diff] [blame] | 64 | /* Please keep in synch with qemu-img.texi */ |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 65 | static void help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 66 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 67 | const char *help_msg = |
| 68 | "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 69 | "usage: qemu-img command [command options]\n" |
| 70 | "QEMU disk image utility\n" |
| 71 | "\n" |
| 72 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 73 | #define DEF(option, callback, arg_string) \ |
| 74 | " " arg_string "\n" |
| 75 | #include "qemu-img-cmds.h" |
| 76 | #undef DEF |
| 77 | #undef GEN_DOCS |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 78 | "\n" |
| 79 | "Command parameters:\n" |
| 80 | " 'filename' is a disk image filename\n" |
| 81 | " '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] | 82 | " '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] | 83 | " options are: 'none', 'writeback' (default, except for convert), 'writethrough',\n" |
| 84 | " 'directsync' and 'unsafe' (default for convert)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 85 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
| 86 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n" |
| 87 | " and T (terabyte, 1024G) are supported. 'b' is ignored.\n" |
| 88 | " 'output_filename' is the destination disk image filename\n" |
| 89 | " 'output_fmt' is the destination format\n" |
| 90 | " 'options' is a comma separated list of format specific options in a\n" |
| 91 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 92 | " used format\n" |
| 93 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
| 94 | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n" |
| 95 | " match exactly. The image doesn't need a working backing file before\n" |
| 96 | " rebasing in this case (useful for renaming the backing file)\n" |
| 97 | " '-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] | 98 | " '-p' show progress of command (only certain commands)\n" |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 99 | " '-S' indicates the consecutive number of bytes that must contain only zeros\n" |
| 100 | " for qemu-img to create a sparse image during conversion\n" |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 101 | " '--output' takes the format in which the output must be done (human or json)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 102 | "\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 103 | "Parameters to check subcommand:\n" |
| 104 | " '-r' tries to repair any inconsistencies that are found during the check.\n" |
| 105 | " '-r leaks' repairs only cluster leaks, whereas '-r all' fixes all\n" |
| 106 | " 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] | 107 | " hiding corruption that has already occurred.\n" |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 108 | "\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 109 | "Parameters to snapshot subcommand:\n" |
| 110 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 111 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 112 | " '-c' creates a snapshot\n" |
| 113 | " '-d' deletes a snapshot\n" |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 114 | " '-l' lists all snapshots in the given image\n"; |
| 115 | |
| 116 | printf("%s\nSupported formats:", help_msg); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 117 | bdrv_iterate_format(format_print, NULL); |
| 118 | printf("\n"); |
| 119 | exit(1); |
| 120 | } |
| 121 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 122 | #if defined(WIN32) |
| 123 | /* XXX: put correct support for win32 */ |
| 124 | static int read_password(char *buf, int buf_size) |
| 125 | { |
| 126 | int c, i; |
| 127 | printf("Password: "); |
| 128 | fflush(stdout); |
| 129 | i = 0; |
| 130 | for(;;) { |
| 131 | c = getchar(); |
| 132 | if (c == '\n') |
| 133 | break; |
| 134 | if (i < (buf_size - 1)) |
| 135 | buf[i++] = c; |
| 136 | } |
| 137 | buf[i] = '\0'; |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | #else |
| 142 | |
| 143 | #include <termios.h> |
| 144 | |
| 145 | static struct termios oldtty; |
| 146 | |
| 147 | static void term_exit(void) |
| 148 | { |
| 149 | tcsetattr (0, TCSANOW, &oldtty); |
| 150 | } |
| 151 | |
| 152 | static void term_init(void) |
| 153 | { |
| 154 | struct termios tty; |
| 155 | |
| 156 | tcgetattr (0, &tty); |
| 157 | oldtty = tty; |
| 158 | |
| 159 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 160 | |INLCR|IGNCR|ICRNL|IXON); |
| 161 | tty.c_oflag |= OPOST; |
| 162 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 163 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 164 | tty.c_cflag |= CS8; |
| 165 | tty.c_cc[VMIN] = 1; |
| 166 | tty.c_cc[VTIME] = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 167 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 168 | tcsetattr (0, TCSANOW, &tty); |
| 169 | |
| 170 | atexit(term_exit); |
| 171 | } |
| 172 | |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 173 | static int read_password(char *buf, int buf_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 174 | { |
| 175 | uint8_t ch; |
| 176 | int i, ret; |
| 177 | |
| 178 | printf("password: "); |
| 179 | fflush(stdout); |
| 180 | term_init(); |
| 181 | i = 0; |
| 182 | for(;;) { |
| 183 | ret = read(0, &ch, 1); |
| 184 | if (ret == -1) { |
| 185 | if (errno == EAGAIN || errno == EINTR) { |
| 186 | continue; |
| 187 | } else { |
| 188 | ret = -1; |
| 189 | break; |
| 190 | } |
| 191 | } else if (ret == 0) { |
| 192 | ret = -1; |
| 193 | break; |
| 194 | } else { |
| 195 | if (ch == '\r') { |
| 196 | ret = 0; |
| 197 | break; |
| 198 | } |
| 199 | if (i < (buf_size - 1)) |
| 200 | buf[i++] = ch; |
| 201 | } |
| 202 | } |
| 203 | term_exit(); |
| 204 | buf[i] = '\0'; |
| 205 | printf("\n"); |
| 206 | return ret; |
| 207 | } |
| 208 | #endif |
| 209 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 210 | static int print_block_option_help(const char *filename, const char *fmt) |
| 211 | { |
| 212 | BlockDriver *drv, *proto_drv; |
| 213 | QEMUOptionParameter *create_options = NULL; |
| 214 | |
| 215 | /* Find driver and parse its options */ |
| 216 | drv = bdrv_find_format(fmt); |
| 217 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 218 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 219 | return 1; |
| 220 | } |
| 221 | |
| 222 | proto_drv = bdrv_find_protocol(filename); |
| 223 | if (!proto_drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 224 | error_report("Unknown protocol '%s'", filename); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 225 | return 1; |
| 226 | } |
| 227 | |
| 228 | create_options = append_option_parameters(create_options, |
| 229 | drv->create_options); |
| 230 | create_options = append_option_parameters(create_options, |
| 231 | proto_drv->create_options); |
| 232 | print_option_help(create_options); |
| 233 | free_option_parameters(create_options); |
| 234 | return 0; |
| 235 | } |
| 236 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 237 | static BlockDriverState *bdrv_new_open(const char *filename, |
Sheng Yang | 9bc378c | 2010-01-29 10:15:06 +0800 | [diff] [blame] | 238 | const char *fmt, |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 239 | int flags, |
| 240 | bool require_io) |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 241 | { |
| 242 | BlockDriverState *bs; |
| 243 | BlockDriver *drv; |
| 244 | char password[256]; |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 245 | int ret; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 246 | |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 247 | bs = bdrv_new("image"); |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 248 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 249 | if (fmt) { |
| 250 | drv = bdrv_find_format(fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 251 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 252 | error_report("Unknown file format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 253 | goto fail; |
| 254 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 255 | } else { |
| 256 | drv = NULL; |
| 257 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 258 | |
| 259 | ret = bdrv_open(bs, filename, flags, drv); |
| 260 | if (ret < 0) { |
| 261 | error_report("Could not open '%s': %s", filename, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 262 | goto fail; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 263 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 264 | |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 265 | if (bdrv_is_encrypted(bs) && require_io) { |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 266 | printf("Disk image '%s' is encrypted.\n", filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 267 | if (read_password(password, sizeof(password)) < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 268 | error_report("No password given"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 269 | goto fail; |
| 270 | } |
| 271 | if (bdrv_set_key(bs, password) < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 272 | error_report("invalid password"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 273 | goto fail; |
| 274 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 275 | } |
| 276 | return bs; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 277 | fail: |
| 278 | if (bs) { |
| 279 | bdrv_delete(bs); |
| 280 | } |
| 281 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 282 | } |
| 283 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 284 | static int add_old_style_options(const char *fmt, QEMUOptionParameter *list, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 285 | const char *base_filename, |
| 286 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 287 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 288 | if (base_filename) { |
| 289 | if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 290 | error_report("Backing file not supported for file format '%s'", |
| 291 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 292 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | if (base_fmt) { |
| 296 | if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 297 | error_report("Backing file format not supported for file " |
| 298 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 299 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 300 | } |
| 301 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 302 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 303 | } |
| 304 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 305 | static int img_create(int argc, char **argv) |
| 306 | { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 307 | int c; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 308 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 309 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 310 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 311 | const char *filename; |
| 312 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 313 | char *options = NULL; |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 314 | Error *local_err = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 315 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 316 | for(;;) { |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 317 | c = getopt(argc, argv, "F:b:f:he6o:"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 318 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 319 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 320 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 321 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 322 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 323 | case 'h': |
| 324 | help(); |
| 325 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 326 | case 'F': |
| 327 | base_fmt = optarg; |
| 328 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 329 | case 'b': |
| 330 | base_filename = optarg; |
| 331 | break; |
| 332 | case 'f': |
| 333 | fmt = optarg; |
| 334 | break; |
| 335 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 336 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 337 | "encryption\' instead!"); |
| 338 | return 1; |
ths | d8871c5 | 2007-10-24 16:11:42 +0000 | [diff] [blame] | 339 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 340 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 341 | "compat6\' instead!"); |
| 342 | return 1; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 343 | case 'o': |
| 344 | options = optarg; |
| 345 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 346 | } |
| 347 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 348 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 349 | /* Get the filename */ |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 350 | if (optind >= argc) { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 351 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 352 | } |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 353 | filename = argv[optind++]; |
| 354 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 355 | /* Get image size, if specified */ |
| 356 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 357 | int64_t sval; |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 358 | char *end; |
| 359 | sval = strtosz_suffix(argv[optind++], &end, STRTOSZ_DEFSUFFIX_B); |
| 360 | if (sval < 0 || *end) { |
liguang | 7944339 | 2012-12-17 09:49:23 +0800 | [diff] [blame] | 361 | if (sval == -ERANGE) { |
| 362 | error_report("Image size must be less than 8 EiB!"); |
| 363 | } else { |
| 364 | error_report("Invalid image size specified! You may use k, M, " |
| 365 | "G or T suffixes for "); |
| 366 | error_report("kilobytes, megabytes, gigabytes and terabytes."); |
| 367 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 368 | return 1; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 369 | } |
| 370 | img_size = (uint64_t)sval; |
| 371 | } |
| 372 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 373 | if (options && is_help_option(options)) { |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 374 | return print_block_option_help(filename, fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Luiz Capitulino | 9b37525 | 2012-11-30 10:52:05 -0200 | [diff] [blame] | 377 | bdrv_img_create(filename, fmt, base_filename, base_fmt, |
| 378 | options, img_size, BDRV_O_FLAGS, &local_err); |
| 379 | if (error_is_set(&local_err)) { |
| 380 | error_report("%s", error_get_pretty(local_err)); |
| 381 | error_free(local_err); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 382 | return 1; |
| 383 | } |
Luiz Capitulino | a930091 | 2012-11-30 10:52:06 -0200 | [diff] [blame] | 384 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 385 | return 0; |
| 386 | } |
| 387 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 388 | static void dump_json_image_check(ImageCheck *check) |
| 389 | { |
| 390 | Error *errp = NULL; |
| 391 | QString *str; |
| 392 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 393 | QObject *obj; |
| 394 | visit_type_ImageCheck(qmp_output_get_visitor(ov), |
| 395 | &check, NULL, &errp); |
| 396 | obj = qmp_output_get_qobject(ov); |
| 397 | str = qobject_to_json_pretty(obj); |
| 398 | assert(str != NULL); |
| 399 | printf("%s\n", qstring_get_str(str)); |
| 400 | qobject_decref(obj); |
| 401 | qmp_output_visitor_cleanup(ov); |
| 402 | QDECREF(str); |
| 403 | } |
| 404 | |
| 405 | static void dump_human_image_check(ImageCheck *check) |
| 406 | { |
| 407 | if (!(check->corruptions || check->leaks || check->check_errors)) { |
| 408 | printf("No errors were found on the image.\n"); |
| 409 | } else { |
| 410 | if (check->corruptions) { |
| 411 | printf("\n%" PRId64 " errors were found on the image.\n" |
| 412 | "Data may be corrupted, or further writes to the image " |
| 413 | "may corrupt it.\n", |
| 414 | check->corruptions); |
| 415 | } |
| 416 | |
| 417 | if (check->leaks) { |
| 418 | printf("\n%" PRId64 " leaked clusters were found on the image.\n" |
| 419 | "This means waste of disk space, but no harm to data.\n", |
| 420 | check->leaks); |
| 421 | } |
| 422 | |
| 423 | if (check->check_errors) { |
| 424 | printf("\n%" PRId64 " internal errors have occurred during the check.\n", |
| 425 | check->check_errors); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | if (check->total_clusters != 0 && check->allocated_clusters != 0) { |
Stefan Hajnoczi | c9fc508 | 2013-02-07 17:15:03 +0100 | [diff] [blame] | 430 | printf("%" PRId64 "/%" PRId64 " = %0.2f%% allocated, " |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame^] | 431 | "%0.2f%% fragmented, %0.2f%% compressed clusters\n", |
Stefan Hajnoczi | c9fc508 | 2013-02-07 17:15:03 +0100 | [diff] [blame] | 432 | check->allocated_clusters, check->total_clusters, |
| 433 | check->allocated_clusters * 100.0 / check->total_clusters, |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame^] | 434 | check->fragmented_clusters * 100.0 / check->allocated_clusters, |
| 435 | check->compressed_clusters * 100.0 / check->allocated_clusters); |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | if (check->image_end_offset) { |
| 439 | printf("Image end offset: %" PRId64 "\n", check->image_end_offset); |
| 440 | } |
| 441 | } |
| 442 | |
| 443 | static int collect_image_check(BlockDriverState *bs, |
| 444 | ImageCheck *check, |
| 445 | const char *filename, |
| 446 | const char *fmt, |
| 447 | int fix) |
| 448 | { |
| 449 | int ret; |
| 450 | BdrvCheckResult result; |
| 451 | |
| 452 | ret = bdrv_check(bs, &result, fix); |
| 453 | if (ret < 0) { |
| 454 | return ret; |
| 455 | } |
| 456 | |
| 457 | check->filename = g_strdup(filename); |
| 458 | check->format = g_strdup(bdrv_get_format_name(bs)); |
| 459 | check->check_errors = result.check_errors; |
| 460 | check->corruptions = result.corruptions; |
| 461 | check->has_corruptions = result.corruptions != 0; |
| 462 | check->leaks = result.leaks; |
| 463 | check->has_leaks = result.leaks != 0; |
| 464 | check->corruptions_fixed = result.corruptions_fixed; |
| 465 | check->has_corruptions_fixed = result.corruptions != 0; |
| 466 | check->leaks_fixed = result.leaks_fixed; |
| 467 | check->has_leaks_fixed = result.leaks != 0; |
| 468 | check->image_end_offset = result.image_end_offset; |
| 469 | check->has_image_end_offset = result.image_end_offset != 0; |
| 470 | check->total_clusters = result.bfi.total_clusters; |
| 471 | check->has_total_clusters = result.bfi.total_clusters != 0; |
| 472 | check->allocated_clusters = result.bfi.allocated_clusters; |
| 473 | check->has_allocated_clusters = result.bfi.allocated_clusters != 0; |
| 474 | check->fragmented_clusters = result.bfi.fragmented_clusters; |
| 475 | check->has_fragmented_clusters = result.bfi.fragmented_clusters != 0; |
Stefan Hajnoczi | e6439d7 | 2013-02-07 17:15:04 +0100 | [diff] [blame^] | 476 | check->compressed_clusters = result.bfi.compressed_clusters; |
| 477 | check->has_compressed_clusters = result.bfi.compressed_clusters != 0; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 478 | |
| 479 | return 0; |
| 480 | } |
| 481 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 482 | /* |
| 483 | * Checks an image for consistency. Exit codes: |
| 484 | * |
| 485 | * 0 - Check completed, image is good |
| 486 | * 1 - Check not completed because of internal errors |
| 487 | * 2 - Check completed, image is corrupted |
| 488 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 489 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 490 | static int img_check(int argc, char **argv) |
| 491 | { |
| 492 | int c, ret; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 493 | OutputFormat output_format = OFORMAT_HUMAN; |
| 494 | const char *filename, *fmt, *output; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 495 | BlockDriverState *bs; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 496 | int fix = 0; |
Stefan Hajnoczi | 058f8f1 | 2012-08-09 13:05:56 +0100 | [diff] [blame] | 497 | int flags = BDRV_O_FLAGS | BDRV_O_CHECK; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 498 | ImageCheck *check; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 499 | |
| 500 | fmt = NULL; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 501 | output = NULL; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 502 | for(;;) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 503 | int option_index = 0; |
| 504 | static const struct option long_options[] = { |
| 505 | {"help", no_argument, 0, 'h'}, |
| 506 | {"format", required_argument, 0, 'f'}, |
| 507 | {"repair", no_argument, 0, 'r'}, |
| 508 | {"output", required_argument, 0, OPTION_OUTPUT}, |
| 509 | {0, 0, 0, 0} |
| 510 | }; |
| 511 | c = getopt_long(argc, argv, "f:hr:", |
| 512 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 513 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 514 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 515 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 516 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 517 | case '?': |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 518 | case 'h': |
| 519 | help(); |
| 520 | break; |
| 521 | case 'f': |
| 522 | fmt = optarg; |
| 523 | break; |
Kevin Wolf | 4534ff5 | 2012-05-11 16:07:02 +0200 | [diff] [blame] | 524 | case 'r': |
| 525 | flags |= BDRV_O_RDWR; |
| 526 | |
| 527 | if (!strcmp(optarg, "leaks")) { |
| 528 | fix = BDRV_FIX_LEAKS; |
| 529 | } else if (!strcmp(optarg, "all")) { |
| 530 | fix = BDRV_FIX_LEAKS | BDRV_FIX_ERRORS; |
| 531 | } else { |
| 532 | help(); |
| 533 | } |
| 534 | break; |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 535 | case OPTION_OUTPUT: |
| 536 | output = optarg; |
| 537 | break; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 538 | } |
| 539 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 540 | if (optind >= argc) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 541 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 542 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 543 | filename = argv[optind++]; |
| 544 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 545 | if (output && !strcmp(output, "json")) { |
| 546 | output_format = OFORMAT_JSON; |
| 547 | } else if (output && !strcmp(output, "human")) { |
| 548 | output_format = OFORMAT_HUMAN; |
| 549 | } else if (output) { |
| 550 | error_report("--output must be used with human or json as argument."); |
| 551 | return 1; |
| 552 | } |
| 553 | |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 554 | bs = bdrv_new_open(filename, fmt, flags, true); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 555 | if (!bs) { |
| 556 | return 1; |
| 557 | } |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 558 | |
| 559 | check = g_new0(ImageCheck, 1); |
| 560 | ret = collect_image_check(bs, check, filename, fmt, fix); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 561 | |
| 562 | if (ret == -ENOTSUP) { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 563 | if (output_format == OFORMAT_HUMAN) { |
| 564 | error_report("This image format does not support checks"); |
| 565 | } |
| 566 | ret = 1; |
| 567 | goto fail; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 568 | } |
| 569 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 570 | if (check->corruptions_fixed || check->leaks_fixed) { |
| 571 | int corruptions_fixed, leaks_fixed; |
| 572 | |
| 573 | leaks_fixed = check->leaks_fixed; |
| 574 | corruptions_fixed = check->corruptions_fixed; |
| 575 | |
| 576 | if (output_format == OFORMAT_HUMAN) { |
| 577 | printf("The following inconsistencies were found and repaired:\n\n" |
| 578 | " %" PRId64 " leaked clusters\n" |
| 579 | " %" PRId64 " corruptions\n\n" |
| 580 | "Double checking the fixed image now...\n", |
| 581 | check->leaks_fixed, |
| 582 | check->corruptions_fixed); |
| 583 | } |
| 584 | |
| 585 | ret = collect_image_check(bs, check, filename, fmt, 0); |
| 586 | |
| 587 | check->leaks_fixed = leaks_fixed; |
| 588 | check->corruptions_fixed = corruptions_fixed; |
Kevin Wolf | ccf3471 | 2012-05-11 18:16:54 +0200 | [diff] [blame] | 589 | } |
| 590 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 591 | switch (output_format) { |
| 592 | case OFORMAT_HUMAN: |
| 593 | dump_human_image_check(check); |
| 594 | break; |
| 595 | case OFORMAT_JSON: |
| 596 | dump_json_image_check(check); |
| 597 | break; |
| 598 | } |
| 599 | |
| 600 | if (ret || check->check_errors) { |
| 601 | ret = 1; |
| 602 | goto fail; |
| 603 | } |
| 604 | |
| 605 | if (check->corruptions) { |
| 606 | ret = 2; |
| 607 | } else if (check->leaks) { |
| 608 | ret = 3; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 609 | } else { |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 610 | ret = 0; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 613 | fail: |
| 614 | qapi_free_ImageCheck(check); |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 615 | bdrv_delete(bs); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 616 | |
Federico Simoncelli | 8599ea4 | 2013-01-28 06:59:47 -0500 | [diff] [blame] | 617 | return ret; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 618 | } |
| 619 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 620 | static int img_commit(int argc, char **argv) |
| 621 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 622 | int c, ret, flags; |
| 623 | const char *filename, *fmt, *cache; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 624 | BlockDriverState *bs; |
| 625 | |
| 626 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 627 | cache = BDRV_DEFAULT_CACHE; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 628 | for(;;) { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 629 | c = getopt(argc, argv, "f:ht:"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 630 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 631 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 632 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 633 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 634 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 635 | case 'h': |
| 636 | help(); |
| 637 | break; |
| 638 | case 'f': |
| 639 | fmt = optarg; |
| 640 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 641 | case 't': |
| 642 | cache = optarg; |
| 643 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 644 | } |
| 645 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 646 | if (optind >= argc) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 647 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 648 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 649 | filename = argv[optind++]; |
| 650 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 651 | flags = BDRV_O_RDWR; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 652 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 653 | if (ret < 0) { |
| 654 | error_report("Invalid cache option: %s", cache); |
| 655 | return -1; |
| 656 | } |
| 657 | |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 658 | bs = bdrv_new_open(filename, fmt, flags, true); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 659 | if (!bs) { |
| 660 | return 1; |
| 661 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 662 | ret = bdrv_commit(bs); |
| 663 | switch(ret) { |
| 664 | case 0: |
| 665 | printf("Image committed.\n"); |
| 666 | break; |
| 667 | case -ENOENT: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 668 | error_report("No disk inserted"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 669 | break; |
| 670 | case -EACCES: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 671 | error_report("Image is read-only"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 672 | break; |
| 673 | case -ENOTSUP: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 674 | error_report("Image is already committed"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 675 | break; |
| 676 | default: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 677 | error_report("Error while committing image"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 678 | break; |
| 679 | } |
| 680 | |
| 681 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 682 | if (ret) { |
| 683 | return 1; |
| 684 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 685 | return 0; |
| 686 | } |
| 687 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 688 | /* |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 689 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 690 | * a non-NUL byte. |
| 691 | * |
| 692 | * 'pnum' is set to the number of sectors (including and immediately following |
| 693 | * the first one) that are known to be in the same allocated/unallocated state. |
| 694 | */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 695 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
| 696 | { |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 697 | bool is_zero; |
| 698 | int i; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 699 | |
| 700 | if (n <= 0) { |
| 701 | *pnum = 0; |
| 702 | return 0; |
| 703 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 704 | is_zero = buffer_is_zero(buf, 512); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 705 | for(i = 1; i < n; i++) { |
| 706 | buf += 512; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 707 | if (is_zero != buffer_is_zero(buf, 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 708 | break; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 709 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 710 | } |
| 711 | *pnum = i; |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 712 | return !is_zero; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 715 | /* |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 716 | * Like is_allocated_sectors, but if the buffer starts with a used sector, |
| 717 | * up to 'min' consecutive sectors containing zeros are ignored. This avoids |
| 718 | * breaking up write requests for only small sparse areas. |
| 719 | */ |
| 720 | static int is_allocated_sectors_min(const uint8_t *buf, int n, int *pnum, |
| 721 | int min) |
| 722 | { |
| 723 | int ret; |
| 724 | int num_checked, num_used; |
| 725 | |
| 726 | if (n < min) { |
| 727 | min = n; |
| 728 | } |
| 729 | |
| 730 | ret = is_allocated_sectors(buf, n, pnum); |
| 731 | if (!ret) { |
| 732 | return ret; |
| 733 | } |
| 734 | |
| 735 | num_used = *pnum; |
| 736 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 737 | n -= *pnum; |
| 738 | num_checked = num_used; |
| 739 | |
| 740 | while (n > 0) { |
| 741 | ret = is_allocated_sectors(buf, n, pnum); |
| 742 | |
| 743 | buf += BDRV_SECTOR_SIZE * *pnum; |
| 744 | n -= *pnum; |
| 745 | num_checked += *pnum; |
| 746 | if (ret) { |
| 747 | num_used = num_checked; |
| 748 | } else if (*pnum >= min) { |
| 749 | break; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | *pnum = num_used; |
| 754 | return 1; |
| 755 | } |
| 756 | |
| 757 | /* |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 758 | * Compares two buffers sector by sector. Returns 0 if the first sector of both |
| 759 | * buffers matches, non-zero otherwise. |
| 760 | * |
| 761 | * pnum is set to the number of sectors (including and immediately following |
| 762 | * the first one) that are known to have the same comparison result |
| 763 | */ |
| 764 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, |
| 765 | int *pnum) |
| 766 | { |
| 767 | int res, i; |
| 768 | |
| 769 | if (n <= 0) { |
| 770 | *pnum = 0; |
| 771 | return 0; |
| 772 | } |
| 773 | |
| 774 | res = !!memcmp(buf1, buf2, 512); |
| 775 | for(i = 1; i < n; i++) { |
| 776 | buf1 += 512; |
| 777 | buf2 += 512; |
| 778 | |
| 779 | if (!!memcmp(buf1, buf2, 512) != res) { |
| 780 | break; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | *pnum = i; |
| 785 | return res; |
| 786 | } |
| 787 | |
Kevin Wolf | 80ee15a | 2009-09-15 12:30:43 +0200 | [diff] [blame] | 788 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 789 | |
| 790 | static int img_convert(int argc, char **argv) |
| 791 | { |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 792 | int c, ret = 0, n, n1, bs_n, bs_i, compress, cluster_size, cluster_sectors; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 793 | int progress = 0, flags; |
| 794 | const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 795 | BlockDriver *drv, *proto_drv; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 796 | BlockDriverState **bs = NULL, *out_bs = NULL; |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 797 | int64_t total_sectors, nb_sectors, sector_num, bs_offset; |
| 798 | uint64_t bs_sectors; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 799 | uint8_t * buf = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 800 | const uint8_t *buf1; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 801 | BlockDriverInfo bdi; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 802 | QEMUOptionParameter *param = NULL, *create_options = NULL; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 803 | QEMUOptionParameter *out_baseimg_param; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 804 | char *options = NULL; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 805 | const char *snapshot_name = NULL; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 806 | float local_progress = 0; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 807 | int min_sparse = 8; /* Need at least 4k of zeros for sparse detection */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 808 | |
| 809 | fmt = NULL; |
| 810 | out_fmt = "raw"; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 811 | cache = "unsafe"; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 812 | out_baseimg = NULL; |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 813 | compress = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 814 | for(;;) { |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 815 | c = getopt(argc, argv, "f:O:B:s:hce6o:pS:t:"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 816 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 817 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 818 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 819 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 820 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 821 | case 'h': |
| 822 | help(); |
| 823 | break; |
| 824 | case 'f': |
| 825 | fmt = optarg; |
| 826 | break; |
| 827 | case 'O': |
| 828 | out_fmt = optarg; |
| 829 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 830 | case 'B': |
| 831 | out_baseimg = optarg; |
| 832 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 833 | case 'c': |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 834 | compress = 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 835 | break; |
| 836 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 837 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 838 | "encryption\' instead!"); |
| 839 | return 1; |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 840 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 841 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 842 | "compat6\' instead!"); |
| 843 | return 1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 844 | case 'o': |
| 845 | options = optarg; |
| 846 | break; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 847 | case 's': |
| 848 | snapshot_name = optarg; |
| 849 | break; |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 850 | case 'S': |
| 851 | { |
| 852 | int64_t sval; |
Markus Armbruster | e36b369 | 2011-11-22 09:46:05 +0100 | [diff] [blame] | 853 | char *end; |
| 854 | sval = strtosz_suffix(optarg, &end, STRTOSZ_DEFSUFFIX_B); |
| 855 | if (sval < 0 || *end) { |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 856 | error_report("Invalid minimum zero buffer size for sparse output specified"); |
| 857 | return 1; |
| 858 | } |
| 859 | |
| 860 | min_sparse = sval / BDRV_SECTOR_SIZE; |
| 861 | break; |
| 862 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 863 | case 'p': |
| 864 | progress = 1; |
| 865 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 866 | case 't': |
| 867 | cache = optarg; |
| 868 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 869 | } |
| 870 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 871 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 872 | bs_n = argc - optind - 1; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 873 | if (bs_n < 1) { |
| 874 | help(); |
| 875 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 876 | |
| 877 | out_filename = argv[argc - 1]; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 878 | |
Charles Arnold | fa170c1 | 2012-05-11 10:57:54 -0600 | [diff] [blame] | 879 | /* Initialize before goto out */ |
| 880 | qemu_progress_init(progress, 2.0); |
| 881 | |
Peter Maydell | c8057f9 | 2012-08-02 13:45:54 +0100 | [diff] [blame] | 882 | if (options && is_help_option(options)) { |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 883 | ret = print_block_option_help(out_filename, out_fmt); |
| 884 | goto out; |
| 885 | } |
| 886 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 887 | if (bs_n > 1 && out_baseimg) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 888 | error_report("-B makes no sense when concatenating multiple input " |
| 889 | "images"); |
Jes Sorensen | 31ca34b | 2010-12-06 15:25:36 +0100 | [diff] [blame] | 890 | ret = -1; |
| 891 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 892 | } |
Dong Xu Wang | f8111c2 | 2012-03-15 20:13:31 +0800 | [diff] [blame] | 893 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 894 | qemu_progress_print(0, 100); |
| 895 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 896 | bs = g_malloc0(bs_n * sizeof(BlockDriverState *)); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 897 | |
| 898 | total_sectors = 0; |
| 899 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 900 | bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS, true); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 901 | if (!bs[bs_i]) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 902 | error_report("Could not open '%s'", argv[optind + bs_i]); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 903 | ret = -1; |
| 904 | goto out; |
| 905 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 906 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 907 | total_sectors += bs_sectors; |
| 908 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 909 | |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 910 | if (snapshot_name != NULL) { |
| 911 | if (bs_n > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 912 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 913 | ret = -1; |
| 914 | goto out; |
| 915 | } |
| 916 | if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 917 | error_report("Failed to load snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 918 | ret = -1; |
| 919 | goto out; |
| 920 | } |
| 921 | } |
| 922 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 923 | /* Find driver and parse its options */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 924 | drv = bdrv_find_format(out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 925 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 926 | error_report("Unknown file format '%s'", out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 927 | ret = -1; |
| 928 | goto out; |
| 929 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 930 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 931 | proto_drv = bdrv_find_protocol(out_filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 932 | if (!proto_drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 933 | error_report("Unknown protocol '%s'", out_filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 934 | ret = -1; |
| 935 | goto out; |
| 936 | } |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 937 | |
| 938 | create_options = append_option_parameters(create_options, |
| 939 | drv->create_options); |
| 940 | create_options = append_option_parameters(create_options, |
| 941 | proto_drv->create_options); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 942 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 943 | if (options) { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 944 | param = parse_option_parameters(options, create_options, param); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 945 | if (param == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 946 | error_report("Invalid options for file format '%s'.", out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 947 | ret = -1; |
| 948 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 949 | } |
| 950 | } else { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 951 | param = parse_option_parameters("", create_options, param); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 952 | } |
| 953 | |
| 954 | set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512); |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 955 | ret = add_old_style_options(out_fmt, param, out_baseimg, NULL); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 956 | if (ret < 0) { |
| 957 | goto out; |
| 958 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 959 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 960 | /* Get backing file name if -o backing_file was used */ |
| 961 | out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
| 962 | if (out_baseimg_param) { |
| 963 | out_baseimg = out_baseimg_param->value.s; |
| 964 | } |
| 965 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 966 | /* Check if compression is supported */ |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 967 | if (compress) { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 968 | QEMUOptionParameter *encryption = |
| 969 | get_option_parameter(param, BLOCK_OPT_ENCRYPT); |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 970 | QEMUOptionParameter *preallocation = |
| 971 | get_option_parameter(param, BLOCK_OPT_PREALLOC); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 972 | |
| 973 | if (!drv->bdrv_write_compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 974 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 975 | ret = -1; |
| 976 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 977 | } |
| 978 | |
| 979 | if (encryption && encryption->value.n) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 980 | error_report("Compression and encryption not supported at " |
| 981 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 982 | ret = -1; |
| 983 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 984 | } |
Kevin Wolf | 41521fa | 2011-10-18 16:19:42 +0200 | [diff] [blame] | 985 | |
| 986 | if (preallocation && preallocation->value.s |
| 987 | && strcmp(preallocation->value.s, "off")) |
| 988 | { |
| 989 | error_report("Compression and preallocation not supported at " |
| 990 | "the same time"); |
| 991 | ret = -1; |
| 992 | goto out; |
| 993 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 994 | } |
| 995 | |
| 996 | /* Create the new image */ |
| 997 | ret = bdrv_create(drv, out_filename, param); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 998 | if (ret < 0) { |
| 999 | if (ret == -ENOTSUP) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1000 | error_report("Formatting not supported for file format '%s'", |
| 1001 | out_fmt); |
aurel32 | 6e9ea0c | 2009-04-15 14:42:46 +0000 | [diff] [blame] | 1002 | } else if (ret == -EFBIG) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1003 | error_report("The image size is too large for file format '%s'", |
| 1004 | out_fmt); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1005 | } else { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1006 | error_report("%s: error while converting %s: %s", |
| 1007 | out_filename, out_fmt, strerror(-ret)); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1008 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1009 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1010 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 1011 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1012 | flags = BDRV_O_RDWR; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 1013 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1014 | if (ret < 0) { |
| 1015 | error_report("Invalid cache option: %s", cache); |
| 1016 | return -1; |
| 1017 | } |
| 1018 | |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 1019 | out_bs = bdrv_new_open(out_filename, out_fmt, flags, true); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1020 | if (!out_bs) { |
| 1021 | ret = -1; |
| 1022 | goto out; |
| 1023 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1024 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1025 | bs_i = 0; |
| 1026 | bs_offset = 0; |
| 1027 | bdrv_get_geometry(bs[0], &bs_sectors); |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 1028 | buf = qemu_blockalign(out_bs, IO_BUF_SIZE); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1029 | |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 1030 | if (compress) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1031 | ret = bdrv_get_info(out_bs, &bdi); |
| 1032 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1033 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1034 | goto out; |
| 1035 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1036 | cluster_size = bdi.cluster_size; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1037 | if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1038 | error_report("invalid cluster size"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1039 | ret = -1; |
| 1040 | goto out; |
| 1041 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1042 | cluster_sectors = cluster_size >> 9; |
| 1043 | sector_num = 0; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1044 | |
| 1045 | nb_sectors = total_sectors; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 1046 | if (nb_sectors != 0) { |
| 1047 | local_progress = (float)100 / |
| 1048 | (nb_sectors / MIN(nb_sectors, cluster_sectors)); |
| 1049 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1050 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1051 | for(;;) { |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1052 | int64_t bs_num; |
| 1053 | int remainder; |
| 1054 | uint8_t *buf2; |
| 1055 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1056 | nb_sectors = total_sectors - sector_num; |
| 1057 | if (nb_sectors <= 0) |
| 1058 | break; |
| 1059 | if (nb_sectors >= cluster_sectors) |
| 1060 | n = cluster_sectors; |
| 1061 | else |
| 1062 | n = nb_sectors; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1063 | |
| 1064 | bs_num = sector_num - bs_offset; |
| 1065 | assert (bs_num >= 0); |
| 1066 | remainder = n; |
| 1067 | buf2 = buf; |
| 1068 | while (remainder > 0) { |
| 1069 | int nlow; |
| 1070 | while (bs_num == bs_sectors) { |
| 1071 | bs_i++; |
| 1072 | assert (bs_i < bs_n); |
| 1073 | bs_offset += bs_sectors; |
| 1074 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 1075 | bs_num = 0; |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 1076 | /* printf("changing part: sector_num=%" PRId64 ", " |
| 1077 | "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64 |
| 1078 | "\n", sector_num, bs_i, bs_offset, bs_sectors); */ |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1079 | } |
| 1080 | assert (bs_num < bs_sectors); |
| 1081 | |
| 1082 | nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder; |
| 1083 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1084 | ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow); |
| 1085 | if (ret < 0) { |
Stefan Hajnoczi | 3fba9d8 | 2011-08-17 17:41:09 +0100 | [diff] [blame] | 1086 | error_report("error while reading sector %" PRId64 ": %s", |
| 1087 | bs_num, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1088 | goto out; |
| 1089 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1090 | |
| 1091 | buf2 += nlow * 512; |
| 1092 | bs_num += nlow; |
| 1093 | |
| 1094 | remainder -= nlow; |
| 1095 | } |
| 1096 | assert (remainder == 0); |
| 1097 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1098 | if (n < cluster_sectors) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1099 | memset(buf + n * 512, 0, cluster_size - n * 512); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1100 | } |
Stefan Hajnoczi | 1a6d39f | 2012-02-07 13:27:24 +0000 | [diff] [blame] | 1101 | if (!buffer_is_zero(buf, cluster_size)) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1102 | ret = bdrv_write_compressed(out_bs, sector_num, buf, |
| 1103 | cluster_sectors); |
| 1104 | if (ret != 0) { |
Stefan Hajnoczi | 3fba9d8 | 2011-08-17 17:41:09 +0100 | [diff] [blame] | 1105 | error_report("error while compressing sector %" PRId64 |
| 1106 | ": %s", sector_num, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1107 | goto out; |
| 1108 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1109 | } |
| 1110 | sector_num += n; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1111 | qemu_progress_print(local_progress, 100); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1112 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1113 | /* signal EOF to align */ |
| 1114 | bdrv_write_compressed(out_bs, 0, NULL, 0); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1115 | } else { |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1116 | int has_zero_init = bdrv_has_zero_init(out_bs); |
| 1117 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1118 | sector_num = 0; // total number of sectors converted so far |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1119 | nb_sectors = total_sectors - sector_num; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 1120 | if (nb_sectors != 0) { |
| 1121 | local_progress = (float)100 / |
| 1122 | (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512)); |
| 1123 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1124 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1125 | for(;;) { |
| 1126 | nb_sectors = total_sectors - sector_num; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1127 | if (nb_sectors <= 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1128 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1129 | } |
| 1130 | if (nb_sectors >= (IO_BUF_SIZE / 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1131 | n = (IO_BUF_SIZE / 512); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1132 | } else { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1133 | n = nb_sectors; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1134 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1135 | |
| 1136 | while (sector_num - bs_offset >= bs_sectors) { |
| 1137 | bs_i ++; |
| 1138 | assert (bs_i < bs_n); |
| 1139 | bs_offset += bs_sectors; |
| 1140 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 1141 | /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, " |
| 1142 | "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n", |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1143 | sector_num, bs_i, bs_offset, bs_sectors); */ |
| 1144 | } |
| 1145 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1146 | if (n > bs_offset + bs_sectors - sector_num) { |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1147 | n = bs_offset + bs_sectors - sector_num; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1148 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 1149 | |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1150 | if (has_zero_init) { |
Akkarit Sangpetch | d032044 | 2009-07-17 10:02:15 +0200 | [diff] [blame] | 1151 | /* If the output image is being created as a copy on write image, |
| 1152 | assume that sectors which are unallocated in the input image |
| 1153 | are present in both the output's and input's base images (no |
| 1154 | need to copy them). */ |
| 1155 | if (out_baseimg) { |
| 1156 | if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, |
| 1157 | n, &n1)) { |
| 1158 | sector_num += n1; |
| 1159 | continue; |
| 1160 | } |
| 1161 | /* The next 'n1' sectors are allocated in the input image. Copy |
| 1162 | only those as they may be followed by unallocated sectors. */ |
| 1163 | n = n1; |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1164 | } |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1165 | } else { |
| 1166 | n1 = n; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1169 | ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n); |
| 1170 | if (ret < 0) { |
Stefan Hajnoczi | 3fba9d8 | 2011-08-17 17:41:09 +0100 | [diff] [blame] | 1171 | error_report("error while reading sector %" PRId64 ": %s", |
| 1172 | sector_num - bs_offset, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1173 | goto out; |
| 1174 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1175 | /* NOTE: at the same time we convert, we do not write zero |
| 1176 | sectors to have a chance to compress the image. Ideally, we |
| 1177 | should add a specific call to have the info to go faster */ |
| 1178 | buf1 = buf; |
| 1179 | while (n > 0) { |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 1180 | /* If the output image is being created as a copy on write image, |
| 1181 | copy all sectors even the ones containing only NUL bytes, |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 1182 | because they may differ from the sectors in the base image. |
| 1183 | |
| 1184 | If the output is to a host device, we also write out |
| 1185 | sectors that are entirely 0, since whatever data was |
| 1186 | already there is garbage, not 0s. */ |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 1187 | if (!has_zero_init || out_baseimg || |
Kevin Wolf | a22f123 | 2011-08-26 15:27:13 +0200 | [diff] [blame] | 1188 | is_allocated_sectors_min(buf1, n, &n1, min_sparse)) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1189 | ret = bdrv_write(out_bs, sector_num, buf1, n1); |
| 1190 | if (ret < 0) { |
Stefan Hajnoczi | 3fba9d8 | 2011-08-17 17:41:09 +0100 | [diff] [blame] | 1191 | error_report("error while writing sector %" PRId64 |
| 1192 | ": %s", sector_num, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1193 | goto out; |
| 1194 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1195 | } |
| 1196 | sector_num += n1; |
| 1197 | n -= n1; |
| 1198 | buf1 += n1 * 512; |
| 1199 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1200 | qemu_progress_print(local_progress, 100); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1201 | } |
| 1202 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1203 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1204 | qemu_progress_end(); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1205 | free_option_parameters(create_options); |
| 1206 | free_option_parameters(param); |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 1207 | qemu_vfree(buf); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1208 | if (out_bs) { |
| 1209 | bdrv_delete(out_bs); |
| 1210 | } |
Jes Sorensen | 31ca34b | 2010-12-06 15:25:36 +0100 | [diff] [blame] | 1211 | if (bs) { |
| 1212 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
| 1213 | if (bs[bs_i]) { |
| 1214 | bdrv_delete(bs[bs_i]); |
| 1215 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1216 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1217 | g_free(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1218 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1219 | if (ret) { |
| 1220 | return 1; |
| 1221 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1222 | return 0; |
| 1223 | } |
| 1224 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 1225 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1226 | static void dump_snapshots(BlockDriverState *bs) |
| 1227 | { |
| 1228 | QEMUSnapshotInfo *sn_tab, *sn; |
| 1229 | int nb_sns, i; |
| 1230 | char buf[256]; |
| 1231 | |
| 1232 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 1233 | if (nb_sns <= 0) |
| 1234 | return; |
| 1235 | printf("Snapshot list:\n"); |
| 1236 | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 1237 | for(i = 0; i < nb_sns; i++) { |
| 1238 | sn = &sn_tab[i]; |
| 1239 | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 1240 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1241 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1242 | } |
| 1243 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1244 | static void dump_json_image_info_list(ImageInfoList *list) |
| 1245 | { |
| 1246 | Error *errp = NULL; |
| 1247 | QString *str; |
| 1248 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 1249 | QObject *obj; |
| 1250 | visit_type_ImageInfoList(qmp_output_get_visitor(ov), |
| 1251 | &list, NULL, &errp); |
| 1252 | obj = qmp_output_get_qobject(ov); |
| 1253 | str = qobject_to_json_pretty(obj); |
| 1254 | assert(str != NULL); |
| 1255 | printf("%s\n", qstring_get_str(str)); |
| 1256 | qobject_decref(obj); |
| 1257 | qmp_output_visitor_cleanup(ov); |
| 1258 | QDECREF(str); |
| 1259 | } |
| 1260 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1261 | static void collect_snapshots(BlockDriverState *bs , ImageInfo *info) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1262 | { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1263 | int i, sn_count; |
| 1264 | QEMUSnapshotInfo *sn_tab = NULL; |
| 1265 | SnapshotInfoList *info_list, *cur_item = NULL; |
| 1266 | sn_count = bdrv_snapshot_list(bs, &sn_tab); |
| 1267 | |
| 1268 | for (i = 0; i < sn_count; i++) { |
| 1269 | info->has_snapshots = true; |
| 1270 | info_list = g_new0(SnapshotInfoList, 1); |
| 1271 | |
| 1272 | info_list->value = g_new0(SnapshotInfo, 1); |
| 1273 | info_list->value->id = g_strdup(sn_tab[i].id_str); |
| 1274 | info_list->value->name = g_strdup(sn_tab[i].name); |
| 1275 | info_list->value->vm_state_size = sn_tab[i].vm_state_size; |
| 1276 | info_list->value->date_sec = sn_tab[i].date_sec; |
| 1277 | info_list->value->date_nsec = sn_tab[i].date_nsec; |
| 1278 | info_list->value->vm_clock_sec = sn_tab[i].vm_clock_nsec / 1000000000; |
| 1279 | info_list->value->vm_clock_nsec = sn_tab[i].vm_clock_nsec % 1000000000; |
| 1280 | |
| 1281 | /* XXX: waiting for the qapi to support qemu-queue.h types */ |
| 1282 | if (!cur_item) { |
| 1283 | info->snapshots = cur_item = info_list; |
| 1284 | } else { |
| 1285 | cur_item->next = info_list; |
| 1286 | cur_item = info_list; |
| 1287 | } |
| 1288 | |
| 1289 | } |
| 1290 | |
| 1291 | g_free(sn_tab); |
| 1292 | } |
| 1293 | |
| 1294 | static void dump_json_image_info(ImageInfo *info) |
| 1295 | { |
| 1296 | Error *errp = NULL; |
| 1297 | QString *str; |
| 1298 | QmpOutputVisitor *ov = qmp_output_visitor_new(); |
| 1299 | QObject *obj; |
| 1300 | visit_type_ImageInfo(qmp_output_get_visitor(ov), |
| 1301 | &info, NULL, &errp); |
| 1302 | obj = qmp_output_get_qobject(ov); |
| 1303 | str = qobject_to_json_pretty(obj); |
| 1304 | assert(str != NULL); |
| 1305 | printf("%s\n", qstring_get_str(str)); |
| 1306 | qobject_decref(obj); |
| 1307 | qmp_output_visitor_cleanup(ov); |
| 1308 | QDECREF(str); |
| 1309 | } |
| 1310 | |
| 1311 | static void collect_image_info(BlockDriverState *bs, |
| 1312 | ImageInfo *info, |
| 1313 | const char *filename, |
| 1314 | const char *fmt) |
| 1315 | { |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 1316 | uint64_t total_sectors; |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 1317 | char backing_filename[1024]; |
| 1318 | char backing_filename2[1024]; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1319 | BlockDriverInfo bdi; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1320 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1321 | bdrv_get_geometry(bs, &total_sectors); |
| 1322 | |
| 1323 | info->filename = g_strdup(filename); |
| 1324 | info->format = g_strdup(bdrv_get_format_name(bs)); |
| 1325 | info->virtual_size = total_sectors * 512; |
| 1326 | info->actual_size = bdrv_get_allocated_file_size(bs); |
| 1327 | info->has_actual_size = info->actual_size >= 0; |
| 1328 | if (bdrv_is_encrypted(bs)) { |
| 1329 | info->encrypted = true; |
| 1330 | info->has_encrypted = true; |
| 1331 | } |
| 1332 | if (bdrv_get_info(bs, &bdi) >= 0) { |
| 1333 | if (bdi.cluster_size != 0) { |
| 1334 | info->cluster_size = bdi.cluster_size; |
| 1335 | info->has_cluster_size = true; |
| 1336 | } |
| 1337 | info->dirty_flag = bdi.is_dirty; |
| 1338 | info->has_dirty_flag = true; |
| 1339 | } |
| 1340 | bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename)); |
| 1341 | if (backing_filename[0] != '\0') { |
| 1342 | info->backing_filename = g_strdup(backing_filename); |
| 1343 | info->has_backing_filename = true; |
| 1344 | bdrv_get_full_backing_filename(bs, backing_filename2, |
| 1345 | sizeof(backing_filename2)); |
| 1346 | |
| 1347 | if (strcmp(backing_filename, backing_filename2) != 0) { |
| 1348 | info->full_backing_filename = |
| 1349 | g_strdup(backing_filename2); |
| 1350 | info->has_full_backing_filename = true; |
| 1351 | } |
| 1352 | |
| 1353 | if (bs->backing_format[0]) { |
| 1354 | info->backing_filename_format = g_strdup(bs->backing_format); |
| 1355 | info->has_backing_filename_format = true; |
| 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | |
| 1360 | static void dump_human_image_info(ImageInfo *info) |
| 1361 | { |
| 1362 | char size_buf[128], dsize_buf[128]; |
| 1363 | if (!info->has_actual_size) { |
| 1364 | snprintf(dsize_buf, sizeof(dsize_buf), "unavailable"); |
| 1365 | } else { |
| 1366 | get_human_readable_size(dsize_buf, sizeof(dsize_buf), |
| 1367 | info->actual_size); |
| 1368 | } |
| 1369 | get_human_readable_size(size_buf, sizeof(size_buf), info->virtual_size); |
| 1370 | printf("image: %s\n" |
| 1371 | "file format: %s\n" |
| 1372 | "virtual size: %s (%" PRId64 " bytes)\n" |
| 1373 | "disk size: %s\n", |
| 1374 | info->filename, info->format, size_buf, |
| 1375 | info->virtual_size, |
| 1376 | dsize_buf); |
| 1377 | |
| 1378 | if (info->has_encrypted && info->encrypted) { |
| 1379 | printf("encrypted: yes\n"); |
| 1380 | } |
| 1381 | |
| 1382 | if (info->has_cluster_size) { |
| 1383 | printf("cluster_size: %" PRId64 "\n", info->cluster_size); |
| 1384 | } |
| 1385 | |
| 1386 | if (info->has_dirty_flag && info->dirty_flag) { |
| 1387 | printf("cleanly shut down: no\n"); |
| 1388 | } |
| 1389 | |
| 1390 | if (info->has_backing_filename) { |
| 1391 | printf("backing file: %s", info->backing_filename); |
| 1392 | if (info->has_full_backing_filename) { |
| 1393 | printf(" (actual path: %s)", info->full_backing_filename); |
| 1394 | } |
| 1395 | putchar('\n'); |
| 1396 | if (info->has_backing_filename_format) { |
| 1397 | printf("backing file format: %s\n", info->backing_filename_format); |
| 1398 | } |
| 1399 | } |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1400 | |
| 1401 | if (info->has_snapshots) { |
| 1402 | SnapshotInfoList *elem; |
| 1403 | char buf[256]; |
| 1404 | |
| 1405 | printf("Snapshot list:\n"); |
| 1406 | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 1407 | |
| 1408 | /* Ideally bdrv_snapshot_dump() would operate on SnapshotInfoList but |
| 1409 | * we convert to the block layer's native QEMUSnapshotInfo for now. |
| 1410 | */ |
| 1411 | for (elem = info->snapshots; elem; elem = elem->next) { |
| 1412 | QEMUSnapshotInfo sn = { |
| 1413 | .vm_state_size = elem->value->vm_state_size, |
| 1414 | .date_sec = elem->value->date_sec, |
| 1415 | .date_nsec = elem->value->date_nsec, |
| 1416 | .vm_clock_nsec = elem->value->vm_clock_sec * 1000000000ULL + |
| 1417 | elem->value->vm_clock_nsec, |
| 1418 | }; |
| 1419 | |
| 1420 | pstrcpy(sn.id_str, sizeof(sn.id_str), elem->value->id); |
| 1421 | pstrcpy(sn.name, sizeof(sn.name), elem->value->name); |
| 1422 | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), &sn)); |
| 1423 | } |
| 1424 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1425 | } |
| 1426 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1427 | static void dump_human_image_info_list(ImageInfoList *list) |
| 1428 | { |
| 1429 | ImageInfoList *elem; |
| 1430 | bool delim = false; |
| 1431 | |
| 1432 | for (elem = list; elem; elem = elem->next) { |
| 1433 | if (delim) { |
| 1434 | printf("\n"); |
| 1435 | } |
| 1436 | delim = true; |
| 1437 | |
| 1438 | dump_human_image_info(elem->value); |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | static gboolean str_equal_func(gconstpointer a, gconstpointer b) |
| 1443 | { |
| 1444 | return strcmp(a, b) == 0; |
| 1445 | } |
| 1446 | |
| 1447 | /** |
| 1448 | * Open an image file chain and return an ImageInfoList |
| 1449 | * |
| 1450 | * @filename: topmost image filename |
| 1451 | * @fmt: topmost image format (may be NULL to autodetect) |
| 1452 | * @chain: true - enumerate entire backing file chain |
| 1453 | * false - only topmost image file |
| 1454 | * |
| 1455 | * Returns a list of ImageInfo objects or NULL if there was an error opening an |
| 1456 | * image file. If there was an error a message will have been printed to |
| 1457 | * stderr. |
| 1458 | */ |
| 1459 | static ImageInfoList *collect_image_info_list(const char *filename, |
| 1460 | const char *fmt, |
| 1461 | bool chain) |
| 1462 | { |
| 1463 | ImageInfoList *head = NULL; |
| 1464 | ImageInfoList **last = &head; |
| 1465 | GHashTable *filenames; |
| 1466 | |
| 1467 | filenames = g_hash_table_new_full(g_str_hash, str_equal_func, NULL, NULL); |
| 1468 | |
| 1469 | while (filename) { |
| 1470 | BlockDriverState *bs; |
| 1471 | ImageInfo *info; |
| 1472 | ImageInfoList *elem; |
| 1473 | |
| 1474 | if (g_hash_table_lookup_extended(filenames, filename, NULL, NULL)) { |
| 1475 | error_report("Backing file '%s' creates an infinite loop.", |
| 1476 | filename); |
| 1477 | goto err; |
| 1478 | } |
| 1479 | g_hash_table_insert(filenames, (gpointer)filename, NULL); |
| 1480 | |
| 1481 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING, |
| 1482 | false); |
| 1483 | if (!bs) { |
| 1484 | goto err; |
| 1485 | } |
| 1486 | |
| 1487 | info = g_new0(ImageInfo, 1); |
| 1488 | collect_image_info(bs, info, filename, fmt); |
| 1489 | collect_snapshots(bs, info); |
| 1490 | |
| 1491 | elem = g_new0(ImageInfoList, 1); |
| 1492 | elem->value = info; |
| 1493 | *last = elem; |
| 1494 | last = &elem->next; |
| 1495 | |
| 1496 | bdrv_delete(bs); |
| 1497 | |
| 1498 | filename = fmt = NULL; |
| 1499 | if (chain) { |
| 1500 | if (info->has_full_backing_filename) { |
| 1501 | filename = info->full_backing_filename; |
| 1502 | } else if (info->has_backing_filename) { |
| 1503 | filename = info->backing_filename; |
| 1504 | } |
| 1505 | if (info->has_backing_filename_format) { |
| 1506 | fmt = info->backing_filename_format; |
| 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | g_hash_table_destroy(filenames); |
| 1511 | return head; |
| 1512 | |
| 1513 | err: |
| 1514 | qapi_free_ImageInfoList(head); |
| 1515 | g_hash_table_destroy(filenames); |
| 1516 | return NULL; |
| 1517 | } |
| 1518 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1519 | static int img_info(int argc, char **argv) |
| 1520 | { |
| 1521 | int c; |
| 1522 | OutputFormat output_format = OFORMAT_HUMAN; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1523 | bool chain = false; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1524 | const char *filename, *fmt, *output; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1525 | ImageInfoList *list; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1526 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1527 | fmt = NULL; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1528 | output = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1529 | for(;;) { |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1530 | int option_index = 0; |
| 1531 | static const struct option long_options[] = { |
| 1532 | {"help", no_argument, 0, 'h'}, |
| 1533 | {"format", required_argument, 0, 'f'}, |
| 1534 | {"output", required_argument, 0, OPTION_OUTPUT}, |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1535 | {"backing-chain", no_argument, 0, OPTION_BACKING_CHAIN}, |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1536 | {0, 0, 0, 0} |
| 1537 | }; |
| 1538 | c = getopt_long(argc, argv, "f:h", |
| 1539 | long_options, &option_index); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1540 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1541 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1542 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1543 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1544 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1545 | case 'h': |
| 1546 | help(); |
| 1547 | break; |
| 1548 | case 'f': |
| 1549 | fmt = optarg; |
| 1550 | break; |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1551 | case OPTION_OUTPUT: |
| 1552 | output = optarg; |
| 1553 | break; |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1554 | case OPTION_BACKING_CHAIN: |
| 1555 | chain = true; |
| 1556 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1557 | } |
| 1558 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1559 | if (optind >= argc) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1560 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1561 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1562 | filename = argv[optind++]; |
| 1563 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1564 | if (output && !strcmp(output, "json")) { |
| 1565 | output_format = OFORMAT_JSON; |
| 1566 | } else if (output && !strcmp(output, "human")) { |
| 1567 | output_format = OFORMAT_HUMAN; |
| 1568 | } else if (output) { |
| 1569 | error_report("--output must be used with human or json as argument."); |
| 1570 | return 1; |
| 1571 | } |
| 1572 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1573 | list = collect_image_info_list(filename, fmt, chain); |
| 1574 | if (!list) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1575 | return 1; |
| 1576 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1577 | |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1578 | switch (output_format) { |
| 1579 | case OFORMAT_HUMAN: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1580 | dump_human_image_info_list(list); |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1581 | break; |
| 1582 | case OFORMAT_JSON: |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1583 | if (chain) { |
| 1584 | dump_json_image_info_list(list); |
| 1585 | } else { |
| 1586 | dump_json_image_info(list->value); |
| 1587 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1588 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1589 | } |
Benoît Canet | c054b3f | 2012-09-05 13:09:02 +0200 | [diff] [blame] | 1590 | |
Stefan Hajnoczi | 9699bf0 | 2012-10-17 14:02:31 +0200 | [diff] [blame] | 1591 | qapi_free_ImageInfoList(list); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1592 | return 0; |
| 1593 | } |
| 1594 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1595 | #define SNAPSHOT_LIST 1 |
| 1596 | #define SNAPSHOT_CREATE 2 |
| 1597 | #define SNAPSHOT_APPLY 3 |
| 1598 | #define SNAPSHOT_DELETE 4 |
| 1599 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1600 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1601 | { |
| 1602 | BlockDriverState *bs; |
| 1603 | QEMUSnapshotInfo sn; |
| 1604 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1605 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1606 | int action = 0; |
| 1607 | qemu_timeval tv; |
| 1608 | |
Kevin Wolf | 710da70 | 2011-01-10 12:33:02 +0100 | [diff] [blame] | 1609 | bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1610 | /* Parse commandline parameters */ |
| 1611 | for(;;) { |
| 1612 | c = getopt(argc, argv, "la:c:d:h"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1613 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1614 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1615 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1616 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1617 | case '?': |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1618 | case 'h': |
| 1619 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1620 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1621 | case 'l': |
| 1622 | if (action) { |
| 1623 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1624 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1625 | } |
| 1626 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 1627 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1628 | break; |
| 1629 | case 'a': |
| 1630 | if (action) { |
| 1631 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1632 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1633 | } |
| 1634 | action = SNAPSHOT_APPLY; |
| 1635 | snapshot_name = optarg; |
| 1636 | break; |
| 1637 | case 'c': |
| 1638 | if (action) { |
| 1639 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1640 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1641 | } |
| 1642 | action = SNAPSHOT_CREATE; |
| 1643 | snapshot_name = optarg; |
| 1644 | break; |
| 1645 | case 'd': |
| 1646 | if (action) { |
| 1647 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1648 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1649 | } |
| 1650 | action = SNAPSHOT_DELETE; |
| 1651 | snapshot_name = optarg; |
| 1652 | break; |
| 1653 | } |
| 1654 | } |
| 1655 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1656 | if (optind >= argc) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1657 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1658 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1659 | filename = argv[optind++]; |
| 1660 | |
| 1661 | /* Open the image */ |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 1662 | bs = bdrv_new_open(filename, NULL, bdrv_oflags, true); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1663 | if (!bs) { |
| 1664 | return 1; |
| 1665 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1666 | |
| 1667 | /* Perform the requested action */ |
| 1668 | switch(action) { |
| 1669 | case SNAPSHOT_LIST: |
| 1670 | dump_snapshots(bs); |
| 1671 | break; |
| 1672 | |
| 1673 | case SNAPSHOT_CREATE: |
| 1674 | memset(&sn, 0, sizeof(sn)); |
| 1675 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 1676 | |
| 1677 | qemu_gettimeofday(&tv); |
| 1678 | sn.date_sec = tv.tv_sec; |
| 1679 | sn.date_nsec = tv.tv_usec * 1000; |
| 1680 | |
| 1681 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1682 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1683 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1684 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1685 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1686 | break; |
| 1687 | |
| 1688 | case SNAPSHOT_APPLY: |
| 1689 | ret = bdrv_snapshot_goto(bs, snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1690 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1691 | error_report("Could not apply snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1692 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1693 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1694 | break; |
| 1695 | |
| 1696 | case SNAPSHOT_DELETE: |
| 1697 | ret = bdrv_snapshot_delete(bs, snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1698 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1699 | error_report("Could not delete snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1700 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1701 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1702 | break; |
| 1703 | } |
| 1704 | |
| 1705 | /* Cleanup */ |
| 1706 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1707 | if (ret) { |
| 1708 | return 1; |
| 1709 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1710 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1711 | } |
| 1712 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1713 | static int img_rebase(int argc, char **argv) |
| 1714 | { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1715 | BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL; |
Stefan Hajnoczi | f163d07 | 2010-04-13 10:29:34 +0100 | [diff] [blame] | 1716 | BlockDriver *old_backing_drv, *new_backing_drv; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1717 | char *filename; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1718 | const char *fmt, *cache, *out_basefmt, *out_baseimg; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1719 | int c, flags, ret; |
| 1720 | int unsafe = 0; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1721 | int progress = 0; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1722 | |
| 1723 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 1724 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1725 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1726 | out_baseimg = NULL; |
| 1727 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1728 | for(;;) { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1729 | c = getopt(argc, argv, "uhf:F:b:pt:"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1730 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1731 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1732 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1733 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1734 | case '?': |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1735 | case 'h': |
| 1736 | help(); |
| 1737 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 1738 | case 'f': |
| 1739 | fmt = optarg; |
| 1740 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1741 | case 'F': |
| 1742 | out_basefmt = optarg; |
| 1743 | break; |
| 1744 | case 'b': |
| 1745 | out_baseimg = optarg; |
| 1746 | break; |
| 1747 | case 'u': |
| 1748 | unsafe = 1; |
| 1749 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1750 | case 'p': |
| 1751 | progress = 1; |
| 1752 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1753 | case 't': |
| 1754 | cache = optarg; |
| 1755 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1756 | } |
| 1757 | } |
| 1758 | |
Anthony Liguori | 9a9d9db | 2011-04-13 15:51:47 +0100 | [diff] [blame] | 1759 | if ((optind >= argc) || (!unsafe && !out_baseimg)) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1760 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1761 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1762 | filename = argv[optind++]; |
| 1763 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1764 | qemu_progress_init(progress, 2.0); |
| 1765 | qemu_progress_print(0, 100); |
| 1766 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1767 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame] | 1768 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1769 | if (ret < 0) { |
| 1770 | error_report("Invalid cache option: %s", cache); |
| 1771 | return -1; |
| 1772 | } |
| 1773 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1774 | /* |
| 1775 | * Open the images. |
| 1776 | * |
| 1777 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 1778 | * the reference to a renamed or moved backing file. |
| 1779 | */ |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 1780 | bs = bdrv_new_open(filename, fmt, flags, true); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1781 | if (!bs) { |
| 1782 | return 1; |
| 1783 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1784 | |
| 1785 | /* Find the right drivers for the backing files */ |
| 1786 | old_backing_drv = NULL; |
| 1787 | new_backing_drv = NULL; |
| 1788 | |
| 1789 | if (!unsafe && bs->backing_format[0] != '\0') { |
| 1790 | old_backing_drv = bdrv_find_format(bs->backing_format); |
| 1791 | if (old_backing_drv == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1792 | error_report("Invalid format name: '%s'", bs->backing_format); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1793 | ret = -1; |
| 1794 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1795 | } |
| 1796 | } |
| 1797 | |
| 1798 | if (out_basefmt != NULL) { |
| 1799 | new_backing_drv = bdrv_find_format(out_basefmt); |
| 1800 | if (new_backing_drv == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1801 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1802 | ret = -1; |
| 1803 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1804 | } |
| 1805 | } |
| 1806 | |
| 1807 | /* For safe rebasing we need to compare old and new backing file */ |
| 1808 | if (unsafe) { |
| 1809 | /* Make the compiler happy */ |
| 1810 | bs_old_backing = NULL; |
| 1811 | bs_new_backing = NULL; |
| 1812 | } else { |
| 1813 | char backing_name[1024]; |
| 1814 | |
| 1815 | bs_old_backing = bdrv_new("old_backing"); |
| 1816 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1817 | ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS, |
| 1818 | old_backing_drv); |
| 1819 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1820 | error_report("Could not open old backing file '%s'", backing_name); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1821 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1822 | } |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 1823 | if (out_baseimg[0]) { |
| 1824 | bs_new_backing = bdrv_new("new_backing"); |
| 1825 | ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS, |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1826 | new_backing_drv); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 1827 | if (ret) { |
| 1828 | error_report("Could not open new backing file '%s'", |
| 1829 | out_baseimg); |
| 1830 | goto out; |
| 1831 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1832 | } |
| 1833 | } |
| 1834 | |
| 1835 | /* |
| 1836 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 1837 | * accesses go to the backing file. We must therefore compare this cluster |
| 1838 | * in the old and new backing file, and if they differ we need to copy it |
| 1839 | * from the old backing file into the COW file. |
| 1840 | * |
| 1841 | * If qemu-img crashes during this step, no harm is done. The content of |
| 1842 | * the image is the same as the original one at any time. |
| 1843 | */ |
| 1844 | if (!unsafe) { |
| 1845 | uint64_t num_sectors; |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 1846 | uint64_t old_backing_num_sectors; |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 1847 | uint64_t new_backing_num_sectors = 0; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1848 | uint64_t sector; |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 1849 | int n; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 1850 | uint8_t * buf_old; |
| 1851 | uint8_t * buf_new; |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 1852 | float local_progress = 0; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 1853 | |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 1854 | buf_old = qemu_blockalign(bs, IO_BUF_SIZE); |
| 1855 | buf_new = qemu_blockalign(bs, IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1856 | |
| 1857 | bdrv_get_geometry(bs, &num_sectors); |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 1858 | bdrv_get_geometry(bs_old_backing, &old_backing_num_sectors); |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 1859 | if (bs_new_backing) { |
| 1860 | bdrv_get_geometry(bs_new_backing, &new_backing_num_sectors); |
| 1861 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1862 | |
Kevin Wolf | 1f71049 | 2012-10-12 14:29:18 +0200 | [diff] [blame] | 1863 | if (num_sectors != 0) { |
| 1864 | local_progress = (float)100 / |
| 1865 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); |
| 1866 | } |
| 1867 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1868 | for (sector = 0; sector < num_sectors; sector += n) { |
| 1869 | |
| 1870 | /* How many sectors can we handle with the next read? */ |
| 1871 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { |
| 1872 | n = (IO_BUF_SIZE / 512); |
| 1873 | } else { |
| 1874 | n = num_sectors - sector; |
| 1875 | } |
| 1876 | |
| 1877 | /* If the cluster is allocated, we don't need to take action */ |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 1878 | ret = bdrv_is_allocated(bs, sector, n, &n); |
| 1879 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1880 | continue; |
| 1881 | } |
| 1882 | |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 1883 | /* |
| 1884 | * Read old and new backing file and take into consideration that |
| 1885 | * backing files may be smaller than the COW image. |
| 1886 | */ |
| 1887 | if (sector >= old_backing_num_sectors) { |
| 1888 | memset(buf_old, 0, n * BDRV_SECTOR_SIZE); |
| 1889 | } else { |
| 1890 | if (sector + n > old_backing_num_sectors) { |
| 1891 | n = old_backing_num_sectors - sector; |
| 1892 | } |
| 1893 | |
| 1894 | ret = bdrv_read(bs_old_backing, sector, buf_old, n); |
| 1895 | if (ret < 0) { |
| 1896 | error_report("error while reading from old backing file"); |
| 1897 | goto out; |
| 1898 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1899 | } |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 1900 | |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 1901 | if (sector >= new_backing_num_sectors || !bs_new_backing) { |
Kevin Wolf | 87a1b3e | 2011-12-07 12:42:10 +0100 | [diff] [blame] | 1902 | memset(buf_new, 0, n * BDRV_SECTOR_SIZE); |
| 1903 | } else { |
| 1904 | if (sector + n > new_backing_num_sectors) { |
| 1905 | n = new_backing_num_sectors - sector; |
| 1906 | } |
| 1907 | |
| 1908 | ret = bdrv_read(bs_new_backing, sector, buf_new, n); |
| 1909 | if (ret < 0) { |
| 1910 | error_report("error while reading from new backing file"); |
| 1911 | goto out; |
| 1912 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1913 | } |
| 1914 | |
| 1915 | /* If they differ, we need to write to the COW file */ |
| 1916 | uint64_t written = 0; |
| 1917 | |
| 1918 | while (written < n) { |
| 1919 | int pnum; |
| 1920 | |
| 1921 | if (compare_sectors(buf_old + written * 512, |
Kevin Wolf | 60b1bd4 | 2010-02-17 12:32:59 +0100 | [diff] [blame] | 1922 | buf_new + written * 512, n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1923 | { |
| 1924 | ret = bdrv_write(bs, sector + written, |
| 1925 | buf_old + written * 512, pnum); |
| 1926 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1927 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1928 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1929 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1930 | } |
| 1931 | } |
| 1932 | |
| 1933 | written += pnum; |
| 1934 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1935 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1936 | } |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 1937 | |
Kevin Wolf | bb1c059 | 2011-08-08 14:09:12 +0200 | [diff] [blame] | 1938 | qemu_vfree(buf_old); |
| 1939 | qemu_vfree(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /* |
| 1943 | * Change the backing file. All clusters that are different from the old |
| 1944 | * backing file are overwritten in the COW file now, so the visible content |
| 1945 | * doesn't change when we switch the backing file. |
| 1946 | */ |
Alex Bligh | a616673 | 2012-10-16 13:46:18 +0100 | [diff] [blame] | 1947 | if (out_baseimg && *out_baseimg) { |
| 1948 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 1949 | } else { |
| 1950 | ret = bdrv_change_backing_file(bs, NULL, NULL); |
| 1951 | } |
| 1952 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1953 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1954 | error_report("Could not change the backing file to '%s': No " |
| 1955 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1956 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1957 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1958 | out_baseimg, strerror(-ret)); |
| 1959 | } |
| 1960 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1961 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1962 | /* |
| 1963 | * TODO At this point it is possible to check if any clusters that are |
| 1964 | * allocated in the COW file are the same in the backing file. If so, they |
| 1965 | * could be dropped from the COW file. Don't do this before switching the |
| 1966 | * backing file, in case of a crash this would lead to corruption. |
| 1967 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1968 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1969 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1970 | /* Cleanup */ |
| 1971 | if (!unsafe) { |
Kevin Wolf | eb863ad | 2011-03-31 12:39:51 +0200 | [diff] [blame] | 1972 | if (bs_old_backing != NULL) { |
| 1973 | bdrv_delete(bs_old_backing); |
| 1974 | } |
| 1975 | if (bs_new_backing != NULL) { |
| 1976 | bdrv_delete(bs_new_backing); |
| 1977 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1981 | if (ret) { |
| 1982 | return 1; |
| 1983 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1984 | return 0; |
| 1985 | } |
| 1986 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1987 | static int img_resize(int argc, char **argv) |
| 1988 | { |
| 1989 | int c, ret, relative; |
| 1990 | const char *filename, *fmt, *size; |
| 1991 | int64_t n, total_size; |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 1992 | BlockDriverState *bs = NULL; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 1993 | QemuOpts *param; |
| 1994 | static QemuOptsList resize_options = { |
| 1995 | .name = "resize_options", |
| 1996 | .head = QTAILQ_HEAD_INITIALIZER(resize_options.head), |
| 1997 | .desc = { |
| 1998 | { |
| 1999 | .name = BLOCK_OPT_SIZE, |
| 2000 | .type = QEMU_OPT_SIZE, |
| 2001 | .help = "Virtual disk size" |
| 2002 | }, { |
| 2003 | /* end of list */ |
| 2004 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2005 | }, |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2006 | }; |
| 2007 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 2008 | /* Remove size from argv manually so that negative numbers are not treated |
| 2009 | * as options by getopt. */ |
| 2010 | if (argc < 3) { |
| 2011 | help(); |
| 2012 | return 1; |
| 2013 | } |
| 2014 | |
| 2015 | size = argv[--argc]; |
| 2016 | |
| 2017 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2018 | fmt = NULL; |
| 2019 | for(;;) { |
| 2020 | c = getopt(argc, argv, "f:h"); |
| 2021 | if (c == -1) { |
| 2022 | break; |
| 2023 | } |
| 2024 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 2025 | case '?': |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2026 | case 'h': |
| 2027 | help(); |
| 2028 | break; |
| 2029 | case 'f': |
| 2030 | fmt = optarg; |
| 2031 | break; |
| 2032 | } |
| 2033 | } |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 2034 | if (optind >= argc) { |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2035 | help(); |
| 2036 | } |
| 2037 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2038 | |
| 2039 | /* Choose grow, shrink, or absolute resize mode */ |
| 2040 | switch (size[0]) { |
| 2041 | case '+': |
| 2042 | relative = 1; |
| 2043 | size++; |
| 2044 | break; |
| 2045 | case '-': |
| 2046 | relative = -1; |
| 2047 | size++; |
| 2048 | break; |
| 2049 | default: |
| 2050 | relative = 0; |
| 2051 | break; |
| 2052 | } |
| 2053 | |
| 2054 | /* Parse size */ |
Dong Xu Wang | e478b44 | 2012-12-06 14:47:22 +0800 | [diff] [blame] | 2055 | param = qemu_opts_create_nofail(&resize_options); |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 2056 | if (qemu_opt_set(param, BLOCK_OPT_SIZE, size)) { |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2057 | /* Error message already printed when size parsing fails */ |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2058 | ret = -1; |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 2059 | qemu_opts_del(param); |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2060 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2061 | } |
Dong Xu Wang | 20caf0f | 2012-08-06 10:18:42 +0800 | [diff] [blame] | 2062 | n = qemu_opt_get_size(param, BLOCK_OPT_SIZE, 0); |
| 2063 | qemu_opts_del(param); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2064 | |
Daniel P. Berrange | f0536bb | 2012-09-10 12:11:31 +0100 | [diff] [blame] | 2065 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR, true); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2066 | if (!bs) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2067 | ret = -1; |
| 2068 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2069 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2070 | |
| 2071 | if (relative) { |
| 2072 | total_size = bdrv_getlength(bs) + n * relative; |
| 2073 | } else { |
| 2074 | total_size = n; |
| 2075 | } |
| 2076 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2077 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2078 | ret = -1; |
| 2079 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2080 | } |
| 2081 | |
| 2082 | ret = bdrv_truncate(bs, total_size); |
| 2083 | switch (ret) { |
| 2084 | case 0: |
| 2085 | printf("Image resized.\n"); |
| 2086 | break; |
| 2087 | case -ENOTSUP: |
Kevin Wolf | 259b217 | 2012-03-06 12:44:45 +0100 | [diff] [blame] | 2088 | error_report("This image does not support resize"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2089 | break; |
| 2090 | case -EACCES: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2091 | error_report("Image is read-only"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2092 | break; |
| 2093 | default: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 2094 | error_report("Error resizing image (%d)", -ret); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2095 | break; |
| 2096 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2097 | out: |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 2098 | if (bs) { |
| 2099 | bdrv_delete(bs); |
| 2100 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 2101 | if (ret) { |
| 2102 | return 1; |
| 2103 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 2104 | return 0; |
| 2105 | } |
| 2106 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2107 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2108 | #define DEF(option, callback, arg_string) \ |
| 2109 | { option, callback }, |
| 2110 | #include "qemu-img-cmds.h" |
| 2111 | #undef DEF |
| 2112 | #undef GEN_DOCS |
| 2113 | { NULL, NULL, }, |
| 2114 | }; |
| 2115 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2116 | int main(int argc, char **argv) |
| 2117 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 2118 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2119 | const char *cmdname; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2120 | |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 2121 | error_set_progname(argv[0]); |
| 2122 | |
Paolo Bonzini | 2592c59 | 2012-11-03 18:10:17 +0100 | [diff] [blame] | 2123 | qemu_init_main_loop(); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2124 | bdrv_init(); |
| 2125 | if (argc < 2) |
| 2126 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2127 | cmdname = argv[1]; |
aurel32 | 8f9b157 | 2009-02-09 18:14:31 +0000 | [diff] [blame] | 2128 | argc--; argv++; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2129 | |
| 2130 | /* find the command */ |
| 2131 | for(cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 2132 | if (!strcmp(cmdname, cmd->name)) { |
| 2133 | return cmd->handler(argc, argv); |
| 2134 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2135 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 2136 | |
| 2137 | /* not found */ |
| 2138 | help(); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 2139 | return 0; |
| 2140 | } |