blob: 0cd5aa6f02bc2c0b4d8deb6257cbb6a7469daf8b [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
275static void *nbd_client_thread(void *arg)
276{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100277 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600278 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000279 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000280 int fd = -1;
281 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100282 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500283 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100284
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000285 sioc = qio_channel_socket_new();
286 if (qio_channel_socket_connect_sync(sioc,
287 saddr,
288 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100289 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000290 goto out;
291 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100292
Alex Chenaf74b552020-12-08 13:49:44 +0000293 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
294 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500295 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100296 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500297 }
Alex Chenaf74b552020-12-08 13:49:44 +0000298 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100299 }
300
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100301 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100302 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100303 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100304 error_report("Failed to open %s: %m", device);
Alex Chenaf74b552020-12-08 13:49:44 +0000305 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100306 }
307
Alex Chenaf74b552020-12-08 13:49:44 +0000308 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300309 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000310 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100311 }
312
313 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100314 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100315
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100316 if (verbose) {
317 fprintf(stderr, "NBD device %s is now connected to %s\n",
318 device, srcpath);
319 } else {
320 /* Close stderr so that the qemu-nbd process exits. */
321 dup2(STDOUT_FILENO, STDERR_FILENO);
322 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100323
Alex Chenaf74b552020-12-08 13:49:44 +0000324 if (nbd_client(fd) < 0) {
325 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100326 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100327
Alex Chenaf74b552020-12-08 13:49:44 +0000328 ret = EXIT_SUCCESS;
329
330 out:
331 if (fd >= 0) {
332 close(fd);
333 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000334 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600335 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100336 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000337 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000338}
Eric Blake3c1fa352018-12-15 07:53:08 -0600339#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000340
Fam Zhenge4afbf42015-05-19 10:50:59 +0000341static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200342{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600343 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200344}
345
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000346static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000347
Eric Blake0c9390d2017-06-08 17:26:17 -0500348static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200349{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200350 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500351 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200352 state = TERMINATE;
353 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000354 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200355 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200356}
357
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000358static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
359 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200360{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200361 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000362 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200363 }
364
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800365 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000366 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000367 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200368}
369
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000370static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000371{
372 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000373 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000374 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000375 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000376 }
377}
378
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100379
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200380static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100381 const char *bindto,
382 const char *port)
383{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200384 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200386 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100387 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200388 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
389 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100390 } else {
Eric Blake03992932016-03-03 09:16:48 -0700391 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200392 saddr->type = SOCKET_ADDRESS_TYPE_INET;
393 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700394 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100395 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700396 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100397 } else {
Eric Blake03992932016-03-03 09:16:48 -0700398 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100399 }
400 }
401
402 return saddr;
403}
404
405
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000406static QemuOptsList file_opts = {
407 .name = "file",
408 .implied_opt_name = "file",
409 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
410 .desc = {
411 /* no elements => accept any params */
412 { /* end of list */ }
413 },
414};
415
Eric Blake68b96f12019-01-17 13:36:56 -0600416static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
417 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000418{
419 Object *obj;
420 QCryptoTLSCreds *creds;
421
422 obj = object_resolve_path_component(
423 object_get_objects_root(), id);
424 if (!obj) {
425 error_setg(errp, "No TLS credentials with id '%s'",
426 id);
427 return NULL;
428 }
429 creds = (QCryptoTLSCreds *)
430 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
431 if (!creds) {
432 error_setg(errp, "Object with id '%s' is not TLS credentials",
433 id);
434 return NULL;
435 }
436
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +0200437 if (!qcrypto_tls_creds_check_endpoint(creds,
438 list
439 ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
440 : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
441 errp)) {
442 return NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000443 }
444 object_ref(obj);
445 return creds;
446}
447
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000448static void setup_address_and_port(const char **address, const char **port)
449{
450 if (*address == NULL) {
451 *address = "0.0.0.0";
452 }
453
454 if (*port == NULL) {
455 *port = stringify(NBD_DEFAULT_PORT);
456 }
457}
458
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000459/*
460 * Check socket parameters compatibility when socket activation is used.
461 */
462static const char *socket_activation_validate_opts(const char *device,
463 const char *sockpath,
464 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600465 const char *port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600466 const char *selinux,
Eric Blake68b96f12019-01-17 13:36:56 -0600467 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000468{
469 if (device != NULL) {
470 return "NBD device can't be set when using socket activation";
471 }
472
473 if (sockpath != NULL) {
474 return "Unix socket can't be set when using socket activation";
475 }
476
477 if (address != NULL) {
478 return "The interface can't be set when using socket activation";
479 }
480
481 if (port != NULL) {
482 return "TCP port number can't be set when using socket activation";
483 }
484
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600485 if (selinux != NULL) {
486 return "SELinux label can't be set when using socket activation";
487 }
488
Eric Blake68b96f12019-01-17 13:36:56 -0600489 if (list) {
490 return "List mode is incompatible with socket activation";
491 }
492
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000493 return NULL;
494}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000495
Kevin Wolfb3b52992018-05-16 13:46:37 +0200496static void qemu_nbd_shutdown(void)
497{
498 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100499 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200500 bdrv_close_all();
501}
502
bellard7a5ca862008-05-27 21:13:40 +0000503int main(int argc, char **argv)
504{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200505 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000506 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600507 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500508 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000509 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000510 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100511 const char *port = NULL;
512 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100513 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800514 QemuOpts *sn_opts = NULL;
515 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500516 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000517 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000518 { "help", no_argument, NULL, 'h' },
519 { "version", no_argument, NULL, 'V' },
520 { "bind", required_argument, NULL, 'b' },
521 { "port", required_argument, NULL, 'p' },
522 { "socket", required_argument, NULL, 'k' },
523 { "offset", required_argument, NULL, 'o' },
524 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500525 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600526 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000527 { "connect", required_argument, NULL, 'c' },
528 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600529 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000530 { "snapshot", no_argument, NULL, 's' },
531 { "load-snapshot", required_argument, NULL, 'l' },
532 { "nocache", no_argument, NULL, 'n' },
533 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
534 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
535 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
536 { "detect-zeroes", required_argument, NULL,
537 QEMU_NBD_OPT_DETECT_ZEROES },
538 { "shared", required_argument, NULL, 'e' },
539 { "format", required_argument, NULL, 'f' },
540 { "persistent", no_argument, NULL, 't' },
541 { "verbose", no_argument, NULL, 'v' },
542 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
543 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500544 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000545 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000546 { "tls-hostname", required_argument, NULL, QEMU_NBD_OPT_TLSHOSTNAME },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000547 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000548 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300549 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200550 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200551 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600552 { "selinux-label", required_argument, NULL,
553 QEMU_NBD_OPT_SELINUX_LABEL },
Blue Swirl660f11b2009-07-31 21:16:51 +0000554 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000555 };
556 int ch;
557 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200558 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500559 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200560 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100561 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200562 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100563 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000564 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200565 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200566 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500567 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600568 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500569 const char *export_description = NULL;
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300570 BlockDirtyBitmapOrStrList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500571 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000572 const char *tlscredsid = NULL;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000573 const char *tlshostname = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000574 bool imageOpts = false;
Nir Soffer09615252021-08-13 23:55:19 +0300575 bool writethrough = false; /* Client will flush as needed. */
Max Reitzffb31e12016-09-28 22:46:42 +0200576 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600577 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200578 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000579 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200580 const char *pid_file_name = NULL;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600581 const char *selinux_label = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200582 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000583
Eric Blake029a88c2020-09-30 07:11:01 -0500584#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100585 os_setup_early_signal_handling();
586 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200587#endif
588
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100589 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100590 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100591 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300592 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100593
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000594 module_call_init(MODULE_INIT_QOM);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300595 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800596 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100597
bellard7a5ca862008-05-27 21:13:40 +0000598 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
599 switch (ch) {
600 case 's':
ths2f726482008-07-03 11:47:46 +0000601 flags |= BDRV_O_SNAPSHOT;
602 break;
603 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200604 optarg = (char *) "none";
605 /* fallthrough */
606 case QEMU_NBD_OPT_CACHE:
607 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100608 error_report("-n and --cache can only be specified once");
609 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200610 }
611 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100612 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100613 error_report("Invalid cache mode `%s'", optarg);
614 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200615 }
bellard7a5ca862008-05-27 21:13:40 +0000616 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200617 case QEMU_NBD_OPT_AIO:
618 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100619 error_report("--aio can only be specified once");
620 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200621 }
622 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000623 if (bdrv_parse_aio(optarg, &flags) < 0) {
624 error_report("Invalid aio mode '%s'", optarg);
625 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200626 }
627 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100628 case QEMU_NBD_OPT_DISCARD:
629 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100630 error_report("--discard can only be specified once");
631 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100632 }
633 seen_discard = true;
634 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100635 error_report("Invalid discard mode `%s'", optarg);
636 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100637 }
638 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200639 case QEMU_NBD_OPT_DETECT_ZEROES:
640 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200641 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200642 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200643 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
644 &local_err);
645 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100646 error_reportf_err(local_err,
647 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100648 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200649 }
650 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
651 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100652 error_report("setting detect-zeroes to unmap is not allowed "
653 "without setting discard operation to unmap");
654 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200655 }
656 break;
bellard7a5ca862008-05-27 21:13:40 +0000657 case 'b':
658 bindto = optarg;
659 break;
660 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100661 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000662 break;
663 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600664 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
665 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100666 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000667 }
bellard7a5ca862008-05-27 21:13:40 +0000668 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800669 case 'l':
670 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100671 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
672 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800673 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100674 error_report("Failed in parsing snapshot param `%s'",
675 optarg);
676 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800677 }
678 } else {
679 sn_id_or_name = optarg;
680 }
681 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000682 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500683 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200684 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000685 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500686 case 'A':
687 alloc_depth = true;
688 break;
Eric Blake636192c2019-01-11 13:47:20 -0600689 case 'B':
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300690 {
691 BlockDirtyBitmapOrStr *el = g_new(BlockDirtyBitmapOrStr, 1);
692 *el = (BlockDirtyBitmapOrStr) {
693 .type = QTYPE_QSTRING,
694 .u.local = g_strdup(optarg),
695 };
696 QAPI_LIST_PREPEND(bitmaps, el);
697 }
Eric Blake636192c2019-01-11 13:47:20 -0600698 break;
thscd831bd2008-07-03 10:23:51 +0000699 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100700 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200701 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100702 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100703 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200704 }
thscd831bd2008-07-03 10:23:51 +0000705 break;
706 case 'd':
707 disconnect = true;
708 break;
709 case 'c':
710 device = optarg;
711 break;
ths3b05a8e2008-07-03 12:45:02 +0000712 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600713 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
Eric Blake3dcf56e2021-02-09 09:27:59 -0600714 shared < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100715 error_report("Invalid shared device number '%s'", optarg);
716 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000717 }
ths3b05a8e2008-07-03 12:45:02 +0000718 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000719 case 'f':
720 fmt = optarg;
721 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200722 case 't':
723 persistent = 1;
724 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000725 case 'x':
726 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600727 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
728 error_report("export name '%s' too long", export_name);
729 exit(EXIT_FAILURE);
730 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000731 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500732 case 'D':
733 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600734 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
735 error_report("export description '%s' too long",
736 export_description);
737 exit(EXIT_FAILURE);
738 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500739 break;
bellard7a5ca862008-05-27 21:13:40 +0000740 case 'v':
741 verbose = 1;
742 break;
743 case 'V':
744 version(argv[0]);
745 exit(0);
746 break;
747 case 'h':
748 usage(argv[0]);
749 exit(0);
750 break;
751 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100752 error_report("Try `%s --help' for more information.", argv[0]);
753 exit(EXIT_FAILURE);
Kevin Wolffa40e432021-02-17 12:56:45 +0100754 case QEMU_NBD_OPT_OBJECT:
755 user_creatable_process_cmdline(optarg);
756 break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000757 case QEMU_NBD_OPT_TLSCREDS:
758 tlscredsid = optarg;
759 break;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000760 case QEMU_NBD_OPT_TLSHOSTNAME:
761 tlshostname = optarg;
762 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000763 case QEMU_NBD_OPT_IMAGE_OPTS:
764 imageOpts = true;
765 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300766 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500767 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300768 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000769 case QEMU_NBD_OPT_TLSAUTHZ:
770 tlsauthz = optarg;
771 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200772 case QEMU_NBD_OPT_FORK:
773 fork_process = true;
774 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600775 case 'L':
776 list = true;
777 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200778 case QEMU_NBD_OPT_PID_FILE:
779 pid_file_name = optarg;
780 break;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600781 case QEMU_NBD_OPT_SELINUX_LABEL:
782 selinux_label = optarg;
783 break;
bellard7a5ca862008-05-27 21:13:40 +0000784 }
785 }
786
Eric Blake68b96f12019-01-17 13:36:56 -0600787 if (list) {
788 if (argc != optind) {
789 error_report("List mode is incompatible with a file name");
790 exit(EXIT_FAILURE);
791 }
Eric Blake0bc16992020-01-23 10:46:50 -0600792 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500793 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500794 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600795 error_report("List mode is incompatible with per-device settings");
796 exit(EXIT_FAILURE);
797 }
798 if (fork_process) {
799 error_report("List mode is incompatible with forking");
800 exit(EXIT_FAILURE);
801 }
802 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100803 error_report("Invalid number of arguments");
804 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100805 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600806 } else if (!export_name) {
807 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000808 }
809
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300810 if (!trace_init_backends()) {
811 exit(1);
812 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500813 trace_init_file();
Richard Hendersonc5955f42022-04-17 11:29:44 -0700814 qemu_set_log(LOG_TRACE, &error_fatal);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300815
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000816 socket_activation = check_socket_activation();
817 if (socket_activation == 0) {
Daniel P. Berrangée8ae8b12022-03-04 19:36:03 +0000818 if (!sockpath) {
819 setup_address_and_port(&bindto, &port);
820 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000821 } else {
822 /* Using socket activation - check user didn't use -p etc. */
823 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600824 bindto, port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600825 selinux_label,
Eric Blake68b96f12019-01-17 13:36:56 -0600826 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000827 if (err_msg != NULL) {
828 error_report("%s", err_msg);
829 exit(EXIT_FAILURE);
830 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100831
832 /* qemu-nbd can only listen on a single socket. */
833 if (socket_activation > 1) {
834 error_report("qemu-nbd does not support socket activation with %s > 1",
835 "LISTEN_FDS");
836 exit(EXIT_FAILURE);
837 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000838 }
839
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000840 if (tlscredsid) {
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000841 if (device) {
842 error_report("TLS is not supported with a host device");
843 exit(EXIT_FAILURE);
844 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000845 if (tlsauthz && list) {
846 error_report("TLS authorization is incompatible with export list");
847 exit(EXIT_FAILURE);
848 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000849 if (tlshostname && !list) {
850 error_report("TLS hostname is only supported with export list");
851 exit(EXIT_FAILURE);
852 }
Eric Blake68b96f12019-01-17 13:36:56 -0600853 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000854 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200855 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000856 exit(EXIT_FAILURE);
857 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000858 } else {
859 if (tlsauthz) {
860 error_report("--tls-authz is not permitted without --tls-creds");
861 exit(EXIT_FAILURE);
862 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000863 if (tlshostname) {
864 error_report("--tls-hostname is not permitted without --tls-creds");
865 exit(EXIT_FAILURE);
866 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000867 }
868
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600869 if (selinux_label) {
870#ifdef CONFIG_SELINUX
871 if (sockpath == NULL && device == NULL) {
872 error_report("--selinux-label is not permitted without --socket");
873 exit(EXIT_FAILURE);
874 }
875#else
876 error_report("SELinux support not enabled in this binary");
877 exit(EXIT_FAILURE);
878#endif
879 }
880
Eric Blake68b96f12019-01-17 13:36:56 -0600881 if (list) {
882 saddr = nbd_build_socket_address(sockpath, bindto, port);
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000883 return qemu_nbd_client_list(saddr, tlscreds,
884 tlshostname ? tlshostname : bindto);
Eric Blake68b96f12019-01-17 13:36:56 -0600885 }
886
Eric Blake3c1fa352018-12-15 07:53:08 -0600887#if !HAVE_NBD_DEVICE
888 if (disconnect || device) {
889 error_report("Kernel /dev/nbdN support not available");
890 exit(EXIT_FAILURE);
891 }
892#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000893 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000894 int nbdfd = open(argv[optind], O_RDWR);
895 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100896 error_report("Cannot open %s: %s", argv[optind],
897 strerror(errno));
898 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100899 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000900 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000901
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000902 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000903
904 printf("%s disconnected\n", argv[optind]);
905
Peter Lieven713cc672014-08-13 19:20:19 +0200906 return 0;
thscd831bd2008-07-03 10:23:51 +0000907 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600908#endif
thscd831bd2008-07-03 10:23:51 +0000909
Max Reitzffb31e12016-09-28 22:46:42 +0200910 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100911#ifndef WIN32
Marc-André Lureaua7241972022-03-29 15:21:00 +0400912 g_autoptr(GError) err = NULL;
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100913 int stderr_fd[2];
914 pid_t pid;
915 int ret;
916
Marc-André Lureaua7241972022-03-29 15:21:00 +0400917 if (!g_unix_open_pipe(stderr_fd, FD_CLOEXEC, &err)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100918 error_report("Error setting up communication pipe: %s",
Marc-André Lureaua7241972022-03-29 15:21:00 +0400919 err->message);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100920 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100921 }
922
923 /* Now daemonize, but keep a communication channel open to
924 * print errors and exit with the proper status code.
925 */
926 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500927 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100928 error_report("Failed to fork: %s", strerror(errno));
929 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500930 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100931 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200932
Raphael Pour0eaf4532020-05-15 08:36:07 +0200933 /* Remember parent's stderr if we will be restoring it. */
934 if (fork_process) {
935 old_stderr = dup(STDERR_FILENO);
936 }
937
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400938 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100939
940 /* Temporarily redirect stderr to the parent's pipe... */
941 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100942 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100943 error_report("Failed to daemonize: %s", strerror(errno));
944 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100945 }
946
947 /* ... close the descriptor we inherited and go on. */
948 close(stderr_fd[1]);
949 } else {
950 bool errors = false;
951 char *buf;
952
953 /* In the parent. Print error messages from the child until
954 * it closes the pipe.
955 */
956 close(stderr_fd[1]);
957 buf = g_malloc(1024);
958 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
959 errors = true;
960 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100961 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100962 exit(EXIT_FAILURE);
963 }
964 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100965 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100966 error_report("Cannot read from daemon: %s",
967 strerror(errno));
968 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100969 }
970
971 /* Usually the daemon should not print any message.
972 * Exit with zero status in that case.
973 */
974 exit(errors);
975 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100976#else /* WIN32 */
977 error_report("Unable to fork into background on Windows hosts");
978 exit(EXIT_FAILURE);
979#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100980 }
981
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100982 if (device != NULL && sockpath == NULL) {
983 sockpath = g_malloc(128);
984 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000985 }
986
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000987 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000988 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600989 int backlog;
990
Eric Blake3dcf56e2021-02-09 09:27:59 -0600991 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600992 backlog = SOMAXCONN;
993 } else {
994 backlog = MIN(shared, SOMAXCONN);
995 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600996#ifdef CONFIG_SELINUX
997 if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) {
998 error_report("Cannot set SELinux socket create context to %s: %s",
999 selinux_label, strerror(errno));
1000 exit(EXIT_FAILURE);
1001 }
1002#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001003 saddr = nbd_build_socket_address(sockpath, bindto, port);
Eric Blake582d4212021-02-09 09:27:58 -06001004 if (qio_net_listener_open_sync(server, saddr, backlog,
1005 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001006 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001007 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001008 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001009 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001010#ifdef CONFIG_SELINUX
1011 if (selinux_label && setsockcreatecon_raw(NULL) == -1) {
1012 error_report("Cannot clear SELinux socket create context: %s",
1013 strerror(errno));
1014 exit(EXIT_FAILURE);
1015 }
1016#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001017 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001018 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001019 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001020 for (i = 0; i < socket_activation; i++) {
1021 QIOChannelSocket *sioc;
1022 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
1023 &local_err);
1024 if (sioc == NULL) {
1025 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +02001026 error_reportf_err(local_err,
1027 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001028 exit(EXIT_FAILURE);
1029 }
1030 qio_net_listener_add(server, sioc);
1031 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001032 }
1033 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +01001034
Markus Armbrusterf9734d52021-07-20 14:53:53 +02001035 qemu_init_main_loop(&error_fatal);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001036 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001037 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001038
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001039 srcpath = argv[optind];
1040 if (imageOpts) {
1041 QemuOpts *opts;
1042 if (fmt) {
1043 error_report("--image-opts and -f are mutually exclusive");
1044 exit(EXIT_FAILURE);
1045 }
1046 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1047 if (!opts) {
1048 qemu_opts_reset(&file_opts);
1049 exit(EXIT_FAILURE);
1050 }
1051 options = qemu_opts_to_qdict(opts, NULL);
1052 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001053 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001054 } else {
1055 if (fmt) {
1056 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001057 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001058 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001059 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001060 }
1061
Max Reitz4fbec262015-02-05 13:58:19 -05001062 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001063 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1064 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001065 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001066 }
Max Reitz4fbec262015-02-05 13:58:19 -05001067 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001068
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001069 if (dev_offset) {
1070 QDict *raw_opts = qdict_new();
1071 qdict_put_str(raw_opts, "driver", "raw");
1072 qdict_put_str(raw_opts, "file", bs->node_name);
1073 qdict_put_int(raw_opts, "offset", dev_offset);
1074 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1075 blk_remove_bs(blk);
1076 blk_insert_bs(blk, bs, &error_fatal);
1077 bdrv_unref(bs);
1078 }
1079
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001080 blk_set_enable_write_cache(blk, !writethrough);
1081
Wenchao Xia8c116b02013-12-04 17:10:55 +08001082 if (sn_opts) {
1083 ret = bdrv_snapshot_load_tmp(bs,
1084 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1085 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1086 &local_err);
1087 } else if (sn_id_or_name) {
1088 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1089 &local_err);
1090 }
1091 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001092 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001093 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001094 }
1095
Peter Lievenb3838a42014-08-13 19:20:18 +02001096 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001097
Eric Blakea5fced42022-05-11 19:49:23 -05001098 nbd_server_is_qemu_nbd(shared);
Kevin Wolf00917172020-09-24 17:26:57 +02001099
1100 export_opts = g_new(BlockExportOptions, 1);
1101 *export_opts = (BlockExportOptions) {
1102 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001103 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001104 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001105 .has_writethrough = true,
1106 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001107 .has_writable = true,
1108 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001109 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001110 .has_name = true,
1111 .name = g_strdup(export_name),
1112 .has_description = !!export_description,
1113 .description = g_strdup(export_description),
1114 .has_bitmaps = !!bitmaps,
1115 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001116 .has_allocation_depth = alloc_depth,
1117 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001118 },
1119 };
1120 blk_exp_add(export_opts, &error_fatal);
1121 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001122
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001123 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001124#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001125 int ret;
1126
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001127 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001128 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001129 error_report("Failed to create client thread: %s", strerror(ret));
1130 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001131 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001132#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001133 } else {
1134 /* Shut up GCC warnings. */
1135 memset(&client_thread, 0, sizeof(client_thread));
1136 }
1137
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001138 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001139
Max Reitz637bc5a2019-05-08 23:18:16 +02001140 if (pid_file_name) {
1141 qemu_write_pidfile(pid_file_name, &error_fatal);
1142 }
1143
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001144 /* now when the initialization is (almost) complete, chdir("/")
1145 * to free any busy filesystems */
1146 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001147 error_report("Could not chdir to root directory: %s",
1148 strerror(errno));
1149 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001150 }
1151
Max Reitzffb31e12016-09-28 22:46:42 +02001152 if (fork_process) {
1153 dup2(old_stderr, STDERR_FILENO);
1154 close(old_stderr);
1155 }
1156
Paolo Bonzini7860a382012-09-18 13:31:56 +02001157 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001158 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001159 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001160 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001161 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001162 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001163 }
1164 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001165
Markus Armbruster26f54e92014-10-07 13:59:04 +02001166 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001167 if (sockpath) {
1168 unlink(sockpath);
1169 }
bellard7a5ca862008-05-27 21:13:40 +00001170
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001171 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001172
Paolo Bonzinia517e882011-11-04 15:51:21 +01001173 if (device) {
1174 void *ret;
1175 pthread_join(client_thread, &ret);
1176 exit(ret != NULL);
1177 } else {
1178 exit(EXIT_SUCCESS);
1179 }
bellard7a5ca862008-05-27 21:13:40 +00001180}