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"
15 #include "qapi/error.h"
17 #include "qemu/error-report.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "qemu/config-file.h"
21 #include "qemu/readline.h"
23 #include "qapi/qmp/qstring.h"
24 #include "qapi/qmp/qbool.h"
25 #include "qapi/qmp/qdict.h"
26 #include "qom/object_interfaces.h"
27 #include "sysemu/block-backend.h"
28 #include "block/block_int.h"
29 #include "trace/control.h"
30 #include "crypto/init.h"
31 #include "qemu-version.h"
33 #define CMD_NOFILE_OK 0x01
35 static char *progname;
37 static BlockBackend *qemuio_blk;
39 /* qemu-io commands passed using -c */
41 static char **cmdline;
42 static bool imageOpts;
44 static ReadLineState *readline_state;
46 static int close_f(BlockBackend *blk, int argc, char **argv)
48 blk_unref(qemuio_blk);
53 static const cmdinfo_t close_cmd = {
57 .oneline = "close the current open file",
60 static int openfile(char *name, int flags, bool writethrough, bool force_share,
63 Error *local_err = NULL;
66 error_report("file open already, try 'help close'");
75 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
76 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
77 error_report("-U conflicts with image options");
81 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
83 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
85 error_reportf_err(local_err, "can't open%s%s: ",
86 name ? " device " : "", name ?: "");
90 blk_set_enable_write_cache(qemuio_blk, !writethrough);
95 static void open_help(void)
99 " opens a new file in the requested mode\n"
102 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
104 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
105 " -r, -- open file read-only\n"
106 " -s, -- use snapshot file\n"
107 " -C, -- use copy-on-read\n"
108 " -n, -- disable host cache, short for -t none\n"
109 " -U, -- force shared permissions\n"
110 " -k, -- use kernel AIO implementation (on Linux only)\n"
111 " -t, -- use the given cache mode for the image\n"
112 " -d, -- use the given discard mode for the image\n"
113 " -o, -- options to be given to the block driver"
117 static int open_f(BlockBackend *blk, int argc, char **argv);
119 static const cmdinfo_t open_cmd = {
125 .flags = CMD_NOFILE_OK,
126 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
127 .oneline = "open the file specified by path",
131 static QemuOptsList empty_opts = {
134 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
136 /* no elements => accept any params */
137 { /* end of list */ }
141 static int open_f(BlockBackend *blk, int argc, char **argv)
143 int flags = BDRV_O_UNMAP;
145 bool writethrough = true;
149 bool force_share = false;
151 while ((c = getopt(argc, argv, "snCro:kt:d:U")) != -1) {
154 flags |= BDRV_O_SNAPSHOT;
157 flags |= BDRV_O_NOCACHE;
158 writethrough = false;
161 flags |= BDRV_O_COPY_ON_READ;
167 flags |= BDRV_O_NATIVE_AIO;
170 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
171 error_report("Invalid cache option: %s", optarg);
172 qemu_opts_reset(&empty_opts);
177 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
178 error_report("Invalid discard option: %s", optarg);
179 qemu_opts_reset(&empty_opts);
185 printf("--image-opts and 'open -o' are mutually exclusive\n");
186 qemu_opts_reset(&empty_opts);
189 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
190 qemu_opts_reset(&empty_opts);
198 qemu_opts_reset(&empty_opts);
199 return qemuio_command_usage(&open_cmd);
204 flags |= BDRV_O_RDWR;
207 if (imageOpts && (optind == argc - 1)) {
208 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
209 qemu_opts_reset(&empty_opts);
215 qopts = qemu_opts_find(&empty_opts, NULL);
216 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
217 qemu_opts_reset(&empty_opts);
219 if (optind == argc - 1) {
220 openfile(argv[optind], flags, writethrough, force_share, opts);
221 } else if (optind == argc) {
222 openfile(NULL, flags, writethrough, force_share, opts);
225 qemuio_command_usage(&open_cmd);
230 static int quit_f(BlockBackend *blk, int argc, char **argv)
235 static const cmdinfo_t quit_cmd = {
241 .flags = CMD_FLAG_GLOBAL,
242 .oneline = "exit the program",
245 static void usage(const char *name)
248 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
249 "QEMU Disk exerciser\n"
251 " --object OBJECTDEF define an object such as 'secret' for\n"
252 " passwords and/or encryption keys\n"
253 " --image-opts treat file as option string\n"
254 " -c, --cmd STRING execute command with its arguments\n"
255 " from the given string\n"
256 " -f, --format FMT specifies the block driver to use\n"
257 " -r, --read-only export read-only\n"
258 " -s, --snapshot use snapshot file\n"
259 " -n, --nocache disable host cache, short for -t none\n"
260 " -C, --copy-on-read enable copy-on-read\n"
261 " -m, --misalign misalign allocations for O_DIRECT\n"
262 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
263 " -t, --cache=MODE use the given cache mode for the image\n"
264 " -d, --discard=MODE use the given discard mode for the image\n"
265 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
266 " specify tracing options\n"
267 " see qemu-img(1) man page for full description\n"
268 " -U, --force-share force shared permissions\n"
269 " -h, --help display this help and exit\n"
270 " -V, --version output version information and exit\n"
272 "See '%s -c help' for information on available commands.\n"
274 QEMU_HELP_BOTTOM "\n",
278 static char *get_prompt(void)
280 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
283 snprintf(prompt, sizeof(prompt), "%s> ", progname);
289 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
290 const char *fmt, ...)
298 static void readline_flush_func(void *opaque)
303 static void readline_func(void *opaque, const char *str, void *readline_opaque)
305 char **line = readline_opaque;
306 *line = g_strdup(str);
309 static void completion_match(const char *cmd, void *opaque)
311 readline_add_completion(readline_state, cmd);
314 static void readline_completion_func(void *opaque, const char *str)
316 readline_set_completion_index(readline_state, strlen(str));
317 qemuio_complete_command(str, completion_match, NULL);
320 static char *fetchline_readline(void)
324 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
330 readline_handle_byte(readline_state, ch);
335 #define MAXREADLINESZ 1024
336 static char *fetchline_fgets(void)
338 char *p, *line = g_malloc(MAXREADLINESZ);
340 if (!fgets(line, MAXREADLINESZ, stdin)) {
345 p = line + strlen(line);
346 if (p != line && p[-1] == '\n') {
353 static char *fetchline(void)
355 if (readline_state) {
356 return fetchline_readline();
358 return fetchline_fgets();
362 static void prep_fetchline(void *opaque)
364 int *fetchable = opaque;
366 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
370 static void command_loop(void)
372 int i, done = 0, fetchable = 0, prompted = 0;
375 for (i = 0; !done && i < ncmdline; i++) {
376 done = qemuio_command(qemuio_blk, cmdline[i]);
385 printf("%s", get_prompt());
387 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
391 main_loop_wait(false);
401 done = qemuio_command(qemuio_blk, input);
407 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
410 static void add_user_command(char *optarg)
412 cmdline = g_renew(char *, cmdline, ++ncmdline);
413 cmdline[ncmdline-1] = optarg;
416 static void reenable_tty_echo(void)
418 qemu_set_tty_echo(STDIN_FILENO, true);
423 OPTION_IMAGE_OPTS = 257,
426 static QemuOptsList qemu_object_opts = {
428 .implied_opt_name = "qom-type",
429 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
436 static QemuOptsList file_opts = {
438 .implied_opt_name = "file",
439 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
441 /* no elements => accept any params */
442 { /* end of list */ }
446 int main(int argc, char **argv)
449 const char *sopt = "hVc:d:f:rsnCmkt:T:U";
450 const struct option lopt[] = {
451 { "help", no_argument, NULL, 'h' },
452 { "version", no_argument, NULL, 'V' },
453 { "cmd", required_argument, NULL, 'c' },
454 { "format", required_argument, NULL, 'f' },
455 { "read-only", no_argument, NULL, 'r' },
456 { "snapshot", no_argument, NULL, 's' },
457 { "nocache", no_argument, NULL, 'n' },
458 { "copy-on-read", no_argument, NULL, 'C' },
459 { "misalign", no_argument, NULL, 'm' },
460 { "native-aio", no_argument, NULL, 'k' },
461 { "discard", required_argument, NULL, 'd' },
462 { "cache", required_argument, NULL, 't' },
463 { "trace", required_argument, NULL, 'T' },
464 { "object", required_argument, NULL, OPTION_OBJECT },
465 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
466 { "force-share", no_argument, 0, 'U'},
471 int flags = BDRV_O_UNMAP;
472 bool writethrough = true;
473 Error *local_error = NULL;
475 const char *format = NULL;
476 char *trace_file = NULL;
477 bool force_share = false;
480 signal(SIGPIPE, SIG_IGN);
483 module_call_init(MODULE_INIT_TRACE);
484 progname = basename(argv[0]);
485 qemu_init_exec_dir(argv[0]);
487 qcrypto_init(&error_fatal);
489 module_call_init(MODULE_INIT_QOM);
490 qemu_add_opts(&qemu_object_opts);
491 qemu_add_opts(&qemu_trace_opts);
494 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
497 flags |= BDRV_O_SNAPSHOT;
500 flags |= BDRV_O_NOCACHE;
501 writethrough = false;
504 flags |= BDRV_O_COPY_ON_READ;
507 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
508 error_report("Invalid discard option: %s", optarg);
516 add_user_command(optarg);
522 qemuio_misalign = true;
525 flags |= BDRV_O_NATIVE_AIO;
528 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
529 error_report("Invalid cache option: %s", optarg);
535 trace_file = trace_opt_parse(optarg);
538 printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
539 QEMU_COPYRIGHT "\n", progname);
547 case OPTION_OBJECT: {
549 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
555 case OPTION_IMAGE_OPTS:
564 if ((argc - optind) > 1) {
569 if (format && imageOpts) {
570 error_report("--image-opts and -f are mutually exclusive");
574 if (qemu_init_main_loop(&local_error)) {
575 error_report_err(local_error);
579 if (qemu_opts_foreach(&qemu_object_opts,
580 user_creatable_add_opts_foreach,
585 if (!trace_init_backends()) {
588 trace_init_file(trace_file);
589 qemu_set_log(LOG_TRACE);
591 /* initialize commands */
592 qemuio_add_command(&quit_cmd);
593 qemuio_add_command(&open_cmd);
594 qemuio_add_command(&close_cmd);
596 if (isatty(STDIN_FILENO)) {
597 readline_state = readline_init(readline_printf_func,
600 readline_completion_func);
601 qemu_set_tty_echo(STDIN_FILENO, false);
602 atexit(reenable_tty_echo);
605 /* open the device */
607 flags |= BDRV_O_RDWR;
610 if ((argc - optind) == 1) {
612 QemuOpts *qopts = NULL;
613 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
617 opts = qemu_opts_to_qdict(qopts, NULL);
618 if (openfile(NULL, flags, writethrough, force_share, opts)) {
624 qdict_put_str(opts, "driver", format);
626 if (openfile(argv[optind], flags, writethrough,
627 force_share, opts)) {
635 * Make sure all outstanding requests complete before the program exits.
639 blk_unref(qemuio_blk);
640 g_free(readline_state);