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