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"
18 #include "qapi/error.h"
20 #include "qemu/error-report.h"
21 #include "qemu/main-loop.h"
22 #include "qemu/option.h"
23 #include "qemu/config-file.h"
24 #include "qemu/readline.h"
26 #include "qapi/qmp/qstring.h"
27 #include "qapi/qmp/qdict.h"
28 #include "qom/object_interfaces.h"
29 #include "sysemu/block-backend.h"
30 #include "block/block_int.h"
31 #include "trace/control.h"
32 #include "crypto/init.h"
33 #include "qemu-version.h"
35 #define CMD_NOFILE_OK 0x01
37 static char *progname
;
39 static BlockBackend
*qemuio_blk
;
41 /* qemu-io commands passed using -c */
43 static char **cmdline
;
44 static bool imageOpts
;
46 static ReadLineState
*readline_state
;
50 static int get_eof_char(void)
53 return 0x4; /* Ctrl-D */
56 if (tcgetattr(STDIN_FILENO
, &tty
) != 0) {
57 if (errno
== ENOTTY
) {
58 return 0x0; /* just expect read() == 0 */
60 return 0x4; /* Ctrl-D */
64 return tty
.c_cc
[VEOF
];
68 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
70 blk_unref(qemuio_blk
);
75 static const cmdinfo_t close_cmd
= {
79 .oneline
= "close the current open file",
82 static int openfile(char *name
, int flags
, bool writethrough
, bool force_share
,
85 Error
*local_err
= NULL
;
88 error_report("file open already, try 'help close'");
97 if (qdict_haskey(opts
, BDRV_OPT_FORCE_SHARE
)
98 && strcmp(qdict_get_str(opts
, BDRV_OPT_FORCE_SHARE
), "on")) {
99 error_report("-U conflicts with image options");
103 qdict_put_str(opts
, BDRV_OPT_FORCE_SHARE
, "on");
105 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
107 error_reportf_err(local_err
, "can't open%s%s: ",
108 name
? " device " : "", name
?: "");
112 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
117 static void open_help(void)
121 " opens a new file in the requested mode\n"
124 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
126 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
127 " -r, -- open file read-only\n"
128 " -s, -- use snapshot file\n"
129 " -C, -- use copy-on-read\n"
130 " -n, -- disable host cache, short for -t none\n"
131 " -U, -- force shared permissions\n"
132 " -k, -- use kernel AIO implementation (on Linux only)\n"
133 " -t, -- use the given cache mode for the image\n"
134 " -d, -- use the given discard mode for the image\n"
135 " -o, -- options to be given to the block driver"
139 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
141 static const cmdinfo_t open_cmd
= {
147 .flags
= CMD_NOFILE_OK
,
148 .args
= "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
149 .oneline
= "open the file specified by path",
153 static QemuOptsList empty_opts
= {
156 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
158 /* no elements => accept any params */
159 { /* end of list */ }
163 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
165 int flags
= BDRV_O_UNMAP
;
167 bool writethrough
= true;
171 bool force_share
= false;
173 while ((c
= getopt(argc
, argv
, "snCro:kt:d:U")) != -1) {
176 flags
|= BDRV_O_SNAPSHOT
;
179 flags
|= BDRV_O_NOCACHE
;
180 writethrough
= false;
183 flags
|= BDRV_O_COPY_ON_READ
;
189 flags
|= BDRV_O_NATIVE_AIO
;
192 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
193 error_report("Invalid cache option: %s", optarg
);
194 qemu_opts_reset(&empty_opts
);
199 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
200 error_report("Invalid discard option: %s", optarg
);
201 qemu_opts_reset(&empty_opts
);
207 printf("--image-opts and 'open -o' are mutually exclusive\n");
208 qemu_opts_reset(&empty_opts
);
211 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
212 qemu_opts_reset(&empty_opts
);
220 qemu_opts_reset(&empty_opts
);
221 return qemuio_command_usage(&open_cmd
);
226 flags
|= BDRV_O_RDWR
;
229 if (imageOpts
&& (optind
== argc
- 1)) {
230 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
231 qemu_opts_reset(&empty_opts
);
237 qopts
= qemu_opts_find(&empty_opts
, NULL
);
238 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
239 qemu_opts_reset(&empty_opts
);
241 if (optind
== argc
- 1) {
242 openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
243 } else if (optind
== argc
) {
244 openfile(NULL
, flags
, writethrough
, force_share
, opts
);
247 qemuio_command_usage(&open_cmd
);
252 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
257 static const cmdinfo_t quit_cmd
= {
263 .flags
= CMD_FLAG_GLOBAL
,
264 .oneline
= "exit the program",
267 static void usage(const char *name
)
270 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
271 "QEMU Disk exerciser\n"
273 " --object OBJECTDEF define an object such as 'secret' for\n"
274 " passwords and/or encryption keys\n"
275 " --image-opts treat file as option string\n"
276 " -c, --cmd STRING execute command with its arguments\n"
277 " from the given string\n"
278 " -f, --format FMT specifies the block driver to use\n"
279 " -r, --read-only export read-only\n"
280 " -s, --snapshot use snapshot file\n"
281 " -n, --nocache disable host cache, short for -t none\n"
282 " -C, --copy-on-read enable copy-on-read\n"
283 " -m, --misalign misalign allocations for O_DIRECT\n"
284 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
285 " -t, --cache=MODE use the given cache mode for the image\n"
286 " -d, --discard=MODE use the given discard mode for the image\n"
287 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
288 " specify tracing options\n"
289 " see qemu-img(1) man page for full description\n"
290 " -U, --force-share force shared permissions\n"
291 " -h, --help display this help and exit\n"
292 " -V, --version output version information and exit\n"
294 "See '%s -c help' for information on available commands.\n"
296 QEMU_HELP_BOTTOM
"\n",
300 static char *get_prompt(void)
302 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
305 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
311 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
312 const char *fmt
, ...)
320 static void readline_flush_func(void *opaque
)
325 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
327 char **line
= readline_opaque
;
328 *line
= g_strdup(str
);
331 static void completion_match(const char *cmd
, void *opaque
)
333 readline_add_completion(readline_state
, cmd
);
336 static void readline_completion_func(void *opaque
, const char *str
)
338 readline_set_completion_index(readline_state
, strlen(str
));
339 qemuio_complete_command(str
, completion_match
, NULL
);
342 static char *fetchline_readline(void)
346 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
349 if (ttyEOF
!= 0x0 && ch
== ttyEOF
) {
353 readline_handle_byte(readline_state
, ch
);
358 #define MAXREADLINESZ 1024
359 static char *fetchline_fgets(void)
361 char *p
, *line
= g_malloc(MAXREADLINESZ
);
363 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
368 p
= line
+ strlen(line
);
369 if (p
!= line
&& p
[-1] == '\n') {
376 static char *fetchline(void)
378 if (readline_state
) {
379 return fetchline_readline();
381 return fetchline_fgets();
385 static void prep_fetchline(void *opaque
)
387 int *fetchable
= opaque
;
389 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
393 static void command_loop(void)
395 int i
, done
= 0, fetchable
= 0, prompted
= 0;
398 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
399 done
= qemuio_command(qemuio_blk
, cmdline
[i
]);
408 printf("%s", get_prompt());
410 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
414 main_loop_wait(false);
424 done
= qemuio_command(qemuio_blk
, input
);
430 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
433 static void add_user_command(char *optarg
)
435 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
436 cmdline
[ncmdline
-1] = optarg
;
439 static void reenable_tty_echo(void)
441 qemu_set_tty_echo(STDIN_FILENO
, true);
446 OPTION_IMAGE_OPTS
= 257,
449 static QemuOptsList qemu_object_opts
= {
451 .implied_opt_name
= "qom-type",
452 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
459 static QemuOptsList file_opts
= {
461 .implied_opt_name
= "file",
462 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
464 /* no elements => accept any params */
465 { /* end of list */ }
469 int main(int argc
, char **argv
)
472 const char *sopt
= "hVc:d:f:rsnCmkt:T:U";
473 const struct option lopt
[] = {
474 { "help", no_argument
, NULL
, 'h' },
475 { "version", no_argument
, NULL
, 'V' },
476 { "cmd", required_argument
, NULL
, 'c' },
477 { "format", required_argument
, NULL
, 'f' },
478 { "read-only", no_argument
, NULL
, 'r' },
479 { "snapshot", no_argument
, NULL
, 's' },
480 { "nocache", no_argument
, NULL
, 'n' },
481 { "copy-on-read", no_argument
, NULL
, 'C' },
482 { "misalign", no_argument
, NULL
, 'm' },
483 { "native-aio", no_argument
, NULL
, 'k' },
484 { "discard", required_argument
, NULL
, 'd' },
485 { "cache", required_argument
, NULL
, 't' },
486 { "trace", required_argument
, NULL
, 'T' },
487 { "object", required_argument
, NULL
, OPTION_OBJECT
},
488 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
489 { "force-share", no_argument
, 0, 'U'},
494 int flags
= BDRV_O_UNMAP
;
495 bool writethrough
= true;
496 Error
*local_error
= NULL
;
498 const char *format
= NULL
;
499 char *trace_file
= NULL
;
500 bool force_share
= false;
503 signal(SIGPIPE
, SIG_IGN
);
506 module_call_init(MODULE_INIT_TRACE
);
507 progname
= g_path_get_basename(argv
[0]);
508 qemu_init_exec_dir(argv
[0]);
510 qcrypto_init(&error_fatal
);
512 module_call_init(MODULE_INIT_QOM
);
513 qemu_add_opts(&qemu_object_opts
);
514 qemu_add_opts(&qemu_trace_opts
);
517 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
520 flags
|= BDRV_O_SNAPSHOT
;
523 flags
|= BDRV_O_NOCACHE
;
524 writethrough
= false;
527 flags
|= BDRV_O_COPY_ON_READ
;
530 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
531 error_report("Invalid discard option: %s", optarg
);
539 add_user_command(optarg
);
545 qemuio_misalign
= true;
548 flags
|= BDRV_O_NATIVE_AIO
;
551 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
552 error_report("Invalid cache option: %s", optarg
);
558 trace_file
= trace_opt_parse(optarg
);
561 printf("%s version " QEMU_FULL_VERSION
"\n"
562 QEMU_COPYRIGHT
"\n", progname
);
570 case OPTION_OBJECT
: {
572 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
578 case OPTION_IMAGE_OPTS
:
587 if ((argc
- optind
) > 1) {
592 if (format
&& imageOpts
) {
593 error_report("--image-opts and -f are mutually exclusive");
597 if (qemu_init_main_loop(&local_error
)) {
598 error_report_err(local_error
);
602 if (qemu_opts_foreach(&qemu_object_opts
,
603 user_creatable_add_opts_foreach
,
608 if (!trace_init_backends()) {
611 trace_init_file(trace_file
);
612 qemu_set_log(LOG_TRACE
);
614 /* initialize commands */
615 qemuio_add_command(&quit_cmd
);
616 qemuio_add_command(&open_cmd
);
617 qemuio_add_command(&close_cmd
);
619 if (isatty(STDIN_FILENO
)) {
620 ttyEOF
= get_eof_char();
621 readline_state
= readline_init(readline_printf_func
,
624 readline_completion_func
);
625 qemu_set_tty_echo(STDIN_FILENO
, false);
626 atexit(reenable_tty_echo
);
629 /* open the device */
631 flags
|= BDRV_O_RDWR
;
634 if ((argc
- optind
) == 1) {
636 QemuOpts
*qopts
= NULL
;
637 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
641 opts
= qemu_opts_to_qdict(qopts
, NULL
);
642 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
648 qdict_put_str(opts
, "driver", format
);
650 if (openfile(argv
[optind
], flags
, writethrough
,
651 force_share
, opts
)) {
659 * Make sure all outstanding requests complete before the program exits.
663 blk_unref(qemuio_blk
);
664 g_free(readline_state
);