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/help-texts.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 "qemu/sockets.h"
29 #include "qapi/qmp/qstring.h"
30 #include "qapi/qmp/qdict.h"
31 #include "qom/object_interfaces.h"
32 #include "sysemu/block-backend.h"
33 #include "block/block_int.h"
34 #include "trace/control.h"
35 #include "crypto/init.h"
36 #include "qemu-version.h"
38 #define CMD_NOFILE_OK 0x01
40 static BlockBackend
*qemuio_blk
;
41 static bool quit_qemu_io
;
43 /* qemu-io commands passed using -c */
45 static char **cmdline
;
46 static bool imageOpts
;
48 static ReadLineState
*readline_state
;
52 static int get_eof_char(void)
55 return 0x4; /* Ctrl-D */
58 if (tcgetattr(STDIN_FILENO
, &tty
) != 0) {
59 if (errno
== ENOTTY
) {
60 return 0x0; /* just expect read() == 0 */
62 return 0x4; /* Ctrl-D */
66 return tty
.c_cc
[VEOF
];
70 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
72 blk_unref(qemuio_blk
);
77 static const cmdinfo_t close_cmd
= {
81 .oneline
= "close the current open file",
84 static int openfile(char *name
, int flags
, bool writethrough
, bool force_share
,
87 Error
*local_err
= NULL
;
90 error_report("file open already, try 'help close'");
99 if (qdict_haskey(opts
, BDRV_OPT_FORCE_SHARE
)
100 && strcmp(qdict_get_str(opts
, BDRV_OPT_FORCE_SHARE
), "on")) {
101 error_report("-U conflicts with image options");
105 qdict_put_str(opts
, BDRV_OPT_FORCE_SHARE
, "on");
107 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
109 error_reportf_err(local_err
, "can't open%s%s: ",
110 name
? " device " : "", name
?: "");
114 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
119 static void open_help(void)
123 " opens a new file in the requested mode\n"
126 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
128 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
129 " -r, -- open file read-only\n"
130 " -s, -- use snapshot file\n"
131 " -C, -- use copy-on-read\n"
132 " -n, -- disable host cache, short for -t none\n"
133 " -U, -- force shared permissions\n"
134 " -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
135 " -i, -- use AIO mode (threads, native or io_uring)\n"
136 " -t, -- use the given cache mode for the image\n"
137 " -d, -- use the given discard mode for the image\n"
138 " -o, -- options to be given to the block driver"
142 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
144 static const cmdinfo_t open_cmd
= {
150 .flags
= CMD_NOFILE_OK
,
151 .args
= "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
152 .oneline
= "open the file specified by path",
156 static QemuOptsList empty_opts
= {
159 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
161 /* no elements => accept any params */
162 { /* end of list */ }
166 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
168 int flags
= BDRV_O_UNMAP
;
170 bool writethrough
= true;
175 bool force_share
= false;
177 while ((c
= getopt(argc
, argv
, "snCro:ki:t:d:U")) != -1) {
180 flags
|= BDRV_O_SNAPSHOT
;
183 flags
|= BDRV_O_NOCACHE
;
184 writethrough
= false;
187 flags
|= BDRV_O_COPY_ON_READ
;
193 flags
|= BDRV_O_NATIVE_AIO
;
196 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
197 error_report("Invalid cache option: %s", optarg
);
198 qemu_opts_reset(&empty_opts
);
203 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
204 error_report("Invalid discard option: %s", optarg
);
205 qemu_opts_reset(&empty_opts
);
210 if (bdrv_parse_aio(optarg
, &flags
) < 0) {
211 error_report("Invalid aio option: %s", optarg
);
212 qemu_opts_reset(&empty_opts
);
218 printf("--image-opts and 'open -o' are mutually exclusive\n");
219 qemu_opts_reset(&empty_opts
);
222 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
223 qemu_opts_reset(&empty_opts
);
231 qemu_opts_reset(&empty_opts
);
232 qemuio_command_usage(&open_cmd
);
238 flags
|= BDRV_O_RDWR
;
241 if (imageOpts
&& (optind
== argc
- 1)) {
242 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
243 qemu_opts_reset(&empty_opts
);
249 qopts
= qemu_opts_find(&empty_opts
, NULL
);
250 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
251 qemu_opts_reset(&empty_opts
);
253 if (optind
== argc
- 1) {
254 ret
= openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
255 } else if (optind
== argc
) {
256 ret
= openfile(NULL
, flags
, writethrough
, force_share
, opts
);
259 qemuio_command_usage(&open_cmd
);
270 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
276 static const cmdinfo_t quit_cmd
= {
282 .flags
= CMD_FLAG_GLOBAL
,
283 .oneline
= "exit the program",
286 static void usage(const char *name
)
289 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
290 "QEMU Disk exerciser\n"
292 " --object OBJECTDEF define an object such as 'secret' for\n"
293 " passwords and/or encryption keys\n"
294 " --image-opts treat file as option string\n"
295 " -c, --cmd STRING execute command with its arguments\n"
296 " from the given string\n"
297 " -f, --format FMT specifies the block driver to use\n"
298 " -r, --read-only export read-only\n"
299 " -s, --snapshot use snapshot file\n"
300 " -n, --nocache disable host cache, short for -t none\n"
301 " -C, --copy-on-read enable copy-on-read\n"
302 " -m, --misalign misalign allocations for O_DIRECT\n"
303 " -k, --native-aio use kernel AIO implementation\n"
304 " (Linux only, prefer use of -i)\n"
305 " -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
306 " -t, --cache=MODE use the given cache mode for the image\n"
307 " -d, --discard=MODE use the given discard mode for the image\n"
308 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
309 " specify tracing options\n"
310 " see qemu-img(1) man page for full description\n"
311 " -U, --force-share force shared permissions\n"
312 " -h, --help display this help and exit\n"
313 " -V, --version output version information and exit\n"
315 "See '%s -c help' for information on available commands.\n"
317 QEMU_HELP_BOTTOM
"\n",
321 static char *get_prompt(void)
323 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
326 snprintf(prompt
, sizeof(prompt
), "%s> ", g_get_prgname());
332 static void G_GNUC_PRINTF(2, 3) readline_printf_func(void *opaque
,
333 const char *fmt
, ...)
341 static void readline_flush_func(void *opaque
)
346 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
348 char **line
= readline_opaque
;
349 *line
= g_strdup(str
);
352 static void completion_match(const char *cmd
, void *opaque
)
354 readline_add_completion(readline_state
, cmd
);
357 static void readline_completion_func(void *opaque
, const char *str
)
359 readline_set_completion_index(readline_state
, strlen(str
));
360 qemuio_complete_command(str
, completion_match
, NULL
);
363 static char *fetchline_readline(void)
367 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
370 if (ttyEOF
!= 0x0 && ch
== ttyEOF
) {
374 readline_handle_byte(readline_state
, ch
);
379 #define MAXREADLINESZ 1024
380 static char *fetchline_fgets(void)
382 char *p
, *line
= g_malloc(MAXREADLINESZ
);
384 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
389 p
= line
+ strlen(line
);
390 if (p
!= line
&& p
[-1] == '\n') {
397 static char *fetchline(void)
399 if (readline_state
) {
400 return fetchline_readline();
402 return fetchline_fgets();
406 static void prep_fetchline(void *opaque
)
408 int *fetchable
= opaque
;
410 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
414 static int do_qemuio_command(const char *cmd
)
418 qemuio_blk
? blk_get_aio_context(qemuio_blk
) : qemu_get_aio_context();
420 aio_context_acquire(ctx
);
421 ret
= qemuio_command(qemuio_blk
, cmd
);
422 aio_context_release(ctx
);
427 static int command_loop(void)
429 int i
, fetchable
= 0, prompted
= 0;
430 int ret
, last_error
= 0;
433 for (i
= 0; !quit_qemu_io
&& i
< ncmdline
; i
++) {
434 ret
= do_qemuio_command(cmdline
[i
]);
444 while (!quit_qemu_io
) {
446 printf("%s", get_prompt());
448 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
452 main_loop_wait(false);
462 ret
= do_qemuio_command(input
);
472 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
477 static void add_user_command(char *optarg
)
479 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
480 cmdline
[ncmdline
-1] = optarg
;
483 static void reenable_tty_echo(void)
485 qemu_set_tty_echo(STDIN_FILENO
, true);
490 OPTION_IMAGE_OPTS
= 257,
493 static QemuOptsList file_opts
= {
495 .implied_opt_name
= "file",
496 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
498 /* no elements => accept any params */
499 { /* end of list */ }
503 int main(int argc
, char **argv
)
506 const char *sopt
= "hVc:d:f:rsnCmki:t:T:U";
507 const struct option lopt
[] = {
508 { "help", no_argument
, NULL
, 'h' },
509 { "version", no_argument
, NULL
, 'V' },
510 { "cmd", required_argument
, NULL
, 'c' },
511 { "format", required_argument
, NULL
, 'f' },
512 { "read-only", no_argument
, NULL
, 'r' },
513 { "snapshot", no_argument
, NULL
, 's' },
514 { "nocache", no_argument
, NULL
, 'n' },
515 { "copy-on-read", no_argument
, NULL
, 'C' },
516 { "misalign", no_argument
, NULL
, 'm' },
517 { "native-aio", no_argument
, NULL
, 'k' },
518 { "aio", required_argument
, NULL
, 'i' },
519 { "discard", required_argument
, NULL
, 'd' },
520 { "cache", required_argument
, NULL
, 't' },
521 { "trace", required_argument
, NULL
, 'T' },
522 { "object", required_argument
, NULL
, OPTION_OBJECT
},
523 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
524 { "force-share", no_argument
, 0, 'U'},
529 int flags
= BDRV_O_UNMAP
;
531 bool writethrough
= true;
533 const char *format
= NULL
;
534 bool force_share
= false;
537 signal(SIGPIPE
, SIG_IGN
);
542 module_call_init(MODULE_INIT_TRACE
);
543 qemu_init_exec_dir(argv
[0]);
545 qcrypto_init(&error_fatal
);
547 module_call_init(MODULE_INIT_QOM
);
548 qemu_add_opts(&qemu_trace_opts
);
551 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
554 flags
|= BDRV_O_SNAPSHOT
;
557 flags
|= BDRV_O_NOCACHE
;
558 writethrough
= false;
561 flags
|= BDRV_O_COPY_ON_READ
;
564 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
565 error_report("Invalid discard option: %s", optarg
);
573 add_user_command(optarg
);
579 qemuio_misalign
= true;
582 flags
|= BDRV_O_NATIVE_AIO
;
585 if (bdrv_parse_aio(optarg
, &flags
) < 0) {
586 error_report("Invalid aio option: %s", optarg
);
591 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
592 error_report("Invalid cache option: %s", optarg
);
597 trace_opt_parse(optarg
);
600 printf("%s version " QEMU_FULL_VERSION
"\n"
601 QEMU_COPYRIGHT
"\n", g_get_prgname());
604 usage(g_get_prgname());
610 user_creatable_process_cmdline(optarg
);
612 case OPTION_IMAGE_OPTS
:
616 usage(g_get_prgname());
621 if ((argc
- optind
) > 1) {
622 usage(g_get_prgname());
626 if (format
&& imageOpts
) {
627 error_report("--image-opts and -f are mutually exclusive");
631 qemu_init_main_loop(&error_fatal
);
633 if (!trace_init_backends()) {
637 qemu_set_log(LOG_TRACE
, &error_fatal
);
639 /* initialize commands */
640 qemuio_add_command(&quit_cmd
);
641 qemuio_add_command(&open_cmd
);
642 qemuio_add_command(&close_cmd
);
644 if (isatty(STDIN_FILENO
)) {
645 ttyEOF
= get_eof_char();
646 readline_state
= readline_init(readline_printf_func
,
649 readline_completion_func
);
650 qemu_set_tty_echo(STDIN_FILENO
, false);
651 atexit(reenable_tty_echo
);
654 /* open the device */
656 flags
|= BDRV_O_RDWR
;
659 if ((argc
- optind
) == 1) {
661 QemuOpts
*qopts
= NULL
;
662 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
666 opts
= qemu_opts_to_qdict(qopts
, NULL
);
667 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
673 qdict_put_str(opts
, "driver", format
);
675 if (openfile(argv
[optind
], flags
, writethrough
,
676 force_share
, opts
)) {
681 ret
= command_loop();
684 * Make sure all outstanding requests complete before the program exits.
688 blk_unref(qemuio_blk
);
689 g_free(readline_state
);