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"
21 #include "qapi/qmp/qstring.h"
22 #include "qom/object_interfaces.h"
23 #include "sysemu/block-backend.h"
24 #include "block/block_int.h"
25 #include "trace/control.h"
27 #define CMD_NOFILE_OK 0x01
29 static char *progname;
31 static BlockBackend *qemuio_blk;
33 /* qemu-io commands passed using -c */
35 static char **cmdline;
36 static bool imageOpts;
38 static ReadLineState *readline_state;
40 static int close_f(BlockBackend *blk, int argc, char **argv)
42 blk_unref(qemuio_blk);
47 static const cmdinfo_t close_cmd = {
51 .oneline = "close the current open file",
54 static int openfile(char *name, int flags, QDict *opts)
56 Error *local_err = NULL;
60 error_report("file open already, try 'help close'");
65 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
67 error_reportf_err(local_err, "can't open%s%s: ",
68 name ? " device " : "", name ?: "");
72 bs = blk_bs(qemuio_blk);
73 if (bdrv_is_encrypted(bs)) {
75 printf("Disk image '%s' is encrypted.\n", name);
76 if (qemu_read_password(password, sizeof(password)) < 0) {
77 error_report("No password given");
80 if (bdrv_set_key(bs, password) < 0) {
81 error_report("invalid password");
90 blk_unref(qemuio_blk);
95 static void open_help(void)
99 " opens a new file in the requested mode\n"
102 " 'open -Cn /tmp/data' - creates/opens data file read-write and 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 " -n, -- disable host cache\n"
108 " -o, -- options to be given to the block driver"
112 static int open_f(BlockBackend *blk, int argc, char **argv);
114 static const cmdinfo_t open_cmd = {
120 .flags = CMD_NOFILE_OK,
121 .args = "[-Crsn] [-o options] [path]",
122 .oneline = "open the file specified by path",
126 static QemuOptsList empty_opts = {
129 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
131 /* no elements => accept any params */
132 { /* end of list */ }
136 static int open_f(BlockBackend *blk, int argc, char **argv)
144 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
147 flags |= BDRV_O_SNAPSHOT;
150 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
157 printf("--image-opts and 'open -o' are mutually exclusive\n");
160 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
161 qemu_opts_reset(&empty_opts);
166 qemu_opts_reset(&empty_opts);
167 return qemuio_command_usage(&open_cmd);
172 flags |= BDRV_O_RDWR;
175 if (imageOpts && (optind == argc - 1)) {
176 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
177 qemu_opts_reset(&empty_opts);
183 qopts = qemu_opts_find(&empty_opts, NULL);
184 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
185 qemu_opts_reset(&empty_opts);
187 if (optind == argc - 1) {
188 return openfile(argv[optind], flags, opts);
189 } else if (optind == argc) {
190 return openfile(NULL, flags, opts);
193 return qemuio_command_usage(&open_cmd);
197 static int quit_f(BlockBackend *blk, int argc, char **argv)
202 static const cmdinfo_t quit_cmd = {
208 .flags = CMD_FLAG_GLOBAL,
209 .oneline = "exit the program",
212 static void usage(const char *name)
215 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
216 "QEMU Disk exerciser\n"
218 " --object OBJECTDEF define an object such as 'secret' for\n"
219 " passwords and/or encryption keys\n"
220 " -c, --cmd STRING execute command with its arguments\n"
221 " from the given string\n"
222 " -f, --format FMT specifies the block driver to use\n"
223 " -r, --read-only export read-only\n"
224 " -s, --snapshot use snapshot file\n"
225 " -n, --nocache disable host cache\n"
226 " -m, --misalign misalign allocations for O_DIRECT\n"
227 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
228 " -t, --cache=MODE use the given cache mode for the image\n"
229 " -T, --trace FILE enable trace events listed in the given file\n"
230 " -h, --help display this help and exit\n"
231 " -V, --version output version information and exit\n"
233 "See '%s -c help' for information on available commands."
238 static char *get_prompt(void)
240 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
243 snprintf(prompt, sizeof(prompt), "%s> ", progname);
249 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
250 const char *fmt, ...)
258 static void readline_flush_func(void *opaque)
263 static void readline_func(void *opaque, const char *str, void *readline_opaque)
265 char **line = readline_opaque;
266 *line = g_strdup(str);
269 static void completion_match(const char *cmd, void *opaque)
271 readline_add_completion(readline_state, cmd);
274 static void readline_completion_func(void *opaque, const char *str)
276 readline_set_completion_index(readline_state, strlen(str));
277 qemuio_complete_command(str, completion_match, NULL);
280 static char *fetchline_readline(void)
284 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
290 readline_handle_byte(readline_state, ch);
295 #define MAXREADLINESZ 1024
296 static char *fetchline_fgets(void)
298 char *p, *line = g_malloc(MAXREADLINESZ);
300 if (!fgets(line, MAXREADLINESZ, stdin)) {
305 p = line + strlen(line);
306 if (p != line && p[-1] == '\n') {
313 static char *fetchline(void)
315 if (readline_state) {
316 return fetchline_readline();
318 return fetchline_fgets();
322 static void prep_fetchline(void *opaque)
324 int *fetchable = opaque;
326 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
330 static void command_loop(void)
332 int i, done = 0, fetchable = 0, prompted = 0;
335 for (i = 0; !done && i < ncmdline; i++) {
336 done = qemuio_command(qemuio_blk, cmdline[i]);
345 printf("%s", get_prompt());
347 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
351 main_loop_wait(false);
361 done = qemuio_command(qemuio_blk, input);
367 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
370 static void add_user_command(char *optarg)
372 cmdline = g_renew(char *, cmdline, ++ncmdline);
373 cmdline[ncmdline-1] = optarg;
376 static void reenable_tty_echo(void)
378 qemu_set_tty_echo(STDIN_FILENO, true);
383 OPTION_IMAGE_OPTS = 257,
386 static QemuOptsList qemu_object_opts = {
388 .implied_opt_name = "qom-type",
389 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
396 static QemuOptsList file_opts = {
398 .implied_opt_name = "file",
399 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
401 /* no elements => accept any params */
402 { /* end of list */ }
406 int main(int argc, char **argv)
409 const char *sopt = "hVc:d:f:rsnmgkt:T:";
410 const struct option lopt[] = {
411 { "help", no_argument, NULL, 'h' },
412 { "version", no_argument, NULL, 'V' },
413 { "offset", required_argument, NULL, 'o' },
414 { "cmd", required_argument, NULL, 'c' },
415 { "format", required_argument, NULL, 'f' },
416 { "read-only", no_argument, NULL, 'r' },
417 { "snapshot", no_argument, NULL, 's' },
418 { "nocache", no_argument, NULL, 'n' },
419 { "misalign", no_argument, NULL, 'm' },
420 { "native-aio", no_argument, NULL, 'k' },
421 { "discard", required_argument, NULL, 'd' },
422 { "cache", required_argument, NULL, 't' },
423 { "trace", required_argument, NULL, 'T' },
424 { "object", required_argument, NULL, OPTION_OBJECT },
425 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
430 int flags = BDRV_O_UNMAP;
431 Error *local_error = NULL;
433 const char *format = NULL;
436 signal(SIGPIPE, SIG_IGN);
439 progname = basename(argv[0]);
440 qemu_init_exec_dir(argv[0]);
442 module_call_init(MODULE_INIT_QOM);
443 qemu_add_opts(&qemu_object_opts);
446 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
449 flags |= BDRV_O_SNAPSHOT;
452 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
455 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
456 error_report("Invalid discard option: %s", optarg);
464 add_user_command(optarg);
470 qemuio_misalign = true;
473 flags |= BDRV_O_NATIVE_AIO;
476 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
477 error_report("Invalid cache option: %s", optarg);
482 if (!trace_init_backends()) {
483 exit(1); /* error message will have been printed */
487 printf("%s version %s\n", progname, QEMU_VERSION);
492 case OPTION_OBJECT: {
494 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
500 case OPTION_IMAGE_OPTS:
509 if ((argc - optind) > 1) {
514 if (format && imageOpts) {
515 error_report("--image-opts and -f are mutually exclusive");
519 if (qemu_init_main_loop(&local_error)) {
520 error_report_err(local_error);
524 if (qemu_opts_foreach(&qemu_object_opts,
525 user_creatable_add_opts_foreach,
526 NULL, &local_error)) {
527 error_report_err(local_error);
531 /* initialize commands */
532 qemuio_add_command(&quit_cmd);
533 qemuio_add_command(&open_cmd);
534 qemuio_add_command(&close_cmd);
536 if (isatty(STDIN_FILENO)) {
537 readline_state = readline_init(readline_printf_func,
540 readline_completion_func);
541 qemu_set_tty_echo(STDIN_FILENO, false);
542 atexit(reenable_tty_echo);
545 /* open the device */
547 flags |= BDRV_O_RDWR;
550 if ((argc - optind) == 1) {
552 QemuOpts *qopts = NULL;
553 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
557 opts = qemu_opts_to_qdict(qopts, NULL);
558 openfile(NULL, flags, opts);
562 qdict_put(opts, "driver", qstring_from_str(format));
564 openfile(argv[optind], flags, opts);
570 * Make sure all outstanding requests complete before the program exits.
574 blk_unref(qemuio_blk);
575 g_free(readline_state);