blob: 74e73a74657b30d84506f82d265e87de76ea85eb [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 Blake636192c2019-01-11 13:47:20 -0600103" -B, --bitmap=NAME expose a persistent dirty bitmap\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200104"\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000105"General purpose options:\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600106" -L, --list list exports available from another NBD server\n"
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000107" --object type,id=ID,... define an object such as 'secret' for providing\n"
108" passwords and/or encryption keys\n"
Eric Blakef7812df2018-10-03 13:04:26 -0500109" --tls-creds=ID use id of an earlier --object to provide TLS\n"
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000110" --tls-authz=ID use id of an earlier --object to provide\n"
111" authorization\n"
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300112" -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
113" specify tracing options\n"
Max Reitzffb31e12016-09-28 22:46:42 +0200114" --fork fork off the server process and exit the parent\n"
115" once the server is running\n"
Max Reitz637bc5a2019-05-08 23:18:16 +0200116" --pid-file=PATH store the server's process ID in the given file\n"
Eric Blake3c1fa352018-12-15 07:53:08 -0600117#if HAVE_NBD_DEVICE
118"\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200119"Kernel NBD client support:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200120" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
121" -d, --disconnect disconnect the specified device\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200122#endif
123"\n"
124"Block device options:\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200125" -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
126" -r, --read-only export read-only\n"
127" -s, --snapshot use FILE as an external snapshot, create a temporary\n"
128" file with backing_file=FILE, redirect the write to\n"
129" the temporary one\n"
Wenchao Xia8c116b02013-12-04 17:10:55 +0800130" -l, --load-snapshot=SNAPSHOT_PARAM\n"
Peter Lieven713cc672014-08-13 19:20:19 +0200131" load an internal snapshot inside FILE and export it\n"
132" as an read-only device, SNAPSHOT_PARAM format is\n"
133" 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
134" '[ID_OR_NAME]'\n"
135" -n, --nocache disable host cache\n"
136" --cache=MODE set cache mode (none, writeback, ...)\n"
Aarushi Mehta76802742020-01-20 14:18:56 +0000137" --aio=MODE set AIO mode (native, io_uring or threads)\n"
Peter Lievenb3838a42014-08-13 19:20:18 +0200138" --discard=MODE set discard mode (ignore, unmap)\n"
Andrey Korolyov6883de62015-07-31 22:12:54 +0300139" --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000140" --image-opts treat FILE as a full set of image options\n"
Paolo Bonzinib033cd82012-07-18 14:50:52 +0200141"\n"
Eric Blakef5048cb2017-08-03 11:33:53 -0500142QEMU_HELP_BOTTOM "\n"
Eric Blake68b96f12019-01-17 13:36:56 -0600143 , name, name, NBD_DEFAULT_PORT, "DEVICE");
bellard7a5ca862008-05-27 21:13:40 +0000144}
145
146static void version(const char *name)
147{
148 printf(
Thomas Huth7e563bf2018-02-15 12:06:47 +0100149"%s " QEMU_FULL_VERSION "\n"
bellard7a5ca862008-05-27 21:13:40 +0000150"Written by Anthony Liguori.\n"
151"\n"
Eric Blakebe377132017-07-21 08:50:46 -0500152QEMU_COPYRIGHT "\n"
bellard7a5ca862008-05-27 21:13:40 +0000153"This is free software; see the source for copying conditions. There is NO\n"
154"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
ths315bc7a2008-07-18 18:06:23 +0000155 , name);
bellard7a5ca862008-05-27 21:13:40 +0000156}
157
Eric Blake029a88c2020-09-30 07:11:01 -0500158#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100159/*
160 * The client thread uses SIGTERM to interrupt the server. A signal
161 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
162 */
163void qemu_system_killed(int signum, pid_t pid)
Paolo Bonzinibb345112011-11-04 15:51:19 +0100164{
Stefan Hajnoczid73415a2020-09-23 11:56:46 +0100165 qatomic_cmpxchg(&state, RUNNING, TERMINATE);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200166 qemu_notify_event();
Paolo Bonzinibb345112011-11-04 15:51:19 +0100167}
Eric Blake029a88c2020-09-30 07:11:01 -0500168#endif /* CONFIG_POSIX */
Paolo Bonzini537b41f2014-02-17 14:43:51 +0100169
Eric Blake68b96f12019-01-17 13:36:56 -0600170static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
171 const char *hostname)
172{
173 int ret = EXIT_FAILURE;
174 int rc;
175 Error *err = NULL;
176 QIOChannelSocket *sioc;
177 NBDExportInfo *list;
178 int i, j;
179
180 sioc = qio_channel_socket_new();
181 if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
182 error_report_err(err);
183 return EXIT_FAILURE;
184 }
185 rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
186 &err);
187 if (rc < 0) {
188 if (err) {
189 error_report_err(err);
190 }
191 goto out;
192 }
193 printf("exports available: %d\n", rc);
194 for (i = 0; i < rc; i++) {
195 printf(" export: '%s'\n", list[i].name);
196 if (list[i].description && *list[i].description) {
197 printf(" description: %s\n", list[i].description);
198 }
199 if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
Max Reitzc4e2aff2019-04-05 21:16:35 +0200200 static const char *const flag_names[] = {
201 [NBD_FLAG_READ_ONLY_BIT] = "readonly",
202 [NBD_FLAG_SEND_FLUSH_BIT] = "flush",
203 [NBD_FLAG_SEND_FUA_BIT] = "fua",
204 [NBD_FLAG_ROTATIONAL_BIT] = "rotational",
205 [NBD_FLAG_SEND_TRIM_BIT] = "trim",
206 [NBD_FLAG_SEND_WRITE_ZEROES_BIT] = "zeroes",
207 [NBD_FLAG_SEND_DF_BIT] = "df",
208 [NBD_FLAG_CAN_MULTI_CONN_BIT] = "multi",
209 [NBD_FLAG_SEND_RESIZE_BIT] = "resize",
210 [NBD_FLAG_SEND_CACHE_BIT] = "cache",
Eric Blake0a479542019-08-23 09:37:23 -0500211 [NBD_FLAG_SEND_FAST_ZERO_BIT] = "fast-zero",
Max Reitzc4e2aff2019-04-05 21:16:35 +0200212 };
213
Eric Blake68b96f12019-01-17 13:36:56 -0600214 printf(" size: %" PRIu64 "\n", list[i].size);
215 printf(" flags: 0x%x (", list[i].flags);
Max Reitzc4e2aff2019-04-05 21:16:35 +0200216 for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
217 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
218 printf(" %s", flag_names[bit]);
219 }
Eric Blake68b96f12019-01-17 13:36:56 -0600220 }
221 printf(" )\n");
222 }
223 if (list[i].min_block) {
224 printf(" min block: %u\n", list[i].min_block);
225 printf(" opt block: %u\n", list[i].opt_block);
226 printf(" max block: %u\n", list[i].max_block);
227 }
228 if (list[i].n_contexts) {
229 printf(" available meta contexts: %d\n", list[i].n_contexts);
230 for (j = 0; j < list[i].n_contexts; j++) {
231 printf(" %s\n", list[i].contexts[j]);
232 }
233 }
234 }
235 nbd_free_export_list(list, rc);
236
237 ret = EXIT_SUCCESS;
238 out:
239 object_unref(OBJECT(sioc));
240 return ret;
241}
242
243
Eric Blake3c1fa352018-12-15 07:53:08 -0600244#if HAVE_NBD_DEVICE
Paolo Bonzinia517e882011-11-04 15:51:21 +0100245static void *show_parts(void *arg)
thscd831bd2008-07-03 10:23:51 +0000246{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100247 char *device = arg;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100248 int nbd;
thscd831bd2008-07-03 10:23:51 +0000249
Paolo Bonzinia517e882011-11-04 15:51:21 +0100250 /* linux just needs an open() to trigger
251 * the partition table update
252 * but remember to load the module with max_part != 0 :
253 * modprobe nbd max_part=63
254 */
255 nbd = open(device, O_RDWR);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100256 if (nbd >= 0) {
Paolo Bonzinia517e882011-11-04 15:51:21 +0100257 close(nbd);
thscd831bd2008-07-03 10:23:51 +0000258 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100259 return NULL;
260}
261
262static void *nbd_client_thread(void *arg)
263{
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100264 char *device = arg;
Eric Blake6dc16672019-01-17 13:36:46 -0600265 NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000266 QIOChannelSocket *sioc;
267 int fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100268 int ret;
269 pthread_t show_parts_thread;
Max Reitz1ce52842015-01-26 21:02:59 -0500270 Error *local_error = NULL;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100271
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000272 sioc = qio_channel_socket_new();
273 if (qio_channel_socket_connect_sync(sioc,
274 saddr,
275 &local_error) < 0) {
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100276 error_report_err(local_error);
Stefan Hajnoczidc10e8b2012-01-05 13:16:07 +0000277 goto out;
278 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100279
Vladimir Sementsov-Ogievskiya8e2bb62019-06-18 14:43:21 +0300280 ret = nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
Eric Blake004a89f2017-07-07 15:30:41 -0500281 NULL, NULL, NULL, &info, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100282 if (ret < 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 }
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100286 goto out_socket;
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);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100293 goto out_socket;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100294 }
295
Eric Blake004a89f2017-07-07 15:30:41 -0500296 ret = nbd_init(fd, sioc, &info, &local_error);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100297 if (ret < 0) {
Vladimir Sementsov-Ogievskiybe41c102017-05-26 14:09:13 +0300298 error_report_err(local_error);
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100299 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100300 }
301
302 /* update partition table */
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100303 pthread_create(&show_parts_thread, NULL, show_parts, device);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100304
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100305 if (verbose) {
306 fprintf(stderr, "NBD device %s is now connected to %s\n",
307 device, srcpath);
308 } else {
309 /* Close stderr so that the qemu-nbd process exits. */
310 dup2(STDOUT_FILENO, STDERR_FILENO);
311 }
Paolo Bonzinia517e882011-11-04 15:51:21 +0100312
313 ret = nbd_client(fd);
314 if (ret) {
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100315 goto out_fd;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100316 }
317 close(fd);
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000318 object_unref(OBJECT(sioc));
Eric Blake6dc16672019-01-17 13:36:46 -0600319 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100320 kill(getpid(), SIGTERM);
321 return (void *) EXIT_SUCCESS;
322
Paolo Bonzini0c544d72014-03-14 18:10:54 +0100323out_fd:
324 close(fd);
325out_socket:
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000326 object_unref(OBJECT(sioc));
Paolo Bonzinia517e882011-11-04 15:51:21 +0100327out:
Eric Blake6dc16672019-01-17 13:36:46 -0600328 g_free(info.name);
Paolo Bonzinia517e882011-11-04 15:51:21 +0100329 kill(getpid(), SIGTERM);
330 return (void *) EXIT_FAILURE;
thscd831bd2008-07-03 10:23:51 +0000331}
Eric Blake3c1fa352018-12-15 07:53:08 -0600332#endif /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000333
Fam Zhenge4afbf42015-05-19 10:50:59 +0000334static int nbd_can_accept(void)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200335{
Eric Blakedf8ad9f2017-05-26 22:04:21 -0500336 return state == RUNNING && nb_fds < shared;
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200337}
338
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000339static void nbd_update_server_watch(void);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000340
Eric Blake0c9390d2017-06-08 17:26:17 -0500341static void nbd_client_closed(NBDClient *client, bool negotiated)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200342{
Paolo Bonzini1743b512011-09-19 14:33:23 +0200343 nb_fds--;
Eric Blake0c9390d2017-06-08 17:26:17 -0500344 if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
Paolo Bonzini7860a382012-09-18 13:31:56 +0200345 state = TERMINATE;
346 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000347 nbd_update_server_watch();
Paolo Bonzini7860a382012-09-18 13:31:56 +0200348 nbd_client_put(client);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200349}
350
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000351static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
352 gpointer opaque)
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200353{
Paolo Bonzini7860a382012-09-18 13:31:56 +0200354 if (state >= TERMINATE) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000355 return;
Paolo Bonzini7860a382012-09-18 13:31:56 +0200356 }
357
Fam Zhengee7d7aa2016-01-14 16:41:01 +0800358 nb_fds++;
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000359 nbd_update_server_watch();
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000360 nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
Paolo Bonzinia61c6782011-09-12 17:28:11 +0200361}
362
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000363static void nbd_update_server_watch(void)
Fam Zhenge4afbf42015-05-19 10:50:59 +0000364{
365 if (nbd_can_accept()) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000366 qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000367 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000368 qio_net_listener_set_client_func(server, NULL, NULL, NULL);
Fam Zhenge4afbf42015-05-19 10:50:59 +0000369 }
370}
371
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100372
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200373static SocketAddress *nbd_build_socket_address(const char *sockpath,
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100374 const char *bindto,
375 const char *port)
376{
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200377 SocketAddress *saddr;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100378
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200379 saddr = g_new0(SocketAddress, 1);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100380 if (sockpath) {
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200381 saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
382 saddr->u.q_unix.path = g_strdup(sockpath);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100383 } else {
Eric Blake03992932016-03-03 09:16:48 -0700384 InetSocketAddress *inet;
Markus Armbrusterbd269eb2017-04-26 09:36:41 +0200385 saddr->type = SOCKET_ADDRESS_TYPE_INET;
386 inet = &saddr->u.inet;
Eric Blake03992932016-03-03 09:16:48 -0700387 inet->host = g_strdup(bindto);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100388 if (port) {
Eric Blake03992932016-03-03 09:16:48 -0700389 inet->port = g_strdup(port);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100390 } else {
Eric Blake03992932016-03-03 09:16:48 -0700391 inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100392 }
393 }
394
395 return saddr;
396}
397
398
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000399static QemuOptsList file_opts = {
400 .name = "file",
401 .implied_opt_name = "file",
402 .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
403 .desc = {
404 /* no elements => accept any params */
405 { /* end of list */ }
406 },
407};
408
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000409static QemuOptsList qemu_object_opts = {
410 .name = "object",
411 .implied_opt_name = "qom-type",
412 .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
413 .desc = {
414 { }
415 },
416};
417
Kevin Wolf495bf892019-10-11 21:49:17 +0200418static bool qemu_nbd_object_print_help(const char *type, QemuOpts *opts)
419{
420 if (user_creatable_print_help(type, opts)) {
421 exit(0);
422 }
423 return true;
424}
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000425
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000426
Eric Blake68b96f12019-01-17 13:36:56 -0600427static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
428 Error **errp)
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000429{
430 Object *obj;
431 QCryptoTLSCreds *creds;
432
433 obj = object_resolve_path_component(
434 object_get_objects_root(), id);
435 if (!obj) {
436 error_setg(errp, "No TLS credentials with id '%s'",
437 id);
438 return NULL;
439 }
440 creds = (QCryptoTLSCreds *)
441 object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
442 if (!creds) {
443 error_setg(errp, "Object with id '%s' is not TLS credentials",
444 id);
445 return NULL;
446 }
447
Eric Blake68b96f12019-01-17 13:36:56 -0600448 if (list) {
449 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
450 error_setg(errp,
451 "Expecting TLS credentials with a client endpoint");
452 return NULL;
453 }
454 } else {
455 if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
456 error_setg(errp,
457 "Expecting TLS credentials with a server endpoint");
458 return NULL;
459 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000460 }
461 object_ref(obj);
462 return creds;
463}
464
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000465static void setup_address_and_port(const char **address, const char **port)
466{
467 if (*address == NULL) {
468 *address = "0.0.0.0";
469 }
470
471 if (*port == NULL) {
472 *port = stringify(NBD_DEFAULT_PORT);
473 }
474}
475
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000476/*
477 * Check socket parameters compatibility when socket activation is used.
478 */
479static const char *socket_activation_validate_opts(const char *device,
480 const char *sockpath,
481 const char *address,
Eric Blake68b96f12019-01-17 13:36:56 -0600482 const char *port,
483 bool list)
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000484{
485 if (device != NULL) {
486 return "NBD device can't be set when using socket activation";
487 }
488
489 if (sockpath != NULL) {
490 return "Unix socket can't be set when using socket activation";
491 }
492
493 if (address != NULL) {
494 return "The interface can't be set when using socket activation";
495 }
496
497 if (port != NULL) {
498 return "TCP port number can't be set when using socket activation";
499 }
500
Eric Blake68b96f12019-01-17 13:36:56 -0600501 if (list) {
502 return "List mode is incompatible with socket activation";
503 }
504
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000505 return NULL;
506}
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000507
Kevin Wolfb3b52992018-05-16 13:46:37 +0200508static void qemu_nbd_shutdown(void)
509{
510 job_cancel_sync_all();
511 bdrv_close_all();
512}
513
bellard7a5ca862008-05-27 21:13:40 +0000514int main(int argc, char **argv)
515{
Markus Armbruster26f54e92014-10-07 13:59:04 +0200516 BlockBackend *blk;
bellard7a5ca862008-05-27 21:13:40 +0000517 BlockDriverState *bs;
Eric Blake9d26dfc2019-01-17 13:36:43 -0600518 uint64_t dev_offset = 0;
Eric Blakedbb38ca2019-08-23 09:37:22 -0500519 bool readonly = false;
thscd831bd2008-07-03 10:23:51 +0000520 bool disconnect = false;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000521 const char *bindto = NULL;
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100522 const char *port = NULL;
523 char *sockpath = NULL;
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100524 char *device = NULL;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800525 QemuOpts *sn_opts = NULL;
526 const char *sn_id_or_name = NULL;
Eric Blake0bc16992020-01-23 10:46:50 -0600527 const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:B:L";
bellard7a5ca862008-05-27 21:13:40 +0000528 struct option lopt[] = {
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000529 { "help", no_argument, NULL, 'h' },
530 { "version", no_argument, NULL, 'V' },
531 { "bind", required_argument, NULL, 'b' },
532 { "port", required_argument, NULL, 'p' },
533 { "socket", required_argument, NULL, 'k' },
534 { "offset", required_argument, NULL, 'o' },
535 { "read-only", no_argument, NULL, 'r' },
Eric Blake636192c2019-01-11 13:47:20 -0600536 { "bitmap", required_argument, NULL, 'B' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000537 { "connect", required_argument, NULL, 'c' },
538 { "disconnect", no_argument, NULL, 'd' },
Eric Blake68b96f12019-01-17 13:36:56 -0600539 { "list", no_argument, NULL, 'L' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000540 { "snapshot", no_argument, NULL, 's' },
541 { "load-snapshot", required_argument, NULL, 'l' },
542 { "nocache", no_argument, NULL, 'n' },
543 { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
544 { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
545 { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
546 { "detect-zeroes", required_argument, NULL,
547 QEMU_NBD_OPT_DETECT_ZEROES },
548 { "shared", required_argument, NULL, 'e' },
549 { "format", required_argument, NULL, 'f' },
550 { "persistent", no_argument, NULL, 't' },
551 { "verbose", no_argument, NULL, 'v' },
552 { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
553 { "export-name", required_argument, NULL, 'x' },
Eric Blakeb1a75b32016-10-14 13:33:03 -0500554 { "description", required_argument, NULL, 'D' },
Daniel P. Berrangeaa6e5462016-02-17 10:10:22 +0000555 { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
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 },
Blue Swirl660f11b2009-07-31 21:16:51 +0000561 { NULL, 0, NULL, 0 }
bellard7a5ca862008-05-27 21:13:40 +0000562 };
563 int ch;
564 int opt_ind = 0;
Naphtali Spreif5edb012010-01-17 16:48:13 +0200565 int flags = BDRV_O_RDWR;
Max Reitz4fbec262015-02-05 13:58:19 -0500566 int ret = 0;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200567 bool seen_cache = false;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100568 bool seen_discard = false;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200569 bool seen_aio = false;
Paolo Bonzinia517e882011-11-04 15:51:21 +0100570 pthread_t client_thread;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000571 const char *fmt = NULL;
Max Reitz34b5d2c2013-09-05 14:45:29 +0200572 Error *local_err = NULL;
Peter Lievenb3838a42014-08-13 19:20:18 +0200573 BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
Max Reitz4fbec262015-02-05 13:58:19 -0500574 QDict *options = NULL;
Eric Blake68b96f12019-01-17 13:36:56 -0600575 const char *export_name = NULL; /* defaults to "" later for server mode */
Eric Blakeb1a75b32016-10-14 13:33:03 -0500576 const char *export_description = NULL;
Eric Blakecbad81c2020-10-27 00:05:49 -0500577 strList *bitmaps = NULL;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000578 const char *tlscredsid = NULL;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000579 bool imageOpts = false;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100580 bool writethrough = true;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300581 char *trace_file = NULL;
Max Reitzffb31e12016-09-28 22:46:42 +0200582 bool fork_process = false;
Eric Blake68b96f12019-01-17 13:36:56 -0600583 bool list = false;
Max Reitzffb31e12016-09-28 22:46:42 +0200584 int old_stderr = -1;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000585 unsigned socket_activation;
Max Reitz637bc5a2019-05-08 23:18:16 +0200586 const char *pid_file_name = NULL;
Kevin Wolf00917172020-09-24 17:26:57 +0200587 BlockExportOptions *export_opts;
bellard7a5ca862008-05-27 21:13:40 +0000588
Eric Blake029a88c2020-09-30 07:11:01 -0500589#ifdef CONFIG_POSIX
Stefan Hajnoczicbc20bf2020-09-29 13:55:15 +0100590 os_setup_early_signal_handling();
591 os_setup_signal_handling();
Max Reitz041e32b2017-06-11 14:37:14 +0200592#endif
593
Daniel P. Berrangé98c5d2e2020-08-25 11:38:48 +0100594 socket_init();
Christophe Fergeauf5852ef2019-01-31 17:46:14 +0100595 error_init(argv[0]);
Daniel P. Berrangefe4db842016-10-04 14:35:52 +0100596 module_call_init(MODULE_INIT_TRACE);
Eduardo Habkoste8f2d272016-05-12 11:10:04 -0300597 qcrypto_init(&error_fatal);
Daniel P. Berrangec2297082016-04-06 12:12:06 +0100598
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000599 module_call_init(MODULE_INIT_QOM);
600 qemu_add_opts(&qemu_object_opts);
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300601 qemu_add_opts(&qemu_trace_opts);
Fam Zheng10f5bff2014-02-10 14:48:51 +0800602 qemu_init_exec_dir(argv[0]);
Paolo Bonzinibb345112011-11-04 15:51:19 +0100603
bellard7a5ca862008-05-27 21:13:40 +0000604 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
605 switch (ch) {
606 case 's':
ths2f726482008-07-03 11:47:46 +0000607 flags |= BDRV_O_SNAPSHOT;
608 break;
609 case 'n':
Paolo Bonzini39a52352012-07-18 14:57:15 +0200610 optarg = (char *) "none";
611 /* fallthrough */
612 case QEMU_NBD_OPT_CACHE:
613 if (seen_cache) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100614 error_report("-n and --cache can only be specified once");
615 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200616 }
617 seen_cache = true;
Kevin Wolf6effd5b2016-03-14 11:43:28 +0100618 if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100619 error_report("Invalid cache mode `%s'", optarg);
620 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200621 }
bellard7a5ca862008-05-27 21:13:40 +0000622 break;
Paolo Bonzini39a52352012-07-18 14:57:15 +0200623 case QEMU_NBD_OPT_AIO:
624 if (seen_aio) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100625 error_report("--aio can only be specified once");
626 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200627 }
628 seen_aio = true;
Aarushi Mehta76802742020-01-20 14:18:56 +0000629 if (bdrv_parse_aio(optarg, &flags) < 0) {
630 error_report("Invalid aio mode '%s'", optarg);
631 exit(EXIT_FAILURE);
Paolo Bonzini39a52352012-07-18 14:57:15 +0200632 }
633 break;
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100634 case QEMU_NBD_OPT_DISCARD:
635 if (seen_discard) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100636 error_report("--discard can only be specified once");
637 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100638 }
639 seen_discard = true;
640 if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100641 error_report("Invalid discard mode `%s'", optarg);
642 exit(EXIT_FAILURE);
Paolo Bonzinided9d2d2013-02-08 14:06:13 +0100643 }
644 break;
Peter Lievenb3838a42014-08-13 19:20:18 +0200645 case QEMU_NBD_OPT_DETECT_ZEROES:
646 detect_zeroes =
Marc-André Lureauf7abe0e2017-08-24 10:46:10 +0200647 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
Peter Lievenb3838a42014-08-13 19:20:18 +0200648 optarg,
Peter Lievenb3838a42014-08-13 19:20:18 +0200649 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
650 &local_err);
651 if (local_err) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +0100652 error_reportf_err(local_err,
653 "Failed to parse detect_zeroes mode: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100654 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200655 }
656 if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
657 !(flags & BDRV_O_UNMAP)) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100658 error_report("setting detect-zeroes to unmap is not allowed "
659 "without setting discard operation to unmap");
660 exit(EXIT_FAILURE);
Peter Lievenb3838a42014-08-13 19:20:18 +0200661 }
662 break;
bellard7a5ca862008-05-27 21:13:40 +0000663 case 'b':
664 bindto = optarg;
665 break;
666 case 'p':
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100667 port = optarg;
bellard7a5ca862008-05-27 21:13:40 +0000668 break;
669 case 'o':
Eric Blake43b51012019-01-17 13:36:44 -0600670 if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
671 error_report("Invalid offset '%s'", optarg);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100672 exit(EXIT_FAILURE);
bellard7a5ca862008-05-27 21:13:40 +0000673 }
bellard7a5ca862008-05-27 21:13:40 +0000674 break;
Wenchao Xia8c116b02013-12-04 17:10:55 +0800675 case 'l':
676 if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
Markus Armbruster70b94332015-02-13 12:50:26 +0100677 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
678 optarg, false);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800679 if (!sn_opts) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100680 error_report("Failed in parsing snapshot param `%s'",
681 optarg);
682 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +0800683 }
684 } else {
685 sn_id_or_name = optarg;
686 }
687 /* fall through */
bellard7a5ca862008-05-27 21:13:40 +0000688 case 'r':
Eric Blakedbb38ca2019-08-23 09:37:22 -0500689 readonly = true;
Naphtali Sprei07108b22010-03-14 15:19:57 +0200690 flags &= ~BDRV_O_RDWR;
bellard7a5ca862008-05-27 21:13:40 +0000691 break;
Eric Blake636192c2019-01-11 13:47:20 -0600692 case 'B':
Eric Blakecbad81c2020-10-27 00:05:49 -0500693 QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
Eric Blake636192c2019-01-11 13:47:20 -0600694 break;
thscd831bd2008-07-03 10:23:51 +0000695 case 'k':
Paolo Bonzinib32f6c22011-11-04 15:51:20 +0100696 sockpath = optarg;
Peter Lieven713cc672014-08-13 19:20:19 +0200697 if (sockpath[0] != '/') {
Markus Armbruster9af9e0f2015-12-18 16:35:19 +0100698 error_report("socket path must be absolute");
Markus Armbruster85b01e02015-12-18 16:35:04 +0100699 exit(EXIT_FAILURE);
Peter Lieven713cc672014-08-13 19:20:19 +0200700 }
thscd831bd2008-07-03 10:23:51 +0000701 break;
702 case 'd':
703 disconnect = true;
704 break;
705 case 'c':
706 device = optarg;
707 break;
ths3b05a8e2008-07-03 12:45:02 +0000708 case 'e':
Eric Blake43b51012019-01-17 13:36:44 -0600709 if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
710 shared < 1) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100711 error_report("Invalid shared device number '%s'", optarg);
712 exit(EXIT_FAILURE);
ths3b05a8e2008-07-03 12:45:02 +0000713 }
ths3b05a8e2008-07-03 12:45:02 +0000714 break;
Daniel P. Berrangee6b63672013-03-19 11:20:20 +0000715 case 'f':
716 fmt = optarg;
717 break;
Peter Lieven713cc672014-08-13 19:20:19 +0200718 case 't':
719 persistent = 1;
720 break;
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000721 case 'x':
722 export_name = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600723 if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
724 error_report("export name '%s' too long", export_name);
725 exit(EXIT_FAILURE);
726 }
Daniel P. Berrange3d4b2f92016-02-10 18:41:08 +0000727 break;
Eric Blakeb1a75b32016-10-14 13:33:03 -0500728 case 'D':
729 export_description = optarg;
Eric Blake93676c82019-11-13 20:46:34 -0600730 if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
731 error_report("export description '%s' too long",
732 export_description);
733 exit(EXIT_FAILURE);
734 }
Eric Blakeb1a75b32016-10-14 13:33:03 -0500735 break;
bellard7a5ca862008-05-27 21:13:40 +0000736 case 'v':
737 verbose = 1;
738 break;
739 case 'V':
740 version(argv[0]);
741 exit(0);
742 break;
743 case 'h':
744 usage(argv[0]);
745 exit(0);
746 break;
747 case '?':
Markus Armbruster85b01e02015-12-18 16:35:04 +0100748 error_report("Try `%s --help' for more information.", argv[0]);
749 exit(EXIT_FAILURE);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000750 case QEMU_NBD_OPT_OBJECT: {
751 QemuOpts *opts;
752 opts = qemu_opts_parse_noisily(&qemu_object_opts,
753 optarg, true);
754 if (!opts) {
755 exit(EXIT_FAILURE);
756 }
757 } break;
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000758 case QEMU_NBD_OPT_TLSCREDS:
759 tlscredsid = optarg;
760 break;
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000761 case QEMU_NBD_OPT_IMAGE_OPTS:
762 imageOpts = true;
763 break;
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300764 case 'T':
765 g_free(trace_file);
766 trace_file = trace_opt_parse(optarg);
767 break;
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000768 case QEMU_NBD_OPT_TLSAUTHZ:
769 tlsauthz = optarg;
770 break;
Max Reitzffb31e12016-09-28 22:46:42 +0200771 case QEMU_NBD_OPT_FORK:
772 fork_process = true;
773 break;
Eric Blake68b96f12019-01-17 13:36:56 -0600774 case 'L':
775 list = true;
776 break;
Max Reitz637bc5a2019-05-08 23:18:16 +0200777 case QEMU_NBD_OPT_PID_FILE:
778 pid_file_name = optarg;
779 break;
bellard7a5ca862008-05-27 21:13:40 +0000780 }
781 }
782
Eric Blake68b96f12019-01-17 13:36:56 -0600783 if (list) {
784 if (argc != optind) {
785 error_report("List mode is incompatible with a file name");
786 exit(EXIT_FAILURE);
787 }
Eric Blake0bc16992020-01-23 10:46:50 -0600788 if (export_name || export_description || dev_offset ||
Eric Blakecbad81c2020-10-27 00:05:49 -0500789 device || disconnect || fmt || sn_id_or_name || bitmaps ||
Eric Blake68b96f12019-01-17 13:36:56 -0600790 seen_aio || seen_discard || seen_cache) {
791 error_report("List mode is incompatible with per-device settings");
792 exit(EXIT_FAILURE);
793 }
794 if (fork_process) {
795 error_report("List mode is incompatible with forking");
796 exit(EXIT_FAILURE);
797 }
798 } else if ((argc - optind) != 1) {
Markus Armbruster433672b2015-12-18 16:35:24 +0100799 error_report("Invalid number of arguments");
800 error_printf("Try `%s --help' for more information.\n", argv[0]);
Markus Armbruster85b01e02015-12-18 16:35:04 +0100801 exit(EXIT_FAILURE);
Eric Blake68b96f12019-01-17 13:36:56 -0600802 } else if (!export_name) {
803 export_name = "";
bellard7a5ca862008-05-27 21:13:40 +0000804 }
805
Markus Armbruster7e1e0c12018-10-17 10:26:43 +0200806 qemu_opts_foreach(&qemu_object_opts,
807 user_creatable_add_opts_foreach,
Kevin Wolf495bf892019-10-11 21:49:17 +0200808 qemu_nbd_object_print_help, &error_fatal);
Daniel P. Berrange0ab3b332016-02-10 18:41:00 +0000809
Denis V. Lunev39ca4632016-06-17 17:44:12 +0300810 if (!trace_init_backends()) {
811 exit(1);
812 }
813 trace_init_file(trace_file);
814 qemu_set_log(LOG_TRACE);
815
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000816 socket_activation = check_socket_activation();
817 if (socket_activation == 0) {
818 setup_address_and_port(&bindto, &port);
819 } else {
820 /* Using socket activation - check user didn't use -p etc. */
821 const char *err_msg = socket_activation_validate_opts(device, sockpath,
Eric Blake68b96f12019-01-17 13:36:56 -0600822 bindto, port,
823 list);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000824 if (err_msg != NULL) {
825 error_report("%s", err_msg);
826 exit(EXIT_FAILURE);
827 }
Paolo Bonzini53fabd42017-03-16 16:29:45 +0100828
829 /* qemu-nbd can only listen on a single socket. */
830 if (socket_activation > 1) {
831 error_report("qemu-nbd does not support socket activation with %s > 1",
832 "LISTEN_FDS");
833 exit(EXIT_FAILURE);
834 }
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000835 }
836
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000837 if (tlscredsid) {
838 if (sockpath) {
839 error_report("TLS is only supported with IPv4/IPv6");
840 exit(EXIT_FAILURE);
841 }
842 if (device) {
843 error_report("TLS is not supported with a host device");
844 exit(EXIT_FAILURE);
845 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000846 if (tlsauthz && list) {
847 error_report("TLS authorization is incompatible with export list");
848 exit(EXIT_FAILURE);
849 }
Eric Blake68b96f12019-01-17 13:36:56 -0600850 tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000851 if (local_err) {
Markus Armbruster5217f182020-05-05 12:19:03 +0200852 error_reportf_err(local_err, "Failed to get TLS creds: ");
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000853 exit(EXIT_FAILURE);
854 }
Daniel P. Berrangeb25e12d2019-02-27 16:20:33 +0000855 } else {
856 if (tlsauthz) {
857 error_report("--tls-authz is not permitted without --tls-creds");
858 exit(EXIT_FAILURE);
859 }
Daniel P. Berrange145614a2016-02-10 18:41:13 +0000860 }
861
Eric Blake68b96f12019-01-17 13:36:56 -0600862 if (list) {
863 saddr = nbd_build_socket_address(sockpath, bindto, port);
864 return qemu_nbd_client_list(saddr, tlscreds, bindto);
865 }
866
Eric Blake3c1fa352018-12-15 07:53:08 -0600867#if !HAVE_NBD_DEVICE
868 if (disconnect || device) {
869 error_report("Kernel /dev/nbdN support not available");
870 exit(EXIT_FAILURE);
871 }
872#else /* HAVE_NBD_DEVICE */
thscd831bd2008-07-03 10:23:51 +0000873 if (disconnect) {
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000874 int nbdfd = open(argv[optind], O_RDWR);
875 if (nbdfd < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100876 error_report("Cannot open %s: %s", argv[optind],
877 strerror(errno));
878 exit(EXIT_FAILURE);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100879 }
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000880 nbd_disconnect(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000881
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +0000882 close(nbdfd);
thscd831bd2008-07-03 10:23:51 +0000883
884 printf("%s disconnected\n", argv[optind]);
885
Peter Lieven713cc672014-08-13 19:20:19 +0200886 return 0;
thscd831bd2008-07-03 10:23:51 +0000887 }
Eric Blake3c1fa352018-12-15 07:53:08 -0600888#endif
thscd831bd2008-07-03 10:23:51 +0000889
Max Reitzffb31e12016-09-28 22:46:42 +0200890 if ((device && !verbose) || fork_process) {
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100891#ifndef WIN32
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100892 int stderr_fd[2];
893 pid_t pid;
894 int ret;
895
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100896 if (qemu_pipe(stderr_fd) < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100897 error_report("Error setting up communication pipe: %s",
898 strerror(errno));
899 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100900 }
901
902 /* Now daemonize, but keep a communication channel open to
903 * print errors and exit with the proper status code.
904 */
905 pid = fork();
Max Reitz70d47392015-02-25 13:08:22 -0500906 if (pid < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100907 error_report("Failed to fork: %s", strerror(errno));
908 exit(EXIT_FAILURE);
Max Reitz70d47392015-02-25 13:08:22 -0500909 } else if (pid == 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100910 close(stderr_fd[0]);
Max Reitze6df58a2019-05-08 23:18:18 +0200911
Raphael Pour0eaf4532020-05-15 08:36:07 +0200912 /* Remember parent's stderr if we will be restoring it. */
913 if (fork_process) {
914 old_stderr = dup(STDERR_FILENO);
915 }
916
Michael Tokarev9faf31b2012-01-16 18:37:44 +0400917 ret = qemu_daemon(1, 0);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100918
919 /* Temporarily redirect stderr to the parent's pipe... */
920 dup2(stderr_fd[1], STDERR_FILENO);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100921 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100922 error_report("Failed to daemonize: %s", strerror(errno));
923 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100924 }
925
926 /* ... close the descriptor we inherited and go on. */
927 close(stderr_fd[1]);
928 } else {
929 bool errors = false;
930 char *buf;
931
932 /* In the parent. Print error messages from the child until
933 * it closes the pipe.
934 */
935 close(stderr_fd[1]);
936 buf = g_malloc(1024);
937 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
938 errors = true;
939 ret = qemu_write_full(STDERR_FILENO, buf, ret);
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100940 if (ret < 0) {
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100941 exit(EXIT_FAILURE);
942 }
943 }
Paolo Bonzinifc19f8a2012-03-07 11:05:34 +0100944 if (ret < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +0100945 error_report("Cannot read from daemon: %s",
946 strerror(errno));
947 exit(EXIT_FAILURE);
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100948 }
949
950 /* Usually the daemon should not print any message.
951 * Exit with zero status in that case.
952 */
953 exit(errors);
954 }
Daniel P. Berrangéeb705982020-08-25 11:38:50 +0100955#else /* WIN32 */
956 error_report("Unable to fork into background on Windows hosts");
957 exit(EXIT_FAILURE);
958#endif /* WIN32 */
Paolo Bonzinic1f8fdc2011-11-04 15:51:22 +0100959 }
960
Paolo Bonzinia6ac2312011-12-06 09:07:00 +0100961 if (device != NULL && sockpath == NULL) {
962 sockpath = g_malloc(128);
963 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
thscd831bd2008-07-03 10:23:51 +0000964 }
965
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000966 server = qio_net_listener_new();
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000967 if (socket_activation == 0) {
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000968 saddr = nbd_build_socket_address(sockpath, bindto, port);
Juan Quintelafc8135c2019-08-19 18:08:21 +0200969 if (qio_net_listener_open_sync(server, saddr, 1, &local_err) < 0) {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000970 object_unref(OBJECT(server));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000971 error_report_err(local_err);
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000972 exit(EXIT_FAILURE);
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000973 }
974 } else {
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000975 size_t i;
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000976 /* See comment in check_socket_activation above. */
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000977 for (i = 0; i < socket_activation; i++) {
978 QIOChannelSocket *sioc;
979 sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
980 &local_err);
981 if (sioc == NULL) {
982 object_unref(OBJECT(server));
Markus Armbruster5217f182020-05-05 12:19:03 +0200983 error_reportf_err(local_err,
984 "Failed to use socket activation: ");
Daniel P. Berrangee4849c12017-12-18 10:16:43 +0000985 exit(EXIT_FAILURE);
986 }
987 qio_net_listener_add(server, sioc);
988 object_unref(OBJECT(sioc));
Richard W.M. Jonesa721f532017-02-04 10:03:17 +0000989 }
990 }
Daniel P. Berrange48bec072015-09-16 14:52:23 +0100991
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300992 if (qemu_init_main_loop(&local_err)) {
Markus Armbruster565f65d2015-02-12 13:55:05 +0100993 error_report_err(local_err);
Chrysostomos Nanakos2f78e492014-09-18 14:30:49 +0300994 exit(EXIT_FAILURE);
995 }
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100996 bdrv_init();
Kevin Wolfb3b52992018-05-16 13:46:37 +0200997 atexit(qemu_nbd_shutdown);
Paolo Bonzini802ddc32011-11-04 15:51:24 +0100998
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +0000999 srcpath = argv[optind];
1000 if (imageOpts) {
1001 QemuOpts *opts;
1002 if (fmt) {
1003 error_report("--image-opts and -f are mutually exclusive");
1004 exit(EXIT_FAILURE);
1005 }
1006 opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
1007 if (!opts) {
1008 qemu_opts_reset(&file_opts);
1009 exit(EXIT_FAILURE);
1010 }
1011 options = qemu_opts_to_qdict(opts, NULL);
1012 qemu_opts_reset(&file_opts);
Max Reitzefaa7c42016-03-16 19:54:38 +01001013 blk = blk_new_open(NULL, NULL, options, flags, &local_err);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001014 } else {
1015 if (fmt) {
1016 options = qdict_new();
Eric Blake46f5ac22017-04-27 16:58:17 -05001017 qdict_put_str(options, "driver", fmt);
Daniel P. Berrange77c9aae2016-02-17 10:10:19 +00001018 }
Max Reitzefaa7c42016-03-16 19:54:38 +01001019 blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
Daniel P. Berrangee6b63672013-03-19 11:20:20 +00001020 }
1021
Max Reitz4fbec262015-02-05 13:58:19 -05001022 if (!blk) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001023 error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1024 argv[optind]);
Markus Armbruster85b01e02015-12-18 16:35:04 +01001025 exit(EXIT_FAILURE);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001026 }
Max Reitz4fbec262015-02-05 13:58:19 -05001027 bs = blk_bs(blk);
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001028
Kevin Wolfb57e4de2020-09-24 17:26:52 +02001029 if (dev_offset) {
1030 QDict *raw_opts = qdict_new();
1031 qdict_put_str(raw_opts, "driver", "raw");
1032 qdict_put_str(raw_opts, "file", bs->node_name);
1033 qdict_put_int(raw_opts, "offset", dev_offset);
1034 bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1035 blk_remove_bs(blk);
1036 blk_insert_bs(blk, bs, &error_fatal);
1037 bdrv_unref(bs);
1038 }
1039
Kevin Wolf6effd5b2016-03-14 11:43:28 +01001040 blk_set_enable_write_cache(blk, !writethrough);
1041
Wenchao Xia8c116b02013-12-04 17:10:55 +08001042 if (sn_opts) {
1043 ret = bdrv_snapshot_load_tmp(bs,
1044 qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1045 qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1046 &local_err);
1047 } else if (sn_id_or_name) {
1048 ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1049 &local_err);
1050 }
1051 if (ret < 0) {
Markus Armbrusterc29b77f2015-12-18 16:35:14 +01001052 error_reportf_err(local_err, "Failed to load snapshot: ");
Markus Armbruster85b01e02015-12-18 16:35:04 +01001053 exit(EXIT_FAILURE);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001054 }
1055
Peter Lievenb3838a42014-08-13 19:20:18 +02001056 bs->detect_zeroes = detect_zeroes;
Paolo Bonzini802ddc32011-11-04 15:51:24 +01001057
Kevin Wolf00917172020-09-24 17:26:57 +02001058 nbd_server_is_qemu_nbd(true);
1059
1060 export_opts = g_new(BlockExportOptions, 1);
1061 *export_opts = (BlockExportOptions) {
1062 .type = BLOCK_EXPORT_TYPE_NBD,
Kevin Wolfd53be9c2020-09-24 17:27:04 +02001063 .id = g_strdup("qemu-nbd-export"),
Kevin Wolfb6076af2020-09-24 17:27:01 +02001064 .node_name = g_strdup(bdrv_get_node_name(bs)),
Kevin Wolf00917172020-09-24 17:26:57 +02001065 .has_writethrough = true,
1066 .writethrough = writethrough,
Kevin Wolf30dbc812020-09-24 17:27:11 +02001067 .has_writable = true,
1068 .writable = !readonly,
Kevin Wolf00917172020-09-24 17:26:57 +02001069 .u.nbd = {
Eric Blakecbad81c2020-10-27 00:05:49 -05001070 .has_name = true,
1071 .name = g_strdup(export_name),
1072 .has_description = !!export_description,
1073 .description = g_strdup(export_description),
1074 .has_bitmaps = !!bitmaps,
1075 .bitmaps = bitmaps,
Kevin Wolf00917172020-09-24 17:26:57 +02001076 },
1077 };
1078 blk_exp_add(export_opts, &error_fatal);
1079 qapi_free_BlockExportOptions(export_opts);
ths3b05a8e2008-07-03 12:45:02 +00001080
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001081 if (device) {
Eric Blake3c1fa352018-12-15 07:53:08 -06001082#if HAVE_NBD_DEVICE
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001083 int ret;
1084
Paolo Bonzinia6ac2312011-12-06 09:07:00 +01001085 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001086 if (ret != 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001087 error_report("Failed to create client thread: %s", strerror(ret));
1088 exit(EXIT_FAILURE);
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001089 }
Eric Blake3c1fa352018-12-15 07:53:08 -06001090#endif
Paolo Bonzinif1ef5552011-11-04 15:51:23 +01001091 } else {
1092 /* Shut up GCC warnings. */
1093 memset(&client_thread, 0, sizeof(client_thread));
1094 }
1095
Daniel P. Berranged0d6ff52016-02-10 18:41:02 +00001096 nbd_update_server_watch();
bellard7a5ca862008-05-27 21:13:40 +00001097
Max Reitz637bc5a2019-05-08 23:18:16 +02001098 if (pid_file_name) {
1099 qemu_write_pidfile(pid_file_name, &error_fatal);
1100 }
1101
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001102 /* now when the initialization is (almost) complete, chdir("/")
1103 * to free any busy filesystems */
1104 if (chdir("/") < 0) {
Markus Armbruster85b01e02015-12-18 16:35:04 +01001105 error_report("Could not chdir to root directory: %s",
1106 strerror(errno));
1107 exit(EXIT_FAILURE);
Michael Tokarev9faf31b2012-01-16 18:37:44 +04001108 }
1109
Max Reitzffb31e12016-09-28 22:46:42 +02001110 if (fork_process) {
1111 dup2(old_stderr, STDERR_FILENO);
1112 close(old_stderr);
1113 }
1114
Paolo Bonzini7860a382012-09-18 13:31:56 +02001115 state = RUNNING;
ths3b05a8e2008-07-03 12:45:02 +00001116 do {
Paolo Bonzinia61c6782011-09-12 17:28:11 +02001117 main_loop_wait(false);
Paolo Bonzini7860a382012-09-18 13:31:56 +02001118 if (state == TERMINATE) {
Kevin Wolfbc4ee652020-09-24 17:27:03 +02001119 blk_exp_close_all();
Kevin Wolfd794f7f2020-09-24 17:26:56 +02001120 state = TERMINATED;
Paolo Bonzini7860a382012-09-18 13:31:56 +02001121 }
1122 } while (state != TERMINATED);
ths3b05a8e2008-07-03 12:45:02 +00001123
Markus Armbruster26f54e92014-10-07 13:59:04 +02001124 blk_unref(blk);
Paolo Bonzinib32f6c22011-11-04 15:51:20 +01001125 if (sockpath) {
1126 unlink(sockpath);
1127 }
bellard7a5ca862008-05-27 21:13:40 +00001128
Markus Armbrusterfbf28a42014-09-29 16:07:55 +02001129 qemu_opts_del(sn_opts);
Wenchao Xia8c116b02013-12-04 17:10:55 +08001130
Paolo Bonzinia517e882011-11-04 15:51:21 +01001131 if (device) {
1132 void *ret;
1133 pthread_join(client_thread, &ret);
1134 exit(ret != NULL);
1135 } else {
1136 exit(EXIT_SUCCESS);
1137 }
bellard7a5ca862008-05-27 21:13:40 +00001138}