blob: b74ac450f6cefedf05a266fe9daffe1e2a874363 [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 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
Max Reitzfd0fee32013-12-20 19:28:20 +0100166 if (optind == argc - 1) {
167 return openfile(argv[optind], flags, growable, opts);
168 } else if (optind == argc) {
169 return openfile(NULL, flags, growable, opts);
170 } else {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200171 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400172 }
aliguorie3aff4f2009-04-05 19:14:04 +0000173}
174
Kevin Wolfe681be72013-06-05 14:19:34 +0200175static int quit_f(BlockDriverState *bs, int argc, char **argv)
176{
177 return 1;
178}
179
180static const cmdinfo_t quit_cmd = {
181 .name = "quit",
182 .altname = "q",
183 .cfunc = quit_f,
184 .argmin = -1,
185 .argmax = -1,
186 .flags = CMD_FLAG_GLOBAL,
187 .oneline = "exit the program",
188};
189
aliguorie3aff4f2009-04-05 19:14:04 +0000190static void usage(const char *name)
191{
Devin Nakamura43642b32011-07-11 11:22:16 -0400192 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100193"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200194"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000195"\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000196" -c, --cmd command to execute\n"
197" -r, --read-only export read-only\n"
198" -s, --snapshot use snapshot file\n"
199" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200200" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000201" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200202" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200203" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000204" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000205" -h, --help display this help and exit\n"
206" -V, --version output version information and exit\n"
207"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -0400208 name);
aliguorie3aff4f2009-04-05 19:14:04 +0000209}
210
Kevin Wolfd1174f12013-06-05 14:19:37 +0200211static char *get_prompt(void)
212{
213 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
214
215 if (!prompt[0]) {
216 snprintf(prompt, sizeof(prompt), "%s> ", progname);
217 }
218
219 return prompt;
220}
221
Stefan Weild5d15072014-01-25 18:18:23 +0100222static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
223 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200224{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100225 va_list ap;
226 va_start(ap, fmt);
227 vprintf(fmt, ap);
228 va_end(ap);
229}
230
231static void readline_flush_func(void *opaque)
232{
233 fflush(stdout);
234}
235
236static void readline_func(void *opaque, const char *str, void *readline_opaque)
237{
238 char **line = readline_opaque;
239 *line = g_strdup(str);
240}
241
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100242static void completion_match(const char *cmd, void *opaque)
243{
244 readline_add_completion(readline_state, cmd);
245}
246
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100247static void readline_completion_func(void *opaque, const char *str)
248{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100249 readline_set_completion_index(readline_state, strlen(str));
250 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100251}
252
253static char *fetchline_readline(void)
254{
255 char *line = NULL;
256
257 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
258 while (!line) {
259 int ch = getchar();
260 if (ch == EOF) {
261 break;
262 }
263 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200264 }
265 return line;
266}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200267
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100268#define MAXREADLINESZ 1024
269static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200270{
271 char *p, *line = g_malloc(MAXREADLINESZ);
272
273 if (!fgets(line, MAXREADLINESZ, stdin)) {
274 g_free(line);
275 return NULL;
276 }
277
278 p = line + strlen(line);
279 if (p != line && p[-1] == '\n') {
280 p[-1] = '\0';
281 }
282
283 return line;
284}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100285
286static char *fetchline(void)
287{
288 if (readline_state) {
289 return fetchline_readline();
290 } else {
291 return fetchline_fgets();
292 }
293}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200294
295static void prep_fetchline(void *opaque)
296{
297 int *fetchable = opaque;
298
299 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
300 *fetchable= 1;
301}
302
303static void command_loop(void)
304{
305 int i, done = 0, fetchable = 0, prompted = 0;
306 char *input;
307
308 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200309 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200310 }
311 if (cmdline) {
312 g_free(cmdline);
313 return;
314 }
315
316 while (!done) {
317 if (!prompted) {
318 printf("%s", get_prompt());
319 fflush(stdout);
320 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
321 prompted = 1;
322 }
323
324 main_loop_wait(false);
325
326 if (!fetchable) {
327 continue;
328 }
329
330 input = fetchline();
331 if (input == NULL) {
332 break;
333 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200334 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200335 g_free(input);
336
337 prompted = 0;
338 fetchable = 0;
339 }
340 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
341}
342
343static void add_user_command(char *optarg)
344{
345 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
346 cmdline[ncmdline-1] = optarg;
347}
348
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100349static void reenable_tty_echo(void)
350{
351 qemu_set_tty_echo(STDIN_FILENO, true);
352}
353
aliguorie3aff4f2009-04-05 19:14:04 +0000354int main(int argc, char **argv)
355{
Devin Nakamura43642b32011-07-11 11:22:16 -0400356 int readonly = 0;
357 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100358 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400359 const struct option lopt[] = {
360 { "help", 0, NULL, 'h' },
361 { "version", 0, NULL, 'V' },
362 { "offset", 1, NULL, 'o' },
363 { "cmd", 1, NULL, 'c' },
364 { "read-only", 0, NULL, 'r' },
365 { "snapshot", 0, NULL, 's' },
366 { "nocache", 0, NULL, 'n' },
367 { "misalign", 0, NULL, 'm' },
368 { "growable", 0, NULL, 'g' },
369 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100370 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200371 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000372 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400373 { NULL, 0, NULL, 0 }
374 };
375 int c;
376 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100377 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +0000378
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900379#ifdef CONFIG_POSIX
380 signal(SIGPIPE, SIG_IGN);
381#endif
382
Devin Nakamura43642b32011-07-11 11:22:16 -0400383 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800384 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000385
Devin Nakamura43642b32011-07-11 11:22:16 -0400386 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
387 switch (c) {
388 case 's':
389 flags |= BDRV_O_SNAPSHOT;
390 break;
391 case 'n':
392 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
393 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100394 case 'd':
395 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
396 error_report("Invalid discard option: %s", optarg);
397 exit(1);
398 }
399 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400400 case 'c':
401 add_user_command(optarg);
402 break;
403 case 'r':
404 readonly = 1;
405 break;
406 case 'm':
Kevin Wolf797ac582013-06-05 14:19:31 +0200407 qemuio_misalign = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400408 break;
409 case 'g':
410 growable = 1;
411 break;
412 case 'k':
413 flags |= BDRV_O_NATIVE_AIO;
414 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200415 case 't':
416 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
417 error_report("Invalid cache option: %s", optarg);
418 exit(1);
419 }
420 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000421 case 'T':
422 if (!trace_backend_init(optarg, NULL)) {
423 exit(1); /* error message will have been printed */
424 }
425 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400426 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200427 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400428 exit(0);
429 case 'h':
430 usage(progname);
431 exit(0);
432 default:
433 usage(progname);
434 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200435 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400436 }
aliguorie3aff4f2009-04-05 19:14:04 +0000437
Devin Nakamura43642b32011-07-11 11:22:16 -0400438 if ((argc - optind) > 1) {
439 usage(progname);
440 exit(1);
441 }
aliguorie3aff4f2009-04-05 19:14:04 +0000442
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800443 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100444 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800445
Devin Nakamura43642b32011-07-11 11:22:16 -0400446 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200447 qemuio_add_command(&quit_cmd);
448 qemuio_add_command(&open_cmd);
449 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400450
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100451 if (isatty(STDIN_FILENO)) {
452 readline_state = readline_init(readline_printf_func,
453 readline_flush_func,
454 NULL,
455 readline_completion_func);
456 qemu_set_tty_echo(STDIN_FILENO, false);
457 atexit(reenable_tty_echo);
458 }
459
Devin Nakamura43642b32011-07-11 11:22:16 -0400460 /* open the device */
461 if (!readonly) {
462 flags |= BDRV_O_RDWR;
463 }
464
465 if ((argc - optind) == 1) {
Max Reitzb543c5c2013-10-11 14:02:10 +0200466 openfile(argv[optind], flags, growable, NULL);
Devin Nakamura43642b32011-07-11 11:22:16 -0400467 }
468 command_loop();
469
470 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000471 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400472 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000473 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400474
Kevin Wolf734c3b82013-06-05 14:19:30 +0200475 if (qemuio_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800476 bdrv_unref(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400477 }
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100478 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400479 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000480}