Statistics
| Branch: | Revision:

root / qemu-nbd.c @ a61c6782

History | View | Annotate | Download (15 kB)

1 cd831bd7 ths
/*
2 7a5ca864 bellard
 *  Copyright (C) 2005  Anthony Liguori <anthony@codemonkey.ws>
3 7a5ca864 bellard
 *
4 7a5ca864 bellard
 *  Network Block Device
5 7a5ca864 bellard
 *
6 7a5ca864 bellard
 *  This program is free software; you can redistribute it and/or modify
7 7a5ca864 bellard
 *  it under the terms of the GNU General Public License as published by
8 7a5ca864 bellard
 *  the Free Software Foundation; under version 2 of the License.
9 7a5ca864 bellard
 *
10 7a5ca864 bellard
 *  This program is distributed in the hope that it will be useful,
11 7a5ca864 bellard
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12 7a5ca864 bellard
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 7a5ca864 bellard
 *  GNU General Public License for more details.
14 7a5ca864 bellard
 *
15 7a5ca864 bellard
 *  You should have received a copy of the GNU General Public License
16 8167ee88 Blue Swirl
 *  along with this program; if not, see <http://www.gnu.org/licenses/>.
17 7a5ca864 bellard
 */
18 7a5ca864 bellard
19 5a61cb60 Stefan Weil
#include "qemu-common.h"
20 7a5ca864 bellard
#include "block_int.h"
21 7a5ca864 bellard
#include "nbd.h"
22 7a5ca864 bellard
23 7a5ca864 bellard
#include <stdarg.h>
24 7a5ca864 bellard
#include <stdio.h>
25 7a5ca864 bellard
#include <getopt.h>
26 7a5ca864 bellard
#include <err.h>
27 cd831bd7 ths
#include <sys/types.h>
28 7a5ca864 bellard
#include <sys/socket.h>
29 7a5ca864 bellard
#include <netinet/in.h>
30 7a5ca864 bellard
#include <netinet/tcp.h>
31 7a5ca864 bellard
#include <arpa/inet.h>
32 cd831bd7 ths
#include <signal.h>
33 2bff4b6f Blue Swirl
#include <libgen.h>
34 a517e88b Paolo Bonzini
#include <pthread.h>
35 cd831bd7 ths
36 cd831bd7 ths
#define SOCKET_PATH    "/var/lock/qemu-nbd-%s"
37 7a5ca864 bellard
38 af49bbbe Paolo Bonzini
static NBDExport *exp;
39 b1d8e52e blueswir1
static int verbose;
40 a517e88b Paolo Bonzini
static char *device;
41 a517e88b Paolo Bonzini
static char *srcpath;
42 a517e88b Paolo Bonzini
static char *sockpath;
43 a61c6782 Paolo Bonzini
static bool sigterm_reported;
44 a61c6782 Paolo Bonzini
static bool nbd_started;
45 a61c6782 Paolo Bonzini
static int shared = 1;
46 a61c6782 Paolo Bonzini
static int nb_fds;
47 7a5ca864 bellard
48 7a5ca864 bellard
static void usage(const char *name)
49 7a5ca864 bellard
{
50 7a5ca864 bellard
    printf(
51 7a5ca864 bellard
"Usage: %s [OPTIONS] FILE\n"
52 7a5ca864 bellard
"QEMU Disk Network Block Device Server\n"
53 7a5ca864 bellard
"\n"
54 c2e2872b Laurent Vivier
"  -p, --port=PORT      port to listen on (default `%d')\n"
55 7a5ca864 bellard
"  -o, --offset=OFFSET  offset into the image\n"
56 7a5ca864 bellard
"  -b, --bind=IFACE     interface to bind to (default `0.0.0.0')\n"
57 cd831bd7 ths
"  -k, --socket=PATH    path to the unix socket\n"
58 cd831bd7 ths
"                       (default '"SOCKET_PATH"')\n"
59 7a5ca864 bellard
"  -r, --read-only      export read-only\n"
60 7a5ca864 bellard
"  -P, --partition=NUM  only expose partition NUM\n"
61 2f726488 ths
"  -s, --snapshot       use snapshot file\n"
62 2f726488 ths
"  -n, --nocache        disable host cache\n"
63 cd831bd7 ths
"  -c, --connect=DEV    connect FILE to the local NBD device DEV\n"
64 cd831bd7 ths
"  -d, --disconnect     disconnect the specified device\n"
65 3b05a8e9 ths
"  -e, --shared=NUM     device can be shared by NUM clients (default '1')\n"
66 75818250 ths
"  -t, --persistent     don't exit on the last connection\n"
67 7a5ca864 bellard
"  -v, --verbose        display extra debugging information\n"
68 7a5ca864 bellard
"  -h, --help           display this help and exit\n"
69 7a5ca864 bellard
"  -V, --version        output version information and exit\n"
70 7a5ca864 bellard
"\n"
71 7a5ca864 bellard
"Report bugs to <anthony@codemonkey.ws>\n"
72 c2e2872b Laurent Vivier
    , name, NBD_DEFAULT_PORT, "DEVICE");
73 7a5ca864 bellard
}
74 7a5ca864 bellard
75 7a5ca864 bellard
static void version(const char *name)
76 7a5ca864 bellard
{
77 7a5ca864 bellard
    printf(
78 315bc7aa ths
"%s version 0.0.1\n"
79 7a5ca864 bellard
"Written by Anthony Liguori.\n"
80 7a5ca864 bellard
"\n"
81 7a5ca864 bellard
"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
82 7a5ca864 bellard
"This is free software; see the source for copying conditions.  There is NO\n"
83 7a5ca864 bellard
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
84 315bc7aa ths
    , name);
85 7a5ca864 bellard
}
86 7a5ca864 bellard
87 7a5ca864 bellard
struct partition_record
88 7a5ca864 bellard
{
89 7a5ca864 bellard
    uint8_t bootable;
90 7a5ca864 bellard
    uint8_t start_head;
91 7a5ca864 bellard
    uint32_t start_cylinder;
92 7a5ca864 bellard
    uint8_t start_sector;
93 7a5ca864 bellard
    uint8_t system;
94 7a5ca864 bellard
    uint8_t end_head;
95 7a5ca864 bellard
    uint8_t end_cylinder;
96 7a5ca864 bellard
    uint8_t end_sector;
97 7a5ca864 bellard
    uint32_t start_sector_abs;
98 7a5ca864 bellard
    uint32_t nb_sectors_abs;
99 7a5ca864 bellard
};
100 7a5ca864 bellard
101 7a5ca864 bellard
static void read_partition(uint8_t *p, struct partition_record *r)
102 7a5ca864 bellard
{
103 7a5ca864 bellard
    r->bootable = p[0];
104 7a5ca864 bellard
    r->start_head = p[1];
105 7a5ca864 bellard
    r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
106 7a5ca864 bellard
    r->start_sector = p[2] & 0x3f;
107 7a5ca864 bellard
    r->system = p[4];
108 7a5ca864 bellard
    r->end_head = p[5];
109 7a5ca864 bellard
    r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
110 7a5ca864 bellard
    r->end_sector = p[6] & 0x3f;
111 7a5ca864 bellard
    r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
112 7a5ca864 bellard
    r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
113 7a5ca864 bellard
}
114 7a5ca864 bellard
115 7a5ca864 bellard
static int find_partition(BlockDriverState *bs, int partition,
116 7a5ca864 bellard
                          off_t *offset, off_t *size)
117 7a5ca864 bellard
{
118 7a5ca864 bellard
    struct partition_record mbr[4];
119 7a5ca864 bellard
    uint8_t data[512];
120 7a5ca864 bellard
    int i;
121 7a5ca864 bellard
    int ext_partnum = 4;
122 cb7cf0e3 Ryota Ozaki
    int ret;
123 7a5ca864 bellard
124 cb7cf0e3 Ryota Ozaki
    if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
125 cb7cf0e3 Ryota Ozaki
        errno = -ret;
126 cb7cf0e3 Ryota Ozaki
        err(EXIT_FAILURE, "error while reading");
127 cb7cf0e3 Ryota Ozaki
    }
128 7a5ca864 bellard
129 7a5ca864 bellard
    if (data[510] != 0x55 || data[511] != 0xaa) {
130 7a5ca864 bellard
        errno = -EINVAL;
131 7a5ca864 bellard
        return -1;
132 7a5ca864 bellard
    }
133 7a5ca864 bellard
134 7a5ca864 bellard
    for (i = 0; i < 4; i++) {
135 7a5ca864 bellard
        read_partition(&data[446 + 16 * i], &mbr[i]);
136 7a5ca864 bellard
137 7a5ca864 bellard
        if (!mbr[i].nb_sectors_abs)
138 7a5ca864 bellard
            continue;
139 7a5ca864 bellard
140 7a5ca864 bellard
        if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
141 7a5ca864 bellard
            struct partition_record ext[4];
142 7a5ca864 bellard
            uint8_t data1[512];
143 7a5ca864 bellard
            int j;
144 7a5ca864 bellard
145 cb7cf0e3 Ryota Ozaki
            if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
146 cb7cf0e3 Ryota Ozaki
                errno = -ret;
147 cb7cf0e3 Ryota Ozaki
                err(EXIT_FAILURE, "error while reading");
148 cb7cf0e3 Ryota Ozaki
            }
149 7a5ca864 bellard
150 7a5ca864 bellard
            for (j = 0; j < 4; j++) {
151 7a5ca864 bellard
                read_partition(&data1[446 + 16 * j], &ext[j]);
152 7a5ca864 bellard
                if (!ext[j].nb_sectors_abs)
153 7a5ca864 bellard
                    continue;
154 7a5ca864 bellard
155 7a5ca864 bellard
                if ((ext_partnum + j + 1) == partition) {
156 7a5ca864 bellard
                    *offset = (uint64_t)ext[j].start_sector_abs << 9;
157 7a5ca864 bellard
                    *size = (uint64_t)ext[j].nb_sectors_abs << 9;
158 7a5ca864 bellard
                    return 0;
159 7a5ca864 bellard
                }
160 7a5ca864 bellard
            }
161 7a5ca864 bellard
            ext_partnum += 4;
162 7a5ca864 bellard
        } else if ((i + 1) == partition) {
163 7a5ca864 bellard
            *offset = (uint64_t)mbr[i].start_sector_abs << 9;
164 7a5ca864 bellard
            *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
165 7a5ca864 bellard
            return 0;
166 7a5ca864 bellard
        }
167 7a5ca864 bellard
    }
168 7a5ca864 bellard
169 7a5ca864 bellard
    errno = -ENOENT;
170 7a5ca864 bellard
    return -1;
171 7a5ca864 bellard
}
172 7a5ca864 bellard
173 bb345110 Paolo Bonzini
static void termsig_handler(int signum)
174 bb345110 Paolo Bonzini
{
175 a61c6782 Paolo Bonzini
    sigterm_reported = true;
176 a61c6782 Paolo Bonzini
    qemu_notify_event();
177 bb345110 Paolo Bonzini
}
178 bb345110 Paolo Bonzini
179 a517e88b Paolo Bonzini
static void *show_parts(void *arg)
180 cd831bd7 ths
{
181 a517e88b Paolo Bonzini
    int nbd;
182 a517e88b Paolo Bonzini
183 a517e88b Paolo Bonzini
    /* linux just needs an open() to trigger
184 a517e88b Paolo Bonzini
     * the partition table update
185 a517e88b Paolo Bonzini
     * but remember to load the module with max_part != 0 :
186 a517e88b Paolo Bonzini
     *     modprobe nbd max_part=63
187 a517e88b Paolo Bonzini
     */
188 a517e88b Paolo Bonzini
    nbd = open(device, O_RDWR);
189 a517e88b Paolo Bonzini
    if (nbd != -1) {
190 a517e88b Paolo Bonzini
        close(nbd);
191 a517e88b Paolo Bonzini
    }
192 a517e88b Paolo Bonzini
    return NULL;
193 a517e88b Paolo Bonzini
}
194 cd831bd7 ths
195 a517e88b Paolo Bonzini
static void *nbd_client_thread(void *arg)
196 a517e88b Paolo Bonzini
{
197 a517e88b Paolo Bonzini
    int fd = *(int *)arg;
198 a517e88b Paolo Bonzini
    off_t size;
199 a517e88b Paolo Bonzini
    size_t blocksize;
200 a517e88b Paolo Bonzini
    uint32_t nbdflags;
201 a517e88b Paolo Bonzini
    int sock;
202 a517e88b Paolo Bonzini
    int ret;
203 a517e88b Paolo Bonzini
    pthread_t show_parts_thread;
204 a517e88b Paolo Bonzini
205 a517e88b Paolo Bonzini
    do {
206 a517e88b Paolo Bonzini
        sock = unix_socket_outgoing(sockpath);
207 a517e88b Paolo Bonzini
        if (sock == -1) {
208 f1ef5555 Paolo Bonzini
            goto out;
209 a517e88b Paolo Bonzini
        }
210 a517e88b Paolo Bonzini
    } while (sock == -1);
211 a517e88b Paolo Bonzini
212 a517e88b Paolo Bonzini
    ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
213 a517e88b Paolo Bonzini
                                &size, &blocksize);
214 a517e88b Paolo Bonzini
    if (ret == -1) {
215 a517e88b Paolo Bonzini
        goto out;
216 a517e88b Paolo Bonzini
    }
217 a517e88b Paolo Bonzini
218 a517e88b Paolo Bonzini
    ret = nbd_init(fd, sock, nbdflags, size, blocksize);
219 a517e88b Paolo Bonzini
    if (ret == -1) {
220 a517e88b Paolo Bonzini
        goto out;
221 a517e88b Paolo Bonzini
    }
222 a517e88b Paolo Bonzini
223 a517e88b Paolo Bonzini
    /* update partition table */
224 a517e88b Paolo Bonzini
    pthread_create(&show_parts_thread, NULL, show_parts, NULL);
225 a517e88b Paolo Bonzini
226 c1f8fdc3 Paolo Bonzini
    if (verbose) {
227 c1f8fdc3 Paolo Bonzini
        fprintf(stderr, "NBD device %s is now connected to %s\n",
228 c1f8fdc3 Paolo Bonzini
                device, srcpath);
229 c1f8fdc3 Paolo Bonzini
    } else {
230 c1f8fdc3 Paolo Bonzini
        /* Close stderr so that the qemu-nbd process exits.  */
231 c1f8fdc3 Paolo Bonzini
        dup2(STDOUT_FILENO, STDERR_FILENO);
232 c1f8fdc3 Paolo Bonzini
    }
233 a517e88b Paolo Bonzini
234 a517e88b Paolo Bonzini
    ret = nbd_client(fd);
235 a517e88b Paolo Bonzini
    if (ret) {
236 a517e88b Paolo Bonzini
        goto out;
237 cd831bd7 ths
    }
238 a517e88b Paolo Bonzini
    close(fd);
239 a517e88b Paolo Bonzini
    kill(getpid(), SIGTERM);
240 a517e88b Paolo Bonzini
    return (void *) EXIT_SUCCESS;
241 a517e88b Paolo Bonzini
242 a517e88b Paolo Bonzini
out:
243 a517e88b Paolo Bonzini
    kill(getpid(), SIGTERM);
244 a517e88b Paolo Bonzini
    return (void *) EXIT_FAILURE;
245 cd831bd7 ths
}
246 cd831bd7 ths
247 a61c6782 Paolo Bonzini
static int nbd_can_accept(void *opaque)
248 a61c6782 Paolo Bonzini
{
249 a61c6782 Paolo Bonzini
    return nb_fds < shared;
250 a61c6782 Paolo Bonzini
}
251 a61c6782 Paolo Bonzini
252 a61c6782 Paolo Bonzini
static void nbd_read(void *opaque)
253 a61c6782 Paolo Bonzini
{
254 a61c6782 Paolo Bonzini
    int fd = (uintptr_t) opaque;
255 a61c6782 Paolo Bonzini
256 a61c6782 Paolo Bonzini
    if (nbd_trip(exp, fd) != 0) {
257 a61c6782 Paolo Bonzini
        qemu_set_fd_handler2(fd, NULL, NULL, NULL, NULL);
258 a61c6782 Paolo Bonzini
        close(fd);
259 a61c6782 Paolo Bonzini
        nb_fds--;
260 a61c6782 Paolo Bonzini
    }
261 a61c6782 Paolo Bonzini
}
262 a61c6782 Paolo Bonzini
263 a61c6782 Paolo Bonzini
static void nbd_accept(void *opaque)
264 a61c6782 Paolo Bonzini
{
265 a61c6782 Paolo Bonzini
    int server_fd = (uintptr_t) opaque;
266 a61c6782 Paolo Bonzini
    struct sockaddr_in addr;
267 a61c6782 Paolo Bonzini
    socklen_t addr_len = sizeof(addr);
268 a61c6782 Paolo Bonzini
269 a61c6782 Paolo Bonzini
    int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
270 a61c6782 Paolo Bonzini
    nbd_started = true;
271 a61c6782 Paolo Bonzini
    if (fd != -1 && nbd_negotiate(exp, fd) != -1) {
272 a61c6782 Paolo Bonzini
        qemu_set_fd_handler2(fd, NULL, nbd_read, NULL, (void *) (intptr_t) fd);
273 a61c6782 Paolo Bonzini
        nb_fds++;
274 a61c6782 Paolo Bonzini
    }
275 a61c6782 Paolo Bonzini
}
276 a61c6782 Paolo Bonzini
277 7a5ca864 bellard
int main(int argc, char **argv)
278 7a5ca864 bellard
{
279 7a5ca864 bellard
    BlockDriverState *bs;
280 7a5ca864 bellard
    off_t dev_offset = 0;
281 b90fb4b8 Paolo Bonzini
    uint32_t nbdflags = 0;
282 cd831bd7 ths
    bool disconnect = false;
283 7a5ca864 bellard
    const char *bindto = "0.0.0.0";
284 c2e2872b Laurent Vivier
    int port = NBD_DEFAULT_PORT;
285 7a5ca864 bellard
    off_t fd_size;
286 d6aa671f aliguori
    const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
287 7a5ca864 bellard
    struct option lopt[] = {
288 660f11be Blue Swirl
        { "help", 0, NULL, 'h' },
289 660f11be Blue Swirl
        { "version", 0, NULL, 'V' },
290 660f11be Blue Swirl
        { "bind", 1, NULL, 'b' },
291 660f11be Blue Swirl
        { "port", 1, NULL, 'p' },
292 660f11be Blue Swirl
        { "socket", 1, NULL, 'k' },
293 660f11be Blue Swirl
        { "offset", 1, NULL, 'o' },
294 660f11be Blue Swirl
        { "read-only", 0, NULL, 'r' },
295 660f11be Blue Swirl
        { "partition", 1, NULL, 'P' },
296 660f11be Blue Swirl
        { "connect", 1, NULL, 'c' },
297 660f11be Blue Swirl
        { "disconnect", 0, NULL, 'd' },
298 660f11be Blue Swirl
        { "snapshot", 0, NULL, 's' },
299 660f11be Blue Swirl
        { "nocache", 0, NULL, 'n' },
300 660f11be Blue Swirl
        { "shared", 1, NULL, 'e' },
301 660f11be Blue Swirl
        { "persistent", 0, NULL, 't' },
302 660f11be Blue Swirl
        { "verbose", 0, NULL, 'v' },
303 660f11be Blue Swirl
        { NULL, 0, NULL, 0 }
304 7a5ca864 bellard
    };
305 7a5ca864 bellard
    int ch;
306 7a5ca864 bellard
    int opt_ind = 0;
307 7a5ca864 bellard
    int li;
308 7a5ca864 bellard
    char *end;
309 f5edb014 Naphtali Sprei
    int flags = BDRV_O_RDWR;
310 7a5ca864 bellard
    int partition = -1;
311 cd831bd7 ths
    int ret;
312 3b05a8e9 ths
    int fd;
313 75818250 ths
    int persistent = 0;
314 a517e88b Paolo Bonzini
    pthread_t client_thread;
315 7a5ca864 bellard
316 a517e88b Paolo Bonzini
    /* The client thread uses SIGTERM to interrupt the server.  A signal
317 a517e88b Paolo Bonzini
     * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
318 a517e88b Paolo Bonzini
     */
319 bb345110 Paolo Bonzini
    struct sigaction sa_sigterm;
320 bb345110 Paolo Bonzini
    memset(&sa_sigterm, 0, sizeof(sa_sigterm));
321 bb345110 Paolo Bonzini
    sa_sigterm.sa_handler = termsig_handler;
322 bb345110 Paolo Bonzini
    sigaction(SIGTERM, &sa_sigterm, NULL);
323 bb345110 Paolo Bonzini
324 7a5ca864 bellard
    while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
325 7a5ca864 bellard
        switch (ch) {
326 7a5ca864 bellard
        case 's':
327 2f726488 ths
            flags |= BDRV_O_SNAPSHOT;
328 2f726488 ths
            break;
329 2f726488 ths
        case 'n':
330 a6599793 Christoph Hellwig
            flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
331 7a5ca864 bellard
            break;
332 7a5ca864 bellard
        case 'b':
333 7a5ca864 bellard
            bindto = optarg;
334 7a5ca864 bellard
            break;
335 7a5ca864 bellard
        case 'p':
336 7a5ca864 bellard
            li = strtol(optarg, &end, 0);
337 7a5ca864 bellard
            if (*end) {
338 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
339 7a5ca864 bellard
            }
340 7a5ca864 bellard
            if (li < 1 || li > 65535) {
341 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
342 7a5ca864 bellard
            }
343 7a5ca864 bellard
            port = (uint16_t)li;
344 7a5ca864 bellard
            break;
345 7a5ca864 bellard
        case 'o':
346 7a5ca864 bellard
                dev_offset = strtoll (optarg, &end, 0);
347 7a5ca864 bellard
            if (*end) {
348 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
349 7a5ca864 bellard
            }
350 7a5ca864 bellard
            if (dev_offset < 0) {
351 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
352 7a5ca864 bellard
            }
353 7a5ca864 bellard
            break;
354 7a5ca864 bellard
        case 'r':
355 b90fb4b8 Paolo Bonzini
            nbdflags |= NBD_FLAG_READ_ONLY;
356 07108b29 Naphtali Sprei
            flags &= ~BDRV_O_RDWR;
357 7a5ca864 bellard
            break;
358 7a5ca864 bellard
        case 'P':
359 7a5ca864 bellard
            partition = strtol(optarg, &end, 0);
360 7a5ca864 bellard
            if (*end)
361 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
362 7a5ca864 bellard
            if (partition < 1 || partition > 8)
363 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid partition %d", partition);
364 7a5ca864 bellard
            break;
365 cd831bd7 ths
        case 'k':
366 b32f6c28 Paolo Bonzini
            sockpath = optarg;
367 b32f6c28 Paolo Bonzini
            if (sockpath[0] != '/')
368 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "socket path must be absolute\n");
369 cd831bd7 ths
            break;
370 cd831bd7 ths
        case 'd':
371 cd831bd7 ths
            disconnect = true;
372 cd831bd7 ths
            break;
373 cd831bd7 ths
        case 'c':
374 cd831bd7 ths
            device = optarg;
375 cd831bd7 ths
            break;
376 3b05a8e9 ths
        case 'e':
377 3b05a8e9 ths
            shared = strtol(optarg, &end, 0);
378 3b05a8e9 ths
            if (*end) {
379 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
380 3b05a8e9 ths
            }
381 3b05a8e9 ths
            if (shared < 1) {
382 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
383 3b05a8e9 ths
            }
384 3b05a8e9 ths
            break;
385 75818250 ths
        case 't':
386 75818250 ths
            persistent = 1;
387 75818250 ths
            break;
388 7a5ca864 bellard
        case 'v':
389 7a5ca864 bellard
            verbose = 1;
390 7a5ca864 bellard
            break;
391 7a5ca864 bellard
        case 'V':
392 7a5ca864 bellard
            version(argv[0]);
393 7a5ca864 bellard
            exit(0);
394 7a5ca864 bellard
            break;
395 7a5ca864 bellard
        case 'h':
396 7a5ca864 bellard
            usage(argv[0]);
397 7a5ca864 bellard
            exit(0);
398 7a5ca864 bellard
            break;
399 7a5ca864 bellard
        case '?':
400 b6353bea Ryota Ozaki
            errx(EXIT_FAILURE, "Try `%s --help' for more information.",
401 7a5ca864 bellard
                 argv[0]);
402 7a5ca864 bellard
        }
403 7a5ca864 bellard
    }
404 7a5ca864 bellard
405 7a5ca864 bellard
    if ((argc - optind) != 1) {
406 b6353bea Ryota Ozaki
        errx(EXIT_FAILURE, "Invalid number of argument.\n"
407 7a5ca864 bellard
             "Try `%s --help' for more information.",
408 7a5ca864 bellard
             argv[0]);
409 7a5ca864 bellard
    }
410 7a5ca864 bellard
411 cd831bd7 ths
    if (disconnect) {
412 cd831bd7 ths
        fd = open(argv[optind], O_RDWR);
413 cd831bd7 ths
        if (fd == -1)
414 cb7cf0e3 Ryota Ozaki
            err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
415 cd831bd7 ths
416 cd831bd7 ths
        nbd_disconnect(fd);
417 cd831bd7 ths
418 cd831bd7 ths
        close(fd);
419 cd831bd7 ths
420 cd831bd7 ths
        printf("%s disconnected\n", argv[optind]);
421 cd831bd7 ths
422 cd831bd7 ths
        return 0;
423 cd831bd7 ths
    }
424 cd831bd7 ths
425 c1f8fdc3 Paolo Bonzini
    if (device && !verbose) {
426 c1f8fdc3 Paolo Bonzini
        int stderr_fd[2];
427 c1f8fdc3 Paolo Bonzini
        pid_t pid;
428 c1f8fdc3 Paolo Bonzini
        int ret;
429 c1f8fdc3 Paolo Bonzini
430 c1f8fdc3 Paolo Bonzini
        if (qemu_pipe(stderr_fd) == -1) {
431 c1f8fdc3 Paolo Bonzini
            err(EXIT_FAILURE, "Error setting up communication pipe");
432 c1f8fdc3 Paolo Bonzini
        }
433 c1f8fdc3 Paolo Bonzini
434 c1f8fdc3 Paolo Bonzini
        /* Now daemonize, but keep a communication channel open to
435 c1f8fdc3 Paolo Bonzini
         * print errors and exit with the proper status code.
436 c1f8fdc3 Paolo Bonzini
         */
437 c1f8fdc3 Paolo Bonzini
        pid = fork();
438 c1f8fdc3 Paolo Bonzini
        if (pid == 0) {
439 c1f8fdc3 Paolo Bonzini
            close(stderr_fd[0]);
440 c1f8fdc3 Paolo Bonzini
            ret = qemu_daemon(0, 0);
441 c1f8fdc3 Paolo Bonzini
442 c1f8fdc3 Paolo Bonzini
            /* Temporarily redirect stderr to the parent's pipe...  */
443 c1f8fdc3 Paolo Bonzini
            dup2(stderr_fd[1], STDERR_FILENO);
444 c1f8fdc3 Paolo Bonzini
            if (ret == -1) {
445 c1f8fdc3 Paolo Bonzini
                err(EXIT_FAILURE, "Failed to daemonize");
446 c1f8fdc3 Paolo Bonzini
            }
447 c1f8fdc3 Paolo Bonzini
448 c1f8fdc3 Paolo Bonzini
            /* ... close the descriptor we inherited and go on.  */
449 c1f8fdc3 Paolo Bonzini
            close(stderr_fd[1]);
450 c1f8fdc3 Paolo Bonzini
        } else {
451 c1f8fdc3 Paolo Bonzini
            bool errors = false;
452 c1f8fdc3 Paolo Bonzini
            char *buf;
453 c1f8fdc3 Paolo Bonzini
454 c1f8fdc3 Paolo Bonzini
            /* In the parent.  Print error messages from the child until
455 c1f8fdc3 Paolo Bonzini
             * it closes the pipe.
456 c1f8fdc3 Paolo Bonzini
             */
457 c1f8fdc3 Paolo Bonzini
            close(stderr_fd[1]);
458 c1f8fdc3 Paolo Bonzini
            buf = g_malloc(1024);
459 c1f8fdc3 Paolo Bonzini
            while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
460 c1f8fdc3 Paolo Bonzini
                errors = true;
461 c1f8fdc3 Paolo Bonzini
                ret = qemu_write_full(STDERR_FILENO, buf, ret);
462 c1f8fdc3 Paolo Bonzini
                if (ret == -1) {
463 c1f8fdc3 Paolo Bonzini
                    exit(EXIT_FAILURE);
464 c1f8fdc3 Paolo Bonzini
                }
465 c1f8fdc3 Paolo Bonzini
            }
466 c1f8fdc3 Paolo Bonzini
            if (ret == -1) {
467 c1f8fdc3 Paolo Bonzini
                err(EXIT_FAILURE, "Cannot read from daemon");
468 c1f8fdc3 Paolo Bonzini
            }
469 c1f8fdc3 Paolo Bonzini
470 c1f8fdc3 Paolo Bonzini
            /* Usually the daemon should not print any message.
471 c1f8fdc3 Paolo Bonzini
             * Exit with zero status in that case.
472 c1f8fdc3 Paolo Bonzini
             */
473 c1f8fdc3 Paolo Bonzini
            exit(errors);
474 c1f8fdc3 Paolo Bonzini
        }
475 c1f8fdc3 Paolo Bonzini
    }
476 c1f8fdc3 Paolo Bonzini
477 cd831bd7 ths
    if (device) {
478 a517e88b Paolo Bonzini
        /* Open before spawning new threads.  In the future, we may
479 a517e88b Paolo Bonzini
         * drop privileges after opening.
480 a517e88b Paolo Bonzini
         */
481 a517e88b Paolo Bonzini
        fd = open(device, O_RDWR);
482 a517e88b Paolo Bonzini
        if (fd == -1) {
483 a517e88b Paolo Bonzini
            err(EXIT_FAILURE, "Failed to open %s", device);
484 cb7cf0e3 Ryota Ozaki
        }
485 cb7cf0e3 Ryota Ozaki
486 b32f6c28 Paolo Bonzini
        if (sockpath == NULL) {
487 b32f6c28 Paolo Bonzini
            sockpath = g_malloc(128);
488 b32f6c28 Paolo Bonzini
            snprintf(sockpath, 128, SOCKET_PATH, basename(device));
489 cd831bd7 ths
        }
490 cd831bd7 ths
    }
491 cd831bd7 ths
492 802ddc37 Paolo Bonzini
    bdrv_init();
493 802ddc37 Paolo Bonzini
    atexit(bdrv_close_all);
494 802ddc37 Paolo Bonzini
495 802ddc37 Paolo Bonzini
    bs = bdrv_new("hda");
496 802ddc37 Paolo Bonzini
    srcpath = argv[optind];
497 802ddc37 Paolo Bonzini
    if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) {
498 802ddc37 Paolo Bonzini
        errno = -ret;
499 802ddc37 Paolo Bonzini
        err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
500 802ddc37 Paolo Bonzini
    }
501 802ddc37 Paolo Bonzini
502 802ddc37 Paolo Bonzini
    fd_size = bs->total_sectors * 512;
503 802ddc37 Paolo Bonzini
504 802ddc37 Paolo Bonzini
    if (partition != -1 &&
505 802ddc37 Paolo Bonzini
        find_partition(bs, partition, &dev_offset, &fd_size)) {
506 802ddc37 Paolo Bonzini
        err(EXIT_FAILURE, "Could not find partition %d", partition);
507 802ddc37 Paolo Bonzini
    }
508 802ddc37 Paolo Bonzini
509 af49bbbe Paolo Bonzini
    exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags);
510 3b05a8e9 ths
511 b32f6c28 Paolo Bonzini
    if (sockpath) {
512 a61c6782 Paolo Bonzini
        fd = unix_socket_incoming(sockpath);
513 cd831bd7 ths
    } else {
514 a61c6782 Paolo Bonzini
        fd = tcp_socket_incoming(bindto, port);
515 cd831bd7 ths
    }
516 cd831bd7 ths
517 a61c6782 Paolo Bonzini
    if (fd == -1) {
518 7a5ca864 bellard
        return 1;
519 a61c6782 Paolo Bonzini
    }
520 f1ef5555 Paolo Bonzini
521 f1ef5555 Paolo Bonzini
    if (device) {
522 f1ef5555 Paolo Bonzini
        int ret;
523 f1ef5555 Paolo Bonzini
524 f1ef5555 Paolo Bonzini
        ret = pthread_create(&client_thread, NULL, nbd_client_thread, &fd);
525 f1ef5555 Paolo Bonzini
        if (ret != 0) {
526 f1ef5555 Paolo Bonzini
            errx(EXIT_FAILURE, "Failed to create client thread: %s",
527 f1ef5555 Paolo Bonzini
                 strerror(ret));
528 f1ef5555 Paolo Bonzini
        }
529 f1ef5555 Paolo Bonzini
    } else {
530 f1ef5555 Paolo Bonzini
        /* Shut up GCC warnings.  */
531 f1ef5555 Paolo Bonzini
        memset(&client_thread, 0, sizeof(client_thread));
532 f1ef5555 Paolo Bonzini
    }
533 f1ef5555 Paolo Bonzini
534 a61c6782 Paolo Bonzini
    qemu_init_main_loop();
535 a61c6782 Paolo Bonzini
    qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
536 a61c6782 Paolo Bonzini
                         (void *)(uintptr_t)fd);
537 7a5ca864 bellard
538 3b05a8e9 ths
    do {
539 a61c6782 Paolo Bonzini
        main_loop_wait(false);
540 a61c6782 Paolo Bonzini
    } while (!sigterm_reported && (persistent || !nbd_started || nb_fds > 0));
541 7a5ca864 bellard
542 af49bbbe Paolo Bonzini
    nbd_export_close(exp);
543 b32f6c28 Paolo Bonzini
    if (sockpath) {
544 b32f6c28 Paolo Bonzini
        unlink(sockpath);
545 b32f6c28 Paolo Bonzini
    }
546 7a5ca864 bellard
547 a517e88b Paolo Bonzini
    if (device) {
548 a517e88b Paolo Bonzini
        void *ret;
549 a517e88b Paolo Bonzini
        pthread_join(client_thread, &ret);
550 a517e88b Paolo Bonzini
        exit(ret != NULL);
551 a517e88b Paolo Bonzini
    } else {
552 a517e88b Paolo Bonzini
        exit(EXIT_SUCCESS);
553 a517e88b Paolo Bonzini
    }
554 7a5ca864 bellard
}