blob: 93ef4e288fd7cb4c3a50e54f36ac5f891a46cf85 [file] [log] [blame]
thscd831bd2008-07-03 10:23:51 +00001/*
bellard7a5ca862008-05-27 21:13:40 +00002 * Copyright (C) 2005 Anthony Liguori <anthony@codemonkey.ws>
3 *
4 * Network Block Device
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; under version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
Blue Swirl8167ee82009-07-16 20:47:01 +000016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
bellard7a5ca862008-05-27 21:13:40 +000017 */
18
Peter Maydelld38ea872016-01-29 17:50:05 +000019#include "qemu/osdep.h"
Eric Blakec2a3d7d2017-07-21 08:50:47 -050020#include <getopt.h>
21#include <libgen.h>
22#include <pthread.h>
23
Markus Armbrustera8d25322019-05-23 16:35:08 +020024#include "qemu-common.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010025#include "qapi/error.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020026#include "qemu/cutils.h"
Markus Armbruster26f54e92014-10-07 13:59:04 +020027#include "sysemu/block-backend.h"
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"
Denis V. Lunev39ca4632016-06-17 17:44:12 +030046#include "trace/control.h"
Eric Blakebe377132017-07-21 08:50:46 -050047#include "qemu-version.h"
bellard7a5ca862008-05-27 21:13:40 +000048
Eric Blake3c1fa352018-12-15 07:53:08 -060049#ifdef __linux__
50#define HAVE_NBD_DEVICE 1
51#else
52#define HAVE_NBD_DEVICE 0
53#endif
54
Peter Lieven713cc672014-08-13 19:20:19 +020055#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
Daniel P. Berrangefa8b7ce2016-02-17 10:10:21 +000056#define QEMU_NBD_OPT_CACHE 256
57#define QEMU_NBD_OPT_AIO 257
58#define QEMU_NBD_OPT_DISCARD 258
59#define QEMU_NBD_OPT_DETECT_ZEROES 259
60#define QEMU_NBD_OPT_OBJECT 260
61#define QEMU_NBD_OPT_TLSCREDS 261
62#define QEMU_NBD_OPT_IMAGE_OPTS 262
Max Reitzffb31e12016-09-28 22:46:42 +020063#define QEMU_NBD_OPT_FORK 263
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000064#define QEMU_NBD_OPT_TLSAUTHZ 264
Max Reitz637bc5a2019-05-08 23:18:16 +020065#define QEMU_NBD_OPT_PID_FILE 265
bellard7a5ca862008-05-27 21:13:40 +000066
Eric Blakebd31c212016-05-06 10:26:42 -060067#define MBR_SIZE 512
68
blueswir1b1d8e522008-10-26 13:43:07 +000069static int verbose;
Paolo Bonzinia517e882011-11-04 15:51:21 +010070static char *srcpath;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +020071static SocketAddress *saddr;
Paolo Bonzini7860a382012-09-18 13:31:56 +020072static int persistent = 0;
Kevin Wolfd794f7f2020-09-24 17:26:56 +020073static enum { RUNNING, TERMINATE, TERMINATED } state;
Paolo Bonzinia61c6782011-09-12 17:28:11 +020074static int shared = 1;
75static int nb_fds;
Daniel P. Berrangee4849c12017-12-18 10:16:43 +000076static QIONetListener *server;
Daniel P. Berrange145614a2016-02-10 18:41:13 +000077static QCryptoTLSCreds *tlscreds;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +000078static const char *tlsauthz;
bellard7a5ca862008-05-27 21:13:40 +000079
80static void usage(const char *name)
81{
Paolo Bonzinib033cd82012-07-18 14:50:52 +020082 (printf) (
bellard7a5ca862008-05-27 21:13:40 +000083"Usage: %s [OPTIONS] FILE\n"
Eric Blake68b96f12019-01-17 13:36:56 -060084" or: %s -L [OPTIONS]\n"
85"QEMU Disk Network Block Device Utility\n"
bellard7a5ca862008-05-27 21:13:40 +000086"\n"
Peter Lieven713cc672014-08-13 19:20:19 +020087" -h, --help display this help and exit\n"
88" -V, --version output version information and exit\n"
bellard7a5ca862008-05-27 21:13:40 +000089"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +020090"Connection properties:\n"
Peter Lieven713cc672014-08-13 19:20:19 +020091" -p, --port=PORT port to listen on (default `%d')\n"
92" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
93" -k, --socket=PATH path to the unix socket\n"
94" (default '"SOCKET_PATH"')\n"
95" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
96" -t, --persistent don't exit on the last connection\n"
97" -v, --verbose display extra debugging information\n"
Vladimir Sementsov-Ogievskiyf5cd0bb2018-10-03 20:02:27 +030098" -x, --export-name=NAME expose export by name (default is empty string)\n"
99" -D, --description=TEXT export a human-readable description\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200100"\n"
101"Exposing part of the image:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200102" -o, --offset=OFFSET offset into the image\n"
Eric Blakedbc7b012020-10-27 00:05:55 -0500103" -A, --allocation-depth expose the allocation depth\n"
Eric Blake636192c2019-01-11 13:47:20 -0600104" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200105"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000106"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600107" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000108" --object type,id=ID,... define an object such as 'secret' for providing\n"
109" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500110" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000111" --tls-authz=ID use id of an earlier --object to provide\n"
112" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300113" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200115" --fork fork off the server process and exit the parent\n"
116" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200117" --pid-file=PATH store the server's process ID in the given file\n"
Eric Blake3c1fa352018-12-15 07:53:08 -0600118#if HAVE_NBD_DEVICE
119"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200120"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200121" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
122" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200123#endif
124"\n"
125"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200126" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
127" -r, --read-only export read-only\n"
128" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
129" file with backing_file=FILE, redirect the write to\n"
130" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800131" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200132" load an internal snapshot inside FILE and export it\n"
133" as an read-only device, SNAPSHOT_PARAM format is\n"
134" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
135" '[ID_OR_NAME]'\n"
136" -n, --nocache disable host cache\n"
137" --cache=MODE set cache mode (none, writeback, ...)\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000138" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200139" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300140" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000141" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200142"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500143QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600144 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000145}
146
147static void version(const char *name)
148{
149 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100150"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000151"Written by Anthony Liguori.\n"
152"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500153QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000154"This is free software; see the source for copying conditions. There is NO\n"
155"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000156 , name);
bellard7a5ca862008-05-27 21:13:40 +0000157}
158
Eric Blake029a88c2020-09-30 07:11:01 -0500159#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100160/*
161 * The client thread uses SIGTERM to interrupt the server. A signal
162 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
163 */
164void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100165{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100166 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200167 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100168}
Eric Blake029a88c2020-09-30 07:11:01 -0500169#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100170
Eric Blake68b96f12019-01-17 13:36:56 -0600171static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
172 const char *hostname)
173{
174 int ret = EXIT_FAILURE;
175 int rc;
176 Error *err = NULL;
177 QIOChannelSocket *sioc;
178 NBDExportInfo *list;
179 int i, j;
180
181 sioc = qio_channel_socket_new();
182 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
183 error_report_err(err);
Alex Chen992809b2020-11-30 12:36:51 +0000184 goto out;
Eric Blake68b96f12019-01-17 13:36:56 -0600185 }
186 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
187 &err);
188 if (rc < 0) {
189 if (err) {
190 error_report_err(err);
191 }
192 goto out;
193 }
194 printf("exports available: %d\n", rc);
195 for (i = 0; i < rc; i++) {
196 printf(" export: '%s'\n", list[i].name);
197 if (list[i].description && *list[i].description) {
198 printf(" description: %s\n", list[i].description);
199 }
200 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200201 static const char *const flag_names[] = {
202 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
203 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
204 [NBD_FLAG_SEND_FUA_BIT] = "fua",
205 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
206 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
207 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
208 [NBD_FLAG_SEND_DF_BIT] = "df",
209 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
210 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
211 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500212 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200213 };
214
Eric Blake68b96f12019-01-17 13:36:56 -0600215 printf(" size: %" PRIu64 "\n", list[i].size);
216 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200217 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
218 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
219 printf(" %s", flag_names[bit]);
220 }
Eric Blake68b96f12019-01-17 13:36:56 -0600221 }
222 printf(" )\n");
223 }
224 if (list[i].min_block) {
225 printf(" min block: %u\n", list[i].min_block);
226 printf(" opt block: %u\n", list[i].opt_block);
227 printf(" max block: %u\n", list[i].max_block);
228 }
229 if (list[i].n_contexts) {
230 printf(" available meta contexts: %d\n", list[i].n_contexts);
231 for (j = 0; j < list[i].n_contexts; j++) {
232 printf(" %s\n", list[i].contexts[j]);
233 }
234 }
235 }
236 nbd_free_export_list(list, rc);
237
238 ret = EXIT_SUCCESS;
239 out:
240 object_unref(OBJECT(sioc));
241 return ret;
242}
243
244
Eric Blake3c1fa352018-12-15 07:53:08 -0600245#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100246static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000247{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100248 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100249 int nbd;
thscd831bd2008-07-03 10:23:51 +0000250
Paolo Bonzinia517e882011-11-04 15:51:21 +0100251 /* linux just needs an open() to trigger
252 * the partition table update
253 * but remember to load the module with max_part != 0 :
254 * modprobe nbd max_part=63
255 */
256 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100257 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100258 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000259 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100260 return NULL;
261}
262
263static void *nbd_client_thread(void *arg)
264{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100265 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600266 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000267 QIOChannelSocket *sioc;
Alex Chenaf74b552020-12-08 13:49:44 +0000268 int fd = -1;
269 int ret = EXIT_FAILURE;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100270 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500271 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100272
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000273 sioc = qio_channel_socket_new();
274 if (qio_channel_socket_connect_sync(sioc,
275 saddr,
276 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100277 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000278 goto out;
279 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100280
Alex Chenaf74b552020-12-08 13:49:44 +0000281 if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
282 NULL, NULL, NULL, &info, &local_error) < 0) {
Max Reitz1ce52842015-01-26 21:02:59 -0500283 if (local_error) {
Markus Armbruster78288672015-12-18 16:35:07 +0100284 error_report_err(local_error);
Max Reitz1ce52842015-01-26 21:02:59 -0500285 }
Alex Chenaf74b552020-12-08 13:49:44 +0000286 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100287 }
288
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100289 fd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100290 if (fd < 0) {
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100291 /* Linux-only, we can use %m in printf. */
Markus Armbrusterb9884682015-12-18 16:35:18 +0100292 error_report("Failed to open %s: %m", device);
Alex Chenaf74b552020-12-08 13:49:44 +0000293 goto out;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100294 }
295
Alex Chenaf74b552020-12-08 13:49:44 +0000296 if (nbd_init(fd, sioc, &info, &local_error) < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300297 error_report_err(local_error);
Alex Chenaf74b552020-12-08 13:49:44 +0000298 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100299 }
300
301 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100302 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100303
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100304 if (verbose) {
305 fprintf(stderr, "NBD device %s is now connected to %s\n",
306 device, srcpath);
307 } else {
308 /* Close stderr so that the qemu-nbd process exits. */
309 dup2(STDOUT_FILENO, STDERR_FILENO);
310 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100311
Alex Chenaf74b552020-12-08 13:49:44 +0000312 if (nbd_client(fd) < 0) {
313 goto out;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100314 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100315
Alex Chenaf74b552020-12-08 13:49:44 +0000316 ret = EXIT_SUCCESS;
317
318 out:
319 if (fd >= 0) {
320 close(fd);
321 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000322 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600323 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100324 kill(getpid(), SIGTERM);
Alex Chenaf74b552020-12-08 13:49:44 +0000325 return (void *) (intptr_t) ret;
thscd831bd2008-07-03 10:23:51 +0000326}
Eric Blake3c1fa352018-12-15 07:53:08 -0600327#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000328
Fam Zhenge4afbf42015-05-19 10:50:59 +0000329static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200330{
Eric Blake3dcf56e2021-02-09 09:27:59 -0600331 return state == RUNNING && (shared == 0 || nb_fds < shared);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200332}
333
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000334static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000335
Eric Blake0c9390d2017-06-08 17:26:17 -0500336static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200337{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200338 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500339 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200340 state = TERMINATE;
341 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000342 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200343 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200344}
345
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000346static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
347 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200348{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200349 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000350 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200351 }
352
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800353 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000354 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000355 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200356}
357
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000358static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000359{
360 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000361 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000362 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000363 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000364 }
365}
366
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100367
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200368static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100369 const char *bindto,
370 const char *port)
371{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200372 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100373
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200374 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100375 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200376 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
377 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100378 } else {
Eric Blake03992932016-03-03 09:16:48 -0700379 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200380 saddr->type = SOCKET_ADDRESS_TYPE_INET;
381 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700382 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100383 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700384 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100385 } else {
Eric Blake03992932016-03-03 09:16:48 -0700386 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100387 }
388 }
389
390 return saddr;
391}
392
393
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000394static QemuOptsList file_opts = {
395 .name = "file",
396 .implied_opt_name = "file",
397 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
398 .desc = {
399 /* no elements => accept any params */
400 { /* end of list */ }
401 },
402};
403
Eric Blake68b96f12019-01-17 13:36:56 -0600404static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
405 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000406{
407 Object *obj;
408 QCryptoTLSCreds *creds;
409
410 obj = object_resolve_path_component(
411 object_get_objects_root(), id);
412 if (!obj) {
413 error_setg(errp, "No TLS credentials with id '%s'",
414 id);
415 return NULL;
416 }
417 creds = (QCryptoTLSCreds *)
418 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
419 if (!creds) {
420 error_setg(errp, "Object with id '%s' is not TLS credentials",
421 id);
422 return NULL;
423 }
424
Eric Blake68b96f12019-01-17 13:36:56 -0600425 if (list) {
426 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
427 error_setg(errp,
428 "Expecting TLS credentials with a client endpoint");
429 return NULL;
430 }
431 } else {
432 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
433 error_setg(errp,
434 "Expecting TLS credentials with a server endpoint");
435 return NULL;
436 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000437 }
438 object_ref(obj);
439 return creds;
440}
441
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000442static void setup_address_and_port(const char **address, const char **port)
443{
444 if (*address == NULL) {
445 *address = "0.0.0.0";
446 }
447
448 if (*port == NULL) {
449 *port = stringify(NBD_DEFAULT_PORT);
450 }
451}
452
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000453/*
454 * Check socket parameters compatibility when socket activation is used.
455 */
456static const char *socket_activation_validate_opts(const char *device,
457 const char *sockpath,
458 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600459 const char *port,
460 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000461{
462 if (device != NULL) {
463 return "NBD device can't be set when using socket activation";
464 }
465
466 if (sockpath != NULL) {
467 return "Unix socket can't be set when using socket activation";
468 }
469
470 if (address != NULL) {
471 return "The interface can't be set when using socket activation";
472 }
473
474 if (port != NULL) {
475 return "TCP port number can't be set when using socket activation";
476 }
477
Eric Blake68b96f12019-01-17 13:36:56 -0600478 if (list) {
479 return "List mode is incompatible with socket activation";
480 }
481
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000482 return NULL;
483}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000484
Kevin Wolfb3b52992018-05-16 13:46:37 +0200485static void qemu_nbd_shutdown(void)
486{
487 job_cancel_sync_all();
Sergio Lopez1895b972021-02-01 13:50:32 +0100488 blk_exp_close_all();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200489 bdrv_close_all();
490}
491
bellard7a5ca862008-05-27 21:13:40 +0000492int main(int argc, char **argv)
493{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200494 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000495 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600496 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500497 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000498 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000499 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100500 const char *port = NULL;
501 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100502 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800503 QemuOpts *sn_opts = NULL;
504 const char *sn_id_or_name = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500505 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
bellard7a5ca862008-05-27 21:13:40 +0000506 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000507 { "help", no_argument, NULL, 'h' },
508 { "version", no_argument, NULL, 'V' },
509 { "bind", required_argument, NULL, 'b' },
510 { "port", required_argument, NULL, 'p' },
511 { "socket", required_argument, NULL, 'k' },
512 { "offset", required_argument, NULL, 'o' },
513 { "read-only", no_argument, NULL, 'r' },
Eric Blakedbc7b012020-10-27 00:05:55 -0500514 { "allocation-depth", no_argument, NULL, 'A' },
Eric Blake636192c2019-01-11 13:47:20 -0600515 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000516 { "connect", required_argument, NULL, 'c' },
517 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600518 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000519 { "snapshot", no_argument, NULL, 's' },
520 { "load-snapshot", required_argument, NULL, 'l' },
521 { "nocache", no_argument, NULL, 'n' },
522 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
523 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
524 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
525 { "detect-zeroes", required_argument, NULL,
526 QEMU_NBD_OPT_DETECT_ZEROES },
527 { "shared", required_argument, NULL, 'e' },
528 { "format", required_argument, NULL, 'f' },
529 { "persistent", no_argument, NULL, 't' },
530 { "verbose", no_argument, NULL, 'v' },
531 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
532 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500533 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000534 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000535 { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000536 { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300537 { "trace", required_argument, NULL, 'T' },
Max Reitzffb31e12016-09-28 22:46:42 +0200538 { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
Max Reitz637bc5a2019-05-08 23:18:16 +0200539 { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
Blue Swirl660f11b2009-07-31 21:16:51 +0000540 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000541 };
542 int ch;
543 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200544 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500545 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200546 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100547 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200548 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100549 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000550 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200551 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200552 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500553 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600554 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500555 const char *export_description = NULL;
Eric Blakecbad81c2020-10-27 00:05:49 -0500556 strList *bitmaps = NULL;
Eric Blakedbc7b012020-10-27 00:05:55 -0500557 bool alloc_depth = false;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000558 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000559 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100560 bool writethrough = true;
Max Reitzffb31e12016-09-28 22:46:42 +0200561 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600562 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200563 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000564 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200565 const char *pid_file_name = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200566 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000567
Eric Blake029a88c2020-09-30 07:11:01 -0500568#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100569 os_setup_early_signal_handling();
570 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200571#endif
572
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100573 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100574 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100575 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300576 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100577
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000578 module_call_init(MODULE_INIT_QOM);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300579 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800580 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100581
bellard7a5ca862008-05-27 21:13:40 +0000582 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
583 switch (ch) {
584 case 's':
ths2f726482008-07-03 11:47:46 +0000585 flags |= BDRV_O_SNAPSHOT;
586 break;
587 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200588 optarg = (char *) "none";
589 /* fallthrough */
590 case QEMU_NBD_OPT_CACHE:
591 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100592 error_report("-n and --cache can only be specified once");
593 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200594 }
595 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100596 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100597 error_report("Invalid cache mode `%s'", optarg);
598 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200599 }
bellard7a5ca862008-05-27 21:13:40 +0000600 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200601 case QEMU_NBD_OPT_AIO:
602 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100603 error_report("--aio can only be specified once");
604 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200605 }
606 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000607 if (bdrv_parse_aio(optarg, &flags) < 0) {
608 error_report("Invalid aio mode '%s'", optarg);
609 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200610 }
611 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100612 case QEMU_NBD_OPT_DISCARD:
613 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100614 error_report("--discard can only be specified once");
615 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100616 }
617 seen_discard = true;
618 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100619 error_report("Invalid discard mode `%s'", optarg);
620 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100621 }
622 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200623 case QEMU_NBD_OPT_DETECT_ZEROES:
624 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200625 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200626 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200627 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
628 &local_err);
629 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100630 error_reportf_err(local_err,
631 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100632 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200633 }
634 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
635 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100636 error_report("setting detect-zeroes to unmap is not allowed "
637 "without setting discard operation to unmap");
638 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200639 }
640 break;
bellard7a5ca862008-05-27 21:13:40 +0000641 case 'b':
642 bindto = optarg;
643 break;
644 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100645 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000646 break;
647 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600648 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
649 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100650 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000651 }
bellard7a5ca862008-05-27 21:13:40 +0000652 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800653 case 'l':
654 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100655 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
656 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800657 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100658 error_report("Failed in parsing snapshot param `%s'",
659 optarg);
660 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800661 }
662 } else {
663 sn_id_or_name = optarg;
664 }
665 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000666 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500667 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200668 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000669 break;
Eric Blakedbc7b012020-10-27 00:05:55 -0500670 case 'A':
671 alloc_depth = true;
672 break;
Eric Blake636192c2019-01-11 13:47:20 -0600673 case 'B':
Eric Blakecbad81c2020-10-27 00:05:49 -0500674 QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
Eric Blake636192c2019-01-11 13:47:20 -0600675 break;
thscd831bd2008-07-03 10:23:51 +0000676 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100677 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200678 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100679 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100680 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200681 }
thscd831bd2008-07-03 10:23:51 +0000682 break;
683 case 'd':
684 disconnect = true;
685 break;
686 case 'c':
687 device = optarg;
688 break;
ths3b05a8e2008-07-03 12:45:02 +0000689 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600690 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
Eric Blake3dcf56e2021-02-09 09:27:59 -0600691 shared < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100692 error_report("Invalid shared device number '%s'", optarg);
693 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000694 }
ths3b05a8e2008-07-03 12:45:02 +0000695 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000696 case 'f':
697 fmt = optarg;
698 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200699 case 't':
700 persistent = 1;
701 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000702 case 'x':
703 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600704 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
705 error_report("export name '%s' too long", export_name);
706 exit(EXIT_FAILURE);
707 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000708 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500709 case 'D':
710 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600711 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
712 error_report("export description '%s' too long",
713 export_description);
714 exit(EXIT_FAILURE);
715 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500716 break;
bellard7a5ca862008-05-27 21:13:40 +0000717 case 'v':
718 verbose = 1;
719 break;
720 case 'V':
721 version(argv[0]);
722 exit(0);
723 break;
724 case 'h':
725 usage(argv[0]);
726 exit(0);
727 break;
728 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100729 error_report("Try `%s --help' for more information.", argv[0]);
730 exit(EXIT_FAILURE);
Kevin Wolffa40e432021-02-17 12:56:45 +0100731 case QEMU_NBD_OPT_OBJECT:
732 user_creatable_process_cmdline(optarg);
733 break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000734 case QEMU_NBD_OPT_TLSCREDS:
735 tlscredsid = optarg;
736 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000737 case QEMU_NBD_OPT_IMAGE_OPTS:
738 imageOpts = true;
739 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300740 case 'T':
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500741 trace_opt_parse(optarg);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300742 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000743 case QEMU_NBD_OPT_TLSAUTHZ:
744 tlsauthz = optarg;
745 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200746 case QEMU_NBD_OPT_FORK:
747 fork_process = true;
748 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600749 case 'L':
750 list = true;
751 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200752 case QEMU_NBD_OPT_PID_FILE:
753 pid_file_name = optarg;
754 break;
bellard7a5ca862008-05-27 21:13:40 +0000755 }
756 }
757
Eric Blake68b96f12019-01-17 13:36:56 -0600758 if (list) {
759 if (argc != optind) {
760 error_report("List mode is incompatible with a file name");
761 exit(EXIT_FAILURE);
762 }
Eric Blake0bc16992020-01-23 10:46:50 -0600763 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500764 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blakedbc7b012020-10-27 00:05:55 -0500765 alloc_depth || seen_aio || seen_discard || seen_cache) {
Eric Blake68b96f12019-01-17 13:36:56 -0600766 error_report("List mode is incompatible with per-device settings");
767 exit(EXIT_FAILURE);
768 }
769 if (fork_process) {
770 error_report("List mode is incompatible with forking");
771 exit(EXIT_FAILURE);
772 }
773 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100774 error_report("Invalid number of arguments");
775 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100776 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600777 } else if (!export_name) {
778 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000779 }
780
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300781 if (!trace_init_backends()) {
782 exit(1);
783 }
Paolo Bonzini92eecff2020-11-02 06:58:41 -0500784 trace_init_file();
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300785 qemu_set_log(LOG_TRACE);
786
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000787 socket_activation = check_socket_activation();
788 if (socket_activation == 0) {
789 setup_address_and_port(&bindto, &port);
790 } else {
791 /* Using socket activation - check user didn't use -p etc. */
792 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600793 bindto, port,
794 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000795 if (err_msg != NULL) {
796 error_report("%s", err_msg);
797 exit(EXIT_FAILURE);
798 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100799
800 /* qemu-nbd can only listen on a single socket. */
801 if (socket_activation > 1) {
802 error_report("qemu-nbd does not support socket activation with %s > 1",
803 "LISTEN_FDS");
804 exit(EXIT_FAILURE);
805 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000806 }
807
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000808 if (tlscredsid) {
809 if (sockpath) {
810 error_report("TLS is only supported with IPv4/IPv6");
811 exit(EXIT_FAILURE);
812 }
813 if (device) {
814 error_report("TLS is not supported with a host device");
815 exit(EXIT_FAILURE);
816 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000817 if (tlsauthz && list) {
818 error_report("TLS authorization is incompatible with export list");
819 exit(EXIT_FAILURE);
820 }
Eric Blake68b96f12019-01-17 13:36:56 -0600821 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000822 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200823 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000824 exit(EXIT_FAILURE);
825 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000826 } else {
827 if (tlsauthz) {
828 error_report("--tls-authz is not permitted without --tls-creds");
829 exit(EXIT_FAILURE);
830 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000831 }
832
Eric Blake68b96f12019-01-17 13:36:56 -0600833 if (list) {
834 saddr = nbd_build_socket_address(sockpath, bindto, port);
835 return qemu_nbd_client_list(saddr, tlscreds, bindto);
836 }
837
Eric Blake3c1fa352018-12-15 07:53:08 -0600838#if !HAVE_NBD_DEVICE
839 if (disconnect || device) {
840 error_report("Kernel /dev/nbdN support not available");
841 exit(EXIT_FAILURE);
842 }
843#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000844 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000845 int nbdfd = open(argv[optind], O_RDWR);
846 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100847 error_report("Cannot open %s: %s", argv[optind],
848 strerror(errno));
849 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100850 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000851 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000852
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000853 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000854
855 printf("%s disconnected\n", argv[optind]);
856
Peter Lieven713cc672014-08-13 19:20:19 +0200857 return 0;
thscd831bd2008-07-03 10:23:51 +0000858 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600859#endif
thscd831bd2008-07-03 10:23:51 +0000860
Max Reitzffb31e12016-09-28 22:46:42 +0200861 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100862#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100863 int stderr_fd[2];
864 pid_t pid;
865 int ret;
866
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100867 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100868 error_report("Error setting up communication pipe: %s",
869 strerror(errno));
870 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100871 }
872
873 /* Now daemonize, but keep a communication channel open to
874 * print errors and exit with the proper status code.
875 */
876 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500877 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100878 error_report("Failed to fork: %s", strerror(errno));
879 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500880 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100881 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200882
Raphael Pour0eaf4532020-05-15 08:36:07 +0200883 /* Remember parent's stderr if we will be restoring it. */
884 if (fork_process) {
885 old_stderr = dup(STDERR_FILENO);
886 }
887
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400888 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100889
890 /* Temporarily redirect stderr to the parent's pipe... */
891 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100892 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100893 error_report("Failed to daemonize: %s", strerror(errno));
894 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100895 }
896
897 /* ... close the descriptor we inherited and go on. */
898 close(stderr_fd[1]);
899 } else {
900 bool errors = false;
901 char *buf;
902
903 /* In the parent. Print error messages from the child until
904 * it closes the pipe.
905 */
906 close(stderr_fd[1]);
907 buf = g_malloc(1024);
908 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
909 errors = true;
910 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100911 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100912 exit(EXIT_FAILURE);
913 }
914 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100915 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100916 error_report("Cannot read from daemon: %s",
917 strerror(errno));
918 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100919 }
920
921 /* Usually the daemon should not print any message.
922 * Exit with zero status in that case.
923 */
924 exit(errors);
925 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100926#else /* WIN32 */
927 error_report("Unable to fork into background on Windows hosts");
928 exit(EXIT_FAILURE);
929#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100930 }
931
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100932 if (device != NULL && sockpath == NULL) {
933 sockpath = g_malloc(128);
934 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000935 }
936
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000937 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000938 if (socket_activation == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600939 int backlog;
940
Eric Blake3dcf56e2021-02-09 09:27:59 -0600941 if (persistent || shared == 0) {
Eric Blake582d4212021-02-09 09:27:58 -0600942 backlog = SOMAXCONN;
943 } else {
944 backlog = MIN(shared, SOMAXCONN);
945 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000946 saddr = nbd_build_socket_address(sockpath, bindto, port);
Eric Blake582d4212021-02-09 09:27:58 -0600947 if (qio_net_listener_open_sync(server, saddr, backlog,
948 &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000949 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000950 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000951 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000952 }
953 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000954 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000955 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000956 for (i = 0; i < socket_activation; i++) {
957 QIOChannelSocket *sioc;
958 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
959 &local_err);
960 if (sioc == NULL) {
961 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +0200962 error_reportf_err(local_err,
963 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000964 exit(EXIT_FAILURE);
965 }
966 qio_net_listener_add(server, sioc);
967 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000968 }
969 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100970
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300971 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100972 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300973 exit(EXIT_FAILURE);
974 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100975 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200976 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100977
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000978 srcpath = argv[optind];
979 if (imageOpts) {
980 QemuOpts *opts;
981 if (fmt) {
982 error_report("--image-opts and -f are mutually exclusive");
983 exit(EXIT_FAILURE);
984 }
985 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
986 if (!opts) {
987 qemu_opts_reset(&file_opts);
988 exit(EXIT_FAILURE);
989 }
990 options = qemu_opts_to_qdict(opts, NULL);
991 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +0100992 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000993 } else {
994 if (fmt) {
995 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -0500996 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000997 }
Max Reitzefaa7c42016-03-16 19:54:38 +0100998 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000999 }
1000
Max Reitz4fbec262015-02-05 13:58:19 -05001001 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001002 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1003 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001004 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001005 }
Max Reitz4fbec262015-02-05 13:58:19 -05001006 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001007
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001008 if (dev_offset) {
1009 QDict *raw_opts = qdict_new();
1010 qdict_put_str(raw_opts, "driver", "raw");
1011 qdict_put_str(raw_opts, "file", bs->node_name);
1012 qdict_put_int(raw_opts, "offset", dev_offset);
1013 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1014 blk_remove_bs(blk);
1015 blk_insert_bs(blk, bs, &error_fatal);
1016 bdrv_unref(bs);
1017 }
1018
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001019 blk_set_enable_write_cache(blk, !writethrough);
1020
Wenchao Xia8c116b02013-12-04 17:10:55 +08001021 if (sn_opts) {
1022 ret = bdrv_snapshot_load_tmp(bs,
1023 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1024 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1025 &local_err);
1026 } else if (sn_id_or_name) {
1027 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1028 &local_err);
1029 }
1030 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001031 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001032 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001033 }
1034
Peter Lievenb3838a42014-08-13 19:20:18 +02001035 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001036
Kevin Wolf00917172020-09-24 17:26:57 +02001037 nbd_server_is_qemu_nbd(true);
1038
1039 export_opts = g_new(BlockExportOptions, 1);
1040 *export_opts = (BlockExportOptions) {
1041 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001042 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001043 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001044 .has_writethrough = true,
1045 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001046 .has_writable = true,
1047 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001048 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001049 .has_name = true,
1050 .name = g_strdup(export_name),
1051 .has_description = !!export_description,
1052 .description = g_strdup(export_description),
1053 .has_bitmaps = !!bitmaps,
1054 .bitmaps = bitmaps,
Eric Blakedbc7b012020-10-27 00:05:55 -05001055 .has_allocation_depth = alloc_depth,
1056 .allocation_depth = alloc_depth,
Kevin Wolf00917172020-09-24 17:26:57 +02001057 },
1058 };
1059 blk_exp_add(export_opts, &error_fatal);
1060 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001061
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001062 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001063#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001064 int ret;
1065
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001066 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001067 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001068 error_report("Failed to create client thread: %s", strerror(ret));
1069 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001070 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001071#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001072 } else {
1073 /* Shut up GCC warnings. */
1074 memset(&client_thread, 0, sizeof(client_thread));
1075 }
1076
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001077 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001078
Max Reitz637bc5a2019-05-08 23:18:16 +02001079 if (pid_file_name) {
1080 qemu_write_pidfile(pid_file_name, &error_fatal);
1081 }
1082
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001083 /* now when the initialization is (almost) complete, chdir("/")
1084 * to free any busy filesystems */
1085 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001086 error_report("Could not chdir to root directory: %s",
1087 strerror(errno));
1088 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001089 }
1090
Max Reitzffb31e12016-09-28 22:46:42 +02001091 if (fork_process) {
1092 dup2(old_stderr, STDERR_FILENO);
1093 close(old_stderr);
1094 }
1095
Paolo Bonzini7860a382012-09-18 13:31:56 +02001096 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001097 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001098 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001099 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001100 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001101 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001102 }
1103 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001104
Markus Armbruster26f54e92014-10-07 13:59:04 +02001105 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001106 if (sockpath) {
1107 unlink(sockpath);
1108 }
bellard7a5ca862008-05-27 21:13:40 +00001109
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001110 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001111
Paolo Bonzinia517e882011-11-04 15:51:21 +01001112 if (device) {
1113 void *ret;
1114 pthread_join(client_thread, &ret);
1115 exit(ret != NULL);
1116 } else {
1117 exit(EXIT_SUCCESS);
1118 }
bellard7a5ca862008-05-27 21:13:40 +00001119}