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.
10 #include "qemu/osdep.h"
14 #include "qapi/error.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/option.h"
19 #include "qemu/config-file.h"
20 #include "qemu/readline.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qapi/qmp/qbool.h"
24 #include "qom/object_interfaces.h"
25 #include "sysemu/block-backend.h"
26 #include "block/block_int.h"
27 #include "trace/control.h"
28 #include "crypto/init.h"
29 #include "qemu-version.h"
31 #define CMD_NOFILE_OK 0x01
33 static char *progname;
35 static BlockBackend *qemuio_blk;
37 /* qemu-io commands passed using -c */
39 static char **cmdline;
40 static bool imageOpts;
42 static ReadLineState *readline_state;
44 static int close_f(BlockBackend *blk, int argc, char **argv)
46 blk_unref(qemuio_blk);
51 static const cmdinfo_t close_cmd = {
55 .oneline = "close the current open file",
58 static int openfile(char *name, int flags, bool writethrough, bool force_share,
61 Error *local_err = NULL;
64 error_report("file open already, try 'help close'");
73 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
74 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
75 error_report("-U conflicts with image options");
79 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
81 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
83 error_reportf_err(local_err, "can't open%s%s: ",
84 name ? " device " : "", name ?: "");
88 blk_set_enable_write_cache(qemuio_blk, !writethrough);
93 static void open_help(void)
97 " opens a new file in the requested mode\n"
100 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
102 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
103 " -r, -- open file read-only\n"
104 " -s, -- use snapshot file\n"
105 " -n, -- disable host cache, short for -t none\n"
106 " -U, -- force shared permissions\n"
107 " -k, -- use kernel AIO implementation (on Linux only)\n"
108 " -t, -- use the given cache mode for the image\n"
109 " -d, -- use the given discard mode for the image\n"
110 " -o, -- options to be given to the block driver"
114 static int open_f(BlockBackend *blk, int argc, char **argv);
116 static const cmdinfo_t open_cmd = {
122 .flags = CMD_NOFILE_OK,
123 .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]",
124 .oneline = "open the file specified by path",
128 static QemuOptsList empty_opts = {
131 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
133 /* no elements => accept any params */
134 { /* end of list */ }
138 static int open_f(BlockBackend *blk, int argc, char **argv)
140 int flags = BDRV_O_UNMAP;
142 bool writethrough = true;
146 bool force_share = false;
148 while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) {
151 flags |= BDRV_O_SNAPSHOT;
154 flags |= BDRV_O_NOCACHE;
155 writethrough = false;
161 flags |= BDRV_O_NATIVE_AIO;
164 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
165 error_report("Invalid cache option: %s", optarg);
166 qemu_opts_reset(&empty_opts);
171 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
172 error_report("Invalid discard option: %s", optarg);
173 qemu_opts_reset(&empty_opts);
179 printf("--image-opts and 'open -o' are mutually exclusive\n");
180 qemu_opts_reset(&empty_opts);
183 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
184 qemu_opts_reset(&empty_opts);
192 qemu_opts_reset(&empty_opts);
193 return qemuio_command_usage(&open_cmd);
198 flags |= BDRV_O_RDWR;
201 if (imageOpts && (optind == argc - 1)) {
202 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
203 qemu_opts_reset(&empty_opts);
209 qopts = qemu_opts_find(&empty_opts, NULL);
210 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
211 qemu_opts_reset(&empty_opts);
213 if (optind == argc - 1) {
214 openfile(argv[optind], flags, writethrough, force_share, opts);
215 } else if (optind == argc) {
216 openfile(NULL, flags, writethrough, force_share, opts);
219 qemuio_command_usage(&open_cmd);
224 static int quit_f(BlockBackend *blk, int argc, char **argv)
229 static const cmdinfo_t quit_cmd = {
235 .flags = CMD_FLAG_GLOBAL,
236 .oneline = "exit the program",
239 static void usage(const char *name)
242 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
243 "QEMU Disk exerciser\n"
245 " --object OBJECTDEF define an object such as 'secret' for\n"
246 " passwords and/or encryption keys\n"
247 " --image-opts treat file as option string\n"
248 " -c, --cmd STRING execute command with its arguments\n"
249 " from the given string\n"
250 " -f, --format FMT specifies the block driver to use\n"
251 " -r, --read-only export read-only\n"
252 " -s, --snapshot use snapshot file\n"
253 " -n, --nocache disable host cache, short for -t none\n"
254 " -m, --misalign misalign allocations for O_DIRECT\n"
255 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
256 " -t, --cache=MODE use the given cache mode for the image\n"
257 " -d, --discard=MODE use the given discard mode for the image\n"
258 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
259 " specify tracing options\n"
260 " see qemu-img(1) man page for full description\n"
261 " -U, --force-share force shared permissions\n"
262 " -h, --help display this help and exit\n"
263 " -V, --version output version information and exit\n"
265 "See '%s -c help' for information on available commands.\n"
267 QEMU_HELP_BOTTOM "\n",
271 static char *get_prompt(void)
273 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
276 snprintf(prompt, sizeof(prompt), "%s> ", progname);
282 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
283 const char *fmt, ...)
291 static void readline_flush_func(void *opaque)
296 static void readline_func(void *opaque, const char *str, void *readline_opaque)
298 char **line = readline_opaque;
299 *line = g_strdup(str);
302 static void completion_match(const char *cmd, void *opaque)
304 readline_add_completion(readline_state, cmd);
307 static void readline_completion_func(void *opaque, const char *str)
309 readline_set_completion_index(readline_state, strlen(str));
310 qemuio_complete_command(str, completion_match, NULL);
313 static char *fetchline_readline(void)
317 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
323 readline_handle_byte(readline_state, ch);
328 #define MAXREADLINESZ 1024
329 static char *fetchline_fgets(void)
331 char *p, *line = g_malloc(MAXREADLINESZ);
333 if (!fgets(line, MAXREADLINESZ, stdin)) {
338 p = line + strlen(line);
339 if (p != line && p[-1] == '\n') {
346 static char *fetchline(void)
348 if (readline_state) {
349 return fetchline_readline();
351 return fetchline_fgets();
355 static void prep_fetchline(void *opaque)
357 int *fetchable = opaque;
359 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
363 static void command_loop(void)
365 int i, done = 0, fetchable = 0, prompted = 0;
368 for (i = 0; !done && i < ncmdline; i++) {
369 done = qemuio_command(qemuio_blk, cmdline[i]);
378 printf("%s", get_prompt());
380 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
384 main_loop_wait(false);
394 done = qemuio_command(qemuio_blk, input);
400 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
403 static void add_user_command(char *optarg)
405 cmdline = g_renew(char *, cmdline, ++ncmdline);
406 cmdline[ncmdline-1] = optarg;
409 static void reenable_tty_echo(void)
411 qemu_set_tty_echo(STDIN_FILENO, true);
416 OPTION_IMAGE_OPTS = 257,
419 static QemuOptsList qemu_object_opts = {
421 .implied_opt_name = "qom-type",
422 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
429 static QemuOptsList file_opts = {
431 .implied_opt_name = "file",
432 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
434 /* no elements => accept any params */
435 { /* end of list */ }
439 int main(int argc, char **argv)
442 const char *sopt = "hVc:d:f:rsnmkt:T:U";
443 const struct option lopt[] = {
444 { "help", no_argument, NULL, 'h' },
445 { "version", no_argument, NULL, 'V' },
446 { "cmd", required_argument, NULL, 'c' },
447 { "format", required_argument, NULL, 'f' },
448 { "read-only", no_argument, NULL, 'r' },
449 { "snapshot", no_argument, NULL, 's' },
450 { "nocache", no_argument, NULL, 'n' },
451 { "misalign", no_argument, NULL, 'm' },
452 { "native-aio", no_argument, NULL, 'k' },
453 { "discard", required_argument, NULL, 'd' },
454 { "cache", required_argument, NULL, 't' },
455 { "trace", required_argument, NULL, 'T' },
456 { "object", required_argument, NULL, OPTION_OBJECT },
457 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
458 { "force-share", no_argument, 0, 'U'},
463 int flags = BDRV_O_UNMAP;
464 bool writethrough = true;
465 Error *local_error = NULL;
467 const char *format = NULL;
468 char *trace_file = NULL;
469 bool force_share = false;
472 signal(SIGPIPE, SIG_IGN);
475 module_call_init(MODULE_INIT_TRACE);
476 progname = basename(argv[0]);
477 qemu_init_exec_dir(argv[0]);
479 qcrypto_init(&error_fatal);
481 module_call_init(MODULE_INIT_QOM);
482 qemu_add_opts(&qemu_object_opts);
483 qemu_add_opts(&qemu_trace_opts);
486 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
489 flags |= BDRV_O_SNAPSHOT;
492 flags |= BDRV_O_NOCACHE;
493 writethrough = false;
496 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
497 error_report("Invalid discard option: %s", optarg);
505 add_user_command(optarg);
511 qemuio_misalign = true;
514 flags |= BDRV_O_NATIVE_AIO;
517 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
518 error_report("Invalid cache option: %s", optarg);
524 trace_file = trace_opt_parse(optarg);
527 printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
528 QEMU_COPYRIGHT "\n", progname);
536 case OPTION_OBJECT: {
538 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
544 case OPTION_IMAGE_OPTS:
553 if ((argc - optind) > 1) {
558 if (format && imageOpts) {
559 error_report("--image-opts and -f are mutually exclusive");
563 if (qemu_init_main_loop(&local_error)) {
564 error_report_err(local_error);
568 if (qemu_opts_foreach(&qemu_object_opts,
569 user_creatable_add_opts_foreach,
574 if (!trace_init_backends()) {
577 trace_init_file(trace_file);
578 qemu_set_log(LOG_TRACE);
580 /* initialize commands */
581 qemuio_add_command(&quit_cmd);
582 qemuio_add_command(&open_cmd);
583 qemuio_add_command(&close_cmd);
585 if (isatty(STDIN_FILENO)) {
586 readline_state = readline_init(readline_printf_func,
589 readline_completion_func);
590 qemu_set_tty_echo(STDIN_FILENO, false);
591 atexit(reenable_tty_echo);
594 /* open the device */
596 flags |= BDRV_O_RDWR;
599 if ((argc - optind) == 1) {
601 QemuOpts *qopts = NULL;
602 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
606 opts = qemu_opts_to_qdict(qopts, NULL);
607 if (openfile(NULL, flags, writethrough, force_share, opts)) {
613 qdict_put_str(opts, "driver", format);
615 if (openfile(argv[optind], flags, writethrough,
616 force_share, opts)) {
624 * Make sure all outstanding requests complete before the program exits.
628 blk_unref(qemuio_blk);
629 g_free(readline_state);