xref: /openbmc/qemu/qemu-nbd.c (revision 5e437d3c)
1 /*
2  *  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
16  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "qemu/osdep.h"
20 #include <getopt.h>
21 #include <libgen.h>
22 #include <pthread.h>
23 
24 #include "qemu-common.h"
25 #include "qapi/error.h"
26 #include "qemu/cutils.h"
27 #include "sysemu/block-backend.h"
28 #include "sysemu/runstate.h" /* for qemu_system_killed() prototype */
29 #include "block/block_int.h"
30 #include "block/nbd.h"
31 #include "qemu/main-loop.h"
32 #include "qemu/module.h"
33 #include "qemu/option.h"
34 #include "qemu/error-report.h"
35 #include "qemu/config-file.h"
36 #include "qemu/bswap.h"
37 #include "qemu/log.h"
38 #include "qemu/systemd.h"
39 #include "block/snapshot.h"
40 #include "qapi/qmp/qdict.h"
41 #include "qapi/qmp/qstring.h"
42 #include "qom/object_interfaces.h"
43 #include "io/channel-socket.h"
44 #include "io/net-listener.h"
45 #include "crypto/init.h"
46 #include "trace/control.h"
47 #include "qemu-version.h"
48 
49 #ifdef __linux__
50 #define HAVE_NBD_DEVICE 1
51 #else
52 #define HAVE_NBD_DEVICE 0
53 #endif
54 
55 #define SOCKET_PATH                "/var/lock/qemu-nbd-%s"
56 #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
63 #define QEMU_NBD_OPT_FORK          263
64 #define QEMU_NBD_OPT_TLSAUTHZ      264
65 #define QEMU_NBD_OPT_PID_FILE      265
66 
67 #define MBR_SIZE 512
68 
69 static int verbose;
70 static char *srcpath;
71 static SocketAddress *saddr;
72 static int persistent = 0;
73 static enum { RUNNING, TERMINATE, TERMINATED } state;
74 static int shared = 1;
75 static int nb_fds;
76 static QIONetListener *server;
77 static QCryptoTLSCreds *tlscreds;
78 static const char *tlsauthz;
79 
80 static void usage(const char *name)
81 {
82     (printf) (
83 "Usage: %s [OPTIONS] FILE\n"
84 "  or:  %s -L [OPTIONS]\n"
85 "QEMU Disk Network Block Device Utility\n"
86 "\n"
87 "  -h, --help                display this help and exit\n"
88 "  -V, --version             output version information and exit\n"
89 "\n"
90 "Connection properties:\n"
91 "  -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"
98 "  -x, --export-name=NAME    expose export by name (default is empty string)\n"
99 "  -D, --description=TEXT    export a human-readable description\n"
100 "\n"
101 "Exposing part of the image:\n"
102 "  -o, --offset=OFFSET       offset into the image\n"
103 "  -A, --allocation-depth    expose the allocation depth\n"
104 "  -B, --bitmap=NAME         expose a persistent dirty bitmap\n"
105 "\n"
106 "General purpose options:\n"
107 "  -L, --list                list exports available from another NBD server\n"
108 "  --object type,id=ID,...   define an object such as 'secret' for providing\n"
109 "                            passwords and/or encryption keys\n"
110 "  --tls-creds=ID            use id of an earlier --object to provide TLS\n"
111 "  --tls-authz=ID            use id of an earlier --object to provide\n"
112 "                            authorization\n"
113 "  -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
114 "                            specify tracing options\n"
115 "  --fork                    fork off the server process and exit the parent\n"
116 "                            once the server is running\n"
117 "  --pid-file=PATH           store the server's process ID in the given file\n"
118 #if HAVE_NBD_DEVICE
119 "\n"
120 "Kernel NBD client support:\n"
121 "  -c, --connect=DEV         connect FILE to the local NBD device DEV\n"
122 "  -d, --disconnect          disconnect the specified device\n"
123 #endif
124 "\n"
125 "Block device options:\n"
126 "  -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"
131 "  -l, --load-snapshot=SNAPSHOT_PARAM\n"
132 "                            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"
138 "      --aio=MODE            set AIO mode (native, io_uring or threads)\n"
139 "      --discard=MODE        set discard mode (ignore, unmap)\n"
140 "      --detect-zeroes=MODE  set detect-zeroes mode (off, on, unmap)\n"
141 "      --image-opts          treat FILE as a full set of image options\n"
142 "\n"
143 QEMU_HELP_BOTTOM "\n"
144     , name, name, NBD_DEFAULT_PORT, "DEVICE");
145 }
146 
147 static void version(const char *name)
148 {
149     printf(
150 "%s " QEMU_FULL_VERSION "\n"
151 "Written by Anthony Liguori.\n"
152 "\n"
153 QEMU_COPYRIGHT "\n"
154 "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"
156     , name);
157 }
158 
159 #ifdef CONFIG_POSIX
160 /*
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  */
164 void qemu_system_killed(int signum, pid_t pid)
165 {
166     qatomic_cmpxchg(&state, RUNNING, TERMINATE);
167     qemu_notify_event();
168 }
169 #endif /* CONFIG_POSIX */
170 
171 static 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);
184         goto out;
185     }
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) {
201             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",
212                 [NBD_FLAG_SEND_FAST_ZERO_BIT]       = "fast-zero",
213             };
214 
215             printf("  size:  %" PRIu64 "\n", list[i].size);
216             printf("  flags: 0x%x (", list[i].flags);
217             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                 }
221             }
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 
245 #if HAVE_NBD_DEVICE
246 static void *show_parts(void *arg)
247 {
248     char *device = arg;
249     int nbd;
250 
251     /* 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);
257     if (nbd >= 0) {
258         close(nbd);
259     }
260     return NULL;
261 }
262 
263 static void *nbd_client_thread(void *arg)
264 {
265     char *device = arg;
266     NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
267     QIOChannelSocket *sioc;
268     int fd = -1;
269     int ret = EXIT_FAILURE;
270     pthread_t show_parts_thread;
271     Error *local_error = NULL;
272 
273     sioc = qio_channel_socket_new();
274     if (qio_channel_socket_connect_sync(sioc,
275                                         saddr,
276                                         &local_error) < 0) {
277         error_report_err(local_error);
278         goto out;
279     }
280 
281     if (nbd_receive_negotiate(NULL, QIO_CHANNEL(sioc),
282                               NULL, NULL, NULL, &info, &local_error) < 0) {
283         if (local_error) {
284             error_report_err(local_error);
285         }
286         goto out;
287     }
288 
289     fd = open(device, O_RDWR);
290     if (fd < 0) {
291         /* Linux-only, we can use %m in printf.  */
292         error_report("Failed to open %s: %m", device);
293         goto out;
294     }
295 
296     if (nbd_init(fd, sioc, &info, &local_error) < 0) {
297         error_report_err(local_error);
298         goto out;
299     }
300 
301     /* update partition table */
302     pthread_create(&show_parts_thread, NULL, show_parts, device);
303 
304     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     }
311 
312     if (nbd_client(fd) < 0) {
313         goto out;
314     }
315 
316     ret = EXIT_SUCCESS;
317 
318  out:
319     if (fd >= 0) {
320         close(fd);
321     }
322     object_unref(OBJECT(sioc));
323     g_free(info.name);
324     kill(getpid(), SIGTERM);
325     return (void *) (intptr_t) ret;
326 }
327 #endif /* HAVE_NBD_DEVICE */
328 
329 static int nbd_can_accept(void)
330 {
331     return state == RUNNING && (shared == 0 || nb_fds < shared);
332 }
333 
334 static void nbd_update_server_watch(void);
335 
336 static void nbd_client_closed(NBDClient *client, bool negotiated)
337 {
338     nb_fds--;
339     if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
340         state = TERMINATE;
341     }
342     nbd_update_server_watch();
343     nbd_client_put(client);
344 }
345 
346 static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
347                        gpointer opaque)
348 {
349     if (state >= TERMINATE) {
350         return;
351     }
352 
353     nb_fds++;
354     nbd_update_server_watch();
355     nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
356 }
357 
358 static void nbd_update_server_watch(void)
359 {
360     if (nbd_can_accept()) {
361         qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
362     } else {
363         qio_net_listener_set_client_func(server, NULL, NULL, NULL);
364     }
365 }
366 
367 
368 static SocketAddress *nbd_build_socket_address(const char *sockpath,
369                                                const char *bindto,
370                                                const char *port)
371 {
372     SocketAddress *saddr;
373 
374     saddr = g_new0(SocketAddress, 1);
375     if (sockpath) {
376         saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
377         saddr->u.q_unix.path = g_strdup(sockpath);
378     } else {
379         InetSocketAddress *inet;
380         saddr->type = SOCKET_ADDRESS_TYPE_INET;
381         inet = &saddr->u.inet;
382         inet->host = g_strdup(bindto);
383         if (port) {
384             inet->port = g_strdup(port);
385         } else  {
386             inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
387         }
388     }
389 
390     return saddr;
391 }
392 
393 
394 static 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 
404 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
405                                           Error **errp)
406 {
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 
425     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         }
437     }
438     object_ref(obj);
439     return creds;
440 }
441 
442 static 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 
453 /*
454  * Check socket parameters compatibility when socket activation is used.
455  */
456 static const char *socket_activation_validate_opts(const char *device,
457                                                    const char *sockpath,
458                                                    const char *address,
459                                                    const char *port,
460                                                    bool list)
461 {
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 
478     if (list) {
479         return "List mode is incompatible with socket activation";
480     }
481 
482     return NULL;
483 }
484 
485 static void qemu_nbd_shutdown(void)
486 {
487     job_cancel_sync_all();
488     blk_exp_close_all();
489     bdrv_close_all();
490 }
491 
492 int main(int argc, char **argv)
493 {
494     BlockBackend *blk;
495     BlockDriverState *bs;
496     uint64_t dev_offset = 0;
497     bool readonly = false;
498     bool disconnect = false;
499     const char *bindto = NULL;
500     const char *port = NULL;
501     char *sockpath = NULL;
502     char *device = NULL;
503     QemuOpts *sn_opts = NULL;
504     const char *sn_id_or_name = NULL;
505     const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
506     struct option lopt[] = {
507         { "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' },
514         { "allocation-depth", no_argument, NULL, 'A' },
515         { "bitmap", required_argument, NULL, 'B' },
516         { "connect", required_argument, NULL, 'c' },
517         { "disconnect", no_argument, NULL, 'd' },
518         { "list", no_argument, NULL, 'L' },
519         { "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' },
533         { "description", required_argument, NULL, 'D' },
534         { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
535         { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
536         { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
537         { "trace", required_argument, NULL, 'T' },
538         { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
539         { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
540         { NULL, 0, NULL, 0 }
541     };
542     int ch;
543     int opt_ind = 0;
544     int flags = BDRV_O_RDWR;
545     int ret = 0;
546     bool seen_cache = false;
547     bool seen_discard = false;
548     bool seen_aio = false;
549     pthread_t client_thread;
550     const char *fmt = NULL;
551     Error *local_err = NULL;
552     BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
553     QDict *options = NULL;
554     const char *export_name = NULL; /* defaults to "" later for server mode */
555     const char *export_description = NULL;
556     strList *bitmaps = NULL;
557     bool alloc_depth = false;
558     const char *tlscredsid = NULL;
559     bool imageOpts = false;
560     bool writethrough = true;
561     bool fork_process = false;
562     bool list = false;
563     int old_stderr = -1;
564     unsigned socket_activation;
565     const char *pid_file_name = NULL;
566     BlockExportOptions *export_opts;
567 
568 #ifdef CONFIG_POSIX
569     os_setup_early_signal_handling();
570     os_setup_signal_handling();
571 #endif
572 
573     socket_init();
574     error_init(argv[0]);
575     module_call_init(MODULE_INIT_TRACE);
576     qcrypto_init(&error_fatal);
577 
578     module_call_init(MODULE_INIT_QOM);
579     qemu_add_opts(&qemu_trace_opts);
580     qemu_init_exec_dir(argv[0]);
581 
582     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
583         switch (ch) {
584         case 's':
585             flags |= BDRV_O_SNAPSHOT;
586             break;
587         case 'n':
588             optarg = (char *) "none";
589             /* fallthrough */
590         case QEMU_NBD_OPT_CACHE:
591             if (seen_cache) {
592                 error_report("-n and --cache can only be specified once");
593                 exit(EXIT_FAILURE);
594             }
595             seen_cache = true;
596             if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
597                 error_report("Invalid cache mode `%s'", optarg);
598                 exit(EXIT_FAILURE);
599             }
600             break;
601         case QEMU_NBD_OPT_AIO:
602             if (seen_aio) {
603                 error_report("--aio can only be specified once");
604                 exit(EXIT_FAILURE);
605             }
606             seen_aio = true;
607             if (bdrv_parse_aio(optarg, &flags) < 0) {
608                 error_report("Invalid aio mode '%s'", optarg);
609                 exit(EXIT_FAILURE);
610             }
611             break;
612         case QEMU_NBD_OPT_DISCARD:
613             if (seen_discard) {
614                 error_report("--discard can only be specified once");
615                 exit(EXIT_FAILURE);
616             }
617             seen_discard = true;
618             if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
619                 error_report("Invalid discard mode `%s'", optarg);
620                 exit(EXIT_FAILURE);
621             }
622             break;
623         case QEMU_NBD_OPT_DETECT_ZEROES:
624             detect_zeroes =
625                 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
626                                 optarg,
627                                 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
628                                 &local_err);
629             if (local_err) {
630                 error_reportf_err(local_err,
631                                   "Failed to parse detect_zeroes mode: ");
632                 exit(EXIT_FAILURE);
633             }
634             if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
635                 !(flags & BDRV_O_UNMAP)) {
636                 error_report("setting detect-zeroes to unmap is not allowed "
637                              "without setting discard operation to unmap");
638                 exit(EXIT_FAILURE);
639             }
640             break;
641         case 'b':
642             bindto = optarg;
643             break;
644         case 'p':
645             port = optarg;
646             break;
647         case 'o':
648             if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
649                 error_report("Invalid offset '%s'", optarg);
650                 exit(EXIT_FAILURE);
651             }
652             break;
653         case 'l':
654             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
655                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
656                                                   optarg, false);
657                 if (!sn_opts) {
658                     error_report("Failed in parsing snapshot param `%s'",
659                                  optarg);
660                     exit(EXIT_FAILURE);
661                 }
662             } else {
663                 sn_id_or_name = optarg;
664             }
665             /* fall through */
666         case 'r':
667             readonly = true;
668             flags &= ~BDRV_O_RDWR;
669             break;
670         case 'A':
671             alloc_depth = true;
672             break;
673         case 'B':
674             QAPI_LIST_PREPEND(bitmaps, g_strdup(optarg));
675             break;
676         case 'k':
677             sockpath = optarg;
678             if (sockpath[0] != '/') {
679                 error_report("socket path must be absolute");
680                 exit(EXIT_FAILURE);
681             }
682             break;
683         case 'd':
684             disconnect = true;
685             break;
686         case 'c':
687             device = optarg;
688             break;
689         case 'e':
690             if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
691                 shared < 0) {
692                 error_report("Invalid shared device number '%s'", optarg);
693                 exit(EXIT_FAILURE);
694             }
695             break;
696         case 'f':
697             fmt = optarg;
698             break;
699         case 't':
700             persistent = 1;
701             break;
702         case 'x':
703             export_name = optarg;
704             if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
705                 error_report("export name '%s' too long", export_name);
706                 exit(EXIT_FAILURE);
707             }
708             break;
709         case 'D':
710             export_description = optarg;
711             if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
712                 error_report("export description '%s' too long",
713                              export_description);
714                 exit(EXIT_FAILURE);
715             }
716             break;
717         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 '?':
729             error_report("Try `%s --help' for more information.", argv[0]);
730             exit(EXIT_FAILURE);
731         case QEMU_NBD_OPT_OBJECT:
732             user_creatable_process_cmdline(optarg);
733             break;
734         case QEMU_NBD_OPT_TLSCREDS:
735             tlscredsid = optarg;
736             break;
737         case QEMU_NBD_OPT_IMAGE_OPTS:
738             imageOpts = true;
739             break;
740         case 'T':
741             trace_opt_parse(optarg);
742             break;
743         case QEMU_NBD_OPT_TLSAUTHZ:
744             tlsauthz = optarg;
745             break;
746         case QEMU_NBD_OPT_FORK:
747             fork_process = true;
748             break;
749         case 'L':
750             list = true;
751             break;
752         case QEMU_NBD_OPT_PID_FILE:
753             pid_file_name = optarg;
754             break;
755         }
756     }
757 
758     if (list) {
759         if (argc != optind) {
760             error_report("List mode is incompatible with a file name");
761             exit(EXIT_FAILURE);
762         }
763         if (export_name || export_description || dev_offset ||
764             device || disconnect || fmt || sn_id_or_name || bitmaps ||
765             alloc_depth || seen_aio || seen_discard || seen_cache) {
766             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) {
774         error_report("Invalid number of arguments");
775         error_printf("Try `%s --help' for more information.\n", argv[0]);
776         exit(EXIT_FAILURE);
777     } else if (!export_name) {
778         export_name = "";
779     }
780 
781     if (!trace_init_backends()) {
782         exit(1);
783     }
784     trace_init_file();
785     qemu_set_log(LOG_TRACE);
786 
787     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,
793                                                               bindto, port,
794                                                               list);
795         if (err_msg != NULL) {
796             error_report("%s", err_msg);
797             exit(EXIT_FAILURE);
798         }
799 
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         }
806     }
807 
808     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         }
817         if (tlsauthz && list) {
818             error_report("TLS authorization is incompatible with export list");
819             exit(EXIT_FAILURE);
820         }
821         tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
822         if (local_err) {
823             error_reportf_err(local_err, "Failed to get TLS creds: ");
824             exit(EXIT_FAILURE);
825         }
826     } else {
827         if (tlsauthz) {
828             error_report("--tls-authz is not permitted without --tls-creds");
829             exit(EXIT_FAILURE);
830         }
831     }
832 
833     if (list) {
834         saddr = nbd_build_socket_address(sockpath, bindto, port);
835         return qemu_nbd_client_list(saddr, tlscreds, bindto);
836     }
837 
838 #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 */
844     if (disconnect) {
845         int nbdfd = open(argv[optind], O_RDWR);
846         if (nbdfd < 0) {
847             error_report("Cannot open %s: %s", argv[optind],
848                          strerror(errno));
849             exit(EXIT_FAILURE);
850         }
851         nbd_disconnect(nbdfd);
852 
853         close(nbdfd);
854 
855         printf("%s disconnected\n", argv[optind]);
856 
857         return 0;
858     }
859 #endif
860 
861     if ((device && !verbose) || fork_process) {
862 #ifndef WIN32
863         int stderr_fd[2];
864         pid_t pid;
865         int ret;
866 
867         if (qemu_pipe(stderr_fd) < 0) {
868             error_report("Error setting up communication pipe: %s",
869                          strerror(errno));
870             exit(EXIT_FAILURE);
871         }
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();
877         if (pid < 0) {
878             error_report("Failed to fork: %s", strerror(errno));
879             exit(EXIT_FAILURE);
880         } else if (pid == 0) {
881             close(stderr_fd[0]);
882 
883             /* Remember parent's stderr if we will be restoring it. */
884             if (fork_process) {
885                 old_stderr = dup(STDERR_FILENO);
886             }
887 
888             ret = qemu_daemon(1, 0);
889 
890             /* Temporarily redirect stderr to the parent's pipe...  */
891             dup2(stderr_fd[1], STDERR_FILENO);
892             if (ret < 0) {
893                 error_report("Failed to daemonize: %s", strerror(errno));
894                 exit(EXIT_FAILURE);
895             }
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);
911                 if (ret < 0) {
912                     exit(EXIT_FAILURE);
913                 }
914             }
915             if (ret < 0) {
916                 error_report("Cannot read from daemon: %s",
917                              strerror(errno));
918                 exit(EXIT_FAILURE);
919             }
920 
921             /* Usually the daemon should not print any message.
922              * Exit with zero status in that case.
923              */
924             exit(errors);
925         }
926 #else /* WIN32 */
927         error_report("Unable to fork into background on Windows hosts");
928         exit(EXIT_FAILURE);
929 #endif /* WIN32 */
930     }
931 
932     if (device != NULL && sockpath == NULL) {
933         sockpath = g_malloc(128);
934         snprintf(sockpath, 128, SOCKET_PATH, basename(device));
935     }
936 
937     server = qio_net_listener_new();
938     if (socket_activation == 0) {
939         int backlog;
940 
941         if (persistent || shared == 0) {
942             backlog = SOMAXCONN;
943         } else {
944             backlog = MIN(shared, SOMAXCONN);
945         }
946         saddr = nbd_build_socket_address(sockpath, bindto, port);
947         if (qio_net_listener_open_sync(server, saddr, backlog,
948                                        &local_err) < 0) {
949             object_unref(OBJECT(server));
950             error_report_err(local_err);
951             exit(EXIT_FAILURE);
952         }
953     } else {
954         size_t i;
955         /* See comment in check_socket_activation above. */
956         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));
962                 error_reportf_err(local_err,
963                                   "Failed to use socket activation: ");
964                 exit(EXIT_FAILURE);
965             }
966             qio_net_listener_add(server, sioc);
967             object_unref(OBJECT(sioc));
968         }
969     }
970 
971     if (qemu_init_main_loop(&local_err)) {
972         error_report_err(local_err);
973         exit(EXIT_FAILURE);
974     }
975     bdrv_init();
976     atexit(qemu_nbd_shutdown);
977 
978     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);
992         blk = blk_new_open(NULL, NULL, options, flags, &local_err);
993     } else {
994         if (fmt) {
995             options = qdict_new();
996             qdict_put_str(options, "driver", fmt);
997         }
998         blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
999     }
1000 
1001     if (!blk) {
1002         error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1003                           argv[optind]);
1004         exit(EXIT_FAILURE);
1005     }
1006     bs = blk_bs(blk);
1007 
1008     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 
1019     blk_set_enable_write_cache(blk, !writethrough);
1020 
1021     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) {
1031         error_reportf_err(local_err, "Failed to load snapshot: ");
1032         exit(EXIT_FAILURE);
1033     }
1034 
1035     bs->detect_zeroes = detect_zeroes;
1036 
1037     nbd_server_is_qemu_nbd(true);
1038 
1039     export_opts = g_new(BlockExportOptions, 1);
1040     *export_opts = (BlockExportOptions) {
1041         .type               = BLOCK_EXPORT_TYPE_NBD,
1042         .id                 = g_strdup("qemu-nbd-export"),
1043         .node_name          = g_strdup(bdrv_get_node_name(bs)),
1044         .has_writethrough   = true,
1045         .writethrough       = writethrough,
1046         .has_writable       = true,
1047         .writable           = !readonly,
1048         .u.nbd = {
1049             .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,
1055             .has_allocation_depth = alloc_depth,
1056             .allocation_depth     = alloc_depth,
1057         },
1058     };
1059     blk_exp_add(export_opts, &error_fatal);
1060     qapi_free_BlockExportOptions(export_opts);
1061 
1062     if (device) {
1063 #if HAVE_NBD_DEVICE
1064         int ret;
1065 
1066         ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
1067         if (ret != 0) {
1068             error_report("Failed to create client thread: %s", strerror(ret));
1069             exit(EXIT_FAILURE);
1070         }
1071 #endif
1072     } else {
1073         /* Shut up GCC warnings.  */
1074         memset(&client_thread, 0, sizeof(client_thread));
1075     }
1076 
1077     nbd_update_server_watch();
1078 
1079     if (pid_file_name) {
1080         qemu_write_pidfile(pid_file_name, &error_fatal);
1081     }
1082 
1083     /* now when the initialization is (almost) complete, chdir("/")
1084      * to free any busy filesystems */
1085     if (chdir("/") < 0) {
1086         error_report("Could not chdir to root directory: %s",
1087                      strerror(errno));
1088         exit(EXIT_FAILURE);
1089     }
1090 
1091     if (fork_process) {
1092         dup2(old_stderr, STDERR_FILENO);
1093         close(old_stderr);
1094     }
1095 
1096     state = RUNNING;
1097     do {
1098         main_loop_wait(false);
1099         if (state == TERMINATE) {
1100             blk_exp_close_all();
1101             state = TERMINATED;
1102         }
1103     } while (state != TERMINATED);
1104 
1105     blk_unref(blk);
1106     if (sockpath) {
1107         unlink(sockpath);
1108     }
1109 
1110     qemu_opts_del(sn_opts);
1111 
1112     if (device) {
1113         void *ret;
1114         pthread_join(client_thread, &ret);
1115         exit(ret != NULL);
1116     } else {
1117         exit(EXIT_SUCCESS);
1118     }
1119 }
1120