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 <sys/types.h>
18 #include "qemu/error-report.h"
19 #include "qemu/main-loop.h"
20 #include "qemu/option.h"
21 #include "qemu/config-file.h"
22 #include "qemu/readline.h"
23 #include "qapi/qmp/qstring.h"
24 #include "sysemu/block-backend.h"
25 #include "block/block_int.h"
26 #include "trace/control.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;
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 fprintf(stderr, "file open already, try 'help close'\n");
65 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
67 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
68 name ? " device " : "", name ?: "",
69 error_get_pretty(local_err));
70 error_free(local_err);
74 bs = blk_bs(qemuio_blk);
75 if (bdrv_is_encrypted(bs)) {
77 printf("Disk image '%s' is encrypted.\n", name);
78 if (qemu_read_password(password, sizeof(password)) < 0) {
79 error_report("No password given");
82 if (bdrv_set_key(bs, password) < 0) {
83 error_report("invalid password");
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)
146 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
149 flags |= BDRV_O_SNAPSHOT;
152 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
158 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
159 printf("could not parse option list -- %s\n", optarg);
160 qemu_opts_reset(&empty_opts);
165 qemu_opts_reset(&empty_opts);
166 return qemuio_command_usage(&open_cmd);
171 flags |= BDRV_O_RDWR;
174 qopts = qemu_opts_find(&empty_opts, NULL);
175 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
176 qemu_opts_reset(&empty_opts);
178 if (optind == argc - 1) {
179 return openfile(argv[optind], flags, opts);
180 } else if (optind == argc) {
181 return openfile(NULL, flags, opts);
184 return qemuio_command_usage(&open_cmd);
188 static int quit_f(BlockBackend *blk, int argc, char **argv)
193 static const cmdinfo_t quit_cmd = {
199 .flags = CMD_FLAG_GLOBAL,
200 .oneline = "exit the program",
203 static void usage(const char *name)
206 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
207 "QEMU Disk exerciser\n"
209 " -c, --cmd STRING execute command with its arguments\n"
210 " from the given string\n"
211 " -f, --format FMT specifies the block driver to use\n"
212 " -r, --read-only export read-only\n"
213 " -s, --snapshot use snapshot file\n"
214 " -n, --nocache disable host cache\n"
215 " -m, --misalign misalign allocations for O_DIRECT\n"
216 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
217 " -t, --cache=MODE use the given cache mode for the image\n"
218 " -T, --trace FILE enable trace events listed in the given file\n"
219 " -h, --help display this help and exit\n"
220 " -V, --version output version information and exit\n"
222 "See '%s -c help' for information on available commands."
227 static char *get_prompt(void)
229 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
232 snprintf(prompt, sizeof(prompt), "%s> ", progname);
238 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
239 const char *fmt, ...)
247 static void readline_flush_func(void *opaque)
252 static void readline_func(void *opaque, const char *str, void *readline_opaque)
254 char **line = readline_opaque;
255 *line = g_strdup(str);
258 static void completion_match(const char *cmd, void *opaque)
260 readline_add_completion(readline_state, cmd);
263 static void readline_completion_func(void *opaque, const char *str)
265 readline_set_completion_index(readline_state, strlen(str));
266 qemuio_complete_command(str, completion_match, NULL);
269 static char *fetchline_readline(void)
273 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
279 readline_handle_byte(readline_state, ch);
284 #define MAXREADLINESZ 1024
285 static char *fetchline_fgets(void)
287 char *p, *line = g_malloc(MAXREADLINESZ);
289 if (!fgets(line, MAXREADLINESZ, stdin)) {
294 p = line + strlen(line);
295 if (p != line && p[-1] == '\n') {
302 static char *fetchline(void)
304 if (readline_state) {
305 return fetchline_readline();
307 return fetchline_fgets();
311 static void prep_fetchline(void *opaque)
313 int *fetchable = opaque;
315 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
319 static void command_loop(void)
321 int i, done = 0, fetchable = 0, prompted = 0;
324 for (i = 0; !done && i < ncmdline; i++) {
325 done = qemuio_command(qemuio_blk, cmdline[i]);
334 printf("%s", get_prompt());
336 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
340 main_loop_wait(false);
350 done = qemuio_command(qemuio_blk, input);
356 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
359 static void add_user_command(char *optarg)
361 cmdline = g_renew(char *, cmdline, ++ncmdline);
362 cmdline[ncmdline-1] = optarg;
365 static void reenable_tty_echo(void)
367 qemu_set_tty_echo(STDIN_FILENO, true);
370 int main(int argc, char **argv)
373 const char *sopt = "hVc:d:f:rsnmgkt:T:";
374 const struct option lopt[] = {
375 { "help", 0, NULL, 'h' },
376 { "version", 0, NULL, 'V' },
377 { "offset", 1, NULL, 'o' },
378 { "cmd", 1, NULL, 'c' },
379 { "format", 1, NULL, 'f' },
380 { "read-only", 0, NULL, 'r' },
381 { "snapshot", 0, NULL, 's' },
382 { "nocache", 0, NULL, 'n' },
383 { "misalign", 0, NULL, 'm' },
384 { "native-aio", 0, NULL, 'k' },
385 { "discard", 1, NULL, 'd' },
386 { "cache", 1, NULL, 't' },
387 { "trace", 1, NULL, 'T' },
392 int flags = BDRV_O_UNMAP;
393 Error *local_error = NULL;
397 signal(SIGPIPE, SIG_IGN);
400 progname = basename(argv[0]);
401 qemu_init_exec_dir(argv[0]);
405 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
408 flags |= BDRV_O_SNAPSHOT;
411 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
414 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
415 error_report("Invalid discard option: %s", optarg);
423 qdict_put(opts, "driver", qstring_from_str(optarg));
426 add_user_command(optarg);
432 qemuio_misalign = true;
435 flags |= BDRV_O_NATIVE_AIO;
438 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
439 error_report("Invalid cache option: %s", optarg);
444 if (!trace_init_backends(optarg, NULL)) {
445 exit(1); /* error message will have been printed */
449 printf("%s version %s\n", progname, QEMU_VERSION);
460 if ((argc - optind) > 1) {
465 if (qemu_init_main_loop(&local_error)) {
466 error_report_err(local_error);
470 /* initialize commands */
471 qemuio_add_command(&quit_cmd);
472 qemuio_add_command(&open_cmd);
473 qemuio_add_command(&close_cmd);
475 if (isatty(STDIN_FILENO)) {
476 readline_state = readline_init(readline_printf_func,
479 readline_completion_func);
480 qemu_set_tty_echo(STDIN_FILENO, false);
481 atexit(reenable_tty_echo);
484 /* open the device */
486 flags |= BDRV_O_RDWR;
489 if ((argc - optind) == 1) {
490 openfile(argv[optind], flags, opts);
495 * Make sure all outstanding requests complete before the program exits.
499 blk_unref(qemuio_blk);
500 g_free(readline_state);