📄 qemu-img.c
字号:
/* * create a COW disk image * * Copyright (c) 2003 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 "vl.h"void *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;}void pstrcpy(char *buf, int buf_size, const char *str){ int c; char *q = buf; if (buf_size <= 0) return; for(;;) { c = *str++; if (c == 0 || q >= buf + buf_size - 1) break; *q++ = c; } *q = '\0';}/* strcat and truncate. */char *pstrcat(char *buf, int buf_size, const char *s){ int len; len = strlen(buf); if (len < buf_size) pstrcpy(buf + len, buf_size - len, s); return buf;}int strstart(const char *str, const char *val, const char **ptr){ const char *p, *q; p = str; q = val; while (*q != '\0') { if (*p != *q) return 0; p++; q++; } if (ptr) *ptr = p; return 1;}void term_printf(const char *fmt, ...){ va_list ap; va_start(ap, fmt); vprintf(fmt, ap); va_end(ap);}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);}void help(void){ printf("qemu-img version " QEMU_VERSION ", Copyright (c) 2004-2005 Fabrice Bellard\n" "usage: qemu-img command [command options]\n" "QEMU disk image utility\n" "\n" "Command syntax:\n" " create [-e] [-b base_image] [-f fmt] filename [size]\n" " commit [-f fmt] filename\n" " convert [-c] [-e] [-f fmt] filename [-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" ); printf("\nSupported format:"); bdrv_iterate_format(format_print, NULL); printf("\n"); exit(1);}#define NB_SUFFIXES 4static void get_human_readable_size(char *buf, int buf_size, int64_t size){ char suffixes[NB_SUFFIXES] = "KMGT"; int64_t base; int i; if (size <= 999) { snprintf(buf, buf_size, "%lld", (long long) size); } else { base = 1024; for(i = 0; i < NB_SUFFIXES; i++) { if (size < (10 * base)) { snprintf(buf, buf_size, "%0.1f%c", (double)size / base, suffixes[i]); break; } else if (size < (1000 * base) || i == (NB_SUFFIXES - 1)) { snprintf(buf, buf_size, "%lld%c", (long long) ((size + (base >> 1)) / base), suffixes[i]); break; } base = base * 1024; } }}#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);}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, encrypted; const char *fmt = "raw"; const char *filename; const char *base_filename = NULL; int64_t size; const char *p; BlockDriver *drv; encrypted = 0; for(;;) { c = getopt(argc, argv, "b:f:he"); if (c == -1) break; switch(c) { case 'h': help(); break; case 'b': base_filename = optarg; break; case 'f': fmt = optarg; break; case 'e': encrypted = 1; 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -