Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blobce559e40d5f9a1a31c72c68f0f69bdfa88556f3a
1 /*
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.
9 */
10 #include <sys/time.h>
11 #include <sys/types.h>
12 #include <stdarg.h>
13 #include <stdio.h>
14 #include <getopt.h>
15 #include <libgen.h>
17 #include "qemu-io.h"
18 #include "qemu/main-loop.h"
19 #include "qemu/option.h"
20 #include "qemu/config-file.h"
21 #include "qemu/readline.h"
22 #include "sysemu/block-backend.h"
23 #include "block/block_int.h"
24 #include "trace/control.h"
26 #define CMD_NOFILE_OK 0x01
28 static char *progname;
30 static BlockBackend *qemuio_blk;
31 static BlockDriverState *qemuio_bs;
33 /* qemu-io commands passed using -c */
34 static int ncmdline;
35 static char **cmdline;
37 static ReadLineState *readline_state;
39 static int close_f(BlockDriverState *bs, int argc, char **argv)
41 blk_unref(qemuio_blk);
42 qemuio_bs = NULL;
43 qemuio_blk = NULL;
44 return 0;
47 static const cmdinfo_t close_cmd = {
48 .name = "close",
49 .altname = "c",
50 .cfunc = close_f,
51 .oneline = "close the current open file",
54 static int openfile(char *name, BlockDriver *drv, int flags, int growable,
55 QDict *opts)
57 Error *local_err = NULL;
59 if (qemuio_bs) {
60 fprintf(stderr, "file open already, try 'help close'\n");
61 QDECREF(opts);
62 return 1;
65 qemuio_blk = blk_new_with_bs("hda", &error_abort);
66 qemuio_bs = blk_bs(qemuio_blk);
68 if (growable) {
69 flags |= BDRV_O_PROTOCOL;
72 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, drv, &local_err) < 0) {
73 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
74 name ? " device " : "", name ?: "",
75 error_get_pretty(local_err));
76 error_free(local_err);
77 blk_unref(qemuio_blk);
78 qemuio_bs = NULL;
79 qemuio_blk = NULL;
80 return 1;
83 return 0;
86 static void open_help(void)
88 printf(
89 "\n"
90 " opens a new file in the requested mode\n"
91 "\n"
92 " Example:\n"
93 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
94 "\n"
95 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
96 " -r, -- open file read-only\n"
97 " -s, -- use snapshot file\n"
98 " -n, -- disable host cache\n"
99 " -g, -- allow file to grow (only applies to protocols)\n"
100 " -o, -- options to be given to the block driver"
101 "\n");
104 static int open_f(BlockDriverState *bs, int argc, char **argv);
106 static const cmdinfo_t open_cmd = {
107 .name = "open",
108 .altname = "o",
109 .cfunc = open_f,
110 .argmin = 1,
111 .argmax = -1,
112 .flags = CMD_NOFILE_OK,
113 .args = "[-Crsn] [-o options] [path]",
114 .oneline = "open the file specified by path",
115 .help = open_help,
118 static QemuOptsList empty_opts = {
119 .name = "drive",
120 .merge_lists = true,
121 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
122 .desc = {
123 /* no elements => accept any params */
124 { /* end of list */ }
128 static int open_f(BlockDriverState *bs, int argc, char **argv)
130 int flags = 0;
131 int readonly = 0;
132 int growable = 0;
133 int c;
134 QemuOpts *qopts;
135 QDict *opts;
137 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
138 switch (c) {
139 case 's':
140 flags |= BDRV_O_SNAPSHOT;
141 break;
142 case 'n':
143 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
144 break;
145 case 'r':
146 readonly = 1;
147 break;
148 case 'g':
149 growable = 1;
150 break;
151 case 'o':
152 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
153 printf("could not parse option list -- %s\n", optarg);
154 qemu_opts_reset(&empty_opts);
155 return 0;
157 break;
158 default:
159 qemu_opts_reset(&empty_opts);
160 return qemuio_command_usage(&open_cmd);
164 if (!readonly) {
165 flags |= BDRV_O_RDWR;
168 qopts = qemu_opts_find(&empty_opts, NULL);
169 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
170 qemu_opts_reset(&empty_opts);
172 if (optind == argc - 1) {
173 return openfile(argv[optind], NULL, flags, growable, opts);
174 } else if (optind == argc) {
175 return openfile(NULL, NULL, flags, growable, opts);
176 } else {
177 QDECREF(opts);
178 return qemuio_command_usage(&open_cmd);
182 static int quit_f(BlockDriverState *bs, int argc, char **argv)
184 return 1;
187 #if defined(CONFIG_FVD)
188 # include "qemu-io-sim.c"
189 #endif
191 static const cmdinfo_t quit_cmd = {
192 .name = "quit",
193 .altname = "q",
194 .cfunc = quit_f,
195 .argmin = -1,
196 .argmax = -1,
197 .flags = CMD_FLAG_GLOBAL,
198 .oneline = "exit the program",
201 static void usage(const char *name)
203 printf(
204 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
205 "QEMU Disk exerciser\n"
206 "\n"
207 " -c, --cmd STRING execute command with its arguments\n"
208 " from the given string\n"
209 " -f, --format FMT specifies the block driver to use\n"
210 " -r, --read-only export read-only\n"
211 " -s, --snapshot use snapshot file\n"
212 " -n, --nocache disable host cache\n"
213 " -g, --growable allow file to grow (only applies to protocols)\n"
214 " -m, --misalign misalign allocations for O_DIRECT\n"
215 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
216 " -t, --cache=MODE use the given cache mode for the image\n"
217 " -T, --trace FILE enable trace events listed in the given file\n"
218 " -h, --help display this help and exit\n"
219 " -V, --version output version information and exit\n"
220 "\n"
221 "See '%s -c help' for information on available commands."
222 "\n",
223 name, name);
226 static char *get_prompt(void)
228 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
230 if (!prompt[0]) {
231 snprintf(prompt, sizeof(prompt), "%s> ", progname);
234 return prompt;
237 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
238 const char *fmt, ...)
240 va_list ap;
241 va_start(ap, fmt);
242 vprintf(fmt, ap);
243 va_end(ap);
246 static void readline_flush_func(void *opaque)
248 fflush(stdout);
251 static void readline_func(void *opaque, const char *str, void *readline_opaque)
253 char **line = readline_opaque;
254 *line = g_strdup(str);
257 static void completion_match(const char *cmd, void *opaque)
259 readline_add_completion(readline_state, cmd);
262 static void readline_completion_func(void *opaque, const char *str)
264 readline_set_completion_index(readline_state, strlen(str));
265 qemuio_complete_command(str, completion_match, NULL);
268 static char *fetchline_readline(void)
270 char *line = NULL;
272 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
273 while (!line) {
274 int ch = getchar();
275 if (ch == EOF) {
276 break;
278 readline_handle_byte(readline_state, ch);
280 return line;
283 #define MAXREADLINESZ 1024
284 static char *fetchline_fgets(void)
286 char *p, *line = g_malloc(MAXREADLINESZ);
288 if (!fgets(line, MAXREADLINESZ, stdin)) {
289 g_free(line);
290 return NULL;
293 p = line + strlen(line);
294 if (p != line && p[-1] == '\n') {
295 p[-1] = '\0';
298 return line;
301 static char *fetchline(void)
303 if (readline_state) {
304 return fetchline_readline();
305 } else {
306 return fetchline_fgets();
310 static void prep_fetchline(void *opaque)
312 int *fetchable = opaque;
314 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
315 *fetchable= 1;
318 static void command_loop(void)
320 int i, done = 0, fetchable = 0, prompted = 0;
321 char *input;
323 for (i = 0; !done && i < ncmdline; i++) {
324 done = qemuio_command(qemuio_bs, cmdline[i]);
326 if (cmdline) {
327 g_free(cmdline);
328 return;
331 while (!done) {
332 if (!prompted) {
333 printf("%s", get_prompt());
334 fflush(stdout);
335 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
336 prompted = 1;
339 main_loop_wait(false);
341 if (!fetchable) {
342 continue;
345 input = fetchline();
346 if (input == NULL) {
347 break;
349 done = qemuio_command(qemuio_bs, input);
350 g_free(input);
352 prompted = 0;
353 fetchable = 0;
355 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
358 static void add_user_command(char *optarg)
360 cmdline = g_renew(char *, cmdline, ++ncmdline);
361 cmdline[ncmdline-1] = optarg;
364 static void reenable_tty_echo(void)
366 qemu_set_tty_echo(STDIN_FILENO, true);
369 int main(int argc, char **argv)
371 int readonly = 0;
372 int growable = 0;
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 { "growable", 0, NULL, 'g' },
385 { "native-aio", 0, NULL, 'k' },
386 { "discard", 1, NULL, 'd' },
387 { "cache", 1, NULL, 't' },
388 { "trace", 1, NULL, 'T' },
389 { NULL, 0, NULL, 0 }
391 int c;
392 int opt_index = 0;
393 int flags = BDRV_O_UNMAP;
394 BlockDriver *drv = NULL;
395 Error *local_error = NULL;
397 #ifdef CONFIG_POSIX
398 signal(SIGPIPE, SIG_IGN);
399 #endif
401 progname = basename(argv[0]);
402 qemu_init_exec_dir(argv[0]);
404 bdrv_init();
406 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
407 switch (c) {
408 case 's':
409 flags |= BDRV_O_SNAPSHOT;
410 break;
411 case 'n':
412 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
413 break;
414 case 'd':
415 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
416 error_report("Invalid discard option: %s", optarg);
417 exit(1);
419 break;
420 case 'f':
421 drv = bdrv_find_format(optarg);
422 if (!drv) {
423 error_report("Invalid format '%s'", optarg);
424 exit(EXIT_FAILURE);
426 break;
427 case 'c':
428 add_user_command(optarg);
429 break;
430 case 'r':
431 readonly = 1;
432 break;
433 case 'm':
434 qemuio_misalign = true;
435 break;
436 case 'g':
437 growable = 1;
438 break;
439 case 'k':
440 flags |= BDRV_O_NATIVE_AIO;
441 break;
442 case 't':
443 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
444 error_report("Invalid cache option: %s", optarg);
445 exit(1);
447 break;
448 case 'T':
449 if (!trace_init_backends(optarg, NULL)) {
450 exit(1); /* error message will have been printed */
452 break;
453 case 'V':
454 printf("%s version %s\n", progname, QEMU_VERSION);
455 exit(0);
456 case 'h':
457 usage(progname);
458 exit(0);
459 default:
460 usage(progname);
461 exit(1);
465 if ((argc - optind) > 1) {
466 usage(progname);
467 exit(1);
470 if (qemu_init_main_loop(&local_error)) {
471 error_report("%s", error_get_pretty(local_error));
472 error_free(local_error);
473 exit(1);
476 /* initialize commands */
477 qemuio_add_command(&quit_cmd);
478 qemuio_add_command(&open_cmd);
479 qemuio_add_command(&close_cmd);
481 if (isatty(STDIN_FILENO)) {
482 readline_state = readline_init(readline_printf_func,
483 readline_flush_func,
484 NULL,
485 readline_completion_func);
486 qemu_set_tty_echo(STDIN_FILENO, false);
487 atexit(reenable_tty_echo);
490 /* open the device */
491 if (!readonly) {
492 flags |= BDRV_O_RDWR;
495 if ((argc - optind) == 1) {
496 openfile(argv[optind], drv, flags, growable, NULL);
498 command_loop();
501 * Make sure all outstanding requests complete before the program exits.
503 bdrv_drain_all();
505 blk_unref(qemuio_blk);
506 g_free(readline_state);
507 return 0;