]> git.proxmox.com Git - mirror_qemu.git/blob - qemu-io.c
Merge remote-tracking branch 'remotes/bonzini/tags/for-upstream' into staging
[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 "qapi/qmp/qstring.h"
22 #include "qom/object_interfaces.h"
23 #include "sysemu/block-backend.h"
24 #include "block/block_int.h"
25 #include "trace/control.h"
26
27 #define CMD_NOFILE_OK 0x01
28
29 static char *progname;
30
31 static BlockBackend *qemuio_blk;
32
33 /* qemu-io commands passed using -c */
34 static int ncmdline;
35 static char **cmdline;
36 static bool imageOpts;
37
38 static ReadLineState *readline_state;
39
40 static int close_f(BlockBackend *blk, int argc, char **argv)
41 {
42 blk_unref(qemuio_blk);
43 qemuio_blk = NULL;
44 return 0;
45 }
46
47 static const cmdinfo_t close_cmd = {
48 .name = "close",
49 .altname = "c",
50 .cfunc = close_f,
51 .oneline = "close the current open file",
52 };
53
54 static int openfile(char *name, int flags, QDict *opts)
55 {
56 Error *local_err = NULL;
57 BlockDriverState *bs;
58
59 if (qemuio_blk) {
60 error_report("file open already, try 'help close'");
61 QDECREF(opts);
62 return 1;
63 }
64
65 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
66 if (!qemuio_blk) {
67 error_reportf_err(local_err, "can't open%s%s: ",
68 name ? " device " : "", name ?: "");
69 return 1;
70 }
71
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;
79 }
80 if (bdrv_set_key(bs, password) < 0) {
81 error_report("invalid password");
82 goto error;
83 }
84 }
85
86
87 return 0;
88
89 error:
90 blk_unref(qemuio_blk);
91 qemuio_blk = NULL;
92 return 1;
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 -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");
110 }
111
112 static int open_f(BlockBackend *blk, int argc, char **argv);
113
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,
124 };
125
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 */ }
133 },
134 };
135
136 static int open_f(BlockBackend *blk, int argc, char **argv)
137 {
138 int flags = 0;
139 int readonly = 0;
140 int c;
141 QemuOpts *qopts;
142 QDict *opts;
143
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 (imageOpts) {
157 printf("--image-opts and 'open -o' are mutually exclusive\n");
158 return 0;
159 }
160 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
161 qemu_opts_reset(&empty_opts);
162 return 0;
163 }
164 break;
165 default:
166 qemu_opts_reset(&empty_opts);
167 return qemuio_command_usage(&open_cmd);
168 }
169 }
170
171 if (!readonly) {
172 flags |= BDRV_O_RDWR;
173 }
174
175 if (imageOpts && (optind == argc - 1)) {
176 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
177 qemu_opts_reset(&empty_opts);
178 return 0;
179 }
180 optind++;
181 }
182
183 qopts = qemu_opts_find(&empty_opts, NULL);
184 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
185 qemu_opts_reset(&empty_opts);
186
187 if (optind == argc - 1) {
188 return openfile(argv[optind], flags, opts);
189 } else if (optind == argc) {
190 return openfile(NULL, flags, opts);
191 } else {
192 QDECREF(opts);
193 return qemuio_command_usage(&open_cmd);
194 }
195 }
196
197 static int quit_f(BlockBackend *blk, int argc, char **argv)
198 {
199 return 1;
200 }
201
202 static const cmdinfo_t quit_cmd = {
203 .name = "quit",
204 .altname = "q",
205 .cfunc = quit_f,
206 .argmin = -1,
207 .argmax = -1,
208 .flags = CMD_FLAG_GLOBAL,
209 .oneline = "exit the program",
210 };
211
212 static void usage(const char *name)
213 {
214 printf(
215 "Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
216 "QEMU Disk exerciser\n"
217 "\n"
218 " --object OBJECTDEF define an object such as 'secret' for\n"
219 " passwords and/or encryption keys\n"
220 " -c, --cmd STRING execute command with its arguments\n"
221 " from the given string\n"
222 " -f, --format FMT specifies the block driver to use\n"
223 " -r, --read-only export read-only\n"
224 " -s, --snapshot use snapshot file\n"
225 " -n, --nocache disable host cache\n"
226 " -m, --misalign misalign allocations for O_DIRECT\n"
227 " -k, --native-aio use kernel AIO implementation (on Linux only)\n"
228 " -t, --cache=MODE use the given cache mode for the image\n"
229 " -T, --trace FILE enable trace events listed in the given file\n"
230 " -h, --help display this help and exit\n"
231 " -V, --version output version information and exit\n"
232 "\n"
233 "See '%s -c help' for information on available commands."
234 "\n",
235 name, name);
236 }
237
238 static char *get_prompt(void)
239 {
240 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
241
242 if (!prompt[0]) {
243 snprintf(prompt, sizeof(prompt), "%s> ", progname);
244 }
245
246 return prompt;
247 }
248
249 static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
250 const char *fmt, ...)
251 {
252 va_list ap;
253 va_start(ap, fmt);
254 vprintf(fmt, ap);
255 va_end(ap);
256 }
257
258 static void readline_flush_func(void *opaque)
259 {
260 fflush(stdout);
261 }
262
263 static void readline_func(void *opaque, const char *str, void *readline_opaque)
264 {
265 char **line = readline_opaque;
266 *line = g_strdup(str);
267 }
268
269 static void completion_match(const char *cmd, void *opaque)
270 {
271 readline_add_completion(readline_state, cmd);
272 }
273
274 static void readline_completion_func(void *opaque, const char *str)
275 {
276 readline_set_completion_index(readline_state, strlen(str));
277 qemuio_complete_command(str, completion_match, NULL);
278 }
279
280 static char *fetchline_readline(void)
281 {
282 char *line = NULL;
283
284 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
285 while (!line) {
286 int ch = getchar();
287 if (ch == EOF) {
288 break;
289 }
290 readline_handle_byte(readline_state, ch);
291 }
292 return line;
293 }
294
295 #define MAXREADLINESZ 1024
296 static char *fetchline_fgets(void)
297 {
298 char *p, *line = g_malloc(MAXREADLINESZ);
299
300 if (!fgets(line, MAXREADLINESZ, stdin)) {
301 g_free(line);
302 return NULL;
303 }
304
305 p = line + strlen(line);
306 if (p != line && p[-1] == '\n') {
307 p[-1] = '\0';
308 }
309
310 return line;
311 }
312
313 static char *fetchline(void)
314 {
315 if (readline_state) {
316 return fetchline_readline();
317 } else {
318 return fetchline_fgets();
319 }
320 }
321
322 static void prep_fetchline(void *opaque)
323 {
324 int *fetchable = opaque;
325
326 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
327 *fetchable= 1;
328 }
329
330 static void command_loop(void)
331 {
332 int i, done = 0, fetchable = 0, prompted = 0;
333 char *input;
334
335 for (i = 0; !done && i < ncmdline; i++) {
336 done = qemuio_command(qemuio_blk, cmdline[i]);
337 }
338 if (cmdline) {
339 g_free(cmdline);
340 return;
341 }
342
343 while (!done) {
344 if (!prompted) {
345 printf("%s", get_prompt());
346 fflush(stdout);
347 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
348 prompted = 1;
349 }
350
351 main_loop_wait(false);
352
353 if (!fetchable) {
354 continue;
355 }
356
357 input = fetchline();
358 if (input == NULL) {
359 break;
360 }
361 done = qemuio_command(qemuio_blk, input);
362 g_free(input);
363
364 prompted = 0;
365 fetchable = 0;
366 }
367 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
368 }
369
370 static void add_user_command(char *optarg)
371 {
372 cmdline = g_renew(char *, cmdline, ++ncmdline);
373 cmdline[ncmdline-1] = optarg;
374 }
375
376 static void reenable_tty_echo(void)
377 {
378 qemu_set_tty_echo(STDIN_FILENO, true);
379 }
380
381 enum {
382 OPTION_OBJECT = 256,
383 OPTION_IMAGE_OPTS = 257,
384 };
385
386 static QemuOptsList qemu_object_opts = {
387 .name = "object",
388 .implied_opt_name = "qom-type",
389 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
390 .desc = {
391 { }
392 },
393 };
394
395
396 static QemuOptsList file_opts = {
397 .name = "file",
398 .implied_opt_name = "file",
399 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
400 .desc = {
401 /* no elements => accept any params */
402 { /* end of list */ }
403 },
404 };
405
406 int main(int argc, char **argv)
407 {
408 int readonly = 0;
409 const char *sopt = "hVc:d:f:rsnmgkt:T:";
410 const struct option lopt[] = {
411 { "help", no_argument, NULL, 'h' },
412 { "version", no_argument, NULL, 'V' },
413 { "offset", required_argument, NULL, 'o' },
414 { "cmd", required_argument, NULL, 'c' },
415 { "format", required_argument, NULL, 'f' },
416 { "read-only", no_argument, NULL, 'r' },
417 { "snapshot", no_argument, NULL, 's' },
418 { "nocache", no_argument, NULL, 'n' },
419 { "misalign", no_argument, NULL, 'm' },
420 { "native-aio", no_argument, NULL, 'k' },
421 { "discard", required_argument, NULL, 'd' },
422 { "cache", required_argument, NULL, 't' },
423 { "trace", required_argument, NULL, 'T' },
424 { "object", required_argument, NULL, OPTION_OBJECT },
425 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
426 { NULL, 0, NULL, 0 }
427 };
428 int c;
429 int opt_index = 0;
430 int flags = BDRV_O_UNMAP;
431 Error *local_error = NULL;
432 QDict *opts = NULL;
433 const char *format = NULL;
434
435 #ifdef CONFIG_POSIX
436 signal(SIGPIPE, SIG_IGN);
437 #endif
438
439 progname = basename(argv[0]);
440 qemu_init_exec_dir(argv[0]);
441
442 module_call_init(MODULE_INIT_QOM);
443 qemu_add_opts(&qemu_object_opts);
444 bdrv_init();
445
446 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
447 switch (c) {
448 case 's':
449 flags |= BDRV_O_SNAPSHOT;
450 break;
451 case 'n':
452 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
453 break;
454 case 'd':
455 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
456 error_report("Invalid discard option: %s", optarg);
457 exit(1);
458 }
459 break;
460 case 'f':
461 format = optarg;
462 break;
463 case 'c':
464 add_user_command(optarg);
465 break;
466 case 'r':
467 readonly = 1;
468 break;
469 case 'm':
470 qemuio_misalign = true;
471 break;
472 case 'k':
473 flags |= BDRV_O_NATIVE_AIO;
474 break;
475 case 't':
476 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
477 error_report("Invalid cache option: %s", optarg);
478 exit(1);
479 }
480 break;
481 case 'T':
482 if (!trace_init_backends()) {
483 exit(1); /* error message will have been printed */
484 }
485 break;
486 case 'V':
487 printf("%s version %s\n", progname, QEMU_VERSION);
488 exit(0);
489 case 'h':
490 usage(progname);
491 exit(0);
492 case OPTION_OBJECT: {
493 QemuOpts *qopts;
494 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
495 optarg, true);
496 if (!qopts) {
497 exit(1);
498 }
499 } break;
500 case OPTION_IMAGE_OPTS:
501 imageOpts = true;
502 break;
503 default:
504 usage(progname);
505 exit(1);
506 }
507 }
508
509 if ((argc - optind) > 1) {
510 usage(progname);
511 exit(1);
512 }
513
514 if (format && imageOpts) {
515 error_report("--image-opts and -f are mutually exclusive");
516 exit(1);
517 }
518
519 if (qemu_init_main_loop(&local_error)) {
520 error_report_err(local_error);
521 exit(1);
522 }
523
524 if (qemu_opts_foreach(&qemu_object_opts,
525 user_creatable_add_opts_foreach,
526 NULL, &local_error)) {
527 error_report_err(local_error);
528 exit(1);
529 }
530
531 /* initialize commands */
532 qemuio_add_command(&quit_cmd);
533 qemuio_add_command(&open_cmd);
534 qemuio_add_command(&close_cmd);
535
536 if (isatty(STDIN_FILENO)) {
537 readline_state = readline_init(readline_printf_func,
538 readline_flush_func,
539 NULL,
540 readline_completion_func);
541 qemu_set_tty_echo(STDIN_FILENO, false);
542 atexit(reenable_tty_echo);
543 }
544
545 /* open the device */
546 if (!readonly) {
547 flags |= BDRV_O_RDWR;
548 }
549
550 if ((argc - optind) == 1) {
551 if (imageOpts) {
552 QemuOpts *qopts = NULL;
553 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
554 if (!qopts) {
555 exit(1);
556 }
557 opts = qemu_opts_to_qdict(qopts, NULL);
558 openfile(NULL, flags, opts);
559 } else {
560 if (format) {
561 opts = qdict_new();
562 qdict_put(opts, "driver", qstring_from_str(format));
563 }
564 openfile(argv[optind], flags, opts);
565 }
566 }
567 command_loop();
568
569 /*
570 * Make sure all outstanding requests complete before the program exits.
571 */
572 bdrv_drain_all();
573
574 blk_unref(qemuio_blk);
575 g_free(readline_state);
576 return 0;
577 }