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, bool writethrough, 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) && bdrv_key_required(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");
86 blk_set_enable_write_cache(qemuio_blk, !writethrough);
91 blk_unref(qemuio_blk);
96 static void open_help(void)
100 " opens a new file in the requested mode\n"
103 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
105 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
106 " -r, -- open file read-only\n"
107 " -s, -- use snapshot file\n"
108 " -n, -- disable host cache\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 = "[-Crsn] [-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)
141 bool writethrough = true;
146 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
149 flags |= BDRV_O_SNAPSHOT;
152 flags |= BDRV_O_NOCACHE;
153 writethrough = false;
160 printf("--image-opts and 'open -o' are mutually exclusive\n");
163 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
164 qemu_opts_reset(&empty_opts);
169 qemu_opts_reset(&empty_opts);
170 return qemuio_command_usage(&open_cmd);
175 flags |= BDRV_O_RDWR;
178 if (imageOpts && (optind == argc - 1)) {
179 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
180 qemu_opts_reset(&empty_opts);
186 qopts = qemu_opts_find(&empty_opts, NULL);
187 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
188 qemu_opts_reset(&empty_opts);
190 if (optind == argc - 1) {
191 return openfile(argv[optind], flags, writethrough, opts);
192 } else if (optind == argc) {
193 return openfile(NULL, flags, writethrough, opts);
196 return qemuio_command_usage(&open_cmd);
200 static int quit_f(BlockBackend *blk, int argc, char **argv)
205 static const cmdinfo_t quit_cmd = {
211 .flags = CMD_FLAG_GLOBAL,
212 .oneline = "exit the program",
215 static void usage(const char *name)
218 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
219 "QEMU Disk exerciser\n"
221 " --object OBJECTDEF define an object such as 'secret' for\n"
222 " passwords and/or encryption keys\n"
223 " -c, --cmd STRING execute command with its arguments\n"
224 " from the given string\n"
225 " -f, --format FMT specifies the block driver to use\n"
226 " -r, --read-only export read-only\n"
227 " -s, --snapshot use snapshot file\n"
228 " -n, --nocache disable host cache\n"
229 " -m, --misalign misalign allocations for O_DIRECT\n"
230 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
231 " -t, --cache=MODE use the given cache mode for the image\n"
232 " -T, --trace FILE enable trace events listed in the given file\n"
233 " -h, --help display this help and exit\n"
234 " -V, --version output version information and exit\n"
236 "See '%s -c help' for information on available commands."
241 static char *get_prompt(void)
243 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
246 snprintf(prompt, sizeof(prompt), "%s> ", progname);
252 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
253 const char *fmt, ...)
261 static void readline_flush_func(void *opaque)
266 static void readline_func(void *opaque, const char *str, void *readline_opaque)
268 char **line = readline_opaque;
269 *line = g_strdup(str);
272 static void completion_match(const char *cmd, void *opaque)
274 readline_add_completion(readline_state, cmd);
277 static void readline_completion_func(void *opaque, const char *str)
279 readline_set_completion_index(readline_state, strlen(str));
280 qemuio_complete_command(str, completion_match, NULL);
283 static char *fetchline_readline(void)
287 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
293 readline_handle_byte(readline_state, ch);
298 #define MAXREADLINESZ 1024
299 static char *fetchline_fgets(void)
301 char *p, *line = g_malloc(MAXREADLINESZ);
303 if (!fgets(line, MAXREADLINESZ, stdin)) {
308 p = line + strlen(line);
309 if (p != line && p[-1] == '\n') {
316 static char *fetchline(void)
318 if (readline_state) {
319 return fetchline_readline();
321 return fetchline_fgets();
325 static void prep_fetchline(void *opaque)
327 int *fetchable = opaque;
329 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
333 static void command_loop(void)
335 int i, done = 0, fetchable = 0, prompted = 0;
338 for (i = 0; !done && i < ncmdline; i++) {
339 done = qemuio_command(qemuio_blk, cmdline[i]);
348 printf("%s", get_prompt());
350 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
354 main_loop_wait(false);
364 done = qemuio_command(qemuio_blk, input);
370 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
373 static void add_user_command(char *optarg)
375 cmdline = g_renew(char *, cmdline, ++ncmdline);
376 cmdline[ncmdline-1] = optarg;
379 static void reenable_tty_echo(void)
381 qemu_set_tty_echo(STDIN_FILENO, true);
386 OPTION_IMAGE_OPTS = 257,
389 static QemuOptsList qemu_object_opts = {
391 .implied_opt_name = "qom-type",
392 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
399 static QemuOptsList file_opts = {
401 .implied_opt_name = "file",
402 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
404 /* no elements => accept any params */
405 { /* end of list */ }
409 int main(int argc, char **argv)
412 const char *sopt = "hVc:d:f:rsnmgkt:T:";
413 const struct option lopt[] = {
414 { "help", no_argument, NULL, 'h' },
415 { "version", no_argument, NULL, 'V' },
416 { "offset", required_argument, NULL, 'o' },
417 { "cmd", required_argument, NULL, 'c' },
418 { "format", required_argument, NULL, 'f' },
419 { "read-only", no_argument, NULL, 'r' },
420 { "snapshot", no_argument, NULL, 's' },
421 { "nocache", no_argument, NULL, 'n' },
422 { "misalign", no_argument, NULL, 'm' },
423 { "native-aio", no_argument, NULL, 'k' },
424 { "discard", required_argument, NULL, 'd' },
425 { "cache", required_argument, NULL, 't' },
426 { "trace", required_argument, NULL, 'T' },
427 { "object", required_argument, NULL, OPTION_OBJECT },
428 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
433 int flags = BDRV_O_UNMAP;
434 bool writethrough = true;
435 Error *local_error = NULL;
437 const char *format = NULL;
440 signal(SIGPIPE, SIG_IGN);
443 progname = basename(argv[0]);
444 qemu_init_exec_dir(argv[0]);
446 module_call_init(MODULE_INIT_QOM);
447 qemu_add_opts(&qemu_object_opts);
450 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
453 flags |= BDRV_O_SNAPSHOT;
456 flags |= BDRV_O_NOCACHE;
457 writethrough = false;
460 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
461 error_report("Invalid discard option: %s", optarg);
469 add_user_command(optarg);
475 qemuio_misalign = true;
478 flags |= BDRV_O_NATIVE_AIO;
481 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
482 error_report("Invalid cache option: %s", optarg);
487 if (!trace_init_backends()) {
488 exit(1); /* error message will have been printed */
492 printf("%s version %s\n", progname, QEMU_VERSION);
497 case OPTION_OBJECT: {
499 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
505 case OPTION_IMAGE_OPTS:
514 if ((argc - optind) > 1) {
519 if (format && imageOpts) {
520 error_report("--image-opts and -f are mutually exclusive");
524 if (qemu_init_main_loop(&local_error)) {
525 error_report_err(local_error);
529 if (qemu_opts_foreach(&qemu_object_opts,
530 user_creatable_add_opts_foreach,
531 NULL, &local_error)) {
532 error_report_err(local_error);
536 /* initialize commands */
537 qemuio_add_command(&quit_cmd);
538 qemuio_add_command(&open_cmd);
539 qemuio_add_command(&close_cmd);
541 if (isatty(STDIN_FILENO)) {
542 readline_state = readline_init(readline_printf_func,
545 readline_completion_func);
546 qemu_set_tty_echo(STDIN_FILENO, false);
547 atexit(reenable_tty_echo);
550 /* open the device */
552 flags |= BDRV_O_RDWR;
555 if ((argc - optind) == 1) {
557 QemuOpts *qopts = NULL;
558 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
562 opts = qemu_opts_to_qdict(qopts, NULL);
563 openfile(NULL, flags, writethrough, opts);
567 qdict_put(opts, "driver", qstring_from_str(format));
569 openfile(argv[optind], flags, writethrough, opts);
575 * Make sure all outstanding requests complete before the program exits.
579 blk_unref(qemuio_blk);
580 g_free(readline_state);