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