blob: f4b8efccebd44b284185110097695008ddb83cc4 [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"
Paolo Bonzini737e1502012-12-17 18:19:44 +010019#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000020#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000021
Devin Nakamura43642b32011-07-11 11:22:16 -040022#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000023
24char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000025
Kevin Wolf734c3b82013-06-05 14:19:30 +020026BlockDriverState *qemuio_bs;
Kevin Wolf797ac582013-06-05 14:19:31 +020027extern int qemuio_misalign;
Kevin Wolf191c2892010-09-16 13:18:08 +020028
Kevin Wolfd1174f12013-06-05 14:19:37 +020029/* qemu-io commands passed using -c */
30static int ncmdline;
31static char **cmdline;
32
Kevin Wolf734c3b82013-06-05 14:19:30 +020033static int close_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000034{
Fam Zheng4f6fd342013-08-23 09:14:47 +080035 bdrv_unref(bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020036 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040037 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000038}
39
40static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040041 .name = "close",
42 .altname = "c",
43 .cfunc = close_f,
44 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000045};
46
Christoph Hellwig9c4bab22009-07-10 13:33:47 +020047static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +000048{
Max Reitz34b5d2c2013-09-05 14:45:29 +020049 Error *local_err = NULL;
50
Kevin Wolf734c3b82013-06-05 14:19:30 +020051 if (qemuio_bs) {
Devin Nakamura43642b32011-07-11 11:22:16 -040052 fprintf(stderr, "file open already, try 'help close'\n");
53 return 1;
54 }
aliguorie3aff4f2009-04-05 19:14:04 +000055
Devin Nakamura43642b32011-07-11 11:22:16 -040056 if (growable) {
Max Reitz34b5d2c2013-09-05 14:45:29 +020057 if (bdrv_file_open(&qemuio_bs, name, NULL, flags, &local_err)) {
58 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
59 error_get_pretty(local_err));
60 error_free(local_err);
Devin Nakamura43642b32011-07-11 11:22:16 -040061 return 1;
62 }
63 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +020064 qemuio_bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +020065
Max Reitz34b5d2c2013-09-05 14:45:29 +020066 if (bdrv_open(qemuio_bs, name, NULL, flags, NULL, &local_err) < 0) {
67 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
68 error_get_pretty(local_err));
69 error_free(local_err);
Fam Zheng4f6fd342013-08-23 09:14:47 +080070 bdrv_unref(qemuio_bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020071 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040072 return 1;
73 }
74 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020075
Devin Nakamura43642b32011-07-11 11:22:16 -040076 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000077}
78
Devin Nakamura43642b32011-07-11 11:22:16 -040079static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000080{
Devin Nakamura43642b32011-07-11 11:22:16 -040081 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000082"\n"
83" opens a new file in the requested mode\n"
84"\n"
85" Example:\n"
86" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
87"\n"
88" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000089" -r, -- open file read-only\n"
90" -s, -- use snapshot file\n"
91" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +020092" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +000093"\n");
94}
95
Kevin Wolf734c3b82013-06-05 14:19:30 +020096static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +000097
98static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040099 .name = "open",
100 .altname = "o",
101 .cfunc = open_f,
102 .argmin = 1,
103 .argmax = -1,
104 .flags = CMD_NOFILE_OK,
105 .args = "[-Crsn] [path]",
106 .oneline = "open the file specified by path",
107 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000108};
aliguorie3aff4f2009-04-05 19:14:04 +0000109
Kevin Wolf734c3b82013-06-05 14:19:30 +0200110static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000111{
Devin Nakamura43642b32011-07-11 11:22:16 -0400112 int flags = 0;
113 int readonly = 0;
114 int growable = 0;
115 int c;
aliguorie3aff4f2009-04-05 19:14:04 +0000116
Devin Nakamura43642b32011-07-11 11:22:16 -0400117 while ((c = getopt(argc, argv, "snrg")) != EOF) {
118 switch (c) {
119 case 's':
120 flags |= BDRV_O_SNAPSHOT;
121 break;
122 case 'n':
123 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
124 break;
125 case 'r':
126 readonly = 1;
127 break;
128 case 'g':
129 growable = 1;
130 break;
131 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200132 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200133 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400134 }
aliguorie3aff4f2009-04-05 19:14:04 +0000135
Devin Nakamura43642b32011-07-11 11:22:16 -0400136 if (!readonly) {
137 flags |= BDRV_O_RDWR;
138 }
aliguorie3aff4f2009-04-05 19:14:04 +0000139
Devin Nakamura43642b32011-07-11 11:22:16 -0400140 if (optind != argc - 1) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200141 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400142 }
143
144 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +0000145}
146
Kevin Wolfe681be72013-06-05 14:19:34 +0200147static int quit_f(BlockDriverState *bs, int argc, char **argv)
148{
149 return 1;
150}
151
152static const cmdinfo_t quit_cmd = {
153 .name = "quit",
154 .altname = "q",
155 .cfunc = quit_f,
156 .argmin = -1,
157 .argmax = -1,
158 .flags = CMD_FLAG_GLOBAL,
159 .oneline = "exit the program",
160};
161
aliguorie3aff4f2009-04-05 19:14:04 +0000162static void usage(const char *name)
163{
Devin Nakamura43642b32011-07-11 11:22:16 -0400164 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100165"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200166"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000167"\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000168" -c, --cmd command to execute\n"
169" -r, --read-only export read-only\n"
170" -s, --snapshot use snapshot file\n"
171" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200172" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000173" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200174" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200175" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000176" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000177" -h, --help display this help and exit\n"
178" -V, --version output version information and exit\n"
179"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -0400180 name);
aliguorie3aff4f2009-04-05 19:14:04 +0000181}
182
183
Kevin Wolfd1174f12013-06-05 14:19:37 +0200184#if defined(ENABLE_READLINE)
185# include <readline/history.h>
186# include <readline/readline.h>
187#elif defined(ENABLE_EDITLINE)
188# include <histedit.h>
189#endif
190
191static char *get_prompt(void)
192{
193 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
194
195 if (!prompt[0]) {
196 snprintf(prompt, sizeof(prompt), "%s> ", progname);
197 }
198
199 return prompt;
200}
201
202#if defined(ENABLE_READLINE)
203static char *fetchline(void)
204{
205 char *line = readline(get_prompt());
206 if (line && *line) {
207 add_history(line);
208 }
209 return line;
210}
211#elif defined(ENABLE_EDITLINE)
212static char *el_get_prompt(EditLine *e)
213{
214 return get_prompt();
215}
216
217static char *fetchline(void)
218{
219 static EditLine *el;
220 static History *hist;
221 HistEvent hevent;
222 char *line;
223 int count;
224
225 if (!el) {
226 hist = history_init();
227 history(hist, &hevent, H_SETSIZE, 100);
228 el = el_init(progname, stdin, stdout, stderr);
229 el_source(el, NULL);
230 el_set(el, EL_SIGNAL, 1);
231 el_set(el, EL_PROMPT, el_get_prompt);
232 el_set(el, EL_HIST, history, (const char *)hist);
233 }
234 line = strdup(el_gets(el, &count));
235 if (line) {
236 if (count > 0) {
237 line[count-1] = '\0';
238 }
239 if (*line) {
240 history(hist, &hevent, H_ENTER, line);
241 }
242 }
243 return line;
244}
245#else
246# define MAXREADLINESZ 1024
247static char *fetchline(void)
248{
249 char *p, *line = g_malloc(MAXREADLINESZ);
250
251 if (!fgets(line, MAXREADLINESZ, stdin)) {
252 g_free(line);
253 return NULL;
254 }
255
256 p = line + strlen(line);
257 if (p != line && p[-1] == '\n') {
258 p[-1] = '\0';
259 }
260
261 return line;
262}
263#endif
264
265static void prep_fetchline(void *opaque)
266{
267 int *fetchable = opaque;
268
269 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
270 *fetchable= 1;
271}
272
273static void command_loop(void)
274{
275 int i, done = 0, fetchable = 0, prompted = 0;
276 char *input;
277
278 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200279 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200280 }
281 if (cmdline) {
282 g_free(cmdline);
283 return;
284 }
285
286 while (!done) {
287 if (!prompted) {
288 printf("%s", get_prompt());
289 fflush(stdout);
290 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
291 prompted = 1;
292 }
293
294 main_loop_wait(false);
295
296 if (!fetchable) {
297 continue;
298 }
299
300 input = fetchline();
301 if (input == NULL) {
302 break;
303 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200304 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200305 g_free(input);
306
307 prompted = 0;
308 fetchable = 0;
309 }
310 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
311}
312
313static void add_user_command(char *optarg)
314{
315 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
316 cmdline[ncmdline-1] = optarg;
317}
318
aliguorie3aff4f2009-04-05 19:14:04 +0000319int main(int argc, char **argv)
320{
Devin Nakamura43642b32011-07-11 11:22:16 -0400321 int readonly = 0;
322 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100323 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400324 const struct option lopt[] = {
325 { "help", 0, NULL, 'h' },
326 { "version", 0, NULL, 'V' },
327 { "offset", 1, NULL, 'o' },
328 { "cmd", 1, NULL, 'c' },
329 { "read-only", 0, NULL, 'r' },
330 { "snapshot", 0, NULL, 's' },
331 { "nocache", 0, NULL, 'n' },
332 { "misalign", 0, NULL, 'm' },
333 { "growable", 0, NULL, 'g' },
334 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100335 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200336 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000337 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400338 { NULL, 0, NULL, 0 }
339 };
340 int c;
341 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100342 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +0000343
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900344#ifdef CONFIG_POSIX
345 signal(SIGPIPE, SIG_IGN);
346#endif
347
Devin Nakamura43642b32011-07-11 11:22:16 -0400348 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000349
Devin Nakamura43642b32011-07-11 11:22:16 -0400350 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
351 switch (c) {
352 case 's':
353 flags |= BDRV_O_SNAPSHOT;
354 break;
355 case 'n':
356 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
357 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100358 case 'd':
359 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
360 error_report("Invalid discard option: %s", optarg);
361 exit(1);
362 }
363 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400364 case 'c':
365 add_user_command(optarg);
366 break;
367 case 'r':
368 readonly = 1;
369 break;
370 case 'm':
Kevin Wolf797ac582013-06-05 14:19:31 +0200371 qemuio_misalign = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400372 break;
373 case 'g':
374 growable = 1;
375 break;
376 case 'k':
377 flags |= BDRV_O_NATIVE_AIO;
378 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200379 case 't':
380 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
381 error_report("Invalid cache option: %s", optarg);
382 exit(1);
383 }
384 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000385 case 'T':
386 if (!trace_backend_init(optarg, NULL)) {
387 exit(1); /* error message will have been printed */
388 }
389 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400390 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200391 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400392 exit(0);
393 case 'h':
394 usage(progname);
395 exit(0);
396 default:
397 usage(progname);
398 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200399 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400400 }
aliguorie3aff4f2009-04-05 19:14:04 +0000401
Devin Nakamura43642b32011-07-11 11:22:16 -0400402 if ((argc - optind) > 1) {
403 usage(progname);
404 exit(1);
405 }
aliguorie3aff4f2009-04-05 19:14:04 +0000406
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800407 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100408 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800409
Devin Nakamura43642b32011-07-11 11:22:16 -0400410 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200411 qemuio_add_command(&quit_cmd);
412 qemuio_add_command(&open_cmd);
413 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400414
415 /* open the device */
416 if (!readonly) {
417 flags |= BDRV_O_RDWR;
418 }
419
420 if ((argc - optind) == 1) {
421 openfile(argv[optind], flags, growable);
422 }
423 command_loop();
424
425 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000426 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400427 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000428 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400429
Kevin Wolf734c3b82013-06-05 14:19:30 +0200430 if (qemuio_bs) {
Fam Zheng4f6fd342013-08-23 09:14:47 +0800431 bdrv_unref(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400432 }
433 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000434}