xref: /openbmc/qemu/qemu-nbd.c (revision d43e967f)
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/help-texts.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 "crypto/tlscreds.h"
47 #include "trace/control.h"
48 #include "qemu-version.h"
49 
50 #ifdef CONFIG_SELINUX
51 #include <selinux/selinux.h>
52 #endif
53 
54 #ifdef __linux__
55 #define HAVE_NBD_DEVICE 1
56 #else
57 #define HAVE_NBD_DEVICE 0
58 #endif
59 
60 #define SOCKET_PATH                "/var/lock/qemu-nbd-%s"
61 #define QEMU_NBD_OPT_CACHE         256
62 #define QEMU_NBD_OPT_AIO           257
63 #define QEMU_NBD_OPT_DISCARD       258
64 #define QEMU_NBD_OPT_DETECT_ZEROES 259
65 #define QEMU_NBD_OPT_OBJECT        260
66 #define QEMU_NBD_OPT_TLSCREDS      261
67 #define QEMU_NBD_OPT_IMAGE_OPTS    262
68 #define QEMU_NBD_OPT_FORK          263
69 #define QEMU_NBD_OPT_TLSAUTHZ      264
70 #define QEMU_NBD_OPT_PID_FILE      265
71 #define QEMU_NBD_OPT_SELINUX_LABEL 266
72 #define QEMU_NBD_OPT_TLSHOSTNAME   267
73 
74 #define MBR_SIZE 512
75 
76 static int persistent = 0;
77 static enum { RUNNING, TERMINATE, TERMINATED } state;
78 static int shared = 1;
79 static int nb_fds;
80 static QIONetListener *server;
81 static QCryptoTLSCreds *tlscreds;
82 static const char *tlsauthz;
83 
84 static void usage(const char *name)
85 {
86     (printf) (
87 "Usage: %s [OPTIONS] FILE\n"
88 "  or:  %s -L [OPTIONS]\n"
89 "QEMU Disk Network Block Device Utility\n"
90 "\n"
91 "  -h, --help                display this help and exit\n"
92 "  -V, --version             output version information and exit\n"
93 "\n"
94 "Connection properties:\n"
95 "  -p, --port=PORT           port to listen on (default `%d')\n"
96 "  -b, --bind=IFACE          interface to bind to (default `0.0.0.0')\n"
97 "  -k, --socket=PATH         path to the unix socket\n"
98 "                            (default '"SOCKET_PATH"')\n"
99 "  -e, --shared=NUM          device can be shared by NUM clients (default '1')\n"
100 "  -t, --persistent          don't exit on the last connection\n"
101 "  -v, --verbose             display extra debugging information\n"
102 "  -x, --export-name=NAME    expose export by name (default is empty string)\n"
103 "  -D, --description=TEXT    export a human-readable description\n"
104 "\n"
105 "Exposing part of the image:\n"
106 "  -o, --offset=OFFSET       offset into the image\n"
107 "  -A, --allocation-depth    expose the allocation depth\n"
108 "  -B, --bitmap=NAME         expose a persistent dirty bitmap\n"
109 "\n"
110 "General purpose options:\n"
111 "  -L, --list                list exports available from another NBD server\n"
112 "  --object type,id=ID,...   define an object such as 'secret' for providing\n"
113 "                            passwords and/or encryption keys\n"
114 "  --tls-creds=ID            use id of an earlier --object to provide TLS\n"
115 "  --tls-authz=ID            use id of an earlier --object to provide\n"
116 "                            authorization\n"
117 "  -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
118 "                            specify tracing options\n"
119 "  --fork                    fork off the server process and exit the parent\n"
120 "                            once the server is running\n"
121 "  --pid-file=PATH           store the server's process ID in the given file\n"
122 #ifdef CONFIG_SELINUX
123 "  --selinux-label=LABEL     set SELinux process label on listening socket\n"
124 #endif
125 #if HAVE_NBD_DEVICE
126 "\n"
127 "Kernel NBD client support:\n"
128 "  -c, --connect=DEV         connect FILE to the local NBD device DEV\n"
129 "  -d, --disconnect          disconnect the specified device\n"
130 #endif
131 "\n"
132 "Block device options:\n"
133 "  -f, --format=FORMAT       set image format (raw, qcow2, ...)\n"
134 "  -r, --read-only           export read-only\n"
135 "  -s, --snapshot            use FILE as an external snapshot, create a temporary\n"
136 "                            file with backing_file=FILE, redirect the write to\n"
137 "                            the temporary one\n"
138 "  -l, --load-snapshot=SNAPSHOT_PARAM\n"
139 "                            load an internal snapshot inside FILE and export it\n"
140 "                            as an read-only device, SNAPSHOT_PARAM format is\n"
141 "                            'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
142 "                            '[ID_OR_NAME]'\n"
143 "  -n, --nocache             disable host cache\n"
144 "      --cache=MODE          set cache mode used to access the disk image, the\n"
145 "                            valid options are: 'none', 'writeback' (default),\n"
146 "                            'writethrough', 'directsync' and 'unsafe'\n"
147 "      --aio=MODE            set AIO mode (native, io_uring or threads)\n"
148 "      --discard=MODE        set discard mode (ignore, unmap)\n"
149 "      --detect-zeroes=MODE  set detect-zeroes mode (off, on, unmap)\n"
150 "      --image-opts          treat FILE as a full set of image options\n"
151 "\n"
152 QEMU_HELP_BOTTOM "\n"
153     , name, name, NBD_DEFAULT_PORT, "DEVICE");
154 }
155 
156 static void version(const char *name)
157 {
158     printf(
159 "%s " QEMU_FULL_VERSION "\n"
160 "Written by Anthony Liguori.\n"
161 "\n"
162 QEMU_COPYRIGHT "\n"
163 "This is free software; see the source for copying conditions.  There is NO\n"
164 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
165     , name);
166 }
167 
168 #ifdef CONFIG_POSIX
169 /*
170  * The client thread uses SIGTERM to interrupt the server.  A signal
171  * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
172  */
173 void qemu_system_killed(int signum, pid_t pid)
174 {
175     qatomic_cmpxchg(&state, RUNNING, TERMINATE);
176     qemu_notify_event();
177 }
178 #endif /* CONFIG_POSIX */
179 
180 static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
181                                 const char *hostname)
182 {
183     int ret = EXIT_FAILURE;
184     int rc;
185     Error *err = NULL;
186     QIOChannelSocket *sioc;
187     NBDExportInfo *list;
188     int i, j;
189 
190     sioc = qio_channel_socket_new();
191     if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
192         error_report_err(err);
193         goto out;
194     }
195     rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
196                                  &err);
197     if (rc < 0) {
198         if (err) {
199             error_report_err(err);
200         }
201         goto out;
202     }
203     printf("exports available: %d\n", rc);
204     for (i = 0; i < rc; i++) {
205         printf(" export: '%s'\n", list[i].name);
206         if (list[i].description && *list[i].description) {
207             printf("  description: %s\n", list[i].description);
208         }
209         if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
210             static const char *const flag_names[] = {
211                 [NBD_FLAG_READ_ONLY_BIT]            = "readonly",
212                 [NBD_FLAG_SEND_FLUSH_BIT]           = "flush",
213                 [NBD_FLAG_SEND_FUA_BIT]             = "fua",
214                 [NBD_FLAG_ROTATIONAL_BIT]           = "rotational",
215                 [NBD_FLAG_SEND_TRIM_BIT]            = "trim",
216                 [NBD_FLAG_SEND_WRITE_ZEROES_BIT]    = "zeroes",
217                 [NBD_FLAG_SEND_DF_BIT]              = "df",
218                 [NBD_FLAG_CAN_MULTI_CONN_BIT]       = "multi",
219                 [NBD_FLAG_SEND_RESIZE_BIT]          = "resize",
220                 [NBD_FLAG_SEND_CACHE_BIT]           = "cache",
221                 [NBD_FLAG_SEND_FAST_ZERO_BIT]       = "fast-zero",
222             };
223 
224             printf("  size:  %" PRIu64 "\n", list[i].size);
225             printf("  flags: 0x%x (", list[i].flags);
226             for (size_t bit = 0; bit < ARRAY_SIZE(flag_names); bit++) {
227                 if (flag_names[bit] && (list[i].flags & (1 << bit))) {
228                     printf(" %s", flag_names[bit]);
229                 }
230             }
231             printf(" )\n");
232         }
233         if (list[i].min_block) {
234             printf("  min block: %u\n", list[i].min_block);
235             printf("  opt block: %u\n", list[i].opt_block);
236             printf("  max block: %u\n", list[i].max_block);
237         }
238         if (list[i].n_contexts) {
239             printf("  available meta contexts: %d\n", list[i].n_contexts);
240             for (j = 0; j < list[i].n_contexts; j++) {
241                 printf("   %s\n", list[i].contexts[j]);
242             }
243         }
244     }
245     nbd_free_export_list(list, rc);
246 
247     ret = EXIT_SUCCESS;
248  out:
249     object_unref(OBJECT(sioc));
250     return ret;
251 }
252 
253 
254 struct NbdClientOpts {
255     char *device;
256     char *srcpath;
257     SocketAddress *saddr;
258     int old_stderr;
259     bool fork_process;
260     bool verbose;
261 };
262 
263 static void nbd_client_release_pipe(int old_stderr)
264 {
265     /* Close stderr so that the qemu-nbd process exits.  */
266     if (dup2(old_stderr, STDERR_FILENO) < 0) {
267         error_report("Could not release pipe to parent: %s",
268                      strerror(errno));
269         exit(EXIT_FAILURE);
270     }
271     if (old_stderr != STDOUT_FILENO && close(old_stderr) < 0) {
272         error_report("Could not release qemu-nbd: %s", strerror(errno));
273         exit(EXIT_FAILURE);
274     }
275 }
276 
277 #if HAVE_NBD_DEVICE
278 static void *show_parts(void *arg)
279 {
280     char *device = arg;
281     int nbd;
282 
283     /* linux just needs an open() to trigger
284      * the partition table update
285      * but remember to load the module with max_part != 0 :
286      *     modprobe nbd max_part=63
287      */
288     nbd = open(device, O_RDWR);
289     if (nbd >= 0) {
290         close(nbd);
291     }
292     return NULL;
293 }
294 
295 static void *nbd_client_thread(void *arg)
296 {
297     struct NbdClientOpts *opts = arg;
298     /* TODO: Revisit this if nbd.ko ever gains support for structured reply */
299     NBDExportInfo info = { .request_sizes = false, .name = g_strdup(""),
300                            .mode = NBD_MODE_SIMPLE };
301     QIOChannelSocket *sioc;
302     int fd = -1;
303     int ret = EXIT_FAILURE;
304     pthread_t show_parts_thread;
305     Error *local_error = NULL;
306 
307     sioc = qio_channel_socket_new();
308     if (qio_channel_socket_connect_sync(sioc,
309                                         opts->saddr,
310                                         &local_error) < 0) {
311         error_report_err(local_error);
312         goto out;
313     }
314 
315     if (nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL, NULL, NULL,
316                               &info, &local_error) < 0) {
317         if (local_error) {
318             error_report_err(local_error);
319         }
320         goto out;
321     }
322 
323     fd = open(opts->device, O_RDWR);
324     if (fd < 0) {
325         /* Linux-only, we can use %m in printf.  */
326         error_report("Failed to open %s: %m", opts->device);
327         goto out;
328     }
329 
330     if (nbd_init(fd, sioc, &info, &local_error) < 0) {
331         error_report_err(local_error);
332         goto out;
333     }
334 
335     /* update partition table */
336     pthread_create(&show_parts_thread, NULL, show_parts, opts->device);
337 
338     if (opts->verbose && !opts->fork_process) {
339         fprintf(stderr, "NBD device %s is now connected to %s\n",
340                 opts->device, opts->srcpath);
341     } else {
342         nbd_client_release_pipe(opts->old_stderr);
343     }
344 
345     if (nbd_client(fd) < 0) {
346         goto out;
347     }
348 
349     ret = EXIT_SUCCESS;
350 
351  out:
352     if (fd >= 0) {
353         close(fd);
354     }
355     object_unref(OBJECT(sioc));
356     g_free(info.name);
357     kill(getpid(), SIGTERM);
358     return (void *) (intptr_t) ret;
359 }
360 #endif /* HAVE_NBD_DEVICE */
361 
362 static int nbd_can_accept(void)
363 {
364     return state == RUNNING && (shared == 0 || nb_fds < shared);
365 }
366 
367 static void nbd_update_server_watch(void);
368 
369 static void nbd_client_closed(NBDClient *client, bool negotiated)
370 {
371     nb_fds--;
372     if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
373         state = TERMINATE;
374     }
375     nbd_update_server_watch();
376     nbd_client_put(client);
377 }
378 
379 static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
380                        gpointer opaque)
381 {
382     if (state >= TERMINATE) {
383         return;
384     }
385 
386     nb_fds++;
387     nbd_update_server_watch();
388     nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
389 }
390 
391 static void nbd_update_server_watch(void)
392 {
393     if (nbd_can_accept()) {
394         qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
395     } else {
396         qio_net_listener_set_client_func(server, NULL, NULL, NULL);
397     }
398 }
399 
400 
401 static SocketAddress *nbd_build_socket_address(const char *sockpath,
402                                                const char *bindto,
403                                                const char *port)
404 {
405     SocketAddress *saddr;
406 
407     saddr = g_new0(SocketAddress, 1);
408     if (sockpath) {
409         saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
410         saddr->u.q_unix.path = g_strdup(sockpath);
411     } else {
412         InetSocketAddress *inet;
413         saddr->type = SOCKET_ADDRESS_TYPE_INET;
414         inet = &saddr->u.inet;
415         inet->host = g_strdup(bindto);
416         if (port) {
417             inet->port = g_strdup(port);
418         } else  {
419             inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
420         }
421     }
422 
423     return saddr;
424 }
425 
426 
427 static QemuOptsList file_opts = {
428     .name = "file",
429     .implied_opt_name = "file",
430     .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
431     .desc = {
432         /* no elements => accept any params */
433         { /* end of list */ }
434     },
435 };
436 
437 static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
438                                           Error **errp)
439 {
440     Object *obj;
441     QCryptoTLSCreds *creds;
442 
443     obj = object_resolve_path_component(
444         object_get_objects_root(), id);
445     if (!obj) {
446         error_setg(errp, "No TLS credentials with id '%s'",
447                    id);
448         return NULL;
449     }
450     creds = (QCryptoTLSCreds *)
451         object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
452     if (!creds) {
453         error_setg(errp, "Object with id '%s' is not TLS credentials",
454                    id);
455         return NULL;
456     }
457 
458     if (!qcrypto_tls_creds_check_endpoint(creds,
459                                           list
460                                           ? QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT
461                                           : QCRYPTO_TLS_CREDS_ENDPOINT_SERVER,
462                                           errp)) {
463         return NULL;
464     }
465     object_ref(obj);
466     return creds;
467 }
468 
469 static void setup_address_and_port(const char **address, const char **port)
470 {
471     if (*address == NULL) {
472         *address = "0.0.0.0";
473     }
474 
475     if (*port == NULL) {
476         *port = stringify(NBD_DEFAULT_PORT);
477     }
478 }
479 
480 /*
481  * Check socket parameters compatibility when socket activation is used.
482  */
483 static const char *socket_activation_validate_opts(const char *device,
484                                                    const char *sockpath,
485                                                    const char *address,
486                                                    const char *port,
487                                                    const char *selinux,
488                                                    bool list)
489 {
490     if (device != NULL) {
491         return "NBD device can't be set when using socket activation";
492     }
493 
494     if (sockpath != NULL) {
495         return "Unix socket can't be set when using socket activation";
496     }
497 
498     if (address != NULL) {
499         return "The interface can't be set when using socket activation";
500     }
501 
502     if (port != NULL) {
503         return "TCP port number can't be set when using socket activation";
504     }
505 
506     if (selinux != NULL) {
507         return "SELinux label can't be set when using socket activation";
508     }
509 
510     if (list) {
511         return "List mode is incompatible with socket activation";
512     }
513 
514     return NULL;
515 }
516 
517 static void qemu_nbd_shutdown(void)
518 {
519     job_cancel_sync_all();
520     blk_exp_close_all();
521     bdrv_close_all();
522 }
523 
524 int main(int argc, char **argv)
525 {
526     BlockBackend *blk;
527     BlockDriverState *bs;
528     uint64_t dev_offset = 0;
529     bool readonly = false;
530     bool disconnect = false;
531     const char *bindto = NULL;
532     const char *port = NULL;
533     char *sockpath = NULL;
534     QemuOpts *sn_opts = NULL;
535     const char *sn_id_or_name = NULL;
536     const char *sopt = "hVb:o:p:rsnc:dvk:e:f:tl:x:T:D:AB:L";
537     struct option lopt[] = {
538         { "help", no_argument, NULL, 'h' },
539         { "version", no_argument, NULL, 'V' },
540         { "bind", required_argument, NULL, 'b' },
541         { "port", required_argument, NULL, 'p' },
542         { "socket", required_argument, NULL, 'k' },
543         { "offset", required_argument, NULL, 'o' },
544         { "read-only", no_argument, NULL, 'r' },
545         { "allocation-depth", no_argument, NULL, 'A' },
546         { "bitmap", required_argument, NULL, 'B' },
547         { "connect", required_argument, NULL, 'c' },
548         { "disconnect", no_argument, NULL, 'd' },
549         { "list", no_argument, NULL, 'L' },
550         { "snapshot", no_argument, NULL, 's' },
551         { "load-snapshot", required_argument, NULL, 'l' },
552         { "nocache", no_argument, NULL, 'n' },
553         { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
554         { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
555         { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
556         { "detect-zeroes", required_argument, NULL,
557           QEMU_NBD_OPT_DETECT_ZEROES },
558         { "shared", required_argument, NULL, 'e' },
559         { "format", required_argument, NULL, 'f' },
560         { "persistent", no_argument, NULL, 't' },
561         { "verbose", no_argument, NULL, 'v' },
562         { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
563         { "export-name", required_argument, NULL, 'x' },
564         { "description", required_argument, NULL, 'D' },
565         { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
566         { "tls-hostname", required_argument, NULL, QEMU_NBD_OPT_TLSHOSTNAME },
567         { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
568         { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
569         { "trace", required_argument, NULL, 'T' },
570         { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
571         { "pid-file", required_argument, NULL, QEMU_NBD_OPT_PID_FILE },
572         { "selinux-label", required_argument, NULL,
573           QEMU_NBD_OPT_SELINUX_LABEL },
574         { NULL, 0, NULL, 0 }
575     };
576     int ch;
577     int opt_ind = 0;
578     int flags = BDRV_O_RDWR;
579     int ret = 0;
580     bool seen_cache = false;
581     bool seen_discard = false;
582     bool seen_aio = false;
583     pthread_t client_thread;
584     const char *fmt = NULL;
585     Error *local_err = NULL;
586     BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
587     QDict *options = NULL;
588     const char *export_name = NULL; /* defaults to "" later for server mode */
589     const char *export_description = NULL;
590     BlockDirtyBitmapOrStrList *bitmaps = NULL;
591     bool alloc_depth = false;
592     const char *tlscredsid = NULL;
593     const char *tlshostname = NULL;
594     bool imageOpts = false;
595     bool writethrough = false; /* Client will flush as needed. */
596     bool list = false;
597     unsigned socket_activation;
598     const char *pid_file_name = NULL;
599     const char *selinux_label = NULL;
600     BlockExportOptions *export_opts;
601     struct NbdClientOpts opts = {
602         .fork_process = false,
603         .verbose = false,
604         .device = NULL,
605         .srcpath = NULL,
606         .saddr = NULL,
607         .old_stderr = STDOUT_FILENO,
608     };
609 
610 #ifdef CONFIG_POSIX
611     os_setup_early_signal_handling();
612     os_setup_signal_handling();
613 #endif
614 
615     socket_init();
616     error_init(argv[0]);
617     module_call_init(MODULE_INIT_TRACE);
618     qcrypto_init(&error_fatal);
619 
620     module_call_init(MODULE_INIT_QOM);
621     qemu_add_opts(&qemu_trace_opts);
622     qemu_init_exec_dir(argv[0]);
623 
624     while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
625         switch (ch) {
626         case 's':
627             flags |= BDRV_O_SNAPSHOT;
628             break;
629         case 'n':
630             optarg = (char *) "none";
631             /* fallthrough */
632         case QEMU_NBD_OPT_CACHE:
633             if (seen_cache) {
634                 error_report("-n and --cache can only be specified once");
635                 exit(EXIT_FAILURE);
636             }
637             seen_cache = true;
638             if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
639                 error_report("Invalid cache mode `%s'", optarg);
640                 exit(EXIT_FAILURE);
641             }
642             break;
643         case QEMU_NBD_OPT_AIO:
644             if (seen_aio) {
645                 error_report("--aio can only be specified once");
646                 exit(EXIT_FAILURE);
647             }
648             seen_aio = true;
649             if (bdrv_parse_aio(optarg, &flags) < 0) {
650                 error_report("Invalid aio mode '%s'", optarg);
651                 exit(EXIT_FAILURE);
652             }
653             break;
654         case QEMU_NBD_OPT_DISCARD:
655             if (seen_discard) {
656                 error_report("--discard can only be specified once");
657                 exit(EXIT_FAILURE);
658             }
659             seen_discard = true;
660             if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
661                 error_report("Invalid discard mode `%s'", optarg);
662                 exit(EXIT_FAILURE);
663             }
664             break;
665         case QEMU_NBD_OPT_DETECT_ZEROES:
666             detect_zeroes =
667                 qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
668                                 optarg,
669                                 BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
670                                 &local_err);
671             if (local_err) {
672                 error_reportf_err(local_err,
673                                   "Failed to parse detect_zeroes mode: ");
674                 exit(EXIT_FAILURE);
675             }
676             if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
677                 !(flags & BDRV_O_UNMAP)) {
678                 error_report("setting detect-zeroes to unmap is not allowed "
679                              "without setting discard operation to unmap");
680                 exit(EXIT_FAILURE);
681             }
682             break;
683         case 'b':
684             bindto = optarg;
685             break;
686         case 'p':
687             port = optarg;
688             break;
689         case 'o':
690             if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
691                 error_report("Invalid offset '%s'", optarg);
692                 exit(EXIT_FAILURE);
693             }
694             break;
695         case 'l':
696             if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
697                 sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
698                                                   optarg, false);
699                 if (!sn_opts) {
700                     error_report("Failed in parsing snapshot param `%s'",
701                                  optarg);
702                     exit(EXIT_FAILURE);
703                 }
704             } else {
705                 sn_id_or_name = optarg;
706             }
707             /* fall through */
708         case 'r':
709             readonly = true;
710             flags &= ~BDRV_O_RDWR;
711             break;
712         case 'A':
713             alloc_depth = true;
714             break;
715         case 'B':
716             {
717                 BlockDirtyBitmapOrStr *el = g_new(BlockDirtyBitmapOrStr, 1);
718                 *el = (BlockDirtyBitmapOrStr) {
719                     .type = QTYPE_QSTRING,
720                     .u.local = g_strdup(optarg),
721                 };
722                 QAPI_LIST_PREPEND(bitmaps, el);
723             }
724             break;
725         case 'k':
726             sockpath = optarg;
727             if (sockpath[0] != '/') {
728                 error_report("socket path must be absolute");
729                 exit(EXIT_FAILURE);
730             }
731             break;
732         case 'd':
733             disconnect = true;
734             break;
735         case 'c':
736             opts.device = optarg;
737             break;
738         case 'e':
739             if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
740                 shared < 0) {
741                 error_report("Invalid shared device number '%s'", optarg);
742                 exit(EXIT_FAILURE);
743             }
744             break;
745         case 'f':
746             fmt = optarg;
747             break;
748         case 't':
749             persistent = 1;
750             break;
751         case 'x':
752             export_name = optarg;
753             if (strlen(export_name) > NBD_MAX_STRING_SIZE) {
754                 error_report("export name '%s' too long", export_name);
755                 exit(EXIT_FAILURE);
756             }
757             break;
758         case 'D':
759             export_description = optarg;
760             if (strlen(export_description) > NBD_MAX_STRING_SIZE) {
761                 error_report("export description '%s' too long",
762                              export_description);
763                 exit(EXIT_FAILURE);
764             }
765             break;
766         case 'v':
767             opts.verbose = true;
768             break;
769         case 'V':
770             version(argv[0]);
771             exit(0);
772             break;
773         case 'h':
774             usage(argv[0]);
775             exit(0);
776             break;
777         case '?':
778             error_report("Try `%s --help' for more information.", argv[0]);
779             exit(EXIT_FAILURE);
780         case QEMU_NBD_OPT_OBJECT:
781             user_creatable_process_cmdline(optarg);
782             break;
783         case QEMU_NBD_OPT_TLSCREDS:
784             tlscredsid = optarg;
785             break;
786         case QEMU_NBD_OPT_TLSHOSTNAME:
787             tlshostname = optarg;
788             break;
789         case QEMU_NBD_OPT_IMAGE_OPTS:
790             imageOpts = true;
791             break;
792         case 'T':
793             trace_opt_parse(optarg);
794             break;
795         case QEMU_NBD_OPT_TLSAUTHZ:
796             tlsauthz = optarg;
797             break;
798         case QEMU_NBD_OPT_FORK:
799             opts.fork_process = true;
800             break;
801         case 'L':
802             list = true;
803             break;
804         case QEMU_NBD_OPT_PID_FILE:
805             pid_file_name = optarg;
806             break;
807         case QEMU_NBD_OPT_SELINUX_LABEL:
808             selinux_label = optarg;
809             break;
810         }
811     }
812 
813     if (list) {
814         if (argc != optind) {
815             error_report("List mode is incompatible with a file name");
816             exit(EXIT_FAILURE);
817         }
818         if (export_name || export_description || dev_offset ||
819             opts.device || disconnect || fmt || sn_id_or_name || bitmaps ||
820             alloc_depth || seen_aio || seen_discard || seen_cache) {
821             error_report("List mode is incompatible with per-device settings");
822             exit(EXIT_FAILURE);
823         }
824         if (opts.fork_process) {
825             error_report("List mode is incompatible with forking");
826             exit(EXIT_FAILURE);
827         }
828     } else if ((argc - optind) != 1) {
829         error_report("Invalid number of arguments");
830         error_printf("Try `%s --help' for more information.\n", argv[0]);
831         exit(EXIT_FAILURE);
832     } else if (!export_name) {
833         export_name = "";
834     }
835 
836     if (!trace_init_backends()) {
837         exit(1);
838     }
839     trace_init_file();
840     qemu_set_log(LOG_TRACE, &error_fatal);
841 
842     socket_activation = check_socket_activation();
843     if (socket_activation == 0) {
844         if (!sockpath) {
845             setup_address_and_port(&bindto, &port);
846         }
847     } else {
848         /* Using socket activation - check user didn't use -p etc. */
849         const char *err_msg = socket_activation_validate_opts(opts.device,
850                                                               sockpath,
851                                                               bindto, port,
852                                                               selinux_label,
853                                                               list);
854         if (err_msg != NULL) {
855             error_report("%s", err_msg);
856             exit(EXIT_FAILURE);
857         }
858 
859         /* qemu-nbd can only listen on a single socket.  */
860         if (socket_activation > 1) {
861             error_report("qemu-nbd does not support socket activation with %s > 1",
862                          "LISTEN_FDS");
863             exit(EXIT_FAILURE);
864         }
865     }
866 
867     if (tlscredsid) {
868         if (opts.device) {
869             error_report("TLS is not supported with a host device");
870             exit(EXIT_FAILURE);
871         }
872         if (tlsauthz && list) {
873             error_report("TLS authorization is incompatible with export list");
874             exit(EXIT_FAILURE);
875         }
876         if (tlshostname && !list) {
877             error_report("TLS hostname is only supported with export list");
878             exit(EXIT_FAILURE);
879         }
880         tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
881         if (local_err) {
882             error_reportf_err(local_err, "Failed to get TLS creds: ");
883             exit(EXIT_FAILURE);
884         }
885     } else {
886         if (tlsauthz) {
887             error_report("--tls-authz is not permitted without --tls-creds");
888             exit(EXIT_FAILURE);
889         }
890         if (tlshostname) {
891             error_report("--tls-hostname is not permitted without --tls-creds");
892             exit(EXIT_FAILURE);
893         }
894     }
895 
896     if (selinux_label) {
897 #ifdef CONFIG_SELINUX
898         if (sockpath == NULL && opts.device == NULL) {
899             error_report("--selinux-label is not permitted without --socket");
900             exit(EXIT_FAILURE);
901         }
902 #else
903         error_report("SELinux support not enabled in this binary");
904         exit(EXIT_FAILURE);
905 #endif
906     }
907 
908     if (list) {
909         opts.saddr = nbd_build_socket_address(sockpath, bindto, port);
910         return qemu_nbd_client_list(opts.saddr, tlscreds,
911                                     tlshostname ? tlshostname : bindto);
912     }
913 
914 #if !HAVE_NBD_DEVICE
915     if (disconnect || opts.device) {
916         error_report("Kernel /dev/nbdN support not available");
917         exit(EXIT_FAILURE);
918     }
919 #else /* HAVE_NBD_DEVICE */
920     if (disconnect) {
921         int nbdfd = open(argv[optind], O_RDWR);
922         if (nbdfd < 0) {
923             error_report("Cannot open %s: %s", argv[optind],
924                          strerror(errno));
925             exit(EXIT_FAILURE);
926         }
927         nbd_disconnect(nbdfd);
928 
929         close(nbdfd);
930 
931         printf("%s disconnected\n", argv[optind]);
932 
933         return 0;
934     }
935 #endif
936 
937     if ((opts.device && !opts.verbose) || opts.fork_process) {
938 #ifndef WIN32
939         g_autoptr(GError) err = NULL;
940         int stderr_fd[2];
941         pid_t pid;
942 
943         if (!g_unix_open_pipe(stderr_fd, FD_CLOEXEC, &err)) {
944             error_report("Error setting up communication pipe: %s",
945                          err->message);
946             exit(EXIT_FAILURE);
947         }
948 
949         /* Now daemonize, but keep a communication channel open to
950          * print errors and exit with the proper status code.
951          */
952         pid = fork();
953         if (pid < 0) {
954             error_report("Failed to fork: %s", strerror(errno));
955             exit(EXIT_FAILURE);
956         } else if (pid == 0) {
957             int saved_errno;
958 
959             close(stderr_fd[0]);
960 
961             /* Remember parent's stderr if we will be restoring it. */
962             if (opts.verbose /* fork_process is set */) {
963                 opts.old_stderr = dup(STDERR_FILENO);
964                 if (opts.old_stderr < 0) {
965                     error_report("Could not dup original stderr: %s",
966                                  strerror(errno));
967                     exit(EXIT_FAILURE);
968                 }
969             }
970 
971             ret = qemu_daemon(1, 0);
972             saved_errno = errno;    /* dup2 will overwrite error below */
973 
974             /* Temporarily redirect stderr to the parent's pipe...  */
975             if (dup2(stderr_fd[1], STDERR_FILENO) < 0) {
976                 char str[256];
977                 snprintf(str, sizeof(str),
978                          "%s: Failed to link stderr to the pipe: %s\n",
979                          g_get_prgname(), strerror(errno));
980                 /*
981                  * We are unable to use error_report() here as we need to get
982                  * stderr pointed to the parent's pipe. Write to that pipe
983                  * manually.
984                  */
985                 ret = write(stderr_fd[1], str, strlen(str));
986                 exit(EXIT_FAILURE);
987             }
988 
989             if (ret < 0) {
990                 error_report("Failed to daemonize: %s", strerror(saved_errno));
991                 exit(EXIT_FAILURE);
992             }
993 
994             /* ... close the descriptor we inherited and go on.  */
995             close(stderr_fd[1]);
996         } else {
997             bool errors = false;
998             char *buf;
999 
1000             /* In the parent.  Print error messages from the child until
1001              * it closes the pipe.
1002              */
1003             close(stderr_fd[1]);
1004             buf = g_malloc(1024);
1005             while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
1006                 errors = true;
1007                 ret = qemu_write_full(STDERR_FILENO, buf, ret);
1008                 if (ret < 0) {
1009                     exit(EXIT_FAILURE);
1010                 }
1011             }
1012             if (ret < 0) {
1013                 error_report("Cannot read from daemon: %s",
1014                              strerror(errno));
1015                 exit(EXIT_FAILURE);
1016             }
1017 
1018             /* Usually the daemon should not print any message.
1019              * Exit with zero status in that case.
1020              */
1021             exit(errors);
1022         }
1023 #else /* WIN32 */
1024         error_report("Unable to fork into background on Windows hosts");
1025         exit(EXIT_FAILURE);
1026 #endif /* WIN32 */
1027     }
1028 
1029     if (opts.device != NULL && sockpath == NULL) {
1030         sockpath = g_malloc(128);
1031         snprintf(sockpath, 128, SOCKET_PATH, basename(opts.device));
1032     }
1033 
1034     server = qio_net_listener_new();
1035     if (socket_activation == 0) {
1036         int backlog;
1037 
1038         if (persistent || shared == 0) {
1039             backlog = SOMAXCONN;
1040         } else {
1041             backlog = MIN(shared, SOMAXCONN);
1042         }
1043 #ifdef CONFIG_SELINUX
1044         if (selinux_label && setsockcreatecon_raw(selinux_label) == -1) {
1045             error_report("Cannot set SELinux socket create context to %s: %s",
1046                          selinux_label, strerror(errno));
1047             exit(EXIT_FAILURE);
1048         }
1049 #endif
1050         opts.saddr = nbd_build_socket_address(sockpath, bindto, port);
1051         if (qio_net_listener_open_sync(server, opts.saddr, backlog,
1052                                        &local_err) < 0) {
1053             object_unref(OBJECT(server));
1054             error_report_err(local_err);
1055             exit(EXIT_FAILURE);
1056         }
1057 #ifdef CONFIG_SELINUX
1058         if (selinux_label && setsockcreatecon_raw(NULL) == -1) {
1059             error_report("Cannot clear SELinux socket create context: %s",
1060                          strerror(errno));
1061             exit(EXIT_FAILURE);
1062         }
1063 #endif
1064     } else {
1065         size_t i;
1066         /* See comment in check_socket_activation above. */
1067         for (i = 0; i < socket_activation; i++) {
1068             QIOChannelSocket *sioc;
1069             sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
1070                                              &local_err);
1071             if (sioc == NULL) {
1072                 object_unref(OBJECT(server));
1073                 error_reportf_err(local_err,
1074                                   "Failed to use socket activation: ");
1075                 exit(EXIT_FAILURE);
1076             }
1077             qio_net_listener_add(server, sioc);
1078             object_unref(OBJECT(sioc));
1079         }
1080     }
1081 
1082     qemu_init_main_loop(&error_fatal);
1083     bdrv_init();
1084     atexit(qemu_nbd_shutdown);
1085 
1086     opts.srcpath = argv[optind];
1087     if (imageOpts) {
1088         QemuOpts *o;
1089         if (fmt) {
1090             error_report("--image-opts and -f are mutually exclusive");
1091             exit(EXIT_FAILURE);
1092         }
1093         o = qemu_opts_parse_noisily(&file_opts, opts.srcpath, true);
1094         if (!o) {
1095             qemu_opts_reset(&file_opts);
1096             exit(EXIT_FAILURE);
1097         }
1098         options = qemu_opts_to_qdict(o, NULL);
1099         qemu_opts_reset(&file_opts);
1100         blk = blk_new_open(NULL, NULL, options, flags, &local_err);
1101     } else {
1102         if (fmt) {
1103             options = qdict_new();
1104             qdict_put_str(options, "driver", fmt);
1105         }
1106         blk = blk_new_open(opts.srcpath, NULL, options, flags, &local_err);
1107     }
1108 
1109     if (!blk) {
1110         error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
1111                           argv[optind]);
1112         exit(EXIT_FAILURE);
1113     }
1114     bs = blk_bs(blk);
1115 
1116     if (dev_offset) {
1117         QDict *raw_opts = qdict_new();
1118         qdict_put_str(raw_opts, "driver", "raw");
1119         qdict_put_str(raw_opts, "file", bs->node_name);
1120         qdict_put_int(raw_opts, "offset", dev_offset);
1121 
1122         aio_context_acquire(qemu_get_aio_context());
1123         bs = bdrv_open(NULL, NULL, raw_opts, flags, &error_fatal);
1124         aio_context_release(qemu_get_aio_context());
1125 
1126         blk_remove_bs(blk);
1127         blk_insert_bs(blk, bs, &error_fatal);
1128         bdrv_unref(bs);
1129     }
1130 
1131     blk_set_enable_write_cache(blk, !writethrough);
1132 
1133     if (sn_opts) {
1134         ret = bdrv_snapshot_load_tmp(bs,
1135                                      qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
1136                                      qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
1137                                      &local_err);
1138     } else if (sn_id_or_name) {
1139         ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
1140                                                    &local_err);
1141     }
1142     if (ret < 0) {
1143         error_reportf_err(local_err, "Failed to load snapshot: ");
1144         exit(EXIT_FAILURE);
1145     }
1146 
1147     bs->detect_zeroes = detect_zeroes;
1148 
1149     nbd_server_is_qemu_nbd(shared);
1150 
1151     export_opts = g_new(BlockExportOptions, 1);
1152     *export_opts = (BlockExportOptions) {
1153         .type               = BLOCK_EXPORT_TYPE_NBD,
1154         .id                 = g_strdup("qemu-nbd-export"),
1155         .node_name          = g_strdup(bdrv_get_node_name(bs)),
1156         .has_writethrough   = true,
1157         .writethrough       = writethrough,
1158         .has_writable       = true,
1159         .writable           = !readonly,
1160         .u.nbd = {
1161             .name                 = g_strdup(export_name),
1162             .description          = g_strdup(export_description),
1163             .has_bitmaps          = !!bitmaps,
1164             .bitmaps              = bitmaps,
1165             .has_allocation_depth = alloc_depth,
1166             .allocation_depth     = alloc_depth,
1167         },
1168     };
1169     blk_exp_add(export_opts, &error_fatal);
1170     qapi_free_BlockExportOptions(export_opts);
1171 
1172     if (opts.device) {
1173 #if HAVE_NBD_DEVICE
1174         ret = pthread_create(&client_thread, NULL, nbd_client_thread, &opts);
1175         if (ret != 0) {
1176             error_report("Failed to create client thread: %s", strerror(ret));
1177             exit(EXIT_FAILURE);
1178         }
1179 #endif
1180     } else {
1181         /* Shut up GCC warnings.  */
1182         memset(&client_thread, 0, sizeof(client_thread));
1183     }
1184 
1185     nbd_update_server_watch();
1186 
1187     if (pid_file_name) {
1188         qemu_write_pidfile(pid_file_name, &error_fatal);
1189     }
1190 
1191     /* now when the initialization is (almost) complete, chdir("/")
1192      * to free any busy filesystems */
1193     if (chdir("/") < 0) {
1194         error_report("Could not chdir to root directory: %s",
1195                      strerror(errno));
1196         exit(EXIT_FAILURE);
1197     }
1198 
1199     if (opts.fork_process) {
1200         nbd_client_release_pipe(opts.old_stderr);
1201     }
1202 
1203     state = RUNNING;
1204     do {
1205         main_loop_wait(false);
1206         if (state == TERMINATE) {
1207             blk_exp_close_all();
1208             state = TERMINATED;
1209         }
1210     } while (state != TERMINATED);
1211 
1212     blk_unref(blk);
1213     if (sockpath) {
1214         unlink(sockpath);
1215     }
1216 
1217     qemu_opts_del(sn_opts);
1218 
1219     if (opts.device) {
1220         void *result;
1221         pthread_join(client_thread, &result);
1222         ret = (intptr_t)result;
1223         exit(ret);
1224     } else {
1225         exit(EXIT_SUCCESS);
1226     }
1227 }
1228