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