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"
26 #include "crypto/init.h"
28 #define CMD_NOFILE_OK 0x01
30 static char *progname;
32 static BlockBackend *qemuio_blk;
34 /* qemu-io commands passed using -c */
36 static char **cmdline;
37 static bool imageOpts;
39 static ReadLineState *readline_state;
41 static int close_f(BlockBackend *blk, int argc, char **argv)
43 blk_unref(qemuio_blk);
48 static const cmdinfo_t close_cmd = {
52 .oneline = "close the current open file",
55 static int openfile(char *name, int flags, bool writethrough, QDict *opts)
57 Error *local_err = NULL;
61 error_report("file open already, try 'help close'");
66 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
68 error_reportf_err(local_err, "can't open%s%s: ",
69 name ? " device " : "", name ?: "");
73 bs = blk_bs(qemuio_blk);
74 if (bdrv_is_encrypted(bs) && bdrv_key_required(bs)) {
76 printf("Disk image '%s' is encrypted.\n", name);
77 if (qemu_read_password(password, sizeof(password)) < 0) {
78 error_report("No password given");
81 if (bdrv_set_key(bs, password) < 0) {
82 error_report("invalid password");
87 blk_set_enable_write_cache(qemuio_blk, !writethrough);
92 blk_unref(qemuio_blk);
97 static void open_help(void)
101 " opens a new file in the requested mode\n"
104 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
106 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
107 " -r, -- open file read-only\n"
108 " -s, -- use snapshot file\n"
109 " -n, -- disable host cache\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 = "[-Crsn] [-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)
142 bool writethrough = true;
147 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
150 flags |= BDRV_O_SNAPSHOT;
153 flags |= BDRV_O_NOCACHE;
154 writethrough = false;
161 printf("--image-opts and 'open -o' are mutually exclusive\n");
164 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
165 qemu_opts_reset(&empty_opts);
170 qemu_opts_reset(&empty_opts);
171 return qemuio_command_usage(&open_cmd);
176 flags |= BDRV_O_RDWR;
179 if (imageOpts && (optind == argc - 1)) {
180 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
181 qemu_opts_reset(&empty_opts);
187 qopts = qemu_opts_find(&empty_opts, NULL);
188 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
189 qemu_opts_reset(&empty_opts);
191 if (optind == argc - 1) {
192 return openfile(argv[optind], flags, writethrough, opts);
193 } else if (optind == argc) {
194 return openfile(NULL, flags, writethrough, opts);
197 return qemuio_command_usage(&open_cmd);
201 static int quit_f(BlockBackend *blk, int argc, char **argv)
206 static const cmdinfo_t quit_cmd = {
212 .flags = CMD_FLAG_GLOBAL,
213 .oneline = "exit the program",
216 static void usage(const char *name)
219 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
220 "QEMU Disk exerciser\n"
222 " --object OBJECTDEF define an object such as 'secret' for\n"
223 " passwords and/or encryption keys\n"
224 " -c, --cmd STRING execute command with its arguments\n"
225 " from the given string\n"
226 " -f, --format FMT specifies the block driver to use\n"
227 " -r, --read-only export read-only\n"
228 " -s, --snapshot use snapshot file\n"
229 " -n, --nocache disable host cache\n"
230 " -m, --misalign misalign allocations for O_DIRECT\n"
231 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
232 " -t, --cache=MODE use the given cache mode for the image\n"
233 " -T, --trace FILE enable trace events listed in the given file\n"
234 " -h, --help display this help and exit\n"
235 " -V, --version output version information and exit\n"
237 "See '%s -c help' for information on available commands."
242 static char *get_prompt(void)
244 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
247 snprintf(prompt, sizeof(prompt), "%s> ", progname);
253 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
254 const char *fmt, ...)
262 static void readline_flush_func(void *opaque)
267 static void readline_func(void *opaque, const char *str, void *readline_opaque)
269 char **line = readline_opaque;
270 *line = g_strdup(str);
273 static void completion_match(const char *cmd, void *opaque)
275 readline_add_completion(readline_state, cmd);
278 static void readline_completion_func(void *opaque, const char *str)
280 readline_set_completion_index(readline_state, strlen(str));
281 qemuio_complete_command(str, completion_match, NULL);
284 static char *fetchline_readline(void)
288 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
294 readline_handle_byte(readline_state, ch);
299 #define MAXREADLINESZ 1024
300 static char *fetchline_fgets(void)
302 char *p, *line = g_malloc(MAXREADLINESZ);
304 if (!fgets(line, MAXREADLINESZ, stdin)) {
309 p = line + strlen(line);
310 if (p != line && p[-1] == '\n') {
317 static char *fetchline(void)
319 if (readline_state) {
320 return fetchline_readline();
322 return fetchline_fgets();
326 static void prep_fetchline(void *opaque)
328 int *fetchable = opaque;
330 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
334 static void command_loop(void)
336 int i, done = 0, fetchable = 0, prompted = 0;
339 for (i = 0; !done && i < ncmdline; i++) {
340 done = qemuio_command(qemuio_blk, cmdline[i]);
349 printf("%s", get_prompt());
351 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
355 main_loop_wait(false);
365 done = qemuio_command(qemuio_blk, input);
371 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
374 static void add_user_command(char *optarg)
376 cmdline = g_renew(char *, cmdline, ++ncmdline);
377 cmdline[ncmdline-1] = optarg;
380 static void reenable_tty_echo(void)
382 qemu_set_tty_echo(STDIN_FILENO, true);
387 OPTION_IMAGE_OPTS = 257,
390 static QemuOptsList qemu_object_opts = {
392 .implied_opt_name = "qom-type",
393 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
400 static QemuOptsList file_opts = {
402 .implied_opt_name = "file",
403 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
405 /* no elements => accept any params */
406 { /* end of list */ }
410 int main(int argc, char **argv)
413 const char *sopt = "hVc:d:f:rsnmgkt:T:";
414 const struct option lopt[] = {
415 { "help", no_argument, NULL, 'h' },
416 { "version", no_argument, NULL, 'V' },
417 { "offset", required_argument, NULL, 'o' },
418 { "cmd", required_argument, NULL, 'c' },
419 { "format", required_argument, NULL, 'f' },
420 { "read-only", no_argument, NULL, 'r' },
421 { "snapshot", no_argument, NULL, 's' },
422 { "nocache", no_argument, NULL, 'n' },
423 { "misalign", no_argument, NULL, 'm' },
424 { "native-aio", no_argument, NULL, 'k' },
425 { "discard", required_argument, NULL, 'd' },
426 { "cache", required_argument, NULL, 't' },
427 { "trace", required_argument, NULL, 'T' },
428 { "object", required_argument, NULL, OPTION_OBJECT },
429 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
434 int flags = BDRV_O_UNMAP;
435 bool writethrough = true;
436 Error *local_error = NULL;
438 const char *format = NULL;
441 signal(SIGPIPE, SIG_IGN);
444 progname = basename(argv[0]);
445 qemu_init_exec_dir(argv[0]);
447 if (qcrypto_init(&local_error) < 0) {
448 error_reportf_err(local_error, "cannot initialize crypto: ");
452 module_call_init(MODULE_INIT_QOM);
453 qemu_add_opts(&qemu_object_opts);
456 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
459 flags |= BDRV_O_SNAPSHOT;
462 flags |= BDRV_O_NOCACHE;
463 writethrough = false;
466 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
467 error_report("Invalid discard option: %s", optarg);
475 add_user_command(optarg);
481 qemuio_misalign = true;
484 flags |= BDRV_O_NATIVE_AIO;
487 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
488 error_report("Invalid cache option: %s", optarg);
493 if (!trace_init_backends()) {
494 exit(1); /* error message will have been printed */
498 printf("%s version %s\n", progname, QEMU_VERSION);
503 case OPTION_OBJECT: {
505 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
511 case OPTION_IMAGE_OPTS:
520 if ((argc - optind) > 1) {
525 if (format && imageOpts) {
526 error_report("--image-opts and -f are mutually exclusive");
530 if (qemu_init_main_loop(&local_error)) {
531 error_report_err(local_error);
535 if (qemu_opts_foreach(&qemu_object_opts,
536 user_creatable_add_opts_foreach,
537 NULL, &local_error)) {
538 error_report_err(local_error);
542 /* initialize commands */
543 qemuio_add_command(&quit_cmd);
544 qemuio_add_command(&open_cmd);
545 qemuio_add_command(&close_cmd);
547 if (isatty(STDIN_FILENO)) {
548 readline_state = readline_init(readline_printf_func,
551 readline_completion_func);
552 qemu_set_tty_echo(STDIN_FILENO, false);
553 atexit(reenable_tty_echo);
556 /* open the device */
558 flags |= BDRV_O_RDWR;
561 if ((argc - optind) == 1) {
563 QemuOpts *qopts = NULL;
564 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
568 opts = qemu_opts_to_qdict(qopts, NULL);
569 openfile(NULL, flags, writethrough, opts);
573 qdict_put(opts, "driver", qstring_from_str(format));
575 openfile(argv[optind], flags, writethrough, opts);
581 * Make sure all outstanding requests complete before the program exits.
585 blk_unref(qemuio_blk);
586 g_free(readline_state);