blob: 5b2757920c1c4c402970d2421b1f3f47f3edbb3e [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
Paolo Bonzinia517e882011-11-04 15:51:21 +010076static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020077static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020078static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020079static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020080static int shared = 1;
81static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000082static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000083static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000084static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000085
86static void usage(const char *name)
87{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020088 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000089"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060090" or: %s -L [OPTIONS]\n"
91"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000092"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020093" -h, --help display this help and exit\n"
94" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000095"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020096"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020097" -p, --port=PORT port to listen on (default `%d')\n"
98" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
99" -k, --socket=PATH path to the unix socket\n"
100" (default '"SOCKET_PATH"')\n"
101" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
102" -t, --persistent don't exit on the last connection\n"
103" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +0300104" -x, --export-name=NAME expose export by name (default is empty string)\n"
105" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200106"\n"
107"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200108" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500109" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600110" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200111"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000112"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600113" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000114" --object type,id=ID,... define an object such as 'secret' for providing\n"
115" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500116" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000117" --tls-authz=ID use id of an earlier --object to provide\n"
118" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300119" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
120" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200121" --fork fork off the server process and exit the parent\n"
122" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200123" --pid-file=PATH store the server's process ID in the given file\n"
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600124#ifdef CONFIG_SELINUX
125" --selinux-label=LABEL set SELinux process label on listening socket\n"
126#endif
Eric Blake3c1fa352018-12-15 07:53:08 -0600127#if HAVE_NBD_DEVICE
128"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200129"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200130" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
131" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200132#endif
133"\n"
134"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200135" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
136" -r, --read-only export read-only\n"
137" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
138" file with backing_file=FILE, redirect the write to\n"
139" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800140" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200141" load an internal snapshot inside FILE and export it\n"
142" as an read-only device, SNAPSHOT_PARAM format is\n"
143" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
144" '[ID_OR_NAME]'\n"
145" -n, --nocache disable host cache\n"
Nir Soffer09615252021-08-13 23:55:19 +0300146" --cache=MODE set cache mode used to access the disk image, the\n"
147" valid options are: 'none', 'writeback' (default),\n"
148" 'writethrough', 'directsync' and 'unsafe'\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000149" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200150" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300151" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000152" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200153"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500154QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600155 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000156}
157
158static void version(const char *name)
159{
160 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100161"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000162"Written by Anthony Liguori.\n"
163"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500164QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000165"This is free software; see the source for copying conditions. There is NO\n"
166"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000167 , name);
bellard7a5ca862008-05-27 21:13:40 +0000168}
169
Eric Blake029a88c2020-09-30 07:11:01 -0500170#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100171/*
172 * The client thread uses SIGTERM to interrupt the server. A signal
173 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
174 */
175void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100176{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100177 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200178 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100179}
Eric Blake029a88c2020-09-30 07:11:01 -0500180#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100181
Eric Blake68b96f12019-01-17 13:36:56 -0600182static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
183 const char *hostname)
184{
185 int ret = EXIT_FAILURE;
186 int rc;
187 Error *err = NULL;
188 QIOChannelSocket *sioc;
189 NBDExportInfo *list;
190 int i, j;
191
192 sioc = qio_channel_socket_new();
193 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
194 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000195 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600196 }
197 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
198 &err);
199 if (rc < 0) {
200 if (err) {
201 error_report_err(err);
202 }
203 goto out;
204 }
205 printf("exports available: %d\n", rc);
206 for (i = 0; i < rc; i++) {
207 printf(" export: '%s'\n", list[i].name);
208 if (list[i].description && *list[i].description) {
209 printf(" description: %s\n", list[i].description);
210 }
211 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200212 static const char *const flag_names[] = {
213 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
214 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
215 [NBD_FLAG_SEND_FUA_BIT] = "fua",
216 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
217 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
218 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
219 [NBD_FLAG_SEND_DF_BIT] = "df",
220 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
221 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
222 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500223 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200224 };
225
Eric Blake68b96f12019-01-17 13:36:56 -0600226 printf(" size: %" PRIu64 "\n", list[i].size);
227 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200228 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
229 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
230 printf(" %s", flag_names[bit]);
231 }
Eric Blake68b96f12019-01-17 13:36:56 -0600232 }
233 printf(" )\n");
234 }
235 if (list[i].min_block) {
236 printf(" min block: %u\n", list[i].min_block);
237 printf(" opt block: %u\n", list[i].opt_block);
238 printf(" max block: %u\n", list[i].max_block);
239 }
240 if (list[i].n_contexts) {
241 printf(" available meta contexts: %d\n", list[i].n_contexts);
242 for (j = 0; j < list[i].n_contexts; j++) {
243 printf(" %s\n", list[i].contexts[j]);
244 }
245 }
246 }
247 nbd_free_export_list(list, rc);
248
249 ret = EXIT_SUCCESS;
250 out:
251 object_unref(OBJECT(sioc));
252 return ret;
253}
254
255
Eric Blake3c1fa352018-12-15 07:53:08 -0600256#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100257static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000258{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100259 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100260 int nbd;
thscd831bd2008-07-03 10:23:51 +0000261
Paolo Bonzinia517e882011-11-04 15:51:21 +0100262 /* linux just needs an open() to trigger
263 * the partition table update
264 * but remember to load the module with max_part != 0 :
265 * modprobe nbd max_part=63
266 */
267 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100268 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100269 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000270 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100271 return NULL;
272}
273
Denis V. Lunev03b67622023-07-17 16:55:40 +0200274struct NbdClientOpts {
275 char *device;
Denis V. Lunev5c56dd22023-07-17 16:55:41 +0200276 bool fork_process;
Denis V. Lunev414c0cf2023-07-17 22:25:20 +0200277 bool verbose;
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. Lunev414c0cf2023-07-17 22:25:20 +0200321 if (opts->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. */
Denis V. Lunev173776f2023-07-17 16:55:44 +0200326 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
327 error_report("Could not set stderr to /dev/null: %s",
328 strerror(errno));
329 exit(EXIT_FAILURE);
330 }
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100331 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100332
Alex Chenaf74b552020-12-08 13:49:44 +0000333 if (nbd_client(fd) < 0) {
334 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100335 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100336
Alex Chenaf74b552020-12-08 13:49:44 +0000337 ret = EXIT_SUCCESS;
338
339 out:
340 if (fd >= 0) {
341 close(fd);
342 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000343 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600344 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100345 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000346 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000347}
Eric Blake3c1fa352018-12-15 07:53:08 -0600348#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000349
Fam Zhenge4afbf42015-05-19 10:50:59 +0000350static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200351{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600352 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200353}
354
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000355static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000356
Eric Blake0c9390d2017-06-08 17:26:17 -0500357static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200358{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200359 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500360 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200361 state = TERMINATE;
362 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000363 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200364 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200365}
366
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000367static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
368 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200369{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200370 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000371 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200372 }
373
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800374 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000375 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000376 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200377}
378
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000379static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000380{
381 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000382 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000383 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000384 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000385 }
386}
387
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100388
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200389static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100390 const char *bindto,
391 const char *port)
392{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200393 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100394
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200395 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100396 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200397 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
398 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100399 } else {
Eric Blake03992932016-03-03 09:16:48 -0700400 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200401 saddr->type = SOCKET_ADDRESS_TYPE_INET;
402 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700403 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100404 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700405 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100406 } else {
Eric Blake03992932016-03-03 09:16:48 -0700407 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100408 }
409 }
410
411 return saddr;
412}
413
414
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000415static QemuOptsList file_opts = {
416 .name = "file",
417 .implied_opt_name = "file",
418 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
419 .desc = {
420 /* no elements => accept any params */
421 { /* end of list */ }
422 },
423};
424
Eric Blake68b96f12019-01-17 13:36:56 -0600425static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
426 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000427{
428 Object *obj;
429 QCryptoTLSCreds *creds;
430
431 obj = object_resolve_path_component(
432 object_get_objects_root(), id);
433 if (!obj) {
434 error_setg(errp, "No TLS credentials with id '%s'",
435 id);
436 return NULL;
437 }
438 creds = (QCryptoTLSCreds *)
439 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
440 if (!creds) {
441 error_setg(errp, "Object with id '%s' is not TLS credentials",
442 id);
443 return NULL;
444 }
445
Philippe Mathieu-Daudé0279cd92021-06-28 18:09:10 +0200446 if (!qcrypto_tls_creds_check_endpoint(creds,
447 list
448 ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
449 : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
450 errp)) {
451 return NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000452 }
453 object_ref(obj);
454 return creds;
455}
456
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000457static void setup_address_and_port(const char **address, const char **port)
458{
459 if (*address == NULL) {
460 *address = "0.0.0.0";
461 }
462
463 if (*port == NULL) {
464 *port = stringify(NBD_DEFAULT_PORT);
465 }
466}
467
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000468/*
469 * Check socket parameters compatibility when socket activation is used.
470 */
471static const char *socket_activation_validate_opts(const char *device,
472 const char *sockpath,
473 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600474 const char *port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600475 const char *selinux,
Eric Blake68b96f12019-01-17 13:36:56 -0600476 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000477{
478 if (device != NULL) {
479 return "NBD device can't be set when using socket activation";
480 }
481
482 if (sockpath != NULL) {
483 return "Unix socket can't be set when using socket activation";
484 }
485
486 if (address != NULL) {
487 return "The interface can't be set when using socket activation";
488 }
489
490 if (port != NULL) {
491 return "TCP port number can't be set when using socket activation";
492 }
493
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600494 if (selinux != NULL) {
495 return "SELinux label can't be set when using socket activation";
496 }
497
Eric Blake68b96f12019-01-17 13:36:56 -0600498 if (list) {
499 return "List mode is incompatible with socket activation";
500 }
501
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000502 return NULL;
503}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000504
Kevin Wolfb3b52992018-05-16 13:46:37 +0200505static void qemu_nbd_shutdown(void)
506{
507 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100508 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200509 bdrv_close_all();
510}
511
bellard7a5ca862008-05-27 21:13:40 +0000512int main(int argc, char **argv)
513{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200514 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000515 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600516 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500517 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000518 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000519 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100520 const char *port = NULL;
521 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100522 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800523 QemuOpts *sn_opts = NULL;
524 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500525 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000526 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000527 { "help", no_argument, NULL, 'h' },
528 { "version", no_argument, NULL, 'V' },
529 { "bind", required_argument, NULL, 'b' },
530 { "port", required_argument, NULL, 'p' },
531 { "socket", required_argument, NULL, 'k' },
532 { "offset", required_argument, NULL, 'o' },
533 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500534 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600535 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000536 { "connect", required_argument, NULL, 'c' },
537 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600538 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000539 { "snapshot", no_argument, NULL, 's' },
540 { "load-snapshot", required_argument, NULL, 'l' },
541 { "nocache", no_argument, NULL, 'n' },
542 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
543 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
544 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
545 { "detect-zeroes", required_argument, NULL,
546 QEMU_NBD_OPT_DETECT_ZEROES },
547 { "shared", required_argument, NULL, 'e' },
548 { "format", required_argument, NULL, 'f' },
549 { "persistent", no_argument, NULL, 't' },
550 { "verbose", no_argument, NULL, 'v' },
551 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
552 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500553 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000554 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000555 { "tls-hostname", required_argument, NULL, QEMU_NBD_OPT_TLSHOSTNAME },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000556 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000557 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300558 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200559 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200560 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600561 { "selinux-label", required_argument, NULL,
562 QEMU_NBD_OPT_SELINUX_LABEL },
Blue Swirl660f11b2009-07-31 21:16:51 +0000563 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000564 };
565 int ch;
566 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200567 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500568 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200569 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100570 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200571 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100572 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000573 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200574 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200575 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500576 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600577 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500578 const char *export_description = NULL;
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300579 BlockDirtyBitmapOrStrList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500580 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000581 const char *tlscredsid = NULL;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000582 const char *tlshostname = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000583 bool imageOpts = false;
Nir Soffer09615252021-08-13 23:55:19 +0300584 bool writethrough = false; /* Client will flush as needed. */
Denis V. Lunev414c0cf2023-07-17 22:25:20 +0200585 bool verbose = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200586 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600587 bool list = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000588 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200589 const char *pid_file_name = NULL;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600590 const char *selinux_label = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200591 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000592
Eric Blake029a88c2020-09-30 07:11:01 -0500593#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100594 os_setup_early_signal_handling();
595 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200596#endif
597
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100598 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100599 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100600 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300601 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100602
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000603 module_call_init(MODULE_INIT_QOM);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300604 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800605 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100606
bellard7a5ca862008-05-27 21:13:40 +0000607 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
608 switch (ch) {
609 case 's':
ths2f726482008-07-03 11:47:46 +0000610 flags |= BDRV_O_SNAPSHOT;
611 break;
612 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200613 optarg = (char *) "none";
614 /* fallthrough */
615 case QEMU_NBD_OPT_CACHE:
616 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100617 error_report("-n and --cache can only be specified once");
618 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200619 }
620 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100621 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100622 error_report("Invalid cache mode `%s'", optarg);
623 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200624 }
bellard7a5ca862008-05-27 21:13:40 +0000625 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200626 case QEMU_NBD_OPT_AIO:
627 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100628 error_report("--aio can only be specified once");
629 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200630 }
631 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000632 if (bdrv_parse_aio(optarg, &flags) < 0) {
633 error_report("Invalid aio mode '%s'", optarg);
634 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200635 }
636 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100637 case QEMU_NBD_OPT_DISCARD:
638 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100639 error_report("--discard can only be specified once");
640 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100641 }
642 seen_discard = true;
643 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100644 error_report("Invalid discard mode `%s'", optarg);
645 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100646 }
647 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200648 case QEMU_NBD_OPT_DETECT_ZEROES:
649 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200650 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200651 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200652 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
653 &local_err);
654 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100655 error_reportf_err(local_err,
656 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100657 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200658 }
659 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
660 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100661 error_report("setting detect-zeroes to unmap is not allowed "
662 "without setting discard operation to unmap");
663 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200664 }
665 break;
bellard7a5ca862008-05-27 21:13:40 +0000666 case 'b':
667 bindto = optarg;
668 break;
669 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100670 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000671 break;
672 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600673 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
674 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100675 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000676 }
bellard7a5ca862008-05-27 21:13:40 +0000677 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800678 case 'l':
679 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100680 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
681 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800682 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100683 error_report("Failed in parsing snapshot param `%s'",
684 optarg);
685 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800686 }
687 } else {
688 sn_id_or_name = optarg;
689 }
690 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000691 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500692 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200693 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000694 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500695 case 'A':
696 alloc_depth = true;
697 break;
Eric Blake636192c2019-01-11 13:47:20 -0600698 case 'B':
Vladimir Sementsov-Ogievskiye5fb29d2022-03-15 00:32:25 +0300699 {
700 BlockDirtyBitmapOrStr *el = g_new(BlockDirtyBitmapOrStr, 1);
701 *el = (BlockDirtyBitmapOrStr) {
702 .type = QTYPE_QSTRING,
703 .u.local = g_strdup(optarg),
704 };
705 QAPI_LIST_PREPEND(bitmaps, el);
706 }
Eric Blake636192c2019-01-11 13:47:20 -0600707 break;
thscd831bd2008-07-03 10:23:51 +0000708 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100709 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200710 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100711 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100712 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200713 }
thscd831bd2008-07-03 10:23:51 +0000714 break;
715 case 'd':
716 disconnect = true;
717 break;
718 case 'c':
719 device = optarg;
720 break;
ths3b05a8e2008-07-03 12:45:02 +0000721 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600722 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
Eric Blake3dcf56e2021-02-09 09:27:59 -0600723 shared < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100724 error_report("Invalid shared device number '%s'", optarg);
725 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000726 }
ths3b05a8e2008-07-03 12:45:02 +0000727 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000728 case 'f':
729 fmt = optarg;
730 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200731 case 't':
732 persistent = 1;
733 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000734 case 'x':
735 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600736 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
737 error_report("export name '%s' too long", export_name);
738 exit(EXIT_FAILURE);
739 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000740 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500741 case 'D':
742 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600743 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
744 error_report("export description '%s' too long",
745 export_description);
746 exit(EXIT_FAILURE);
747 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500748 break;
bellard7a5ca862008-05-27 21:13:40 +0000749 case 'v':
Denis V. Lunev414c0cf2023-07-17 22:25:20 +0200750 verbose = true;
bellard7a5ca862008-05-27 21:13:40 +0000751 break;
752 case 'V':
753 version(argv[0]);
754 exit(0);
755 break;
756 case 'h':
757 usage(argv[0]);
758 exit(0);
759 break;
760 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100761 error_report("Try `%s --help' for more information.", argv[0]);
762 exit(EXIT_FAILURE);
Kevin Wolffa40e432021-02-17 12:56:45 +0100763 case QEMU_NBD_OPT_OBJECT:
764 user_creatable_process_cmdline(optarg);
765 break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000766 case QEMU_NBD_OPT_TLSCREDS:
767 tlscredsid = optarg;
768 break;
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000769 case QEMU_NBD_OPT_TLSHOSTNAME:
770 tlshostname = optarg;
771 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000772 case QEMU_NBD_OPT_IMAGE_OPTS:
773 imageOpts = true;
774 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300775 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500776 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300777 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000778 case QEMU_NBD_OPT_TLSAUTHZ:
779 tlsauthz = optarg;
780 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200781 case QEMU_NBD_OPT_FORK:
782 fork_process = true;
783 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600784 case 'L':
785 list = true;
786 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200787 case QEMU_NBD_OPT_PID_FILE:
788 pid_file_name = optarg;
789 break;
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600790 case QEMU_NBD_OPT_SELINUX_LABEL:
791 selinux_label = optarg;
792 break;
bellard7a5ca862008-05-27 21:13:40 +0000793 }
794 }
795
Eric Blake68b96f12019-01-17 13:36:56 -0600796 if (list) {
797 if (argc != optind) {
798 error_report("List mode is incompatible with a file name");
799 exit(EXIT_FAILURE);
800 }
Eric Blake0bc16992020-01-23 10:46:50 -0600801 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500802 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500803 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600804 error_report("List mode is incompatible with per-device settings");
805 exit(EXIT_FAILURE);
806 }
807 if (fork_process) {
808 error_report("List mode is incompatible with forking");
809 exit(EXIT_FAILURE);
810 }
811 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100812 error_report("Invalid number of arguments");
813 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100814 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600815 } else if (!export_name) {
816 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000817 }
818
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300819 if (!trace_init_backends()) {
820 exit(1);
821 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500822 trace_init_file();
Richard Hendersonc5955f42022-04-17 11:29:44 -0700823 qemu_set_log(LOG_TRACE, &error_fatal);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300824
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000825 socket_activation = check_socket_activation();
826 if (socket_activation == 0) {
Daniel P. Berrangée8ae8b12022-03-04 19:36:03 +0000827 if (!sockpath) {
828 setup_address_and_port(&bindto, &port);
829 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000830 } else {
831 /* Using socket activation - check user didn't use -p etc. */
832 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600833 bindto, port,
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600834 selinux_label,
Eric Blake68b96f12019-01-17 13:36:56 -0600835 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000836 if (err_msg != NULL) {
837 error_report("%s", err_msg);
838 exit(EXIT_FAILURE);
839 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100840
841 /* qemu-nbd can only listen on a single socket. */
842 if (socket_activation > 1) {
843 error_report("qemu-nbd does not support socket activation with %s > 1",
844 "LISTEN_FDS");
845 exit(EXIT_FAILURE);
846 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000847 }
848
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000849 if (tlscredsid) {
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000850 if (device) {
851 error_report("TLS is not supported with a host device");
852 exit(EXIT_FAILURE);
853 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000854 if (tlsauthz && list) {
855 error_report("TLS authorization is incompatible with export list");
856 exit(EXIT_FAILURE);
857 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000858 if (tlshostname && !list) {
859 error_report("TLS hostname is only supported with export list");
860 exit(EXIT_FAILURE);
861 }
Eric Blake68b96f12019-01-17 13:36:56 -0600862 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000863 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200864 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000865 exit(EXIT_FAILURE);
866 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000867 } else {
868 if (tlsauthz) {
869 error_report("--tls-authz is not permitted without --tls-creds");
870 exit(EXIT_FAILURE);
871 }
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000872 if (tlshostname) {
873 error_report("--tls-hostname is not permitted without --tls-creds");
874 exit(EXIT_FAILURE);
875 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000876 }
877
Richard W.M. Jones3d212b42021-11-15 14:29:43 -0600878 if (selinux_label) {
879#ifdef CONFIG_SELINUX
880 if (sockpath == NULL && device == NULL) {
881 error_report("--selinux-label is not permitted without --socket");
882 exit(EXIT_FAILURE);
883 }
884#else
885 error_report("SELinux support not enabled in this binary");
886 exit(EXIT_FAILURE);
887#endif
888 }
889
Eric Blake68b96f12019-01-17 13:36:56 -0600890 if (list) {
891 saddr = nbd_build_socket_address(sockpath, bindto, port);
Daniel P. Berrangé003b2b22022-03-04 19:36:02 +0000892 return qemu_nbd_client_list(saddr, tlscreds,
893 tlshostname ? tlshostname : bindto);
Eric Blake68b96f12019-01-17 13:36:56 -0600894 }
895
Eric Blake3c1fa352018-12-15 07:53:08 -0600896#if !HAVE_NBD_DEVICE
897 if (disconnect || device) {
898 error_report("Kernel /dev/nbdN support not available");
899 exit(EXIT_FAILURE);
900 }
901#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000902 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000903 int nbdfd = open(argv[optind], O_RDWR);
904 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100905 error_report("Cannot open %s: %s", argv[optind],
906 strerror(errno));
907 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100908 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000909 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000910
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000911 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000912
913 printf("%s disconnected\n", argv[optind]);
914
Peter Lieven713cc672014-08-13 19:20:19 +0200915 return 0;
thscd831bd2008-07-03 10:23:51 +0000916 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600917#endif
thscd831bd2008-07-03 10:23:51 +0000918
Max Reitzffb31e12016-09-28 22:46:42 +0200919 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100920#ifndef WIN32
Marc-André Lureaua7241972022-03-29 15:21:00 +0400921 g_autoptr(GError) err = NULL;
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100922 int stderr_fd[2];
923 pid_t pid;
924 int ret;
925
Marc-André Lureaua7241972022-03-29 15:21:00 +0400926 if (!g_unix_open_pipe(stderr_fd, FD_CLOEXEC, &err)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100927 error_report("Error setting up communication pipe: %s",
Marc-André Lureaua7241972022-03-29 15:21:00 +0400928 err->message);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100929 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100930 }
931
932 /* Now daemonize, but keep a communication channel open to
933 * print errors and exit with the proper status code.
934 */
935 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500936 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100937 error_report("Failed to fork: %s", strerror(errno));
938 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500939 } else if (pid == 0) {
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200940 int saved_errno;
941
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100942 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200943
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400944 ret = qemu_daemon(1, 0);
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200945 saved_errno = errno; /* dup2 will overwrite error below */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100946
947 /* Temporarily redirect stderr to the parent's pipe... */
Denis V. Luneve0892ce2023-07-17 16:55:42 +0200948 if (dup2(stderr_fd[1], STDERR_FILENO) < 0) {
949 char str[256];
950 snprintf(str, sizeof(str),
951 "%s: Failed to link stderr to the pipe: %s\n",
952 g_get_prgname(), strerror(errno));
953 /*
954 * We are unable to use error_report() here as we need to get
955 * stderr pointed to the parent's pipe. Write to that pipe
956 * manually.
957 */
958 ret = write(stderr_fd[1], str, strlen(str));
959 exit(EXIT_FAILURE);
960 }
961
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100962 if (ret < 0) {
Denis V. Lunev1dc82152023-07-17 16:55:43 +0200963 error_report("Failed to daemonize: %s", strerror(saved_errno));
Markus Armbruster85b01e02015-12-18 16:35:04 +0100964 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100965 }
966
967 /* ... close the descriptor we inherited and go on. */
968 close(stderr_fd[1]);
969 } else {
970 bool errors = false;
971 char *buf;
972
973 /* In the parent. Print error messages from the child until
974 * it closes the pipe.
975 */
976 close(stderr_fd[1]);
977 buf = g_malloc(1024);
978 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
979 errors = true;
980 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100981 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100982 exit(EXIT_FAILURE);
983 }
984 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100985 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100986 error_report("Cannot read from daemon: %s",
987 strerror(errno));
988 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100989 }
990
991 /* Usually the daemon should not print any message.
992 * Exit with zero status in that case.
993 */
994 exit(errors);
995 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100996#else /* WIN32 */
997 error_report("Unable to fork into background on Windows hosts");
998 exit(EXIT_FAILURE);
999#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +01001000 }
1001
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001002 if (device != NULL && sockpath == NULL) {
1003 sockpath = g_malloc(128);
1004 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +00001005 }
1006
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001007 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001008 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -06001009 int backlog;
1010
Eric Blake3dcf56e2021-02-09 09:27:59 -06001011 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -06001012 backlog = SOMAXCONN;
1013 } else {
1014 backlog = MIN(shared, SOMAXCONN);
1015 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001016#ifdef CONFIG_SELINUX
1017 if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) {
1018 error_report("Cannot set SELinux socket create context to %s: %s",
1019 selinux_label, strerror(errno));
1020 exit(EXIT_FAILURE);
1021 }
1022#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001023 saddr = nbd_build_socket_address(sockpath, bindto, port);
Eric Blake582d4212021-02-09 09:27:58 -06001024 if (qio_net_listener_open_sync(server, saddr, backlog,
1025 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001026 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001027 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001028 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001029 }
Richard W.M. Jones3d212b42021-11-15 14:29:43 -06001030#ifdef CONFIG_SELINUX
1031 if (selinux_label && setsockcreatecon_raw(NULL) == -1) {
1032 error_report("Cannot clear SELinux socket create context: %s",
1033 strerror(errno));
1034 exit(EXIT_FAILURE);
1035 }
1036#endif
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001037 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001038 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001039 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001040 for (i = 0; i < socket_activation; i++) {
1041 QIOChannelSocket *sioc;
1042 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
1043 &local_err);
1044 if (sioc == NULL) {
1045 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +02001046 error_reportf_err(local_err,
1047 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +00001048 exit(EXIT_FAILURE);
1049 }
1050 qio_net_listener_add(server, sioc);
1051 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +00001052 }
1053 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +01001054
Markus Armbrusterf9734d52021-07-20 14:53:53 +02001055 qemu_init_main_loop(&error_fatal);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001056 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +02001057 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001058
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001059 srcpath = argv[optind];
1060 if (imageOpts) {
1061 QemuOpts *opts;
1062 if (fmt) {
1063 error_report("--image-opts and -f are mutually exclusive");
1064 exit(EXIT_FAILURE);
1065 }
1066 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1067 if (!opts) {
1068 qemu_opts_reset(&file_opts);
1069 exit(EXIT_FAILURE);
1070 }
1071 options = qemu_opts_to_qdict(opts, NULL);
1072 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001073 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001074 } else {
1075 if (fmt) {
1076 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001077 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001078 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001079 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001080 }
1081
Max Reitz4fbec262015-02-05 13:58:19 -05001082 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001083 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1084 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001085 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001086 }
Max Reitz4fbec262015-02-05 13:58:19 -05001087 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001088
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001089 if (dev_offset) {
1090 QDict *raw_opts = qdict_new();
1091 qdict_put_str(raw_opts, "driver", "raw");
1092 qdict_put_str(raw_opts, "file", bs->node_name);
1093 qdict_put_int(raw_opts, "offset", dev_offset);
Kevin Wolfc6e0a6d2023-05-25 14:47:04 +02001094
1095 aio_context_acquire(qemu_get_aio_context());
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001096 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
Kevin Wolfc6e0a6d2023-05-25 14:47:04 +02001097 aio_context_release(qemu_get_aio_context());
1098
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001099 blk_remove_bs(blk);
1100 blk_insert_bs(blk, bs, &error_fatal);
1101 bdrv_unref(bs);
1102 }
1103
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001104 blk_set_enable_write_cache(blk, !writethrough);
1105
Wenchao Xia8c116b02013-12-04 17:10:55 +08001106 if (sn_opts) {
1107 ret = bdrv_snapshot_load_tmp(bs,
1108 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1109 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1110 &local_err);
1111 } else if (sn_id_or_name) {
1112 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1113 &local_err);
1114 }
1115 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001116 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001117 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001118 }
1119
Peter Lievenb3838a42014-08-13 19:20:18 +02001120 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001121
Eric Blakea5fced42022-05-11 19:49:23 -05001122 nbd_server_is_qemu_nbd(shared);
Kevin Wolf00917172020-09-24 17:26:57 +02001123
1124 export_opts = g_new(BlockExportOptions, 1);
1125 *export_opts = (BlockExportOptions) {
1126 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001127 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001128 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001129 .has_writethrough = true,
1130 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001131 .has_writable = true,
1132 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001133 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001134 .name = g_strdup(export_name),
Eric Blakecbad81c2020-10-27 00:05:49 -05001135 .description = g_strdup(export_description),
1136 .has_bitmaps = !!bitmaps,
1137 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001138 .has_allocation_depth = alloc_depth,
1139 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001140 },
1141 };
1142 blk_exp_add(export_opts, &error_fatal);
1143 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001144
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001145 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001146#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001147 int ret;
Denis V. Lunev03b67622023-07-17 16:55:40 +02001148 struct NbdClientOpts opts = {
1149 .device = device,
Denis V. Lunev5c56dd22023-07-17 16:55:41 +02001150 .fork_process = fork_process,
Denis V. Lunev414c0cf2023-07-17 22:25:20 +02001151 .verbose = verbose,
Denis V. Lunev03b67622023-07-17 16:55:40 +02001152 };
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001153
Denis V. Lunev03b67622023-07-17 16:55:40 +02001154 ret = pthread_create(&client_thread, NULL, nbd_client_thread, &opts);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001155 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001156 error_report("Failed to create client thread: %s", strerror(ret));
1157 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001158 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001159#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001160 } else {
1161 /* Shut up GCC warnings. */
1162 memset(&client_thread, 0, sizeof(client_thread));
1163 }
1164
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001165 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001166
Max Reitz637bc5a2019-05-08 23:18:16 +02001167 if (pid_file_name) {
1168 qemu_write_pidfile(pid_file_name, &error_fatal);
1169 }
1170
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001171 /* now when the initialization is (almost) complete, chdir("/")
1172 * to free any busy filesystems */
1173 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001174 error_report("Could not chdir to root directory: %s",
1175 strerror(errno));
1176 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001177 }
1178
Max Reitzffb31e12016-09-28 22:46:42 +02001179 if (fork_process) {
Denis V. Lunev173776f2023-07-17 16:55:44 +02001180 if (dup2(STDOUT_FILENO, STDERR_FILENO) < 0) {
1181 error_report("Could not set stderr to /dev/null: %s",
1182 strerror(errno));
1183 exit(EXIT_FAILURE);
1184 }
Max Reitzffb31e12016-09-28 22:46:42 +02001185 }
1186
Paolo Bonzini7860a382012-09-18 13:31:56 +02001187 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001188 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001189 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001190 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001191 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001192 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001193 }
1194 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001195
Markus Armbruster26f54e92014-10-07 13:59:04 +02001196 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001197 if (sockpath) {
1198 unlink(sockpath);
1199 }
bellard7a5ca862008-05-27 21:13:40 +00001200
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001201 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001202
Paolo Bonzinia517e882011-11-04 15:51:21 +01001203 if (device) {
1204 void *ret;
1205 pthread_join(client_thread, &ret);
1206 exit(ret != NULL);
1207 } else {
1208 exit(EXIT_SUCCESS);
1209 }
bellard7a5ca862008-05-27 21:13:40 +00001210}