blob: eec8cbc9f2a6b3292c75cf2115b797dd613e26c1 [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
17#include "qemu-common.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010018#include "qemu/main-loop.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/block_int.h"
aliguorie3aff4f2009-04-05 19:14:04 +000020#include "cmd.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000021#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000022
23#define VERSION "0.0.1"
24
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
Kevin Wolf734c3b82013-06-05 14:19:30 +020036static int close_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000037{
Stefan Hajnoczib4657852011-10-27 10:54:26 +010038 bdrv_delete(bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020039 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040040 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000041}
42
43static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040044 .name = "close",
45 .altname = "c",
46 .cfunc = close_f,
47 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000048};
49
Christoph Hellwig9c4bab22009-07-10 13:33:47 +020050static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +000051{
Kevin Wolf734c3b82013-06-05 14:19:30 +020052 if (qemuio_bs) {
Devin Nakamura43642b32011-07-11 11:22:16 -040053 fprintf(stderr, "file open already, try 'help close'\n");
54 return 1;
55 }
aliguorie3aff4f2009-04-05 19:14:04 +000056
Devin Nakamura43642b32011-07-11 11:22:16 -040057 if (growable) {
Kevin Wolf734c3b82013-06-05 14:19:30 +020058 if (bdrv_file_open(&qemuio_bs, name, NULL, flags)) {
Devin Nakamura43642b32011-07-11 11:22:16 -040059 fprintf(stderr, "%s: can't open device %s\n", progname, name);
60 return 1;
61 }
62 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +020063 qemuio_bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +020064
Kevin Wolf734c3b82013-06-05 14:19:30 +020065 if (bdrv_open(qemuio_bs, name, NULL, flags, NULL) < 0) {
Devin Nakamura43642b32011-07-11 11:22:16 -040066 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Kevin Wolf734c3b82013-06-05 14:19:30 +020067 bdrv_delete(qemuio_bs);
68 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040069 return 1;
70 }
71 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020072
Devin Nakamura43642b32011-07-11 11:22:16 -040073 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000074}
75
Devin Nakamura43642b32011-07-11 11:22:16 -040076static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000077{
Devin Nakamura43642b32011-07-11 11:22:16 -040078 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000079"\n"
80" opens a new file in the requested mode\n"
81"\n"
82" Example:\n"
83" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
84"\n"
85" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000086" -r, -- open file read-only\n"
87" -s, -- use snapshot file\n"
88" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +020089" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +000090"\n");
91}
92
Kevin Wolf734c3b82013-06-05 14:19:30 +020093static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +000094
95static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040096 .name = "open",
97 .altname = "o",
98 .cfunc = open_f,
99 .argmin = 1,
100 .argmax = -1,
101 .flags = CMD_NOFILE_OK,
102 .args = "[-Crsn] [path]",
103 .oneline = "open the file specified by path",
104 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000105};
aliguorie3aff4f2009-04-05 19:14:04 +0000106
Kevin Wolf734c3b82013-06-05 14:19:30 +0200107static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000108{
Devin Nakamura43642b32011-07-11 11:22:16 -0400109 int flags = 0;
110 int readonly = 0;
111 int growable = 0;
112 int c;
aliguorie3aff4f2009-04-05 19:14:04 +0000113
Devin Nakamura43642b32011-07-11 11:22:16 -0400114 while ((c = getopt(argc, argv, "snrg")) != EOF) {
115 switch (c) {
116 case 's':
117 flags |= BDRV_O_SNAPSHOT;
118 break;
119 case 'n':
120 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
121 break;
122 case 'r':
123 readonly = 1;
124 break;
125 case 'g':
126 growable = 1;
127 break;
128 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200129 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200130 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400131 }
aliguorie3aff4f2009-04-05 19:14:04 +0000132
Devin Nakamura43642b32011-07-11 11:22:16 -0400133 if (!readonly) {
134 flags |= BDRV_O_RDWR;
135 }
aliguorie3aff4f2009-04-05 19:14:04 +0000136
Devin Nakamura43642b32011-07-11 11:22:16 -0400137 if (optind != argc - 1) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200138 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400139 }
140
141 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +0000142}
143
Kevin Wolfe681be72013-06-05 14:19:34 +0200144static int quit_f(BlockDriverState *bs, int argc, char **argv)
145{
146 return 1;
147}
148
149static const cmdinfo_t quit_cmd = {
150 .name = "quit",
151 .altname = "q",
152 .cfunc = quit_f,
153 .argmin = -1,
154 .argmax = -1,
155 .flags = CMD_FLAG_GLOBAL,
156 .oneline = "exit the program",
157};
158
aliguorie3aff4f2009-04-05 19:14:04 +0000159static void usage(const char *name)
160{
Devin Nakamura43642b32011-07-11 11:22:16 -0400161 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100162"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200163"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000164"\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000165" -c, --cmd command to execute\n"
166" -r, --read-only export read-only\n"
167" -s, --snapshot use snapshot file\n"
168" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200169" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000170" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200171" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200172" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000173" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000174" -h, --help display this help and exit\n"
175" -V, --version output version information and exit\n"
176"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -0400177 name);
aliguorie3aff4f2009-04-05 19:14:04 +0000178}
179
180
Kevin Wolfd1174f12013-06-05 14:19:37 +0200181#if defined(ENABLE_READLINE)
182# include <readline/history.h>
183# include <readline/readline.h>
184#elif defined(ENABLE_EDITLINE)
185# include <histedit.h>
186#endif
187
188static char *get_prompt(void)
189{
190 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
191
192 if (!prompt[0]) {
193 snprintf(prompt, sizeof(prompt), "%s> ", progname);
194 }
195
196 return prompt;
197}
198
199#if defined(ENABLE_READLINE)
200static char *fetchline(void)
201{
202 char *line = readline(get_prompt());
203 if (line && *line) {
204 add_history(line);
205 }
206 return line;
207}
208#elif defined(ENABLE_EDITLINE)
209static char *el_get_prompt(EditLine *e)
210{
211 return get_prompt();
212}
213
214static char *fetchline(void)
215{
216 static EditLine *el;
217 static History *hist;
218 HistEvent hevent;
219 char *line;
220 int count;
221
222 if (!el) {
223 hist = history_init();
224 history(hist, &hevent, H_SETSIZE, 100);
225 el = el_init(progname, stdin, stdout, stderr);
226 el_source(el, NULL);
227 el_set(el, EL_SIGNAL, 1);
228 el_set(el, EL_PROMPT, el_get_prompt);
229 el_set(el, EL_HIST, history, (const char *)hist);
230 }
231 line = strdup(el_gets(el, &count));
232 if (line) {
233 if (count > 0) {
234 line[count-1] = '\0';
235 }
236 if (*line) {
237 history(hist, &hevent, H_ENTER, line);
238 }
239 }
240 return line;
241}
242#else
243# define MAXREADLINESZ 1024
244static char *fetchline(void)
245{
246 char *p, *line = g_malloc(MAXREADLINESZ);
247
248 if (!fgets(line, MAXREADLINESZ, stdin)) {
249 g_free(line);
250 return NULL;
251 }
252
253 p = line + strlen(line);
254 if (p != line && p[-1] == '\n') {
255 p[-1] = '\0';
256 }
257
258 return line;
259}
260#endif
261
262static void prep_fetchline(void *opaque)
263{
264 int *fetchable = opaque;
265
266 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
267 *fetchable= 1;
268}
269
270static void command_loop(void)
271{
272 int i, done = 0, fetchable = 0, prompted = 0;
273 char *input;
274
275 for (i = 0; !done && i < ncmdline; i++) {
276 done = qemuio_command(cmdline[i]);
277 }
278 if (cmdline) {
279 g_free(cmdline);
280 return;
281 }
282
283 while (!done) {
284 if (!prompted) {
285 printf("%s", get_prompt());
286 fflush(stdout);
287 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
288 prompted = 1;
289 }
290
291 main_loop_wait(false);
292
293 if (!fetchable) {
294 continue;
295 }
296
297 input = fetchline();
298 if (input == NULL) {
299 break;
300 }
301 done = qemuio_command(input);
302 g_free(input);
303
304 prompted = 0;
305 fetchable = 0;
306 }
307 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
308}
309
310static void add_user_command(char *optarg)
311{
312 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
313 cmdline[ncmdline-1] = optarg;
314}
315
aliguorie3aff4f2009-04-05 19:14:04 +0000316int main(int argc, char **argv)
317{
Devin Nakamura43642b32011-07-11 11:22:16 -0400318 int readonly = 0;
319 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100320 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400321 const struct option lopt[] = {
322 { "help", 0, NULL, 'h' },
323 { "version", 0, NULL, 'V' },
324 { "offset", 1, NULL, 'o' },
325 { "cmd", 1, NULL, 'c' },
326 { "read-only", 0, NULL, 'r' },
327 { "snapshot", 0, NULL, 's' },
328 { "nocache", 0, NULL, 'n' },
329 { "misalign", 0, NULL, 'm' },
330 { "growable", 0, NULL, 'g' },
331 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100332 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200333 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000334 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400335 { NULL, 0, NULL, 0 }
336 };
337 int c;
338 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100339 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +0000340
Devin Nakamura43642b32011-07-11 11:22:16 -0400341 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000342
Devin Nakamura43642b32011-07-11 11:22:16 -0400343 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
344 switch (c) {
345 case 's':
346 flags |= BDRV_O_SNAPSHOT;
347 break;
348 case 'n':
349 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
350 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100351 case 'd':
352 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
353 error_report("Invalid discard option: %s", optarg);
354 exit(1);
355 }
356 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400357 case 'c':
358 add_user_command(optarg);
359 break;
360 case 'r':
361 readonly = 1;
362 break;
363 case 'm':
Kevin Wolf797ac582013-06-05 14:19:31 +0200364 qemuio_misalign = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400365 break;
366 case 'g':
367 growable = 1;
368 break;
369 case 'k':
370 flags |= BDRV_O_NATIVE_AIO;
371 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200372 case 't':
373 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
374 error_report("Invalid cache option: %s", optarg);
375 exit(1);
376 }
377 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000378 case 'T':
379 if (!trace_backend_init(optarg, NULL)) {
380 exit(1); /* error message will have been printed */
381 }
382 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400383 case 'V':
384 printf("%s version %s\n", progname, VERSION);
385 exit(0);
386 case 'h':
387 usage(progname);
388 exit(0);
389 default:
390 usage(progname);
391 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200392 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400393 }
aliguorie3aff4f2009-04-05 19:14:04 +0000394
Devin Nakamura43642b32011-07-11 11:22:16 -0400395 if ((argc - optind) > 1) {
396 usage(progname);
397 exit(1);
398 }
aliguorie3aff4f2009-04-05 19:14:04 +0000399
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800400 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100401 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800402
Devin Nakamura43642b32011-07-11 11:22:16 -0400403 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200404 qemuio_add_command(&quit_cmd);
405 qemuio_add_command(&open_cmd);
406 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400407
408 /* open the device */
409 if (!readonly) {
410 flags |= BDRV_O_RDWR;
411 }
412
413 if ((argc - optind) == 1) {
414 openfile(argv[optind], flags, growable);
415 }
416 command_loop();
417
418 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000419 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400420 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000421 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400422
Kevin Wolf734c3b82013-06-05 14:19:30 +0200423 if (qemuio_bs) {
424 bdrv_delete(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400425 }
426 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000427}