blob: 514edcb527549683d1a2f29b0729be2ee87d7199 [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
22#define VERSION "0.0.1"
23
Devin Nakamura43642b32011-07-11 11:22:16 -040024#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000025
26char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000027
Kevin Wolf734c3b82013-06-05 14:19:30 +020028BlockDriverState *qemuio_bs;
Kevin Wolf797ac582013-06-05 14:19:31 +020029extern int qemuio_misalign;
Kevin Wolf191c2892010-09-16 13:18:08 +020030
Kevin Wolfd1174f12013-06-05 14:19:37 +020031/* qemu-io commands passed using -c */
32static int ncmdline;
33static char **cmdline;
34
Kevin Wolf734c3b82013-06-05 14:19:30 +020035static int close_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000036{
Stefan Hajnoczib4657852011-10-27 10:54:26 +010037 bdrv_delete(bs);
Kevin Wolf734c3b82013-06-05 14:19:30 +020038 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040039 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000040}
41
42static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040043 .name = "close",
44 .altname = "c",
45 .cfunc = close_f,
46 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000047};
48
Christoph Hellwig9c4bab22009-07-10 13:33:47 +020049static int openfile(char *name, int flags, int growable)
aliguorie3aff4f2009-04-05 19:14:04 +000050{
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) {
Kevin Wolf734c3b82013-06-05 14:19:30 +020057 if (bdrv_file_open(&qemuio_bs, name, NULL, flags)) {
Devin Nakamura43642b32011-07-11 11:22:16 -040058 fprintf(stderr, "%s: can't open device %s\n", progname, name);
59 return 1;
60 }
61 } else {
Kevin Wolf734c3b82013-06-05 14:19:30 +020062 qemuio_bs = bdrv_new("hda");
Christoph Hellwig6db95602010-04-05 16:53:57 +020063
Kevin Wolf734c3b82013-06-05 14:19:30 +020064 if (bdrv_open(qemuio_bs, name, NULL, flags, NULL) < 0) {
Devin Nakamura43642b32011-07-11 11:22:16 -040065 fprintf(stderr, "%s: can't open device %s\n", progname, name);
Kevin Wolf734c3b82013-06-05 14:19:30 +020066 bdrv_delete(qemuio_bs);
67 qemuio_bs = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040068 return 1;
69 }
70 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020071
Devin Nakamura43642b32011-07-11 11:22:16 -040072 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000073}
74
Devin Nakamura43642b32011-07-11 11:22:16 -040075static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000076{
Devin Nakamura43642b32011-07-11 11:22:16 -040077 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000078"\n"
79" opens a new file in the requested mode\n"
80"\n"
81" Example:\n"
82" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
83"\n"
84" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +000085" -r, -- open file read-only\n"
86" -s, -- use snapshot file\n"
87" -n, -- disable host cache\n"
Christoph Hellwig9c4bab22009-07-10 13:33:47 +020088" -g, -- allow file to grow (only applies to protocols)"
aliguorie3aff4f2009-04-05 19:14:04 +000089"\n");
90}
91
Kevin Wolf734c3b82013-06-05 14:19:30 +020092static int open_f(BlockDriverState *bs, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +000093
94static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040095 .name = "open",
96 .altname = "o",
97 .cfunc = open_f,
98 .argmin = 1,
99 .argmax = -1,
100 .flags = CMD_NOFILE_OK,
101 .args = "[-Crsn] [path]",
102 .oneline = "open the file specified by path",
103 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000104};
aliguorie3aff4f2009-04-05 19:14:04 +0000105
Kevin Wolf734c3b82013-06-05 14:19:30 +0200106static int open_f(BlockDriverState *bs, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000107{
Devin Nakamura43642b32011-07-11 11:22:16 -0400108 int flags = 0;
109 int readonly = 0;
110 int growable = 0;
111 int c;
aliguorie3aff4f2009-04-05 19:14:04 +0000112
Devin Nakamura43642b32011-07-11 11:22:16 -0400113 while ((c = getopt(argc, argv, "snrg")) != EOF) {
114 switch (c) {
115 case 's':
116 flags |= BDRV_O_SNAPSHOT;
117 break;
118 case 'n':
119 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
120 break;
121 case 'r':
122 readonly = 1;
123 break;
124 case 'g':
125 growable = 1;
126 break;
127 default:
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200128 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200129 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400130 }
aliguorie3aff4f2009-04-05 19:14:04 +0000131
Devin Nakamura43642b32011-07-11 11:22:16 -0400132 if (!readonly) {
133 flags |= BDRV_O_RDWR;
134 }
aliguorie3aff4f2009-04-05 19:14:04 +0000135
Devin Nakamura43642b32011-07-11 11:22:16 -0400136 if (optind != argc - 1) {
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200137 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400138 }
139
140 return openfile(argv[optind], flags, growable);
aliguorie3aff4f2009-04-05 19:14:04 +0000141}
142
Kevin Wolfe681be72013-06-05 14:19:34 +0200143static int quit_f(BlockDriverState *bs, int argc, char **argv)
144{
145 return 1;
146}
147
148static const cmdinfo_t quit_cmd = {
149 .name = "quit",
150 .altname = "q",
151 .cfunc = quit_f,
152 .argmin = -1,
153 .argmax = -1,
154 .flags = CMD_FLAG_GLOBAL,
155 .oneline = "exit the program",
156};
157
aliguorie3aff4f2009-04-05 19:14:04 +0000158static void usage(const char *name)
159{
Devin Nakamura43642b32011-07-11 11:22:16 -0400160 printf(
Christoph Hellwig9a2d77a2010-01-20 18:13:42 +0100161"Usage: %s [-h] [-V] [-rsnm] [-c cmd] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200162"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000163"\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000164" -c, --cmd command to execute\n"
165" -r, --read-only export read-only\n"
166" -s, --snapshot use snapshot file\n"
167" -n, --nocache disable host cache\n"
Christoph Hellwig1db69472009-07-15 23:11:21 +0200168" -g, --growable allow file to grow (only applies to protocols)\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000169" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200170" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200171" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000172" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000173" -h, --help display this help and exit\n"
174" -V, --version output version information and exit\n"
175"\n",
Devin Nakamura43642b32011-07-11 11:22:16 -0400176 name);
aliguorie3aff4f2009-04-05 19:14:04 +0000177}
178
179
Kevin Wolfd1174f12013-06-05 14:19:37 +0200180#if defined(ENABLE_READLINE)
181# include <readline/history.h>
182# include <readline/readline.h>
183#elif defined(ENABLE_EDITLINE)
184# include <histedit.h>
185#endif
186
187static char *get_prompt(void)
188{
189 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
190
191 if (!prompt[0]) {
192 snprintf(prompt, sizeof(prompt), "%s> ", progname);
193 }
194
195 return prompt;
196}
197
198#if defined(ENABLE_READLINE)
199static char *fetchline(void)
200{
201 char *line = readline(get_prompt());
202 if (line && *line) {
203 add_history(line);
204 }
205 return line;
206}
207#elif defined(ENABLE_EDITLINE)
208static char *el_get_prompt(EditLine *e)
209{
210 return get_prompt();
211}
212
213static char *fetchline(void)
214{
215 static EditLine *el;
216 static History *hist;
217 HistEvent hevent;
218 char *line;
219 int count;
220
221 if (!el) {
222 hist = history_init();
223 history(hist, &hevent, H_SETSIZE, 100);
224 el = el_init(progname, stdin, stdout, stderr);
225 el_source(el, NULL);
226 el_set(el, EL_SIGNAL, 1);
227 el_set(el, EL_PROMPT, el_get_prompt);
228 el_set(el, EL_HIST, history, (const char *)hist);
229 }
230 line = strdup(el_gets(el, &count));
231 if (line) {
232 if (count > 0) {
233 line[count-1] = '\0';
234 }
235 if (*line) {
236 history(hist, &hevent, H_ENTER, line);
237 }
238 }
239 return line;
240}
241#else
242# define MAXREADLINESZ 1024
243static char *fetchline(void)
244{
245 char *p, *line = g_malloc(MAXREADLINESZ);
246
247 if (!fgets(line, MAXREADLINESZ, stdin)) {
248 g_free(line);
249 return NULL;
250 }
251
252 p = line + strlen(line);
253 if (p != line && p[-1] == '\n') {
254 p[-1] = '\0';
255 }
256
257 return line;
258}
259#endif
260
261static void prep_fetchline(void *opaque)
262{
263 int *fetchable = opaque;
264
265 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
266 *fetchable= 1;
267}
268
269static void command_loop(void)
270{
271 int i, done = 0, fetchable = 0, prompted = 0;
272 char *input;
273
274 for (i = 0; !done && i < ncmdline; i++) {
Kevin Wolf3d219942013-06-05 14:19:39 +0200275 done = qemuio_command(qemuio_bs, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200276 }
277 if (cmdline) {
278 g_free(cmdline);
279 return;
280 }
281
282 while (!done) {
283 if (!prompted) {
284 printf("%s", get_prompt());
285 fflush(stdout);
286 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
287 prompted = 1;
288 }
289
290 main_loop_wait(false);
291
292 if (!fetchable) {
293 continue;
294 }
295
296 input = fetchline();
297 if (input == NULL) {
298 break;
299 }
Kevin Wolf3d219942013-06-05 14:19:39 +0200300 done = qemuio_command(qemuio_bs, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200301 g_free(input);
302
303 prompted = 0;
304 fetchable = 0;
305 }
306 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
307}
308
309static void add_user_command(char *optarg)
310{
311 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
312 cmdline[ncmdline-1] = optarg;
313}
314
aliguorie3aff4f2009-04-05 19:14:04 +0000315int main(int argc, char **argv)
316{
Devin Nakamura43642b32011-07-11 11:22:16 -0400317 int readonly = 0;
318 int growable = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100319 const char *sopt = "hVc:d:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400320 const struct option lopt[] = {
321 { "help", 0, NULL, 'h' },
322 { "version", 0, NULL, 'V' },
323 { "offset", 1, NULL, 'o' },
324 { "cmd", 1, NULL, 'c' },
325 { "read-only", 0, NULL, 'r' },
326 { "snapshot", 0, NULL, 's' },
327 { "nocache", 0, NULL, 'n' },
328 { "misalign", 0, NULL, 'm' },
329 { "growable", 0, NULL, 'g' },
330 { "native-aio", 0, NULL, 'k' },
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100331 { "discard", 1, NULL, 'd' },
Kevin Wolf592fa072012-04-18 12:07:39 +0200332 { "cache", 1, NULL, 't' },
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000333 { "trace", 1, NULL, 'T' },
Devin Nakamura43642b32011-07-11 11:22:16 -0400334 { NULL, 0, NULL, 0 }
335 };
336 int c;
337 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100338 int flags = BDRV_O_UNMAP;
aliguorie3aff4f2009-04-05 19:14:04 +0000339
Devin Nakamura43642b32011-07-11 11:22:16 -0400340 progname = basename(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000341
Devin Nakamura43642b32011-07-11 11:22:16 -0400342 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
343 switch (c) {
344 case 's':
345 flags |= BDRV_O_SNAPSHOT;
346 break;
347 case 'n':
348 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
349 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100350 case 'd':
351 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
352 error_report("Invalid discard option: %s", optarg);
353 exit(1);
354 }
355 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400356 case 'c':
357 add_user_command(optarg);
358 break;
359 case 'r':
360 readonly = 1;
361 break;
362 case 'm':
Kevin Wolf797ac582013-06-05 14:19:31 +0200363 qemuio_misalign = 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400364 break;
365 case 'g':
366 growable = 1;
367 break;
368 case 'k':
369 flags |= BDRV_O_NATIVE_AIO;
370 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200371 case 't':
372 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
373 error_report("Invalid cache option: %s", optarg);
374 exit(1);
375 }
376 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000377 case 'T':
378 if (!trace_backend_init(optarg, NULL)) {
379 exit(1); /* error message will have been printed */
380 }
381 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400382 case 'V':
383 printf("%s version %s\n", progname, VERSION);
384 exit(0);
385 case 'h':
386 usage(progname);
387 exit(0);
388 default:
389 usage(progname);
390 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200391 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400392 }
aliguorie3aff4f2009-04-05 19:14:04 +0000393
Devin Nakamura43642b32011-07-11 11:22:16 -0400394 if ((argc - optind) > 1) {
395 usage(progname);
396 exit(1);
397 }
aliguorie3aff4f2009-04-05 19:14:04 +0000398
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800399 qemu_init_main_loop();
Paolo Bonzini2592c592012-11-03 18:10:17 +0100400 bdrv_init();
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800401
Devin Nakamura43642b32011-07-11 11:22:16 -0400402 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200403 qemuio_add_command(&quit_cmd);
404 qemuio_add_command(&open_cmd);
405 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400406
407 /* open the device */
408 if (!readonly) {
409 flags |= BDRV_O_RDWR;
410 }
411
412 if ((argc - optind) == 1) {
413 openfile(argv[optind], flags, growable);
414 }
415 command_loop();
416
417 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000418 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400419 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000420 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400421
Kevin Wolf734c3b82013-06-05 14:19:30 +0200422 if (qemuio_bs) {
423 bdrv_delete(qemuio_bs);
Devin Nakamura43642b32011-07-11 11:22:16 -0400424 }
425 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000426}