?? qemu-img.c
字號:
/* * QEMU disk image utility * * Copyright (c) 2003-2008 Fabrice Bellard * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */#include "qemu-common.h"#include "block_int.h"#include <assert.h>#ifdef _WIN32#define WIN32_LEAN_AND_MEAN#include <windows.h>#endifvoid *get_mmap_addr(unsigned long size){ return NULL;}void qemu_free(void *ptr){ free(ptr);}void *qemu_malloc(size_t size){ return malloc(size);}void *qemu_mallocz(size_t size){ void *ptr; ptr = qemu_malloc(size); if (!ptr) return NULL; memset(ptr, 0, size); return ptr;}char *qemu_strdup(const char *str){ char *ptr; ptr = qemu_malloc(strlen(str) + 1); if (!ptr) return NULL; strcpy(ptr, str); return ptr;}static void __attribute__((noreturn)) error(const char *fmt, ...){ va_list ap; va_start(ap, fmt); fprintf(stderr, "qemu-img: "); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); exit(1); va_end(ap);}static void format_print(void *opaque, const char *name){ printf(" %s", name);}static void help(void){ printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2008 Fabrice Bellard\n" "usage: qemu-img command [command options]\n" "QEMU disk image utility\n" "\n" "Command syntax:\n" " create [-e] [-6] [-b base_image] [-f fmt] filename [size]\n" " commit [-f fmt] filename\n" " convert [-c] [-e] [-6] [-f fmt] filename [filename2 [...]] [-O output_fmt] output_filename\n" " info [-f fmt] filename\n" "\n" "Command parameters:\n" " 'filename' is a disk image filename\n" " 'base_image' is the read-only disk image which is used as base for a copy on\n" " write image; the copy on write image only stores the modified data\n" " 'fmt' is the disk image format. It is guessed automatically in most cases\n" " 'size' is the disk image size in kilobytes. Optional suffixes 'M' (megabyte)\n" " and 'G' (gigabyte) are supported\n" " 'output_filename' is the destination disk image filename\n" " 'output_fmt' is the destination format\n" " '-c' indicates that target image must be compressed (qcow format only)\n" " '-e' indicates that the target image must be encrypted (qcow format only)\n" " '-6' indicates that the target image must use compatibility level 6 (vmdk format only)\n" ); printf("\nSupported format:"); bdrv_iterate_format(format_print, NULL); printf("\n"); exit(1);}#if defined(WIN32)/* XXX: put correct support for win32 */static int read_password(char *buf, int buf_size){ int c, i; printf("Password: "); fflush(stdout); i = 0; for(;;) { c = getchar(); if (c == '\n') break; if (i < (buf_size - 1)) buf[i++] = c; } buf[i] = '\0'; return 0;}#else#include <termios.h>static struct termios oldtty;static void term_exit(void){ tcsetattr (0, TCSANOW, &oldtty);}static void term_init(void){ struct termios tty; tcgetattr (0, &tty); oldtty = tty; tty.c_iflag &= ~(IGNBRK|BRKINT|PARMRK|ISTRIP |INLCR|IGNCR|ICRNL|IXON); tty.c_oflag |= OPOST; tty.c_lflag &= ~(ECHO|ECHONL|ICANON|IEXTEN); tty.c_cflag &= ~(CSIZE|PARENB); tty.c_cflag |= CS8; tty.c_cc[VMIN] = 1; tty.c_cc[VTIME] = 0; tcsetattr (0, TCSANOW, &tty); atexit(term_exit);}static int read_password(char *buf, int buf_size){ uint8_t ch; int i, ret; printf("password: "); fflush(stdout); term_init(); i = 0; for(;;) { ret = read(0, &ch, 1); if (ret == -1) { if (errno == EAGAIN || errno == EINTR) { continue; } else { ret = -1; break; } } else if (ret == 0) { ret = -1; break; } else { if (ch == '\r') { ret = 0; break; } if (i < (buf_size - 1)) buf[i++] = ch; } } term_exit(); buf[i] = '\0'; printf("\n"); return ret;}#endifstatic BlockDriverState *bdrv_new_open(const char *filename, const char *fmt){ BlockDriverState *bs; BlockDriver *drv; char password[256]; bs = bdrv_new(""); if (!bs) error("Not enough memory"); if (fmt) { drv = bdrv_find_format(fmt); if (!drv) error("Unknown file format '%s'", fmt); } else { drv = NULL; } if (bdrv_open2(bs, filename, 0, drv) < 0) { error("Could not open '%s'", filename); } if (bdrv_is_encrypted(bs)) { printf("Disk image '%s' is encrypted.\n", filename); if (read_password(password, sizeof(password)) < 0) error("No password given"); if (bdrv_set_key(bs, password) < 0) error("invalid password"); } return bs;}static int img_create(int argc, char **argv){ int c, ret, flags; const char *fmt = "raw"; const char *filename; const char *base_filename = NULL; uint64_t size; const char *p; BlockDriver *drv; flags = 0; for(;;) { c = getopt(argc, argv, "b:f:he6"); if (c == -1) break; switch(c) { case 'h': help(); break; case 'b': base_filename = optarg; break; case 'f': fmt = optarg; break; case 'e': flags |= BLOCK_FLAG_ENCRYPT; break; case '6': flags |= BLOCK_FLAG_COMPAT6; break; } } if (optind >= argc) help(); filename = argv[optind++]; size = 0; if (base_filename) { BlockDriverState *bs; bs = bdrv_new_open(base_filename, NULL); bdrv_get_geometry(bs, &size); size *= 512; bdrv_delete(bs); } else { if (optind >= argc) help(); p = argv[optind]; size = strtoul(p, (char **)&p, 0); if (*p == 'M') { size *= 1024 * 1024; } else if (*p == 'G') { size *= 1024 * 1024 * 1024; } else if (*p == 'k' || *p == 'K' || *p == '\0') { size *= 1024; } else { help(); } } drv = bdrv_find_format(fmt); if (!drv) error("Unknown file format '%s'", fmt); printf("Formatting '%s', fmt=%s", filename, fmt); if (flags & BLOCK_FLAG_ENCRYPT) printf(", encrypted"); if (flags & BLOCK_FLAG_COMPAT6) printf(", compatibility level=6"); if (base_filename) { printf(", backing_file=%s", base_filename); } printf(", size=%" PRIu64 " kB\n", size / 1024); ret = bdrv_create(drv, filename, size / 512, base_filename, flags); if (ret < 0) { if (ret == -ENOTSUP) { error("Formatting or formatting option not supported for file format '%s'", fmt); } else { error("Error while formatting"); } } return 0;}static int img_commit(int argc, char **argv){ int c, ret; const char *filename, *fmt; BlockDriver *drv; BlockDriverState *bs; fmt = NULL; for(;;) { c = getopt(argc, argv, "f:h"); if (c == -1) break; switch(c) { case 'h': help(); break; case 'f': fmt = optarg; break; } } if (optind >= argc) help(); filename = argv[optind++]; bs = bdrv_new(""); if (!bs) error("Not enough memory"); if (fmt) { drv = bdrv_find_format(fmt); if (!drv) error("Unknown file format '%s'", fmt); } else { drv = NULL; } if (bdrv_open2(bs, filename, 0, drv) < 0) { error("Could not open '%s'", filename); } ret = bdrv_commit(bs); switch(ret) { case 0: printf("Image committed.\n"); break; case -ENOENT: error("No disk inserted"); break; case -EACCES: error("Image is read-only"); break; case -ENOTSUP: error("Image is already committed"); break; default: error("Error while committing image"); break; } bdrv_delete(bs); return 0;}static int is_not_zero(const uint8_t *sector, int len)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -