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