qemu-io: Make 'open' subcommand more like command line

The command line defaults to BDRV_O_UNMAP, but can use
-d to reset it.  Meanwhile, the 'open' subcommand was
defaulting to no discards, with no way to set it.

The command line has both -n and -tMODE to set a variety
of cache modes, but the 'open' subcommand had only -n.

The 'open' subcommand had no way to set BDRV_O_NATIVE_AIO.

Note that the 'reopen' subcommand uses '-c' where the
command line and 'open' use -t.  Making that consistent
would be a separate patch.

Signed-off-by: Eric Blake <eblake@redhat.com>
Message-id: 1462677405-4752-3-git-send-email-eblake@redhat.com
Reviewed-by: Max Reitz <mreitz@redhat.com>
Signed-off-by: Max Reitz <mreitz@redhat.com>
This commit is contained in:
Eric Blake 2016-05-07 21:16:41 -06:00 committed by Max Reitz
parent e4e12bb26d
commit b8d970f1a9

View File

@ -106,7 +106,10 @@ static void open_help(void)
" Opens a file for subsequent use by all of the other qemu-io commands.\n" " Opens a file for subsequent use by all of the other qemu-io commands.\n"
" -r, -- open file read-only\n" " -r, -- open file read-only\n"
" -s, -- use snapshot file\n" " -s, -- use snapshot file\n"
" -n, -- disable host cache\n" " -n, -- disable host cache, short for -t none\n"
" -k, -- use kernel AIO implementation (on Linux only)\n"
" -t, -- use the given cache mode for the image\n"
" -d, -- use the given discard mode for the image\n"
" -o, -- options to be given to the block driver" " -o, -- options to be given to the block driver"
"\n"); "\n");
} }
@ -120,7 +123,7 @@ static const cmdinfo_t open_cmd = {
.argmin = 1, .argmin = 1,
.argmax = -1, .argmax = -1,
.flags = CMD_NOFILE_OK, .flags = CMD_NOFILE_OK,
.args = "[-rsn] [-o options] [path]", .args = "[-rsnk] [-t cache] [-d discard] [-o options] [path]",
.oneline = "open the file specified by path", .oneline = "open the file specified by path",
.help = open_help, .help = open_help,
}; };
@ -137,14 +140,14 @@ static QemuOptsList empty_opts = {
static int open_f(BlockBackend *blk, int argc, char **argv) static int open_f(BlockBackend *blk, int argc, char **argv)
{ {
int flags = 0; int flags = BDRV_O_UNMAP;
int readonly = 0; int readonly = 0;
bool writethrough = true; bool writethrough = true;
int c; int c;
QemuOpts *qopts; QemuOpts *qopts;
QDict *opts; QDict *opts;
while ((c = getopt(argc, argv, "snro:")) != -1) { while ((c = getopt(argc, argv, "snro:kt:d:")) != -1) {
switch (c) { switch (c) {
case 's': case 's':
flags |= BDRV_O_SNAPSHOT; flags |= BDRV_O_SNAPSHOT;
@ -156,9 +159,27 @@ static int open_f(BlockBackend *blk, int argc, char **argv)
case 'r': case 'r':
readonly = 1; readonly = 1;
break; break;
case 'k':
flags |= BDRV_O_NATIVE_AIO;
break;
case 't':
if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
error_report("Invalid cache option: %s", optarg);
qemu_opts_reset(&empty_opts);
return 0;
}
break;
case 'd':
if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
error_report("Invalid discard option: %s", optarg);
qemu_opts_reset(&empty_opts);
return 0;
}
break;
case 'o': case 'o':
if (imageOpts) { if (imageOpts) {
printf("--image-opts and 'open -o' are mutually exclusive\n"); printf("--image-opts and 'open -o' are mutually exclusive\n");
qemu_opts_reset(&empty_opts);
return 0; return 0;
} }
if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) { if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {