#include <windows.h>
#endif
+typedef struct img_cmd_t {
+ const char *name;
+ int (*handler)(int argc, char **argv);
+} img_cmd_t;
+
/* Default to cache=writeback as data integrity is not important for qemu-tcg. */
#define BRDV_O_FLAGS BDRV_O_CACHE_WB
"QEMU disk image utility\n"
"\n"
"Command syntax:\n"
- " check [-f fmt] filename\n"
- " create [-e] [-6] [-F fmt] [-b base_image] [-f fmt] filename [size]\n"
- " commit [-f fmt] filename\n"
- " convert [-c] [-e] [-6] [-f fmt] [-O output_fmt] [-B output_base_image] filename [filename2 [...]] output_filename\n"
- " info [-f fmt] filename\n"
- " snapshot [-l | -a snapshot | -c snapshot | -d snapshot] filename\n"
+#define DEF(option, callback, arg_string) \
+ " " arg_string "\n"
+#include "qemu-img-cmds.h"
+#undef DEF
+#undef GEN_DOCS
"\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"
- " 'output_base_image' forces the output image to be created as a copy on write\n"
- " image of the specified base image; 'output_base_image' should have the same\n"
- " content as the input's base image, however the path, image format, etc may\n"
- " differ\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\n"
" 'M' (megabyte, 1024 * 1024) and 'G' (gigabyte, 1024 * 1024 * 1024) are\n"
" supported any 'k' or 'K' is ignored\n"
" 'output_filename' is the destination disk image filename\n"
" 'output_fmt' is the destination format\n"
+ " 'options' is a comma separated list of format specific options in a\n"
+ " name=value format. Use -o ? for an overview of the options supported by the\n"
+ " used 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"
" '-h' with or without a command shows this help and lists the supported formats\n"
"\n"
"Parameters to snapshot subcommand:\n"
break;
}
}
- if (optind >= argc)
- help();
- filename = argv[optind++];
/* Find driver and parse its options */
drv = bdrv_find_format(fmt);
if (!drv)
error("Unknown file format '%s'", fmt);
+ if (options && !strcmp(options, "?")) {
+ print_option_help(drv->create_options);
+ return 0;
+ }
+
+ /* Create parameter list with default values */
+ param = parse_option_parameters("", drv->create_options, param);
+ set_option_parameter_int(param, BLOCK_OPT_SIZE, -1);
+
+ /* Parse -o options */
if (options) {
param = parse_option_parameters(options, drv->create_options, param);
if (param == NULL) {
error("Invalid options for file format '%s'.", fmt);
}
- } else {
- param = parse_option_parameters("", drv->create_options, param);
}
+ /* Get the filename */
+ if (optind >= argc)
+ help();
+ filename = argv[optind++];
+
/* Add size to parameters */
if (optind < argc) {
set_option_parameter(param, BLOCK_OPT_SIZE, argv[optind++]);
// The size for the image must always be specified, with one exception:
// If we are using a backing file, we can obtain the size from there
- if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == 0) {
+ if (get_option_parameter(param, BLOCK_OPT_SIZE)->value.n == -1) {
QEMUOptionParameter *backing_file =
get_option_parameter(param, BLOCK_OPT_BACKING_FILE);
return v;
}
-#define IO_BUF_SIZE 65536
+#define IO_BUF_SIZE (2 * 1024 * 1024)
static int img_convert(int argc, char **argv)
{
if (!drv)
error("Unknown file format '%s'", out_fmt);
+ if (options && !strcmp(options, "?")) {
+ print_option_help(drv->create_options);
+ return 0;
+ }
+
if (options) {
param = parse_option_parameters(options, drv->create_options, param);
if (param == NULL) {
n = bs_offset + bs_sectors - sector_num;
if (strcmp(drv->format_name, "host_device")) {
- if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
- n, &n1)) {
- sector_num += n1;
- continue;
+ /* If the output image is being created as a copy on write image,
+ assume that sectors which are unallocated in the input image
+ are present in both the output's and input's base images (no
+ need to copy them). */
+ if (out_baseimg) {
+ if (!bdrv_is_allocated(bs[bs_i], sector_num - bs_offset,
+ n, &n1)) {
+ sector_num += n1;
+ continue;
+ }
+ /* The next 'n1' sectors are allocated in the input image. Copy
+ only those as they may be followed by unallocated sectors. */
+ n = n1;
}
- /* The next 'n1' sectors are allocated in the input image. Copy
- only those as they may be followed by unallocated sectors. */
- n = n1;
} else {
n1 = n;
}
#define SNAPSHOT_APPLY 3
#define SNAPSHOT_DELETE 4
-static void img_snapshot(int argc, char **argv)
+static int img_snapshot(int argc, char **argv)
{
BlockDriverState *bs;
QEMUSnapshotInfo sn;
switch(c) {
case 'h':
help();
- return;
+ return 0;
case 'l':
if (action) {
help();
- return;
+ return 0;
}
action = SNAPSHOT_LIST;
break;
case 'a':
if (action) {
help();
- return;
+ return 0;
}
action = SNAPSHOT_APPLY;
snapshot_name = optarg;
case 'c':
if (action) {
help();
- return;
+ return 0;
}
action = SNAPSHOT_CREATE;
snapshot_name = optarg;
case 'd':
if (action) {
help();
- return;
+ return 0;
}
action = SNAPSHOT_DELETE;
snapshot_name = optarg;
/* Cleanup */
bdrv_delete(bs);
+
+ return 0;
}
+static const img_cmd_t img_cmds[] = {
+#define DEF(option, callback, arg_string) \
+ { option, callback },
+#include "qemu-img-cmds.h"
+#undef DEF
+#undef GEN_DOCS
+ { NULL, NULL, },
+};
+
int main(int argc, char **argv)
{
- const char *cmd;
+ const img_cmd_t *cmd;
+ const char *cmdname;
bdrv_init();
if (argc < 2)
help();
- cmd = argv[1];
+ cmdname = argv[1];
argc--; argv++;
- if (!strcmp(cmd, "create")) {
- img_create(argc, argv);
- } else if (!strcmp(cmd, "check")) {
- img_check(argc, argv);
- } else if (!strcmp(cmd, "commit")) {
- img_commit(argc, argv);
- } else if (!strcmp(cmd, "convert")) {
- img_convert(argc, argv);
- } else if (!strcmp(cmd, "info")) {
- img_info(argc, argv);
- } else if (!strcmp(cmd, "snapshot")) {
- img_snapshot(argc, argv);
- } else {
- help();
+
+ /* find the command */
+ for(cmd = img_cmds; cmd->name != NULL; cmd++) {
+ if (!strcmp(cmdname, cmd->name)) {
+ return cmd->handler(argc, argv);
+ }
}
+
+ /* not found */
+ help();
return 0;
}