blob: 61d54c01a084326e6a58bb8c4a463c2cd0c71229 [file] [log] [blame]
aliguorie3aff4f2009-04-05 19:14:04 +00001/*
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 */
Stefan Weilc32d7662009-08-31 22:16:16 +020010#include <sys/time.h>
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020015#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000016
Kevin Wolf3d219942013-06-05 14:19:39 +020017#include "qemu-io.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020019#include "qemu/option.h"
20#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010021#include "qemu/readline.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010022#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000023#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000024
Devin Nakamura43642b32011-07-11 11:22:16 -040025#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000026
27char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000028
Kevin Wolf734c3b82013-06-05 14:19:30 +020029BlockDriverState *qemuio_bs;
Kevin Wolf797ac582013-06-05 14:19:31 +020030extern int qemuio_misalign;
Kevin Wolf191c2892010-09-16 13:18:08 +020031
Kevin Wolfd1174f12013-06-05 14:19:37 +020032/* qemu-io commands passed using -c */
33static int ncmdline;
34static char **cmdline;
35
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010036static ReadLineState *readline_state;
37
Kevin Wolf734c3b82013-06-05 14:19:30 +020038static int close_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000039{
Fam Zheng4f6fd342013-08-23 09:14:47 +080040 bdrv_unref(bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020041 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040042 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000043}
44
45static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040046 .name = "close",
47 .altname = "c",
48 .cfunc = close_f,
49 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000050};
51
Max Reitzb543c5c2013-10-11 14:02:10 +020052static int openfile(char *name, int flags, int growable, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000053{
Max Reitz34b5d2c2013-09-05 14:45:29 +020054 Error *local_err = NULL;
55
Kevin Wolf734c3b82013-06-05 14:19:30 +020056 if (qemuio_bs) {
Devin Nakamura43642b32011-07-11 11:22:16 -040057 fprintf(stderr, "file open already, try 'help close'\n");
58 return 1;
59 }
aliguorie3aff4f2009-04-05 19:14:04 +000060
Devin Nakamura43642b32011-07-11 11:22:16 -040061 if (growable) {
Max Reitz72daa722013-12-20 19:28:08 +010062 if (bdrv_file_open(&qemuio_bs, name, NULL, opts, flags, &local_err)) {
Max Reitz34b5d2c2013-09-05 14:45:29 +020063 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
64 error_get_pretty(local_err));
65 error_free(local_err);
Devin Nakamura43642b32011-07-11 11:22:16 -040066 return 1;
67 }
68 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +020069 qemuio_bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +020070
Max Reitzddf56362014-02-18 18:33:06 +010071 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
72 < 0)
73 {
Max Reitz34b5d2c2013-09-05 14:45:29 +020074 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
75 error_get_pretty(local_err));
76 error_free(local_err);
Fam Zheng4f6fd342013-08-23 09:14:47 +080077 bdrv_unref(qemuio_bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020078 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040079 return 1;
80 }
81 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020082
Devin Nakamura43642b32011-07-11 11:22:16 -040083 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000084}
85
Devin Nakamura43642b32011-07-11 11:22:16 -040086static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000087{
Devin Nakamura43642b32011-07-11 11:22:16 -040088 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000089"\n"
90" opens a new file in the requested mode\n"
91"\n"
92" Example:\n"
93" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
94"\n"
95" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000096" -r, -- open file read-only\n"
97" -s, -- use snapshot file\n"
98" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +020099" -g, -- allow file to grow (only applies to protocols)\n"
100" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000101"\n");
102}
103
Kevin Wolf734c3b82013-06-05 14:19:30 +0200104static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000105
106static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400107 .name = "open",
108 .altname = "o",
109 .cfunc = open_f,
110 .argmin = 1,
111 .argmax = -1,
112 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200113 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400114 .oneline = "open the file specified by path",
115 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000116};
aliguorie3aff4f2009-04-05 19:14:04 +0000117
Max Reitzb543c5c2013-10-11 14:02:10 +0200118static QemuOptsList empty_opts = {
119 .name = "drive",
120 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
121 .desc = {
122 /* no elements => accept any params */
123 { /* end of list */ }
124 },
125};
126
Kevin Wolf734c3b82013-06-05 14:19:30 +0200127static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000128{
Devin Nakamura43642b32011-07-11 11:22:16 -0400129 int flags = 0;
130 int readonly = 0;
131 int growable = 0;
132 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200133 QemuOpts *qopts;
134 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000135
Max Reitzb543c5c2013-10-11 14:02:10 +0200136 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400137 switch (c) {
138 case 's':
139 flags |= BDRV_O_SNAPSHOT;
140 break;
141 case 'n':
142 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
143 break;
144 case 'r':
145 readonly = 1;
146 break;
147 case 'g':
148 growable = 1;
149 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200150 case 'o':
151 qopts = qemu_opts_parse(&empty_opts, optarg, 0);
152 if (qopts == NULL) {
153 printf("could not parse option list -- %s\n", optarg);
154 return 0;
155 }
156 opts = qemu_opts_to_qdict(qopts, opts);
157 qemu_opts_del(qopts);
158 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400159 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200160 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200161 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400162 }
aliguorie3aff4f2009-04-05 19:14:04 +0000163
Devin Nakamura43642b32011-07-11 11:22:16 -0400164 if (!readonly) {
165 flags |= BDRV_O_RDWR;
166 }
aliguorie3aff4f2009-04-05 19:14:04 +0000167
Max Reitzfd0fee32013-12-20 19:28:20 +0100168 if (optind == argc - 1) {
169 return openfile(argv[optind], flags, growable, opts);
170 } else if (optind == argc) {
171 return openfile(NULL, flags, growable, opts);
172 } else {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200173 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400174 }
aliguorie3aff4f2009-04-05 19:14:04 +0000175}
176
Kevin Wolfe681be72013-06-05 14:19:34 +0200177static int quit_f(BlockDriverState *bs, int argc, char **argv)
178{
179 return 1;
180}
181
182static const cmdinfo_t quit_cmd = {
183 .name = "quit",
184 .altname = "q",
185 .cfunc = quit_f,
186 .argmin = -1,
187 .argmax = -1,
188 .flags = CMD_FLAG_GLOBAL,
189 .oneline = "exit the program",
190};
191
aliguorie3aff4f2009-04-05 19:14:04 +0000192static void usage(const char *name)
193{
Devin Nakamura43642b32011-07-11 11:22:16 -0400194 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100195"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200196"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000197"\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000198" -c, --cmd command to execute\n"
199" -r, --read-only export read-only\n"
200" -s, --snapshot use snapshot file\n"
201" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200202" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000203" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200204" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200205" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000206" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000207" -h, --help display this help and exit\n"
208" -V, --version output version information and exit\n"
209"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -0400210 name);
aliguorie3aff4f2009-04-05 19:14:04 +0000211}
212
Kevin Wolfd1174f12013-06-05 14:19:37 +0200213static char *get_prompt(void)
214{
215 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
216
217 if (!prompt[0]) {
218 snprintf(prompt, sizeof(prompt), "%s> ", progname);
219 }
220
221 return prompt;
222}
223
Stefan Weild5d15072014-01-25 18:18:23 +0100224static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
225 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200226{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100227 va_list ap;
228 va_start(ap, fmt);
229 vprintf(fmt, ap);
230 va_end(ap);
231}
232
233static void readline_flush_func(void *opaque)
234{
235 fflush(stdout);
236}
237
238static void readline_func(void *opaque, const char *str, void *readline_opaque)
239{
240 char **line = readline_opaque;
241 *line = g_strdup(str);
242}
243
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100244static void completion_match(const char *cmd, void *opaque)
245{
246 readline_add_completion(readline_state, cmd);
247}
248
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100249static void readline_completion_func(void *opaque, const char *str)
250{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100251 readline_set_completion_index(readline_state, strlen(str));
252 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100253}
254
255static char *fetchline_readline(void)
256{
257 char *line = NULL;
258
259 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
260 while (!line) {
261 int ch = getchar();
262 if (ch == EOF) {
263 break;
264 }
265 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200266 }
267 return line;
268}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200269
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100270#define MAXREADLINESZ 1024
271static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200272{
273 char *p, *line = g_malloc(MAXREADLINESZ);
274
275 if (!fgets(line, MAXREADLINESZ, stdin)) {
276 g_free(line);
277 return NULL;
278 }
279
280 p = line + strlen(line);
281 if (p != line && p[-1] == '\n') {
282 p[-1] = '\0';
283 }
284
285 return line;
286}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100287
288static char *fetchline(void)
289{
290 if (readline_state) {
291 return fetchline_readline();
292 } else {
293 return fetchline_fgets();
294 }
295}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200296
297static void prep_fetchline(void *opaque)
298{
299 int *fetchable = opaque;
300
301 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
302 *fetchable= 1;
303}
304
305static void command_loop(void)
306{
307 int i, done = 0, fetchable = 0, prompted = 0;
308 char *input;
309
310 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200311 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200312 }
313 if (cmdline) {
314 g_free(cmdline);
315 return;
316 }
317
318 while (!done) {
319 if (!prompted) {
320 printf("%s", get_prompt());
321 fflush(stdout);
322 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
323 prompted = 1;
324 }
325
326 main_loop_wait(false);
327
328 if (!fetchable) {
329 continue;
330 }
331
332 input = fetchline();
333 if (input == NULL) {
334 break;
335 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200336 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200337 g_free(input);
338
339 prompted = 0;
340 fetchable = 0;
341 }
342 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
343}
344
345static void add_user_command(char *optarg)
346{
347 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
348 cmdline[ncmdline-1] = optarg;
349}
350
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100351static void reenable_tty_echo(void)
352{
353 qemu_set_tty_echo(STDIN_FILENO, true);
354}
355
aliguorie3aff4f2009-04-05 19:14:04 +0000356int main(int argc, char **argv)
357{
Devin Nakamura43642b32011-07-11 11:22:16 -0400358 int readonly = 0;
359 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100360 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400361 const struct option lopt[] = {
362 { "help", 0, NULL, 'h' },
363 { "version", 0, NULL, 'V' },
364 { "offset", 1, NULL, 'o' },
365 { "cmd", 1, NULL, 'c' },
366 { "read-only", 0, NULL, 'r' },
367 { "snapshot", 0, NULL, 's' },
368 { "nocache", 0, NULL, 'n' },
369 { "misalign", 0, NULL, 'm' },
370 { "growable", 0, NULL, 'g' },
371 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100372 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200373 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000374 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400375 { NULL, 0, NULL, 0 }
376 };
377 int c;
378 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100379 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +0000380
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900381#ifdef CONFIG_POSIX
382 signal(SIGPIPE, SIG_IGN);
383#endif
384
Devin Nakamura43642b32011-07-11 11:22:16 -0400385 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000386
Devin Nakamura43642b32011-07-11 11:22:16 -0400387 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
388 switch (c) {
389 case 's':
390 flags |= BDRV_O_SNAPSHOT;
391 break;
392 case 'n':
393 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
394 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100395 case 'd':
396 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
397 error_report("Invalid discard option: %s", optarg);
398 exit(1);
399 }
400 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400401 case 'c':
402 add_user_command(optarg);
403 break;
404 case 'r':
405 readonly = 1;
406 break;
407 case 'm':
Kevin Wolf797ac582013-06-05 14:19:31 +0200408 qemuio_misalign = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400409 break;
410 case 'g':
411 growable = 1;
412 break;
413 case 'k':
414 flags |= BDRV_O_NATIVE_AIO;
415 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200416 case 't':
417 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
418 error_report("Invalid cache option: %s", optarg);
419 exit(1);
420 }
421 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000422 case 'T':
423 if (!trace_backend_init(optarg, NULL)) {
424 exit(1); /* error message will have been printed */
425 }
426 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400427 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200428 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400429 exit(0);
430 case 'h':
431 usage(progname);
432 exit(0);
433 default:
434 usage(progname);
435 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200436 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400437 }
aliguorie3aff4f2009-04-05 19:14:04 +0000438
Devin Nakamura43642b32011-07-11 11:22:16 -0400439 if ((argc - optind) > 1) {
440 usage(progname);
441 exit(1);
442 }
aliguorie3aff4f2009-04-05 19:14:04 +0000443
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800444 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100445 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800446
Devin Nakamura43642b32011-07-11 11:22:16 -0400447 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200448 qemuio_add_command(&quit_cmd);
449 qemuio_add_command(&open_cmd);
450 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400451
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100452 if (isatty(STDIN_FILENO)) {
453 readline_state = readline_init(readline_printf_func,
454 readline_flush_func,
455 NULL,
456 readline_completion_func);
457 qemu_set_tty_echo(STDIN_FILENO, false);
458 atexit(reenable_tty_echo);
459 }
460
Devin Nakamura43642b32011-07-11 11:22:16 -0400461 /* open the device */
462 if (!readonly) {
463 flags |= BDRV_O_RDWR;
464 }
465
466 if ((argc - optind) == 1) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200467 openfile(argv[optind], flags, growable, NULL);
Devin Nakamura43642b32011-07-11 11:22:16 -0400468 }
469 command_loop();
470
471 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000472 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400473 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000474 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400475
Kevin Wolf734c3b82013-06-05 14:19:30 +0200476 if (qemuio_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800477 bdrv_unref(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400478 }
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100479 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400480 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000481}