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