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