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