Statistics
| Branch: | Revision:

root / qemu-nbd.c @ 0834c9ea

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