blob: d7c2f26bbbc5463b9344b551cb77521315fec38a [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 */
Peter Maydell80c71a22016-01-18 18:01:42 +000010#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000011#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020012#include <libgen.h>
aliguorie3aff4f2009-04-05 19:14:04 +000013
Kevin Wolf3d219942013-06-05 14:19:39 +020014#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010015#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010016#include "qemu/main-loop.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020017#include "qemu/option.h"
18#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010019#include "qemu/readline.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010020#include "qapi/qmp/qstring.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000021#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020022#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010023#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000024#include "trace/control.h"
aliguorie3aff4f2009-04-05 19:14:04 +000025
Devin Nakamura43642b32011-07-11 11:22:16 -040026#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000027
Stefan Weilf9883882014-03-05 22:23:00 +010028static char *progname;
aliguorie3aff4f2009-04-05 19:14:04 +000029
Markus Armbruster26f54e92014-10-07 13:59:04 +020030static BlockBackend *qemuio_blk;
Kevin Wolf191c2892010-09-16 13:18:08 +020031
Kevin Wolfd1174f12013-06-05 14:19:37 +020032/* qemu-io commands passed using -c */
33static int ncmdline;
34static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000035static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020036
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010037static ReadLineState *readline_state;
38
Max Reitz4c7b7e92015-02-05 13:58:22 -050039static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000040{
Markus Armbruster26f54e92014-10-07 13:59:04 +020041 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020042 qemuio_blk = NULL;
Devin Nakamura43642b32011-07-11 11:22:16 -040043 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000044}
45
46static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040047 .name = "close",
48 .altname = "c",
49 .cfunc = close_f,
50 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000051};
52
Max Reitz10d9d752015-02-05 13:58:21 -050053static int openfile(char *name, int flags, QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000054{
Max Reitz34b5d2c2013-09-05 14:45:29 +020055 Error *local_err = NULL;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010056 BlockDriverState *bs;
Max Reitz34b5d2c2013-09-05 14:45:29 +020057
Max Reitz1b58b432015-02-05 13:58:20 -050058 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010059 error_report("file open already, try 'help close'");
Markus Armbruster29f26012014-05-28 11:16:59 +020060 QDECREF(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040061 return 1;
62 }
aliguorie3aff4f2009-04-05 19:14:04 +000063
Max Reitzefaa7c42016-03-16 19:54:38 +010064 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -050065 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010066 error_reportf_err(local_err, "can't open%s%s: ",
67 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +020068 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -040069 }
Christoph Hellwig1db69472009-07-15 23:11:21 +020070
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010071 bs = blk_bs(qemuio_blk);
72 if (bdrv_is_encrypted(bs)) {
73 char password[256];
74 printf("Disk image '%s' is encrypted.\n", name);
75 if (qemu_read_password(password, sizeof(password)) < 0) {
76 error_report("No password given");
77 goto error;
78 }
79 if (bdrv_set_key(bs, password) < 0) {
80 error_report("invalid password");
81 goto error;
82 }
83 }
84
85
Devin Nakamura43642b32011-07-11 11:22:16 -040086 return 0;
Daniel P. Berrange8caf0212015-05-12 17:09:21 +010087
88 error:
89 blk_unref(qemuio_blk);
90 qemuio_blk = NULL;
91 return 1;
aliguorie3aff4f2009-04-05 19:14:04 +000092}
93
Devin Nakamura43642b32011-07-11 11:22:16 -040094static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +000095{
Devin Nakamura43642b32011-07-11 11:22:16 -040096 printf(
aliguorie3aff4f2009-04-05 19:14:04 +000097"\n"
98" opens a new file in the requested mode\n"
99"\n"
100" Example:\n"
101" 'open -Cn /tmp/data' - creates/opens data file read-write and uncached\n"
102"\n"
103" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000104" -r, -- open file read-only\n"
105" -s, -- use snapshot file\n"
106" -n, -- disable host cache\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200107" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000108"\n");
109}
110
Max Reitz4c7b7e92015-02-05 13:58:22 -0500111static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000112
113static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400114 .name = "open",
115 .altname = "o",
116 .cfunc = open_f,
117 .argmin = 1,
118 .argmax = -1,
119 .flags = CMD_NOFILE_OK,
Max Reitzb543c5c2013-10-11 14:02:10 +0200120 .args = "[-Crsn] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400121 .oneline = "open the file specified by path",
122 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000123};
aliguorie3aff4f2009-04-05 19:14:04 +0000124
Max Reitzb543c5c2013-10-11 14:02:10 +0200125static QemuOptsList empty_opts = {
126 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200127 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200128 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
129 .desc = {
130 /* no elements => accept any params */
131 { /* end of list */ }
132 },
133};
134
Max Reitz4c7b7e92015-02-05 13:58:22 -0500135static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000136{
Devin Nakamura43642b32011-07-11 11:22:16 -0400137 int flags = 0;
138 int readonly = 0;
Devin Nakamura43642b32011-07-11 11:22:16 -0400139 int c;
Max Reitzb543c5c2013-10-11 14:02:10 +0200140 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200141 QDict *opts;
aliguorie3aff4f2009-04-05 19:14:04 +0000142
Eric Blakeb062ad82015-05-12 09:10:56 -0600143 while ((c = getopt(argc, argv, "snrgo:")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400144 switch (c) {
145 case 's':
146 flags |= BDRV_O_SNAPSHOT;
147 break;
148 case 'n':
149 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
150 break;
151 case 'r':
152 readonly = 1;
153 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200154 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000155 if (imageOpts) {
156 printf("--image-opts and 'open -o' are mutually exclusive\n");
157 return 0;
158 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100159 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200160 qemu_opts_reset(&empty_opts);
Max Reitzb543c5c2013-10-11 14:02:10 +0200161 return 0;
162 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200163 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400164 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200165 qemu_opts_reset(&empty_opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200166 return qemuio_command_usage(&open_cmd);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200167 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400168 }
aliguorie3aff4f2009-04-05 19:14:04 +0000169
Devin Nakamura43642b32011-07-11 11:22:16 -0400170 if (!readonly) {
171 flags |= BDRV_O_RDWR;
172 }
aliguorie3aff4f2009-04-05 19:14:04 +0000173
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000174 if (imageOpts && (optind == argc - 1)) {
175 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
176 qemu_opts_reset(&empty_opts);
177 return 0;
178 }
179 optind++;
180 }
181
Markus Armbruster443422f2014-05-28 11:16:58 +0200182 qopts = qemu_opts_find(&empty_opts, NULL);
183 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
184 qemu_opts_reset(&empty_opts);
185
Max Reitzfd0fee32013-12-20 19:28:20 +0100186 if (optind == argc - 1) {
Max Reitz10d9d752015-02-05 13:58:21 -0500187 return openfile(argv[optind], flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100188 } else if (optind == argc) {
Max Reitz10d9d752015-02-05 13:58:21 -0500189 return openfile(NULL, flags, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100190 } else {
Markus Armbruster29f26012014-05-28 11:16:59 +0200191 QDECREF(opts);
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200192 return qemuio_command_usage(&open_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400193 }
aliguorie3aff4f2009-04-05 19:14:04 +0000194}
195
Max Reitz4c7b7e92015-02-05 13:58:22 -0500196static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200197{
198 return 1;
199}
200
201static const cmdinfo_t quit_cmd = {
202 .name = "quit",
203 .altname = "q",
204 .cfunc = quit_f,
205 .argmin = -1,
206 .argmax = -1,
207 .flags = CMD_FLAG_GLOBAL,
208 .oneline = "exit the program",
209};
210
aliguorie3aff4f2009-04-05 19:14:04 +0000211static void usage(const char *name)
212{
Devin Nakamura43642b32011-07-11 11:22:16 -0400213 printf(
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100214"Usage: %s [-h] [-V] [-rsnm] [-f FMT] [-c STRING] ... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200215"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000216"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000217" --object OBJECTDEF define an object such as 'secret' for\n"
218" passwords and/or encryption keys\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400219" -c, --cmd STRING execute command with its arguments\n"
220" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100221" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000222" -r, --read-only export read-only\n"
223" -s, --snapshot use snapshot file\n"
224" -n, --nocache disable host cache\n"
225" -m, --misalign misalign allocations for O_DIRECT\n"
Christoph Hellwig5c6c3a62009-08-20 16:58:35 +0200226" -k, --native-aio use kernel AIO implementation (on Linux only)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200227" -t, --cache=MODE use the given cache mode for the image\n"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000228" -T, --trace FILE enable trace events listed in the given file\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000229" -h, --help display this help and exit\n"
230" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400231"\n"
232"See '%s -c help' for information on available commands."
aliguorie3aff4f2009-04-05 19:14:04 +0000233"\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400234 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000235}
236
Kevin Wolfd1174f12013-06-05 14:19:37 +0200237static char *get_prompt(void)
238{
239 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
240
241 if (!prompt[0]) {
242 snprintf(prompt, sizeof(prompt), "%s> ", progname);
243 }
244
245 return prompt;
246}
247
Stefan Weild5d15072014-01-25 18:18:23 +0100248static void GCC_FMT_ATTR(2, 3) readline_printf_func(void *opaque,
249 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200250{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100251 va_list ap;
252 va_start(ap, fmt);
253 vprintf(fmt, ap);
254 va_end(ap);
255}
256
257static void readline_flush_func(void *opaque)
258{
259 fflush(stdout);
260}
261
262static void readline_func(void *opaque, const char *str, void *readline_opaque)
263{
264 char **line = readline_opaque;
265 *line = g_strdup(str);
266}
267
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100268static void completion_match(const char *cmd, void *opaque)
269{
270 readline_add_completion(readline_state, cmd);
271}
272
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100273static void readline_completion_func(void *opaque, const char *str)
274{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100275 readline_set_completion_index(readline_state, strlen(str));
276 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100277}
278
279static char *fetchline_readline(void)
280{
281 char *line = NULL;
282
283 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
284 while (!line) {
285 int ch = getchar();
286 if (ch == EOF) {
287 break;
288 }
289 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200290 }
291 return line;
292}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200293
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100294#define MAXREADLINESZ 1024
295static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200296{
297 char *p, *line = g_malloc(MAXREADLINESZ);
298
299 if (!fgets(line, MAXREADLINESZ, stdin)) {
300 g_free(line);
301 return NULL;
302 }
303
304 p = line + strlen(line);
305 if (p != line && p[-1] == '\n') {
306 p[-1] = '\0';
307 }
308
309 return line;
310}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100311
312static char *fetchline(void)
313{
314 if (readline_state) {
315 return fetchline_readline();
316 } else {
317 return fetchline_fgets();
318 }
319}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200320
321static void prep_fetchline(void *opaque)
322{
323 int *fetchable = opaque;
324
325 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
326 *fetchable= 1;
327}
328
329static void command_loop(void)
330{
331 int i, done = 0, fetchable = 0, prompted = 0;
332 char *input;
333
334 for (i = 0; !done && i < ncmdline; i++) {
Max Reitz4c7b7e92015-02-05 13:58:22 -0500335 done = qemuio_command(qemuio_blk, cmdline[i]);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200336 }
337 if (cmdline) {
338 g_free(cmdline);
339 return;
340 }
341
342 while (!done) {
343 if (!prompted) {
344 printf("%s", get_prompt());
345 fflush(stdout);
346 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
347 prompted = 1;
348 }
349
350 main_loop_wait(false);
351
352 if (!fetchable) {
353 continue;
354 }
355
356 input = fetchline();
357 if (input == NULL) {
358 break;
359 }
Max Reitz4c7b7e92015-02-05 13:58:22 -0500360 done = qemuio_command(qemuio_blk, input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200361 g_free(input);
362
363 prompted = 0;
364 fetchable = 0;
365 }
366 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
367}
368
369static void add_user_command(char *optarg)
370{
Markus Armbruster5839e532014-08-19 10:31:08 +0200371 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200372 cmdline[ncmdline-1] = optarg;
373}
374
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100375static void reenable_tty_echo(void)
376{
377 qemu_set_tty_echo(STDIN_FILENO, true);
378}
379
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000380enum {
381 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000382 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000383};
384
385static QemuOptsList qemu_object_opts = {
386 .name = "object",
387 .implied_opt_name = "qom-type",
388 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
389 .desc = {
390 { }
391 },
392};
393
394
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000395static QemuOptsList file_opts = {
396 .name = "file",
397 .implied_opt_name = "file",
398 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
399 .desc = {
400 /* no elements => accept any params */
401 { /* end of list */ }
402 },
403};
404
aliguorie3aff4f2009-04-05 19:14:04 +0000405int main(int argc, char **argv)
406{
Devin Nakamura43642b32011-07-11 11:22:16 -0400407 int readonly = 0;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100408 const char *sopt = "hVc:d:f:rsnmgkt:T:";
Devin Nakamura43642b32011-07-11 11:22:16 -0400409 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000410 { "help", no_argument, NULL, 'h' },
411 { "version", no_argument, NULL, 'V' },
412 { "offset", required_argument, NULL, 'o' },
413 { "cmd", required_argument, NULL, 'c' },
414 { "format", required_argument, NULL, 'f' },
415 { "read-only", no_argument, NULL, 'r' },
416 { "snapshot", no_argument, NULL, 's' },
417 { "nocache", no_argument, NULL, 'n' },
418 { "misalign", no_argument, NULL, 'm' },
419 { "native-aio", no_argument, NULL, 'k' },
420 { "discard", required_argument, NULL, 'd' },
421 { "cache", required_argument, NULL, 't' },
422 { "trace", required_argument, NULL, 'T' },
423 { "object", required_argument, NULL, OPTION_OBJECT },
424 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Devin Nakamura43642b32011-07-11 11:22:16 -0400425 { NULL, 0, NULL, 0 }
426 };
427 int c;
428 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100429 int flags = BDRV_O_UNMAP;
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300430 Error *local_error = NULL;
Max Reitz1b58b432015-02-05 13:58:20 -0500431 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000432 const char *format = NULL;
aliguorie3aff4f2009-04-05 19:14:04 +0000433
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900434#ifdef CONFIG_POSIX
435 signal(SIGPIPE, SIG_IGN);
436#endif
437
Devin Nakamura43642b32011-07-11 11:22:16 -0400438 progname = basename(argv[0]);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800439 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000440
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000441 module_call_init(MODULE_INIT_QOM);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000442 qemu_add_opts(&qemu_object_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100443 bdrv_init();
444
Devin Nakamura43642b32011-07-11 11:22:16 -0400445 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
446 switch (c) {
447 case 's':
448 flags |= BDRV_O_SNAPSHOT;
449 break;
450 case 'n':
451 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
452 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100453 case 'd':
454 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
455 error_report("Invalid discard option: %s", optarg);
456 exit(1);
457 }
458 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100459 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000460 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100461 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400462 case 'c':
463 add_user_command(optarg);
464 break;
465 case 'r':
466 readonly = 1;
467 break;
468 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100469 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400470 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400471 case 'k':
472 flags |= BDRV_O_NATIVE_AIO;
473 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200474 case 't':
475 if (bdrv_parse_cache_flags(optarg, &flags) < 0) {
476 error_report("Invalid cache option: %s", optarg);
477 exit(1);
478 }
479 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000480 case 'T':
Paolo Bonzini41fc57e2016-01-07 16:55:24 +0300481 if (!trace_init_backends()) {
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000482 exit(1); /* error message will have been printed */
483 }
484 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400485 case 'V':
Kevin Wolf02da3862013-06-05 14:19:40 +0200486 printf("%s version %s\n", progname, QEMU_VERSION);
Devin Nakamura43642b32011-07-11 11:22:16 -0400487 exit(0);
488 case 'h':
489 usage(progname);
490 exit(0);
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000491 case OPTION_OBJECT: {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000492 QemuOpts *qopts;
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000493 qopts = qemu_opts_parse_noisily(&qemu_object_opts,
494 optarg, true);
495 if (!qopts) {
496 exit(1);
497 }
498 } break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000499 case OPTION_IMAGE_OPTS:
500 imageOpts = true;
501 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400502 default:
503 usage(progname);
504 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200505 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400506 }
aliguorie3aff4f2009-04-05 19:14:04 +0000507
Devin Nakamura43642b32011-07-11 11:22:16 -0400508 if ((argc - optind) > 1) {
509 usage(progname);
510 exit(1);
511 }
aliguorie3aff4f2009-04-05 19:14:04 +0000512
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000513 if (format && imageOpts) {
514 error_report("--image-opts and -f are mutually exclusive");
515 exit(1);
516 }
517
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300518 if (qemu_init_main_loop(&local_error)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100519 error_report_err(local_error);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300520 exit(1);
521 }
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800522
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000523 if (qemu_opts_foreach(&qemu_object_opts,
524 user_creatable_add_opts_foreach,
525 NULL, &local_error)) {
526 error_report_err(local_error);
527 exit(1);
528 }
529
Devin Nakamura43642b32011-07-11 11:22:16 -0400530 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200531 qemuio_add_command(&quit_cmd);
532 qemuio_add_command(&open_cmd);
533 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400534
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100535 if (isatty(STDIN_FILENO)) {
536 readline_state = readline_init(readline_printf_func,
537 readline_flush_func,
538 NULL,
539 readline_completion_func);
540 qemu_set_tty_echo(STDIN_FILENO, false);
541 atexit(reenable_tty_echo);
542 }
543
Devin Nakamura43642b32011-07-11 11:22:16 -0400544 /* open the device */
545 if (!readonly) {
546 flags |= BDRV_O_RDWR;
547 }
548
549 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000550 if (imageOpts) {
551 QemuOpts *qopts = NULL;
552 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
553 if (!qopts) {
554 exit(1);
555 }
556 opts = qemu_opts_to_qdict(qopts, NULL);
557 openfile(NULL, flags, opts);
558 } else {
559 if (format) {
560 opts = qdict_new();
561 qdict_put(opts, "driver", qstring_from_str(format));
562 }
563 openfile(argv[optind], flags, opts);
564 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400565 }
566 command_loop();
567
568 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000569 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400570 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000571 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400572
Markus Armbruster26f54e92014-10-07 13:59:04 +0200573 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100574 g_free(readline_state);
Devin Nakamura43642b32011-07-11 11:22:16 -0400575 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000576}