]> git.proxmox.com Git - mirror_qemu.git/blob - qemu-io.c
qemu-io: Give more --version information
[mirror_qemu.git] / qemu-io.c
1 /*
2 * Command line utility to exercise the QEMU I/O path.
3 *
4 * Copyright (C) 2009 Red Hat, Inc.
5 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
6 *
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 "qemu/osdep.h"
11 #include <getopt.h>
12 #include <libgen.h>
13
14 #include "qapi/error.h"
15 #include "qemu-io.h"
16 #include "qemu/error-report.h"
17 #include "qemu/main-loop.h"
18 #include "qemu/option.h"
19 #include "qemu/config-file.h"
20 #include "qemu/readline.h"
21 #include "qemu/log.h"
22 #include "qapi/qmp/qstring.h"
23 #include "qapi/qmp/qbool.h"
24 #include "qom/object_interfaces.h"
25 #include "sysemu/block-backend.h"
26 #include "block/block_int.h"
27 #include "trace/control.h"
28 #include "crypto/init.h"
29 #include "qemu-version.h"
30
31 #define CMD_NOFILE_OK 0x01
32
33 static char *progname;
34
35 static BlockBackend *qemuio_blk;
36
37 /* qemu-io commands passed using -c */
38 static int ncmdline;
39 static char **cmdline;
40 static bool imageOpts;
41
42 static ReadLineState *readline_state;
43
44 static int close_f(BlockBackend *blk, int argc, char **argv)
45 {
46 blk_unref(qemuio_blk);
47 qemuio_blk = NULL;
48 return 0;
49 }
50
51 static const cmdinfo_t close_cmd = {
52 .name = "close",
53 .altname = "c",
54 .cfunc = close_f,
55 .oneline = "close the current open file",
56 };
57
58 static int openfile(char *name, int flags, bool writethrough, bool force_share,
59 QDict *opts)
60 {
61 Error *local_err = NULL;
62
63 if (qemuio_blk) {
64 error_report("file open already, try 'help close'");
65 QDECREF(opts);
66 return 1;
67 }
68
69 if (force_share) {
70 if (!opts) {
71 opts = qdict_new();
72 }
73 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
74 && !qdict_get_bool(opts, BDRV_OPT_FORCE_SHARE)) {
75 error_report("-U conflicts with image options");
76 QDECREF(opts);
77 return 1;
78 }
79 qdict_put_bool(opts, BDRV_OPT_FORCE_SHARE, true);
80 }
81 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
82 if (!qemuio_blk) {
83 error_reportf_err(local_err, "can't open%s%s: ",
84 name ? " device " : "", name ?: "");
85 return 1;
86 }
87
88 blk_set_enable_write_cache(qemuio_blk, !writethrough);
89
90 return 0;
91 }
92
93 static void open_help(void)
94 {
95 printf(
96 "\n"
97 " opens a new file in the requested mode\n"
98 "\n"
99 " Example:\n"
100 " 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
101 "\n"
102 " Opens a file for subsequent use by all of the other qemu-io commands.\n"
103 " -r, -- open file read-only\n"
104 " -s, -- use snapshot file\n"
105 " -n, -- disable host cache, short for -t none\n"
106 " -U, -- force shared permissions\n"
107 " -k, -- use kernel AIO implementation (on Linux only)\n"
108 " -t, -- use the given cache mode for the image\n"
109 " -d, -- use the given discard mode for the image\n"
110 " -o, -- options to be given to the block driver"
111 "\n");
112 }
113
114 static int open_f(BlockBackend *blk, int argc, char **argv);
115
116 static const cmdinfo_t open_cmd = {
117 .name = "open",
118 .altname = "o",
119 .cfunc = open_f,
120 .argmin = 1,
121 .argmax = -1,
122 .flags = CMD_NOFILE_OK,
123 .args = "[-rsnkU] [-t cache] [-d discard] [-o options] [path]",
124 .oneline = "open the file specified by path",
125 .help = open_help,
126 };
127
128 static QemuOptsList empty_opts = {
129 .name = "drive",
130 .merge_lists = true,
131 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
132 .desc = {
133 /* no elements => accept any params */
134 { /* end of list */ }
135 },
136 };
137
138 static int open_f(BlockBackend *blk, int argc, char **argv)
139 {
140 int flags = BDRV_O_UNMAP;
141 int readonly = 0;
142 bool writethrough = true;
143 int c;
144 QemuOpts *qopts;
145 QDict *opts;
146 bool force_share = false;
147
148 while ((c = getopt(argc, argv, "snro:kt:d:U")) != -1) {
149 switch (c) {
150 case 's':
151 flags |= BDRV_O_SNAPSHOT;
152 break;
153 case 'n':
154 flags |= BDRV_O_NOCACHE;
155 writethrough = false;
156 break;
157 case 'r':
158 readonly = 1;
159 break;
160 case 'k':
161 flags |= BDRV_O_NATIVE_AIO;
162 break;
163 case 't':
164 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
165 error_report("Invalid cache option: %s", optarg);
166 qemu_opts_reset(&empty_opts);
167 return 0;
168 }
169 break;
170 case 'd':
171 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
172 error_report("Invalid discard option: %s", optarg);
173 qemu_opts_reset(&empty_opts);
174 return 0;
175 }
176 break;
177 case 'o':
178 if (imageOpts) {
179 printf("--image-opts and 'open -o' are mutually exclusive\n");
180 qemu_opts_reset(&empty_opts);
181 return 0;
182 }
183 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
184 qemu_opts_reset(&empty_opts);
185 return 0;
186 }
187 break;
188 case 'U':
189 force_share = true;
190 break;
191 default:
192 qemu_opts_reset(&empty_opts);
193 return qemuio_command_usage(&open_cmd);
194 }
195 }
196
197 if (!readonly) {
198 flags |= BDRV_O_RDWR;
199 }
200
201 if (imageOpts && (optind == argc - 1)) {
202 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
203 qemu_opts_reset(&empty_opts);
204 return 0;
205 }
206 optind++;
207 }
208
209 qopts = qemu_opts_find(&empty_opts, NULL);
210 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
211 qemu_opts_reset(&empty_opts);
212
213 if (optind == argc - 1) {
214 openfile(argv[optind], flags, writethrough, force_share, opts);
215 } else if (optind == argc) {
216 openfile(NULL, flags, writethrough, force_share, opts);
217 } else {
218 QDECREF(opts);
219 qemuio_command_usage(&open_cmd);
220 }
221 return 0;
222 }
223
224 static int quit_f(BlockBackend *blk, int argc, char **argv)
225 {
226 return 1;
227 }
228
229 static const cmdinfo_t quit_cmd = {
230 .name = "quit",
231 .altname = "q",
232 .cfunc = quit_f,
233 .argmin = -1,
234 .argmax = -1,
235 .flags = CMD_FLAG_GLOBAL,
236 .oneline = "exit the program",
237 };
238
239 static void usage(const char *name)
240 {
241 printf(
242 "Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
243 "QEMU Disk exerciser\n"
244 "\n"
245 " --object OBJECTDEF define an object such as 'secret' for\n"
246 " passwords and/or encryption keys\n"
247 " --image-opts treat file as option string\n"
248 " -c, --cmd STRING execute command with its arguments\n"
249 " from the given string\n"
250 " -f, --format FMT specifies the block driver to use\n"
251 " -r, --read-only export read-only\n"
252 " -s, --snapshot use snapshot file\n"
253 " -n, --nocache disable host cache, short for -t none\n"
254 " -m, --misalign misalign allocations for O_DIRECT\n"
255 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
256 " -t, --cache=MODE use the given cache mode for the image\n"
257 " -d, --discard=MODE use the given discard mode for the image\n"
258 " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
259 " specify tracing options\n"
260 " see qemu-img(1) man page for full description\n"
261 " -U, --force-share force shared permissions\n"
262 " -h, --help display this help and exit\n"
263 " -V, --version output version information and exit\n"
264 "\n"
265 "See '%s -c help' for information on available commands."
266 "\n",
267 name, name);
268 }
269
270 static char *get_prompt(void)
271 {
272 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
273
274 if (!prompt[0]) {
275 snprintf(prompt, sizeof(prompt), "%s> ", progname);
276 }
277
278 return prompt;
279 }
280
281 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
282 const char *fmt, ...)
283 {
284 va_list ap;
285 va_start(ap, fmt);
286 vprintf(fmt, ap);
287 va_end(ap);
288 }
289
290 static void readline_flush_func(void *opaque)
291 {
292 fflush(stdout);
293 }
294
295 static void readline_func(void *opaque, const char *str, void *readline_opaque)
296 {
297 char **line = readline_opaque;
298 *line = g_strdup(str);
299 }
300
301 static void completion_match(const char *cmd, void *opaque)
302 {
303 readline_add_completion(readline_state, cmd);
304 }
305
306 static void readline_completion_func(void *opaque, const char *str)
307 {
308 readline_set_completion_index(readline_state, strlen(str));
309 qemuio_complete_command(str, completion_match, NULL);
310 }
311
312 static char *fetchline_readline(void)
313 {
314 char *line = NULL;
315
316 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
317 while (!line) {
318 int ch = getchar();
319 if (ch == EOF) {
320 break;
321 }
322 readline_handle_byte(readline_state, ch);
323 }
324 return line;
325 }
326
327 #define MAXREADLINESZ 1024
328 static char *fetchline_fgets(void)
329 {
330 char *p, *line = g_malloc(MAXREADLINESZ);
331
332 if (!fgets(line, MAXREADLINESZ, stdin)) {
333 g_free(line);
334 return NULL;
335 }
336
337 p = line + strlen(line);
338 if (p != line && p[-1] == '\n') {
339 p[-1] = '\0';
340 }
341
342 return line;
343 }
344
345 static char *fetchline(void)
346 {
347 if (readline_state) {
348 return fetchline_readline();
349 } else {
350 return fetchline_fgets();
351 }
352 }
353
354 static void prep_fetchline(void *opaque)
355 {
356 int *fetchable = opaque;
357
358 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
359 *fetchable= 1;
360 }
361
362 static void command_loop(void)
363 {
364 int i, done = 0, fetchable = 0, prompted = 0;
365 char *input;
366
367 for (i = 0; !done && i < ncmdline; i++) {
368 done = qemuio_command(qemuio_blk, cmdline[i]);
369 }
370 if (cmdline) {
371 g_free(cmdline);
372 return;
373 }
374
375 while (!done) {
376 if (!prompted) {
377 printf("%s", get_prompt());
378 fflush(stdout);
379 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
380 prompted = 1;
381 }
382
383 main_loop_wait(false);
384
385 if (!fetchable) {
386 continue;
387 }
388
389 input = fetchline();
390 if (input == NULL) {
391 break;
392 }
393 done = qemuio_command(qemuio_blk, input);
394 g_free(input);
395
396 prompted = 0;
397 fetchable = 0;
398 }
399 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
400 }
401
402 static void add_user_command(char *optarg)
403 {
404 cmdline = g_renew(char *, cmdline, ++ncmdline);
405 cmdline[ncmdline-1] = optarg;
406 }
407
408 static void reenable_tty_echo(void)
409 {
410 qemu_set_tty_echo(STDIN_FILENO, true);
411 }
412
413 enum {
414 OPTION_OBJECT = 256,
415 OPTION_IMAGE_OPTS = 257,
416 };
417
418 static QemuOptsList qemu_object_opts = {
419 .name = "object",
420 .implied_opt_name = "qom-type",
421 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
422 .desc = {
423 { }
424 },
425 };
426
427
428 static QemuOptsList file_opts = {
429 .name = "file",
430 .implied_opt_name = "file",
431 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
432 .desc = {
433 /* no elements => accept any params */
434 { /* end of list */ }
435 },
436 };
437
438 int main(int argc, char **argv)
439 {
440 int readonly = 0;
441 const char *sopt = "hVc:d:f:rsnmkt:T:U";
442 const struct option lopt[] = {
443 { "help", no_argument, NULL, 'h' },
444 { "version", no_argument, NULL, 'V' },
445 { "cmd", required_argument, NULL, 'c' },
446 { "format", required_argument, NULL, 'f' },
447 { "read-only", no_argument, NULL, 'r' },
448 { "snapshot", no_argument, NULL, 's' },
449 { "nocache", no_argument, NULL, 'n' },
450 { "misalign", no_argument, NULL, 'm' },
451 { "native-aio", no_argument, NULL, 'k' },
452 { "discard", required_argument, NULL, 'd' },
453 { "cache", required_argument, NULL, 't' },
454 { "trace", required_argument, NULL, 'T' },
455 { "object", required_argument, NULL, OPTION_OBJECT },
456 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
457 { "force-share", no_argument, 0, 'U'},
458 { NULL, 0, NULL, 0 }
459 };
460 int c;
461 int opt_index = 0;
462 int flags = BDRV_O_UNMAP;
463 bool writethrough = true;
464 Error *local_error = NULL;
465 QDict *opts = NULL;
466 const char *format = NULL;
467 char *trace_file = NULL;
468 bool force_share = false;
469
470 #ifdef CONFIG_POSIX
471 signal(SIGPIPE, SIG_IGN);
472 #endif
473
474 module_call_init(MODULE_INIT_TRACE);
475 progname = basename(argv[0]);
476 qemu_init_exec_dir(argv[0]);
477
478 qcrypto_init(&error_fatal);
479
480 module_call_init(MODULE_INIT_QOM);
481 qemu_add_opts(&qemu_object_opts);
482 qemu_add_opts(&qemu_trace_opts);
483 bdrv_init();
484
485 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
486 switch (c) {
487 case 's':
488 flags |= BDRV_O_SNAPSHOT;
489 break;
490 case 'n':
491 flags |= BDRV_O_NOCACHE;
492 writethrough = false;
493 break;
494 case 'd':
495 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
496 error_report("Invalid discard option: %s", optarg);
497 exit(1);
498 }
499 break;
500 case 'f':
501 format = optarg;
502 break;
503 case 'c':
504 add_user_command(optarg);
505 break;
506 case 'r':
507 readonly = 1;
508 break;
509 case 'm':
510 qemuio_misalign = true;
511 break;
512 case 'k':
513 flags |= BDRV_O_NATIVE_AIO;
514 break;
515 case 't':
516 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
517 error_report("Invalid cache option: %s", optarg);
518 exit(1);
519 }
520 break;
521 case 'T':
522 g_free(trace_file);
523 trace_file = trace_opt_parse(optarg);
524 break;
525 case 'V':
526 printf("%s version " QEMU_VERSION QEMU_PKGVERSION "\n"
527 QEMU_COPYRIGHT "\n", progname);
528 exit(0);
529 case 'h':
530 usage(progname);
531 exit(0);
532 case 'U':
533 force_share = true;
534 break;
535 case OPTION_OBJECT: {
536 QemuOpts *qopts;
537 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
538 optarg, true);
539 if (!qopts) {
540 exit(1);
541 }
542 } break;
543 case OPTION_IMAGE_OPTS:
544 imageOpts = true;
545 break;
546 default:
547 usage(progname);
548 exit(1);
549 }
550 }
551
552 if ((argc - optind) > 1) {
553 usage(progname);
554 exit(1);
555 }
556
557 if (format && imageOpts) {
558 error_report("--image-opts and -f are mutually exclusive");
559 exit(1);
560 }
561
562 if (qemu_init_main_loop(&local_error)) {
563 error_report_err(local_error);
564 exit(1);
565 }
566
567 if (qemu_opts_foreach(&qemu_object_opts,
568 user_creatable_add_opts_foreach,
569 NULL, NULL)) {
570 exit(1);
571 }
572
573 if (!trace_init_backends()) {
574 exit(1);
575 }
576 trace_init_file(trace_file);
577 qemu_set_log(LOG_TRACE);
578
579 /* initialize commands */
580 qemuio_add_command(&quit_cmd);
581 qemuio_add_command(&open_cmd);
582 qemuio_add_command(&close_cmd);
583
584 if (isatty(STDIN_FILENO)) {
585 readline_state = readline_init(readline_printf_func,
586 readline_flush_func,
587 NULL,
588 readline_completion_func);
589 qemu_set_tty_echo(STDIN_FILENO, false);
590 atexit(reenable_tty_echo);
591 }
592
593 /* open the device */
594 if (!readonly) {
595 flags |= BDRV_O_RDWR;
596 }
597
598 if ((argc - optind) == 1) {
599 if (imageOpts) {
600 QemuOpts *qopts = NULL;
601 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
602 if (!qopts) {
603 exit(1);
604 }
605 opts = qemu_opts_to_qdict(qopts, NULL);
606 if (openfile(NULL, flags, writethrough, force_share, opts)) {
607 exit(1);
608 }
609 } else {
610 if (format) {
611 opts = qdict_new();
612 qdict_put_str(opts, "driver", format);
613 }
614 if (openfile(argv[optind], flags, writethrough,
615 force_share, opts)) {
616 exit(1);
617 }
618 }
619 }
620 command_loop();
621
622 /*
623 * Make sure all outstanding requests complete before the program exits.
624 */
625 bdrv_drain_all();
626
627 blk_unref(qemuio_blk);
628 g_free(readline_state);
629 return 0;
630 }