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 BlockBackend *qemuio_blk;
38 static bool quit_qemu_io;
40 /* qemu-io commands passed using -c */
42 static char **cmdline;
43 static bool imageOpts;
45 static ReadLineState *readline_state;
49 static int get_eof_char(void)
52 return 0x4; /* Ctrl-D */
55 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
56 if (errno == ENOTTY) {
57 return 0x0; /* just expect read() == 0 */
59 return 0x4; /* Ctrl-D */
63 return tty.c_cc[VEOF];
67 static int close_f(BlockBackend *blk, int argc, char **argv)
69 blk_unref(qemuio_blk);
74 static const cmdinfo_t close_cmd = {
78 .oneline = "close the current open file",
81 static int openfile(char *name, int flags, bool writethrough, bool force_share,
84 Error *local_err = NULL;
87 error_report("file open already, try 'help close'");
96 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
97 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
98 error_report("-U conflicts with image options");
102 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
104 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
106 error_reportf_err(local_err, "can't open%s%s: ",
107 name ? " device " : "", name ?: "");
111 blk_set_enable_write_cache(qemuio_blk, !writethrough);
116 static void open_help(void)
120 " opens a new file in the requested mode\n"
123 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
125 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
126 " -r, -- open file read-only\n"
127 " -s, -- use snapshot file\n"
128 " -C, -- use copy-on-read\n"
129 " -n, -- disable host cache, short for -t none\n"
130 " -U, -- force shared permissions\n"
131 " -k, -- use kernel AIO implementation (on Linux only)\n"
132 " -t, -- use the given cache mode for the image\n"
133 " -d, -- use the given discard mode for the image\n"
134 " -o, -- options to be given to the block driver"
138 static int open_f(BlockBackend *blk, int argc, char **argv);
140 static const cmdinfo_t open_cmd = {
146 .flags = CMD_NOFILE_OK,
147 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
148 .oneline = "open the file specified by path",
152 static QemuOptsList empty_opts = {
155 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
157 /* no elements => accept any params */
158 { /* end of list */ }
162 static int open_f(BlockBackend *blk, int argc, char **argv)
164 int flags = BDRV_O_UNMAP;
166 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 qemuio_command_usage(&open_cmd);
227 flags |= BDRV_O_RDWR;
230 if (imageOpts && (optind == argc - 1)) {
231 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
232 qemu_opts_reset(&empty_opts);
238 qopts = qemu_opts_find(&empty_opts, NULL);
239 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
240 qemu_opts_reset(&empty_opts);
242 if (optind == argc - 1) {
243 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
244 } else if (optind == argc) {
245 ret = openfile(NULL, flags, writethrough, force_share, opts);
248 qemuio_command_usage(&open_cmd);
259 static int quit_f(BlockBackend *blk, int argc, char **argv)
265 static const cmdinfo_t quit_cmd = {
271 .flags = CMD_FLAG_GLOBAL,
272 .oneline = "exit the program",
275 static void usage(const char *name)
278 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
279 "QEMU Disk exerciser\n"
281 " --object OBJECTDEF define an object such as 'secret' for\n"
282 " passwords and/or encryption keys\n"
283 " --image-opts treat file as option string\n"
284 " -c, --cmd STRING execute command with its arguments\n"
285 " from the given string\n"
286 " -f, --format FMT specifies the block driver to use\n"
287 " -r, --read-only export read-only\n"
288 " -s, --snapshot use snapshot file\n"
289 " -n, --nocache disable host cache, short for -t none\n"
290 " -C, --copy-on-read enable copy-on-read\n"
291 " -m, --misalign misalign allocations for O_DIRECT\n"
292 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
293 " -t, --cache=MODE use the given cache mode for the image\n"
294 " -d, --discard=MODE use the given discard mode for the image\n"
295 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
296 " specify tracing options\n"
297 " see qemu-img(1) man page for full description\n"
298 " -U, --force-share force shared permissions\n"
299 " -h, --help display this help and exit\n"
300 " -V, --version output version information and exit\n"
302 "See '%s -c help' for information on available commands.\n"
304 QEMU_HELP_BOTTOM "\n",
308 static char *get_prompt(void)
310 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
313 snprintf(prompt, sizeof(prompt), "%s> ", error_get_progname());
319 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
320 const char *fmt, ...)
328 static void readline_flush_func(void *opaque)
333 static void readline_func(void *opaque, const char *str, void *readline_opaque)
335 char **line = readline_opaque;
336 *line = g_strdup(str);
339 static void completion_match(const char *cmd, void *opaque)
341 readline_add_completion(readline_state, cmd);
344 static void readline_completion_func(void *opaque, const char *str)
346 readline_set_completion_index(readline_state, strlen(str));
347 qemuio_complete_command(str, completion_match, NULL);
350 static char *fetchline_readline(void)
354 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
357 if (ttyEOF != 0x0 && ch == ttyEOF) {
361 readline_handle_byte(readline_state, ch);
366 #define MAXREADLINESZ 1024
367 static char *fetchline_fgets(void)
369 char *p, *line = g_malloc(MAXREADLINESZ);
371 if (!fgets(line, MAXREADLINESZ, stdin)) {
376 p = line + strlen(line);
377 if (p != line && p[-1] == '\n') {
384 static char *fetchline(void)
386 if (readline_state) {
387 return fetchline_readline();
389 return fetchline_fgets();
393 static void prep_fetchline(void *opaque)
395 int *fetchable = opaque;
397 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
401 static int command_loop(void)
403 int i, fetchable = 0, prompted = 0;
404 int ret, last_error = 0;
407 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
408 ret = qemuio_command(qemuio_blk, cmdline[i]);
418 while (!quit_qemu_io) {
420 printf("%s", get_prompt());
422 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
426 main_loop_wait(false);
436 ret = qemuio_command(qemuio_blk, input);
446 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
451 static void add_user_command(char *optarg)
453 cmdline = g_renew(char *, cmdline, ++ncmdline);
454 cmdline[ncmdline-1] = optarg;
457 static void reenable_tty_echo(void)
459 qemu_set_tty_echo(STDIN_FILENO, true);
464 OPTION_IMAGE_OPTS = 257,
467 static QemuOptsList qemu_object_opts = {
469 .implied_opt_name = "qom-type",
470 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
477 static QemuOptsList file_opts = {
479 .implied_opt_name = "file",
480 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
482 /* no elements => accept any params */
483 { /* end of list */ }
487 int main(int argc, char **argv)
490 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
491 const struct option lopt[] = {
492 { "help", no_argument, NULL, 'h' },
493 { "version", no_argument, NULL, 'V' },
494 { "cmd", required_argument, NULL, 'c' },
495 { "format", required_argument, NULL, 'f' },
496 { "read-only", no_argument, NULL, 'r' },
497 { "snapshot", no_argument, NULL, 's' },
498 { "nocache", no_argument, NULL, 'n' },
499 { "copy-on-read", no_argument, NULL, 'C' },
500 { "misalign", no_argument, NULL, 'm' },
501 { "native-aio", no_argument, NULL, 'k' },
502 { "discard", required_argument, NULL, 'd' },
503 { "cache", required_argument, NULL, 't' },
504 { "trace", required_argument, NULL, 'T' },
505 { "object", required_argument, NULL, OPTION_OBJECT },
506 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
507 { "force-share", no_argument, 0, 'U'},
512 int flags = BDRV_O_UNMAP;
514 bool writethrough = true;
515 Error *local_error = NULL;
517 const char *format = NULL;
518 char *trace_file = NULL;
519 bool force_share = false;
522 signal(SIGPIPE, SIG_IGN);
526 module_call_init(MODULE_INIT_TRACE);
527 qemu_init_exec_dir(argv[0]);
529 qcrypto_init(&error_fatal);
531 module_call_init(MODULE_INIT_QOM);
532 qemu_add_opts(&qemu_object_opts);
533 qemu_add_opts(&qemu_trace_opts);
536 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
539 flags |= BDRV_O_SNAPSHOT;
542 flags |= BDRV_O_NOCACHE;
543 writethrough = false;
546 flags |= BDRV_O_COPY_ON_READ;
549 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
550 error_report("Invalid discard option: %s", optarg);
558 add_user_command(optarg);
564 qemuio_misalign = true;
567 flags |= BDRV_O_NATIVE_AIO;
570 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
571 error_report("Invalid cache option: %s", optarg);
577 trace_file = trace_opt_parse(optarg);
580 printf("%s version " QEMU_FULL_VERSION "\n"
581 QEMU_COPYRIGHT "\n", error_get_progname());
584 usage(error_get_progname());
589 case OPTION_OBJECT: {
591 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
597 case OPTION_IMAGE_OPTS:
601 usage(error_get_progname());
606 if ((argc - optind) > 1) {
607 usage(error_get_progname());
611 if (format && imageOpts) {
612 error_report("--image-opts and -f are mutually exclusive");
616 if (qemu_init_main_loop(&local_error)) {
617 error_report_err(local_error);
621 qemu_opts_foreach(&qemu_object_opts,
622 user_creatable_add_opts_foreach,
625 if (!trace_init_backends()) {
628 trace_init_file(trace_file);
629 qemu_set_log(LOG_TRACE);
631 /* initialize commands */
632 qemuio_add_command(&quit_cmd);
633 qemuio_add_command(&open_cmd);
634 qemuio_add_command(&close_cmd);
636 if (isatty(STDIN_FILENO)) {
637 ttyEOF = get_eof_char();
638 readline_state = readline_init(readline_printf_func,
641 readline_completion_func);
642 qemu_set_tty_echo(STDIN_FILENO, false);
643 atexit(reenable_tty_echo);
646 /* open the device */
648 flags |= BDRV_O_RDWR;
651 if ((argc - optind) == 1) {
653 QemuOpts *qopts = NULL;
654 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
658 opts = qemu_opts_to_qdict(qopts, NULL);
659 if (openfile(NULL, flags, writethrough, force_share, opts)) {
665 qdict_put_str(opts, "driver", format);
667 if (openfile(argv[optind], flags, writethrough,
668 force_share, opts)) {
673 ret = command_loop();
676 * Make sure all outstanding requests complete before the program exits.
680 blk_unref(qemuio_blk);
681 g_free(readline_state);