blob: 16473d809cd302ae6abf8e1f21cbec51b4b18b85 [file] [log] [blame]
thscd831bd2008-07-03 10:23:51 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard7a5ca862008-05-27 21:13:40 +000017 */
18
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050020#include <getopt.h>
21#include <libgen.h>
22#include <pthread.h>
23
Markus Armbrustera8d25322019-05-23 16:35:08 +020024#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020027#include "sysemu/block-backend.h"
Peter Lievenb3838a42014-08-13 19:20:18 +020028#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010029#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010030#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020031#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010032#include "qemu/option.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010033#include "qemu/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000034#include "qemu/config-file.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010035#include "qemu/bswap.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030036#include "qemu/log.h"
Paolo Bonzini53fabd42017-03-16 16:29:45 +010037#include "qemu/systemd.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080038#include "block/snapshot.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010039#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010040#include "qapi/qmp/qstring.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000041#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000042#include "io/channel-socket.h"
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000043#include "io/net-listener.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010044#include "crypto/init.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030045#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050046#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000047
Eric Blake3c1fa352018-12-15 07:53:08 -060048#ifdef __linux__
49#define HAVE_NBD_DEVICE 1
50#else
51#define HAVE_NBD_DEVICE 0
52#endif
53
Peter Lieven713cc672014-08-13 19:20:19 +020054#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000055#define QEMU_NBD_OPT_CACHE 256
56#define QEMU_NBD_OPT_AIO 257
57#define QEMU_NBD_OPT_DISCARD 258
58#define QEMU_NBD_OPT_DETECT_ZEROES 259
59#define QEMU_NBD_OPT_OBJECT 260
60#define QEMU_NBD_OPT_TLSCREDS 261
61#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020062#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000063#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020064#define QEMU_NBD_OPT_PID_FILE 265
bellard7a5ca862008-05-27 21:13:40 +000065
Eric Blakebd31c212016-05-06 10:26:42 -060066#define MBR_SIZE 512
67
Philippe Mathieu-Daudé9d976582019-01-11 17:35:19 +010068static NBDExport *export;
blueswir1b1d8e522008-10-26 13:43:07 +000069static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010070static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020071static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020072static int persistent = 0;
73static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020074static int shared = 1;
75static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000076static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000077static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000078static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000079
80static void usage(const char *name)
81{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020082 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000083"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060084" or: %s -L [OPTIONS]\n"
85"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000086"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020087" -h, --help display this help and exit\n"
88" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000089"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020090"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020091" -p, --port=PORT port to listen on (default `%d')\n"
92" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
93" -k, --socket=PATH path to the unix socket\n"
94" (default '"SOCKET_PATH"')\n"
95" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
96" -t, --persistent don't exit on the last connection\n"
97" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +030098" -x, --export-name=NAME expose export by name (default is empty string)\n"
99" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200100"\n"
101"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200102" -o, --offset=OFFSET offset into the image\n"
Eric Blake636192c2019-01-11 13:47:20 -0600103" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200104"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000105"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600106" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000107" --object type,id=ID,... define an object such as 'secret' for providing\n"
108" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500109" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000110" --tls-authz=ID use id of an earlier --object to provide\n"
111" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300112" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
113" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200114" --fork fork off the server process and exit the parent\n"
115" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200116" --pid-file=PATH store the server's process ID in the given file\n"
Eric Blake3c1fa352018-12-15 07:53:08 -0600117#if HAVE_NBD_DEVICE
118"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200119"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200120" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
121" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200122#endif
123"\n"
124"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200125" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
126" -r, --read-only export read-only\n"
127" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
128" file with backing_file=FILE, redirect the write to\n"
129" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800130" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200131" load an internal snapshot inside FILE and export it\n"
132" as an read-only device, SNAPSHOT_PARAM format is\n"
133" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
134" '[ID_OR_NAME]'\n"
135" -n, --nocache disable host cache\n"
136" --cache=MODE set cache mode (none, writeback, ...)\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000137" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200138" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300139" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000140" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200141"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500142QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600143 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000144}
145
146static void version(const char *name)
147{
148 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100149"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000150"Written by Anthony Liguori.\n"
151"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500152QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000153"This is free software; see the source for copying conditions. There is NO\n"
154"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000155 , name);
bellard7a5ca862008-05-27 21:13:40 +0000156}
157
Daniel P. Berrangé6e64dd52020-08-25 11:38:49 +0100158#if HAVE_NBD_DEVICE
Paolo Bonzinibb345112011-11-04 15:51:19 +0100159static void termsig_handler(int signum)
160{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100161 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200162 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100163}
Daniel P. Berrangé6e64dd52020-08-25 11:38:49 +0100164#endif /* HAVE_NBD_DEVICE */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100165
Eric Blake68b96f12019-01-17 13:36:56 -0600166static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
167 const char *hostname)
168{
169 int ret = EXIT_FAILURE;
170 int rc;
171 Error *err = NULL;
172 QIOChannelSocket *sioc;
173 NBDExportInfo *list;
174 int i, j;
175
176 sioc = qio_channel_socket_new();
177 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
178 error_report_err(err);
179 return EXIT_FAILURE;
180 }
181 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
182 &err);
183 if (rc < 0) {
184 if (err) {
185 error_report_err(err);
186 }
187 goto out;
188 }
189 printf("exports available: %d\n", rc);
190 for (i = 0; i < rc; i++) {
191 printf(" export: '%s'\n", list[i].name);
192 if (list[i].description && *list[i].description) {
193 printf(" description: %s\n", list[i].description);
194 }
195 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200196 static const char *const flag_names[] = {
197 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
198 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
199 [NBD_FLAG_SEND_FUA_BIT] = "fua",
200 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
201 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
202 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
203 [NBD_FLAG_SEND_DF_BIT] = "df",
204 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
205 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
206 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500207 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200208 };
209
Eric Blake68b96f12019-01-17 13:36:56 -0600210 printf(" size: %" PRIu64 "\n", list[i].size);
211 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200212 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
213 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
214 printf(" %s", flag_names[bit]);
215 }
Eric Blake68b96f12019-01-17 13:36:56 -0600216 }
217 printf(" )\n");
218 }
219 if (list[i].min_block) {
220 printf(" min block: %u\n", list[i].min_block);
221 printf(" opt block: %u\n", list[i].opt_block);
222 printf(" max block: %u\n", list[i].max_block);
223 }
224 if (list[i].n_contexts) {
225 printf(" available meta contexts: %d\n", list[i].n_contexts);
226 for (j = 0; j < list[i].n_contexts; j++) {
227 printf(" %s\n", list[i].contexts[j]);
228 }
229 }
230 }
231 nbd_free_export_list(list, rc);
232
233 ret = EXIT_SUCCESS;
234 out:
235 object_unref(OBJECT(sioc));
236 return ret;
237}
238
239
Eric Blake3c1fa352018-12-15 07:53:08 -0600240#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100241static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000242{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100243 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100244 int nbd;
thscd831bd2008-07-03 10:23:51 +0000245
Paolo Bonzinia517e882011-11-04 15:51:21 +0100246 /* linux just needs an open() to trigger
247 * the partition table update
248 * but remember to load the module with max_part != 0 :
249 * modprobe nbd max_part=63
250 */
251 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100252 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100253 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000254 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100255 return NULL;
256}
257
258static void *nbd_client_thread(void *arg)
259{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100260 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600261 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000262 QIOChannelSocket *sioc;
263 int fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100264 int ret;
265 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500266 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100267
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000268 sioc = qio_channel_socket_new();
269 if (qio_channel_socket_connect_sync(sioc,
270 saddr,
271 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100272 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000273 goto out;
274 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100275
Vladimir Sementsov-Ogievskiya8e2bb62019-06-18 14:43:21 +0300276 ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
Eric Blake004a89f2017-07-07 15:30:41 -0500277 NULL, NULL, NULL, &info, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100278 if (ret < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500279 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100280 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500281 }
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100282 goto out_socket;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100283 }
284
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100285 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100286 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100287 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100288 error_report("Failed to open %s: %m", device);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100289 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100290 }
291
Eric Blake004a89f2017-07-07 15:30:41 -0500292 ret = nbd_init(fd, sioc, &info, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100293 if (ret < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300294 error_report_err(local_error);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100295 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100296 }
297
298 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100299 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100300
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100301 if (verbose) {
302 fprintf(stderr, "NBD device %s is now connected to %s\n",
303 device, srcpath);
304 } else {
305 /* Close stderr so that the qemu-nbd process exits. */
306 dup2(STDOUT_FILENO, STDERR_FILENO);
307 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100308
309 ret = nbd_client(fd);
310 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100311 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100312 }
313 close(fd);
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000314 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600315 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100316 kill(getpid(), SIGTERM);
317 return (void *) EXIT_SUCCESS;
318
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100319out_fd:
320 close(fd);
321out_socket:
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000322 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100323out:
Eric Blake6dc16672019-01-17 13:36:46 -0600324 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100325 kill(getpid(), SIGTERM);
326 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000327}
Eric Blake3c1fa352018-12-15 07:53:08 -0600328#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000329
Fam Zhenge4afbf42015-05-19 10:50:59 +0000330static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200331{
Eric Blakedf8ad9f2017-05-26 22:04:21 -0500332 return state == RUNNING && nb_fds < shared;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200333}
334
Philippe Mathieu-Daudé9d976582019-01-11 17:35:19 +0100335static void nbd_export_closed(NBDExport *export)
Paolo Bonzini7860a382012-09-18 13:31:56 +0200336{
337 assert(state == TERMINATING);
338 state = TERMINATED;
339}
340
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000341static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000342
Eric Blake0c9390d2017-06-08 17:26:17 -0500343static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200344{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200345 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500346 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200347 state = TERMINATE;
348 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000349 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200350 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200351}
352
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000353static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
354 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200355{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200356 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000357 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200358 }
359
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800360 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000361 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000362 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200363}
364
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000365static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000366{
367 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000368 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000369 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000370 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000371 }
372}
373
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100374
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200375static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100376 const char *bindto,
377 const char *port)
378{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200379 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100380
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200381 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100382 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200383 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
384 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385 } else {
Eric Blake03992932016-03-03 09:16:48 -0700386 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200387 saddr->type = SOCKET_ADDRESS_TYPE_INET;
388 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700389 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100390 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700391 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100392 } else {
Eric Blake03992932016-03-03 09:16:48 -0700393 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100394 }
395 }
396
397 return saddr;
398}
399
400
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000401static QemuOptsList file_opts = {
402 .name = "file",
403 .implied_opt_name = "file",
404 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
405 .desc = {
406 /* no elements => accept any params */
407 { /* end of list */ }
408 },
409};
410
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000411static QemuOptsList qemu_object_opts = {
412 .name = "object",
413 .implied_opt_name = "qom-type",
414 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
415 .desc = {
416 { }
417 },
418};
419
Kevin Wolf495bf892019-10-11 21:49:17 +0200420static bool qemu_nbd_object_print_help(const char *type, QemuOpts *opts)
421{
422 if (user_creatable_print_help(type, opts)) {
423 exit(0);
424 }
425 return true;
426}
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000427
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000428
Eric Blake68b96f12019-01-17 13:36:56 -0600429static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
430 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000431{
432 Object *obj;
433 QCryptoTLSCreds *creds;
434
435 obj = object_resolve_path_component(
436 object_get_objects_root(), id);
437 if (!obj) {
438 error_setg(errp, "No TLS credentials with id '%s'",
439 id);
440 return NULL;
441 }
442 creds = (QCryptoTLSCreds *)
443 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
444 if (!creds) {
445 error_setg(errp, "Object with id '%s' is not TLS credentials",
446 id);
447 return NULL;
448 }
449
Eric Blake68b96f12019-01-17 13:36:56 -0600450 if (list) {
451 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
452 error_setg(errp,
453 "Expecting TLS credentials with a client endpoint");
454 return NULL;
455 }
456 } else {
457 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
458 error_setg(errp,
459 "Expecting TLS credentials with a server endpoint");
460 return NULL;
461 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000462 }
463 object_ref(obj);
464 return creds;
465}
466
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000467static void setup_address_and_port(const char **address, const char **port)
468{
469 if (*address == NULL) {
470 *address = "0.0.0.0";
471 }
472
473 if (*port == NULL) {
474 *port = stringify(NBD_DEFAULT_PORT);
475 }
476}
477
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000478/*
479 * Check socket parameters compatibility when socket activation is used.
480 */
481static const char *socket_activation_validate_opts(const char *device,
482 const char *sockpath,
483 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600484 const char *port,
485 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000486{
487 if (device != NULL) {
488 return "NBD device can't be set when using socket activation";
489 }
490
491 if (sockpath != NULL) {
492 return "Unix socket can't be set when using socket activation";
493 }
494
495 if (address != NULL) {
496 return "The interface can't be set when using socket activation";
497 }
498
499 if (port != NULL) {
500 return "TCP port number can't be set when using socket activation";
501 }
502
Eric Blake68b96f12019-01-17 13:36:56 -0600503 if (list) {
504 return "List mode is incompatible with socket activation";
505 }
506
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000507 return NULL;
508}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000509
Kevin Wolfb3b52992018-05-16 13:46:37 +0200510static void qemu_nbd_shutdown(void)
511{
512 job_cancel_sync_all();
513 bdrv_close_all();
514}
515
bellard7a5ca862008-05-27 21:13:40 +0000516int main(int argc, char **argv)
517{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200518 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000519 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600520 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500521 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000522 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000523 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100524 const char *port = NULL;
525 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100526 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800527 QemuOpts *sn_opts = NULL;
528 const char *sn_id_or_name = NULL;
Eric Blake0bc16992020-01-23 10:46:50 -0600529 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:B:L";
bellard7a5ca862008-05-27 21:13:40 +0000530 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000531 { "help", no_argument, NULL, 'h' },
532 { "version", no_argument, NULL, 'V' },
533 { "bind", required_argument, NULL, 'b' },
534 { "port", required_argument, NULL, 'p' },
535 { "socket", required_argument, NULL, 'k' },
536 { "offset", required_argument, NULL, 'o' },
537 { "read-only", no_argument, NULL, 'r' },
Eric Blake636192c2019-01-11 13:47:20 -0600538 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000539 { "connect", required_argument, NULL, 'c' },
540 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600541 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000542 { "snapshot", no_argument, NULL, 's' },
543 { "load-snapshot", required_argument, NULL, 'l' },
544 { "nocache", no_argument, NULL, 'n' },
545 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
546 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
547 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
548 { "detect-zeroes", required_argument, NULL,
549 QEMU_NBD_OPT_DETECT_ZEROES },
550 { "shared", required_argument, NULL, 'e' },
551 { "format", required_argument, NULL, 'f' },
552 { "persistent", no_argument, NULL, 't' },
553 { "verbose", no_argument, NULL, 'v' },
554 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
555 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500556 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000557 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000558 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000559 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300560 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200561 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200562 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Blue Swirl660f11b2009-07-31 21:16:51 +0000563 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000564 };
565 int ch;
566 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200567 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500568 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200569 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100570 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200571 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100572 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000573 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200574 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200575 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500576 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600577 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500578 const char *export_description = NULL;
Eric Blake636192c2019-01-11 13:47:20 -0600579 const char *bitmap = NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000580 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000581 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100582 bool writethrough = true;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300583 char *trace_file = NULL;
Max Reitzffb31e12016-09-28 22:46:42 +0200584 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600585 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200586 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000587 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200588 const char *pid_file_name = NULL;
bellard7a5ca862008-05-27 21:13:40 +0000589
Daniel P. Berrangé6e64dd52020-08-25 11:38:49 +0100590#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100591 /* The client thread uses SIGTERM to interrupt the server. A signal
592 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
593 */
Paolo Bonzinibb345112011-11-04 15:51:19 +0100594 struct sigaction sa_sigterm;
Paolo Bonzinibb345112011-11-04 15:51:19 +0100595 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
596 sa_sigterm.sa_handler = termsig_handler;
597 sigaction(SIGTERM, &sa_sigterm, NULL);
Daniel P. Berrangé6e64dd52020-08-25 11:38:49 +0100598#endif /* HAVE_NBD_DEVICE */
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100599
Max Reitz041e32b2017-06-11 14:37:14 +0200600#ifdef CONFIG_POSIX
601 signal(SIGPIPE, SIG_IGN);
602#endif
603
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100604 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100605 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100606 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300607 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100608
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000609 module_call_init(MODULE_INIT_QOM);
610 qemu_add_opts(&qemu_object_opts);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300611 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800612 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100613
bellard7a5ca862008-05-27 21:13:40 +0000614 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
615 switch (ch) {
616 case 's':
ths2f726482008-07-03 11:47:46 +0000617 flags |= BDRV_O_SNAPSHOT;
618 break;
619 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200620 optarg = (char *) "none";
621 /* fallthrough */
622 case QEMU_NBD_OPT_CACHE:
623 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100624 error_report("-n and --cache can only be specified once");
625 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200626 }
627 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100628 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100629 error_report("Invalid cache mode `%s'", optarg);
630 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200631 }
bellard7a5ca862008-05-27 21:13:40 +0000632 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200633 case QEMU_NBD_OPT_AIO:
634 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100635 error_report("--aio can only be specified once");
636 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200637 }
638 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000639 if (bdrv_parse_aio(optarg, &flags) < 0) {
640 error_report("Invalid aio mode '%s'", optarg);
641 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200642 }
643 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100644 case QEMU_NBD_OPT_DISCARD:
645 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100646 error_report("--discard can only be specified once");
647 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100648 }
649 seen_discard = true;
650 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100651 error_report("Invalid discard mode `%s'", optarg);
652 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100653 }
654 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200655 case QEMU_NBD_OPT_DETECT_ZEROES:
656 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200657 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200658 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200659 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
660 &local_err);
661 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100662 error_reportf_err(local_err,
663 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100664 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200665 }
666 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
667 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100668 error_report("setting detect-zeroes to unmap is not allowed "
669 "without setting discard operation to unmap");
670 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200671 }
672 break;
bellard7a5ca862008-05-27 21:13:40 +0000673 case 'b':
674 bindto = optarg;
675 break;
676 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100677 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000678 break;
679 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600680 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
681 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100682 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000683 }
bellard7a5ca862008-05-27 21:13:40 +0000684 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800685 case 'l':
686 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100687 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
688 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800689 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100690 error_report("Failed in parsing snapshot param `%s'",
691 optarg);
692 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800693 }
694 } else {
695 sn_id_or_name = optarg;
696 }
697 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000698 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500699 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200700 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000701 break;
Eric Blake636192c2019-01-11 13:47:20 -0600702 case 'B':
703 bitmap = optarg;
704 break;
thscd831bd2008-07-03 10:23:51 +0000705 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100706 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200707 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100708 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100709 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200710 }
thscd831bd2008-07-03 10:23:51 +0000711 break;
712 case 'd':
713 disconnect = true;
714 break;
715 case 'c':
716 device = optarg;
717 break;
ths3b05a8e2008-07-03 12:45:02 +0000718 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600719 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
720 shared < 1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100721 error_report("Invalid shared device number '%s'", optarg);
722 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000723 }
ths3b05a8e2008-07-03 12:45:02 +0000724 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000725 case 'f':
726 fmt = optarg;
727 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200728 case 't':
729 persistent = 1;
730 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000731 case 'x':
732 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600733 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
734 error_report("export name '%s' too long", export_name);
735 exit(EXIT_FAILURE);
736 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000737 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500738 case 'D':
739 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600740 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
741 error_report("export description '%s' too long",
742 export_description);
743 exit(EXIT_FAILURE);
744 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500745 break;
bellard7a5ca862008-05-27 21:13:40 +0000746 case 'v':
747 verbose = 1;
748 break;
749 case 'V':
750 version(argv[0]);
751 exit(0);
752 break;
753 case 'h':
754 usage(argv[0]);
755 exit(0);
756 break;
757 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100758 error_report("Try `%s --help' for more information.", argv[0]);
759 exit(EXIT_FAILURE);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000760 case QEMU_NBD_OPT_OBJECT: {
761 QemuOpts *opts;
762 opts = qemu_opts_parse_noisily(&qemu_object_opts,
763 optarg, true);
764 if (!opts) {
765 exit(EXIT_FAILURE);
766 }
767 } break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000768 case QEMU_NBD_OPT_TLSCREDS:
769 tlscredsid = optarg;
770 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000771 case QEMU_NBD_OPT_IMAGE_OPTS:
772 imageOpts = true;
773 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300774 case 'T':
775 g_free(trace_file);
776 trace_file = trace_opt_parse(optarg);
777 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000778 case QEMU_NBD_OPT_TLSAUTHZ:
779 tlsauthz = optarg;
780 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200781 case QEMU_NBD_OPT_FORK:
782 fork_process = true;
783 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600784 case 'L':
785 list = true;
786 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200787 case QEMU_NBD_OPT_PID_FILE:
788 pid_file_name = optarg;
789 break;
bellard7a5ca862008-05-27 21:13:40 +0000790 }
791 }
792
Eric Blake68b96f12019-01-17 13:36:56 -0600793 if (list) {
794 if (argc != optind) {
795 error_report("List mode is incompatible with a file name");
796 exit(EXIT_FAILURE);
797 }
Eric Blake0bc16992020-01-23 10:46:50 -0600798 if (export_name || export_description || dev_offset ||
Eric Blake68b96f12019-01-17 13:36:56 -0600799 device || disconnect || fmt || sn_id_or_name || bitmap ||
800 seen_aio || seen_discard || seen_cache) {
801 error_report("List mode is incompatible with per-device settings");
802 exit(EXIT_FAILURE);
803 }
804 if (fork_process) {
805 error_report("List mode is incompatible with forking");
806 exit(EXIT_FAILURE);
807 }
808 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100809 error_report("Invalid number of arguments");
810 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100811 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600812 } else if (!export_name) {
813 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000814 }
815
Markus Armbruster7e1e0c12018-10-17 10:26:43 +0200816 qemu_opts_foreach(&qemu_object_opts,
817 user_creatable_add_opts_foreach,
Kevin Wolf495bf892019-10-11 21:49:17 +0200818 qemu_nbd_object_print_help, &error_fatal);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000819
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300820 if (!trace_init_backends()) {
821 exit(1);
822 }
823 trace_init_file(trace_file);
824 qemu_set_log(LOG_TRACE);
825
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000826 socket_activation = check_socket_activation();
827 if (socket_activation == 0) {
828 setup_address_and_port(&bindto, &port);
829 } else {
830 /* Using socket activation - check user didn't use -p etc. */
831 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600832 bindto, port,
833 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000834 if (err_msg != NULL) {
835 error_report("%s", err_msg);
836 exit(EXIT_FAILURE);
837 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100838
839 /* qemu-nbd can only listen on a single socket. */
840 if (socket_activation > 1) {
841 error_report("qemu-nbd does not support socket activation with %s > 1",
842 "LISTEN_FDS");
843 exit(EXIT_FAILURE);
844 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000845 }
846
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000847 if (tlscredsid) {
848 if (sockpath) {
849 error_report("TLS is only supported with IPv4/IPv6");
850 exit(EXIT_FAILURE);
851 }
852 if (device) {
853 error_report("TLS is not supported with a host device");
854 exit(EXIT_FAILURE);
855 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000856 if (tlsauthz && list) {
857 error_report("TLS authorization is incompatible with export list");
858 exit(EXIT_FAILURE);
859 }
Eric Blake68b96f12019-01-17 13:36:56 -0600860 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000861 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200862 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000863 exit(EXIT_FAILURE);
864 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000865 } else {
866 if (tlsauthz) {
867 error_report("--tls-authz is not permitted without --tls-creds");
868 exit(EXIT_FAILURE);
869 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000870 }
871
Eric Blake68b96f12019-01-17 13:36:56 -0600872 if (list) {
873 saddr = nbd_build_socket_address(sockpath, bindto, port);
874 return qemu_nbd_client_list(saddr, tlscreds, bindto);
875 }
876
Eric Blake3c1fa352018-12-15 07:53:08 -0600877#if !HAVE_NBD_DEVICE
878 if (disconnect || device) {
879 error_report("Kernel /dev/nbdN support not available");
880 exit(EXIT_FAILURE);
881 }
882#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000883 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000884 int nbdfd = open(argv[optind], O_RDWR);
885 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100886 error_report("Cannot open %s: %s", argv[optind],
887 strerror(errno));
888 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100889 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000890 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000891
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000892 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000893
894 printf("%s disconnected\n", argv[optind]);
895
Peter Lieven713cc672014-08-13 19:20:19 +0200896 return 0;
thscd831bd2008-07-03 10:23:51 +0000897 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600898#endif
thscd831bd2008-07-03 10:23:51 +0000899
Max Reitzffb31e12016-09-28 22:46:42 +0200900 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100901#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100902 int stderr_fd[2];
903 pid_t pid;
904 int ret;
905
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100906 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100907 error_report("Error setting up communication pipe: %s",
908 strerror(errno));
909 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100910 }
911
912 /* Now daemonize, but keep a communication channel open to
913 * print errors and exit with the proper status code.
914 */
915 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500916 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100917 error_report("Failed to fork: %s", strerror(errno));
918 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500919 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100920 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200921
Raphael Pour0eaf4532020-05-15 08:36:07 +0200922 /* Remember parent's stderr if we will be restoring it. */
923 if (fork_process) {
924 old_stderr = dup(STDERR_FILENO);
925 }
926
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400927 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100928
929 /* Temporarily redirect stderr to the parent's pipe... */
930 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100931 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100932 error_report("Failed to daemonize: %s", strerror(errno));
933 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100934 }
935
936 /* ... close the descriptor we inherited and go on. */
937 close(stderr_fd[1]);
938 } else {
939 bool errors = false;
940 char *buf;
941
942 /* In the parent. Print error messages from the child until
943 * it closes the pipe.
944 */
945 close(stderr_fd[1]);
946 buf = g_malloc(1024);
947 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
948 errors = true;
949 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100950 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100951 exit(EXIT_FAILURE);
952 }
953 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100954 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100955 error_report("Cannot read from daemon: %s",
956 strerror(errno));
957 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100958 }
959
960 /* Usually the daemon should not print any message.
961 * Exit with zero status in that case.
962 */
963 exit(errors);
964 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100965#else /* WIN32 */
966 error_report("Unable to fork into background on Windows hosts");
967 exit(EXIT_FAILURE);
968#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100969 }
970
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100971 if (device != NULL && sockpath == NULL) {
972 sockpath = g_malloc(128);
973 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000974 }
975
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000976 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000977 if (socket_activation == 0) {
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000978 saddr = nbd_build_socket_address(sockpath, bindto, port);
Juan Quintelafc8135c2019-08-19 18:08:21 +0200979 if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000980 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000981 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000982 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000983 }
984 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000985 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000986 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000987 for (i = 0; i < socket_activation; i++) {
988 QIOChannelSocket *sioc;
989 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
990 &local_err);
991 if (sioc == NULL) {
992 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +0200993 error_reportf_err(local_err,
994 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000995 exit(EXIT_FAILURE);
996 }
997 qio_net_listener_add(server, sioc);
998 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000999 }
1000 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +01001001
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03001002 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +01001003 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +03001004 exit(EXIT_FAILURE);
1005 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001006 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001007 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001008
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001009 srcpath = argv[optind];
1010 if (imageOpts) {
1011 QemuOpts *opts;
1012 if (fmt) {
1013 error_report("--image-opts and -f are mutually exclusive");
1014 exit(EXIT_FAILURE);
1015 }
1016 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1017 if (!opts) {
1018 qemu_opts_reset(&file_opts);
1019 exit(EXIT_FAILURE);
1020 }
1021 options = qemu_opts_to_qdict(opts, NULL);
1022 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001023 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001024 } else {
1025 if (fmt) {
1026 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001027 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001028 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001029 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001030 }
1031
Max Reitz4fbec262015-02-05 13:58:19 -05001032 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001033 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1034 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001035 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001036 }
Max Reitz4fbec262015-02-05 13:58:19 -05001037 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001038
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001039 if (dev_offset) {
1040 QDict *raw_opts = qdict_new();
1041 qdict_put_str(raw_opts, "driver", "raw");
1042 qdict_put_str(raw_opts, "file", bs->node_name);
1043 qdict_put_int(raw_opts, "offset", dev_offset);
1044 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1045 blk_remove_bs(blk);
1046 blk_insert_bs(blk, bs, &error_fatal);
1047 bdrv_unref(bs);
1048 }
1049
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001050 blk_set_enable_write_cache(blk, !writethrough);
1051
Wenchao Xia8c116b02013-12-04 17:10:55 +08001052 if (sn_opts) {
1053 ret = bdrv_snapshot_load_tmp(bs,
1054 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1055 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1056 &local_err);
1057 } else if (sn_id_or_name) {
1058 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1059 &local_err);
1060 }
1061 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001062 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001063 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001064 }
1065
Peter Lievenb3838a42014-08-13 19:20:18 +02001066 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001067
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001068 export = nbd_export_new(bs, export_name,
Eric Blakedbb38ca2019-08-23 09:37:22 -05001069 export_description, bitmap, readonly, shared > 1,
Eric Blake678ba272019-01-11 13:47:19 -06001070 nbd_export_closed, writethrough, NULL,
1071 &error_fatal);
ths3b05a8e2008-07-03 12:45:02 +00001072
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001073 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001074#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001075 int ret;
1076
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001077 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001078 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001079 error_report("Failed to create client thread: %s", strerror(ret));
1080 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001081 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001082#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001083 } else {
1084 /* Shut up GCC warnings. */
1085 memset(&client_thread, 0, sizeof(client_thread));
1086 }
1087
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001088 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001089
Max Reitz637bc5a2019-05-08 23:18:16 +02001090 if (pid_file_name) {
1091 qemu_write_pidfile(pid_file_name, &error_fatal);
1092 }
1093
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001094 /* now when the initialization is (almost) complete, chdir("/")
1095 * to free any busy filesystems */
1096 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001097 error_report("Could not chdir to root directory: %s",
1098 strerror(errno));
1099 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001100 }
1101
Max Reitzffb31e12016-09-28 22:46:42 +02001102 if (fork_process) {
1103 dup2(old_stderr, STDERR_FILENO);
1104 close(old_stderr);
1105 }
1106
Paolo Bonzini7860a382012-09-18 13:31:56 +02001107 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001108 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001109 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001110 if (state == TERMINATE) {
1111 state = TERMINATING;
Philippe Mathieu-Daudé9d976582019-01-11 17:35:19 +01001112 nbd_export_close(export);
1113 nbd_export_put(export);
1114 export = NULL;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001115 }
1116 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001117
Markus Armbruster26f54e92014-10-07 13:59:04 +02001118 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001119 if (sockpath) {
1120 unlink(sockpath);
1121 }
bellard7a5ca862008-05-27 21:13:40 +00001122
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001123 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001124
Paolo Bonzinia517e882011-11-04 15:51:21 +01001125 if (device) {
1126 void *ret;
1127 pthread_join(client_thread, &ret);
1128 exit(ret != NULL);
1129 } else {
1130 exit(EXIT_SUCCESS);
1131 }
bellard7a5ca862008-05-27 21:13:40 +00001132}