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/main-loop.h"
19 #include "qemu/option.h"
20 #include "qemu/config-file.h"
21 #include "block/block_int.h"
22 #include "trace/control.h"
24 #define CMD_NOFILE_OK 0x01
28 BlockDriverState *qemuio_bs;
29 extern int qemuio_misalign;
31 /* qemu-io commands passed using -c */
33 static char **cmdline;
35 static int close_f(BlockDriverState *bs, int argc, char **argv)
42 static const cmdinfo_t close_cmd = {
46 .oneline = "close the current open file",
49 static int openfile(char *name, int flags, int growable, QDict *opts)
51 Error *local_err = NULL;
54 fprintf(stderr, "file open already, try 'help close'\n");
59 if (bdrv_file_open(&qemuio_bs, name, opts, flags, &local_err)) {
60 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
61 error_get_pretty(local_err));
62 error_free(local_err);
66 qemuio_bs = bdrv_new("hda");
68 if (bdrv_open(qemuio_bs, name, opts, flags, NULL, &local_err) < 0) {
69 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
70 error_get_pretty(local_err));
71 error_free(local_err);
72 bdrv_unref(qemuio_bs);
81 static void open_help(void)
85 " opens a new file in the requested mode\n"
88 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
90 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
91 " -r, -- open file read-only\n"
92 " -s, -- use snapshot file\n"
93 " -n, -- disable host cache\n"
94 " -g, -- allow file to grow (only applies to protocols)\n"
95 " -o, -- options to be given to the block driver"
99 static int open_f(BlockDriverState *bs, int argc, char **argv);
101 static const cmdinfo_t open_cmd = {
107 .flags = CMD_NOFILE_OK,
108 .args = "[-Crsn] [-o options] [path]",
109 .oneline = "open the file specified by path",
113 static QemuOptsList empty_opts = {
115 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
117 /* no elements => accept any params */
118 { /* end of list */ }
122 static int open_f(BlockDriverState *bs, int argc, char **argv)
131 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
134 flags |= BDRV_O_SNAPSHOT;
137 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
146 qopts = qemu_opts_parse(&empty_opts, optarg, 0);
148 printf("could not parse option list -- %s\n", optarg);
151 opts = qemu_opts_to_qdict(qopts, opts);
152 qemu_opts_del(qopts);
155 return qemuio_command_usage(&open_cmd);
160 flags |= BDRV_O_RDWR;
163 if (optind != argc - 1) {
164 return qemuio_command_usage(&open_cmd);
167 return openfile(argv[optind], flags, growable, opts);
170 static int quit_f(BlockDriverState *bs, int argc, char **argv)
175 static const cmdinfo_t quit_cmd = {
181 .flags = CMD_FLAG_GLOBAL,
182 .oneline = "exit the program",
185 static void usage(const char *name)
188 "Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
189 "QEMU Disk exerciser\n"
191 " -c, --cmd command to execute\n"
192 " -r, --read-only export read-only\n"
193 " -s, --snapshot use snapshot file\n"
194 " -n, --nocache disable host cache\n"
195 " -g, --growable allow file to grow (only applies to protocols)\n"
196 " -m, --misalign misalign allocations for O_DIRECT\n"
197 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
198 " -t, --cache=MODE use the given cache mode for the image\n"
199 " -T, --trace FILE enable trace events listed in the given file\n"
200 " -h, --help display this help and exit\n"
201 " -V, --version output version information and exit\n"
207 #if defined(ENABLE_READLINE)
208 # include <readline/history.h>
209 # include <readline/readline.h>
210 #elif defined(ENABLE_EDITLINE)
211 # include <histedit.h>
214 static char *get_prompt(void)
216 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
219 snprintf(prompt, sizeof(prompt), "%s> ", progname);
225 #if defined(ENABLE_READLINE)
226 static char *fetchline(void)
228 char *line = readline(get_prompt());
234 #elif defined(ENABLE_EDITLINE)
235 static char *el_get_prompt(EditLine *e)
240 static char *fetchline(void)
243 static History *hist;
249 hist = history_init();
250 history(hist, &hevent, H_SETSIZE, 100);
251 el = el_init(progname, stdin, stdout, stderr);
253 el_set(el, EL_SIGNAL, 1);
254 el_set(el, EL_PROMPT, el_get_prompt);
255 el_set(el, EL_HIST, history, (const char *)hist);
257 line = strdup(el_gets(el, &count));
260 line[count-1] = '\0';
263 history(hist, &hevent, H_ENTER, line);
269 # define MAXREADLINESZ 1024
270 static char *fetchline(void)
272 char *p, *line = g_malloc(MAXREADLINESZ);
274 if (!fgets(line, MAXREADLINESZ, stdin)) {
279 p = line + strlen(line);
280 if (p != line && p[-1] == '\n') {
288 static void prep_fetchline(void *opaque)
290 int *fetchable = opaque;
292 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
296 static void command_loop(void)
298 int i, done = 0, fetchable = 0, prompted = 0;
301 for (i = 0; !done && i < ncmdline; i++) {
302 done = qemuio_command(qemuio_bs, cmdline[i]);
311 printf("%s", get_prompt());
313 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
317 main_loop_wait(false);
327 done = qemuio_command(qemuio_bs, input);
333 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
336 static void add_user_command(char *optarg)
338 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
339 cmdline[ncmdline-1] = optarg;
342 int main(int argc, char **argv)
346 const char *sopt = "hVc:d:rsnmgkt:T:";
347 const struct option lopt[] = {
348 { "help", 0, NULL, 'h' },
349 { "version", 0, NULL, 'V' },
350 { "offset", 1, NULL, 'o' },
351 { "cmd", 1, NULL, 'c' },
352 { "read-only", 0, NULL, 'r' },
353 { "snapshot", 0, NULL, 's' },
354 { "nocache", 0, NULL, 'n' },
355 { "misalign", 0, NULL, 'm' },
356 { "growable", 0, NULL, 'g' },
357 { "native-aio", 0, NULL, 'k' },
358 { "discard", 1, NULL, 'd' },
359 { "cache", 1, NULL, 't' },
360 { "trace", 1, NULL, 'T' },
365 int flags = BDRV_O_UNMAP;
368 signal(SIGPIPE, SIG_IGN);
371 progname = basename(argv[0]);
373 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
376 flags |= BDRV_O_SNAPSHOT;
379 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
382 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
383 error_report("Invalid discard option: %s", optarg);
388 add_user_command(optarg);
400 flags |= BDRV_O_NATIVE_AIO;
403 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
404 error_report("Invalid cache option: %s", optarg);
409 if (!trace_backend_init(optarg, NULL)) {
410 exit(1); /* error message will have been printed */
414 printf("%s version %s\n", progname, QEMU_VERSION);
425 if ((argc - optind) > 1) {
430 qemu_init_main_loop();
433 /* initialize commands */
434 qemuio_add_command(&quit_cmd);
435 qemuio_add_command(&open_cmd);
436 qemuio_add_command(&close_cmd);
438 /* open the device */
440 flags |= BDRV_O_RDWR;
443 if ((argc - optind) == 1) {
444 openfile(argv[optind], flags, growable, NULL);
449 * Make sure all outstanding requests complete before the program exits.
454 bdrv_unref(qemuio_bs);