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 "qemu-common.h"
19 #include "qapi/error.h"
21 #include "qemu/error-report.h"
22 #include "qemu/main-loop.h"
23 #include "qemu/module.h"
24 #include "qemu/option.h"
25 #include "qemu/config-file.h"
26 #include "qemu/readline.h"
28 #include "qapi/qmp/qstring.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qom/object_interfaces.h"
31 #include "sysemu/block-backend.h"
32 #include "block/block_int.h"
33 #include "trace/control.h"
34 #include "crypto/init.h"
35 #include "qemu-version.h"
37 #define CMD_NOFILE_OK 0x01
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 (Linux only, prefer use of -i)\n"
134 " -i, -- use AIO mode (threads, native or io_uring)\n"
135 " -t, -- use the given cache mode for the image\n"
136 " -d, -- use the given discard mode for the image\n"
137 " -o, -- options to be given to the block driver"
141 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
143 static const cmdinfo_t open_cmd
= {
149 .flags
= CMD_NOFILE_OK
,
150 .args
= "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
151 .oneline
= "open the file specified by path",
155 static QemuOptsList empty_opts
= {
158 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
160 /* no elements => accept any params */
161 { /* end of list */ }
165 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
167 int flags
= BDRV_O_UNMAP
;
169 bool writethrough
= true;
174 bool force_share
= false;
176 while ((c
= getopt(argc
, argv
, "snCro:ki:t:d:U")) != -1) {
179 flags
|= BDRV_O_SNAPSHOT
;
182 flags
|= BDRV_O_NOCACHE
;
183 writethrough
= false;
186 flags
|= BDRV_O_COPY_ON_READ
;
192 flags
|= BDRV_O_NATIVE_AIO
;
195 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
196 error_report("Invalid cache option: %s", optarg
);
197 qemu_opts_reset(&empty_opts
);
202 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
203 error_report("Invalid discard option: %s", optarg
);
204 qemu_opts_reset(&empty_opts
);
209 if (bdrv_parse_aio(optarg
, &flags
) < 0) {
210 error_report("Invalid aio option: %s", optarg
);
211 qemu_opts_reset(&empty_opts
);
217 printf("--image-opts and 'open -o' are mutually exclusive\n");
218 qemu_opts_reset(&empty_opts
);
221 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
222 qemu_opts_reset(&empty_opts
);
230 qemu_opts_reset(&empty_opts
);
231 qemuio_command_usage(&open_cmd
);
237 flags
|= BDRV_O_RDWR
;
240 if (imageOpts
&& (optind
== argc
- 1)) {
241 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
242 qemu_opts_reset(&empty_opts
);
248 qopts
= qemu_opts_find(&empty_opts
, NULL
);
249 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
250 qemu_opts_reset(&empty_opts
);
252 if (optind
== argc
- 1) {
253 ret
= openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
254 } else if (optind
== argc
) {
255 ret
= openfile(NULL
, flags
, writethrough
, force_share
, opts
);
258 qemuio_command_usage(&open_cmd
);
269 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
275 #if defined(CONFIG_FVD)
276 # include "qemu-io-sim.c"
279 static const cmdinfo_t quit_cmd
= {
285 .flags
= CMD_FLAG_GLOBAL
,
286 .oneline
= "exit the program",
289 static void usage(const char *name
)
292 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
293 "QEMU Disk exerciser\n"
295 " --object OBJECTDEF define an object such as 'secret' for\n"
296 " passwords and/or encryption keys\n"
297 " --image-opts treat file as option string\n"
298 " -c, --cmd STRING execute command with its arguments\n"
299 " from the given string\n"
300 " -f, --format FMT specifies the block driver to use\n"
301 " -r, --read-only export read-only\n"
302 " -s, --snapshot use snapshot file\n"
303 " -n, --nocache disable host cache, short for -t none\n"
304 " -C, --copy-on-read enable copy-on-read\n"
305 " -m, --misalign misalign allocations for O_DIRECT\n"
306 " -k, --native-aio use kernel AIO implementation\n"
307 " (Linux only, prefer use of -i)\n"
308 " -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
309 " -t, --cache=MODE use the given cache mode for the image\n"
310 " -d, --discard=MODE use the given discard mode for the image\n"
311 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
312 " specify tracing options\n"
313 " see qemu-img(1) man page for full description\n"
314 " -U, --force-share force shared permissions\n"
315 " -h, --help display this help and exit\n"
316 " -V, --version output version information and exit\n"
318 "See '%s -c help' for information on available commands.\n"
320 QEMU_HELP_BOTTOM
"\n",
324 static char *get_prompt(void)
326 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
329 snprintf(prompt
, sizeof(prompt
), "%s> ", error_get_progname());
335 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque
,
336 const char *fmt
, ...)
344 static void readline_flush_func(void *opaque
)
349 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
351 char **line
= readline_opaque
;
352 *line
= g_strdup(str
);
355 static void completion_match(const char *cmd
, void *opaque
)
357 readline_add_completion(readline_state
, cmd
);
360 static void readline_completion_func(void *opaque
, const char *str
)
362 readline_set_completion_index(readline_state
, strlen(str
));
363 qemuio_complete_command(str
, completion_match
, NULL
);
366 static char *fetchline_readline(void)
370 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
373 if (ttyEOF
!= 0x0 && ch
== ttyEOF
) {
377 readline_handle_byte(readline_state
, ch
);
382 #define MAXREADLINESZ 1024
383 static char *fetchline_fgets(void)
385 char *p
, *line
= g_malloc(MAXREADLINESZ
);
387 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
392 p
= line
+ strlen(line
);
393 if (p
!= line
&& p
[-1] == '\n') {
400 static char *fetchline(void)
402 if (readline_state
) {
403 return fetchline_readline();
405 return fetchline_fgets();
409 static void prep_fetchline(void *opaque
)
411 int *fetchable
= opaque
;
413 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
417 static int command_loop(void)
419 int i
, fetchable
= 0, prompted
= 0;
420 int ret
, last_error
= 0;
423 for (i
= 0; !quit_qemu_io
&& i
< ncmdline
; i
++) {
424 ret
= qemuio_command(qemuio_blk
, cmdline
[i
]);
434 while (!quit_qemu_io
) {
436 printf("%s", get_prompt());
438 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
442 main_loop_wait(false);
452 ret
= qemuio_command(qemuio_blk
, input
);
462 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
467 static void add_user_command(char *optarg
)
469 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
470 cmdline
[ncmdline
-1] = optarg
;
473 static void reenable_tty_echo(void)
475 qemu_set_tty_echo(STDIN_FILENO
, true);
480 OPTION_IMAGE_OPTS
= 257,
483 static QemuOptsList qemu_object_opts
= {
485 .implied_opt_name
= "qom-type",
486 .head
= QTAILQ_HEAD_INITIALIZER(qemu_object_opts
.head
),
492 static bool qemu_io_object_print_help(const char *type
, QemuOpts
*opts
)
494 if (user_creatable_print_help(type
, opts
)) {
500 static QemuOptsList file_opts
= {
502 .implied_opt_name
= "file",
503 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
505 /* no elements => accept any params */
506 { /* end of list */ }
510 int main(int argc
, char **argv
)
513 const char *sopt
= "hVc:d:f:rsnCmki:t:T:U";
514 const struct option lopt
[] = {
515 { "help", no_argument
, NULL
, 'h' },
516 { "version", no_argument
, NULL
, 'V' },
517 { "cmd", required_argument
, NULL
, 'c' },
518 { "format", required_argument
, NULL
, 'f' },
519 { "read-only", no_argument
, NULL
, 'r' },
520 { "snapshot", no_argument
, NULL
, 's' },
521 { "nocache", no_argument
, NULL
, 'n' },
522 { "copy-on-read", no_argument
, NULL
, 'C' },
523 { "misalign", no_argument
, NULL
, 'm' },
524 { "native-aio", no_argument
, NULL
, 'k' },
525 { "aio", required_argument
, NULL
, 'i' },
526 { "discard", required_argument
, NULL
, 'd' },
527 { "cache", required_argument
, NULL
, 't' },
528 { "trace", required_argument
, NULL
, 'T' },
529 { "object", required_argument
, NULL
, OPTION_OBJECT
},
530 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
531 { "force-share", no_argument
, 0, 'U'},
536 int flags
= BDRV_O_UNMAP
;
538 bool writethrough
= true;
539 Error
*local_error
= NULL
;
541 const char *format
= NULL
;
542 char *trace_file
= NULL
;
543 bool force_share
= false;
546 signal(SIGPIPE
, SIG_IGN
);
550 module_call_init(MODULE_INIT_TRACE
);
551 qemu_init_exec_dir(argv
[0]);
553 qcrypto_init(&error_fatal
);
555 module_call_init(MODULE_INIT_QOM
);
556 qemu_add_opts(&qemu_object_opts
);
557 qemu_add_opts(&qemu_trace_opts
);
560 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
563 flags
|= BDRV_O_SNAPSHOT
;
566 flags
|= BDRV_O_NOCACHE
;
567 writethrough
= false;
570 flags
|= BDRV_O_COPY_ON_READ
;
573 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
574 error_report("Invalid discard option: %s", optarg
);
582 add_user_command(optarg
);
588 qemuio_misalign
= true;
591 flags
|= BDRV_O_NATIVE_AIO
;
594 if (bdrv_parse_aio(optarg
, &flags
) < 0) {
595 error_report("Invalid aio option: %s", optarg
);
600 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
601 error_report("Invalid cache option: %s", optarg
);
607 trace_file
= trace_opt_parse(optarg
);
610 printf("%s version " QEMU_FULL_VERSION
"\n"
611 QEMU_COPYRIGHT
"\n", error_get_progname());
614 usage(error_get_progname());
619 case OPTION_OBJECT
: {
621 qopts
= qemu_opts_parse_noisily(&qemu_object_opts
,
627 case OPTION_IMAGE_OPTS
:
631 usage(error_get_progname());
636 if ((argc
- optind
) > 1) {
637 usage(error_get_progname());
641 if (format
&& imageOpts
) {
642 error_report("--image-opts and -f are mutually exclusive");
646 if (qemu_init_main_loop(&local_error
)) {
647 error_report_err(local_error
);
651 qemu_opts_foreach(&qemu_object_opts
,
652 user_creatable_add_opts_foreach
,
653 qemu_io_object_print_help
, &error_fatal
);
655 if (!trace_init_backends()) {
658 trace_init_file(trace_file
);
659 qemu_set_log(LOG_TRACE
);
661 /* initialize commands */
662 qemuio_add_command(&quit_cmd
);
663 qemuio_add_command(&open_cmd
);
664 qemuio_add_command(&close_cmd
);
666 if (isatty(STDIN_FILENO
)) {
667 ttyEOF
= get_eof_char();
668 readline_state
= readline_init(readline_printf_func
,
671 readline_completion_func
);
672 qemu_set_tty_echo(STDIN_FILENO
, false);
673 atexit(reenable_tty_echo
);
676 /* open the device */
678 flags
|= BDRV_O_RDWR
;
681 if ((argc
- optind
) == 1) {
683 QemuOpts
*qopts
= NULL
;
684 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
688 opts
= qemu_opts_to_qdict(qopts
, NULL
);
689 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
695 qdict_put_str(opts
, "driver", format
);
697 if (openfile(argv
[optind
], flags
, writethrough
,
698 force_share
, opts
)) {
703 ret
= command_loop();
706 * Make sure all outstanding requests complete before the program exits.
710 blk_unref(qemuio_blk
);
711 g_free(readline_state
);