]> git.proxmox.com Git - mirror_qemu.git/blame - qemu-io.c
qemu-io: Support multiple -o in open command
[mirror_qemu.git] / qemu-io.c
CommitLineData
e3aff4f6
AL
1/*
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 */
c32d766a 10#include <sys/time.h>
e3aff4f6
AL
11#include <sys/types.h>
12#include <stdarg.h>
13#include <stdio.h>
14#include <getopt.h>
c32d766a 15#include <libgen.h>
e3aff4f6 16
3d21994f 17#include "qemu-io.h"
1de7afc9 18#include "qemu/main-loop.h"
b543c5cd
HR
19#include "qemu/option.h"
20#include "qemu/config-file.h"
0cf17e18 21#include "qemu/readline.h"
737e150e 22#include "block/block_int.h"
d7bb72c8 23#include "trace/control.h"
e3aff4f6 24
43642b38 25#define CMD_NOFILE_OK 0x01
e3aff4f6 26
f9883880 27static char *progname;
e3aff4f6 28
f9883880 29static BlockDriverState *qemuio_bs;
191c2890 30
d1174f13
KW
31/* qemu-io commands passed using -c */
32static int ncmdline;
33static char **cmdline;
34
0cf17e18
SH
35static ReadLineState *readline_state;
36
734c3b85 37static int close_f(BlockDriverState *bs, int argc, char **argv)
e3aff4f6 38{
4f6fd349 39 bdrv_unref(bs);
734c3b85 40 qemuio_bs = NULL;
43642b38 41 return 0;
e3aff4f6
AL
42}
43
44static const cmdinfo_t close_cmd = {
43642b38
DN
45 .name = "close",
46 .altname = "c",
47 .cfunc = close_f,
48 .oneline = "close the current open file",
e3aff4f6
AL
49};
50
b543c5cd 51static int openfile(char *name, int flags, int growable, QDict *opts)
e3aff4f6 52{
34b5d2c6
HR
53 Error *local_err = NULL;
54
734c3b85 55 if (qemuio_bs) {
43642b38
DN
56 fprintf(stderr, "file open already, try 'help close'\n");
57 return 1;
58 }
59
60 if (growable) {
2e40134b
HR
61 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags | BDRV_O_PROTOCOL,
62 NULL, &local_err))
63 {
34b5d2c6
HR
64 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
65 error_get_pretty(local_err));
66 error_free(local_err);
43642b38
DN
67 return 1;
68 }
69 } else {
98522f63 70 qemuio_bs = bdrv_new("hda", &error_abort);
43642b38 71
ddf5636d
HR
72 if (bdrv_open(&qemuio_bs, name, NULL, opts, flags, NULL, &local_err)
73 < 0)
74 {
34b5d2c6
HR
75 fprintf(stderr, "%s: can't open device %s: %s\n", progname, name,
76 error_get_pretty(local_err));
77 error_free(local_err);
4f6fd349 78 bdrv_unref(qemuio_bs);
734c3b85 79 qemuio_bs = NULL;
43642b38
DN
80 return 1;
81 }
82 }
83
84 return 0;
e3aff4f6
AL
85}
86
43642b38 87static void open_help(void)
e3aff4f6 88{
43642b38 89 printf(
e3aff4f6
AL
90"\n"
91" opens a new file in the requested mode\n"
92"\n"
93" Example:\n"
94" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
95"\n"
96" Opens a file for subsequent use by all of the other qemu-io commands.\n"
e3aff4f6
AL
97" -r, -- open file read-only\n"
98" -s, -- use snapshot file\n"
99" -n, -- disable host cache\n"
b543c5cd
HR
100" -g, -- allow file to grow (only applies to protocols)\n"
101" -o, -- options to be given to the block driver"
e3aff4f6
AL
102"\n");
103}
104
734c3b85 105static int open_f(BlockDriverState *bs, int argc, char **argv);
22a2bdcb
BS
106
107static const cmdinfo_t open_cmd = {
43642b38
DN
108 .name = "open",
109 .altname = "o",
110 .cfunc = open_f,
111 .argmin = 1,
112 .argmax = -1,
113 .flags = CMD_NOFILE_OK,
b543c5cd 114 .args = "[-Crsn] [-o options] [path]",
43642b38
DN
115 .oneline = "open the file specified by path",
116 .help = open_help,
22a2bdcb 117};
e3aff4f6 118
b543c5cd
HR
119static QemuOptsList empty_opts = {
120 .name = "drive",
443422fd 121 .merge_lists = true,
b543c5cd
HR
122 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
123 .desc = {
124 /* no elements => accept any params */
125 { /* end of list */ }
126 },
127};
128
734c3b85 129static int open_f(BlockDriverState *bs, int argc, char **argv)
e3aff4f6 130{
43642b38
DN
131 int flags = 0;
132 int readonly = 0;
133 int growable = 0;
134 int c;
b543c5cd 135 QemuOpts *qopts;
443422fd 136 QDict *opts;
43642b38 137
b543c5cd 138 while ((c = getopt(argc, argv, "snrgo:")) != EOF) {
43642b38
DN
139 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;
b543c5cd 152 case 'o':
443422fd 153 if (!qemu_opts_parse(&empty_opts, optarg, 0)) {
b543c5cd 154 printf("could not parse option list -- %s\n", optarg);
443422fd 155 qemu_opts_reset(&empty_opts);
b543c5cd
HR
156 return 0;
157 }
b543c5cd 158 break;
43642b38 159 default:
443422fd 160 qemu_opts_reset(&empty_opts);
c2cdf5c5 161 return qemuio_command_usage(&open_cmd);
f5edb014 162 }
43642b38
DN
163 }
164
165 if (!readonly) {
166 flags |= BDRV_O_RDWR;
167 }
e3aff4f6 168
443422fd
MA
169 qopts = qemu_opts_find(&empty_opts, NULL);
170 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
171 qemu_opts_reset(&empty_opts);
172
fd0fee34
HR
173 if (optind == argc - 1) {
174 return openfile(argv[optind], flags, growable, opts);
175 } else if (optind == argc) {
176 return openfile(NULL, flags, growable, opts);
177 } else {
c2cdf5c5 178 return qemuio_command_usage(&open_cmd);
43642b38 179 }
e3aff4f6
AL
180}
181
e681be7e
KW
182static int quit_f(BlockDriverState *bs, int argc, char **argv)
183{
184 return 1;
185}
186
187static const cmdinfo_t quit_cmd = {
188 .name = "quit",
189 .altname = "q",
190 .cfunc = quit_f,
191 .argmin = -1,
192 .argmax = -1,
193 .flags = CMD_FLAG_GLOBAL,
194 .oneline = "exit the program",
195};
196
e3aff4f6
AL
197static void usage(const char *name)
198{
43642b38 199 printf(
d208cc35 200"Usage: %s [-h] [-V] [-rsnm] [-c STRING] ... [file]\n"
84844a20 201"QEMU Disk exerciser\n"
e3aff4f6 202"\n"
d208cc35
MK
203" -c, --cmd STRING execute command with its arguments\n"
204" from the given string\n"
e3aff4f6
AL
205" -r, --read-only export read-only\n"
206" -s, --snapshot use snapshot file\n"
207" -n, --nocache disable host cache\n"
1db6947d 208" -g, --growable allow file to grow (only applies to protocols)\n"
e3aff4f6 209" -m, --misalign misalign allocations for O_DIRECT\n"
5c6c3a6c 210" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
592fa070 211" -t, --cache=MODE use the given cache mode for the image\n"
d7bb72c8 212" -T, --trace FILE enable trace events listed in the given file\n"
e3aff4f6
AL
213" -h, --help display this help and exit\n"
214" -V, --version output version information and exit\n"
d208cc35
MK
215"\n"
216"See '%s -c help' for information on available commands."
e3aff4f6 217"\n",
d208cc35 218 name, name);
e3aff4f6
AL
219}
220
d1174f13
KW
221static char *get_prompt(void)
222{
223 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
224
225 if (!prompt[0]) {
226 snprintf(prompt, sizeof(prompt), "%s> ", progname);
227 }
228
229 return prompt;
230}
231
d5d1507b
SW
232static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
233 const char *fmt, ...)
d1174f13 234{
0cf17e18
SH
235 va_list ap;
236 va_start(ap, fmt);
237 vprintf(fmt, ap);
238 va_end(ap);
d1174f13 239}
0cf17e18
SH
240
241static void readline_flush_func(void *opaque)
d1174f13 242{
0cf17e18 243 fflush(stdout);
d1174f13
KW
244}
245
0cf17e18 246static void readline_func(void *opaque, const char *str, void *readline_opaque)
d1174f13 247{
0cf17e18
SH
248 char **line = readline_opaque;
249 *line = g_strdup(str);
250}
251
4694020d
SH
252static void completion_match(const char *cmd, void *opaque)
253{
254 readline_add_completion(readline_state, cmd);
255}
256
0cf17e18
SH
257static void readline_completion_func(void *opaque, const char *str)
258{
4694020d
SH
259 readline_set_completion_index(readline_state, strlen(str));
260 qemuio_complete_command(str, completion_match, NULL);
0cf17e18
SH
261}
262
263static char *fetchline_readline(void)
264{
265 char *line = NULL;
266
267 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
268 while (!line) {
269 int ch = getchar();
270 if (ch == EOF) {
271 break;
d1174f13 272 }
0cf17e18 273 readline_handle_byte(readline_state, ch);
d1174f13
KW
274 }
275 return line;
276}
0cf17e18
SH
277
278#define MAXREADLINESZ 1024
279static char *fetchline_fgets(void)
d1174f13
KW
280{
281 char *p, *line = g_malloc(MAXREADLINESZ);
282
283 if (!fgets(line, MAXREADLINESZ, stdin)) {
284 g_free(line);
285 return NULL;
286 }
287
288 p = line + strlen(line);
289 if (p != line && p[-1] == '\n') {
290 p[-1] = '\0';
291 }
292
293 return line;
294}
0cf17e18
SH
295
296static char *fetchline(void)
297{
298 if (readline_state) {
299 return fetchline_readline();
300 } else {
301 return fetchline_fgets();
302 }
303}
d1174f13
KW
304
305static void prep_fetchline(void *opaque)
306{
307 int *fetchable = opaque;
308
309 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
310 *fetchable= 1;
311}
312
313static void command_loop(void)
314{
315 int i, done = 0, fetchable = 0, prompted = 0;
316 char *input;
317
318 for (i = 0; !done && i < ncmdline; i++) {
3d21994f 319 done = qemuio_command(qemuio_bs, cmdline[i]);
d1174f13
KW
320 }
321 if (cmdline) {
322 g_free(cmdline);
323 return;
324 }
325
326 while (!done) {
327 if (!prompted) {
328 printf("%s", get_prompt());
329 fflush(stdout);
330 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
331 prompted = 1;
332 }
333
334 main_loop_wait(false);
335
336 if (!fetchable) {
337 continue;
338 }
339
340 input = fetchline();
341 if (input == NULL) {
342 break;
343 }
3d21994f 344 done = qemuio_command(qemuio_bs, input);
d1174f13
KW
345 g_free(input);
346
347 prompted = 0;
348 fetchable = 0;
349 }
350 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
351}
352
353static void add_user_command(char *optarg)
354{
355 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
356 cmdline[ncmdline-1] = optarg;
357}
358
0cf17e18
SH
359static void reenable_tty_echo(void)
360{
361 qemu_set_tty_echo(STDIN_FILENO, true);
362}
363
e3aff4f6
AL
364int main(int argc, char **argv)
365{
43642b38
DN
366 int readonly = 0;
367 int growable = 0;
9e8f1835 368 const char *sopt = "hVc:d:rsnmgkt:T:";
43642b38
DN
369 const struct option lopt[] = {
370 { "help", 0, NULL, 'h' },
371 { "version", 0, NULL, 'V' },
372 { "offset", 1, NULL, 'o' },
373 { "cmd", 1, NULL, 'c' },
374 { "read-only", 0, NULL, 'r' },
375 { "snapshot", 0, NULL, 's' },
376 { "nocache", 0, NULL, 'n' },
377 { "misalign", 0, NULL, 'm' },
378 { "growable", 0, NULL, 'g' },
379 { "native-aio", 0, NULL, 'k' },
9e8f1835 380 { "discard", 1, NULL, 'd' },
592fa070 381 { "cache", 1, NULL, 't' },
d7bb72c8 382 { "trace", 1, NULL, 'T' },
43642b38
DN
383 { NULL, 0, NULL, 0 }
384 };
385 int c;
386 int opt_index = 0;
9e8f1835 387 int flags = BDRV_O_UNMAP;
43642b38 388
526eda14
MK
389#ifdef CONFIG_POSIX
390 signal(SIGPIPE, SIG_IGN);
391#endif
392
43642b38 393 progname = basename(argv[0]);
10f5bff6 394 qemu_init_exec_dir(argv[0]);
43642b38
DN
395
396 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
397 switch (c) {
398 case 's':
399 flags |= BDRV_O_SNAPSHOT;
400 break;
401 case 'n':
402 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
403 break;
9e8f1835
PB
404 case 'd':
405 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
406 error_report("Invalid discard option: %s", optarg);
407 exit(1);
408 }
409 break;
43642b38
DN
410 case 'c':
411 add_user_command(optarg);
412 break;
413 case 'r':
414 readonly = 1;
415 break;
416 case 'm':
f9883880 417 qemuio_misalign = true;
43642b38
DN
418 break;
419 case 'g':
420 growable = 1;
421 break;
422 case 'k':
423 flags |= BDRV_O_NATIVE_AIO;
424 break;
592fa070
KW
425 case 't':
426 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
427 error_report("Invalid cache option: %s", optarg);
428 exit(1);
429 }
430 break;
d7bb72c8
SH
431 case 'T':
432 if (!trace_backend_init(optarg, NULL)) {
433 exit(1); /* error message will have been printed */
434 }
435 break;
43642b38 436 case 'V':
02da386a 437 printf("%s version %s\n", progname, QEMU_VERSION);
43642b38
DN
438 exit(0);
439 case 'h':
440 usage(progname);
441 exit(0);
442 default:
443 usage(progname);
444 exit(1);
f5edb014 445 }
43642b38
DN
446 }
447
448 if ((argc - optind) > 1) {
449 usage(progname);
450 exit(1);
451 }
e3aff4f6 452
a57d1143 453 qemu_init_main_loop();
2592c59a 454 bdrv_init();
a57d1143 455
43642b38 456 /* initialize commands */
c2cdf5c5
KW
457 qemuio_add_command(&quit_cmd);
458 qemuio_add_command(&open_cmd);
459 qemuio_add_command(&close_cmd);
43642b38 460
0cf17e18
SH
461 if (isatty(STDIN_FILENO)) {
462 readline_state = readline_init(readline_printf_func,
463 readline_flush_func,
464 NULL,
465 readline_completion_func);
466 qemu_set_tty_echo(STDIN_FILENO, false);
467 atexit(reenable_tty_echo);
468 }
469
43642b38
DN
470 /* open the device */
471 if (!readonly) {
472 flags |= BDRV_O_RDWR;
473 }
474
475 if ((argc - optind) == 1) {
b543c5cd 476 openfile(argv[optind], flags, growable, NULL);
43642b38
DN
477 }
478 command_loop();
e3aff4f6 479
43642b38 480 /*
922453bc 481 * Make sure all outstanding requests complete before the program exits.
43642b38 482 */
922453bc 483 bdrv_drain_all();
95533d5f 484
734c3b85 485 if (qemuio_bs) {
4f6fd349 486 bdrv_unref(qemuio_bs);
43642b38 487 }
0cf17e18 488 g_free(readline_state);
43642b38 489 return 0;
e3aff4f6 490}