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 "qemu/cutils.h"
20 #include "qapi/error.h"
22 #include "qemu/error-report.h"
23 #include "qemu/main-loop.h"
24 #include "qemu/module.h"
25 #include "qemu/option.h"
26 #include "qemu/config-file.h"
27 #include "qemu/readline.h"
29 #include "qemu/sockets.h"
30 #include "qapi/qmp/qstring.h"
31 #include "qapi/qmp/qdict.h"
32 #include "qom/object_interfaces.h"
33 #include "sysemu/block-backend.h"
34 #include "block/block_int.h"
35 #include "trace/control.h"
36 #include "crypto/init.h"
37 #include "qemu-version.h"
39 #define CMD_NOFILE_OK 0x01
41 static BlockBackend
*qemuio_blk
;
42 static bool quit_qemu_io
;
44 /* qemu-io commands passed using -c */
46 static char **cmdline
;
47 static bool imageOpts
;
49 static ReadLineState
*readline_state
;
53 static int get_eof_char(void)
56 return 0x4; /* Ctrl-D */
59 if (tcgetattr(STDIN_FILENO
, &tty
) != 0) {
60 if (errno
== ENOTTY
) {
61 return 0x0; /* just expect read() == 0 */
63 return 0x4; /* Ctrl-D */
67 return tty
.c_cc
[VEOF
];
71 static int close_f(BlockBackend
*blk
, int argc
, char **argv
)
73 blk_unref(qemuio_blk
);
78 static const cmdinfo_t close_cmd
= {
82 .oneline
= "close the current open file",
85 static int openfile(char *name
, int flags
, bool writethrough
, bool force_share
,
88 Error
*local_err
= NULL
;
91 error_report("file open already, try 'help close'");
100 if (qdict_haskey(opts
, BDRV_OPT_FORCE_SHARE
)
101 && strcmp(qdict_get_str(opts
, BDRV_OPT_FORCE_SHARE
), "on")) {
102 error_report("-U conflicts with image options");
106 qdict_put_str(opts
, BDRV_OPT_FORCE_SHARE
, "on");
108 qemuio_blk
= blk_new_open(name
, NULL
, opts
, flags
, &local_err
);
110 error_reportf_err(local_err
, "can't open%s%s: ",
111 name
? " device " : "", name
?: "");
115 blk_set_enable_write_cache(qemuio_blk
, !writethrough
);
120 static void open_help(void)
124 " opens a new file in the requested mode\n"
127 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
129 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
130 " -r, -- open file read-only\n"
131 " -s, -- use snapshot file\n"
132 " -C, -- use copy-on-read\n"
133 " -n, -- disable host cache, short for -t none\n"
134 " -U, -- force shared permissions\n"
135 " -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
136 " -i, -- use AIO mode (threads, native or io_uring)\n"
137 " -t, -- use the given cache mode for the image\n"
138 " -d, -- use the given discard mode for the image\n"
139 " -o, -- options to be given to the block driver"
143 static int open_f(BlockBackend
*blk
, int argc
, char **argv
);
145 static const cmdinfo_t open_cmd
= {
151 .flags
= CMD_NOFILE_OK
,
152 .args
= "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
153 .oneline
= "open the file specified by path",
157 static QemuOptsList empty_opts
= {
160 .head
= QTAILQ_HEAD_INITIALIZER(empty_opts
.head
),
162 /* no elements => accept any params */
163 { /* end of list */ }
167 static int open_f(BlockBackend
*blk
, int argc
, char **argv
)
169 int flags
= BDRV_O_UNMAP
;
171 bool writethrough
= true;
176 bool force_share
= false;
178 while ((c
= getopt(argc
, argv
, "snCro:ki:t:d:U")) != -1) {
181 flags
|= BDRV_O_SNAPSHOT
;
184 flags
|= BDRV_O_NOCACHE
;
185 writethrough
= false;
188 flags
|= BDRV_O_COPY_ON_READ
;
194 flags
|= BDRV_O_NATIVE_AIO
;
197 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
198 error_report("Invalid cache option: %s", optarg
);
199 qemu_opts_reset(&empty_opts
);
204 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
205 error_report("Invalid discard option: %s", optarg
);
206 qemu_opts_reset(&empty_opts
);
211 if (bdrv_parse_aio(optarg
, &flags
) < 0) {
212 error_report("Invalid aio option: %s", optarg
);
213 qemu_opts_reset(&empty_opts
);
219 printf("--image-opts and 'open -o' are mutually exclusive\n");
220 qemu_opts_reset(&empty_opts
);
223 if (!qemu_opts_parse_noisily(&empty_opts
, optarg
, false)) {
224 qemu_opts_reset(&empty_opts
);
232 qemu_opts_reset(&empty_opts
);
233 qemuio_command_usage(&open_cmd
);
239 flags
|= BDRV_O_RDWR
;
242 if (imageOpts
&& (optind
== argc
- 1)) {
243 if (!qemu_opts_parse_noisily(&empty_opts
, argv
[optind
], false)) {
244 qemu_opts_reset(&empty_opts
);
250 qopts
= qemu_opts_find(&empty_opts
, NULL
);
251 opts
= qopts
? qemu_opts_to_qdict(qopts
, NULL
) : NULL
;
252 qemu_opts_reset(&empty_opts
);
254 if (optind
== argc
- 1) {
255 ret
= openfile(argv
[optind
], flags
, writethrough
, force_share
, opts
);
256 } else if (optind
== argc
) {
257 ret
= openfile(NULL
, flags
, writethrough
, force_share
, opts
);
260 qemuio_command_usage(&open_cmd
);
271 static int quit_f(BlockBackend
*blk
, int argc
, char **argv
)
277 #if defined(CONFIG_FVD)
278 # include "qemu-io-sim.c"
281 static const cmdinfo_t quit_cmd
= {
287 .flags
= CMD_FLAG_GLOBAL
,
288 .oneline
= "exit the program",
291 static void usage(const char *name
)
294 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
295 "QEMU Disk exerciser\n"
297 " --object OBJECTDEF define an object such as 'secret' for\n"
298 " passwords and/or encryption keys\n"
299 " --image-opts treat file as option string\n"
300 " -c, --cmd STRING execute command with its arguments\n"
301 " from the given string\n"
302 " -f, --format FMT specifies the block driver to use\n"
303 " -r, --read-only export read-only\n"
304 " -s, --snapshot use snapshot file\n"
305 " -n, --nocache disable host cache, short for -t none\n"
306 " -C, --copy-on-read enable copy-on-read\n"
307 " -m, --misalign misalign allocations for O_DIRECT\n"
308 " -k, --native-aio use kernel AIO implementation\n"
309 " (Linux only, prefer use of -i)\n"
310 " -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
311 " -t, --cache=MODE use the given cache mode for the image\n"
312 " -d, --discard=MODE use the given discard mode for the image\n"
313 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
314 " specify tracing options\n"
315 " see qemu-img(1) man page for full description\n"
316 " -U, --force-share force shared permissions\n"
317 " -h, --help display this help and exit\n"
318 " -V, --version output version information and exit\n"
320 "See '%s -c help' for information on available commands.\n"
322 QEMU_HELP_BOTTOM
"\n",
326 static char *get_prompt(void)
328 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
331 snprintf(prompt
, sizeof(prompt
), "%s> ", g_get_prgname());
337 static void G_GNUC_PRINTF(2, 3) readline_printf_func(void *opaque
,
338 const char *fmt
, ...)
346 static void readline_flush_func(void *opaque
)
351 static void readline_func(void *opaque
, const char *str
, void *readline_opaque
)
353 char **line
= readline_opaque
;
354 *line
= g_strdup(str
);
357 static void completion_match(const char *cmd
, void *opaque
)
359 readline_add_completion(readline_state
, cmd
);
362 static void readline_completion_func(void *opaque
, const char *str
)
364 readline_set_completion_index(readline_state
, strlen(str
));
365 qemuio_complete_command(str
, completion_match
, NULL
);
368 static char *fetchline_readline(void)
372 readline_start(readline_state
, get_prompt(), 0, readline_func
, &line
);
375 if (ttyEOF
!= 0x0 && ch
== ttyEOF
) {
379 readline_handle_byte(readline_state
, ch
);
384 #define MAXREADLINESZ 1024
385 static char *fetchline_fgets(void)
387 char *p
, *line
= g_malloc(MAXREADLINESZ
);
389 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
394 p
= line
+ strlen(line
);
395 if (p
!= line
&& p
[-1] == '\n') {
402 static char *fetchline(void)
404 if (readline_state
) {
405 return fetchline_readline();
407 return fetchline_fgets();
411 static void prep_fetchline(void *opaque
)
413 int *fetchable
= opaque
;
415 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
419 static int do_qemuio_command(const char *cmd
)
421 return qemuio_command(qemuio_blk
, cmd
);
424 static int command_loop(void)
426 int i
, fetchable
= 0, prompted
= 0;
427 int ret
, last_error
= 0;
430 for (i
= 0; !quit_qemu_io
&& i
< ncmdline
; i
++) {
431 ret
= do_qemuio_command(cmdline
[i
]);
441 while (!quit_qemu_io
) {
443 printf("%s", get_prompt());
445 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
449 main_loop_wait(false);
459 ret
= do_qemuio_command(input
);
469 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
474 static void add_user_command(char *user_cmd
)
476 cmdline
= g_renew(char *, cmdline
, ++ncmdline
);
477 cmdline
[ncmdline
- 1] = user_cmd
;
480 static void reenable_tty_echo(void)
482 qemu_set_tty_echo(STDIN_FILENO
, true);
487 OPTION_IMAGE_OPTS
= 257,
490 static QemuOptsList file_opts
= {
492 .implied_opt_name
= "file",
493 .head
= QTAILQ_HEAD_INITIALIZER(file_opts
.head
),
495 /* no elements => accept any params */
496 { /* end of list */ }
500 int main(int argc
, char **argv
)
503 const char *sopt
= "hVc:d:f:rsnCmki:t:T:U";
504 const struct option lopt
[] = {
505 { "help", no_argument
, NULL
, 'h' },
506 { "version", no_argument
, NULL
, 'V' },
507 { "cmd", required_argument
, NULL
, 'c' },
508 { "format", required_argument
, NULL
, 'f' },
509 { "read-only", no_argument
, NULL
, 'r' },
510 { "snapshot", no_argument
, NULL
, 's' },
511 { "nocache", no_argument
, NULL
, 'n' },
512 { "copy-on-read", no_argument
, NULL
, 'C' },
513 { "misalign", no_argument
, NULL
, 'm' },
514 { "native-aio", no_argument
, NULL
, 'k' },
515 { "aio", required_argument
, NULL
, 'i' },
516 { "discard", required_argument
, NULL
, 'd' },
517 { "cache", required_argument
, NULL
, 't' },
518 { "trace", required_argument
, NULL
, 'T' },
519 { "object", required_argument
, NULL
, OPTION_OBJECT
},
520 { "image-opts", no_argument
, NULL
, OPTION_IMAGE_OPTS
},
521 { "force-share", no_argument
, 0, 'U'},
526 int flags
= BDRV_O_UNMAP
;
528 bool writethrough
= true;
530 const char *format
= NULL
;
531 bool force_share
= false;
534 signal(SIGPIPE
, SIG_IGN
);
539 module_call_init(MODULE_INIT_TRACE
);
540 qemu_init_exec_dir(argv
[0]);
542 qcrypto_init(&error_fatal
);
544 module_call_init(MODULE_INIT_QOM
);
545 qemu_add_opts(&qemu_trace_opts
);
548 while ((c
= getopt_long(argc
, argv
, sopt
, lopt
, &opt_index
)) != -1) {
551 flags
|= BDRV_O_SNAPSHOT
;
554 flags
|= BDRV_O_NOCACHE
;
555 writethrough
= false;
558 flags
|= BDRV_O_COPY_ON_READ
;
561 if (bdrv_parse_discard_flags(optarg
, &flags
) < 0) {
562 error_report("Invalid discard option: %s", optarg
);
570 add_user_command(optarg
);
576 qemuio_misalign
= true;
579 flags
|= BDRV_O_NATIVE_AIO
;
582 if (bdrv_parse_aio(optarg
, &flags
) < 0) {
583 error_report("Invalid aio option: %s", optarg
);
588 if (bdrv_parse_cache_mode(optarg
, &flags
, &writethrough
) < 0) {
589 error_report("Invalid cache option: %s", optarg
);
594 trace_opt_parse(optarg
);
597 printf("%s version " QEMU_FULL_VERSION
"\n"
598 QEMU_COPYRIGHT
"\n", g_get_prgname());
601 usage(g_get_prgname());
607 user_creatable_process_cmdline(optarg
);
609 case OPTION_IMAGE_OPTS
:
613 usage(g_get_prgname());
618 if ((argc
- optind
) > 1) {
619 usage(g_get_prgname());
623 if (format
&& imageOpts
) {
624 error_report("--image-opts and -f are mutually exclusive");
628 qemu_init_main_loop(&error_fatal
);
630 if (!trace_init_backends()) {
634 qemu_set_log(LOG_TRACE
, &error_fatal
);
636 /* initialize commands */
637 qemuio_add_command(&quit_cmd
);
638 qemuio_add_command(&open_cmd
);
639 qemuio_add_command(&close_cmd
);
641 if (isatty(STDIN_FILENO
)) {
642 ttyEOF
= get_eof_char();
643 readline_state
= readline_init(readline_printf_func
,
646 readline_completion_func
);
647 qemu_set_tty_echo(STDIN_FILENO
, false);
648 atexit(reenable_tty_echo
);
651 /* open the device */
653 flags
|= BDRV_O_RDWR
;
656 if ((argc
- optind
) == 1) {
658 QemuOpts
*qopts
= NULL
;
659 qopts
= qemu_opts_parse_noisily(&file_opts
, argv
[optind
], false);
663 opts
= qemu_opts_to_qdict(qopts
, NULL
);
664 if (openfile(NULL
, flags
, writethrough
, force_share
, opts
)) {
670 qdict_put_str(opts
, "driver", format
);
672 if (openfile(argv
[optind
], flags
, writethrough
,
673 force_share
, opts
)) {
678 ret
= command_loop();
681 * Make sure all outstanding requests complete before the program exits.
685 blk_unref(qemuio_blk
);
686 g_free(readline_state
);