blob: 77f98c736bbf6aadec77cb98b9be69597ba592f7 [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
Marc-André Lureau49f95222022-04-20 17:25:49 +040024#include "qemu/help-texts.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"
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +010028#include "sysemu/runstate.h" /* for qemu_system_killed() prototype */
Peter Lievenb3838a42014-08-13 19:20:18 +020029#include "block/block_int.h"
Paolo Bonzini737e1502012-12-17 18:19:44 +010030#include "block/nbd.h"
Alex Bligh6a1751b2013-08-21 16:02:47 +010031#include "qemu/main-loop.h"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020032#include "qemu/module.h"
Markus Armbruster922a01a2018-02-01 12:18:46 +010033#include "qemu/option.h"
Paolo Bonzini537b41f2014-02-17 14:43:51 +010034#include "qemu/error-report.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000035#include "qemu/config-file.h"
Paolo Bonzini58369e22016-03-15 17:22:36 +010036#include "qemu/bswap.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030037#include "qemu/log.h"
Paolo Bonzini53fabd42017-03-16 16:29:45 +010038#include "qemu/systemd.h"
Wenchao Xia8c116b02013-12-04 17:10:55 +080039#include "block/snapshot.h"
Markus Armbruster452fcdb2018-02-01 12:18:39 +010040#include "qapi/qmp/qdict.h"
Markus Armbrusterd49b6832015-03-17 18:29:20 +010041#include "qapi/qmp/qstring.h"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +000042#include "qom/object_interfaces.h"
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +000043#include "io/channel-socket.h"
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000044#include "io/net-listener.h"
Daniel P. Berrangec2297082016-04-06 12:12:06 +010045#include "crypto/init.h"
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +020046#include "crypto/tlscreds.h"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030047#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050048#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000049
Richard W.M. Jones3d212b42021-11-15 14:29:43 -060050#ifdef CONFIG_SELINUX
51#include <selinux/selinux.h>
52#endif
53
Eric Blake3c1fa352018-12-15 07:53:08 -060054#ifdef __linux__
55#define HAVE_NBD_DEVICE 1
56#else
57#define HAVE_NBD_DEVICE 0
58#endif
59
Peter Lieven713cc672014-08-13 19:20:19 +020060#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000061#define QEMU_NBD_OPT_CACHE 256
62#define QEMU_NBD_OPT_AIO 257
63#define QEMU_NBD_OPT_DISCARD 258
64#define QEMU_NBD_OPT_DETECT_ZEROES 259
65#define QEMU_NBD_OPT_OBJECT 260
66#define QEMU_NBD_OPT_TLSCREDS 261
67#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020068#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000069#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020070#define QEMU_NBD_OPT_PID_FILE 265
Richard W.M. Jones3d212b42021-11-15 14:29:43 -060071#define QEMU_NBD_OPT_SELINUX_LABEL 266
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +000072#define QEMU_NBD_OPT_TLSHOSTNAME 267
bellard7a5ca862008-05-27 21:13:40 +000073
Eric Blakebd31c212016-05-06 10:26:42 -060074#define MBR_SIZE 512
75
blueswir1b1d8e522008-10-26 13:43:07 +000076static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010077static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020078static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020079static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020080static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020081static int shared = 1;
82static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000083static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000084static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000085static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000086
87static void usage(const char *name)
88{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020089 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000090"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060091" or: %s -L [OPTIONS]\n"
92"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000093"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020094" -h, --help display this help and exit\n"
95" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000096"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020097"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020098" -p, --port=PORT port to listen on (default `%d')\n"
99" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
100" -k, --socket=PATH path to the unix socket\n"
101" (default '"SOCKET_PATH"')\n"
102" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
103" -t, --persistent don't exit on the last connection\n"
104" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +0300105" -x, --export-name=NAME expose export by name (default is empty string)\n"
106" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200107"\n"
108"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200109" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500110" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600111" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200112"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000113"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600114" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000115" --object type,id=ID,... define an object such as 'secret' for providing\n"
116" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500117" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000118" --tls-authz=ID use id of an earlier --object to provide\n"
119" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300120" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
121" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200122" --fork fork off the server process and exit the parent\n"
123" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200124" --pid-file=PATH store the server's process ID in the given file\n"
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600125#ifdef CONFIG_SELINUX
126" --selinux-label=LABEL set SELinux process label on listening socket\n"
127#endif
Eric Blake3c1fa352018-12-15 07:53:08 -0600128#if HAVE_NBD_DEVICE
129"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200130"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200131" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
132" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200133#endif
134"\n"
135"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200136" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
137" -r, --read-only export read-only\n"
138" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
139" file with backing_file=FILE, redirect the write to\n"
140" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800141" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200142" load an internal snapshot inside FILE and export it\n"
143" as an read-only device, SNAPSHOT_PARAM format is\n"
144" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
145" '[ID_OR_NAME]'\n"
146" -n, --nocache disable host cache\n"
Nir Soffer09615252021-08-13 23:55:19 +0300147" --cache=MODE set cache mode used to access the disk image, the\n"
148" valid options are: 'none', 'writeback' (default),\n"
149" 'writethrough', 'directsync' and 'unsafe'\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000150" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200151" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300152" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000153" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200154"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500155QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600156 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000157}
158
159static void version(const char *name)
160{
161 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100162"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000163"Written by Anthony Liguori.\n"
164"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500165QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000166"This is free software; see the source for copying conditions. There is NO\n"
167"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000168 , name);
bellard7a5ca862008-05-27 21:13:40 +0000169}
170
Eric Blake029a88c2020-09-30 07:11:01 -0500171#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100172/*
173 * The client thread uses SIGTERM to interrupt the server. A signal
174 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
175 */
176void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100177{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100178 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200179 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100180}
Eric Blake029a88c2020-09-30 07:11:01 -0500181#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100182
Eric Blake68b96f12019-01-17 13:36:56 -0600183static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
184 const char *hostname)
185{
186 int ret = EXIT_FAILURE;
187 int rc;
188 Error *err = NULL;
189 QIOChannelSocket *sioc;
190 NBDExportInfo *list;
191 int i, j;
192
193 sioc = qio_channel_socket_new();
194 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
195 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000196 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600197 }
198 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
199 &err);
200 if (rc < 0) {
201 if (err) {
202 error_report_err(err);
203 }
204 goto out;
205 }
206 printf("exports available: %d\n", rc);
207 for (i = 0; i < rc; i++) {
208 printf(" export: '%s'\n", list[i].name);
209 if (list[i].description && *list[i].description) {
210 printf(" description: %s\n", list[i].description);
211 }
212 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200213 static const char *const flag_names[] = {
214 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
215 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
216 [NBD_FLAG_SEND_FUA_BIT] = "fua",
217 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
218 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
219 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
220 [NBD_FLAG_SEND_DF_BIT] = "df",
221 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
222 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
223 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500224 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200225 };
226
Eric Blake68b96f12019-01-17 13:36:56 -0600227 printf(" size: %" PRIu64 "\n", list[i].size);
228 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200229 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
230 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
231 printf(" %s", flag_names[bit]);
232 }
Eric Blake68b96f12019-01-17 13:36:56 -0600233 }
234 printf(" )\n");
235 }
236 if (list[i].min_block) {
237 printf(" min block: %u\n", list[i].min_block);
238 printf(" opt block: %u\n", list[i].opt_block);
239 printf(" max block: %u\n", list[i].max_block);
240 }
241 if (list[i].n_contexts) {
242 printf(" available meta contexts: %d\n", list[i].n_contexts);
243 for (j = 0; j < list[i].n_contexts; j++) {
244 printf(" %s\n", list[i].contexts[j]);
245 }
246 }
247 }
248 nbd_free_export_list(list, rc);
249
250 ret = EXIT_SUCCESS;
251 out:
252 object_unref(OBJECT(sioc));
253 return ret;
254}
255
256
Eric Blake3c1fa352018-12-15 07:53:08 -0600257#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100258static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000259{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100260 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100261 int nbd;
thscd831bd2008-07-03 10:23:51 +0000262
Paolo Bonzinia517e882011-11-04 15:51:21 +0100263 /* linux just needs an open() to trigger
264 * the partition table update
265 * but remember to load the module with max_part != 0 :
266 * modprobe nbd max_part=63
267 */
268 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100269 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100270 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000271 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100272 return NULL;
273}
274
Denis V. Lunev03b67622023-07-17 16:55:40 +0200275struct NbdClientOpts {
276 char *device;
277};
278
Paolo Bonzinia517e882011-11-04 15:51:21 +0100279static void *nbd_client_thread(void *arg)
280{
Denis V. Lunev03b67622023-07-17 16:55:40 +0200281 struct NbdClientOpts *opts = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600282 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000283 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000284 int fd = -1;
285 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100286 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500287 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100288
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000289 sioc = qio_channel_socket_new();
290 if (qio_channel_socket_connect_sync(sioc,
291 saddr,
292 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100293 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000294 goto out;
295 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100296
Alex Chenaf74b552020-12-08 13:49:44 +0000297 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
298 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500299 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100300 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500301 }
Alex Chenaf74b552020-12-08 13:49:44 +0000302 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100303 }
304
Denis V. Lunev03b67622023-07-17 16:55:40 +0200305 fd = open(opts->device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100306 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100307 /* Linux-only, we can use %m in printf. */
Denis V. Lunev03b67622023-07-17 16:55:40 +0200308 error_report("Failed to open %s: %m", opts->device);
Alex Chenaf74b552020-12-08 13:49:44 +0000309 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100310 }
311
Alex Chenaf74b552020-12-08 13:49:44 +0000312 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300313 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000314 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100315 }
316
317 /* update partition table */
Denis V. Lunev03b67622023-07-17 16:55:40 +0200318 pthread_create(&show_parts_thread, NULL, show_parts, opts->device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100319
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100320 if (verbose) {
321 fprintf(stderr, "NBD device %s is now connected to %s\n",
Denis V. Lunev03b67622023-07-17 16:55:40 +0200322 opts->device, srcpath);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100323 } else {
324 /* Close stderr so that the qemu-nbd process exits. */
325 dup2(STDOUT_FILENO, STDERR_FILENO);
326 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100327
Alex Chenaf74b552020-12-08 13:49:44 +0000328 if (nbd_client(fd) < 0) {
329 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100330 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100331
Alex Chenaf74b552020-12-08 13:49:44 +0000332 ret = EXIT_SUCCESS;
333
334 out:
335 if (fd >= 0) {
336 close(fd);
337 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000338 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600339 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100340 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000341 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000342}
Eric Blake3c1fa352018-12-15 07:53:08 -0600343#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000344
Fam Zhenge4afbf42015-05-19 10:50:59 +0000345static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200346{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600347 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200348}
349
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000350static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000351
Eric Blake0c9390d2017-06-08 17:26:17 -0500352static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200353{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200354 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500355 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200356 state = TERMINATE;
357 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000358 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200359 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200360}
361
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000362static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
363 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200364{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200365 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000366 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200367 }
368
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800369 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000370 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000371 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200372}
373
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000374static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000375{
376 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000377 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000378 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000379 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000380 }
381}
382
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100383
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200384static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385 const char *bindto,
386 const char *port)
387{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200388 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100389
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200390 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100391 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200392 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
393 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100394 } else {
Eric Blake03992932016-03-03 09:16:48 -0700395 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200396 saddr->type = SOCKET_ADDRESS_TYPE_INET;
397 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700398 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100399 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700400 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100401 } else {
Eric Blake03992932016-03-03 09:16:48 -0700402 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100403 }
404 }
405
406 return saddr;
407}
408
409
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000410static QemuOptsList file_opts = {
411 .name = "file",
412 .implied_opt_name = "file",
413 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
414 .desc = {
415 /* no elements => accept any params */
416 { /* end of list */ }
417 },
418};
419
Eric Blake68b96f12019-01-17 13:36:56 -0600420static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
421 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000422{
423 Object *obj;
424 QCryptoTLSCreds *creds;
425
426 obj = object_resolve_path_component(
427 object_get_objects_root(), id);
428 if (!obj) {
429 error_setg(errp, "No TLS credentials with id '%s'",
430 id);
431 return NULL;
432 }
433 creds = (QCryptoTLSCreds *)
434 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
435 if (!creds) {
436 error_setg(errp, "Object with id '%s' is not TLS credentials",
437 id);
438 return NULL;
439 }
440
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +0200441 if (!qcrypto_tls_creds_check_endpoint(creds,
442 list
443 ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
444 : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
445 errp)) {
446 return NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000447 }
448 object_ref(obj);
449 return creds;
450}
451
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000452static void setup_address_and_port(const char **address, const char **port)
453{
454 if (*address == NULL) {
455 *address = "0.0.0.0";
456 }
457
458 if (*port == NULL) {
459 *port = stringify(NBD_DEFAULT_PORT);
460 }
461}
462
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000463/*
464 * Check socket parameters compatibility when socket activation is used.
465 */
466static const char *socket_activation_validate_opts(const char *device,
467 const char *sockpath,
468 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600469 const char *port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600470 const char *selinux,
Eric Blake68b96f12019-01-17 13:36:56 -0600471 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000472{
473 if (device != NULL) {
474 return "NBD device can't be set when using socket activation";
475 }
476
477 if (sockpath != NULL) {
478 return "Unix socket can't be set when using socket activation";
479 }
480
481 if (address != NULL) {
482 return "The interface can't be set when using socket activation";
483 }
484
485 if (port != NULL) {
486 return "TCP port number can't be set when using socket activation";
487 }
488
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600489 if (selinux != NULL) {
490 return "SELinux label can't be set when using socket activation";
491 }
492
Eric Blake68b96f12019-01-17 13:36:56 -0600493 if (list) {
494 return "List mode is incompatible with socket activation";
495 }
496
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000497 return NULL;
498}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000499
Kevin Wolfb3b52992018-05-16 13:46:37 +0200500static void qemu_nbd_shutdown(void)
501{
502 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100503 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200504 bdrv_close_all();
505}
506
bellard7a5ca862008-05-27 21:13:40 +0000507int main(int argc, char **argv)
508{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200509 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000510 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600511 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500512 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000513 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000514 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100515 const char *port = NULL;
516 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100517 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800518 QemuOpts *sn_opts = NULL;
519 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500520 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000521 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000522 { "help", no_argument, NULL, 'h' },
523 { "version", no_argument, NULL, 'V' },
524 { "bind", required_argument, NULL, 'b' },
525 { "port", required_argument, NULL, 'p' },
526 { "socket", required_argument, NULL, 'k' },
527 { "offset", required_argument, NULL, 'o' },
528 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500529 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600530 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000531 { "connect", required_argument, NULL, 'c' },
532 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600533 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000534 { "snapshot", no_argument, NULL, 's' },
535 { "load-snapshot", required_argument, NULL, 'l' },
536 { "nocache", no_argument, NULL, 'n' },
537 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
538 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
539 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
540 { "detect-zeroes", required_argument, NULL,
541 QEMU_NBD_OPT_DETECT_ZEROES },
542 { "shared", required_argument, NULL, 'e' },
543 { "format", required_argument, NULL, 'f' },
544 { "persistent", no_argument, NULL, 't' },
545 { "verbose", no_argument, NULL, 'v' },
546 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
547 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500548 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000549 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000550 { "tls-hostname", required_argument, NULL, QEMU_NBD_OPT_TLSHOSTNAME },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000551 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000552 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300553 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200554 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200555 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600556 { "selinux-label", required_argument, NULL,
557 QEMU_NBD_OPT_SELINUX_LABEL },
Blue Swirl660f11b2009-07-31 21:16:51 +0000558 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000559 };
560 int ch;
561 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200562 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500563 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200564 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100565 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200566 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100567 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000568 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200569 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200570 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500571 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600572 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500573 const char *export_description = NULL;
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300574 BlockDirtyBitmapOrStrList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500575 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000576 const char *tlscredsid = NULL;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000577 const char *tlshostname = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000578 bool imageOpts = false;
Nir Soffer09615252021-08-13 23:55:19 +0300579 bool writethrough = false; /* Client will flush as needed. */
Max Reitzffb31e12016-09-28 22:46:42 +0200580 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600581 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200582 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000583 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200584 const char *pid_file_name = NULL;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600585 const char *selinux_label = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200586 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000587
Eric Blake029a88c2020-09-30 07:11:01 -0500588#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100589 os_setup_early_signal_handling();
590 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200591#endif
592
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100593 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100594 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100595 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300596 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100597
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000598 module_call_init(MODULE_INIT_QOM);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300599 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800600 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100601
bellard7a5ca862008-05-27 21:13:40 +0000602 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
603 switch (ch) {
604 case 's':
ths2f726482008-07-03 11:47:46 +0000605 flags |= BDRV_O_SNAPSHOT;
606 break;
607 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200608 optarg = (char *) "none";
609 /* fallthrough */
610 case QEMU_NBD_OPT_CACHE:
611 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100612 error_report("-n and --cache can only be specified once");
613 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200614 }
615 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100616 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100617 error_report("Invalid cache mode `%s'", optarg);
618 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200619 }
bellard7a5ca862008-05-27 21:13:40 +0000620 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200621 case QEMU_NBD_OPT_AIO:
622 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100623 error_report("--aio can only be specified once");
624 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200625 }
626 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000627 if (bdrv_parse_aio(optarg, &flags) < 0) {
628 error_report("Invalid aio mode '%s'", optarg);
629 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200630 }
631 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100632 case QEMU_NBD_OPT_DISCARD:
633 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100634 error_report("--discard can only be specified once");
635 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100636 }
637 seen_discard = true;
638 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100639 error_report("Invalid discard mode `%s'", optarg);
640 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100641 }
642 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200643 case QEMU_NBD_OPT_DETECT_ZEROES:
644 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200645 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200646 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200647 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
648 &local_err);
649 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100650 error_reportf_err(local_err,
651 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100652 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200653 }
654 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
655 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100656 error_report("setting detect-zeroes to unmap is not allowed "
657 "without setting discard operation to unmap");
658 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200659 }
660 break;
bellard7a5ca862008-05-27 21:13:40 +0000661 case 'b':
662 bindto = optarg;
663 break;
664 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100665 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000666 break;
667 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600668 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
669 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100670 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000671 }
bellard7a5ca862008-05-27 21:13:40 +0000672 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800673 case 'l':
674 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100675 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
676 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800677 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100678 error_report("Failed in parsing snapshot param `%s'",
679 optarg);
680 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800681 }
682 } else {
683 sn_id_or_name = optarg;
684 }
685 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000686 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500687 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200688 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000689 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500690 case 'A':
691 alloc_depth = true;
692 break;
Eric Blake636192c2019-01-11 13:47:20 -0600693 case 'B':
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300694 {
695 BlockDirtyBitmapOrStr *el = g_new(BlockDirtyBitmapOrStr, 1);
696 *el = (BlockDirtyBitmapOrStr) {
697 .type = QTYPE_QSTRING,
698 .u.local = g_strdup(optarg),
699 };
700 QAPI_LIST_PREPEND(bitmaps, el);
701 }
Eric Blake636192c2019-01-11 13:47:20 -0600702 break;
thscd831bd2008-07-03 10:23:51 +0000703 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100704 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200705 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100706 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100707 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200708 }
thscd831bd2008-07-03 10:23:51 +0000709 break;
710 case 'd':
711 disconnect = true;
712 break;
713 case 'c':
714 device = optarg;
715 break;
ths3b05a8e2008-07-03 12:45:02 +0000716 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600717 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
Eric Blake3dcf56e2021-02-09 09:27:59 -0600718 shared < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100719 error_report("Invalid shared device number '%s'", optarg);
720 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000721 }
ths3b05a8e2008-07-03 12:45:02 +0000722 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000723 case 'f':
724 fmt = optarg;
725 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200726 case 't':
727 persistent = 1;
728 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000729 case 'x':
730 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600731 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
732 error_report("export name '%s' too long", export_name);
733 exit(EXIT_FAILURE);
734 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000735 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500736 case 'D':
737 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600738 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
739 error_report("export description '%s' too long",
740 export_description);
741 exit(EXIT_FAILURE);
742 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500743 break;
bellard7a5ca862008-05-27 21:13:40 +0000744 case 'v':
745 verbose = 1;
746 break;
747 case 'V':
748 version(argv[0]);
749 exit(0);
750 break;
751 case 'h':
752 usage(argv[0]);
753 exit(0);
754 break;
755 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100756 error_report("Try `%s --help' for more information.", argv[0]);
757 exit(EXIT_FAILURE);
Kevin Wolffa40e432021-02-17 12:56:45 +0100758 case QEMU_NBD_OPT_OBJECT:
759 user_creatable_process_cmdline(optarg);
760 break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000761 case QEMU_NBD_OPT_TLSCREDS:
762 tlscredsid = optarg;
763 break;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000764 case QEMU_NBD_OPT_TLSHOSTNAME:
765 tlshostname = optarg;
766 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000767 case QEMU_NBD_OPT_IMAGE_OPTS:
768 imageOpts = true;
769 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300770 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500771 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300772 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000773 case QEMU_NBD_OPT_TLSAUTHZ:
774 tlsauthz = optarg;
775 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200776 case QEMU_NBD_OPT_FORK:
777 fork_process = true;
778 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600779 case 'L':
780 list = true;
781 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200782 case QEMU_NBD_OPT_PID_FILE:
783 pid_file_name = optarg;
784 break;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600785 case QEMU_NBD_OPT_SELINUX_LABEL:
786 selinux_label = optarg;
787 break;
bellard7a5ca862008-05-27 21:13:40 +0000788 }
789 }
790
Eric Blake68b96f12019-01-17 13:36:56 -0600791 if (list) {
792 if (argc != optind) {
793 error_report("List mode is incompatible with a file name");
794 exit(EXIT_FAILURE);
795 }
Eric Blake0bc16992020-01-23 10:46:50 -0600796 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500797 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500798 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600799 error_report("List mode is incompatible with per-device settings");
800 exit(EXIT_FAILURE);
801 }
802 if (fork_process) {
803 error_report("List mode is incompatible with forking");
804 exit(EXIT_FAILURE);
805 }
806 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100807 error_report("Invalid number of arguments");
808 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100809 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600810 } else if (!export_name) {
811 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000812 }
813
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300814 if (!trace_init_backends()) {
815 exit(1);
816 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500817 trace_init_file();
Richard Hendersonc5955f42022-04-17 11:29:44 -0700818 qemu_set_log(LOG_TRACE, &error_fatal);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300819
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000820 socket_activation = check_socket_activation();
821 if (socket_activation == 0) {
Daniel P. Berrangée8ae8b12022-03-04 19:36:03 +0000822 if (!sockpath) {
823 setup_address_and_port(&bindto, &port);
824 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000825 } else {
826 /* Using socket activation - check user didn't use -p etc. */
827 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600828 bindto, port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600829 selinux_label,
Eric Blake68b96f12019-01-17 13:36:56 -0600830 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000831 if (err_msg != NULL) {
832 error_report("%s", err_msg);
833 exit(EXIT_FAILURE);
834 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100835
836 /* qemu-nbd can only listen on a single socket. */
837 if (socket_activation > 1) {
838 error_report("qemu-nbd does not support socket activation with %s > 1",
839 "LISTEN_FDS");
840 exit(EXIT_FAILURE);
841 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000842 }
843
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000844 if (tlscredsid) {
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000845 if (device) {
846 error_report("TLS is not supported with a host device");
847 exit(EXIT_FAILURE);
848 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000849 if (tlsauthz && list) {
850 error_report("TLS authorization is incompatible with export list");
851 exit(EXIT_FAILURE);
852 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000853 if (tlshostname && !list) {
854 error_report("TLS hostname is only supported with export list");
855 exit(EXIT_FAILURE);
856 }
Eric Blake68b96f12019-01-17 13:36:56 -0600857 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000858 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200859 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000860 exit(EXIT_FAILURE);
861 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000862 } else {
863 if (tlsauthz) {
864 error_report("--tls-authz is not permitted without --tls-creds");
865 exit(EXIT_FAILURE);
866 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000867 if (tlshostname) {
868 error_report("--tls-hostname is not permitted without --tls-creds");
869 exit(EXIT_FAILURE);
870 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000871 }
872
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600873 if (selinux_label) {
874#ifdef CONFIG_SELINUX
875 if (sockpath == NULL && device == NULL) {
876 error_report("--selinux-label is not permitted without --socket");
877 exit(EXIT_FAILURE);
878 }
879#else
880 error_report("SELinux support not enabled in this binary");
881 exit(EXIT_FAILURE);
882#endif
883 }
884
Eric Blake68b96f12019-01-17 13:36:56 -0600885 if (list) {
886 saddr = nbd_build_socket_address(sockpath, bindto, port);
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000887 return qemu_nbd_client_list(saddr, tlscreds,
888 tlshostname ? tlshostname : bindto);
Eric Blake68b96f12019-01-17 13:36:56 -0600889 }
890
Eric Blake3c1fa352018-12-15 07:53:08 -0600891#if !HAVE_NBD_DEVICE
892 if (disconnect || device) {
893 error_report("Kernel /dev/nbdN support not available");
894 exit(EXIT_FAILURE);
895 }
896#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000897 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000898 int nbdfd = open(argv[optind], O_RDWR);
899 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100900 error_report("Cannot open %s: %s", argv[optind],
901 strerror(errno));
902 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100903 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000904 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000905
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000906 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000907
908 printf("%s disconnected\n", argv[optind]);
909
Peter Lieven713cc672014-08-13 19:20:19 +0200910 return 0;
thscd831bd2008-07-03 10:23:51 +0000911 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600912#endif
thscd831bd2008-07-03 10:23:51 +0000913
Max Reitzffb31e12016-09-28 22:46:42 +0200914 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100915#ifndef WIN32
Marc-André Lureaua7241972022-03-29 15:21:00 +0400916 g_autoptr(GError) err = NULL;
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100917 int stderr_fd[2];
918 pid_t pid;
919 int ret;
920
Marc-André Lureaua7241972022-03-29 15:21:00 +0400921 if (!g_unix_open_pipe(stderr_fd, FD_CLOEXEC, &err)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100922 error_report("Error setting up communication pipe: %s",
Marc-André Lureaua7241972022-03-29 15:21:00 +0400923 err->message);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100924 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100925 }
926
927 /* Now daemonize, but keep a communication channel open to
928 * print errors and exit with the proper status code.
929 */
930 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500931 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100932 error_report("Failed to fork: %s", strerror(errno));
933 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500934 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100935 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200936
Raphael Pour0eaf4532020-05-15 08:36:07 +0200937 /* Remember parent's stderr if we will be restoring it. */
938 if (fork_process) {
939 old_stderr = dup(STDERR_FILENO);
940 }
941
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400942 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100943
944 /* Temporarily redirect stderr to the parent's pipe... */
945 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100946 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100947 error_report("Failed to daemonize: %s", strerror(errno));
948 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100949 }
950
951 /* ... close the descriptor we inherited and go on. */
952 close(stderr_fd[1]);
953 } else {
954 bool errors = false;
955 char *buf;
956
957 /* In the parent. Print error messages from the child until
958 * it closes the pipe.
959 */
960 close(stderr_fd[1]);
961 buf = g_malloc(1024);
962 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
963 errors = true;
964 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100965 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100966 exit(EXIT_FAILURE);
967 }
968 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100969 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100970 error_report("Cannot read from daemon: %s",
971 strerror(errno));
972 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100973 }
974
975 /* Usually the daemon should not print any message.
976 * Exit with zero status in that case.
977 */
978 exit(errors);
979 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100980#else /* WIN32 */
981 error_report("Unable to fork into background on Windows hosts");
982 exit(EXIT_FAILURE);
983#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100984 }
985
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100986 if (device != NULL && sockpath == NULL) {
987 sockpath = g_malloc(128);
988 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000989 }
990
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000991 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000992 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600993 int backlog;
994
Eric Blake3dcf56e2021-02-09 09:27:59 -0600995 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600996 backlog = SOMAXCONN;
997 } else {
998 backlog = MIN(shared, SOMAXCONN);
999 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001000#ifdef CONFIG_SELINUX
1001 if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) {
1002 error_report("Cannot set SELinux socket create context to %s: %s",
1003 selinux_label, strerror(errno));
1004 exit(EXIT_FAILURE);
1005 }
1006#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001007 saddr = nbd_build_socket_address(sockpath, bindto, port);
Eric Blake582d4212021-02-09 09:27:58 -06001008 if (qio_net_listener_open_sync(server, saddr, backlog,
1009 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001010 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001011 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001012 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001013 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001014#ifdef CONFIG_SELINUX
1015 if (selinux_label && setsockcreatecon_raw(NULL) == -1) {
1016 error_report("Cannot clear SELinux socket create context: %s",
1017 strerror(errno));
1018 exit(EXIT_FAILURE);
1019 }
1020#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001021 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001022 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001023 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001024 for (i = 0; i < socket_activation; i++) {
1025 QIOChannelSocket *sioc;
1026 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
1027 &local_err);
1028 if (sioc == NULL) {
1029 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +02001030 error_reportf_err(local_err,
1031 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001032 exit(EXIT_FAILURE);
1033 }
1034 qio_net_listener_add(server, sioc);
1035 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001036 }
1037 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +01001038
Markus Armbrusterf9734d52021-07-20 14:53:53 +02001039 qemu_init_main_loop(&error_fatal);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001040 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001041 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001042
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001043 srcpath = argv[optind];
1044 if (imageOpts) {
1045 QemuOpts *opts;
1046 if (fmt) {
1047 error_report("--image-opts and -f are mutually exclusive");
1048 exit(EXIT_FAILURE);
1049 }
1050 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1051 if (!opts) {
1052 qemu_opts_reset(&file_opts);
1053 exit(EXIT_FAILURE);
1054 }
1055 options = qemu_opts_to_qdict(opts, NULL);
1056 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001057 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001058 } else {
1059 if (fmt) {
1060 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001061 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001062 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001063 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001064 }
1065
Max Reitz4fbec262015-02-05 13:58:19 -05001066 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001067 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1068 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001069 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001070 }
Max Reitz4fbec262015-02-05 13:58:19 -05001071 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001072
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001073 if (dev_offset) {
1074 QDict *raw_opts = qdict_new();
1075 qdict_put_str(raw_opts, "driver", "raw");
1076 qdict_put_str(raw_opts, "file", bs->node_name);
1077 qdict_put_int(raw_opts, "offset", dev_offset);
Kevin Wolfc6e0a6d2023-05-25 14:47:04 +02001078
1079 aio_context_acquire(qemu_get_aio_context());
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001080 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
Kevin Wolfc6e0a6d2023-05-25 14:47:04 +02001081 aio_context_release(qemu_get_aio_context());
1082
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001083 blk_remove_bs(blk);
1084 blk_insert_bs(blk, bs, &error_fatal);
1085 bdrv_unref(bs);
1086 }
1087
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001088 blk_set_enable_write_cache(blk, !writethrough);
1089
Wenchao Xia8c116b02013-12-04 17:10:55 +08001090 if (sn_opts) {
1091 ret = bdrv_snapshot_load_tmp(bs,
1092 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1093 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1094 &local_err);
1095 } else if (sn_id_or_name) {
1096 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1097 &local_err);
1098 }
1099 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001100 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001101 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001102 }
1103
Peter Lievenb3838a42014-08-13 19:20:18 +02001104 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001105
Eric Blakea5fced42022-05-11 19:49:23 -05001106 nbd_server_is_qemu_nbd(shared);
Kevin Wolf00917172020-09-24 17:26:57 +02001107
1108 export_opts = g_new(BlockExportOptions, 1);
1109 *export_opts = (BlockExportOptions) {
1110 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001111 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001112 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001113 .has_writethrough = true,
1114 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001115 .has_writable = true,
1116 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001117 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001118 .name = g_strdup(export_name),
Eric Blakecbad81c2020-10-27 00:05:49 -05001119 .description = g_strdup(export_description),
1120 .has_bitmaps = !!bitmaps,
1121 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001122 .has_allocation_depth = alloc_depth,
1123 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001124 },
1125 };
1126 blk_exp_add(export_opts, &error_fatal);
1127 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001128
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001129 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001130#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001131 int ret;
Denis V. Lunev03b67622023-07-17 16:55:40 +02001132 struct NbdClientOpts opts = {
1133 .device = device,
1134 };
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001135
Denis V. Lunev03b67622023-07-17 16:55:40 +02001136 ret = pthread_create(&client_thread, NULL, nbd_client_thread, &opts);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001137 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001138 error_report("Failed to create client thread: %s", strerror(ret));
1139 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001140 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001141#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001142 } else {
1143 /* Shut up GCC warnings. */
1144 memset(&client_thread, 0, sizeof(client_thread));
1145 }
1146
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001147 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001148
Max Reitz637bc5a2019-05-08 23:18:16 +02001149 if (pid_file_name) {
1150 qemu_write_pidfile(pid_file_name, &error_fatal);
1151 }
1152
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001153 /* now when the initialization is (almost) complete, chdir("/")
1154 * to free any busy filesystems */
1155 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001156 error_report("Could not chdir to root directory: %s",
1157 strerror(errno));
1158 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001159 }
1160
Max Reitzffb31e12016-09-28 22:46:42 +02001161 if (fork_process) {
1162 dup2(old_stderr, STDERR_FILENO);
1163 close(old_stderr);
1164 }
1165
Paolo Bonzini7860a382012-09-18 13:31:56 +02001166 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001167 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001168 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001169 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001170 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001171 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001172 }
1173 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001174
Markus Armbruster26f54e92014-10-07 13:59:04 +02001175 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001176 if (sockpath) {
1177 unlink(sockpath);
1178 }
bellard7a5ca862008-05-27 21:13:40 +00001179
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001180 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001181
Paolo Bonzinia517e882011-11-04 15:51:21 +01001182 if (device) {
1183 void *ret;
1184 pthread_join(client_thread, &ret);
1185 exit(ret != NULL);
1186 } else {
1187 exit(EXIT_SUCCESS);
1188 }
bellard7a5ca862008-05-27 21:13:40 +00001189}