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