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
;
40 static bool quit_qemu_io
;
42 /* qemu-io commands passed using -c */
44 static char **cmdline
;
45 static bool imageOpts
;
47 static ReadLineState
*readline_state
;
51 static int get_eof_char(void)
54 return 0x4; /* Ctrl-D */
57 if (tcgetattr(STDIN_FILENO
, &tty
) != 0) {
58 if (errno
== ENOTTY
) {
59 return 0x0; /* just expect read() == 0 */
61 return 0x4; /* Ctrl-D */
65 return tty
.c_cc
[VEOF
];
69 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
71 blk_unref(qemuio_blk
);
76 static const cmdinfo_t close_cmd
= {
80 .oneline
= "close the current open file",
83 static int openfile(char *name
, int flags
, bool writethrough
, bool force_share
,
86 Error
*local_err
= NULL
;
89 error_report("file open already, try 'help close'");
98 if (qdict_haskey(opts
, BDRV_OPT_FORCE_SHARE
)
99 && strcmp(qdict_get_str(opts
, BDRV_OPT_FORCE_SHARE
), "on")) {
100 error_report("-U conflicts with image options");
104 qdict_put_str(opts
, BDRV_OPT_FORCE_SHARE
, "on");
106 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
108 error_reportf_err(local_err
, "can't open%s%s: ",
109 name
? " device " : "", name
?: "");
113 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
118 static void open_help(void)
122 " opens a new file in the requested mode\n"
125 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
127 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
128 " -r, -- open file read-only\n"
129 " -s, -- use snapshot file\n"
130 " -C, -- use copy-on-read\n"
131 " -n, -- disable host cache, short for -t none\n"
132 " -U, -- force shared permissions\n"
133 " -k, -- use kernel AIO implementation (on Linux only)\n"
134 " -t, -- use the given cache mode for the image\n"
135 " -d, -- use the given discard mode for the image\n"
136 " -o, -- options to be given to the block driver"
140 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
142 static const cmdinfo_t open_cmd
= {
148 .flags
= CMD_NOFILE_OK
,
149 .args
= "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
150 .oneline
= "open the file specified by path",
154 static QemuOptsList empty_opts
= {
157 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
159 /* no elements => accept any params */
160 { /* end of list */ }
164 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
166 int flags
= BDRV_O_UNMAP
;
168 bool writethrough
= true;
173 bool force_share
= false;
175 while ((c
= getopt(argc
, argv
, "snCro:kt:d:U")) != -1) {
178 flags
|= BDRV_O_SNAPSHOT
;
181 flags
|= BDRV_O_NOCACHE
;
182 writethrough
= false;
185 flags
|= BDRV_O_COPY_ON_READ
;
191 flags
|= BDRV_O_NATIVE_AIO
;
194 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
195 error_report("Invalid cache option: %s", optarg
);
196 qemu_opts_reset(&empty_opts
);
201 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
202 error_report("Invalid discard option: %s", optarg
);
203 qemu_opts_reset(&empty_opts
);
209 printf("--image-opts and 'open -o' are mutually exclusive\n");
210 qemu_opts_reset(&empty_opts
);
213 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
214 qemu_opts_reset(&empty_opts
);
222 qemu_opts_reset(&empty_opts
);
223 qemuio_command_usage(&open_cmd
);
229 flags
|= BDRV_O_RDWR
;
232 if (imageOpts
&& (optind
== argc
- 1)) {
233 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
234 qemu_opts_reset(&empty_opts
);
240 qopts
= qemu_opts_find(&empty_opts
, NULL
);
241 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
242 qemu_opts_reset(&empty_opts
);
244 if (optind
== argc
- 1) {
245 ret
= openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
246 } else if (optind
== argc
) {
247 ret
= openfile(NULL
, flags
, writethrough
, force_share
, opts
);
250 qemuio_command_usage(&open_cmd
);
261 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
267 static const cmdinfo_t quit_cmd
= {
273 .flags
= CMD_FLAG_GLOBAL
,
274 .oneline
= "exit the program",
277 static void usage(const char *name
)
280 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
281 "QEMU Disk exerciser\n"
283 " --object OBJECTDEF define an object such as 'secret' for\n"
284 " passwords and/or encryption keys\n"
285 " --image-opts treat file as option string\n"
286 " -c, --cmd STRING execute command with its arguments\n"
287 " from the given string\n"
288 " -f, --format FMT specifies the block driver to use\n"
289 " -r, --read-only export read-only\n"
290 " -s, --snapshot use snapshot file\n"
291 " -n, --nocache disable host cache, short for -t none\n"
292 " -C, --copy-on-read enable copy-on-read\n"
293 " -m, --misalign misalign allocations for O_DIRECT\n"
294 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
295 " -t, --cache=MODE use the given cache mode for the image\n"
296 " -d, --discard=MODE use the given discard mode for the image\n"
297 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
298 " specify tracing options\n"
299 " see qemu-img(1) man page for full description\n"
300 " -U, --force-share force shared permissions\n"
301 " -h, --help display this help and exit\n"
302 " -V, --version output version information and exit\n"
304 "See '%s -c help' for information on available commands.\n"
306 QEMU_HELP_BOTTOM
"\n",
310 static char *get_prompt(void)
312 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
315 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
321 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
322 const char *fmt
, ...)
330 static void readline_flush_func(void *opaque
)
335 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
337 char **line
= readline_opaque
;
338 *line
= g_strdup(str
);
341 static void completion_match(const char *cmd
, void *opaque
)
343 readline_add_completion(readline_state
, cmd
);
346 static void readline_completion_func(void *opaque
, const char *str
)
348 readline_set_completion_index(readline_state
, strlen(str
));
349 qemuio_complete_command(str
, completion_match
, NULL
);
352 static char *fetchline_readline(void)
356 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
359 if (ttyEOF
!= 0x0 && ch
== ttyEOF
) {
363 readline_handle_byte(readline_state
, ch
);
368 #define MAXREADLINESZ 1024
369 static char *fetchline_fgets(void)
371 char *p
, *line
= g_malloc(MAXREADLINESZ
);
373 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
378 p
= line
+ strlen(line
);
379 if (p
!= line
&& p
[-1] == '\n') {
386 static char *fetchline(void)
388 if (readline_state
) {
389 return fetchline_readline();
391 return fetchline_fgets();
395 static void prep_fetchline(void *opaque
)
397 int *fetchable
= opaque
;
399 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
403 static int command_loop(void)
405 int i
, fetchable
= 0, prompted
= 0;
406 int ret
, last_error
= 0;
409 for (i
= 0; !quit_qemu_io
&& i
< ncmdline
; i
++) {
410 ret
= qemuio_command(qemuio_blk
, cmdline
[i
]);
420 while (!quit_qemu_io
) {
422 printf("%s", get_prompt());
424 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
428 main_loop_wait(false);
438 ret
= qemuio_command(qemuio_blk
, input
);
448 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
453 static void add_user_command(char *optarg
)
455 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
456 cmdline
[ncmdline
-1] = optarg
;
459 static void reenable_tty_echo(void)
461 qemu_set_tty_echo(STDIN_FILENO
, true);
466 OPTION_IMAGE_OPTS
= 257,
469 static QemuOptsList qemu_object_opts
= {
471 .implied_opt_name
= "qom-type",
472 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
479 static QemuOptsList file_opts
= {
481 .implied_opt_name
= "file",
482 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
484 /* no elements => accept any params */
485 { /* end of list */ }
489 int main(int argc
, char **argv
)
492 const char *sopt
= "hVc:d:f:rsnCmkt:T:U";
493 const struct option lopt
[] = {
494 { "help", no_argument
, NULL
, 'h' },
495 { "version", no_argument
, NULL
, 'V' },
496 { "cmd", required_argument
, NULL
, 'c' },
497 { "format", required_argument
, NULL
, 'f' },
498 { "read-only", no_argument
, NULL
, 'r' },
499 { "snapshot", no_argument
, NULL
, 's' },
500 { "nocache", no_argument
, NULL
, 'n' },
501 { "copy-on-read", no_argument
, NULL
, 'C' },
502 { "misalign", no_argument
, NULL
, 'm' },
503 { "native-aio", no_argument
, NULL
, 'k' },
504 { "discard", required_argument
, NULL
, 'd' },
505 { "cache", required_argument
, NULL
, 't' },
506 { "trace", required_argument
, NULL
, 'T' },
507 { "object", required_argument
, NULL
, OPTION_OBJECT
},
508 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
509 { "force-share", no_argument
, 0, 'U'},
514 int flags
= BDRV_O_UNMAP
;
516 bool writethrough
= true;
517 Error
*local_error
= NULL
;
519 const char *format
= NULL
;
520 char *trace_file
= NULL
;
521 bool force_share
= false;
524 signal(SIGPIPE
, SIG_IGN
);
527 module_call_init(MODULE_INIT_TRACE
);
528 progname
= g_path_get_basename(argv
[0]);
529 qemu_init_exec_dir(argv
[0]);
531 qcrypto_init(&error_fatal
);
533 module_call_init(MODULE_INIT_QOM
);
534 qemu_add_opts(&qemu_object_opts
);
535 qemu_add_opts(&qemu_trace_opts
);
538 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
541 flags
|= BDRV_O_SNAPSHOT
;
544 flags
|= BDRV_O_NOCACHE
;
545 writethrough
= false;
548 flags
|= BDRV_O_COPY_ON_READ
;
551 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
552 error_report("Invalid discard option: %s", optarg
);
560 add_user_command(optarg
);
566 qemuio_misalign
= true;
569 flags
|= BDRV_O_NATIVE_AIO
;
572 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
573 error_report("Invalid cache option: %s", optarg
);
579 trace_file
= trace_opt_parse(optarg
);
582 printf("%s version " QEMU_FULL_VERSION
"\n"
583 QEMU_COPYRIGHT
"\n", progname
);
591 case OPTION_OBJECT
: {
593 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
599 case OPTION_IMAGE_OPTS
:
608 if ((argc
- optind
) > 1) {
613 if (format
&& imageOpts
) {
614 error_report("--image-opts and -f are mutually exclusive");
618 if (qemu_init_main_loop(&local_error
)) {
619 error_report_err(local_error
);
623 qemu_opts_foreach(&qemu_object_opts
,
624 user_creatable_add_opts_foreach
,
627 if (!trace_init_backends()) {
630 trace_init_file(trace_file
);
631 qemu_set_log(LOG_TRACE
);
633 /* initialize commands */
634 qemuio_add_command(&quit_cmd
);
635 qemuio_add_command(&open_cmd
);
636 qemuio_add_command(&close_cmd
);
638 if (isatty(STDIN_FILENO
)) {
639 ttyEOF
= get_eof_char();
640 readline_state
= readline_init(readline_printf_func
,
643 readline_completion_func
);
644 qemu_set_tty_echo(STDIN_FILENO
, false);
645 atexit(reenable_tty_echo
);
648 /* open the device */
650 flags
|= BDRV_O_RDWR
;
653 if ((argc
- optind
) == 1) {
655 QemuOpts
*qopts
= NULL
;
656 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
660 opts
= qemu_opts_to_qdict(qopts
, NULL
);
661 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
667 qdict_put_str(opts
, "driver", format
);
669 if (openfile(argv
[optind
], flags
, writethrough
,
670 force_share
, opts
)) {
675 ret
= command_loop();
678 * Make sure all outstanding requests complete before the program exits.
682 blk_unref(qemuio_blk
);
683 g_free(readline_state
);