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