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 | */ |
pbrook | faf0796 | 2007-11-11 02:51:17 +0000 | [diff] [blame] | 24 | #include "qemu-common.h" |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 25 | #include "qemu-option.h" |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 26 | #include "qemu-error.h" |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 27 | #include "osdep.h" |
Jes Sorensen | dc786bc | 2010-10-26 10:39:23 +0200 | [diff] [blame] | 28 | #include "sysemu.h" |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 29 | #include "block_int.h" |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 30 | #include <stdio.h> |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 31 | |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 32 | #ifdef _WIN32 |
| 33 | #include <windows.h> |
| 34 | #endif |
| 35 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 36 | typedef struct img_cmd_t { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 37 | const char *name; |
| 38 | int (*handler)(int argc, char **argv); |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 39 | } img_cmd_t; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 40 | |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 41 | /* 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] | 42 | #define BDRV_O_FLAGS BDRV_O_CACHE_WB |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 43 | #define BDRV_DEFAULT_CACHE "writeback" |
aurel32 | 137519c | 2008-11-30 19:12:49 +0000 | [diff] [blame] | 44 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 45 | static void format_print(void *opaque, const char *name) |
| 46 | { |
| 47 | printf(" %s", name); |
| 48 | } |
| 49 | |
blueswir1 | d2c639d | 2009-01-24 18:19:25 +0000 | [diff] [blame] | 50 | /* Please keep in synch with qemu-img.texi */ |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 51 | static void help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 52 | { |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 53 | const char *help_msg = |
| 54 | "qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 55 | "usage: qemu-img command [command options]\n" |
| 56 | "QEMU disk image utility\n" |
| 57 | "\n" |
| 58 | "Command syntax:\n" |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 59 | #define DEF(option, callback, arg_string) \ |
| 60 | " " arg_string "\n" |
| 61 | #include "qemu-img-cmds.h" |
| 62 | #undef DEF |
| 63 | #undef GEN_DOCS |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 64 | "\n" |
| 65 | "Command parameters:\n" |
| 66 | " 'filename' is a disk image filename\n" |
| 67 | " '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] | 68 | " 'cache' is the cache mode used to write the output disk image, the valid\n" |
| 69 | " options are: 'none', 'writeback' (default), 'writethrough' and 'unsafe'\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 70 | " 'size' is the disk image size in bytes. Optional suffixes\n" |
| 71 | " 'k' or 'K' (kilobyte, 1024), 'M' (megabyte, 1024k), 'G' (gigabyte, 1024M)\n" |
| 72 | " and T (terabyte, 1024G) are supported. 'b' is ignored.\n" |
| 73 | " 'output_filename' is the destination disk image filename\n" |
| 74 | " 'output_fmt' is the destination format\n" |
| 75 | " 'options' is a comma separated list of format specific options in a\n" |
| 76 | " name=value format. Use -o ? for an overview of the options supported by the\n" |
| 77 | " used format\n" |
| 78 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
| 79 | " '-u' enables unsafe rebasing. It is assumed that old and new backing file\n" |
| 80 | " match exactly. The image doesn't need a working backing file before\n" |
| 81 | " rebasing in this case (useful for renaming the backing file)\n" |
| 82 | " '-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] | 83 | " '-p' show progress of command (only certain commands)\n" |
malc | 3f020d7 | 2010-02-08 12:04:56 +0300 | [diff] [blame] | 84 | "\n" |
| 85 | "Parameters to snapshot subcommand:\n" |
| 86 | " 'snapshot' is the name of the snapshot to create, apply or delete\n" |
| 87 | " '-a' applies a snapshot (revert disk to saved state)\n" |
| 88 | " '-c' creates a snapshot\n" |
| 89 | " '-d' deletes a snapshot\n" |
Paolo Bonzini | e00291c | 2010-02-04 16:49:56 +0100 | [diff] [blame] | 90 | " '-l' lists all snapshots in the given image\n"; |
| 91 | |
| 92 | printf("%s\nSupported formats:", help_msg); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 93 | bdrv_iterate_format(format_print, NULL); |
| 94 | printf("\n"); |
| 95 | exit(1); |
| 96 | } |
| 97 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 98 | #if defined(WIN32) |
| 99 | /* XXX: put correct support for win32 */ |
| 100 | static int read_password(char *buf, int buf_size) |
| 101 | { |
| 102 | int c, i; |
| 103 | printf("Password: "); |
| 104 | fflush(stdout); |
| 105 | i = 0; |
| 106 | for(;;) { |
| 107 | c = getchar(); |
| 108 | if (c == '\n') |
| 109 | break; |
| 110 | if (i < (buf_size - 1)) |
| 111 | buf[i++] = c; |
| 112 | } |
| 113 | buf[i] = '\0'; |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | #else |
| 118 | |
| 119 | #include <termios.h> |
| 120 | |
| 121 | static struct termios oldtty; |
| 122 | |
| 123 | static void term_exit(void) |
| 124 | { |
| 125 | tcsetattr (0, TCSANOW, &oldtty); |
| 126 | } |
| 127 | |
| 128 | static void term_init(void) |
| 129 | { |
| 130 | struct termios tty; |
| 131 | |
| 132 | tcgetattr (0, &tty); |
| 133 | oldtty = tty; |
| 134 | |
| 135 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 136 | |INLCR|IGNCR|ICRNL|IXON); |
| 137 | tty.c_oflag |= OPOST; |
| 138 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 139 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 140 | tty.c_cflag |= CS8; |
| 141 | tty.c_cc[VMIN] = 1; |
| 142 | tty.c_cc[VTIME] = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 143 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 144 | tcsetattr (0, TCSANOW, &tty); |
| 145 | |
| 146 | atexit(term_exit); |
| 147 | } |
| 148 | |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 149 | static int read_password(char *buf, int buf_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 150 | { |
| 151 | uint8_t ch; |
| 152 | int i, ret; |
| 153 | |
| 154 | printf("password: "); |
| 155 | fflush(stdout); |
| 156 | term_init(); |
| 157 | i = 0; |
| 158 | for(;;) { |
| 159 | ret = read(0, &ch, 1); |
| 160 | if (ret == -1) { |
| 161 | if (errno == EAGAIN || errno == EINTR) { |
| 162 | continue; |
| 163 | } else { |
| 164 | ret = -1; |
| 165 | break; |
| 166 | } |
| 167 | } else if (ret == 0) { |
| 168 | ret = -1; |
| 169 | break; |
| 170 | } else { |
| 171 | if (ch == '\r') { |
| 172 | ret = 0; |
| 173 | break; |
| 174 | } |
| 175 | if (i < (buf_size - 1)) |
| 176 | buf[i++] = ch; |
| 177 | } |
| 178 | } |
| 179 | term_exit(); |
| 180 | buf[i] = '\0'; |
| 181 | printf("\n"); |
| 182 | return ret; |
| 183 | } |
| 184 | #endif |
| 185 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 186 | static int print_block_option_help(const char *filename, const char *fmt) |
| 187 | { |
| 188 | BlockDriver *drv, *proto_drv; |
| 189 | QEMUOptionParameter *create_options = NULL; |
| 190 | |
| 191 | /* Find driver and parse its options */ |
| 192 | drv = bdrv_find_format(fmt); |
| 193 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 194 | error_report("Unknown file format '%s'", fmt); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 195 | return 1; |
| 196 | } |
| 197 | |
| 198 | proto_drv = bdrv_find_protocol(filename); |
| 199 | if (!proto_drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 200 | error_report("Unknown protocol '%s'", filename); |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 201 | return 1; |
| 202 | } |
| 203 | |
| 204 | create_options = append_option_parameters(create_options, |
| 205 | drv->create_options); |
| 206 | create_options = append_option_parameters(create_options, |
| 207 | proto_drv->create_options); |
| 208 | print_option_help(create_options); |
| 209 | free_option_parameters(create_options); |
| 210 | return 0; |
| 211 | } |
| 212 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 213 | static BlockDriverState *bdrv_new_open(const char *filename, |
Sheng Yang | 9bc378c | 2010-01-29 10:15:06 +0800 | [diff] [blame] | 214 | const char *fmt, |
Stefan Hajnoczi | f163d07 | 2010-04-13 10:29:34 +0100 | [diff] [blame] | 215 | int flags) |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 216 | { |
| 217 | BlockDriverState *bs; |
| 218 | BlockDriver *drv; |
| 219 | char password[256]; |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 220 | int ret; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 221 | |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 222 | bs = bdrv_new("image"); |
Kevin Wolf | ad71713 | 2010-12-16 15:37:41 +0100 | [diff] [blame] | 223 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 224 | if (fmt) { |
| 225 | drv = bdrv_find_format(fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 226 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 227 | error_report("Unknown file format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 228 | goto fail; |
| 229 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 230 | } else { |
| 231 | drv = NULL; |
| 232 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 233 | |
| 234 | ret = bdrv_open(bs, filename, flags, drv); |
| 235 | if (ret < 0) { |
| 236 | error_report("Could not open '%s': %s", filename, strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 237 | goto fail; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 238 | } |
Kevin Wolf | b9eaf9e | 2011-02-09 11:25:53 +0100 | [diff] [blame] | 239 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 240 | if (bdrv_is_encrypted(bs)) { |
| 241 | printf("Disk image '%s' is encrypted.\n", filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 242 | if (read_password(password, sizeof(password)) < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 243 | error_report("No password given"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 244 | goto fail; |
| 245 | } |
| 246 | if (bdrv_set_key(bs, password) < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 247 | error_report("invalid password"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 248 | goto fail; |
| 249 | } |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 250 | } |
| 251 | return bs; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 252 | fail: |
| 253 | if (bs) { |
| 254 | bdrv_delete(bs); |
| 255 | } |
| 256 | return NULL; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 257 | } |
| 258 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 259 | static int add_old_style_options(const char *fmt, QEMUOptionParameter *list, |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 260 | const char *base_filename, |
| 261 | const char *base_fmt) |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 262 | { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 263 | if (base_filename) { |
| 264 | if (set_option_parameter(list, BLOCK_OPT_BACKING_FILE, base_filename)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 265 | error_report("Backing file not supported for file format '%s'", |
| 266 | fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 267 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | if (base_fmt) { |
| 271 | if (set_option_parameter(list, BLOCK_OPT_BACKING_FMT, base_fmt)) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 272 | error_report("Backing file format not supported for file " |
| 273 | "format '%s'", fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 274 | return -1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 275 | } |
| 276 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 277 | return 0; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 278 | } |
| 279 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 280 | static int img_create(int argc, char **argv) |
| 281 | { |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 282 | int c, ret = 0; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 283 | uint64_t img_size = -1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 284 | const char *fmt = "raw"; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 285 | const char *base_fmt = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 286 | const char *filename; |
| 287 | const char *base_filename = NULL; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 288 | char *options = NULL; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 289 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 290 | for(;;) { |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 291 | c = getopt(argc, argv, "F:b:f:he6o:"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 292 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 293 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 294 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 295 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 296 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 297 | case 'h': |
| 298 | help(); |
| 299 | break; |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 300 | case 'F': |
| 301 | base_fmt = optarg; |
| 302 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 303 | case 'b': |
| 304 | base_filename = optarg; |
| 305 | break; |
| 306 | case 'f': |
| 307 | fmt = optarg; |
| 308 | break; |
| 309 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 310 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 311 | "encryption\' instead!"); |
| 312 | return 1; |
ths | d8871c5 | 2007-10-24 16:11:42 +0000 | [diff] [blame] | 313 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 314 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 315 | "compat6\' instead!"); |
| 316 | return 1; |
Kevin Wolf | 9ea2ea7 | 2009-05-18 16:42:11 +0200 | [diff] [blame] | 317 | case 'o': |
| 318 | options = optarg; |
| 319 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
aliguori | 9230eaf | 2009-03-28 17:55:19 +0000 | [diff] [blame] | 322 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 323 | /* Get the filename */ |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 324 | if (optind >= argc) { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 325 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 326 | } |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 327 | filename = argv[optind++]; |
| 328 | |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 329 | /* Get image size, if specified */ |
| 330 | if (optind < argc) { |
Jes Sorensen | 70b4f4b | 2011-01-05 11:41:02 +0100 | [diff] [blame] | 331 | int64_t sval; |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 332 | sval = strtosz_suffix(argv[optind++], NULL, STRTOSZ_DEFSUFFIX_B); |
| 333 | if (sval < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 334 | error_report("Invalid image size specified! You may use k, M, G or " |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 335 | "T suffixes for "); |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 336 | error_report("kilobytes, megabytes, gigabytes and terabytes."); |
Jes Sorensen | 1da7cfb | 2010-12-09 14:17:25 +0100 | [diff] [blame] | 337 | ret = -1; |
| 338 | goto out; |
| 339 | } |
| 340 | img_size = (uint64_t)sval; |
| 341 | } |
| 342 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 343 | if (options && !strcmp(options, "?")) { |
| 344 | ret = print_block_option_help(filename, fmt); |
| 345 | goto out; |
| 346 | } |
| 347 | |
Jes Sorensen | f88e1a4 | 2010-12-16 13:52:15 +0100 | [diff] [blame] | 348 | ret = bdrv_img_create(filename, fmt, base_filename, base_fmt, |
| 349 | options, img_size, BDRV_O_FLAGS); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 350 | out: |
| 351 | if (ret) { |
| 352 | return 1; |
| 353 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 354 | return 0; |
| 355 | } |
| 356 | |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 357 | /* |
| 358 | * Checks an image for consistency. Exit codes: |
| 359 | * |
| 360 | * 0 - Check completed, image is good |
| 361 | * 1 - Check not completed because of internal errors |
| 362 | * 2 - Check completed, image is corrupted |
| 363 | * 3 - Check completed, image has leaked clusters, but is good otherwise |
| 364 | */ |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 365 | static int img_check(int argc, char **argv) |
| 366 | { |
| 367 | int c, ret; |
| 368 | const char *filename, *fmt; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 369 | BlockDriverState *bs; |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 370 | BdrvCheckResult result; |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 371 | |
| 372 | fmt = NULL; |
| 373 | for(;;) { |
| 374 | c = getopt(argc, argv, "f:h"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 375 | if (c == -1) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 376 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 377 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 378 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 379 | case '?': |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 380 | case 'h': |
| 381 | help(); |
| 382 | break; |
| 383 | case 'f': |
| 384 | fmt = optarg; |
| 385 | break; |
| 386 | } |
| 387 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 388 | if (optind >= argc) { |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 389 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 390 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 391 | filename = argv[optind++]; |
| 392 | |
Stefan Hajnoczi | adfe078 | 2010-04-13 10:29:35 +0100 | [diff] [blame] | 393 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 394 | if (!bs) { |
| 395 | return 1; |
| 396 | } |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 397 | ret = bdrv_check(bs, &result); |
| 398 | |
| 399 | if (ret == -ENOTSUP) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 400 | error_report("This image format does not support checks"); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 401 | bdrv_delete(bs); |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | if (!(result.corruptions || result.leaks || result.check_errors)) { |
| 406 | printf("No errors were found on the image.\n"); |
| 407 | } else { |
| 408 | if (result.corruptions) { |
| 409 | printf("\n%d errors were found on the image.\n" |
| 410 | "Data may be corrupted, or further writes to the image " |
| 411 | "may corrupt it.\n", |
| 412 | result.corruptions); |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 413 | } |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 414 | |
| 415 | if (result.leaks) { |
| 416 | printf("\n%d leaked clusters were found on the image.\n" |
| 417 | "This means waste of disk space, but no harm to data.\n", |
| 418 | result.leaks); |
| 419 | } |
| 420 | |
| 421 | if (result.check_errors) { |
| 422 | printf("\n%d internal errors have occurred during the check.\n", |
| 423 | result.check_errors); |
| 424 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | bdrv_delete(bs); |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 428 | |
| 429 | if (ret < 0 || result.check_errors) { |
| 430 | printf("\nAn error has occurred during the check: %s\n" |
| 431 | "The check is not complete and may have missed error.\n", |
| 432 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 433 | return 1; |
| 434 | } |
Kevin Wolf | e076f33 | 2010-06-29 11:43:13 +0200 | [diff] [blame] | 435 | |
| 436 | if (result.corruptions) { |
| 437 | return 2; |
| 438 | } else if (result.leaks) { |
| 439 | return 3; |
| 440 | } else { |
| 441 | return 0; |
| 442 | } |
aliguori | 1585969 | 2009-04-21 23:11:53 +0000 | [diff] [blame] | 443 | } |
| 444 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 445 | static int img_commit(int argc, char **argv) |
| 446 | { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 447 | int c, ret, flags; |
| 448 | const char *filename, *fmt, *cache; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 449 | BlockDriverState *bs; |
| 450 | |
| 451 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 452 | cache = BDRV_DEFAULT_CACHE; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 453 | for(;;) { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 454 | c = getopt(argc, argv, "f:ht:"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 455 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 456 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 457 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 458 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 459 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 460 | case 'h': |
| 461 | help(); |
| 462 | break; |
| 463 | case 'f': |
| 464 | fmt = optarg; |
| 465 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 466 | case 't': |
| 467 | cache = optarg; |
| 468 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 469 | } |
| 470 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 471 | if (optind >= argc) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 472 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 473 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 474 | filename = argv[optind++]; |
| 475 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 476 | flags = BDRV_O_RDWR; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame^] | 477 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 478 | if (ret < 0) { |
| 479 | error_report("Invalid cache option: %s", cache); |
| 480 | return -1; |
| 481 | } |
| 482 | |
| 483 | bs = bdrv_new_open(filename, fmt, flags); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 484 | if (!bs) { |
| 485 | return 1; |
| 486 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 487 | ret = bdrv_commit(bs); |
| 488 | switch(ret) { |
| 489 | case 0: |
| 490 | printf("Image committed.\n"); |
| 491 | break; |
| 492 | case -ENOENT: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 493 | error_report("No disk inserted"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 494 | break; |
| 495 | case -EACCES: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 496 | error_report("Image is read-only"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 497 | break; |
| 498 | case -ENOTSUP: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 499 | error_report("Image is already committed"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 500 | break; |
| 501 | default: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 502 | error_report("Error while committing image"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 503 | break; |
| 504 | } |
| 505 | |
| 506 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 507 | if (ret) { |
| 508 | return 1; |
| 509 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 510 | return 0; |
| 511 | } |
| 512 | |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 513 | /* |
| 514 | * Checks whether the sector is not a zero sector. |
| 515 | * |
| 516 | * Attention! The len must be a multiple of 4 * sizeof(long) due to |
| 517 | * restriction of optimizations in this function. |
| 518 | */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 519 | static int is_not_zero(const uint8_t *sector, int len) |
| 520 | { |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 521 | /* |
| 522 | * Use long as the biggest available internal data type that fits into the |
| 523 | * CPU register and unroll the loop to smooth out the effect of memory |
| 524 | * latency. |
| 525 | */ |
| 526 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 527 | int i; |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 528 | long d0, d1, d2, d3; |
| 529 | const long * const data = (const long *) sector; |
| 530 | |
| 531 | len /= sizeof(long); |
| 532 | |
| 533 | for(i = 0; i < len; i += 4) { |
| 534 | d0 = data[i + 0]; |
| 535 | d1 = data[i + 1]; |
| 536 | d2 = data[i + 2]; |
| 537 | d3 = data[i + 3]; |
| 538 | |
| 539 | if (d0 || d1 || d2 || d3) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 540 | return 1; |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 541 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 542 | } |
Dmitry Konishchev | f6a00aa | 2011-05-18 15:03:59 +0400 | [diff] [blame] | 543 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 544 | return 0; |
| 545 | } |
| 546 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 547 | /* |
| 548 | * Returns true iff the first sector pointed to by 'buf' contains at least |
| 549 | * a non-NUL byte. |
| 550 | * |
| 551 | * 'pnum' is set to the number of sectors (including and immediately following |
| 552 | * the first one) that are known to be in the same allocated/unallocated state. |
| 553 | */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 554 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
| 555 | { |
| 556 | int v, i; |
| 557 | |
| 558 | if (n <= 0) { |
| 559 | *pnum = 0; |
| 560 | return 0; |
| 561 | } |
| 562 | v = is_not_zero(buf, 512); |
| 563 | for(i = 1; i < n; i++) { |
| 564 | buf += 512; |
| 565 | if (v != is_not_zero(buf, 512)) |
| 566 | break; |
| 567 | } |
| 568 | *pnum = i; |
| 569 | return v; |
| 570 | } |
| 571 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 572 | /* |
| 573 | * Compares two buffers sector by sector. Returns 0 if the first sector of both |
| 574 | * buffers matches, non-zero otherwise. |
| 575 | * |
| 576 | * pnum is set to the number of sectors (including and immediately following |
| 577 | * the first one) that are known to have the same comparison result |
| 578 | */ |
| 579 | static int compare_sectors(const uint8_t *buf1, const uint8_t *buf2, int n, |
| 580 | int *pnum) |
| 581 | { |
| 582 | int res, i; |
| 583 | |
| 584 | if (n <= 0) { |
| 585 | *pnum = 0; |
| 586 | return 0; |
| 587 | } |
| 588 | |
| 589 | res = !!memcmp(buf1, buf2, 512); |
| 590 | for(i = 1; i < n; i++) { |
| 591 | buf1 += 512; |
| 592 | buf2 += 512; |
| 593 | |
| 594 | if (!!memcmp(buf1, buf2, 512) != res) { |
| 595 | break; |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | *pnum = i; |
| 600 | return res; |
| 601 | } |
| 602 | |
Kevin Wolf | 80ee15a | 2009-09-15 12:30:43 +0200 | [diff] [blame] | 603 | #define IO_BUF_SIZE (2 * 1024 * 1024) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 604 | |
| 605 | static int img_convert(int argc, char **argv) |
| 606 | { |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 607 | 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] | 608 | int progress = 0, flags; |
| 609 | const char *fmt, *out_fmt, *cache, *out_baseimg, *out_filename; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 610 | BlockDriver *drv, *proto_drv; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 611 | BlockDriverState **bs = NULL, *out_bs = NULL; |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 612 | int64_t total_sectors, nb_sectors, sector_num, bs_offset; |
| 613 | uint64_t bs_sectors; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 614 | uint8_t * buf = NULL; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 615 | const uint8_t *buf1; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 616 | BlockDriverInfo bdi; |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 617 | QEMUOptionParameter *param = NULL, *create_options = NULL; |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 618 | QEMUOptionParameter *out_baseimg_param; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 619 | char *options = NULL; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 620 | const char *snapshot_name = NULL; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 621 | float local_progress; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 622 | |
| 623 | fmt = NULL; |
| 624 | out_fmt = "raw"; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 625 | cache = "unsafe"; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 626 | out_baseimg = NULL; |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 627 | compress = 0; |
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:O:B:s:hce6o:pt:"); |
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; |
| 641 | case 'O': |
| 642 | out_fmt = optarg; |
| 643 | break; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 644 | case 'B': |
| 645 | out_baseimg = optarg; |
| 646 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 647 | case 'c': |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 648 | compress = 1; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 649 | break; |
| 650 | case 'e': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 651 | error_report("option -e is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 652 | "encryption\' instead!"); |
| 653 | return 1; |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 654 | case '6': |
Markus Armbruster | 9d42e15 | 2011-06-22 14:03:55 +0200 | [diff] [blame] | 655 | error_report("option -6 is deprecated, please use \'-o " |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 656 | "compat6\' instead!"); |
| 657 | return 1; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 658 | case 'o': |
| 659 | options = optarg; |
| 660 | break; |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 661 | case 's': |
| 662 | snapshot_name = optarg; |
| 663 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 664 | case 'p': |
| 665 | progress = 1; |
| 666 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 667 | case 't': |
| 668 | cache = optarg; |
| 669 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 672 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 673 | bs_n = argc - optind - 1; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 674 | if (bs_n < 1) { |
| 675 | help(); |
| 676 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 677 | |
| 678 | out_filename = argv[argc - 1]; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 679 | |
Jes Sorensen | 4ac8aac | 2010-12-06 15:25:38 +0100 | [diff] [blame] | 680 | if (options && !strcmp(options, "?")) { |
| 681 | ret = print_block_option_help(out_filename, out_fmt); |
| 682 | goto out; |
| 683 | } |
| 684 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 685 | if (bs_n > 1 && out_baseimg) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 686 | error_report("-B makes no sense when concatenating multiple input " |
| 687 | "images"); |
Jes Sorensen | 31ca34b | 2010-12-06 15:25:36 +0100 | [diff] [blame] | 688 | ret = -1; |
| 689 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 690 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 691 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 692 | qemu_progress_init(progress, 2.0); |
| 693 | qemu_progress_print(0, 100); |
| 694 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 695 | bs = g_malloc0(bs_n * sizeof(BlockDriverState *)); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 696 | |
| 697 | total_sectors = 0; |
| 698 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
Stefan Hajnoczi | adfe078 | 2010-04-13 10:29:35 +0100 | [diff] [blame] | 699 | bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt, BDRV_O_FLAGS); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 700 | if (!bs[bs_i]) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 701 | error_report("Could not open '%s'", argv[optind + bs_i]); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 702 | ret = -1; |
| 703 | goto out; |
| 704 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 705 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 706 | total_sectors += bs_sectors; |
| 707 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 708 | |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 709 | if (snapshot_name != NULL) { |
| 710 | if (bs_n > 1) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 711 | error_report("No support for concatenating multiple snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 712 | ret = -1; |
| 713 | goto out; |
| 714 | } |
| 715 | if (bdrv_snapshot_load_tmp(bs[0], snapshot_name) < 0) { |
Markus Armbruster | 6daf194 | 2011-06-22 14:03:54 +0200 | [diff] [blame] | 716 | error_report("Failed to load snapshot"); |
edison | 51ef672 | 2010-09-21 19:58:41 -0700 | [diff] [blame] | 717 | ret = -1; |
| 718 | goto out; |
| 719 | } |
| 720 | } |
| 721 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 722 | /* Find driver and parse its options */ |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 723 | drv = bdrv_find_format(out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 724 | if (!drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 725 | error_report("Unknown file format '%s'", out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 726 | ret = -1; |
| 727 | goto out; |
| 728 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 729 | |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 730 | proto_drv = bdrv_find_protocol(out_filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 731 | if (!proto_drv) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 732 | error_report("Unknown protocol '%s'", out_filename); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 733 | ret = -1; |
| 734 | goto out; |
| 735 | } |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 736 | |
| 737 | create_options = append_option_parameters(create_options, |
| 738 | drv->create_options); |
| 739 | create_options = append_option_parameters(create_options, |
| 740 | proto_drv->create_options); |
Kevin Wolf | db08adf | 2009-06-04 15:39:38 +0200 | [diff] [blame] | 741 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 742 | if (options) { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 743 | param = parse_option_parameters(options, create_options, param); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 744 | if (param == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 745 | error_report("Invalid options for file format '%s'.", out_fmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 746 | ret = -1; |
| 747 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 748 | } |
| 749 | } else { |
MORITA Kazutaka | b50cbab | 2010-05-26 11:35:36 +0900 | [diff] [blame] | 750 | param = parse_option_parameters("", create_options, param); |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 751 | } |
| 752 | |
| 753 | set_option_parameter_int(param, BLOCK_OPT_SIZE, total_sectors * 512); |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 754 | ret = add_old_style_options(out_fmt, param, out_baseimg, NULL); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 755 | if (ret < 0) { |
| 756 | goto out; |
| 757 | } |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 758 | |
Kevin Wolf | a18953f | 2010-10-14 15:46:04 +0200 | [diff] [blame] | 759 | /* Get backing file name if -o backing_file was used */ |
| 760 | out_baseimg_param = get_option_parameter(param, BLOCK_OPT_BACKING_FILE); |
| 761 | if (out_baseimg_param) { |
| 762 | out_baseimg = out_baseimg_param->value.s; |
| 763 | } |
| 764 | |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 765 | /* Check if compression is supported */ |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 766 | if (compress) { |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 767 | QEMUOptionParameter *encryption = |
| 768 | get_option_parameter(param, BLOCK_OPT_ENCRYPT); |
| 769 | |
| 770 | if (!drv->bdrv_write_compressed) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 771 | error_report("Compression not supported for this file format"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 772 | ret = -1; |
| 773 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 774 | } |
| 775 | |
| 776 | if (encryption && encryption->value.n) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 777 | error_report("Compression and encryption not supported at " |
| 778 | "the same time"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 779 | ret = -1; |
| 780 | goto out; |
Kevin Wolf | efa84d4 | 2009-05-18 16:42:12 +0200 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
| 784 | /* Create the new image */ |
| 785 | ret = bdrv_create(drv, out_filename, param); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 786 | if (ret < 0) { |
| 787 | if (ret == -ENOTSUP) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 788 | error_report("Formatting not supported for file format '%s'", |
| 789 | out_fmt); |
aurel32 | 6e9ea0c | 2009-04-15 14:42:46 +0000 | [diff] [blame] | 790 | } else if (ret == -EFBIG) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 791 | error_report("The image size is too large for file format '%s'", |
| 792 | out_fmt); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 793 | } else { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 794 | error_report("%s: error while converting %s: %s", |
| 795 | out_filename, out_fmt, strerror(-ret)); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 796 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 797 | goto out; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 798 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 799 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 800 | flags = BDRV_O_RDWR; |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame^] | 801 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 802 | if (ret < 0) { |
| 803 | error_report("Invalid cache option: %s", cache); |
| 804 | return -1; |
| 805 | } |
| 806 | |
| 807 | out_bs = bdrv_new_open(out_filename, out_fmt, flags); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 808 | if (!out_bs) { |
| 809 | ret = -1; |
| 810 | goto out; |
| 811 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 812 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 813 | bs_i = 0; |
| 814 | bs_offset = 0; |
| 815 | bdrv_get_geometry(bs[0], &bs_sectors); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 816 | buf = g_malloc(IO_BUF_SIZE); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 817 | |
Jes Sorensen | eec77d9 | 2010-12-07 17:44:34 +0100 | [diff] [blame] | 818 | if (compress) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 819 | ret = bdrv_get_info(out_bs, &bdi); |
| 820 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 821 | error_report("could not get block driver info"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 822 | goto out; |
| 823 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 824 | cluster_size = bdi.cluster_size; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 825 | if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 826 | error_report("invalid cluster size"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 827 | ret = -1; |
| 828 | goto out; |
| 829 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 830 | cluster_sectors = cluster_size >> 9; |
| 831 | sector_num = 0; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 832 | |
| 833 | nb_sectors = total_sectors; |
| 834 | local_progress = (float)100 / |
Jes Sorensen | 4ee9641 | 2011-05-06 11:39:11 +0200 | [diff] [blame] | 835 | (nb_sectors / MIN(nb_sectors, cluster_sectors)); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 836 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 837 | for(;;) { |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 838 | int64_t bs_num; |
| 839 | int remainder; |
| 840 | uint8_t *buf2; |
| 841 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 842 | nb_sectors = total_sectors - sector_num; |
| 843 | if (nb_sectors <= 0) |
| 844 | break; |
| 845 | if (nb_sectors >= cluster_sectors) |
| 846 | n = cluster_sectors; |
| 847 | else |
| 848 | n = nb_sectors; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 849 | |
| 850 | bs_num = sector_num - bs_offset; |
| 851 | assert (bs_num >= 0); |
| 852 | remainder = n; |
| 853 | buf2 = buf; |
| 854 | while (remainder > 0) { |
| 855 | int nlow; |
| 856 | while (bs_num == bs_sectors) { |
| 857 | bs_i++; |
| 858 | assert (bs_i < bs_n); |
| 859 | bs_offset += bs_sectors; |
| 860 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 861 | bs_num = 0; |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 862 | /* printf("changing part: sector_num=%" PRId64 ", " |
| 863 | "bs_i=%d, bs_offset=%" PRId64 ", bs_sectors=%" PRId64 |
| 864 | "\n", sector_num, bs_i, bs_offset, bs_sectors); */ |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 865 | } |
| 866 | assert (bs_num < bs_sectors); |
| 867 | |
| 868 | nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder; |
| 869 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 870 | ret = bdrv_read(bs[bs_i], bs_num, buf2, nlow); |
| 871 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 872 | error_report("error while reading"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 873 | goto out; |
| 874 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 875 | |
| 876 | buf2 += nlow * 512; |
| 877 | bs_num += nlow; |
| 878 | |
| 879 | remainder -= nlow; |
| 880 | } |
| 881 | assert (remainder == 0); |
| 882 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 883 | if (n < cluster_sectors) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 884 | memset(buf + n * 512, 0, cluster_size - n * 512); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 885 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 886 | if (is_not_zero(buf, cluster_size)) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 887 | ret = bdrv_write_compressed(out_bs, sector_num, buf, |
| 888 | cluster_sectors); |
| 889 | if (ret != 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 890 | error_report("error while compressing sector %" PRId64, |
bellard | ec3757d | 2006-06-14 15:50:07 +0000 | [diff] [blame] | 891 | sector_num); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 892 | goto out; |
| 893 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 894 | } |
| 895 | sector_num += n; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 896 | qemu_progress_print(local_progress, 100); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 897 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 898 | /* signal EOF to align */ |
| 899 | bdrv_write_compressed(out_bs, 0, NULL, 0); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 900 | } else { |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 901 | int has_zero_init = bdrv_has_zero_init(out_bs); |
| 902 | |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 903 | sector_num = 0; // total number of sectors converted so far |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 904 | nb_sectors = total_sectors - sector_num; |
| 905 | local_progress = (float)100 / |
Jes Sorensen | 4ee9641 | 2011-05-06 11:39:11 +0200 | [diff] [blame] | 906 | (nb_sectors / MIN(nb_sectors, IO_BUF_SIZE / 512)); |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 907 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 908 | for(;;) { |
| 909 | nb_sectors = total_sectors - sector_num; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 910 | if (nb_sectors <= 0) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 911 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 912 | } |
| 913 | if (nb_sectors >= (IO_BUF_SIZE / 512)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 914 | n = (IO_BUF_SIZE / 512); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 915 | } else { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 916 | n = nb_sectors; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 917 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 918 | |
| 919 | while (sector_num - bs_offset >= bs_sectors) { |
| 920 | bs_i ++; |
| 921 | assert (bs_i < bs_n); |
| 922 | bs_offset += bs_sectors; |
| 923 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
Blue Swirl | 0bfcd59 | 2010-05-22 08:02:12 +0000 | [diff] [blame] | 924 | /* printf("changing part: sector_num=%" PRId64 ", bs_i=%d, " |
| 925 | "bs_offset=%" PRId64 ", bs_sectors=%" PRId64 "\n", |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 926 | sector_num, bs_i, bs_offset, bs_sectors); */ |
| 927 | } |
| 928 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 929 | if (n > bs_offset + bs_sectors - sector_num) { |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 930 | n = bs_offset + bs_sectors - sector_num; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 931 | } |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 932 | |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 933 | if (has_zero_init) { |
Akkarit Sangpetch | d032044 | 2009-07-17 10:02:15 +0200 | [diff] [blame] | 934 | /* If the output image is being created as a copy on write image, |
| 935 | assume that sectors which are unallocated in the input image |
| 936 | are present in both the output's and input's base images (no |
| 937 | need to copy them). */ |
| 938 | if (out_baseimg) { |
| 939 | if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset, |
| 940 | n, &n1)) { |
| 941 | sector_num += n1; |
| 942 | continue; |
| 943 | } |
| 944 | /* The next 'n1' sectors are allocated in the input image. Copy |
| 945 | only those as they may be followed by unallocated sectors. */ |
| 946 | n = n1; |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 947 | } |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 948 | } else { |
| 949 | n1 = n; |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 950 | } |
| 951 | |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 952 | ret = bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n); |
| 953 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 954 | error_report("error while reading"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 955 | goto out; |
| 956 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 957 | /* NOTE: at the same time we convert, we do not write zero |
| 958 | sectors to have a chance to compress the image. Ideally, we |
| 959 | should add a specific call to have the info to go faster */ |
| 960 | buf1 = buf; |
| 961 | while (n > 0) { |
ths | f58c7b3 | 2008-06-05 21:53:49 +0000 | [diff] [blame] | 962 | /* If the output image is being created as a copy on write image, |
| 963 | copy all sectors even the ones containing only NUL bytes, |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 964 | because they may differ from the sectors in the base image. |
| 965 | |
| 966 | If the output is to a host device, we also write out |
| 967 | sectors that are entirely 0, since whatever data was |
| 968 | already there is garbage, not 0s. */ |
Kevin Wolf | f2feebb | 2010-04-14 17:30:35 +0200 | [diff] [blame] | 969 | if (!has_zero_init || out_baseimg || |
aliguori | 93c65b4 | 2009-04-05 17:40:43 +0000 | [diff] [blame] | 970 | is_allocated_sectors(buf1, n, &n1)) { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 971 | ret = bdrv_write(out_bs, sector_num, buf1, n1); |
| 972 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 973 | error_report("error while writing"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 974 | goto out; |
| 975 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 976 | } |
| 977 | sector_num += n1; |
| 978 | n -= n1; |
| 979 | buf1 += n1 * 512; |
| 980 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 981 | qemu_progress_print(local_progress, 100); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 982 | } |
| 983 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 984 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 985 | qemu_progress_end(); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 986 | free_option_parameters(create_options); |
| 987 | free_option_parameters(param); |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 988 | g_free(buf); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 989 | if (out_bs) { |
| 990 | bdrv_delete(out_bs); |
| 991 | } |
Jes Sorensen | 31ca34b | 2010-12-06 15:25:36 +0100 | [diff] [blame] | 992 | if (bs) { |
| 993 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
| 994 | if (bs[bs_i]) { |
| 995 | bdrv_delete(bs[bs_i]); |
| 996 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 997 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 998 | g_free(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 999 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1000 | if (ret) { |
| 1001 | return 1; |
| 1002 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1003 | return 0; |
| 1004 | } |
| 1005 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 1006 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1007 | static void dump_snapshots(BlockDriverState *bs) |
| 1008 | { |
| 1009 | QEMUSnapshotInfo *sn_tab, *sn; |
| 1010 | int nb_sns, i; |
| 1011 | char buf[256]; |
| 1012 | |
| 1013 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 1014 | if (nb_sns <= 0) |
| 1015 | return; |
| 1016 | printf("Snapshot list:\n"); |
| 1017 | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 1018 | for(i = 0; i < nb_sns; i++) { |
| 1019 | sn = &sn_tab[i]; |
| 1020 | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 1021 | } |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1022 | g_free(sn_tab); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1025 | static int img_info(int argc, char **argv) |
| 1026 | { |
| 1027 | int c; |
| 1028 | const char *filename, *fmt; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1029 | BlockDriverState *bs; |
| 1030 | char fmt_name[128], size_buf[128], dsize_buf[128]; |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame] | 1031 | uint64_t total_sectors; |
| 1032 | int64_t allocated_size; |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 1033 | char backing_filename[1024]; |
| 1034 | char backing_filename2[1024]; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1035 | BlockDriverInfo bdi; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1036 | |
| 1037 | fmt = NULL; |
| 1038 | for(;;) { |
| 1039 | c = getopt(argc, argv, "f:h"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1040 | if (c == -1) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1041 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1042 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1043 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1044 | case '?': |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1045 | case 'h': |
| 1046 | help(); |
| 1047 | break; |
| 1048 | case 'f': |
| 1049 | fmt = optarg; |
| 1050 | break; |
| 1051 | } |
| 1052 | } |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1053 | if (optind >= argc) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1054 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1055 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1056 | filename = argv[optind++]; |
| 1057 | |
Stefan Hajnoczi | adfe078 | 2010-04-13 10:29:35 +0100 | [diff] [blame] | 1058 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_NO_BACKING); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1059 | if (!bs) { |
| 1060 | return 1; |
| 1061 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1062 | bdrv_get_format(bs, fmt_name, sizeof(fmt_name)); |
| 1063 | bdrv_get_geometry(bs, &total_sectors); |
| 1064 | get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512); |
Fam Zheng | 4a1d5e1 | 2011-07-12 19:56:39 +0800 | [diff] [blame] | 1065 | allocated_size = bdrv_get_allocated_file_size(bs); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1066 | if (allocated_size < 0) { |
blueswir1 | a10ea30 | 2008-08-24 10:30:33 +0000 | [diff] [blame] | 1067 | snprintf(dsize_buf, sizeof(dsize_buf), "unavailable"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1068 | } else { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1069 | get_human_readable_size(dsize_buf, sizeof(dsize_buf), |
bellard | de167e4 | 2005-04-28 21:15:08 +0000 | [diff] [blame] | 1070 | allocated_size); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1071 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1072 | printf("image: %s\n" |
| 1073 | "file format: %s\n" |
bellard | ec3757d | 2006-06-14 15:50:07 +0000 | [diff] [blame] | 1074 | "virtual size: %s (%" PRId64 " bytes)\n" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1075 | "disk size: %s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1076 | filename, fmt_name, size_buf, |
bellard | ec3757d | 2006-06-14 15:50:07 +0000 | [diff] [blame] | 1077 | (total_sectors * 512), |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1078 | dsize_buf); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1079 | if (bdrv_is_encrypted(bs)) { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1080 | printf("encrypted: yes\n"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1081 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1082 | if (bdrv_get_info(bs, &bdi) >= 0) { |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1083 | if (bdi.cluster_size != 0) { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1084 | printf("cluster_size: %d\n", bdi.cluster_size); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1085 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1086 | } |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 1087 | bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename)); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1088 | if (backing_filename[0] != '\0') { |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 1089 | path_combine(backing_filename2, sizeof(backing_filename2), |
| 1090 | filename, backing_filename); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 1091 | printf("backing file: %s (actual path: %s)\n", |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 1092 | backing_filename, |
| 1093 | backing_filename2); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 1094 | } |
| 1095 | dump_snapshots(bs); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1096 | bdrv_delete(bs); |
| 1097 | return 0; |
| 1098 | } |
| 1099 | |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1100 | #define SNAPSHOT_LIST 1 |
| 1101 | #define SNAPSHOT_CREATE 2 |
| 1102 | #define SNAPSHOT_APPLY 3 |
| 1103 | #define SNAPSHOT_DELETE 4 |
| 1104 | |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1105 | static int img_snapshot(int argc, char **argv) |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1106 | { |
| 1107 | BlockDriverState *bs; |
| 1108 | QEMUSnapshotInfo sn; |
| 1109 | char *filename, *snapshot_name = NULL; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1110 | int c, ret = 0, bdrv_oflags; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1111 | int action = 0; |
| 1112 | qemu_timeval tv; |
| 1113 | |
Kevin Wolf | 710da70 | 2011-01-10 12:33:02 +0100 | [diff] [blame] | 1114 | bdrv_oflags = BDRV_O_FLAGS | BDRV_O_RDWR; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1115 | /* Parse commandline parameters */ |
| 1116 | for(;;) { |
| 1117 | c = getopt(argc, argv, "la:c:d:h"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1118 | if (c == -1) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1119 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1120 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1121 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1122 | case '?': |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1123 | case 'h': |
| 1124 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1125 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1126 | case 'l': |
| 1127 | if (action) { |
| 1128 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1129 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1130 | } |
| 1131 | action = SNAPSHOT_LIST; |
Naphtali Sprei | f5edb01 | 2010-01-17 16:48:13 +0200 | [diff] [blame] | 1132 | bdrv_oflags &= ~BDRV_O_RDWR; /* no need for RW */ |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1133 | break; |
| 1134 | case 'a': |
| 1135 | if (action) { |
| 1136 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1137 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1138 | } |
| 1139 | action = SNAPSHOT_APPLY; |
| 1140 | snapshot_name = optarg; |
| 1141 | break; |
| 1142 | case 'c': |
| 1143 | if (action) { |
| 1144 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1145 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1146 | } |
| 1147 | action = SNAPSHOT_CREATE; |
| 1148 | snapshot_name = optarg; |
| 1149 | break; |
| 1150 | case 'd': |
| 1151 | if (action) { |
| 1152 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1153 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1154 | } |
| 1155 | action = SNAPSHOT_DELETE; |
| 1156 | snapshot_name = optarg; |
| 1157 | break; |
| 1158 | } |
| 1159 | } |
| 1160 | |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1161 | if (optind >= argc) { |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1162 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1163 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1164 | filename = argv[optind++]; |
| 1165 | |
| 1166 | /* Open the image */ |
Stefan Hajnoczi | f163d07 | 2010-04-13 10:29:34 +0100 | [diff] [blame] | 1167 | bs = bdrv_new_open(filename, NULL, bdrv_oflags); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1168 | if (!bs) { |
| 1169 | return 1; |
| 1170 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1171 | |
| 1172 | /* Perform the requested action */ |
| 1173 | switch(action) { |
| 1174 | case SNAPSHOT_LIST: |
| 1175 | dump_snapshots(bs); |
| 1176 | break; |
| 1177 | |
| 1178 | case SNAPSHOT_CREATE: |
| 1179 | memset(&sn, 0, sizeof(sn)); |
| 1180 | pstrcpy(sn.name, sizeof(sn.name), snapshot_name); |
| 1181 | |
| 1182 | qemu_gettimeofday(&tv); |
| 1183 | sn.date_sec = tv.tv_sec; |
| 1184 | sn.date_nsec = tv.tv_usec * 1000; |
| 1185 | |
| 1186 | ret = bdrv_snapshot_create(bs, &sn); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1187 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1188 | error_report("Could not create snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1189 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1190 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1191 | break; |
| 1192 | |
| 1193 | case SNAPSHOT_APPLY: |
| 1194 | ret = bdrv_snapshot_goto(bs, snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1195 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1196 | error_report("Could not apply snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1197 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1198 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1199 | break; |
| 1200 | |
| 1201 | case SNAPSHOT_DELETE: |
| 1202 | ret = bdrv_snapshot_delete(bs, snapshot_name); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1203 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1204 | error_report("Could not delete snapshot '%s': %d (%s)", |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1205 | snapshot_name, ret, strerror(-ret)); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1206 | } |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | } |
| 1209 | |
| 1210 | /* Cleanup */ |
| 1211 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1212 | if (ret) { |
| 1213 | return 1; |
| 1214 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1215 | return 0; |
aliguori | f7b4a94 | 2009-01-07 17:40:15 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1218 | static int img_rebase(int argc, char **argv) |
| 1219 | { |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1220 | BlockDriverState *bs, *bs_old_backing = NULL, *bs_new_backing = NULL; |
Stefan Hajnoczi | f163d07 | 2010-04-13 10:29:34 +0100 | [diff] [blame] | 1221 | BlockDriver *old_backing_drv, *new_backing_drv; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1222 | char *filename; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1223 | const char *fmt, *cache, *out_basefmt, *out_baseimg; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1224 | int c, flags, ret; |
| 1225 | int unsafe = 0; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1226 | int progress = 0; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1227 | |
| 1228 | /* Parse commandline parameters */ |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 1229 | fmt = NULL; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1230 | cache = BDRV_DEFAULT_CACHE; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1231 | out_baseimg = NULL; |
| 1232 | out_basefmt = NULL; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1233 | for(;;) { |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1234 | c = getopt(argc, argv, "uhf:F:b:pt:"); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1235 | if (c == -1) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1236 | break; |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1237 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1238 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1239 | case '?': |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1240 | case 'h': |
| 1241 | help(); |
| 1242 | return 0; |
Kevin Wolf | e53dbee | 2010-03-02 12:14:31 +0100 | [diff] [blame] | 1243 | case 'f': |
| 1244 | fmt = optarg; |
| 1245 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1246 | case 'F': |
| 1247 | out_basefmt = optarg; |
| 1248 | break; |
| 1249 | case 'b': |
| 1250 | out_baseimg = optarg; |
| 1251 | break; |
| 1252 | case 'u': |
| 1253 | unsafe = 1; |
| 1254 | break; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1255 | case 'p': |
| 1256 | progress = 1; |
| 1257 | break; |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1258 | case 't': |
| 1259 | cache = optarg; |
| 1260 | break; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | |
Anthony Liguori | 9a9d9db | 2011-04-13 15:51:47 +0100 | [diff] [blame] | 1264 | if ((optind >= argc) || (!unsafe && !out_baseimg)) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1265 | help(); |
Jes Sorensen | b8fb60d | 2010-12-06 15:25:39 +0100 | [diff] [blame] | 1266 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1267 | filename = argv[optind++]; |
| 1268 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1269 | qemu_progress_init(progress, 2.0); |
| 1270 | qemu_progress_print(0, 100); |
| 1271 | |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1272 | flags = BDRV_O_RDWR | (unsafe ? BDRV_O_NO_BACKING : 0); |
Stefan Hajnoczi | c3993cd | 2011-08-04 12:26:51 +0100 | [diff] [blame^] | 1273 | ret = bdrv_parse_cache_flags(cache, &flags); |
Federico Simoncelli | 661a0f7 | 2011-06-20 12:48:19 -0400 | [diff] [blame] | 1274 | if (ret < 0) { |
| 1275 | error_report("Invalid cache option: %s", cache); |
| 1276 | return -1; |
| 1277 | } |
| 1278 | |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1279 | /* |
| 1280 | * Open the images. |
| 1281 | * |
| 1282 | * Ignore the old backing file for unsafe rebase in case we want to correct |
| 1283 | * the reference to a renamed or moved backing file. |
| 1284 | */ |
Stefan Hajnoczi | f163d07 | 2010-04-13 10:29:34 +0100 | [diff] [blame] | 1285 | bs = bdrv_new_open(filename, fmt, flags); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1286 | if (!bs) { |
| 1287 | return 1; |
| 1288 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1289 | |
| 1290 | /* Find the right drivers for the backing files */ |
| 1291 | old_backing_drv = NULL; |
| 1292 | new_backing_drv = NULL; |
| 1293 | |
| 1294 | if (!unsafe && bs->backing_format[0] != '\0') { |
| 1295 | old_backing_drv = bdrv_find_format(bs->backing_format); |
| 1296 | if (old_backing_drv == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1297 | error_report("Invalid format name: '%s'", bs->backing_format); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1298 | ret = -1; |
| 1299 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1300 | } |
| 1301 | } |
| 1302 | |
| 1303 | if (out_basefmt != NULL) { |
| 1304 | new_backing_drv = bdrv_find_format(out_basefmt); |
| 1305 | if (new_backing_drv == NULL) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1306 | error_report("Invalid format name: '%s'", out_basefmt); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1307 | ret = -1; |
| 1308 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | /* For safe rebasing we need to compare old and new backing file */ |
| 1313 | if (unsafe) { |
| 1314 | /* Make the compiler happy */ |
| 1315 | bs_old_backing = NULL; |
| 1316 | bs_new_backing = NULL; |
| 1317 | } else { |
| 1318 | char backing_name[1024]; |
| 1319 | |
| 1320 | bs_old_backing = bdrv_new("old_backing"); |
| 1321 | bdrv_get_backing_filename(bs, backing_name, sizeof(backing_name)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1322 | ret = bdrv_open(bs_old_backing, backing_name, BDRV_O_FLAGS, |
| 1323 | old_backing_drv); |
| 1324 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1325 | error_report("Could not open old backing file '%s'", backing_name); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1326 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | bs_new_backing = bdrv_new("new_backing"); |
Kevin Wolf | cdbae85 | 2010-08-17 18:58:55 +0200 | [diff] [blame] | 1330 | ret = bdrv_open(bs_new_backing, out_baseimg, BDRV_O_FLAGS, |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1331 | new_backing_drv); |
| 1332 | if (ret) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1333 | error_report("Could not open new backing file '%s'", out_baseimg); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1334 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | /* |
| 1339 | * Check each unallocated cluster in the COW file. If it is unallocated, |
| 1340 | * accesses go to the backing file. We must therefore compare this cluster |
| 1341 | * in the old and new backing file, and if they differ we need to copy it |
| 1342 | * from the old backing file into the COW file. |
| 1343 | * |
| 1344 | * If qemu-img crashes during this step, no harm is done. The content of |
| 1345 | * the image is the same as the original one at any time. |
| 1346 | */ |
| 1347 | if (!unsafe) { |
| 1348 | uint64_t num_sectors; |
| 1349 | uint64_t sector; |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 1350 | int n; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 1351 | uint8_t * buf_old; |
| 1352 | uint8_t * buf_new; |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1353 | float local_progress; |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 1354 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1355 | buf_old = g_malloc(IO_BUF_SIZE); |
| 1356 | buf_new = g_malloc(IO_BUF_SIZE); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1357 | |
| 1358 | bdrv_get_geometry(bs, &num_sectors); |
| 1359 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1360 | local_progress = (float)100 / |
Jes Sorensen | 4ee9641 | 2011-05-06 11:39:11 +0200 | [diff] [blame] | 1361 | (num_sectors / MIN(num_sectors, IO_BUF_SIZE / 512)); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1362 | for (sector = 0; sector < num_sectors; sector += n) { |
| 1363 | |
| 1364 | /* How many sectors can we handle with the next read? */ |
| 1365 | if (sector + (IO_BUF_SIZE / 512) <= num_sectors) { |
| 1366 | n = (IO_BUF_SIZE / 512); |
| 1367 | } else { |
| 1368 | n = num_sectors - sector; |
| 1369 | } |
| 1370 | |
| 1371 | /* If the cluster is allocated, we don't need to take action */ |
Kevin Wolf | cc60e32 | 2010-04-29 14:47:48 +0200 | [diff] [blame] | 1372 | ret = bdrv_is_allocated(bs, sector, n, &n); |
| 1373 | if (ret) { |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1374 | continue; |
| 1375 | } |
| 1376 | |
| 1377 | /* Read old and new backing file */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1378 | ret = bdrv_read(bs_old_backing, sector, buf_old, n); |
| 1379 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1380 | error_report("error while reading from old backing file"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1381 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1382 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1383 | ret = bdrv_read(bs_new_backing, sector, buf_new, n); |
| 1384 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1385 | error_report("error while reading from new backing file"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1386 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | /* If they differ, we need to write to the COW file */ |
| 1390 | uint64_t written = 0; |
| 1391 | |
| 1392 | while (written < n) { |
| 1393 | int pnum; |
| 1394 | |
| 1395 | if (compare_sectors(buf_old + written * 512, |
Kevin Wolf | 60b1bd4 | 2010-02-17 12:32:59 +0100 | [diff] [blame] | 1396 | buf_new + written * 512, n - written, &pnum)) |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1397 | { |
| 1398 | ret = bdrv_write(bs, sector + written, |
| 1399 | buf_old + written * 512, pnum); |
| 1400 | if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1401 | error_report("Error while writing to COW image: %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1402 | strerror(-ret)); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1403 | goto out; |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | |
| 1407 | written += pnum; |
| 1408 | } |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1409 | qemu_progress_print(local_progress, 100); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1410 | } |
TeLeMan | d6771bf | 2010-02-08 16:20:00 +0800 | [diff] [blame] | 1411 | |
Anthony Liguori | 7267c09 | 2011-08-20 22:09:37 -0500 | [diff] [blame] | 1412 | g_free(buf_old); |
| 1413 | g_free(buf_new); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * Change the backing file. All clusters that are different from the old |
| 1418 | * backing file are overwritten in the COW file now, so the visible content |
| 1419 | * doesn't change when we switch the backing file. |
| 1420 | */ |
| 1421 | ret = bdrv_change_backing_file(bs, out_baseimg, out_basefmt); |
| 1422 | if (ret == -ENOSPC) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1423 | error_report("Could not change the backing file to '%s': No " |
| 1424 | "space left in the file header", out_baseimg); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1425 | } else if (ret < 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1426 | error_report("Could not change the backing file to '%s': %s", |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1427 | out_baseimg, strerror(-ret)); |
| 1428 | } |
| 1429 | |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1430 | qemu_progress_print(100, 0); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1431 | /* |
| 1432 | * TODO At this point it is possible to check if any clusters that are |
| 1433 | * allocated in the COW file are the same in the backing file. If so, they |
| 1434 | * could be dropped from the COW file. Don't do this before switching the |
| 1435 | * backing file, in case of a crash this would lead to corruption. |
| 1436 | */ |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1437 | out: |
Jes Sorensen | 6b837bc | 2011-03-30 14:16:25 +0200 | [diff] [blame] | 1438 | qemu_progress_end(); |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1439 | /* Cleanup */ |
| 1440 | if (!unsafe) { |
Kevin Wolf | eb863ad | 2011-03-31 12:39:51 +0200 | [diff] [blame] | 1441 | if (bs_old_backing != NULL) { |
| 1442 | bdrv_delete(bs_old_backing); |
| 1443 | } |
| 1444 | if (bs_new_backing != NULL) { |
| 1445 | bdrv_delete(bs_new_backing); |
| 1446 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | bdrv_delete(bs); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1450 | if (ret) { |
| 1451 | return 1; |
| 1452 | } |
Kevin Wolf | 3e85c6f | 2010-01-12 12:55:18 +0100 | [diff] [blame] | 1453 | return 0; |
| 1454 | } |
| 1455 | |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1456 | static int img_resize(int argc, char **argv) |
| 1457 | { |
| 1458 | int c, ret, relative; |
| 1459 | const char *filename, *fmt, *size; |
| 1460 | int64_t n, total_size; |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 1461 | BlockDriverState *bs = NULL; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1462 | QEMUOptionParameter *param; |
| 1463 | QEMUOptionParameter resize_options[] = { |
| 1464 | { |
| 1465 | .name = BLOCK_OPT_SIZE, |
| 1466 | .type = OPT_SIZE, |
| 1467 | .help = "Virtual disk size" |
| 1468 | }, |
| 1469 | { NULL } |
| 1470 | }; |
| 1471 | |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 1472 | /* Remove size from argv manually so that negative numbers are not treated |
| 1473 | * as options by getopt. */ |
| 1474 | if (argc < 3) { |
| 1475 | help(); |
| 1476 | return 1; |
| 1477 | } |
| 1478 | |
| 1479 | size = argv[--argc]; |
| 1480 | |
| 1481 | /* Parse getopt arguments */ |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1482 | fmt = NULL; |
| 1483 | for(;;) { |
| 1484 | c = getopt(argc, argv, "f:h"); |
| 1485 | if (c == -1) { |
| 1486 | break; |
| 1487 | } |
| 1488 | switch(c) { |
Jes Sorensen | ef87394 | 2010-12-06 15:25:40 +0100 | [diff] [blame] | 1489 | case '?': |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1490 | case 'h': |
| 1491 | help(); |
| 1492 | break; |
| 1493 | case 'f': |
| 1494 | fmt = optarg; |
| 1495 | break; |
| 1496 | } |
| 1497 | } |
Kevin Wolf | e80fec7 | 2011-04-29 10:58:12 +0200 | [diff] [blame] | 1498 | if (optind >= argc) { |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1499 | help(); |
| 1500 | } |
| 1501 | filename = argv[optind++]; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1502 | |
| 1503 | /* Choose grow, shrink, or absolute resize mode */ |
| 1504 | switch (size[0]) { |
| 1505 | case '+': |
| 1506 | relative = 1; |
| 1507 | size++; |
| 1508 | break; |
| 1509 | case '-': |
| 1510 | relative = -1; |
| 1511 | size++; |
| 1512 | break; |
| 1513 | default: |
| 1514 | relative = 0; |
| 1515 | break; |
| 1516 | } |
| 1517 | |
| 1518 | /* Parse size */ |
| 1519 | param = parse_option_parameters("", resize_options, NULL); |
| 1520 | if (set_option_parameter(param, BLOCK_OPT_SIZE, size)) { |
| 1521 | /* Error message already printed when size parsing fails */ |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 1522 | ret = -1; |
| 1523 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1524 | } |
| 1525 | n = get_option_parameter(param, BLOCK_OPT_SIZE)->value.n; |
| 1526 | free_option_parameters(param); |
| 1527 | |
| 1528 | bs = bdrv_new_open(filename, fmt, BDRV_O_FLAGS | BDRV_O_RDWR); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1529 | if (!bs) { |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 1530 | ret = -1; |
| 1531 | goto out; |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1532 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1533 | |
| 1534 | if (relative) { |
| 1535 | total_size = bdrv_getlength(bs) + n * relative; |
| 1536 | } else { |
| 1537 | total_size = n; |
| 1538 | } |
| 1539 | if (total_size <= 0) { |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1540 | error_report("New image size must be positive"); |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1541 | ret = -1; |
| 1542 | goto out; |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1543 | } |
| 1544 | |
| 1545 | ret = bdrv_truncate(bs, total_size); |
| 1546 | switch (ret) { |
| 1547 | case 0: |
| 1548 | printf("Image resized.\n"); |
| 1549 | break; |
| 1550 | case -ENOTSUP: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1551 | error_report("This image format does not support resize"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1552 | break; |
| 1553 | case -EACCES: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1554 | error_report("Image is read-only"); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1555 | break; |
| 1556 | default: |
Jes Sorensen | 15654a6 | 2010-12-16 14:31:53 +0100 | [diff] [blame] | 1557 | error_report("Error resizing image (%d)", -ret); |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1558 | break; |
| 1559 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1560 | out: |
Jes Sorensen | 2a81998 | 2010-12-06 17:08:31 +0100 | [diff] [blame] | 1561 | if (bs) { |
| 1562 | bdrv_delete(bs); |
| 1563 | } |
MORITA Kazutaka | c2abcce | 2010-06-21 04:26:35 +0900 | [diff] [blame] | 1564 | if (ret) { |
| 1565 | return 1; |
| 1566 | } |
Stefan Hajnoczi | ae6b0ed | 2010-04-24 09:12:12 +0100 | [diff] [blame] | 1567 | return 0; |
| 1568 | } |
| 1569 | |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1570 | static const img_cmd_t img_cmds[] = { |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1571 | #define DEF(option, callback, arg_string) \ |
| 1572 | { option, callback }, |
| 1573 | #include "qemu-img-cmds.h" |
| 1574 | #undef DEF |
| 1575 | #undef GEN_DOCS |
| 1576 | { NULL, NULL, }, |
| 1577 | }; |
| 1578 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1579 | int main(int argc, char **argv) |
| 1580 | { |
Anthony Liguori | c227f09 | 2009-10-01 16:12:16 -0500 | [diff] [blame] | 1581 | const img_cmd_t *cmd; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1582 | const char *cmdname; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1583 | |
Kevin Wolf | 53f76e5 | 2010-12-16 15:10:32 +0100 | [diff] [blame] | 1584 | error_set_progname(argv[0]); |
| 1585 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1586 | bdrv_init(); |
| 1587 | if (argc < 2) |
| 1588 | help(); |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1589 | cmdname = argv[1]; |
aurel32 | 8f9b157 | 2009-02-09 18:14:31 +0000 | [diff] [blame] | 1590 | argc--; argv++; |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1591 | |
| 1592 | /* find the command */ |
| 1593 | for(cmd = img_cmds; cmd->name != NULL; cmd++) { |
| 1594 | if (!strcmp(cmdname, cmd->name)) { |
| 1595 | return cmd->handler(argc, argv); |
| 1596 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1597 | } |
Stuart Brady | 153859b | 2009-06-07 00:42:17 +0100 | [diff] [blame] | 1598 | |
| 1599 | /* not found */ |
| 1600 | help(); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 1601 | return 0; |
| 1602 | } |