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