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