qemu-nbd.c 32.5 KB
Newer Older
1
/*
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 *  Copyright (C) 2005  Anthony Liguori <anthony@codemonkey.ws>
 *
 *  Network Block Device
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; under version 2 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  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
 */

Peter Maydell's avatar
Peter Maydell committed
19
#include "qemu/osdep.h"
20 21 22 23
#include <getopt.h>
#include <libgen.h>
#include <pthread.h>

24
#include "qapi/error.h"
25
#include "qemu-common.h"
26
#include "qemu/cutils.h"
27
#include "sysemu/block-backend.h"
28
#include "block/block_int.h"
29
#include "block/nbd.h"
30
#include "qemu/main-loop.h"
31
#include "qemu/error-report.h"
32
#include "qemu/config-file.h"
33
#include "qemu/bswap.h"
34
#include "qemu/log.h"
35
#include "qemu/systemd.h"
36
#include "block/snapshot.h"
37
#include "qapi/qmp/qstring.h"
38
#include "qom/object_interfaces.h"
39
#include "io/channel-socket.h"
40
#include "io/net-listener.h"
41
#include "crypto/init.h"
42
#include "trace/control.h"
43
#include "qemu-version.h"
44

45
#define SOCKET_PATH                "/var/lock/qemu-nbd-%s"
46 47 48 49 50 51 52
#define QEMU_NBD_OPT_CACHE         256
#define QEMU_NBD_OPT_AIO           257
#define QEMU_NBD_OPT_DISCARD       258
#define QEMU_NBD_OPT_DETECT_ZEROES 259
#define QEMU_NBD_OPT_OBJECT        260
#define QEMU_NBD_OPT_TLSCREDS      261
#define QEMU_NBD_OPT_IMAGE_OPTS    262
Max Reitz's avatar
Max Reitz committed
53
#define QEMU_NBD_OPT_FORK          263
54

55 56
#define MBR_SIZE 512

57
static NBDExport *exp;
58
static bool newproto;
59
static int verbose;
60
static char *srcpath;
61
static SocketAddress *saddr;
62 63
static int persistent = 0;
static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
64 65
static int shared = 1;
static int nb_fds;
66
static QIONetListener *server;
67
static QCryptoTLSCreds *tlscreds;
68 69 70

static void usage(const char *name)
{
71
    (printf) (
72 73 74
"Usage: %s [OPTIONS] FILE\n"
"QEMU Disk Network Block Device Server\n"
"\n"
75 76
"  -h, --help                display this help and exit\n"
"  -V, --version             output version information and exit\n"
77 78
"\n"
"Connection properties:\n"
79 80 81 82 83 84 85
"  -p, --port=PORT           port to listen on (default `%d')\n"
"  -b, --bind=IFACE          interface to bind to (default `0.0.0.0')\n"
"  -k, --socket=PATH         path to the unix socket\n"
"                            (default '"SOCKET_PATH"')\n"
"  -e, --shared=NUM          device can be shared by NUM clients (default '1')\n"
"  -t, --persistent          don't exit on the last connection\n"
"  -v, --verbose             display extra debugging information\n"
86
"  -x, --export-name=NAME    expose export by name\n"
87
"  -D, --description=TEXT    with -x, also export a human-readable description\n"
88
"\n"
89
"Exposing part of the image:\n"
90 91
"  -o, --offset=OFFSET       offset into the image\n"
"  -P, --partition=NUM       only expose partition NUM\n"
92
"\n"
93 94 95
"General purpose options:\n"
"  --object type,id=ID,...   define an object such as 'secret' for providing\n"
"                            passwords and/or encryption keys\n"
96 97
"  -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
"                            specify tracing options\n"
Max Reitz's avatar
Max Reitz committed
98 99
"  --fork                    fork off the server process and exit the parent\n"
"                            once the server is running\n"
100 101
#ifdef __linux__
"Kernel NBD client support:\n"
102 103
"  -c, --connect=DEV         connect FILE to the local NBD device DEV\n"
"  -d, --disconnect          disconnect the specified device\n"
104 105 106 107
"\n"
#endif
"\n"
"Block device options:\n"
108 109 110 111 112
"  -f, --format=FORMAT       set image format (raw, qcow2, ...)\n"
"  -r, --read-only           export read-only\n"
"  -s, --snapshot            use FILE as an external snapshot, create a temporary\n"
"                            file with backing_file=FILE, redirect the write to\n"
"                            the temporary one\n"
113
"  -l, --load-snapshot=SNAPSHOT_PARAM\n"
114 115 116 117 118 119 120
"                            load an internal snapshot inside FILE and export it\n"
"                            as an read-only device, SNAPSHOT_PARAM format is\n"
"                            'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
"                            '[ID_OR_NAME]'\n"
"  -n, --nocache             disable host cache\n"
"      --cache=MODE          set cache mode (none, writeback, ...)\n"
"      --aio=MODE            set AIO mode (native or threads)\n"
121
"      --discard=MODE        set discard mode (ignore, unmap)\n"
122
"      --detect-zeroes=MODE  set detect-zeroes mode (off, on, unmap)\n"
123
"      --image-opts          treat FILE as a full set of image options\n"
124
"\n"
125
QEMU_HELP_BOTTOM "\n"
126
    , name, NBD_DEFAULT_PORT, "DEVICE");
127 128 129 130 131
}

static void version(const char *name)
{
    printf(
132
"%s " QEMU_VERSION QEMU_PKGVERSION "\n"
133 134
"Written by Anthony Liguori.\n"
"\n"
135
QEMU_COPYRIGHT "\n"
136 137
"This is free software; see the source for copying conditions.  There is NO\n"
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
138
    , name);
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

struct partition_record
{
    uint8_t bootable;
    uint8_t start_head;
    uint32_t start_cylinder;
    uint8_t start_sector;
    uint8_t system;
    uint8_t end_head;
    uint8_t end_cylinder;
    uint8_t end_sector;
    uint32_t start_sector_abs;
    uint32_t nb_sectors_abs;
};

static void read_partition(uint8_t *p, struct partition_record *r)
{
    r->bootable = p[0];
    r->start_head = p[1];
    r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
    r->start_sector = p[2] & 0x3f;
    r->system = p[4];
    r->end_head = p[5];
    r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
    r->end_sector = p[6] & 0x3f;
165

166 167
    r->start_sector_abs = ldl_le_p(p + 8);
    r->nb_sectors_abs   = ldl_le_p(p + 12);
168 169
}

170
static int find_partition(BlockBackend *blk, int partition,
171 172 173
                          off_t *offset, off_t *size)
{
    struct partition_record mbr[4];
174
    uint8_t data[MBR_SIZE];
175 176
    int i;
    int ext_partnum = 4;
177
    int ret;
178

179 180
    ret = blk_pread(blk, 0, data, sizeof(data));
    if (ret < 0) {
181
        error_report("error while reading: %s", strerror(-ret));
182
        exit(EXIT_FAILURE);
183
    }
184 185

    if (data[510] != 0x55 || data[511] != 0xaa) {
186
        return -EINVAL;
187 188 189 190 191
    }

    for (i = 0; i < 4; i++) {
        read_partition(&data[446 + 16 * i], &mbr[i]);

192
        if (!mbr[i].system || !mbr[i].nb_sectors_abs) {
193
            continue;
194
        }
195 196 197

        if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
            struct partition_record ext[4];
198
            uint8_t data1[MBR_SIZE];
199 200
            int j;

201 202 203
            ret = blk_pread(blk, mbr[i].start_sector_abs * MBR_SIZE,
                            data1, sizeof(data1));
            if (ret < 0) {
204
                error_report("error while reading: %s", strerror(-ret));
205
                exit(EXIT_FAILURE);
206
            }
207 208 209

            for (j = 0; j < 4; j++) {
                read_partition(&data1[446 + 16 * j], &ext[j]);
210
                if (!ext[j].system || !ext[j].nb_sectors_abs) {
211
                    continue;
212
                }
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227

                if ((ext_partnum + j + 1) == partition) {
                    *offset = (uint64_t)ext[j].start_sector_abs << 9;
                    *size = (uint64_t)ext[j].nb_sectors_abs << 9;
                    return 0;
                }
            }
            ext_partnum += 4;
        } else if ((i + 1) == partition) {
            *offset = (uint64_t)mbr[i].start_sector_abs << 9;
            *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
            return 0;
        }
    }

228
    return -ENOENT;
229 230
}

Paolo Bonzini's avatar
Paolo Bonzini committed
231 232
static void termsig_handler(int signum)
{
233
    atomic_cmpxchg(&state, RUNNING, TERMINATE);
234
    qemu_notify_event();
Paolo Bonzini's avatar
Paolo Bonzini committed
235 236
}

237

238
static void *show_parts(void *arg)
239
{
240
    char *device = arg;
241 242 243 244 245 246 247 248
    int nbd;

    /* linux just needs an open() to trigger
     * the partition table update
     * but remember to load the module with max_part != 0 :
     *     modprobe nbd max_part=63
     */
    nbd = open(device, O_RDWR);
249
    if (nbd >= 0) {
250 251 252 253
        close(nbd);
    }
    return NULL;
}
254

255 256
static void *nbd_client_thread(void *arg)
{
257
    char *device = arg;
258
    NBDExportInfo info = { .request_sizes = false, };
259 260
    QIOChannelSocket *sioc;
    int fd;
261 262
    int ret;
    pthread_t show_parts_thread;
Max Reitz's avatar
Max Reitz committed
263
    Error *local_error = NULL;
264

265 266 267 268
    sioc = qio_channel_socket_new();
    if (qio_channel_socket_connect_sync(sioc,
                                        saddr,
                                        &local_error) < 0) {
269
        error_report_err(local_error);
270 271
        goto out;
    }
272

273 274
    ret = nbd_receive_negotiate(QIO_CHANNEL(sioc), NULL,
                                NULL, NULL, NULL, &info, &local_error);
275
    if (ret < 0) {
Max Reitz's avatar
Max Reitz committed
276
        if (local_error) {
277
            error_report_err(local_error);
Max Reitz's avatar
Max Reitz committed
278
        }
279
        goto out_socket;
280 281
    }

282
    fd = open(device, O_RDWR);
283
    if (fd < 0) {
284
        /* Linux-only, we can use %m in printf.  */
285
        error_report("Failed to open %s: %m", device);
286
        goto out_socket;
287 288
    }

289
    ret = nbd_init(fd, sioc, &info, &local_error);
290
    if (ret < 0) {
291
        error_report_err(local_error);
292
        goto out_fd;
293 294 295
    }

    /* update partition table */
296
    pthread_create(&show_parts_thread, NULL, show_parts, device);
297

298 299 300 301 302 303 304
    if (verbose) {
        fprintf(stderr, "NBD device %s is now connected to %s\n",
                device, srcpath);
    } else {
        /* Close stderr so that the qemu-nbd process exits.  */
        dup2(STDOUT_FILENO, STDERR_FILENO);
    }
305 306 307

    ret = nbd_client(fd);
    if (ret) {
308
        goto out_fd;
309
    }
310
    close(fd);
311
    object_unref(OBJECT(sioc));
312 313 314
    kill(getpid(), SIGTERM);
    return (void *) EXIT_SUCCESS;

315 316 317
out_fd:
    close(fd);
out_socket:
318
    object_unref(OBJECT(sioc));
319 320 321
out:
    kill(getpid(), SIGTERM);
    return (void *) EXIT_FAILURE;
322 323
}

324
static int nbd_can_accept(void)
325
{
326
    return state == RUNNING && nb_fds < shared;
327 328
}

329 330 331 332 333 334
static void nbd_export_closed(NBDExport *exp)
{
    assert(state == TERMINATING);
    state = TERMINATED;
}

335
static void nbd_update_server_watch(void);
336

337
static void nbd_client_closed(NBDClient *client, bool negotiated)
338
{
339
    nb_fds--;
340
    if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
341 342
        state = TERMINATE;
    }
343
    nbd_update_server_watch();
344
    nbd_client_put(client);
345 346
}

347 348
static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
                       gpointer opaque)
349
{
350
    if (state >= TERMINATE) {
351
        return;
352 353
    }

354
    nb_fds++;
355
    nbd_update_server_watch();
356
    nbd_client_new(newproto ? NULL : exp, cioc,
357
                   tlscreds, NULL, nbd_client_closed);
358 359
}

360
static void nbd_update_server_watch(void)
361 362
{
    if (nbd_can_accept()) {
363
        qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
364
    } else {
365
        qio_net_listener_set_client_func(server, NULL, NULL, NULL);
366 367 368
    }
}

369

370
static SocketAddress *nbd_build_socket_address(const char *sockpath,
371 372 373
                                               const char *bindto,
                                               const char *port)
{
374
    SocketAddress *saddr;
375

376
    saddr = g_new0(SocketAddress, 1);
377
    if (sockpath) {
378 379
        saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
        saddr->u.q_unix.path = g_strdup(sockpath);
380
    } else {
381
        InetSocketAddress *inet;
382 383
        saddr->type = SOCKET_ADDRESS_TYPE_INET;
        inet = &saddr->u.inet;
384
        inet->host = g_strdup(bindto);
385
        if (port) {
386
            inet->port = g_strdup(port);
387
        } else  {
388
            inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
389 390 391 392 393 394 395
        }
    }

    return saddr;
}


396 397 398 399 400 401 402 403 404 405
static QemuOptsList file_opts = {
    .name = "file",
    .implied_opt_name = "file",
    .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
    .desc = {
        /* no elements => accept any params */
        { /* end of list */ }
    },
};

406 407 408 409 410 411 412 413 414 415
static QemuOptsList qemu_object_opts = {
    .name = "object",
    .implied_opt_name = "qom-type",
    .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
    .desc = {
        { }
    },
};


416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445

static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, Error **errp)
{
    Object *obj;
    QCryptoTLSCreds *creds;

    obj = object_resolve_path_component(
        object_get_objects_root(), id);
    if (!obj) {
        error_setg(errp, "No TLS credentials with id '%s'",
                   id);
        return NULL;
    }
    creds = (QCryptoTLSCreds *)
        object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
    if (!creds) {
        error_setg(errp, "Object with id '%s' is not TLS credentials",
                   id);
        return NULL;
    }

    if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
        error_setg(errp,
                   "Expecting TLS credentials with a server endpoint");
        return NULL;
    }
    object_ref(obj);
    return creds;
}

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
static void setup_address_and_port(const char **address, const char **port)
{
    if (*address == NULL) {
        *address = "0.0.0.0";
    }

    if (*port == NULL) {
        *port = stringify(NBD_DEFAULT_PORT);
    }
}

/*
 * Check socket parameters compatibility when socket activation is used.
 */
static const char *socket_activation_validate_opts(const char *device,
                                                   const char *sockpath,
                                                   const char *address,
                                                   const char *port)
{
    if (device != NULL) {
        return "NBD device can't be set when using socket activation";
    }

    if (sockpath != NULL) {
        return "Unix socket can't be set when using socket activation";
    }

    if (address != NULL) {
        return "The interface can't be set when using socket activation";
    }

    if (port != NULL) {
        return "TCP port number can't be set when using socket activation";
    }

    return NULL;
}
483

484 485
int main(int argc, char **argv)
{
486
    BlockBackend *blk;
487 488
    BlockDriverState *bs;
    off_t dev_offset = 0;
489
    uint16_t nbdflags = 0;
490
    bool disconnect = false;
491
    const char *bindto = NULL;
492 493
    const char *port = NULL;
    char *sockpath = NULL;
494
    char *device = NULL;
495
    off_t fd_size;
496 497
    QemuOpts *sn_opts = NULL;
    const char *sn_id_or_name = NULL;
498
    const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:x:T:D:";
499
    struct option lopt[] = {
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
        { "help", no_argument, NULL, 'h' },
        { "version", no_argument, NULL, 'V' },
        { "bind", required_argument, NULL, 'b' },
        { "port", required_argument, NULL, 'p' },
        { "socket", required_argument, NULL, 'k' },
        { "offset", required_argument, NULL, 'o' },
        { "read-only", no_argument, NULL, 'r' },
        { "partition", required_argument, NULL, 'P' },
        { "connect", required_argument, NULL, 'c' },
        { "disconnect", no_argument, NULL, 'd' },
        { "snapshot", no_argument, NULL, 's' },
        { "load-snapshot", required_argument, NULL, 'l' },
        { "nocache", no_argument, NULL, 'n' },
        { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
        { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
        { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
        { "detect-zeroes", required_argument, NULL,
          QEMU_NBD_OPT_DETECT_ZEROES },
        { "shared", required_argument, NULL, 'e' },
        { "format", required_argument, NULL, 'f' },
        { "persistent", no_argument, NULL, 't' },
        { "verbose", no_argument, NULL, 'v' },
        { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
        { "export-name", required_argument, NULL, 'x' },
524
        { "description", required_argument, NULL, 'D' },
525 526
        { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
        { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
527
        { "trace", required_argument, NULL, 'T' },
Max Reitz's avatar
Max Reitz committed
528
        { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
529
        { NULL, 0, NULL, 0 }
530 531 532 533
    };
    int ch;
    int opt_ind = 0;
    char *end;
534
    int flags = BDRV_O_RDWR;
535
    int partition = -1;
536
    int ret = 0;
537
    bool seen_cache = false;
538
    bool seen_discard = false;
539
    bool seen_aio = false;
540
    pthread_t client_thread;
541
    const char *fmt = NULL;
542
    Error *local_err = NULL;
543
    BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
544
    QDict *options = NULL;
545
    const char *export_name = NULL;
546
    const char *export_description = NULL;
547
    const char *tlscredsid = NULL;
548
    bool imageOpts = false;
549
    bool writethrough = true;
550
    char *trace_file = NULL;
Max Reitz's avatar
Max Reitz committed
551 552
    bool fork_process = false;
    int old_stderr = -1;
553
    unsigned socket_activation;
554

555 556 557
    /* The client thread uses SIGTERM to interrupt the server.  A signal
     * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
     */
Paolo Bonzini's avatar
Paolo Bonzini committed
558 559 560 561
    struct sigaction sa_sigterm;
    memset(&sa_sigterm, 0, sizeof(sa_sigterm));
    sa_sigterm.sa_handler = termsig_handler;
    sigaction(SIGTERM, &sa_sigterm, NULL);
562

Max Reitz's avatar
Max Reitz committed
563 564 565 566
#ifdef CONFIG_POSIX
    signal(SIGPIPE, SIG_IGN);
#endif

567
    module_call_init(MODULE_INIT_TRACE);
568
    qcrypto_init(&error_fatal);
569

570 571
    module_call_init(MODULE_INIT_QOM);
    qemu_add_opts(&qemu_object_opts);
572
    qemu_add_opts(&qemu_trace_opts);
573
    qemu_init_exec_dir(argv[0]);
Paolo Bonzini's avatar
Paolo Bonzini committed
574

575 576 577
    while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
        switch (ch) {
        case 's':
578 579 580
            flags |= BDRV_O_SNAPSHOT;
            break;
        case 'n':
581 582 583 584
            optarg = (char *) "none";
            /* fallthrough */
        case QEMU_NBD_OPT_CACHE:
            if (seen_cache) {
585 586
                error_report("-n and --cache can only be specified once");
                exit(EXIT_FAILURE);
587 588
            }
            seen_cache = true;
589
            if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
590 591
                error_report("Invalid cache mode `%s'", optarg);
                exit(EXIT_FAILURE);
592
            }
593
            break;
594 595
        case QEMU_NBD_OPT_AIO:
            if (seen_aio) {
596 597
                error_report("--aio can only be specified once");
                exit(EXIT_FAILURE);
598 599 600 601 602 603 604
            }
            seen_aio = true;
            if (!strcmp(optarg, "native")) {
                flags |= BDRV_O_NATIVE_AIO;
            } else if (!strcmp(optarg, "threads")) {
                /* this is the default */
            } else {
605 606
               error_report("invalid aio mode `%s'", optarg);
               exit(EXIT_FAILURE);
607 608
            }
            break;
609 610
        case QEMU_NBD_OPT_DISCARD:
            if (seen_discard) {
611 612
                error_report("--discard can only be specified once");
                exit(EXIT_FAILURE);
613 614 615
            }
            seen_discard = true;
            if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
616 617
                error_report("Invalid discard mode `%s'", optarg);
                exit(EXIT_FAILURE);
618 619
            }
            break;
620 621
        case QEMU_NBD_OPT_DETECT_ZEROES:
            detect_zeroes =
622
                qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
623 624 625 626
                                optarg,
                                BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
                                &local_err);
            if (local_err) {
627 628
                error_reportf_err(local_err,
                                  "Failed to parse detect_zeroes mode: ");
629
                exit(EXIT_FAILURE);
630 631 632
            }
            if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
                !(flags & BDRV_O_UNMAP)) {
633 634 635
                error_report("setting detect-zeroes to unmap is not allowed "
                             "without setting discard operation to unmap");
                exit(EXIT_FAILURE);
636 637
            }
            break;
638 639 640 641
        case 'b':
            bindto = optarg;
            break;
        case 'p':
642
            port = optarg;
643 644 645 646
            break;
        case 'o':
                dev_offset = strtoll (optarg, &end, 0);
            if (*end) {
647 648
                error_report("Invalid offset `%s'", optarg);
                exit(EXIT_FAILURE);
649 650
            }
            if (dev_offset < 0) {
651 652
                error_report("Offset must be positive `%s'", optarg);
                exit(EXIT_FAILURE);
653 654
            }
            break;
655 656
        case 'l':
            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
657 658
                sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
                                                  optarg, false);
659
                if (!sn_opts) {
660 661 662
                    error_report("Failed in parsing snapshot param `%s'",
                                 optarg);
                    exit(EXIT_FAILURE);
663 664 665 666 667
                }
            } else {
                sn_id_or_name = optarg;
            }
            /* fall through */
668
        case 'r':
669
            nbdflags |= NBD_FLAG_READ_ONLY;
670
            flags &= ~BDRV_O_RDWR;
671 672 673
            break;
        case 'P':
            partition = strtol(optarg, &end, 0);
674
            if (*end) {
675 676
                error_report("Invalid partition `%s'", optarg);
                exit(EXIT_FAILURE);
677 678
            }
            if (partition < 1 || partition > 8) {
679 680
                error_report("Invalid partition %d", partition);
                exit(EXIT_FAILURE);
681
            }
682
            break;
683
        case 'k':
684
            sockpath = optarg;
685
            if (sockpath[0] != '/') {
686
                error_report("socket path must be absolute");
687
                exit(EXIT_FAILURE);
688
            }
689 690 691 692 693 694 695
            break;
        case 'd':
            disconnect = true;
            break;
        case 'c':
            device = optarg;
            break;
696 697 698
        case 'e':
            shared = strtol(optarg, &end, 0);
            if (*end) {
699 700
                error_report("Invalid shared device number '%s'", optarg);
                exit(EXIT_FAILURE);
701 702
            }
            if (shared < 1) {
703
                error_report("Shared device number must be greater than 0");
704
                exit(EXIT_FAILURE);
705 706
            }
            break;
707 708 709
        case 'f':
            fmt = optarg;
            break;
710 711 712
        case 't':
            persistent = 1;
            break;
713 714 715
        case 'x':
            export_name = optarg;
            break;
716 717 718
        case 'D':
            export_description = optarg;
            break;
719 720 721 722 723 724 725 726 727 728 729 730
        case 'v':
            verbose = 1;
            break;
        case 'V':
            version(argv[0]);
            exit(0);
            break;
        case 'h':
            usage(argv[0]);
            exit(0);
            break;
        case '?':
731 732
            error_report("Try `%s --help' for more information.", argv[0]);
            exit(EXIT_FAILURE);
733 734 735 736 737 738 739 740
        case QEMU_NBD_OPT_OBJECT: {
            QemuOpts *opts;
            opts = qemu_opts_parse_noisily(&qemu_object_opts,
                                           optarg, true);
            if (!opts) {
                exit(EXIT_FAILURE);
            }
        }   break;
741 742 743
        case QEMU_NBD_OPT_TLSCREDS:
            tlscredsid = optarg;
            break;
744 745 746
        case QEMU_NBD_OPT_IMAGE_OPTS:
            imageOpts = true;
            break;
747 748 749 750
        case 'T':
            g_free(trace_file);
            trace_file = trace_opt_parse(optarg);
            break;
Max Reitz's avatar
Max Reitz committed
751 752 753
        case QEMU_NBD_OPT_FORK:
            fork_process = true;
            break;
754 755 756 757
        }
    }

    if ((argc - optind) != 1) {
758 759
        error_report("Invalid number of arguments");
        error_printf("Try `%s --help' for more information.\n", argv[0]);
760
        exit(EXIT_FAILURE);
761 762
    }

763 764
    if (qemu_opts_foreach(&qemu_object_opts,
                          user_creatable_add_opts_foreach,
765
                          NULL, NULL)) {
766 767 768
        exit(EXIT_FAILURE);
    }

769 770 771 772 773 774
    if (!trace_init_backends()) {
        exit(1);
    }
    trace_init_file(trace_file);
    qemu_set_log(LOG_TRACE);

775 776 777 778 779 780 781 782 783 784 785
    socket_activation = check_socket_activation();
    if (socket_activation == 0) {
        setup_address_and_port(&bindto, &port);
    } else {
        /* Using socket activation - check user didn't use -p etc. */
        const char *err_msg = socket_activation_validate_opts(device, sockpath,
                                                              bindto, port);
        if (err_msg != NULL) {
            error_report("%s", err_msg);
            exit(EXIT_FAILURE);
        }
786 787 788 789 790 791 792

        /* qemu-nbd can only listen on a single socket.  */
        if (socket_activation > 1) {
            error_report("qemu-nbd does not support socket activation with %s > 1",
                         "LISTEN_FDS");
            exit(EXIT_FAILURE);
        }
793 794
    }

795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816
    if (tlscredsid) {
        if (sockpath) {
            error_report("TLS is only supported with IPv4/IPv6");
            exit(EXIT_FAILURE);
        }
        if (device) {
            error_report("TLS is not supported with a host device");
            exit(EXIT_FAILURE);
        }
        if (!export_name) {
            /* Set the default NBD protocol export name, since
             * we *must* use new style protocol for TLS */
            export_name = "";
        }
        tlscreds = nbd_get_tls_creds(tlscredsid, &local_err);
        if (local_err) {
            error_report("Failed to get TLS creds %s",
                         error_get_pretty(local_err));
            exit(EXIT_FAILURE);
        }
    }

817
    if (disconnect) {
818 819
        int nbdfd = open(argv[optind], O_RDWR);
        if (nbdfd < 0) {
820 821 822
            error_report("Cannot open %s: %s", argv[optind],
                         strerror(errno));
            exit(EXIT_FAILURE);
823
        }
824
        nbd_disconnect(nbdfd);
825

826
        close(nbdfd);
827 828 829

        printf("%s disconnected\n", argv[optind]);

830
        return 0;
831 832
    }

Max Reitz's avatar
Max Reitz committed
833
    if ((device && !verbose) || fork_process) {
834 835 836 837
        int stderr_fd[2];
        pid_t pid;
        int ret;

838
        if (qemu_pipe(stderr_fd) < 0) {
839 840 841
            error_report("Error setting up communication pipe: %s",
                         strerror(errno));
            exit(EXIT_FAILURE);
842 843 844 845 846 847
        }

        /* Now daemonize, but keep a communication channel open to
         * print errors and exit with the proper status code.
         */
        pid = fork();
Max Reitz's avatar
Max Reitz committed
848
        if (pid < 0) {
849 850
            error_report("Failed to fork: %s", strerror(errno));
            exit(EXIT_FAILURE);
Max Reitz's avatar
Max Reitz committed
851
        } else if (pid == 0) {
852
            close(stderr_fd[0]);
853
            ret = qemu_daemon(1, 0);
854 855

            /* Temporarily redirect stderr to the parent's pipe...  */
Max Reitz's avatar
Max Reitz committed
856
            old_stderr = dup(STDERR_FILENO);
857
            dup2(stderr_fd[1], STDERR_FILENO);
858
            if (ret < 0) {
859 860
                error_report("Failed to daemonize: %s", strerror(errno));
                exit(EXIT_FAILURE);
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876
            }

            /* ... close the descriptor we inherited and go on.  */
            close(stderr_fd[1]);
        } else {
            bool errors = false;
            char *buf;

            /* In the parent.  Print error messages from the child until
             * it closes the pipe.
             */
            close(stderr_fd[1]);
            buf = g_malloc(1024);
            while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
                errors = true;
                ret = qemu_write_full(STDERR_FILENO, buf, ret);
877
                if (ret < 0) {
878 879 880
                    exit(EXIT_FAILURE);
                }
            }
881
            if (ret < 0) {
882 883 884
                error_report("Cannot read from daemon: %s",
                             strerror(errno));
                exit(EXIT_FAILURE);
885 886 887 888 889 890 891 892 893
            }

            /* Usually the daemon should not print any message.
             * Exit with zero status in that case.
             */
            exit(errors);
        }
    }

894 895 896
    if (device != NULL && sockpath == NULL) {
        sockpath = g_malloc(128);
        snprintf(sockpath, 128, SOCKET_PATH, basename(device));
897 898
    }

899
    server = qio_net_listener_new();
900 901
    if (socket_activation == 0) {
        saddr = nbd_build_socket_address(sockpath, bindto, port);
902 903
        if (qio_net_listener_open_sync(server, saddr, &local_err) < 0) {
            object_unref(OBJECT(server));
904
            error_report_err(local_err);
905
            exit(EXIT_FAILURE);
906 907
        }
    } else {
908
        size_t i;
909
        /* See comment in check_socket_activation above. */
910 911 912 913 914 915 916 917 918 919 920 921
        for (i = 0; i < socket_activation; i++) {
            QIOChannelSocket *sioc;
            sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
                                             &local_err);
            if (sioc == NULL) {
                object_unref(OBJECT(server));
                error_report("Failed to use socket activation: %s",
                             error_get_pretty(local_err));
                exit(EXIT_FAILURE);
            }
            qio_net_listener_add(server, sioc);
            object_unref(OBJECT(sioc));
922 923
        }
    }
924

925
    if (qemu_init_main_loop(&local_err)) {
926
        error_report_err(local_err);
927 928
        exit(EXIT_FAILURE);
    }
929 930 931
    bdrv_init();
    atexit(bdrv_close_all);

932 933 934 935 936 937 938 939 940 941 942 943 944 945
    srcpath = argv[optind];
    if (imageOpts) {
        QemuOpts *opts;
        if (fmt) {
            error_report("--image-opts and -f are mutually exclusive");
            exit(EXIT_FAILURE);
        }
        opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
        if (!opts) {
            qemu_opts_reset(&file_opts);
            exit(EXIT_FAILURE);
        }
        options = qemu_opts_to_qdict(opts, NULL);
        qemu_opts_reset(&file_opts);
946
        blk = blk_new_open(NULL, NULL, options, flags, &local_err);
947 948 949
    } else {
        if (fmt) {
            options = qdict_new();
950
            qdict_put_str(options, "driver", fmt);
951
        }
952
        blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
953 954
    }

955
    if (!blk) {
956 957
        error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
                          argv[optind]);
958
        exit(EXIT_FAILURE);
959
    }
960
    bs = blk_bs(blk);
961

962 963
    blk_set_enable_write_cache(blk, !writethrough);

964 965 966 967 968 969 970 971 972 973
    if (sn_opts) {
        ret = bdrv_snapshot_load_tmp(bs,
                                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
                                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
                                     &local_err);
    } else if (sn_id_or_name) {
        ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
                                                   &local_err);
    }
    if (ret < 0) {
974
        error_reportf_err(local_err, "Failed to load snapshot: ");
975
        exit(EXIT_FAILURE);
976 977
    }

978
    bs->detect_zeroes = detect_zeroes;
979
    fd_size = blk_getlength(blk);
980
    if (fd_size < 0) {
981 982 983
        error_report("Failed to determine the image length: %s",
                     strerror(-fd_size));
        exit(EXIT_FAILURE);
984
    }
985

986 987 988 989 990 991 992 993
    if (dev_offset >= fd_size) {
        error_report("Offset (%lld) has to be smaller than the image size "
                     "(%lld)",
                     (long long int)dev_offset, (long long int)fd_size);
        exit(EXIT_FAILURE);
    }
    fd_size -= dev_offset;

994
    if (partition != -1) {
995
        ret = find_partition(blk, partition, &dev_offset, &fd_size);
996
        if (ret < 0) {
997
            error_report("Could not find partition %d: %s", partition,
998
                         strerror(-ret));
999
            exit(EXIT_FAILURE);
1000
        }
1001 1002
    }

1003 1004
    exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed,
                         writethrough, NULL, &local_err);
1005
    if (!exp) {
1006
        error_report_err(local_err);
1007
        exit(EXIT_FAILURE);
1008
    }
1009 1010
    if (export_name) {
        nbd_export_set_name(exp, export_name);
1011
        nbd_export_set_description(exp, export_description);
1012
        newproto = true;
1013 1014 1015
    } else if (export_description) {
        error_report("Export description requires an export name");
        exit(EXIT_FAILURE);
1016
    }
1017

1018 1019 1020
    if (device) {
        int ret;

1021
        ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
1022
        if (ret != 0) {
1023 1024
            error_report("Failed to create client thread: %s", strerror(ret));
            exit(EXIT_FAILURE);
1025 1026 1027 1028 1029 1030
        }
    } else {
        /* Shut up GCC warnings.  */
        memset(&client_thread, 0, sizeof(client_thread));
    }

1031
    nbd_update_server_watch();
1032

1033 1034 1035
    /* now when the initialization is (almost) complete, chdir("/")
     * to free any busy filesystems */
    if (chdir("/") < 0) {
1036 1037 1038
        error_report("Could not chdir to root directory: %s",
                     strerror(errno));
        exit(EXIT_FAILURE);
1039 1040
    }

Max Reitz's avatar
Max Reitz committed
1041 1042 1043 1044 1045
    if (fork_process) {
        dup2(old_stderr, STDERR_FILENO);
        close(old_stderr);
    }

1046
    state = RUNNING;
1047
    do {
1048
        main_loop_wait(false);
1049 1050 1051 1052 1053 1054 1055
        if (state == TERMINATE) {
            state = TERMINATING;
            nbd_export_close(exp);
            nbd_export_put(exp);
            exp = NULL;
        }
    } while (state != TERMINATED);
1056

1057
    blk_unref(blk);
1058 1059 1060
    if (sockpath) {
        unlink(sockpath);
    }
1061

1062
    qemu_opts_del(sn_opts);
1063

1064 1065 1066 1067 1068 1069 1070
    if (device) {
        void *ret;
        pthread_join(client_thread, &ret);
        exit(ret != NULL);
    } else {
        exit(EXIT_SUCCESS);
    }
1071
}