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