blob: 5a8ae1f747273e2fbc41d89d2b2877febd5f33b4 [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;
Denis V. Lunev5c56dd22023-07-17 16:55:41 +0200277 bool fork_process;
Denis V. Lunev03b67622023-07-17 16:55:40 +0200278};
279
Paolo Bonzinia517e882011-11-04 15:51:21 +0100280static void *nbd_client_thread(void *arg)
281{
Denis V. Lunev03b67622023-07-17 16:55:40 +0200282 struct NbdClientOpts *opts = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600283 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000284 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000285 int fd = -1;
286 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100287 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500288 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100289
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000290 sioc = qio_channel_socket_new();
291 if (qio_channel_socket_connect_sync(sioc,
292 saddr,
293 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100294 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000295 goto out;
296 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100297
Alex Chenaf74b552020-12-08 13:49:44 +0000298 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
299 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500300 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100301 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500302 }
Alex Chenaf74b552020-12-08 13:49:44 +0000303 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100304 }
305
Denis V. Lunev03b67622023-07-17 16:55:40 +0200306 fd = open(opts->device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100307 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100308 /* Linux-only, we can use %m in printf. */
Denis V. Lunev03b67622023-07-17 16:55:40 +0200309 error_report("Failed to open %s: %m", opts->device);
Alex Chenaf74b552020-12-08 13:49:44 +0000310 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100311 }
312
Alex Chenaf74b552020-12-08 13:49:44 +0000313 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300314 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000315 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100316 }
317
318 /* update partition table */
Denis V. Lunev03b67622023-07-17 16:55:40 +0200319 pthread_create(&show_parts_thread, NULL, show_parts, opts->device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100320
Denis V. Lunev5c56dd22023-07-17 16:55:41 +0200321 if (verbose && !opts->fork_process) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100322 fprintf(stderr, "NBD device %s is now connected to %s\n",
Denis V. Lunev03b67622023-07-17 16:55:40 +0200323 opts->device, srcpath);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100324 } else {
325 /* Close stderr so that the qemu-nbd process exits. */
326 dup2(STDOUT_FILENO, STDERR_FILENO);
327 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100328
Alex Chenaf74b552020-12-08 13:49:44 +0000329 if (nbd_client(fd) < 0) {
330 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100331 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100332
Alex Chenaf74b552020-12-08 13:49:44 +0000333 ret = EXIT_SUCCESS;
334
335 out:
336 if (fd >= 0) {
337 close(fd);
338 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000339 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600340 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100341 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000342 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000343}
Eric Blake3c1fa352018-12-15 07:53:08 -0600344#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000345
Fam Zhenge4afbf42015-05-19 10:50:59 +0000346static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200347{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600348 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200349}
350
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000351static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000352
Eric Blake0c9390d2017-06-08 17:26:17 -0500353static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200354{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200355 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500356 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200357 state = TERMINATE;
358 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000359 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200360 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200361}
362
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000363static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
364 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200365{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200366 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000367 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200368 }
369
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800370 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000371 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000372 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200373}
374
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000375static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000376{
377 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000378 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000379 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000380 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000381 }
382}
383
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100384
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200385static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100386 const char *bindto,
387 const char *port)
388{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200389 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100390
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200391 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100392 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200393 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
394 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100395 } else {
Eric Blake03992932016-03-03 09:16:48 -0700396 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200397 saddr->type = SOCKET_ADDRESS_TYPE_INET;
398 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700399 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100400 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700401 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100402 } else {
Eric Blake03992932016-03-03 09:16:48 -0700403 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100404 }
405 }
406
407 return saddr;
408}
409
410
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000411static QemuOptsList file_opts = {
412 .name = "file",
413 .implied_opt_name = "file",
414 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
415 .desc = {
416 /* no elements => accept any params */
417 { /* end of list */ }
418 },
419};
420
Eric Blake68b96f12019-01-17 13:36:56 -0600421static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
422 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000423{
424 Object *obj;
425 QCryptoTLSCreds *creds;
426
427 obj = object_resolve_path_component(
428 object_get_objects_root(), id);
429 if (!obj) {
430 error_setg(errp, "No TLS credentials with id '%s'",
431 id);
432 return NULL;
433 }
434 creds = (QCryptoTLSCreds *)
435 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
436 if (!creds) {
437 error_setg(errp, "Object with id '%s' is not TLS credentials",
438 id);
439 return NULL;
440 }
441
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +0200442 if (!qcrypto_tls_creds_check_endpoint(creds,
443 list
444 ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
445 : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
446 errp)) {
447 return NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000448 }
449 object_ref(obj);
450 return creds;
451}
452
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000453static void setup_address_and_port(const char **address, const char **port)
454{
455 if (*address == NULL) {
456 *address = "0.0.0.0";
457 }
458
459 if (*port == NULL) {
460 *port = stringify(NBD_DEFAULT_PORT);
461 }
462}
463
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000464/*
465 * Check socket parameters compatibility when socket activation is used.
466 */
467static const char *socket_activation_validate_opts(const char *device,
468 const char *sockpath,
469 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600470 const char *port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600471 const char *selinux,
Eric Blake68b96f12019-01-17 13:36:56 -0600472 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000473{
474 if (device != NULL) {
475 return "NBD device can't be set when using socket activation";
476 }
477
478 if (sockpath != NULL) {
479 return "Unix socket can't be set when using socket activation";
480 }
481
482 if (address != NULL) {
483 return "The interface can't be set when using socket activation";
484 }
485
486 if (port != NULL) {
487 return "TCP port number can't be set when using socket activation";
488 }
489
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600490 if (selinux != NULL) {
491 return "SELinux label can't be set when using socket activation";
492 }
493
Eric Blake68b96f12019-01-17 13:36:56 -0600494 if (list) {
495 return "List mode is incompatible with socket activation";
496 }
497
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000498 return NULL;
499}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000500
Kevin Wolfb3b52992018-05-16 13:46:37 +0200501static void qemu_nbd_shutdown(void)
502{
503 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100504 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200505 bdrv_close_all();
506}
507
bellard7a5ca862008-05-27 21:13:40 +0000508int main(int argc, char **argv)
509{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200510 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000511 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600512 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500513 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000514 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000515 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100516 const char *port = NULL;
517 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100518 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800519 QemuOpts *sn_opts = NULL;
520 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500521 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000522 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000523 { "help", no_argument, NULL, 'h' },
524 { "version", no_argument, NULL, 'V' },
525 { "bind", required_argument, NULL, 'b' },
526 { "port", required_argument, NULL, 'p' },
527 { "socket", required_argument, NULL, 'k' },
528 { "offset", required_argument, NULL, 'o' },
529 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500530 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600531 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000532 { "connect", required_argument, NULL, 'c' },
533 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600534 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000535 { "snapshot", no_argument, NULL, 's' },
536 { "load-snapshot", required_argument, NULL, 'l' },
537 { "nocache", no_argument, NULL, 'n' },
538 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
539 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
540 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
541 { "detect-zeroes", required_argument, NULL,
542 QEMU_NBD_OPT_DETECT_ZEROES },
543 { "shared", required_argument, NULL, 'e' },
544 { "format", required_argument, NULL, 'f' },
545 { "persistent", no_argument, NULL, 't' },
546 { "verbose", no_argument, NULL, 'v' },
547 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
548 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500549 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000550 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000551 { "tls-hostname", required_argument, NULL, QEMU_NBD_OPT_TLSHOSTNAME },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000552 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000553 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300554 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200555 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200556 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600557 { "selinux-label", required_argument, NULL,
558 QEMU_NBD_OPT_SELINUX_LABEL },
Blue Swirl660f11b2009-07-31 21:16:51 +0000559 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000560 };
561 int ch;
562 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200563 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500564 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200565 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100566 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200567 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100568 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000569 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200570 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200571 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500572 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600573 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500574 const char *export_description = NULL;
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300575 BlockDirtyBitmapOrStrList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500576 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000577 const char *tlscredsid = NULL;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000578 const char *tlshostname = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000579 bool imageOpts = false;
Nir Soffer09615252021-08-13 23:55:19 +0300580 bool writethrough = false; /* Client will flush as needed. */
Max Reitzffb31e12016-09-28 22:46:42 +0200581 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600582 bool list = false;
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) {
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200935 int saved_errno;
936
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100937 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200938
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400939 ret = qemu_daemon(1, 0);
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200940 saved_errno = errno; /* dup2 will overwrite error below */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100941
942 /* Temporarily redirect stderr to the parent's pipe... */
943 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100944 if (ret < 0) {
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200945 error_report("Failed to daemonize: %s", strerror(saved_errno));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100946 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100947 }
948
949 /* ... close the descriptor we inherited and go on. */
950 close(stderr_fd[1]);
951 } else {
952 bool errors = false;
953 char *buf;
954
955 /* In the parent. Print error messages from the child until
956 * it closes the pipe.
957 */
958 close(stderr_fd[1]);
959 buf = g_malloc(1024);
960 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
961 errors = true;
962 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100963 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100964 exit(EXIT_FAILURE);
965 }
966 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100967 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100968 error_report("Cannot read from daemon: %s",
969 strerror(errno));
970 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100971 }
972
973 /* Usually the daemon should not print any message.
974 * Exit with zero status in that case.
975 */
976 exit(errors);
977 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100978#else /* WIN32 */
979 error_report("Unable to fork into background on Windows hosts");
980 exit(EXIT_FAILURE);
981#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100982 }
983
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100984 if (device != NULL && sockpath == NULL) {
985 sockpath = g_malloc(128);
986 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000987 }
988
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000989 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000990 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600991 int backlog;
992
Eric Blake3dcf56e2021-02-09 09:27:59 -0600993 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600994 backlog = SOMAXCONN;
995 } else {
996 backlog = MIN(shared, SOMAXCONN);
997 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600998#ifdef CONFIG_SELINUX
999 if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) {
1000 error_report("Cannot set SELinux socket create context to %s: %s",
1001 selinux_label, strerror(errno));
1002 exit(EXIT_FAILURE);
1003 }
1004#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001005 saddr = nbd_build_socket_address(sockpath, bindto, port);
Eric Blake582d4212021-02-09 09:27:58 -06001006 if (qio_net_listener_open_sync(server, saddr, backlog,
1007 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001008 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001009 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001010 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001011 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001012#ifdef CONFIG_SELINUX
1013 if (selinux_label && setsockcreatecon_raw(NULL) == -1) {
1014 error_report("Cannot clear SELinux socket create context: %s",
1015 strerror(errno));
1016 exit(EXIT_FAILURE);
1017 }
1018#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001019 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001020 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001021 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001022 for (i = 0; i < socket_activation; i++) {
1023 QIOChannelSocket *sioc;
1024 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
1025 &local_err);
1026 if (sioc == NULL) {
1027 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +02001028 error_reportf_err(local_err,
1029 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001030 exit(EXIT_FAILURE);
1031 }
1032 qio_net_listener_add(server, sioc);
1033 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001034 }
1035 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +01001036
Markus Armbrusterf9734d52021-07-20 14:53:53 +02001037 qemu_init_main_loop(&error_fatal);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001038 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001039 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001040
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001041 srcpath = argv[optind];
1042 if (imageOpts) {
1043 QemuOpts *opts;
1044 if (fmt) {
1045 error_report("--image-opts and -f are mutually exclusive");
1046 exit(EXIT_FAILURE);
1047 }
1048 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1049 if (!opts) {
1050 qemu_opts_reset(&file_opts);
1051 exit(EXIT_FAILURE);
1052 }
1053 options = qemu_opts_to_qdict(opts, NULL);
1054 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001055 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001056 } else {
1057 if (fmt) {
1058 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001059 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001060 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001061 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001062 }
1063
Max Reitz4fbec262015-02-05 13:58:19 -05001064 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001065 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1066 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001067 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001068 }
Max Reitz4fbec262015-02-05 13:58:19 -05001069 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001070
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001071 if (dev_offset) {
1072 QDict *raw_opts = qdict_new();
1073 qdict_put_str(raw_opts, "driver", "raw");
1074 qdict_put_str(raw_opts, "file", bs->node_name);
1075 qdict_put_int(raw_opts, "offset", dev_offset);
Kevin Wolfc6e0a6d2023-05-25 14:47:04 +02001076
1077 aio_context_acquire(qemu_get_aio_context());
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001078 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
Kevin Wolfc6e0a6d2023-05-25 14:47:04 +02001079 aio_context_release(qemu_get_aio_context());
1080
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001081 blk_remove_bs(blk);
1082 blk_insert_bs(blk, bs, &error_fatal);
1083 bdrv_unref(bs);
1084 }
1085
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001086 blk_set_enable_write_cache(blk, !writethrough);
1087
Wenchao Xia8c116b02013-12-04 17:10:55 +08001088 if (sn_opts) {
1089 ret = bdrv_snapshot_load_tmp(bs,
1090 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1091 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1092 &local_err);
1093 } else if (sn_id_or_name) {
1094 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1095 &local_err);
1096 }
1097 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001098 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001099 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001100 }
1101
Peter Lievenb3838a42014-08-13 19:20:18 +02001102 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001103
Eric Blakea5fced42022-05-11 19:49:23 -05001104 nbd_server_is_qemu_nbd(shared);
Kevin Wolf00917172020-09-24 17:26:57 +02001105
1106 export_opts = g_new(BlockExportOptions, 1);
1107 *export_opts = (BlockExportOptions) {
1108 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001109 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001110 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001111 .has_writethrough = true,
1112 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001113 .has_writable = true,
1114 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001115 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001116 .name = g_strdup(export_name),
Eric Blakecbad81c2020-10-27 00:05:49 -05001117 .description = g_strdup(export_description),
1118 .has_bitmaps = !!bitmaps,
1119 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001120 .has_allocation_depth = alloc_depth,
1121 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001122 },
1123 };
1124 blk_exp_add(export_opts, &error_fatal);
1125 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001126
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001127 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001128#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001129 int ret;
Denis V. Lunev03b67622023-07-17 16:55:40 +02001130 struct NbdClientOpts opts = {
1131 .device = device,
Denis V. Lunev5c56dd22023-07-17 16:55:41 +02001132 .fork_process = fork_process,
Denis V. Lunev03b67622023-07-17 16:55:40 +02001133 };
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001134
Denis V. Lunev03b67622023-07-17 16:55:40 +02001135 ret = pthread_create(&client_thread, NULL, nbd_client_thread, &opts);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001136 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001137 error_report("Failed to create client thread: %s", strerror(ret));
1138 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001139 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001140#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001141 } else {
1142 /* Shut up GCC warnings. */
1143 memset(&client_thread, 0, sizeof(client_thread));
1144 }
1145
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001146 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001147
Max Reitz637bc5a2019-05-08 23:18:16 +02001148 if (pid_file_name) {
1149 qemu_write_pidfile(pid_file_name, &error_fatal);
1150 }
1151
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001152 /* now when the initialization is (almost) complete, chdir("/")
1153 * to free any busy filesystems */
1154 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001155 error_report("Could not chdir to root directory: %s",
1156 strerror(errno));
1157 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001158 }
1159
Max Reitzffb31e12016-09-28 22:46:42 +02001160 if (fork_process) {
Denis V. Lunev5c56dd22023-07-17 16:55:41 +02001161 dup2(STDOUT_FILENO, STDERR_FILENO);
Max Reitzffb31e12016-09-28 22:46:42 +02001162 }
1163
Paolo Bonzini7860a382012-09-18 13:31:56 +02001164 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001165 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001166 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001167 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001168 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001169 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001170 }
1171 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001172
Markus Armbruster26f54e92014-10-07 13:59:04 +02001173 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001174 if (sockpath) {
1175 unlink(sockpath);
1176 }
bellard7a5ca862008-05-27 21:13:40 +00001177
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001178 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001179
Paolo Bonzinia517e882011-11-04 15:51:21 +01001180 if (device) {
1181 void *ret;
1182 pthread_join(client_thread, &ret);
1183 exit(ret != NULL);
1184 } else {
1185 exit(EXIT_SUCCESS);
1186 }
bellard7a5ca862008-05-27 21:13:40 +00001187}