blob: d7c26d3ed0b68ad927963ef8087a12bc1a7a9d64 [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 Reitzb543c5c2013-10-11 14:02:10 +020062 if (bdrv_file_open(&qemuio_bs, name, 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 Reitzb543c5c2013-10-11 14:02:10 +020071 if (bdrv_open(qemuio_bs, name, opts, flags, NULL, &local_err) < 0) {
Max Reitz34b5d2c2013-09-05 14:45:29 +020072 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
73 error_get_pretty(local_err));
74 error_free(local_err);
Fam Zheng4f6fd342013-08-23 09:14:47 +080075 bdrv_unref(qemuio_bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020076 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040077 return 1;
78 }
79 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020080
Devin Nakamura43642b32011-07-11 11:22:16 -040081 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000082}
83
Devin Nakamura43642b32011-07-11 11:22:16 -040084static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000085{
Devin Nakamura43642b32011-07-11 11:22:16 -040086 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000087"\n"
88" opens a new file in the requested mode\n"
89"\n"
90" Example:\n"
91" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
92"\n"
93" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000094" -r, -- open file read-only\n"
95" -s, -- use snapshot file\n"
96" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +020097" -g, -- allow file to grow (only applies to protocols)\n"
98" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +000099"\n");
100}
101
Kevin Wolf734c3b82013-06-05 14:19:30 +0200102static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000103
104static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400105 .name = "open",
106 .altname = "o",
107 .cfunc = open_f,
108 .argmin = 1,
109 .argmax = -1,
110 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200111 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400112 .oneline = "open the file specified by path",
113 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000114};
aliguorie3aff4f2009-04-05 19:14:04 +0000115
Max Reitzb543c5c2013-10-11 14:02:10 +0200116static QemuOptsList empty_opts = {
117 .name = "drive",
118 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
119 .desc = {
120 /* no elements => accept any params */
121 { /* end of list */ }
122 },
123};
124
Kevin Wolf734c3b82013-06-05 14:19:30 +0200125static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000126{
Devin Nakamura43642b32011-07-11 11:22:16 -0400127 int flags = 0;
128 int readonly = 0;
129 int growable = 0;
130 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200131 QemuOpts *qopts;
132 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000133
Max Reitzb543c5c2013-10-11 14:02:10 +0200134 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400135 switch (c) {
136 case 's':
137 flags |= BDRV_O_SNAPSHOT;
138 break;
139 case 'n':
140 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
141 break;
142 case 'r':
143 readonly = 1;
144 break;
145 case 'g':
146 growable = 1;
147 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200148 case 'o':
149 qopts = qemu_opts_parse(&empty_opts, optarg, 0);
150 if (qopts == NULL) {
151 printf("could not parse option list -- %s\n", optarg);
152 return 0;
153 }
154 opts = qemu_opts_to_qdict(qopts, opts);
155 qemu_opts_del(qopts);
156 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400157 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200158 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200159 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400160 }
aliguorie3aff4f2009-04-05 19:14:04 +0000161
Devin Nakamura43642b32011-07-11 11:22:16 -0400162 if (!readonly) {
163 flags |= BDRV_O_RDWR;
164 }
aliguorie3aff4f2009-04-05 19:14:04 +0000165
Devin Nakamura43642b32011-07-11 11:22:16 -0400166 if (optind != argc - 1) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200167 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400168 }
169
Max Reitzb543c5c2013-10-11 14:02:10 +0200170 return openfile(argv[optind], flags, growable, opts);
aliguorie3aff4f2009-04-05 19:14:04 +0000171}
172
Kevin Wolfe681be72013-06-05 14:19:34 +0200173static int quit_f(BlockDriverState *bs, int argc, char **argv)
174{
175 return 1;
176}
177
178static const cmdinfo_t quit_cmd = {
179 .name = "quit",
180 .altname = "q",
181 .cfunc = quit_f,
182 .argmin = -1,
183 .argmax = -1,
184 .flags = CMD_FLAG_GLOBAL,
185 .oneline = "exit the program",
186};
187
aliguorie3aff4f2009-04-05 19:14:04 +0000188static void usage(const char *name)
189{
Devin Nakamura43642b32011-07-11 11:22:16 -0400190 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100191"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200192"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000193"\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000194" -c, --cmd command to execute\n"
195" -r, --read-only export read-only\n"
196" -s, --snapshot use snapshot file\n"
197" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200198" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000199" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200200" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200201" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000202" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000203" -h, --help display this help and exit\n"
204" -V, --version output version information and exit\n"
205"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -0400206 name);
aliguorie3aff4f2009-04-05 19:14:04 +0000207}
208
Kevin Wolfd1174f12013-06-05 14:19:37 +0200209static char *get_prompt(void)
210{
211 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
212
213 if (!prompt[0]) {
214 snprintf(prompt, sizeof(prompt), "%s> ", progname);
215 }
216
217 return prompt;
218}
219
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100220static void readline_printf_func(void *opaque, const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200221{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100222 va_list ap;
223 va_start(ap, fmt);
224 vprintf(fmt, ap);
225 va_end(ap);
226}
227
228static void readline_flush_func(void *opaque)
229{
230 fflush(stdout);
231}
232
233static void readline_func(void *opaque, const char *str, void *readline_opaque)
234{
235 char **line = readline_opaque;
236 *line = g_strdup(str);
237}
238
239static void readline_completion_func(void *opaque, const char *str)
240{
241 /* No command or argument completion implemented yet */
242}
243
244static char *fetchline_readline(void)
245{
246 char *line = NULL;
247
248 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
249 while (!line) {
250 int ch = getchar();
251 if (ch == EOF) {
252 break;
253 }
254 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200255 }
256 return line;
257}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200258
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100259#define MAXREADLINESZ 1024
260static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200261{
262 char *p, *line = g_malloc(MAXREADLINESZ);
263
264 if (!fgets(line, MAXREADLINESZ, stdin)) {
265 g_free(line);
266 return NULL;
267 }
268
269 p = line + strlen(line);
270 if (p != line && p[-1] == '\n') {
271 p[-1] = '\0';
272 }
273
274 return line;
275}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100276
277static char *fetchline(void)
278{
279 if (readline_state) {
280 return fetchline_readline();
281 } else {
282 return fetchline_fgets();
283 }
284}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200285
286static void prep_fetchline(void *opaque)
287{
288 int *fetchable = opaque;
289
290 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
291 *fetchable= 1;
292}
293
294static void command_loop(void)
295{
296 int i, done = 0, fetchable = 0, prompted = 0;
297 char *input;
298
299 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200300 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200301 }
302 if (cmdline) {
303 g_free(cmdline);
304 return;
305 }
306
307 while (!done) {
308 if (!prompted) {
309 printf("%s", get_prompt());
310 fflush(stdout);
311 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
312 prompted = 1;
313 }
314
315 main_loop_wait(false);
316
317 if (!fetchable) {
318 continue;
319 }
320
321 input = fetchline();
322 if (input == NULL) {
323 break;
324 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200325 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200326 g_free(input);
327
328 prompted = 0;
329 fetchable = 0;
330 }
331 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
332}
333
334static void add_user_command(char *optarg)
335{
336 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
337 cmdline[ncmdline-1] = optarg;
338}
339
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100340static void reenable_tty_echo(void)
341{
342 qemu_set_tty_echo(STDIN_FILENO, true);
343}
344
aliguorie3aff4f2009-04-05 19:14:04 +0000345int main(int argc, char **argv)
346{
Devin Nakamura43642b32011-07-11 11:22:16 -0400347 int readonly = 0;
348 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100349 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400350 const struct option lopt[] = {
351 { "help", 0, NULL, 'h' },
352 { "version", 0, NULL, 'V' },
353 { "offset", 1, NULL, 'o' },
354 { "cmd", 1, NULL, 'c' },
355 { "read-only", 0, NULL, 'r' },
356 { "snapshot", 0, NULL, 's' },
357 { "nocache", 0, NULL, 'n' },
358 { "misalign", 0, NULL, 'm' },
359 { "growable", 0, NULL, 'g' },
360 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100361 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200362 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000363 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400364 { NULL, 0, NULL, 0 }
365 };
366 int c;
367 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100368 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +0000369
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900370#ifdef CONFIG_POSIX
371 signal(SIGPIPE, SIG_IGN);
372#endif
373
Devin Nakamura43642b32011-07-11 11:22:16 -0400374 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000375
Devin Nakamura43642b32011-07-11 11:22:16 -0400376 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
377 switch (c) {
378 case 's':
379 flags |= BDRV_O_SNAPSHOT;
380 break;
381 case 'n':
382 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
383 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100384 case 'd':
385 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
386 error_report("Invalid discard option: %s", optarg);
387 exit(1);
388 }
389 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400390 case 'c':
391 add_user_command(optarg);
392 break;
393 case 'r':
394 readonly = 1;
395 break;
396 case 'm':
Kevin Wolf797ac582013-06-05 14:19:31 +0200397 qemuio_misalign = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400398 break;
399 case 'g':
400 growable = 1;
401 break;
402 case 'k':
403 flags |= BDRV_O_NATIVE_AIO;
404 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200405 case 't':
406 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
407 error_report("Invalid cache option: %s", optarg);
408 exit(1);
409 }
410 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000411 case 'T':
412 if (!trace_backend_init(optarg, NULL)) {
413 exit(1); /* error message will have been printed */
414 }
415 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400416 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200417 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400418 exit(0);
419 case 'h':
420 usage(progname);
421 exit(0);
422 default:
423 usage(progname);
424 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200425 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400426 }
aliguorie3aff4f2009-04-05 19:14:04 +0000427
Devin Nakamura43642b32011-07-11 11:22:16 -0400428 if ((argc - optind) > 1) {
429 usage(progname);
430 exit(1);
431 }
aliguorie3aff4f2009-04-05 19:14:04 +0000432
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800433 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100434 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800435
Devin Nakamura43642b32011-07-11 11:22:16 -0400436 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200437 qemuio_add_command(&quit_cmd);
438 qemuio_add_command(&open_cmd);
439 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400440
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100441 if (isatty(STDIN_FILENO)) {
442 readline_state = readline_init(readline_printf_func,
443 readline_flush_func,
444 NULL,
445 readline_completion_func);
446 qemu_set_tty_echo(STDIN_FILENO, false);
447 atexit(reenable_tty_echo);
448 }
449
Devin Nakamura43642b32011-07-11 11:22:16 -0400450 /* open the device */
451 if (!readonly) {
452 flags |= BDRV_O_RDWR;
453 }
454
455 if ((argc - optind) == 1) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200456 openfile(argv[optind], flags, growable, NULL);
Devin Nakamura43642b32011-07-11 11:22:16 -0400457 }
458 command_loop();
459
460 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000461 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400462 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000463 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400464
Kevin Wolf734c3b82013-06-05 14:19:30 +0200465 if (qemuio_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800466 bdrv_unref(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400467 }
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100468 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400469 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000470}