blob: fc3860884c2a017d3fe297cdab2bde606886e34c [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 Reitz2e401342014-02-18 18:33:07 +010062 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags | BDRV_O_PROTOCOL,
63 NULL, &local_err))
64 {
Max Reitz34b5d2c2013-09-05 14:45:29 +020065 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
66 error_get_pretty(local_err));
67 error_free(local_err);
Devin Nakamura43642b32011-07-11 11:22:16 -040068 return 1;
69 }
70 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +020071 qemuio_bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +020072
Max Reitzddf56362014-02-18 18:33:06 +010073 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
74 < 0)
75 {
Max Reitz34b5d2c2013-09-05 14:45:29 +020076 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
77 error_get_pretty(local_err));
78 error_free(local_err);
Fam Zheng4f6fd342013-08-23 09:14:47 +080079 bdrv_unref(qemuio_bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020080 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040081 return 1;
82 }
83 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020084
Devin Nakamura43642b32011-07-11 11:22:16 -040085 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000086}
87
Devin Nakamura43642b32011-07-11 11:22:16 -040088static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000089{
Devin Nakamura43642b32011-07-11 11:22:16 -040090 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000091"\n"
92" opens a new file in the requested mode\n"
93"\n"
94" Example:\n"
95" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
96"\n"
97" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000098" -r, -- open file read-only\n"
99" -s, -- use snapshot file\n"
100" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200101" -g, -- allow file to grow (only applies to protocols)\n"
102" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000103"\n");
104}
105
Kevin Wolf734c3b82013-06-05 14:19:30 +0200106static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000107
108static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400109 .name = "open",
110 .altname = "o",
111 .cfunc = open_f,
112 .argmin = 1,
113 .argmax = -1,
114 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200115 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400116 .oneline = "open the file specified by path",
117 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000118};
aliguorie3aff4f2009-04-05 19:14:04 +0000119
Max Reitzb543c5c2013-10-11 14:02:10 +0200120static QemuOptsList empty_opts = {
121 .name = "drive",
122 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
123 .desc = {
124 /* no elements => accept any params */
125 { /* end of list */ }
126 },
127};
128
Kevin Wolf734c3b82013-06-05 14:19:30 +0200129static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000130{
Devin Nakamura43642b32011-07-11 11:22:16 -0400131 int flags = 0;
132 int readonly = 0;
133 int growable = 0;
134 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200135 QemuOpts *qopts;
136 QDict *opts = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000137
Max Reitzb543c5c2013-10-11 14:02:10 +0200138 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400139 switch (c) {
140 case 's':
141 flags |= BDRV_O_SNAPSHOT;
142 break;
143 case 'n':
144 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
145 break;
146 case 'r':
147 readonly = 1;
148 break;
149 case 'g':
150 growable = 1;
151 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200152 case 'o':
153 qopts = qemu_opts_parse(&empty_opts, optarg, 0);
154 if (qopts == NULL) {
155 printf("could not parse option list -- %s\n", optarg);
156 return 0;
157 }
158 opts = qemu_opts_to_qdict(qopts, opts);
159 qemu_opts_del(qopts);
160 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400161 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200162 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200163 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400164 }
aliguorie3aff4f2009-04-05 19:14:04 +0000165
Devin Nakamura43642b32011-07-11 11:22:16 -0400166 if (!readonly) {
167 flags |= BDRV_O_RDWR;
168 }
aliguorie3aff4f2009-04-05 19:14:04 +0000169
Max Reitzfd0fee32013-12-20 19:28:20 +0100170 if (optind == argc - 1) {
171 return openfile(argv[optind], flags, growable, opts);
172 } else if (optind == argc) {
173 return openfile(NULL, flags, growable, opts);
174 } else {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200175 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400176 }
aliguorie3aff4f2009-04-05 19:14:04 +0000177}
178
Kevin Wolfe681be72013-06-05 14:19:34 +0200179static int quit_f(BlockDriverState *bs, int argc, char **argv)
180{
181 return 1;
182}
183
184static const cmdinfo_t quit_cmd = {
185 .name = "quit",
186 .altname = "q",
187 .cfunc = quit_f,
188 .argmin = -1,
189 .argmax = -1,
190 .flags = CMD_FLAG_GLOBAL,
191 .oneline = "exit the program",
192};
193
aliguorie3aff4f2009-04-05 19:14:04 +0000194static void usage(const char *name)
195{
Devin Nakamura43642b32011-07-11 11:22:16 -0400196 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100197"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200198"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000199"\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000200" -c, --cmd command to execute\n"
201" -r, --read-only export read-only\n"
202" -s, --snapshot use snapshot file\n"
203" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200204" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000205" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200206" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200207" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000208" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000209" -h, --help display this help and exit\n"
210" -V, --version output version information and exit\n"
211"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -0400212 name);
aliguorie3aff4f2009-04-05 19:14:04 +0000213}
214
Kevin Wolfd1174f12013-06-05 14:19:37 +0200215static char *get_prompt(void)
216{
217 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
218
219 if (!prompt[0]) {
220 snprintf(prompt, sizeof(prompt), "%s> ", progname);
221 }
222
223 return prompt;
224}
225
Stefan Weild5d15072014-01-25 18:18:23 +0100226static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
227 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200228{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100229 va_list ap;
230 va_start(ap, fmt);
231 vprintf(fmt, ap);
232 va_end(ap);
233}
234
235static void readline_flush_func(void *opaque)
236{
237 fflush(stdout);
238}
239
240static void readline_func(void *opaque, const char *str, void *readline_opaque)
241{
242 char **line = readline_opaque;
243 *line = g_strdup(str);
244}
245
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100246static void completion_match(const char *cmd, void *opaque)
247{
248 readline_add_completion(readline_state, cmd);
249}
250
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100251static void readline_completion_func(void *opaque, const char *str)
252{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100253 readline_set_completion_index(readline_state, strlen(str));
254 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100255}
256
257static char *fetchline_readline(void)
258{
259 char *line = NULL;
260
261 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
262 while (!line) {
263 int ch = getchar();
264 if (ch == EOF) {
265 break;
266 }
267 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200268 }
269 return line;
270}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200271
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100272#define MAXREADLINESZ 1024
273static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200274{
275 char *p, *line = g_malloc(MAXREADLINESZ);
276
277 if (!fgets(line, MAXREADLINESZ, stdin)) {
278 g_free(line);
279 return NULL;
280 }
281
282 p = line + strlen(line);
283 if (p != line && p[-1] == '\n') {
284 p[-1] = '\0';
285 }
286
287 return line;
288}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100289
290static char *fetchline(void)
291{
292 if (readline_state) {
293 return fetchline_readline();
294 } else {
295 return fetchline_fgets();
296 }
297}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200298
299static void prep_fetchline(void *opaque)
300{
301 int *fetchable = opaque;
302
303 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
304 *fetchable= 1;
305}
306
307static void command_loop(void)
308{
309 int i, done = 0, fetchable = 0, prompted = 0;
310 char *input;
311
312 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200313 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200314 }
315 if (cmdline) {
316 g_free(cmdline);
317 return;
318 }
319
320 while (!done) {
321 if (!prompted) {
322 printf("%s", get_prompt());
323 fflush(stdout);
324 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
325 prompted = 1;
326 }
327
328 main_loop_wait(false);
329
330 if (!fetchable) {
331 continue;
332 }
333
334 input = fetchline();
335 if (input == NULL) {
336 break;
337 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200338 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200339 g_free(input);
340
341 prompted = 0;
342 fetchable = 0;
343 }
344 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
345}
346
347static void add_user_command(char *optarg)
348{
349 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
350 cmdline[ncmdline-1] = optarg;
351}
352
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100353static void reenable_tty_echo(void)
354{
355 qemu_set_tty_echo(STDIN_FILENO, true);
356}
357
aliguorie3aff4f2009-04-05 19:14:04 +0000358int main(int argc, char **argv)
359{
Devin Nakamura43642b32011-07-11 11:22:16 -0400360 int readonly = 0;
361 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100362 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400363 const struct option lopt[] = {
364 { "help", 0, NULL, 'h' },
365 { "version", 0, NULL, 'V' },
366 { "offset", 1, NULL, 'o' },
367 { "cmd", 1, NULL, 'c' },
368 { "read-only", 0, NULL, 'r' },
369 { "snapshot", 0, NULL, 's' },
370 { "nocache", 0, NULL, 'n' },
371 { "misalign", 0, NULL, 'm' },
372 { "growable", 0, NULL, 'g' },
373 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100374 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200375 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000376 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400377 { NULL, 0, NULL, 0 }
378 };
379 int c;
380 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100381 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +0000382
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900383#ifdef CONFIG_POSIX
384 signal(SIGPIPE, SIG_IGN);
385#endif
386
Devin Nakamura43642b32011-07-11 11:22:16 -0400387 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800388 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000389
Devin Nakamura43642b32011-07-11 11:22:16 -0400390 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
391 switch (c) {
392 case 's':
393 flags |= BDRV_O_SNAPSHOT;
394 break;
395 case 'n':
396 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
397 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100398 case 'd':
399 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
400 error_report("Invalid discard option: %s", optarg);
401 exit(1);
402 }
403 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400404 case 'c':
405 add_user_command(optarg);
406 break;
407 case 'r':
408 readonly = 1;
409 break;
410 case 'm':
Kevin Wolf797ac582013-06-05 14:19:31 +0200411 qemuio_misalign = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400412 break;
413 case 'g':
414 growable = 1;
415 break;
416 case 'k':
417 flags |= BDRV_O_NATIVE_AIO;
418 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200419 case 't':
420 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
421 error_report("Invalid cache option: %s", optarg);
422 exit(1);
423 }
424 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000425 case 'T':
426 if (!trace_backend_init(optarg, NULL)) {
427 exit(1); /* error message will have been printed */
428 }
429 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400430 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200431 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400432 exit(0);
433 case 'h':
434 usage(progname);
435 exit(0);
436 default:
437 usage(progname);
438 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200439 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400440 }
aliguorie3aff4f2009-04-05 19:14:04 +0000441
Devin Nakamura43642b32011-07-11 11:22:16 -0400442 if ((argc - optind) > 1) {
443 usage(progname);
444 exit(1);
445 }
aliguorie3aff4f2009-04-05 19:14:04 +0000446
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800447 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100448 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800449
Devin Nakamura43642b32011-07-11 11:22:16 -0400450 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200451 qemuio_add_command(&quit_cmd);
452 qemuio_add_command(&open_cmd);
453 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400454
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100455 if (isatty(STDIN_FILENO)) {
456 readline_state = readline_init(readline_printf_func,
457 readline_flush_func,
458 NULL,
459 readline_completion_func);
460 qemu_set_tty_echo(STDIN_FILENO, false);
461 atexit(reenable_tty_echo);
462 }
463
Devin Nakamura43642b32011-07-11 11:22:16 -0400464 /* open the device */
465 if (!readonly) {
466 flags |= BDRV_O_RDWR;
467 }
468
469 if ((argc - optind) == 1) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200470 openfile(argv[optind], flags, growable, NULL);
Devin Nakamura43642b32011-07-11 11:22:16 -0400471 }
472 command_loop();
473
474 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000475 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400476 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000477 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400478
Kevin Wolf734c3b82013-06-05 14:19:30 +0200479 if (qemuio_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800480 bdrv_unref(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400481 }
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100482 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400483 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000484}