Skip to content

Commit

Permalink
qemu-io: Let "open" pass options to block driver
Browse files Browse the repository at this point in the history
Add an option to the open command to specify runtime options for the
block driver used.

Signed-off-by: Max Reitz <[email protected]>
Signed-off-by: Kevin Wolf <[email protected]>
  • Loading branch information
XanClic authored and kevmw committed Oct 11, 2013
1 parent 899f1ae commit b543c5c
Showing 1 changed file with 31 additions and 8 deletions.
39 changes: 31 additions & 8 deletions qemu-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "qemu-io.h"
#include "qemu/main-loop.h"
#include "qemu/option.h"
#include "qemu/config-file.h"
#include "block/block_int.h"
#include "trace/control.h"

Expand Down Expand Up @@ -44,7 +46,7 @@ static const cmdinfo_t close_cmd = {
.oneline = "close the current open file",
};

static int openfile(char *name, int flags, int growable)
static int openfile(char *name, int flags, int growable, QDict *opts)
{
Error *local_err = NULL;

Expand All @@ -54,7 +56,7 @@ static int openfile(char *name, int flags, int growable)
}

if (growable) {
if (bdrv_file_open(&qemuio_bs, name, NULL, flags, &local_err)) {
if (bdrv_file_open(&qemuio_bs, name, opts, flags, &local_err)) {
fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
error_get_pretty(local_err));
error_free(local_err);
Expand All @@ -63,7 +65,7 @@ static int openfile(char *name, int flags, int growable)
} else {
qemuio_bs = bdrv_new("hda");

if (bdrv_open(qemuio_bs, name, NULL, flags, NULL, &local_err) < 0) {
if (bdrv_open(qemuio_bs, name, opts, flags, NULL, &local_err) < 0) {
fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
error_get_pretty(local_err));
error_free(local_err);
Expand All @@ -89,7 +91,8 @@ static void open_help(void)
" -r, -- open file read-only\n"
" -s, -- use snapshot file\n"
" -n, -- disable host cache\n"
" -g, -- allow file to grow (only applies to protocols)"
" -g, -- allow file to grow (only applies to protocols)\n"
" -o, -- options to be given to the block driver"
"\n");
}

Expand All @@ -102,19 +105,30 @@ static const cmdinfo_t open_cmd = {
.argmin = 1,
.argmax = -1,
.flags = CMD_NOFILE_OK,
.args = "[-Crsn] [path]",
.args = "[-Crsn] [-o options] [path]",
.oneline = "open the file specified by path",
.help = open_help,
};

static QemuOptsList empty_opts = {
.name = "drive",
.head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
.desc = {
/* no elements => accept any params */
{ /* end of list */ }
},
};

static int open_f(BlockDriverState *bs, int argc, char **argv)
{
int flags = 0;
int readonly = 0;
int growable = 0;
int c;
QemuOpts *qopts;
QDict *opts = NULL;

while ((c = getopt(argc, argv, "snrg")) != EOF) {
while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
switch (c) {
case 's':
flags |= BDRV_O_SNAPSHOT;
Expand All @@ -128,6 +142,15 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
case 'g':
growable = 1;
break;
case 'o':
qopts = qemu_opts_parse(&empty_opts, optarg, 0);
if (qopts == NULL) {
printf("could not parse option list -- %s\n", optarg);
return 0;
}
opts = qemu_opts_to_qdict(qopts, opts);
qemu_opts_del(qopts);
break;
default:
return qemuio_command_usage(&open_cmd);
}
Expand All @@ -141,7 +164,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
return qemuio_command_usage(&open_cmd);
}

return openfile(argv[optind], flags, growable);
return openfile(argv[optind], flags, growable, opts);
}

static int quit_f(BlockDriverState *bs, int argc, char **argv)
Expand Down Expand Up @@ -418,7 +441,7 @@ int main(int argc, char **argv)
}

if ((argc - optind) == 1) {
openfile(argv[optind], flags, growable);
openfile(argv[optind], flags, growable, NULL);
}
command_loop();

Expand Down

0 comments on commit b543c5c

Please sign in to comment.