Skip to content

Commit b543c5c

Browse files
XanClickevmw
authored andcommitted
qemu-io: Let "open" pass options to block driver
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]>
1 parent 899f1ae commit b543c5c

File tree

1 file changed

+31
-8
lines changed

1 file changed

+31
-8
lines changed

qemu-io.c

+31-8
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "qemu-io.h"
1818
#include "qemu/main-loop.h"
19+
#include "qemu/option.h"
20+
#include "qemu/config-file.h"
1921
#include "block/block_int.h"
2022
#include "trace/control.h"
2123

@@ -44,7 +46,7 @@ static const cmdinfo_t close_cmd = {
4446
.oneline = "close the current open file",
4547
};
4648

47-
static int openfile(char *name, int flags, int growable)
49+
static int openfile(char *name, int flags, int growable, QDict *opts)
4850
{
4951
Error *local_err = NULL;
5052

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

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

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

@@ -102,19 +105,30 @@ static const cmdinfo_t open_cmd = {
102105
.argmin = 1,
103106
.argmax = -1,
104107
.flags = CMD_NOFILE_OK,
105-
.args = "[-Crsn] [path]",
108+
.args = "[-Crsn] [-o options] [path]",
106109
.oneline = "open the file specified by path",
107110
.help = open_help,
108111
};
109112

113+
static QemuOptsList empty_opts = {
114+
.name = "drive",
115+
.head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
116+
.desc = {
117+
/* no elements => accept any params */
118+
{ /* end of list */ }
119+
},
120+
};
121+
110122
static int open_f(BlockDriverState *bs, int argc, char **argv)
111123
{
112124
int flags = 0;
113125
int readonly = 0;
114126
int growable = 0;
115127
int c;
128+
QemuOpts *qopts;
129+
QDict *opts = NULL;
116130

117-
while ((c = getopt(argc, argv, "snrg")) != EOF) {
131+
while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
118132
switch (c) {
119133
case 's':
120134
flags |= BDRV_O_SNAPSHOT;
@@ -128,6 +142,15 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
128142
case 'g':
129143
growable = 1;
130144
break;
145+
case 'o':
146+
qopts = qemu_opts_parse(&empty_opts, optarg, 0);
147+
if (qopts == NULL) {
148+
printf("could not parse option list -- %s\n", optarg);
149+
return 0;
150+
}
151+
opts = qemu_opts_to_qdict(qopts, opts);
152+
qemu_opts_del(qopts);
153+
break;
131154
default:
132155
return qemuio_command_usage(&open_cmd);
133156
}
@@ -141,7 +164,7 @@ static int open_f(BlockDriverState *bs, int argc, char **argv)
141164
return qemuio_command_usage(&open_cmd);
142165
}
143166

144-
return openfile(argv[optind], flags, growable);
167+
return openfile(argv[optind], flags, growable, opts);
145168
}
146169

147170
static int quit_f(BlockDriverState *bs, int argc, char **argv)
@@ -418,7 +441,7 @@ int main(int argc, char **argv)
418441
}
419442

420443
if ((argc - optind) == 1) {
421-
openfile(argv[optind], flags, growable);
444+
openfile(argv[optind], flags, growable, NULL);
422445
}
423446
command_loop();
424447

0 commit comments

Comments
 (0)