blob: 2bd7bfb65073a23a862183247c9bbf044ea6ae44 [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 */
Markus Armbruster452fcdb2018-02-01 12:18:39 +010010
Peter Maydell80c71a22016-01-18 18:01:42 +000011#include "qemu/osdep.h"
aliguorie3aff4f2009-04-05 19:14:04 +000012#include <getopt.h>
Stefan Weilc32d7662009-08-31 22:16:16 +020013#include <libgen.h>
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000014#ifndef _WIN32
15#include <termios.h>
16#endif
aliguorie3aff4f2009-04-05 19:14:04 +000017
Marc-André Lureau49f95222022-04-20 17:25:49 +040018#include "qemu/help-texts.h"
Marc-André Lureau06680b12022-05-25 16:41:26 +020019#include "qemu/cutils.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010020#include "qapi/error.h"
Kevin Wolf3d219942013-06-05 14:19:39 +020021#include "qemu-io.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010022#include "qemu/error-report.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010023#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020024#include "qemu/module.h"
Max Reitzb543c5c2013-10-11 14:02:10 +020025#include "qemu/option.h"
26#include "qemu/config-file.h"
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010027#include "qemu/readline.h"
Denis V. Luneve9a80852016-06-17 17:44:11 +030028#include "qemu/log.h"
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +010029#include "qemu/sockets.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010030#include "qapi/qmp/qstring.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010031#include "qapi/qmp/qdict.h"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +000032#include "qom/object_interfaces.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020033#include "sysemu/block-backend.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010034#include "block/block_int.h"
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +000035#include "trace/control.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010036#include "crypto/init.h"
Eric Blake4face322017-08-03 11:33:51 -050037#include "qemu-version.h"
aliguorie3aff4f2009-04-05 19:14:04 +000038
Devin Nakamura43642b32011-07-11 11:22:16 -040039#define CMD_NOFILE_OK 0x01
aliguorie3aff4f2009-04-05 19:14:04 +000040
Markus Armbruster26f54e92014-10-07 13:59:04 +020041static BlockBackend *qemuio_blk;
Max Reitzb444d0e2018-05-09 21:42:58 +020042static bool quit_qemu_io;
Kevin Wolf191c2892010-09-16 13:18:08 +020043
Kevin Wolfd1174f12013-06-05 14:19:37 +020044/* qemu-io commands passed using -c */
45static int ncmdline;
46static char **cmdline;
Daniel P. Berrange499afa22016-02-17 10:10:18 +000047static bool imageOpts;
Kevin Wolfd1174f12013-06-05 14:19:37 +020048
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +010049static ReadLineState *readline_state;
50
Daniel P. Berrange0e448a02018-02-12 18:48:49 +000051static int ttyEOF;
52
53static int get_eof_char(void)
54{
55#ifdef _WIN32
56 return 0x4; /* Ctrl-D */
57#else
58 struct termios tty;
59 if (tcgetattr(STDIN_FILENO, &tty) != 0) {
60 if (errno == ENOTTY) {
61 return 0x0; /* just expect read() == 0 */
62 } else {
63 return 0x4; /* Ctrl-D */
64 }
65 }
66
67 return tty.c_cc[VEOF];
68#endif
69}
70
Max Reitzb32d7a32018-05-09 21:42:59 +020071static int close_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +000072{
Markus Armbruster26f54e92014-10-07 13:59:04 +020073 blk_unref(qemuio_blk);
Markus Armbruster26f54e92014-10-07 13:59:04 +020074 qemuio_blk = NULL;
Max Reitzb32d7a32018-05-09 21:42:59 +020075 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +000076}
77
78static const cmdinfo_t close_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -040079 .name = "close",
80 .altname = "c",
81 .cfunc = close_f,
82 .oneline = "close the current open file",
aliguorie3aff4f2009-04-05 19:14:04 +000083};
84
Fam Zheng459571f2017-05-03 00:35:41 +080085static int openfile(char *name, int flags, bool writethrough, bool force_share,
86 QDict *opts)
aliguorie3aff4f2009-04-05 19:14:04 +000087{
Max Reitz34b5d2c2013-09-05 14:45:29 +020088 Error *local_err = NULL;
89
Max Reitz1b58b432015-02-05 13:58:20 -050090 if (qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +010091 error_report("file open already, try 'help close'");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +020092 qobject_unref(opts);
Devin Nakamura43642b32011-07-11 11:22:16 -040093 return 1;
94 }
aliguorie3aff4f2009-04-05 19:14:04 +000095
Fam Zheng459571f2017-05-03 00:35:41 +080096 if (force_share) {
97 if (!opts) {
98 opts = qdict_new();
99 }
100 if (qdict_haskey(opts, BDRV_OPT_FORCE_SHARE)
Max Reitz2a01c012018-05-02 22:20:49 +0200101 && strcmp(qdict_get_str(opts, BDRV_OPT_FORCE_SHARE), "on")) {
Fam Zheng459571f2017-05-03 00:35:41 +0800102 error_report("-U conflicts with image options");
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200103 qobject_unref(opts);
Fam Zheng459571f2017-05-03 00:35:41 +0800104 return 1;
105 }
Max Reitz2a01c012018-05-02 22:20:49 +0200106 qdict_put_str(opts, BDRV_OPT_FORCE_SHARE, "on");
Fam Zheng459571f2017-05-03 00:35:41 +0800107 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100108 qemuio_blk = blk_new_open(name, NULL, opts, flags, &local_err);
Max Reitz1b58b432015-02-05 13:58:20 -0500109 if (!qemuio_blk) {
Markus Armbrusterb9884682015-12-18 16:35:18 +0100110 error_reportf_err(local_err, "can't open%s%s: ",
111 name ? " device " : "", name ?: "");
Markus Armbrusterdbb651c2014-09-08 18:50:58 +0200112 return 1;
Devin Nakamura43642b32011-07-11 11:22:16 -0400113 }
Christoph Hellwig1db69472009-07-15 23:11:21 +0200114
Kevin Wolfe151fc12016-03-15 12:52:30 +0100115 blk_set_enable_write_cache(qemuio_blk, !writethrough);
Daniel P. Berrange8caf0212015-05-12 17:09:21 +0100116
Devin Nakamura43642b32011-07-11 11:22:16 -0400117 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000118}
119
Devin Nakamura43642b32011-07-11 11:22:16 -0400120static void open_help(void)
aliguorie3aff4f2009-04-05 19:14:04 +0000121{
Devin Nakamura43642b32011-07-11 11:22:16 -0400122 printf(
aliguorie3aff4f2009-04-05 19:14:04 +0000123"\n"
124" opens a new file in the requested mode\n"
125"\n"
126" Example:\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600127" 'open -n -o driver=raw /tmp/data' - opens raw data file read-write, uncached\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000128"\n"
129" Opens a file for subsequent use by all of the other qemu-io commands.\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000130" -r, -- open file read-only\n"
131" -s, -- use snapshot file\n"
Eric Blake0f404442017-10-05 14:02:43 -0500132" -C, -- use copy-on-read\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600133" -n, -- disable host cache, short for -t none\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800134" -U, -- force shared permissions\n"
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000135" -k, -- use kernel AIO implementation (Linux only, prefer use of -i)\n"
136" -i, -- use AIO mode (threads, native or io_uring)\n"
Eric Blakeb8d970f2016-05-07 21:16:41 -0600137" -t, -- use the given cache mode for the image\n"
138" -d, -- use the given discard mode for the image\n"
Max Reitzb543c5c2013-10-11 14:02:10 +0200139" -o, -- options to be given to the block driver"
aliguorie3aff4f2009-04-05 19:14:04 +0000140"\n");
141}
142
Max Reitzb32d7a32018-05-09 21:42:59 +0200143static int open_f(BlockBackend *blk, int argc, char **argv);
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000144
145static const cmdinfo_t open_cmd = {
Devin Nakamura43642b32011-07-11 11:22:16 -0400146 .name = "open",
147 .altname = "o",
148 .cfunc = open_f,
149 .argmin = 1,
150 .argmax = -1,
151 .flags = CMD_NOFILE_OK,
Eric Blake0f404442017-10-05 14:02:43 -0500152 .args = "[-rsCnkU] [-t cache] [-d discard] [-o options] [path]",
Devin Nakamura43642b32011-07-11 11:22:16 -0400153 .oneline = "open the file specified by path",
154 .help = open_help,
Blue Swirl22a2bdc2009-11-21 09:06:46 +0000155};
aliguorie3aff4f2009-04-05 19:14:04 +0000156
Max Reitzb543c5c2013-10-11 14:02:10 +0200157static QemuOptsList empty_opts = {
158 .name = "drive",
Markus Armbruster443422f2014-05-28 11:16:58 +0200159 .merge_lists = true,
Max Reitzb543c5c2013-10-11 14:02:10 +0200160 .head = QTAILQ_HEAD_INITIALIZER(empty_opts.head),
161 .desc = {
162 /* no elements => accept any params */
163 { /* end of list */ }
164 },
165};
166
Max Reitzb32d7a32018-05-09 21:42:59 +0200167static int open_f(BlockBackend *blk, int argc, char **argv)
aliguorie3aff4f2009-04-05 19:14:04 +0000168{
Eric Blakeb8d970f2016-05-07 21:16:41 -0600169 int flags = BDRV_O_UNMAP;
Devin Nakamura43642b32011-07-11 11:22:16 -0400170 int readonly = 0;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100171 bool writethrough = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400172 int c;
Max Reitzb32d7a32018-05-09 21:42:59 +0200173 int ret;
Max Reitzb543c5c2013-10-11 14:02:10 +0200174 QemuOpts *qopts;
Markus Armbruster443422f2014-05-28 11:16:58 +0200175 QDict *opts;
Fam Zheng459571f2017-05-03 00:35:41 +0800176 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000177
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000178 while ((c = getopt(argc, argv, "snCro:ki:t:d:U")) != -1) {
Devin Nakamura43642b32011-07-11 11:22:16 -0400179 switch (c) {
180 case 's':
181 flags |= BDRV_O_SNAPSHOT;
182 break;
183 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100184 flags |= BDRV_O_NOCACHE;
185 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400186 break;
Eric Blake0f404442017-10-05 14:02:43 -0500187 case 'C':
188 flags |= BDRV_O_COPY_ON_READ;
189 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400190 case 'r':
191 readonly = 1;
192 break;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600193 case 'k':
194 flags |= BDRV_O_NATIVE_AIO;
195 break;
196 case 't':
197 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
198 error_report("Invalid cache option: %s", optarg);
199 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200200 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600201 }
202 break;
203 case 'd':
204 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
205 error_report("Invalid discard option: %s", optarg);
206 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200207 return -EINVAL;
Eric Blakeb8d970f2016-05-07 21:16:41 -0600208 }
209 break;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000210 case 'i':
211 if (bdrv_parse_aio(optarg, &flags) < 0) {
212 error_report("Invalid aio option: %s", optarg);
213 qemu_opts_reset(&empty_opts);
214 return -EINVAL;
215 }
216 break;
Max Reitzb543c5c2013-10-11 14:02:10 +0200217 case 'o':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000218 if (imageOpts) {
219 printf("--image-opts and 'open -o' are mutually exclusive\n");
Eric Blakeb8d970f2016-05-07 21:16:41 -0600220 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200221 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000222 }
Markus Armbruster70b94332015-02-13 12:50:26 +0100223 if (!qemu_opts_parse_noisily(&empty_opts, optarg, false)) {
Markus Armbruster443422f2014-05-28 11:16:58 +0200224 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200225 return -EINVAL;
Max Reitzb543c5c2013-10-11 14:02:10 +0200226 }
Max Reitzb543c5c2013-10-11 14:02:10 +0200227 break;
Fam Zheng459571f2017-05-03 00:35:41 +0800228 case 'U':
229 force_share = true;
230 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400231 default:
Markus Armbruster443422f2014-05-28 11:16:58 +0200232 qemu_opts_reset(&empty_opts);
Max Reitzb444d0e2018-05-09 21:42:58 +0200233 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200234 return -EINVAL;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200235 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400236 }
aliguorie3aff4f2009-04-05 19:14:04 +0000237
Devin Nakamura43642b32011-07-11 11:22:16 -0400238 if (!readonly) {
239 flags |= BDRV_O_RDWR;
240 }
aliguorie3aff4f2009-04-05 19:14:04 +0000241
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000242 if (imageOpts && (optind == argc - 1)) {
243 if (!qemu_opts_parse_noisily(&empty_opts, argv[optind], false)) {
244 qemu_opts_reset(&empty_opts);
Max Reitzb32d7a32018-05-09 21:42:59 +0200245 return -EINVAL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000246 }
247 optind++;
248 }
249
Markus Armbruster443422f2014-05-28 11:16:58 +0200250 qopts = qemu_opts_find(&empty_opts, NULL);
251 opts = qopts ? qemu_opts_to_qdict(qopts, NULL) : NULL;
252 qemu_opts_reset(&empty_opts);
253
Max Reitzfd0fee32013-12-20 19:28:20 +0100254 if (optind == argc - 1) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200255 ret = openfile(argv[optind], flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100256 } else if (optind == argc) {
Max Reitzb32d7a32018-05-09 21:42:59 +0200257 ret = openfile(NULL, flags, writethrough, force_share, opts);
Max Reitzfd0fee32013-12-20 19:28:20 +0100258 } else {
Marc-André Lureaucb3e7f02018-04-19 17:01:43 +0200259 qobject_unref(opts);
Eric Blake64ebf552017-06-05 15:38:42 -0500260 qemuio_command_usage(&open_cmd);
Max Reitzb32d7a32018-05-09 21:42:59 +0200261 return -EINVAL;
Devin Nakamura43642b32011-07-11 11:22:16 -0400262 }
Max Reitzb32d7a32018-05-09 21:42:59 +0200263
264 if (ret) {
265 return -EINVAL;
266 }
267
268 return 0;
aliguorie3aff4f2009-04-05 19:14:04 +0000269}
270
Max Reitzb32d7a32018-05-09 21:42:59 +0200271static int quit_f(BlockBackend *blk, int argc, char **argv)
Kevin Wolfe681be72013-06-05 14:19:34 +0200272{
Max Reitzb444d0e2018-05-09 21:42:58 +0200273 quit_qemu_io = true;
Max Reitzb32d7a32018-05-09 21:42:59 +0200274 return 0;
Kevin Wolfe681be72013-06-05 14:19:34 +0200275}
276
277static const cmdinfo_t quit_cmd = {
278 .name = "quit",
279 .altname = "q",
280 .cfunc = quit_f,
281 .argmin = -1,
282 .argmax = -1,
283 .flags = CMD_FLAG_GLOBAL,
284 .oneline = "exit the program",
285};
286
aliguorie3aff4f2009-04-05 19:14:04 +0000287static void usage(const char *name)
288{
Devin Nakamura43642b32011-07-11 11:22:16 -0400289 printf(
Eric Blakee4e12bb2016-05-07 21:16:40 -0600290"Usage: %s [OPTIONS]... [-c STRING]... [file]\n"
Stefan Weil84844a22009-06-22 15:08:47 +0200291"QEMU Disk exerciser\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000292"\n"
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000293" --object OBJECTDEF define an object such as 'secret' for\n"
294" passwords and/or encryption keys\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600295" --image-opts treat file as option string\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400296" -c, --cmd STRING execute command with its arguments\n"
297" from the given string\n"
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100298" -f, --format FMT specifies the block driver to use\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000299" -r, --read-only export read-only\n"
300" -s, --snapshot use snapshot file\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600301" -n, --nocache disable host cache, short for -t none\n"
Eric Blake0f404442017-10-05 14:02:43 -0500302" -C, --copy-on-read enable copy-on-read\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000303" -m, --misalign misalign allocations for O_DIRECT\n"
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000304" -k, --native-aio use kernel AIO implementation\n"
305" (Linux only, prefer use of -i)\n"
306" -i, --aio=MODE use AIO mode (threads, native or io_uring)\n"
Kevin Wolf592fa072012-04-18 12:07:39 +0200307" -t, --cache=MODE use the given cache mode for the image\n"
Eric Blakee4e12bb2016-05-07 21:16:40 -0600308" -d, --discard=MODE use the given discard mode for the image\n"
Denis V. Luneve9a80852016-06-17 17:44:11 +0300309" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
310" specify tracing options\n"
311" see qemu-img(1) man page for full description\n"
Fam Zheng459571f2017-05-03 00:35:41 +0800312" -U, --force-share force shared permissions\n"
aliguorie3aff4f2009-04-05 19:14:04 +0000313" -h, --help display this help and exit\n"
314" -V, --version output version information and exit\n"
Maria Kustovad208cc32014-03-18 09:59:19 +0400315"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500316"See '%s -c help' for information on available commands.\n"
317"\n"
318QEMU_HELP_BOTTOM "\n",
Maria Kustovad208cc32014-03-18 09:59:19 +0400319 name, name);
aliguorie3aff4f2009-04-05 19:14:04 +0000320}
321
Kevin Wolfd1174f12013-06-05 14:19:37 +0200322static char *get_prompt(void)
323{
324 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
325
326 if (!prompt[0]) {
Marc-André Lureau336d3542022-02-21 14:11:47 +0400327 snprintf(prompt, sizeof(prompt), "%s> ", g_get_prgname());
Kevin Wolfd1174f12013-06-05 14:19:37 +0200328 }
329
330 return prompt;
331}
332
Marc-André Lureau9edc6312022-02-20 20:39:25 +0400333static void G_GNUC_PRINTF(2, 3) readline_printf_func(void *opaque,
Stefan Weild5d15072014-01-25 18:18:23 +0100334 const char *fmt, ...)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200335{
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100336 va_list ap;
337 va_start(ap, fmt);
338 vprintf(fmt, ap);
339 va_end(ap);
340}
341
342static void readline_flush_func(void *opaque)
343{
344 fflush(stdout);
345}
346
347static void readline_func(void *opaque, const char *str, void *readline_opaque)
348{
349 char **line = readline_opaque;
350 *line = g_strdup(str);
351}
352
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100353static void completion_match(const char *cmd, void *opaque)
354{
355 readline_add_completion(readline_state, cmd);
356}
357
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100358static void readline_completion_func(void *opaque, const char *str)
359{
Stefan Hajnoczi46940202013-11-14 11:54:18 +0100360 readline_set_completion_index(readline_state, strlen(str));
361 qemuio_complete_command(str, completion_match, NULL);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100362}
363
364static char *fetchline_readline(void)
365{
366 char *line = NULL;
367
368 readline_start(readline_state, get_prompt(), 0, readline_func, &line);
369 while (!line) {
370 int ch = getchar();
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000371 if (ttyEOF != 0x0 && ch == ttyEOF) {
372 printf("\n");
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100373 break;
374 }
375 readline_handle_byte(readline_state, ch);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200376 }
377 return line;
378}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200379
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100380#define MAXREADLINESZ 1024
381static char *fetchline_fgets(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200382{
383 char *p, *line = g_malloc(MAXREADLINESZ);
384
385 if (!fgets(line, MAXREADLINESZ, stdin)) {
386 g_free(line);
387 return NULL;
388 }
389
390 p = line + strlen(line);
391 if (p != line && p[-1] == '\n') {
392 p[-1] = '\0';
393 }
394
395 return line;
396}
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100397
398static char *fetchline(void)
399{
400 if (readline_state) {
401 return fetchline_readline();
402 } else {
403 return fetchline_fgets();
404 }
405}
Kevin Wolfd1174f12013-06-05 14:19:37 +0200406
407static void prep_fetchline(void *opaque)
408{
409 int *fetchable = opaque;
410
411 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
412 *fetchable= 1;
413}
414
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +0300415static int do_qemuio_command(const char *cmd)
416{
417 int ret;
418 AioContext *ctx =
419 qemuio_blk ? blk_get_aio_context(qemuio_blk) : qemu_get_aio_context();
420
421 aio_context_acquire(ctx);
422 ret = qemuio_command(qemuio_blk, cmd);
423 aio_context_release(ctx);
424
425 return ret;
426}
427
Max Reitz6b3aa842018-05-09 21:43:00 +0200428static int command_loop(void)
Kevin Wolfd1174f12013-06-05 14:19:37 +0200429{
Max Reitzb444d0e2018-05-09 21:42:58 +0200430 int i, fetchable = 0, prompted = 0;
Max Reitz6b3aa842018-05-09 21:43:00 +0200431 int ret, last_error = 0;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200432 char *input;
433
Max Reitzb444d0e2018-05-09 21:42:58 +0200434 for (i = 0; !quit_qemu_io && i < ncmdline; i++) {
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +0300435 ret = do_qemuio_command(cmdline[i]);
Max Reitz6b3aa842018-05-09 21:43:00 +0200436 if (ret < 0) {
437 last_error = ret;
438 }
Kevin Wolfd1174f12013-06-05 14:19:37 +0200439 }
440 if (cmdline) {
441 g_free(cmdline);
Max Reitz6b3aa842018-05-09 21:43:00 +0200442 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200443 }
444
Max Reitzb444d0e2018-05-09 21:42:58 +0200445 while (!quit_qemu_io) {
Kevin Wolfd1174f12013-06-05 14:19:37 +0200446 if (!prompted) {
447 printf("%s", get_prompt());
448 fflush(stdout);
449 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
450 prompted = 1;
451 }
452
453 main_loop_wait(false);
454
455 if (!fetchable) {
456 continue;
457 }
458
459 input = fetchline();
460 if (input == NULL) {
461 break;
462 }
Vladimir Sementsov-Ogievskiy78632a3d2021-04-23 16:42:33 +0300463 ret = do_qemuio_command(input);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200464 g_free(input);
465
Max Reitz6b3aa842018-05-09 21:43:00 +0200466 if (ret < 0) {
467 last_error = ret;
468 }
469
Kevin Wolfd1174f12013-06-05 14:19:37 +0200470 prompted = 0;
471 fetchable = 0;
472 }
473 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
Max Reitz6b3aa842018-05-09 21:43:00 +0200474
475 return last_error;
Kevin Wolfd1174f12013-06-05 14:19:37 +0200476}
477
478static void add_user_command(char *optarg)
479{
Markus Armbruster5839e532014-08-19 10:31:08 +0200480 cmdline = g_renew(char *, cmdline, ++ncmdline);
Kevin Wolfd1174f12013-06-05 14:19:37 +0200481 cmdline[ncmdline-1] = optarg;
482}
483
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100484static void reenable_tty_echo(void)
485{
486 qemu_set_tty_echo(STDIN_FILENO, true);
487}
488
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000489enum {
490 OPTION_OBJECT = 256,
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000491 OPTION_IMAGE_OPTS = 257,
Daniel P. Berrange9ba371b2016-02-17 10:10:16 +0000492};
493
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000494static QemuOptsList file_opts = {
495 .name = "file",
496 .implied_opt_name = "file",
497 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
498 .desc = {
499 /* no elements => accept any params */
500 { /* end of list */ }
501 },
502};
503
aliguorie3aff4f2009-04-05 19:14:04 +0000504int main(int argc, char **argv)
505{
Devin Nakamura43642b32011-07-11 11:22:16 -0400506 int readonly = 0;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000507 const char *sopt = "hVc:d:f:rsnCmki:t:T:U";
Devin Nakamura43642b32011-07-11 11:22:16 -0400508 const struct option lopt[] = {
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000509 { "help", no_argument, NULL, 'h' },
510 { "version", no_argument, NULL, 'V' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000511 { "cmd", required_argument, NULL, 'c' },
512 { "format", required_argument, NULL, 'f' },
513 { "read-only", no_argument, NULL, 'r' },
514 { "snapshot", no_argument, NULL, 's' },
515 { "nocache", no_argument, NULL, 'n' },
Eric Blake0f404442017-10-05 14:02:43 -0500516 { "copy-on-read", no_argument, NULL, 'C' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000517 { "misalign", no_argument, NULL, 'm' },
518 { "native-aio", no_argument, NULL, 'k' },
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000519 { "aio", required_argument, NULL, 'i' },
Daniel P. Berrangea5134162016-02-17 10:10:23 +0000520 { "discard", required_argument, NULL, 'd' },
521 { "cache", required_argument, NULL, 't' },
522 { "trace", required_argument, NULL, 'T' },
523 { "object", required_argument, NULL, OPTION_OBJECT },
524 { "image-opts", no_argument, NULL, OPTION_IMAGE_OPTS },
Fam Zheng459571f2017-05-03 00:35:41 +0800525 { "force-share", no_argument, 0, 'U'},
Devin Nakamura43642b32011-07-11 11:22:16 -0400526 { NULL, 0, NULL, 0 }
527 };
528 int c;
529 int opt_index = 0;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100530 int flags = BDRV_O_UNMAP;
Max Reitz6b3aa842018-05-09 21:43:00 +0200531 int ret;
Kevin Wolfe151fc12016-03-15 12:52:30 +0100532 bool writethrough = true;
Max Reitz1b58b432015-02-05 13:58:20 -0500533 QDict *opts = NULL;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000534 const char *format = NULL;
Fam Zheng459571f2017-05-03 00:35:41 +0800535 bool force_share = false;
aliguorie3aff4f2009-04-05 19:14:04 +0000536
MORITA Kazutaka526eda12013-07-23 17:30:11 +0900537#ifdef CONFIG_POSIX
538 signal(SIGPIPE, SIG_IGN);
539#endif
540
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100541 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100542 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100543 module_call_init(MODULE_INIT_TRACE);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800544 qemu_init_exec_dir(argv[0]);
aliguorie3aff4f2009-04-05 19:14:04 +0000545
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300546 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100547
Daniel P. Berrange064097d2016-02-10 18:41:01 +0000548 module_call_init(MODULE_INIT_QOM);
Denis V. Luneve9a80852016-06-17 17:44:11 +0300549 qemu_add_opts(&qemu_trace_opts);
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100550 bdrv_init();
551
Devin Nakamura43642b32011-07-11 11:22:16 -0400552 while ((c = getopt_long(argc, argv, sopt, lopt, &opt_index)) != -1) {
553 switch (c) {
554 case 's':
555 flags |= BDRV_O_SNAPSHOT;
556 break;
557 case 'n':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100558 flags |= BDRV_O_NOCACHE;
559 writethrough = false;
Devin Nakamura43642b32011-07-11 11:22:16 -0400560 break;
Eric Blake0f404442017-10-05 14:02:43 -0500561 case 'C':
562 flags |= BDRV_O_COPY_ON_READ;
563 break;
Paolo Bonzini9e8f1832013-02-08 14:06:11 +0100564 case 'd':
565 if (bdrv_parse_discard_flags(optarg, &flags) < 0) {
566 error_report("Invalid discard option: %s", optarg);
567 exit(1);
568 }
569 break;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100570 case 'f':
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000571 format = optarg;
Kevin Wolfbe6273d2014-11-20 16:27:06 +0100572 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400573 case 'c':
574 add_user_command(optarg);
575 break;
576 case 'r':
577 readonly = 1;
578 break;
579 case 'm':
Stefan Weilf9883882014-03-05 22:23:00 +0100580 qemuio_misalign = true;
Devin Nakamura43642b32011-07-11 11:22:16 -0400581 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400582 case 'k':
583 flags |= BDRV_O_NATIVE_AIO;
584 break;
Aarushi Mehta1c5a2ae2020-01-20 14:18:54 +0000585 case 'i':
586 if (bdrv_parse_aio(optarg, &flags) < 0) {
587 error_report("Invalid aio option: %s", optarg);
588 exit(1);
589 }
590 break;
Kevin Wolf592fa072012-04-18 12:07:39 +0200591 case 't':
Kevin Wolfe151fc12016-03-15 12:52:30 +0100592 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) < 0) {
Kevin Wolf592fa072012-04-18 12:07:39 +0200593 error_report("Invalid cache option: %s", optarg);
594 exit(1);
595 }
596 break;
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000597 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500598 trace_opt_parse(optarg);
Stefan Hajnoczid7bb72c2012-03-12 16:36:07 +0000599 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400600 case 'V':
Thomas Huth7e563bf2018-02-15 12:06:47 +0100601 printf("%s version " QEMU_FULL_VERSION "\n"
Marc-André Lureau336d3542022-02-21 14:11:47 +0400602 QEMU_COPYRIGHT "\n", g_get_prgname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400603 exit(0);
604 case 'h':
Marc-André Lureau336d3542022-02-21 14:11:47 +0400605 usage(g_get_prgname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400606 exit(0);
Fam Zheng459571f2017-05-03 00:35:41 +0800607 case 'U':
608 force_share = true;
609 break;
Kevin Wolfb3e79bc2021-02-17 12:56:45 +0100610 case OPTION_OBJECT:
611 user_creatable_process_cmdline(optarg);
612 break;
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000613 case OPTION_IMAGE_OPTS:
614 imageOpts = true;
615 break;
Devin Nakamura43642b32011-07-11 11:22:16 -0400616 default:
Marc-André Lureau336d3542022-02-21 14:11:47 +0400617 usage(g_get_prgname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400618 exit(1);
Naphtali Spreif5edb012010-01-17 16:48:13 +0200619 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400620 }
aliguorie3aff4f2009-04-05 19:14:04 +0000621
Devin Nakamura43642b32011-07-11 11:22:16 -0400622 if ((argc - optind) > 1) {
Marc-André Lureau336d3542022-02-21 14:11:47 +0400623 usage(g_get_prgname());
Devin Nakamura43642b32011-07-11 11:22:16 -0400624 exit(1);
625 }
aliguorie3aff4f2009-04-05 19:14:04 +0000626
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000627 if (format && imageOpts) {
628 error_report("--image-opts and -f are mutually exclusive");
629 exit(1);
630 }
631
Markus Armbrusterf9734d52021-07-20 14:53:53 +0200632 qemu_init_main_loop(&error_fatal);
Zhi Yong Wua57d1142012-02-19 22:24:59 +0800633
Denis V. Luneve9a80852016-06-17 17:44:11 +0300634 if (!trace_init_backends()) {
635 exit(1);
636 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500637 trace_init_file();
Richard Hendersonc5955f42022-04-17 11:29:44 -0700638 qemu_set_log(LOG_TRACE, &error_fatal);
Denis V. Luneve9a80852016-06-17 17:44:11 +0300639
Devin Nakamura43642b32011-07-11 11:22:16 -0400640 /* initialize commands */
Kevin Wolfc2cdf5c2013-06-05 14:19:36 +0200641 qemuio_add_command(&quit_cmd);
642 qemuio_add_command(&open_cmd);
643 qemuio_add_command(&close_cmd);
Devin Nakamura43642b32011-07-11 11:22:16 -0400644
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100645 if (isatty(STDIN_FILENO)) {
Daniel P. Berrange0e448a02018-02-12 18:48:49 +0000646 ttyEOF = get_eof_char();
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100647 readline_state = readline_init(readline_printf_func,
648 readline_flush_func,
649 NULL,
650 readline_completion_func);
651 qemu_set_tty_echo(STDIN_FILENO, false);
652 atexit(reenable_tty_echo);
653 }
654
Devin Nakamura43642b32011-07-11 11:22:16 -0400655 /* open the device */
656 if (!readonly) {
657 flags |= BDRV_O_RDWR;
658 }
659
660 if ((argc - optind) == 1) {
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000661 if (imageOpts) {
662 QemuOpts *qopts = NULL;
663 qopts = qemu_opts_parse_noisily(&file_opts, argv[optind], false);
664 if (!qopts) {
665 exit(1);
666 }
667 opts = qemu_opts_to_qdict(qopts, NULL);
Fam Zheng459571f2017-05-03 00:35:41 +0800668 if (openfile(NULL, flags, writethrough, force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200669 exit(1);
670 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000671 } else {
672 if (format) {
673 opts = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -0500674 qdict_put_str(opts, "driver", format);
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000675 }
Fam Zheng459571f2017-05-03 00:35:41 +0800676 if (openfile(argv[optind], flags, writethrough,
677 force_share, opts)) {
Nir Sofferb7aa1312017-02-01 02:31:18 +0200678 exit(1);
679 }
Daniel P. Berrange499afa22016-02-17 10:10:18 +0000680 }
Devin Nakamura43642b32011-07-11 11:22:16 -0400681 }
Max Reitz6b3aa842018-05-09 21:43:00 +0200682 ret = command_loop();
Devin Nakamura43642b32011-07-11 11:22:16 -0400683
684 /*
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000685 * Make sure all outstanding requests complete before the program exits.
Devin Nakamura43642b32011-07-11 11:22:16 -0400686 */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +0000687 bdrv_drain_all();
Devin Nakamura43642b32011-07-11 11:22:16 -0400688
Markus Armbruster26f54e92014-10-07 13:59:04 +0200689 blk_unref(qemuio_blk);
Stefan Hajnoczi0cf17e12013-11-14 11:54:17 +0100690 g_free(readline_state);
Max Reitz6b3aa842018-05-09 21:43:00 +0200691
692 if (ret < 0) {
693 return 1;
694 } else {
695 return 0;
696 }
aliguorie3aff4f2009-04-05 19:14:04 +0000697}