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 | 84f2e8e | 2007-02-05 20:21:32 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003-2007 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" |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 25 | #include "block_int.h" |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 26 | #include <assert.h> |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 27 | |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 28 | #ifdef _WIN32 |
| 29 | #include <windows.h> |
| 30 | #endif |
| 31 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 32 | void *get_mmap_addr(unsigned long size) |
| 33 | { |
| 34 | return NULL; |
| 35 | } |
| 36 | |
| 37 | void qemu_free(void *ptr) |
| 38 | { |
| 39 | free(ptr); |
| 40 | } |
| 41 | |
| 42 | void *qemu_malloc(size_t size) |
| 43 | { |
| 44 | return malloc(size); |
| 45 | } |
| 46 | |
| 47 | void *qemu_mallocz(size_t size) |
| 48 | { |
| 49 | void *ptr; |
| 50 | ptr = qemu_malloc(size); |
| 51 | if (!ptr) |
| 52 | return NULL; |
| 53 | memset(ptr, 0, size); |
| 54 | return ptr; |
| 55 | } |
| 56 | |
| 57 | char *qemu_strdup(const char *str) |
| 58 | { |
| 59 | char *ptr; |
| 60 | ptr = qemu_malloc(strlen(str) + 1); |
| 61 | if (!ptr) |
| 62 | return NULL; |
| 63 | strcpy(ptr, str); |
| 64 | return ptr; |
| 65 | } |
| 66 | |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 67 | static void __attribute__((noreturn)) error(const char *fmt, ...) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 68 | { |
| 69 | va_list ap; |
| 70 | va_start(ap, fmt); |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 71 | fprintf(stderr, "qemu-img: "); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 72 | vfprintf(stderr, fmt, ap); |
| 73 | fprintf(stderr, "\n"); |
| 74 | exit(1); |
| 75 | va_end(ap); |
| 76 | } |
| 77 | |
| 78 | static void format_print(void *opaque, const char *name) |
| 79 | { |
| 80 | printf(" %s", name); |
| 81 | } |
| 82 | |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 83 | static void help(void) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 84 | { |
bellard | 84f2e8e | 2007-02-05 20:21:32 +0000 | [diff] [blame] | 85 | printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2007 Fabrice Bellard\n" |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 86 | "usage: qemu-img command [command options]\n" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 87 | "QEMU disk image utility\n" |
| 88 | "\n" |
| 89 | "Command syntax:\n" |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 90 | " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 91 | " commit [-f fmt] filename\n" |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 92 | " convert [-c] [-e] [-6] [-f fmt] filename [filename2 [...]] [-O output_fmt] output_filename\n" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 93 | " info [-f fmt] filename\n" |
| 94 | "\n" |
| 95 | "Command parameters:\n" |
| 96 | " 'filename' is a disk image filename\n" |
| 97 | " 'base_image' is the read-only disk image which is used as base for a copy on\n" |
| 98 | " write image; the copy on write image only stores the modified data\n" |
| 99 | " 'fmt' is the disk image format. It is guessed automatically in most cases\n" |
| 100 | " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n" |
| 101 | " and 'G' (gigabyte) are supported\n" |
| 102 | " 'output_filename' is the destination disk image filename\n" |
| 103 | " 'output_fmt' is the destination format\n" |
| 104 | " '-c' indicates that target image must be compressed (qcow format only)\n" |
| 105 | " '-e' indicates that the target image must be encrypted (qcow format only)\n" |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 106 | " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 107 | ); |
| 108 | printf("\nSupported format:"); |
| 109 | bdrv_iterate_format(format_print, NULL); |
| 110 | printf("\n"); |
| 111 | exit(1); |
| 112 | } |
| 113 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 114 | #if defined(WIN32) |
| 115 | /* XXX: put correct support for win32 */ |
| 116 | static int read_password(char *buf, int buf_size) |
| 117 | { |
| 118 | int c, i; |
| 119 | printf("Password: "); |
| 120 | fflush(stdout); |
| 121 | i = 0; |
| 122 | for(;;) { |
| 123 | c = getchar(); |
| 124 | if (c == '\n') |
| 125 | break; |
| 126 | if (i < (buf_size - 1)) |
| 127 | buf[i++] = c; |
| 128 | } |
| 129 | buf[i] = '\0'; |
| 130 | return 0; |
| 131 | } |
| 132 | |
| 133 | #else |
| 134 | |
| 135 | #include <termios.h> |
| 136 | |
| 137 | static struct termios oldtty; |
| 138 | |
| 139 | static void term_exit(void) |
| 140 | { |
| 141 | tcsetattr (0, TCSANOW, &oldtty); |
| 142 | } |
| 143 | |
| 144 | static void term_init(void) |
| 145 | { |
| 146 | struct termios tty; |
| 147 | |
| 148 | tcgetattr (0, &tty); |
| 149 | oldtty = tty; |
| 150 | |
| 151 | tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |
| 152 | |INLCR|IGNCR|ICRNL|IXON); |
| 153 | tty.c_oflag |= OPOST; |
| 154 | tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); |
| 155 | tty.c_cflag &= ~(CSIZE|PARENB); |
| 156 | tty.c_cflag |= CS8; |
| 157 | tty.c_cc[VMIN] = 1; |
| 158 | tty.c_cc[VTIME] = 0; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 159 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 160 | tcsetattr (0, TCSANOW, &tty); |
| 161 | |
| 162 | atexit(term_exit); |
| 163 | } |
| 164 | |
pbrook | 3f379ab | 2007-11-11 03:33:13 +0000 | [diff] [blame] | 165 | static int read_password(char *buf, int buf_size) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 166 | { |
| 167 | uint8_t ch; |
| 168 | int i, ret; |
| 169 | |
| 170 | printf("password: "); |
| 171 | fflush(stdout); |
| 172 | term_init(); |
| 173 | i = 0; |
| 174 | for(;;) { |
| 175 | ret = read(0, &ch, 1); |
| 176 | if (ret == -1) { |
| 177 | if (errno == EAGAIN || errno == EINTR) { |
| 178 | continue; |
| 179 | } else { |
| 180 | ret = -1; |
| 181 | break; |
| 182 | } |
| 183 | } else if (ret == 0) { |
| 184 | ret = -1; |
| 185 | break; |
| 186 | } else { |
| 187 | if (ch == '\r') { |
| 188 | ret = 0; |
| 189 | break; |
| 190 | } |
| 191 | if (i < (buf_size - 1)) |
| 192 | buf[i++] = ch; |
| 193 | } |
| 194 | } |
| 195 | term_exit(); |
| 196 | buf[i] = '\0'; |
| 197 | printf("\n"); |
| 198 | return ret; |
| 199 | } |
| 200 | #endif |
| 201 | |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 202 | static BlockDriverState *bdrv_new_open(const char *filename, |
| 203 | const char *fmt) |
| 204 | { |
| 205 | BlockDriverState *bs; |
| 206 | BlockDriver *drv; |
| 207 | char password[256]; |
| 208 | |
| 209 | bs = bdrv_new(""); |
| 210 | if (!bs) |
| 211 | error("Not enough memory"); |
| 212 | if (fmt) { |
| 213 | drv = bdrv_find_format(fmt); |
| 214 | if (!drv) |
| 215 | error("Unknown file format '%s'", fmt); |
| 216 | } else { |
| 217 | drv = NULL; |
| 218 | } |
| 219 | if (bdrv_open2(bs, filename, 0, drv) < 0) { |
| 220 | error("Could not open '%s'", filename); |
| 221 | } |
| 222 | if (bdrv_is_encrypted(bs)) { |
| 223 | printf("Disk image '%s' is encrypted.\n", filename); |
| 224 | if (read_password(password, sizeof(password)) < 0) |
| 225 | error("No password given"); |
| 226 | if (bdrv_set_key(bs, password) < 0) |
| 227 | error("invalid password"); |
| 228 | } |
| 229 | return bs; |
| 230 | } |
| 231 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 232 | static int img_create(int argc, char **argv) |
| 233 | { |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 234 | int c, ret, flags; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 235 | const char *fmt = "raw"; |
| 236 | const char *filename; |
| 237 | const char *base_filename = NULL; |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame^] | 238 | uint64_t size; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 239 | const char *p; |
| 240 | BlockDriver *drv; |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 241 | |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 242 | flags = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 243 | for(;;) { |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 244 | c = getopt(argc, argv, "b:f:he6"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 245 | if (c == -1) |
| 246 | break; |
| 247 | switch(c) { |
| 248 | case 'h': |
| 249 | help(); |
| 250 | break; |
| 251 | case 'b': |
| 252 | base_filename = optarg; |
| 253 | break; |
| 254 | case 'f': |
| 255 | fmt = optarg; |
| 256 | break; |
| 257 | case 'e': |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 258 | flags |= BLOCK_FLAG_ENCRYPT; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 259 | break; |
ths | d8871c5 | 2007-10-24 16:11:42 +0000 | [diff] [blame] | 260 | case '6': |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 261 | flags |= BLOCK_FLAG_COMPAT6; |
ths | d8871c5 | 2007-10-24 16:11:42 +0000 | [diff] [blame] | 262 | break; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 263 | } |
| 264 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 265 | if (optind >= argc) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 266 | help(); |
| 267 | filename = argv[optind++]; |
| 268 | size = 0; |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 269 | if (base_filename) { |
| 270 | BlockDriverState *bs; |
| 271 | bs = bdrv_new_open(base_filename, NULL); |
| 272 | bdrv_get_geometry(bs, &size); |
| 273 | size *= 512; |
| 274 | bdrv_delete(bs); |
| 275 | } else { |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 276 | if (optind >= argc) |
| 277 | help(); |
| 278 | p = argv[optind]; |
| 279 | size = strtoul(p, (char **)&p, 0); |
| 280 | if (*p == 'M') { |
| 281 | size *= 1024 * 1024; |
| 282 | } else if (*p == 'G') { |
| 283 | size *= 1024 * 1024 * 1024; |
| 284 | } else if (*p == 'k' || *p == 'K' || *p == '\0') { |
| 285 | size *= 1024; |
| 286 | } else { |
| 287 | help(); |
| 288 | } |
| 289 | } |
| 290 | drv = bdrv_find_format(fmt); |
| 291 | if (!drv) |
| 292 | error("Unknown file format '%s'", fmt); |
ths | 0cfec83 | 2007-06-23 16:02:43 +0000 | [diff] [blame] | 293 | printf("Formatting '%s', fmt=%s", |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 294 | filename, fmt); |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 295 | if (flags & BLOCK_FLAG_ENCRYPT) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 296 | printf(", encrypted"); |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 297 | if (flags & BLOCK_FLAG_COMPAT6) |
| 298 | printf(", compatibility level=6"); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 299 | if (base_filename) { |
| 300 | printf(", backing_file=%s", |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 301 | base_filename); |
bellard | 75c2380 | 2004-08-27 21:28:58 +0000 | [diff] [blame] | 302 | } |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame^] | 303 | printf(", size=%" PRIu64 " kB\n", size / 1024); |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 304 | ret = bdrv_create(drv, filename, size / 512, base_filename, flags); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 305 | if (ret < 0) { |
| 306 | if (ret == -ENOTSUP) { |
bellard | 3c56521 | 2004-09-29 21:29:14 +0000 | [diff] [blame] | 307 | error("Formatting or formatting option not supported for file format '%s'", fmt); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 308 | } else { |
| 309 | error("Error while formatting"); |
| 310 | } |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | |
| 315 | static int img_commit(int argc, char **argv) |
| 316 | { |
| 317 | int c, ret; |
| 318 | const char *filename, *fmt; |
| 319 | BlockDriver *drv; |
| 320 | BlockDriverState *bs; |
| 321 | |
| 322 | fmt = NULL; |
| 323 | for(;;) { |
| 324 | c = getopt(argc, argv, "f:h"); |
| 325 | if (c == -1) |
| 326 | break; |
| 327 | switch(c) { |
| 328 | case 'h': |
| 329 | help(); |
| 330 | break; |
| 331 | case 'f': |
| 332 | fmt = optarg; |
| 333 | break; |
| 334 | } |
| 335 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 336 | if (optind >= argc) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 337 | help(); |
| 338 | filename = argv[optind++]; |
| 339 | |
| 340 | bs = bdrv_new(""); |
| 341 | if (!bs) |
| 342 | error("Not enough memory"); |
| 343 | if (fmt) { |
| 344 | drv = bdrv_find_format(fmt); |
| 345 | if (!drv) |
| 346 | error("Unknown file format '%s'", fmt); |
| 347 | } else { |
| 348 | drv = NULL; |
| 349 | } |
| 350 | if (bdrv_open2(bs, filename, 0, drv) < 0) { |
| 351 | error("Could not open '%s'", filename); |
| 352 | } |
| 353 | ret = bdrv_commit(bs); |
| 354 | switch(ret) { |
| 355 | case 0: |
| 356 | printf("Image committed.\n"); |
| 357 | break; |
| 358 | case -ENOENT: |
| 359 | error("No disk inserted"); |
| 360 | break; |
| 361 | case -EACCES: |
| 362 | error("Image is read-only"); |
| 363 | break; |
| 364 | case -ENOTSUP: |
| 365 | error("Image is already committed"); |
| 366 | break; |
| 367 | default: |
| 368 | error("Error while committing image"); |
| 369 | break; |
| 370 | } |
| 371 | |
| 372 | bdrv_delete(bs); |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | static int is_not_zero(const uint8_t *sector, int len) |
| 377 | { |
| 378 | int i; |
| 379 | len >>= 2; |
| 380 | for(i = 0;i < len; i++) { |
| 381 | if (((uint32_t *)sector)[i] != 0) |
| 382 | return 1; |
| 383 | } |
| 384 | return 0; |
| 385 | } |
| 386 | |
| 387 | static int is_allocated_sectors(const uint8_t *buf, int n, int *pnum) |
| 388 | { |
| 389 | int v, i; |
| 390 | |
| 391 | if (n <= 0) { |
| 392 | *pnum = 0; |
| 393 | return 0; |
| 394 | } |
| 395 | v = is_not_zero(buf, 512); |
| 396 | for(i = 1; i < n; i++) { |
| 397 | buf += 512; |
| 398 | if (v != is_not_zero(buf, 512)) |
| 399 | break; |
| 400 | } |
| 401 | *pnum = i; |
| 402 | return v; |
| 403 | } |
| 404 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 405 | #define IO_BUF_SIZE 65536 |
| 406 | |
| 407 | static int img_convert(int argc, char **argv) |
| 408 | { |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 409 | int c, ret, n, n1, bs_n, bs_i, flags, cluster_size, cluster_sectors; |
| 410 | const char *fmt, *out_fmt, *out_filename; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 411 | BlockDriver *drv; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 412 | BlockDriverState **bs, *out_bs; |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame^] | 413 | int64_t total_sectors, nb_sectors, sector_num, bs_offset; |
| 414 | uint64_t bs_sectors; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 415 | uint8_t buf[IO_BUF_SIZE]; |
| 416 | const uint8_t *buf1; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 417 | BlockDriverInfo bdi; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 418 | |
| 419 | fmt = NULL; |
| 420 | out_fmt = "raw"; |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 421 | flags = 0; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 422 | for(;;) { |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 423 | c = getopt(argc, argv, "f:O:hce6"); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 424 | if (c == -1) |
| 425 | break; |
| 426 | switch(c) { |
| 427 | case 'h': |
| 428 | help(); |
| 429 | break; |
| 430 | case 'f': |
| 431 | fmt = optarg; |
| 432 | break; |
| 433 | case 'O': |
| 434 | out_fmt = optarg; |
| 435 | break; |
| 436 | case 'c': |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 437 | flags |= BLOCK_FLAG_COMPRESS; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 438 | break; |
| 439 | case 'e': |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 440 | flags |= BLOCK_FLAG_ENCRYPT; |
| 441 | break; |
| 442 | case '6': |
| 443 | flags |= BLOCK_FLAG_COMPAT6; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 444 | break; |
| 445 | } |
| 446 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 447 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 448 | bs_n = argc - optind - 1; |
| 449 | if (bs_n < 1) help(); |
| 450 | |
| 451 | out_filename = argv[argc - 1]; |
| 452 | |
| 453 | bs = calloc(bs_n, sizeof(BlockDriverState *)); |
| 454 | if (!bs) |
| 455 | error("Out of memory"); |
| 456 | |
| 457 | total_sectors = 0; |
| 458 | for (bs_i = 0; bs_i < bs_n; bs_i++) { |
| 459 | bs[bs_i] = bdrv_new_open(argv[optind + bs_i], fmt); |
| 460 | if (!bs[bs_i]) |
| 461 | error("Could not open '%s'", argv[optind + bs_i]); |
| 462 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 463 | total_sectors += bs_sectors; |
| 464 | } |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 465 | |
| 466 | drv = bdrv_find_format(out_fmt); |
| 467 | if (!drv) |
ths | d34dda5 | 2007-02-10 22:59:40 +0000 | [diff] [blame] | 468 | error("Unknown file format '%s'", out_fmt); |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 469 | if (flags & BLOCK_FLAG_COMPRESS && drv != &bdrv_qcow && drv != &bdrv_qcow2) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 470 | error("Compression not supported for this file format"); |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 471 | if (flags & BLOCK_FLAG_ENCRYPT && drv != &bdrv_qcow && drv != &bdrv_qcow2) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 472 | error("Encryption not supported for this file format"); |
ths | d8871c5 | 2007-10-24 16:11:42 +0000 | [diff] [blame] | 473 | if (flags & BLOCK_FLAG_COMPAT6 && drv != &bdrv_vmdk) |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 474 | error("Alternative compatibility level not supported for this file format"); |
| 475 | if (flags & BLOCK_FLAG_ENCRYPT && flags & BLOCK_FLAG_COMPRESS) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 476 | error("Compression and encryption not supported at the same time"); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 477 | |
ths | ec36ba1 | 2007-09-16 21:59:02 +0000 | [diff] [blame] | 478 | ret = bdrv_create(drv, out_filename, total_sectors, NULL, flags); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 479 | if (ret < 0) { |
| 480 | if (ret == -ENOTSUP) { |
bellard | 3c56521 | 2004-09-29 21:29:14 +0000 | [diff] [blame] | 481 | error("Formatting not supported for file format '%s'", fmt); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 482 | } else { |
| 483 | error("Error while formatting '%s'", out_filename); |
| 484 | } |
| 485 | } |
ths | 3b46e62 | 2007-09-17 08:09:54 +0000 | [diff] [blame] | 486 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 487 | out_bs = bdrv_new_open(out_filename, out_fmt); |
| 488 | |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 489 | bs_i = 0; |
| 490 | bs_offset = 0; |
| 491 | bdrv_get_geometry(bs[0], &bs_sectors); |
| 492 | |
| 493 | if (flags & BLOCK_FLAG_COMPRESS) { |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 494 | if (bdrv_get_info(out_bs, &bdi) < 0) |
| 495 | error("could not get block driver info"); |
| 496 | cluster_size = bdi.cluster_size; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 497 | if (cluster_size <= 0 || cluster_size > IO_BUF_SIZE) |
| 498 | error("invalid cluster size"); |
| 499 | cluster_sectors = cluster_size >> 9; |
| 500 | sector_num = 0; |
| 501 | for(;;) { |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 502 | int64_t bs_num; |
| 503 | int remainder; |
| 504 | uint8_t *buf2; |
| 505 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 506 | nb_sectors = total_sectors - sector_num; |
| 507 | if (nb_sectors <= 0) |
| 508 | break; |
| 509 | if (nb_sectors >= cluster_sectors) |
| 510 | n = cluster_sectors; |
| 511 | else |
| 512 | n = nb_sectors; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 513 | |
| 514 | bs_num = sector_num - bs_offset; |
| 515 | assert (bs_num >= 0); |
| 516 | remainder = n; |
| 517 | buf2 = buf; |
| 518 | while (remainder > 0) { |
| 519 | int nlow; |
| 520 | while (bs_num == bs_sectors) { |
| 521 | bs_i++; |
| 522 | assert (bs_i < bs_n); |
| 523 | bs_offset += bs_sectors; |
| 524 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 525 | bs_num = 0; |
| 526 | /* printf("changing part: sector_num=%lld, " |
| 527 | "bs_i=%d, bs_offset=%lld, bs_sectors=%lld\n", |
| 528 | sector_num, bs_i, bs_offset, bs_sectors); */ |
| 529 | } |
| 530 | assert (bs_num < bs_sectors); |
| 531 | |
| 532 | nlow = (remainder > bs_sectors - bs_num) ? bs_sectors - bs_num : remainder; |
| 533 | |
| 534 | if (bdrv_read(bs[bs_i], bs_num, buf2, nlow) < 0) |
| 535 | error("error while reading"); |
| 536 | |
| 537 | buf2 += nlow * 512; |
| 538 | bs_num += nlow; |
| 539 | |
| 540 | remainder -= nlow; |
| 541 | } |
| 542 | assert (remainder == 0); |
| 543 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 544 | if (n < cluster_sectors) |
| 545 | memset(buf + n * 512, 0, cluster_size - n * 512); |
| 546 | if (is_not_zero(buf, cluster_size)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 547 | if (bdrv_write_compressed(out_bs, sector_num, buf, |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 548 | cluster_sectors) != 0) |
bellard | ec3757d | 2006-06-14 15:50:07 +0000 | [diff] [blame] | 549 | error("error while compressing sector %" PRId64, |
| 550 | sector_num); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 551 | } |
| 552 | sector_num += n; |
| 553 | } |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 554 | /* signal EOF to align */ |
| 555 | bdrv_write_compressed(out_bs, 0, NULL, 0); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 556 | } else { |
| 557 | sector_num = 0; |
| 558 | for(;;) { |
| 559 | nb_sectors = total_sectors - sector_num; |
| 560 | if (nb_sectors <= 0) |
| 561 | break; |
| 562 | if (nb_sectors >= (IO_BUF_SIZE / 512)) |
| 563 | n = (IO_BUF_SIZE / 512); |
| 564 | else |
| 565 | n = nb_sectors; |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 566 | |
| 567 | while (sector_num - bs_offset >= bs_sectors) { |
| 568 | bs_i ++; |
| 569 | assert (bs_i < bs_n); |
| 570 | bs_offset += bs_sectors; |
| 571 | bdrv_get_geometry(bs[bs_i], &bs_sectors); |
| 572 | /* printf("changing part: sector_num=%lld, bs_i=%d, " |
| 573 | "bs_offset=%lld, bs_sectors=%lld\n", |
| 574 | sector_num, bs_i, bs_offset, bs_sectors); */ |
| 575 | } |
| 576 | |
| 577 | if (n > bs_offset + bs_sectors - sector_num) |
| 578 | n = bs_offset + bs_sectors - sector_num; |
| 579 | |
| 580 | if (bdrv_read(bs[bs_i], sector_num - bs_offset, buf, n) < 0) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 581 | error("error while reading"); |
| 582 | /* NOTE: at the same time we convert, we do not write zero |
| 583 | sectors to have a chance to compress the image. Ideally, we |
| 584 | should add a specific call to have the info to go faster */ |
| 585 | buf1 = buf; |
| 586 | while (n > 0) { |
| 587 | if (is_allocated_sectors(buf1, n, &n1)) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 588 | if (bdrv_write(out_bs, sector_num, buf1, n1) < 0) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 589 | error("error while writing"); |
| 590 | } |
| 591 | sector_num += n1; |
| 592 | n -= n1; |
| 593 | buf1 += n1 * 512; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | bdrv_delete(out_bs); |
balrog | 926c2d2 | 2007-10-31 01:11:44 +0000 | [diff] [blame] | 598 | for (bs_i = 0; bs_i < bs_n; bs_i++) |
| 599 | bdrv_delete(bs[bs_i]); |
| 600 | free(bs); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 601 | return 0; |
| 602 | } |
| 603 | |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 604 | #ifdef _WIN32 |
| 605 | static int64_t get_allocated_file_size(const char *filename) |
| 606 | { |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 607 | typedef DWORD (WINAPI * get_compressed_t)(const char *filename, DWORD *high); |
| 608 | get_compressed_t get_compressed; |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 609 | struct _stati64 st; |
bellard | e844533 | 2006-06-14 15:32:10 +0000 | [diff] [blame] | 610 | |
| 611 | /* WinNT support GetCompressedFileSize to determine allocate size */ |
| 612 | get_compressed = (get_compressed_t) GetProcAddress(GetModuleHandle("kernel32"), "GetCompressedFileSizeA"); |
| 613 | if (get_compressed) { |
| 614 | DWORD high, low; |
| 615 | low = get_compressed(filename, &high); |
| 616 | if (low != 0xFFFFFFFFlu || GetLastError() == NO_ERROR) |
| 617 | return (((int64_t) high) << 32) + low; |
| 618 | } |
| 619 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 620 | if (_stati64(filename, &st) < 0) |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 621 | return -1; |
| 622 | return st.st_size; |
| 623 | } |
| 624 | #else |
| 625 | static int64_t get_allocated_file_size(const char *filename) |
| 626 | { |
| 627 | struct stat st; |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 628 | if (stat(filename, &st) < 0) |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 629 | return -1; |
| 630 | return (int64_t)st.st_blocks * 512; |
| 631 | } |
| 632 | #endif |
| 633 | |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 634 | static void dump_snapshots(BlockDriverState *bs) |
| 635 | { |
| 636 | QEMUSnapshotInfo *sn_tab, *sn; |
| 637 | int nb_sns, i; |
| 638 | char buf[256]; |
| 639 | |
| 640 | nb_sns = bdrv_snapshot_list(bs, &sn_tab); |
| 641 | if (nb_sns <= 0) |
| 642 | return; |
| 643 | printf("Snapshot list:\n"); |
| 644 | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL)); |
| 645 | for(i = 0; i < nb_sns; i++) { |
| 646 | sn = &sn_tab[i]; |
| 647 | printf("%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn)); |
| 648 | } |
| 649 | qemu_free(sn_tab); |
| 650 | } |
| 651 | |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 652 | static int img_info(int argc, char **argv) |
| 653 | { |
| 654 | int c; |
| 655 | const char *filename, *fmt; |
| 656 | BlockDriver *drv; |
| 657 | BlockDriverState *bs; |
| 658 | char fmt_name[128], size_buf[128], dsize_buf[128]; |
ths | 96b8f13 | 2007-12-17 01:35:20 +0000 | [diff] [blame^] | 659 | uint64_t total_sectors; |
| 660 | int64_t allocated_size; |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 661 | char backing_filename[1024]; |
| 662 | char backing_filename2[1024]; |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 663 | BlockDriverInfo bdi; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 664 | |
| 665 | fmt = NULL; |
| 666 | for(;;) { |
| 667 | c = getopt(argc, argv, "f:h"); |
| 668 | if (c == -1) |
| 669 | break; |
| 670 | switch(c) { |
| 671 | case 'h': |
| 672 | help(); |
| 673 | break; |
| 674 | case 'f': |
| 675 | fmt = optarg; |
| 676 | break; |
| 677 | } |
| 678 | } |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 679 | if (optind >= argc) |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 680 | help(); |
| 681 | filename = argv[optind++]; |
| 682 | |
| 683 | bs = bdrv_new(""); |
| 684 | if (!bs) |
| 685 | error("Not enough memory"); |
| 686 | if (fmt) { |
| 687 | drv = bdrv_find_format(fmt); |
| 688 | if (!drv) |
| 689 | error("Unknown file format '%s'", fmt); |
| 690 | } else { |
| 691 | drv = NULL; |
| 692 | } |
| 693 | if (bdrv_open2(bs, filename, 0, drv) < 0) { |
| 694 | error("Could not open '%s'", filename); |
| 695 | } |
| 696 | bdrv_get_format(bs, fmt_name, sizeof(fmt_name)); |
| 697 | bdrv_get_geometry(bs, &total_sectors); |
| 698 | get_human_readable_size(size_buf, sizeof(size_buf), total_sectors * 512); |
bellard | 57d1a2b | 2004-08-03 21:15:11 +0000 | [diff] [blame] | 699 | allocated_size = get_allocated_file_size(filename); |
| 700 | if (allocated_size < 0) |
bellard | de167e4 | 2005-04-28 21:15:08 +0000 | [diff] [blame] | 701 | sprintf(dsize_buf, "unavailable"); |
| 702 | else |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 703 | get_human_readable_size(dsize_buf, sizeof(dsize_buf), |
bellard | de167e4 | 2005-04-28 21:15:08 +0000 | [diff] [blame] | 704 | allocated_size); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 705 | printf("image: %s\n" |
| 706 | "file format: %s\n" |
bellard | ec3757d | 2006-06-14 15:50:07 +0000 | [diff] [blame] | 707 | "virtual size: %s (%" PRId64 " bytes)\n" |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 708 | "disk size: %s\n", |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 709 | filename, fmt_name, size_buf, |
bellard | ec3757d | 2006-06-14 15:50:07 +0000 | [diff] [blame] | 710 | (total_sectors * 512), |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 711 | dsize_buf); |
| 712 | if (bdrv_is_encrypted(bs)) |
| 713 | printf("encrypted: yes\n"); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 714 | if (bdrv_get_info(bs, &bdi) >= 0) { |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 715 | if (bdi.cluster_size != 0) |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 716 | printf("cluster_size: %d\n", bdi.cluster_size); |
| 717 | } |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 718 | bdrv_get_backing_filename(bs, backing_filename, sizeof(backing_filename)); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 719 | if (backing_filename[0] != '\0') { |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 720 | path_combine(backing_filename2, sizeof(backing_filename2), |
| 721 | filename, backing_filename); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 722 | printf("backing file: %s (actual path: %s)\n", |
bellard | 93b6b2a | 2006-08-01 15:51:11 +0000 | [diff] [blame] | 723 | backing_filename, |
| 724 | backing_filename2); |
bellard | faea38e | 2006-08-05 21:31:00 +0000 | [diff] [blame] | 725 | } |
| 726 | dump_snapshots(bs); |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 727 | bdrv_delete(bs); |
| 728 | return 0; |
| 729 | } |
| 730 | |
| 731 | int main(int argc, char **argv) |
| 732 | { |
| 733 | const char *cmd; |
| 734 | |
| 735 | bdrv_init(); |
| 736 | if (argc < 2) |
| 737 | help(); |
| 738 | cmd = argv[1]; |
bellard | e388818 | 2004-10-09 16:44:06 +0000 | [diff] [blame] | 739 | optind++; |
bellard | ea2384d | 2004-08-01 21:59:26 +0000 | [diff] [blame] | 740 | if (!strcmp(cmd, "create")) { |
| 741 | img_create(argc, argv); |
| 742 | } else if (!strcmp(cmd, "commit")) { |
| 743 | img_commit(argc, argv); |
| 744 | } else if (!strcmp(cmd, "convert")) { |
| 745 | img_convert(argc, argv); |
| 746 | } else if (!strcmp(cmd, "info")) { |
| 747 | img_info(argc, argv); |
| 748 | } else { |
| 749 | help(); |
| 750 | } |
| 751 | return 0; |
| 752 | } |