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"
30 #define CMD_NOFILE_OK 0x01
32 static char *progname;
34 static BlockBackend *qemuio_blk;
36 /* qemu-io commands passed using -c */
38 static char **cmdline;
39 static bool imageOpts;
41 static ReadLineState *readline_state;
43 static int close_f(BlockBackend *blk, int argc, char **argv)
45 blk_unref(qemuio_blk);
50 static const cmdinfo_t close_cmd = {
54 .oneline = "close the current open file",
57 static int openfile(char *name, int flags, bool writethrough, bool force_share,
60 Error *local_err = NULL;
63 error_report("file open already, try 'help close'");
72 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
73 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
74 error_report("-U conflicts with image options");
78 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
80 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
82 error_reportf_err(local_err, "can't open%s%s: ",
83 name ? " device " : "", name ?: "");
87 blk_set_enable_write_cache(qemuio_blk, !writethrough);
92 static void open_help(void)
96 " opens a new file in the requested mode\n"
99 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
101 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
102 " -r, -- open file read-only\n"
103 " -s, -- use snapshot file\n"
104 " -n, -- disable host cache, short for -t none\n"
105 " -U, -- force shared permissions\n"
106 " -k, -- use kernel AIO implementation (on Linux only)\n"
107 " -t, -- use the given cache mode for the image\n"
108 " -d, -- use the given discard mode for the image\n"
109 " -o, -- options to be given to the block driver"
113 static int open_f(BlockBackend *blk, int argc, char **argv);
115 static const cmdinfo_t open_cmd = {
121 .flags = CMD_NOFILE_OK,
122 .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]",
123 .oneline = "open the file specified by path",
127 static QemuOptsList empty_opts = {
130 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
132 /* no elements => accept any params */
133 { /* end of list */ }
137 static int open_f(BlockBackend *blk, int argc, char **argv)
139 int flags = BDRV_O_UNMAP;
141 bool writethrough = true;
145 bool force_share = false;
147 while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) {
150 flags |= BDRV_O_SNAPSHOT;
153 flags |= BDRV_O_NOCACHE;
154 writethrough = false;
160 flags |= BDRV_O_NATIVE_AIO;
163 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
164 error_report("Invalid cache option: %s", optarg);
165 qemu_opts_reset(&empty_opts);
170 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
171 error_report("Invalid discard option: %s", optarg);
172 qemu_opts_reset(&empty_opts);
178 printf("--image-opts and 'open -o' are mutually exclusive\n");
179 qemu_opts_reset(&empty_opts);
182 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
183 qemu_opts_reset(&empty_opts);
191 qemu_opts_reset(&empty_opts);
192 return qemuio_command_usage(&open_cmd);
197 flags |= BDRV_O_RDWR;
200 if (imageOpts && (optind == argc - 1)) {
201 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
202 qemu_opts_reset(&empty_opts);
208 qopts = qemu_opts_find(&empty_opts, NULL);
209 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
210 qemu_opts_reset(&empty_opts);
212 if (optind == argc - 1) {
213 openfile(argv[optind], flags, writethrough, force_share, opts);
214 } else if (optind == argc) {
215 openfile(NULL, flags, writethrough, force_share, opts);
218 qemuio_command_usage(&open_cmd);
223 static int quit_f(BlockBackend *blk, int argc, char **argv)
228 static const cmdinfo_t quit_cmd = {
234 .flags = CMD_FLAG_GLOBAL,
235 .oneline = "exit the program",
238 static void usage(const char *name)
241 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
242 "QEMU Disk exerciser\n"
244 " --object OBJECTDEF define an object such as 'secret' for\n"
245 " passwords and/or encryption keys\n"
246 " --image-opts treat file as option string\n"
247 " -c, --cmd STRING execute command with its arguments\n"
248 " from the given string\n"
249 " -f, --format FMT specifies the block driver to use\n"
250 " -r, --read-only export read-only\n"
251 " -s, --snapshot use snapshot file\n"
252 " -n, --nocache disable host cache, short for -t none\n"
253 " -m, --misalign misalign allocations for O_DIRECT\n"
254 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
255 " -t, --cache=MODE use the given cache mode for the image\n"
256 " -d, --discard=MODE use the given discard mode for the image\n"
257 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
258 " specify tracing options\n"
259 " see qemu-img(1) man page for full description\n"
260 " -U, --force-share force shared permissions\n"
261 " -h, --help display this help and exit\n"
262 " -V, --version output version information and exit\n"
264 "See '%s -c help' for information on available commands."
269 static char *get_prompt(void)
271 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
274 snprintf(prompt, sizeof(prompt), "%s> ", progname);
280 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
281 const char *fmt, ...)
289 static void readline_flush_func(void *opaque)
294 static void readline_func(void *opaque, const char *str, void *readline_opaque)
296 char **line = readline_opaque;
297 *line = g_strdup(str);
300 static void completion_match(const char *cmd, void *opaque)
302 readline_add_completion(readline_state, cmd);
305 static void readline_completion_func(void *opaque, const char *str)
307 readline_set_completion_index(readline_state, strlen(str));
308 qemuio_complete_command(str, completion_match, NULL);
311 static char *fetchline_readline(void)
315 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
321 readline_handle_byte(readline_state, ch);
326 #define MAXREADLINESZ 1024
327 static char *fetchline_fgets(void)
329 char *p, *line = g_malloc(MAXREADLINESZ);
331 if (!fgets(line, MAXREADLINESZ, stdin)) {
336 p = line + strlen(line);
337 if (p != line && p[-1] == '\n') {
344 static char *fetchline(void)
346 if (readline_state) {
347 return fetchline_readline();
349 return fetchline_fgets();
353 static void prep_fetchline(void *opaque)
355 int *fetchable = opaque;
357 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
361 static void command_loop(void)
363 int i, done = 0, fetchable = 0, prompted = 0;
366 for (i = 0; !done && i < ncmdline; i++) {
367 done = qemuio_command(qemuio_blk, cmdline[i]);
376 printf("%s", get_prompt());
378 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
382 main_loop_wait(false);
392 done = qemuio_command(qemuio_blk, input);
398 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
401 static void add_user_command(char *optarg)
403 cmdline = g_renew(char *, cmdline, ++ncmdline);
404 cmdline[ncmdline-1] = optarg;
407 static void reenable_tty_echo(void)
409 qemu_set_tty_echo(STDIN_FILENO, true);
414 OPTION_IMAGE_OPTS = 257,
417 static QemuOptsList qemu_object_opts = {
419 .implied_opt_name = "qom-type",
420 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
427 static QemuOptsList file_opts = {
429 .implied_opt_name = "file",
430 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
432 /* no elements => accept any params */
433 { /* end of list */ }
437 int main(int argc, char **argv)
440 const char *sopt = "hVc:d:f:rsnmkt:T:U";
441 const struct option lopt[] = {
442 { "help", no_argument, NULL, 'h' },
443 { "version", no_argument, NULL, 'V' },
444 { "cmd", required_argument, NULL, 'c' },
445 { "format", required_argument, NULL, 'f' },
446 { "read-only", no_argument, NULL, 'r' },
447 { "snapshot", no_argument, NULL, 's' },
448 { "nocache", no_argument, NULL, 'n' },
449 { "misalign", no_argument, NULL, 'm' },
450 { "native-aio", no_argument, NULL, 'k' },
451 { "discard", required_argument, NULL, 'd' },
452 { "cache", required_argument, NULL, 't' },
453 { "trace", required_argument, NULL, 'T' },
454 { "object", required_argument, NULL, OPTION_OBJECT },
455 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
456 { "force-share", no_argument, 0, 'U'},
461 int flags = BDRV_O_UNMAP;
462 bool writethrough = true;
463 Error *local_error = NULL;
465 const char *format = NULL;
466 char *trace_file = NULL;
467 bool force_share = false;
470 signal(SIGPIPE, SIG_IGN);
473 module_call_init(MODULE_INIT_TRACE);
474 progname = basename(argv[0]);
475 qemu_init_exec_dir(argv[0]);
477 qcrypto_init(&error_fatal);
479 module_call_init(MODULE_INIT_QOM);
480 qemu_add_opts(&qemu_object_opts);
481 qemu_add_opts(&qemu_trace_opts);
484 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
487 flags |= BDRV_O_SNAPSHOT;
490 flags |= BDRV_O_NOCACHE;
491 writethrough = false;
494 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
495 error_report("Invalid discard option: %s", optarg);
503 add_user_command(optarg);
509 qemuio_misalign = true;
512 flags |= BDRV_O_NATIVE_AIO;
515 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
516 error_report("Invalid cache option: %s", optarg);
522 trace_file = trace_opt_parse(optarg);
525 printf("%s version %s\n", progname, QEMU_VERSION);
533 case OPTION_OBJECT: {
535 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
541 case OPTION_IMAGE_OPTS:
550 if ((argc - optind) > 1) {
555 if (format && imageOpts) {
556 error_report("--image-opts and -f are mutually exclusive");
560 if (qemu_init_main_loop(&local_error)) {
561 error_report_err(local_error);
565 if (qemu_opts_foreach(&qemu_object_opts,
566 user_creatable_add_opts_foreach,
571 if (!trace_init_backends()) {
574 trace_init_file(trace_file);
575 qemu_set_log(LOG_TRACE);
577 /* initialize commands */
578 qemuio_add_command(&quit_cmd);
579 qemuio_add_command(&open_cmd);
580 qemuio_add_command(&close_cmd);
582 if (isatty(STDIN_FILENO)) {
583 readline_state = readline_init(readline_printf_func,
586 readline_completion_func);
587 qemu_set_tty_echo(STDIN_FILENO, false);
588 atexit(reenable_tty_echo);
591 /* open the device */
593 flags |= BDRV_O_RDWR;
596 if ((argc - optind) == 1) {
598 QemuOpts *qopts = NULL;
599 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
603 opts = qemu_opts_to_qdict(qopts, NULL);
604 if (openfile(NULL, flags, writethrough, force_share, opts)) {
610 qdict_put_str(opts, "driver", format);
612 if (openfile(argv[optind], flags, writethrough,
613 force_share, opts)) {
621 * Make sure all outstanding requests complete before the program exits.
625 blk_unref(qemuio_blk);
626 g_free(readline_state);