Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / qemu-io.c
blob2f6d61a14629125333374ce7684d4124e0871fc7
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;
32 /* qemu-io commands passed using -c */
33 static int ncmdline;
34 static char **cmdline;
36 static ReadLineState *readline_state;
38 static int close_f(BlockBackend *blk, int argc, char **argv)
40 blk_unref(qemuio_blk);
41 qemuio_blk = NULL;
42 return 0;
45 static const cmdinfo_t close_cmd = {
46 .name = "close",
47 .altname = "c",
48 .cfunc = close_f,
49 .oneline = "close the current open file",
52 static int openfile(char *name, int flags, QDict *opts)
54 Error *local_err = NULL;
55 BlockDriverState *bs;
57 if (qemuio_blk) {
58 fprintf(stderr, "file open already, try 'help close'\n");
59 QDECREF(opts);
60 return 1;
63 qemuio_blk = blk_new_open("hda", name, NULL, opts, flags, &local_err);
64 if (!qemuio_blk) {
65 fprintf(stderr, "%s: can't open%s%s: %s\n", progname,
66 name ? " device " : "", name ?: "",
67 error_get_pretty(local_err));
68 error_free(local_err);
69 return 1;
72 bs = blk_bs(qemuio_blk);
73 if (bdrv_is_encrypted(bs)) {
74 char password[256];
75 printf("Disk image '%s' is encrypted.\n", name);
76 if (qemu_read_password(password, sizeof(password)) < 0) {
77 error_report("No password given");
78 goto error;
80 if (bdrv_set_key(bs, password) < 0) {
81 error_report("invalid password");
82 goto error;
87 return 0;
89 error:
90 blk_unref(qemuio_blk);
91 qemuio_blk = NULL;
92 return 1;
95 static void open_help(void)
97 printf(
98 "\n"
99 " opens a new file in the requested mode\n"
100 "\n"
101 " Example:\n"
102 " 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
103 "\n"
104 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
105 " -r, -- open file read-only\n"
106 " -s, -- use snapshot file\n"
107 " -n, -- disable host cache\n"
108 " -o, -- options to be given to the block driver"
109 "\n");
112 static int open_f(BlockBackend *blk, int argc, char **argv);
114 static const cmdinfo_t open_cmd = {
115 .name = "open",
116 .altname = "o",
117 .cfunc = open_f,
118 .argmin = 1,
119 .argmax = -1,
120 .flags = CMD_NOFILE_OK,
121 .args = "[-Crsn] [-o options] [path]",
122 .oneline = "open the file specified by path",
123 .help = open_help,
126 static QemuOptsList empty_opts = {
127 .name = "drive",
128 .merge_lists = true,
129 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
130 .desc = {
131 /* no elements => accept any params */
132 { /* end of list */ }
136 static int open_f(BlockBackend *blk, int argc, char **argv)
138 int flags = 0;
139 int readonly = 0;
140 int c;
141 QemuOpts *qopts;
142 QDict *opts;
144 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
145 switch (c) {
146 case 's':
147 flags |= BDRV_O_SNAPSHOT;
148 break;
149 case 'n':
150 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
151 break;
152 case 'r':
153 readonly = 1;
154 break;
155 case 'o':
156 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
157 printf("could not parse option list -- %s\n", optarg);
158 qemu_opts_reset(&empty_opts);
159 return 0;
161 break;
162 default:
163 qemu_opts_reset(&empty_opts);
164 return qemuio_command_usage(&open_cmd);
168 if (!readonly) {
169 flags |= BDRV_O_RDWR;
172 qopts = qemu_opts_find(&empty_opts, NULL);
173 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
174 qemu_opts_reset(&empty_opts);
176 if (optind == argc - 1) {
177 return openfile(argv[optind], flags, opts);
178 } else if (optind == argc) {
179 return openfile(NULL, flags, opts);
180 } else {
181 QDECREF(opts);
182 return qemuio_command_usage(&open_cmd);
186 static int quit_f(BlockBackend *blk, int argc, char **argv)
188 return 1;
191 #if defined(CONFIG_FVD)
192 # include "qemu-io-sim.c"
193 #endif
195 static const cmdinfo_t quit_cmd = {
196 .name = "quit",
197 .altname = "q",
198 .cfunc = quit_f,
199 .argmin = -1,
200 .argmax = -1,
201 .flags = CMD_FLAG_GLOBAL,
202 .oneline = "exit the program",
205 static void usage(const char *name)
207 printf(
208 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
209 "QEMU Disk exerciser\n"
210 "\n"
211 " -c, --cmd STRING execute command with its arguments\n"
212 " from the given string\n"
213 " -f, --format FMT specifies the block driver to use\n"
214 " -r, --read-only export read-only\n"
215 " -s, --snapshot use snapshot file\n"
216 " -n, --nocache disable host cache\n"
217 " -m, --misalign misalign allocations for O_DIRECT\n"
218 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
219 " -t, --cache=MODE use the given cache mode for the image\n"
220 " -T, --trace FILE enable trace events listed in the given file\n"
221 " -h, --help display this help and exit\n"
222 " -V, --version output version information and exit\n"
223 "\n"
224 "See '%s -c help' for information on available commands."
225 "\n",
226 name, name);
229 static char *get_prompt(void)
231 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
233 if (!prompt[0]) {
234 snprintf(prompt, sizeof(prompt), "%s> ", progname);
237 return prompt;
240 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
241 const char *fmt, ...)
243 va_list ap;
244 va_start(ap, fmt);
245 vprintf(fmt, ap);
246 va_end(ap);
249 static void readline_flush_func(void *opaque)
251 fflush(stdout);
254 static void readline_func(void *opaque, const char *str, void *readline_opaque)
256 char **line = readline_opaque;
257 *line = g_strdup(str);
260 static void completion_match(const char *cmd, void *opaque)
262 readline_add_completion(readline_state, cmd);
265 static void readline_completion_func(void *opaque, const char *str)
267 readline_set_completion_index(readline_state, strlen(str));
268 qemuio_complete_command(str, completion_match, NULL);
271 static char *fetchline_readline(void)
273 char *line = NULL;
275 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
276 while (!line) {
277 int ch = getchar();
278 if (ch == EOF) {
279 break;
281 readline_handle_byte(readline_state, ch);
283 return line;
286 #define MAXREADLINESZ 1024
287 static char *fetchline_fgets(void)
289 char *p, *line = g_malloc(MAXREADLINESZ);
291 if (!fgets(line, MAXREADLINESZ, stdin)) {
292 g_free(line);
293 return NULL;
296 p = line + strlen(line);
297 if (p != line && p[-1] == '\n') {
298 p[-1] = '\0';
301 return line;
304 static char *fetchline(void)
306 if (readline_state) {
307 return fetchline_readline();
308 } else {
309 return fetchline_fgets();
313 static void prep_fetchline(void *opaque)
315 int *fetchable = opaque;
317 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
318 *fetchable= 1;
321 static void command_loop(void)
323 int i, done = 0, fetchable = 0, prompted = 0;
324 char *input;
326 for (i = 0; !done && i < ncmdline; i++) {
327 done = qemuio_command(qemuio_blk, cmdline[i]);
329 if (cmdline) {
330 g_free(cmdline);
331 return;
334 while (!done) {
335 if (!prompted) {
336 printf("%s", get_prompt());
337 fflush(stdout);
338 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
339 prompted = 1;
342 main_loop_wait(false);
344 if (!fetchable) {
345 continue;
348 input = fetchline();
349 if (input == NULL) {
350 break;
352 done = qemuio_command(qemuio_blk, input);
353 g_free(input);
355 prompted = 0;
356 fetchable = 0;
358 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
361 static void add_user_command(char *optarg)
363 cmdline = g_renew(char *, cmdline, ++ncmdline);
364 cmdline[ncmdline-1] = optarg;
367 static void reenable_tty_echo(void)
369 qemu_set_tty_echo(STDIN_FILENO, true);
372 int main(int argc, char **argv)
374 int readonly = 0;
375 const char *sopt = "hVc:d:f:rsnmgkt:T:";
376 const struct option lopt[] = {
377 { "help", 0, NULL, 'h' },
378 { "version", 0, NULL, 'V' },
379 { "offset", 1, NULL, 'o' },
380 { "cmd", 1, NULL, 'c' },
381 { "format", 1, NULL, 'f' },
382 { "read-only", 0, NULL, 'r' },
383 { "snapshot", 0, NULL, 's' },
384 { "nocache", 0, NULL, 'n' },
385 { "misalign", 0, NULL, 'm' },
386 { "native-aio", 0, NULL, 'k' },
387 { "discard", 1, NULL, 'd' },
388 { "cache", 1, NULL, 't' },
389 { "trace", 1, NULL, 'T' },
390 { NULL, 0, NULL, 0 }
392 int c;
393 int opt_index = 0;
394 int flags = BDRV_O_UNMAP;
395 Error *local_error = NULL;
396 QDict *opts = NULL;
398 #ifdef CONFIG_POSIX
399 signal(SIGPIPE, SIG_IGN);
400 #endif
402 progname = basename(argv[0]);
403 qemu_init_exec_dir(argv[0]);
405 bdrv_init();
407 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
408 switch (c) {
409 case 's':
410 flags |= BDRV_O_SNAPSHOT;
411 break;
412 case 'n':
413 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
414 break;
415 case 'd':
416 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
417 error_report("Invalid discard option: %s", optarg);
418 exit(1);
420 break;
421 case 'f':
422 if (!opts) {
423 opts = qdict_new();
425 qdict_put(opts, "driver", qstring_from_str(optarg));
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 'k':
437 flags |= BDRV_O_NATIVE_AIO;
438 break;
439 case 't':
440 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
441 error_report("Invalid cache option: %s", optarg);
442 exit(1);
444 break;
445 case 'T':
446 if (!trace_init_backends(optarg, NULL)) {
447 exit(1); /* error message will have been printed */
449 break;
450 case 'V':
451 printf("%s version %s\n", progname, QEMU_VERSION);
452 exit(0);
453 case 'h':
454 usage(progname);
455 exit(0);
456 default:
457 usage(progname);
458 exit(1);
462 if ((argc - optind) > 1) {
463 usage(progname);
464 exit(1);
467 if (qemu_init_main_loop(&local_error)) {
468 error_report_err(local_error);
469 exit(1);
472 /* initialize commands */
473 qemuio_add_command(&quit_cmd);
474 qemuio_add_command(&open_cmd);
475 qemuio_add_command(&close_cmd);
477 if (isatty(STDIN_FILENO)) {
478 readline_state = readline_init(readline_printf_func,
479 readline_flush_func,
480 NULL,
481 readline_completion_func);
482 qemu_set_tty_echo(STDIN_FILENO, false);
483 atexit(reenable_tty_echo);
486 /* open the device */
487 if (!readonly) {
488 flags |= BDRV_O_RDWR;
491 if ((argc - optind) == 1) {
492 openfile(argv[optind], flags, opts);
494 command_loop();
497 * Make sure all outstanding requests complete before the program exits.
499 bdrv_drain_all();
501 blk_unref(qemuio_blk);
502 g_free(readline_state);
503 return 0;