2 * Command line utility to exercise the QEMU I/O path.
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 * This work is licensed under the terms of the GNU GPL, version 2 or later.
8 * See the COPYING file in the top-level directory.
11 #include "qemu/osdep.h"
15 #include "qapi/error.h"
17 #include "qemu/error-report.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "qemu/config-file.h"
21 #include "qemu/readline.h"
23 #include "qapi/qmp/qstring.h"
24 #include "qapi/qmp/qdict.h"
25 #include "qom/object_interfaces.h"
26 #include "sysemu/block-backend.h"
27 #include "block/block_int.h"
28 #include "trace/control.h"
29 #include "crypto/init.h"
30 #include "qemu-version.h"
32 #define CMD_NOFILE_OK 0x01
34 static char *progname
;
36 static BlockBackend
*qemuio_blk
;
38 /* qemu-io commands passed using -c */
40 static char **cmdline
;
41 static bool imageOpts
;
43 static ReadLineState
*readline_state
;
45 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
47 blk_unref(qemuio_blk
);
52 static const cmdinfo_t close_cmd
= {
56 .oneline
= "close the current open file",
59 static int openfile(char *name
, int flags
, bool writethrough
, bool force_share
,
62 Error
*local_err
= NULL
;
65 error_report("file open already, try 'help close'");
74 if (qdict_haskey(opts
, BDRV_OPT_FORCE_SHARE
)
75 && !qdict_get_bool(opts
, BDRV_OPT_FORCE_SHARE
)) {
76 error_report("-U conflicts with image options");
80 qdict_put_bool(opts
, BDRV_OPT_FORCE_SHARE
, true);
82 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
84 error_reportf_err(local_err
, "can't open%s%s: ",
85 name
? " device " : "", name
?: "");
89 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
94 static void open_help(void)
98 " opens a new file in the requested mode\n"
101 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
103 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
104 " -r, -- open file read-only\n"
105 " -s, -- use snapshot file\n"
106 " -C, -- use copy-on-read\n"
107 " -n, -- disable host cache, short for -t none\n"
108 " -U, -- force shared permissions\n"
109 " -k, -- use kernel AIO implementation (on Linux only)\n"
110 " -t, -- use the given cache mode for the image\n"
111 " -d, -- use the given discard mode for the image\n"
112 " -o, -- options to be given to the block driver"
116 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
118 static const cmdinfo_t open_cmd
= {
124 .flags
= CMD_NOFILE_OK
,
125 .args
= "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
126 .oneline
= "open the file specified by path",
130 static QemuOptsList empty_opts
= {
133 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
135 /* no elements => accept any params */
136 { /* end of list */ }
140 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
142 int flags
= BDRV_O_UNMAP
;
144 bool writethrough
= true;
148 bool force_share
= false;
150 while ((c
= getopt(argc
, argv
, "snCro:kt:d:U")) != -1) {
153 flags
|= BDRV_O_SNAPSHOT
;
156 flags
|= BDRV_O_NOCACHE
;
157 writethrough
= false;
160 flags
|= BDRV_O_COPY_ON_READ
;
166 flags
|= BDRV_O_NATIVE_AIO
;
169 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
170 error_report("Invalid cache option: %s", optarg
);
171 qemu_opts_reset(&empty_opts
);
176 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
177 error_report("Invalid discard option: %s", optarg
);
178 qemu_opts_reset(&empty_opts
);
184 printf("--image-opts and 'open -o' are mutually exclusive\n");
185 qemu_opts_reset(&empty_opts
);
188 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
189 qemu_opts_reset(&empty_opts
);
197 qemu_opts_reset(&empty_opts
);
198 return qemuio_command_usage(&open_cmd
);
203 flags
|= BDRV_O_RDWR
;
206 if (imageOpts
&& (optind
== argc
- 1)) {
207 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
208 qemu_opts_reset(&empty_opts
);
214 qopts
= qemu_opts_find(&empty_opts
, NULL
);
215 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
216 qemu_opts_reset(&empty_opts
);
218 if (optind
== argc
- 1) {
219 openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
220 } else if (optind
== argc
) {
221 openfile(NULL
, flags
, writethrough
, force_share
, opts
);
224 qemuio_command_usage(&open_cmd
);
229 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
234 static const cmdinfo_t quit_cmd
= {
240 .flags
= CMD_FLAG_GLOBAL
,
241 .oneline
= "exit the program",
244 static void usage(const char *name
)
247 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
248 "QEMU Disk exerciser\n"
250 " --object OBJECTDEF define an object such as 'secret' for\n"
251 " passwords and/or encryption keys\n"
252 " --image-opts treat file as option string\n"
253 " -c, --cmd STRING execute command with its arguments\n"
254 " from the given string\n"
255 " -f, --format FMT specifies the block driver to use\n"
256 " -r, --read-only export read-only\n"
257 " -s, --snapshot use snapshot file\n"
258 " -n, --nocache disable host cache, short for -t none\n"
259 " -C, --copy-on-read enable copy-on-read\n"
260 " -m, --misalign misalign allocations for O_DIRECT\n"
261 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
262 " -t, --cache=MODE use the given cache mode for the image\n"
263 " -d, --discard=MODE use the given discard mode for the image\n"
264 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
265 " specify tracing options\n"
266 " see qemu-img(1) man page for full description\n"
267 " -U, --force-share force shared permissions\n"
268 " -h, --help display this help and exit\n"
269 " -V, --version output version information and exit\n"
271 "See '%s -c help' for information on available commands.\n"
273 QEMU_HELP_BOTTOM
"\n",
277 static char *get_prompt(void)
279 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
282 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
288 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
289 const char *fmt
, ...)
297 static void readline_flush_func(void *opaque
)
302 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
304 char **line
= readline_opaque
;
305 *line
= g_strdup(str
);
308 static void completion_match(const char *cmd
, void *opaque
)
310 readline_add_completion(readline_state
, cmd
);
313 static void readline_completion_func(void *opaque
, const char *str
)
315 readline_set_completion_index(readline_state
, strlen(str
));
316 qemuio_complete_command(str
, completion_match
, NULL
);
319 static char *fetchline_readline(void)
323 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
329 readline_handle_byte(readline_state
, ch
);
334 #define MAXREADLINESZ 1024
335 static char *fetchline_fgets(void)
337 char *p
, *line
= g_malloc(MAXREADLINESZ
);
339 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
344 p
= line
+ strlen(line
);
345 if (p
!= line
&& p
[-1] == '\n') {
352 static char *fetchline(void)
354 if (readline_state
) {
355 return fetchline_readline();
357 return fetchline_fgets();
361 static void prep_fetchline(void *opaque
)
363 int *fetchable
= opaque
;
365 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
369 static void command_loop(void)
371 int i
, done
= 0, fetchable
= 0, prompted
= 0;
374 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
375 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
384 printf("%s", get_prompt());
386 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
390 main_loop_wait(false);
400 done
= qemuio_command(qemuio_blk
, input
);
406 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
409 static void add_user_command(char *optarg
)
411 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
412 cmdline
[ncmdline
-1] = optarg
;
415 static void reenable_tty_echo(void)
417 qemu_set_tty_echo(STDIN_FILENO
, true);
422 OPTION_IMAGE_OPTS
= 257,
425 static QemuOptsList qemu_object_opts
= {
427 .implied_opt_name
= "qom-type",
428 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
435 static QemuOptsList file_opts
= {
437 .implied_opt_name
= "file",
438 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
440 /* no elements => accept any params */
441 { /* end of list */ }
445 int main(int argc
, char **argv
)
448 const char *sopt
= "hVc:d:f:rsnCmkt:T:U";
449 const struct option lopt
[] = {
450 { "help", no_argument
, NULL
, 'h' },
451 { "version", no_argument
, NULL
, 'V' },
452 { "cmd", required_argument
, NULL
, 'c' },
453 { "format", required_argument
, NULL
, 'f' },
454 { "read-only", no_argument
, NULL
, 'r' },
455 { "snapshot", no_argument
, NULL
, 's' },
456 { "nocache", no_argument
, NULL
, 'n' },
457 { "copy-on-read", no_argument
, NULL
, 'C' },
458 { "misalign", no_argument
, NULL
, 'm' },
459 { "native-aio", no_argument
, NULL
, 'k' },
460 { "discard", required_argument
, NULL
, 'd' },
461 { "cache", required_argument
, NULL
, 't' },
462 { "trace", required_argument
, NULL
, 'T' },
463 { "object", required_argument
, NULL
, OPTION_OBJECT
},
464 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
465 { "force-share", no_argument
, 0, 'U'},
470 int flags
= BDRV_O_UNMAP
;
471 bool writethrough
= true;
472 Error
*local_error
= NULL
;
474 const char *format
= NULL
;
475 char *trace_file
= NULL
;
476 bool force_share
= false;
479 signal(SIGPIPE
, SIG_IGN
);
482 module_call_init(MODULE_INIT_TRACE
);
483 progname
= basename(argv
[0]);
484 qemu_init_exec_dir(argv
[0]);
486 qcrypto_init(&error_fatal
);
488 module_call_init(MODULE_INIT_QOM
);
489 qemu_add_opts(&qemu_object_opts
);
490 qemu_add_opts(&qemu_trace_opts
);
493 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
496 flags
|= BDRV_O_SNAPSHOT
;
499 flags
|= BDRV_O_NOCACHE
;
500 writethrough
= false;
503 flags
|= BDRV_O_COPY_ON_READ
;
506 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
507 error_report("Invalid discard option: %s", optarg
);
515 add_user_command(optarg
);
521 qemuio_misalign
= true;
524 flags
|= BDRV_O_NATIVE_AIO
;
527 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
528 error_report("Invalid cache option: %s", optarg
);
534 trace_file
= trace_opt_parse(optarg
);
537 printf("%s version " QEMU_VERSION QEMU_PKGVERSION
"\n"
538 QEMU_COPYRIGHT
"\n", progname
);
546 case OPTION_OBJECT
: {
548 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
554 case OPTION_IMAGE_OPTS
:
563 if ((argc
- optind
) > 1) {
568 if (format
&& imageOpts
) {
569 error_report("--image-opts and -f are mutually exclusive");
573 if (qemu_init_main_loop(&local_error
)) {
574 error_report_err(local_error
);
578 if (qemu_opts_foreach(&qemu_object_opts
,
579 user_creatable_add_opts_foreach
,
584 if (!trace_init_backends()) {
587 trace_init_file(trace_file
);
588 qemu_set_log(LOG_TRACE
);
590 /* initialize commands */
591 qemuio_add_command(&quit_cmd
);
592 qemuio_add_command(&open_cmd
);
593 qemuio_add_command(&close_cmd
);
595 if (isatty(STDIN_FILENO
)) {
596 readline_state
= readline_init(readline_printf_func
,
599 readline_completion_func
);
600 qemu_set_tty_echo(STDIN_FILENO
, false);
601 atexit(reenable_tty_echo
);
604 /* open the device */
606 flags
|= BDRV_O_RDWR
;
609 if ((argc
- optind
) == 1) {
611 QemuOpts
*qopts
= NULL
;
612 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
616 opts
= qemu_opts_to_qdict(qopts
, NULL
);
617 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
623 qdict_put_str(opts
, "driver", format
);
625 if (openfile(argv
[optind
], flags
, writethrough
,
626 force_share
, opts
)) {
634 * Make sure all outstanding requests complete before the program exits.
638 blk_unref(qemuio_blk
);
639 g_free(readline_state
);