virtio-pci: drop identical virtio_pci_cap
[qemu/ar7.git] / qemu-io.c
blob9bc83c6ec19432592fcc35bb86c9f5464a339ee7
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 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 " -m, --misalign misalign allocations for O_DIRECT\n"
214 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
215 " -t, --cache=MODE use the given cache mode for the image\n"
216 " -T, --trace FILE enable trace events listed in the given file\n"
217 " -h, --help display this help and exit\n"
218 " -V, --version output version information and exit\n"
219 "\n"
220 "See '%s -c help' for information on available commands."
221 "\n",
222 name, name);
225 static char *get_prompt(void)
227 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
229 if (!prompt[0]) {
230 snprintf(prompt, sizeof(prompt), "%s> ", progname);
233 return prompt;
236 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
237 const char *fmt, ...)
239 va_list ap;
240 va_start(ap, fmt);
241 vprintf(fmt, ap);
242 va_end(ap);
245 static void readline_flush_func(void *opaque)
247 fflush(stdout);
250 static void readline_func(void *opaque, const char *str, void *readline_opaque)
252 char **line = readline_opaque;
253 *line = g_strdup(str);
256 static void completion_match(const char *cmd, void *opaque)
258 readline_add_completion(readline_state, cmd);
261 static void readline_completion_func(void *opaque, const char *str)
263 readline_set_completion_index(readline_state, strlen(str));
264 qemuio_complete_command(str, completion_match, NULL);
267 static char *fetchline_readline(void)
269 char *line = NULL;
271 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
272 while (!line) {
273 int ch = getchar();
274 if (ch == EOF) {
275 break;
277 readline_handle_byte(readline_state, ch);
279 return line;
282 #define MAXREADLINESZ 1024
283 static char *fetchline_fgets(void)
285 char *p, *line = g_malloc(MAXREADLINESZ);
287 if (!fgets(line, MAXREADLINESZ, stdin)) {
288 g_free(line);
289 return NULL;
292 p = line + strlen(line);
293 if (p != line && p[-1] == '\n') {
294 p[-1] = '\0';
297 return line;
300 static char *fetchline(void)
302 if (readline_state) {
303 return fetchline_readline();
304 } else {
305 return fetchline_fgets();
309 static void prep_fetchline(void *opaque)
311 int *fetchable = opaque;
313 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
314 *fetchable= 1;
317 static void command_loop(void)
319 int i, done = 0, fetchable = 0, prompted = 0;
320 char *input;
322 for (i = 0; !done && i < ncmdline; i++) {
323 done = qemuio_command(qemuio_blk, cmdline[i]);
325 if (cmdline) {
326 g_free(cmdline);
327 return;
330 while (!done) {
331 if (!prompted) {
332 printf("%s", get_prompt());
333 fflush(stdout);
334 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
335 prompted = 1;
338 main_loop_wait(false);
340 if (!fetchable) {
341 continue;
344 input = fetchline();
345 if (input == NULL) {
346 break;
348 done = qemuio_command(qemuio_blk, input);
349 g_free(input);
351 prompted = 0;
352 fetchable = 0;
354 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
357 static void add_user_command(char *optarg)
359 cmdline = g_renew(char *, cmdline, ++ncmdline);
360 cmdline[ncmdline-1] = optarg;
363 static void reenable_tty_echo(void)
365 qemu_set_tty_echo(STDIN_FILENO, true);
368 int main(int argc, char **argv)
370 int readonly = 0;
371 const char *sopt = "hVc:d:f:rsnmgkt:T:";
372 const struct option lopt[] = {
373 { "help", 0, NULL, 'h' },
374 { "version", 0, NULL, 'V' },
375 { "offset", 1, NULL, 'o' },
376 { "cmd", 1, NULL, 'c' },
377 { "format", 1, NULL, 'f' },
378 { "read-only", 0, NULL, 'r' },
379 { "snapshot", 0, NULL, 's' },
380 { "nocache", 0, NULL, 'n' },
381 { "misalign", 0, NULL, 'm' },
382 { "native-aio", 0, NULL, 'k' },
383 { "discard", 1, NULL, 'd' },
384 { "cache", 1, NULL, 't' },
385 { "trace", 1, NULL, 'T' },
386 { NULL, 0, NULL, 0 }
388 int c;
389 int opt_index = 0;
390 int flags = BDRV_O_UNMAP;
391 Error *local_error = NULL;
392 QDict *opts = NULL;
394 #ifdef CONFIG_POSIX
395 signal(SIGPIPE, SIG_IGN);
396 #endif
398 progname = basename(argv[0]);
399 qemu_init_exec_dir(argv[0]);
401 bdrv_init();
403 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
404 switch (c) {
405 case 's':
406 flags |= BDRV_O_SNAPSHOT;
407 break;
408 case 'n':
409 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
410 break;
411 case 'd':
412 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
413 error_report("Invalid discard option: %s", optarg);
414 exit(1);
416 break;
417 case 'f':
418 if (!opts) {
419 opts = qdict_new();
421 qdict_put(opts, "driver", qstring_from_str(optarg));
422 break;
423 case 'c':
424 add_user_command(optarg);
425 break;
426 case 'r':
427 readonly = 1;
428 break;
429 case 'm':
430 qemuio_misalign = true;
431 break;
432 case 'k':
433 flags |= BDRV_O_NATIVE_AIO;
434 break;
435 case 't':
436 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
437 error_report("Invalid cache option: %s", optarg);
438 exit(1);
440 break;
441 case 'T':
442 if (!trace_init_backends(optarg, NULL)) {
443 exit(1); /* error message will have been printed */
445 break;
446 case 'V':
447 printf("%s version %s\n", progname, QEMU_VERSION);
448 exit(0);
449 case 'h':
450 usage(progname);
451 exit(0);
452 default:
453 usage(progname);
454 exit(1);
458 if ((argc - optind) > 1) {
459 usage(progname);
460 exit(1);
463 if (qemu_init_main_loop(&local_error)) {
464 error_report_err(local_error);
465 exit(1);
468 /* initialize commands */
469 qemuio_add_command(&quit_cmd);
470 qemuio_add_command(&open_cmd);
471 qemuio_add_command(&close_cmd);
473 if (isatty(STDIN_FILENO)) {
474 readline_state = readline_init(readline_printf_func,
475 readline_flush_func,
476 NULL,
477 readline_completion_func);
478 qemu_set_tty_echo(STDIN_FILENO, false);
479 atexit(reenable_tty_echo);
482 /* open the device */
483 if (!readonly) {
484 flags |= BDRV_O_RDWR;
487 if ((argc - optind) == 1) {
488 openfile(argv[optind], flags, opts);
490 command_loop();
493 * Make sure all outstanding requests complete before the program exits.
495 bdrv_drain_all();
497 blk_unref(qemuio_blk);
498 g_free(readline_state);
499 return 0;