Statistics
| Branch: | Revision:

root / qemu-nbd.c @ feature-archipelago

History | View | Annotate | Download (20.7 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 737e150e Paolo Bonzini
#include "block/block.h"
21 737e150e Paolo Bonzini
#include "block/nbd.h"
22 6a1751b7 Alex Bligh
#include "qemu/main-loop.h"
23 537b41f5 Paolo Bonzini
#include "qemu/sockets.h"
24 537b41f5 Paolo Bonzini
#include "qemu/error-report.h"
25 8c116b0e Wenchao Xia
#include "block/snapshot.h"
26 7a5ca864 bellard
27 7a5ca864 bellard
#include <stdarg.h>
28 7a5ca864 bellard
#include <stdio.h>
29 7a5ca864 bellard
#include <getopt.h>
30 7a5ca864 bellard
#include <err.h>
31 cd831bd7 ths
#include <sys/types.h>
32 7a5ca864 bellard
#include <sys/socket.h>
33 7a5ca864 bellard
#include <netinet/in.h>
34 7a5ca864 bellard
#include <netinet/tcp.h>
35 7a5ca864 bellard
#include <arpa/inet.h>
36 cd831bd7 ths
#include <signal.h>
37 2bff4b6f Blue Swirl
#include <libgen.h>
38 a517e88b Paolo Bonzini
#include <pthread.h>
39 cd831bd7 ths
40 ded9d2d5 Paolo Bonzini
#define SOCKET_PATH          "/var/lock/qemu-nbd-%s"
41 ded9d2d5 Paolo Bonzini
#define QEMU_NBD_OPT_CACHE   1
42 ded9d2d5 Paolo Bonzini
#define QEMU_NBD_OPT_AIO     2
43 ded9d2d5 Paolo Bonzini
#define QEMU_NBD_OPT_DISCARD 3
44 7a5ca864 bellard
45 af49bbbe Paolo Bonzini
static NBDExport *exp;
46 b1d8e52e blueswir1
static int verbose;
47 a517e88b Paolo Bonzini
static char *srcpath;
48 a517e88b Paolo Bonzini
static char *sockpath;
49 7860a380 Paolo Bonzini
static int persistent = 0;
50 7860a380 Paolo Bonzini
static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
51 a61c6782 Paolo Bonzini
static int shared = 1;
52 a61c6782 Paolo Bonzini
static int nb_fds;
53 7a5ca864 bellard
54 7a5ca864 bellard
static void usage(const char *name)
55 7a5ca864 bellard
{
56 b033cd86 Paolo Bonzini
    (printf) (
57 7a5ca864 bellard
"Usage: %s [OPTIONS] FILE\n"
58 7a5ca864 bellard
"QEMU Disk Network Block Device Server\n"
59 7a5ca864 bellard
"\n"
60 b033cd86 Paolo Bonzini
"  -h, --help           display this help and exit\n"
61 b033cd86 Paolo Bonzini
"  -V, --version        output version information and exit\n"
62 b033cd86 Paolo Bonzini
"\n"
63 b033cd86 Paolo Bonzini
"Connection properties:\n"
64 c2e2872b Laurent Vivier
"  -p, --port=PORT      port to listen on (default `%d')\n"
65 7a5ca864 bellard
"  -b, --bind=IFACE     interface to bind to (default `0.0.0.0')\n"
66 cd831bd7 ths
"  -k, --socket=PATH    path to the unix socket\n"
67 cd831bd7 ths
"                       (default '"SOCKET_PATH"')\n"
68 3b05a8e9 ths
"  -e, --shared=NUM     device can be shared by NUM clients (default '1')\n"
69 75818250 ths
"  -t, --persistent     don't exit on the last connection\n"
70 7a5ca864 bellard
"  -v, --verbose        display extra debugging information\n"
71 7a5ca864 bellard
"\n"
72 b033cd86 Paolo Bonzini
"Exposing part of the image:\n"
73 b033cd86 Paolo Bonzini
"  -o, --offset=OFFSET  offset into the image\n"
74 b033cd86 Paolo Bonzini
"  -P, --partition=NUM  only expose partition NUM\n"
75 b033cd86 Paolo Bonzini
"\n"
76 b033cd86 Paolo Bonzini
#ifdef __linux__
77 b033cd86 Paolo Bonzini
"Kernel NBD client support:\n"
78 b033cd86 Paolo Bonzini
"  -c, --connect=DEV    connect FILE to the local NBD device DEV\n"
79 b033cd86 Paolo Bonzini
"  -d, --disconnect     disconnect the specified device\n"
80 b033cd86 Paolo Bonzini
"\n"
81 b033cd86 Paolo Bonzini
#endif
82 b033cd86 Paolo Bonzini
"\n"
83 b033cd86 Paolo Bonzini
"Block device options:\n"
84 4323fdcf Wenchao Xia
"  -f, --format=FORMAT  set image format (raw, qcow2, ...)\n"
85 b033cd86 Paolo Bonzini
"  -r, --read-only      export read-only\n"
86 8c116b0e Wenchao Xia
"  -s, --snapshot       use FILE as an external snapshot, create a temporary\n"
87 8c116b0e Wenchao Xia
"                       file with backing_file=FILE, redirect the write to\n"
88 8c116b0e Wenchao Xia
"                       the temporary one\n"
89 8c116b0e Wenchao Xia
"  -l, --load-snapshot=SNAPSHOT_PARAM\n"
90 8c116b0e Wenchao Xia
"                       load an internal snapshot inside FILE and export it\n"
91 8c116b0e Wenchao Xia
"                       as an read-only device, SNAPSHOT_PARAM format is\n"
92 8c116b0e Wenchao Xia
"                       'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
93 8c116b0e Wenchao Xia
"                       '[ID_OR_NAME]'\n"
94 b033cd86 Paolo Bonzini
"  -n, --nocache        disable host cache\n"
95 39a5235c Paolo Bonzini
"      --cache=MODE     set cache mode (none, writeback, ...)\n"
96 39a5235c Paolo Bonzini
#ifdef CONFIG_LINUX_AIO
97 39a5235c Paolo Bonzini
"      --aio=MODE       set AIO mode (native or threads)\n"
98 39a5235c Paolo Bonzini
#endif
99 b033cd86 Paolo Bonzini
"\n"
100 b033cd86 Paolo Bonzini
"Report bugs to <qemu-devel@nongnu.org>\n"
101 c2e2872b Laurent Vivier
    , name, NBD_DEFAULT_PORT, "DEVICE");
102 7a5ca864 bellard
}
103 7a5ca864 bellard
104 7a5ca864 bellard
static void version(const char *name)
105 7a5ca864 bellard
{
106 7a5ca864 bellard
    printf(
107 315bc7aa ths
"%s version 0.0.1\n"
108 7a5ca864 bellard
"Written by Anthony Liguori.\n"
109 7a5ca864 bellard
"\n"
110 7a5ca864 bellard
"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
111 7a5ca864 bellard
"This is free software; see the source for copying conditions.  There is NO\n"
112 7a5ca864 bellard
"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
113 315bc7aa ths
    , name);
114 7a5ca864 bellard
}
115 7a5ca864 bellard
116 7a5ca864 bellard
struct partition_record
117 7a5ca864 bellard
{
118 7a5ca864 bellard
    uint8_t bootable;
119 7a5ca864 bellard
    uint8_t start_head;
120 7a5ca864 bellard
    uint32_t start_cylinder;
121 7a5ca864 bellard
    uint8_t start_sector;
122 7a5ca864 bellard
    uint8_t system;
123 7a5ca864 bellard
    uint8_t end_head;
124 7a5ca864 bellard
    uint8_t end_cylinder;
125 7a5ca864 bellard
    uint8_t end_sector;
126 7a5ca864 bellard
    uint32_t start_sector_abs;
127 7a5ca864 bellard
    uint32_t nb_sectors_abs;
128 7a5ca864 bellard
};
129 7a5ca864 bellard
130 7a5ca864 bellard
static void read_partition(uint8_t *p, struct partition_record *r)
131 7a5ca864 bellard
{
132 7a5ca864 bellard
    r->bootable = p[0];
133 7a5ca864 bellard
    r->start_head = p[1];
134 7a5ca864 bellard
    r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
135 7a5ca864 bellard
    r->start_sector = p[2] & 0x3f;
136 7a5ca864 bellard
    r->system = p[4];
137 7a5ca864 bellard
    r->end_head = p[5];
138 7a5ca864 bellard
    r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
139 7a5ca864 bellard
    r->end_sector = p[6] & 0x3f;
140 7a5ca864 bellard
    r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
141 7a5ca864 bellard
    r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
142 7a5ca864 bellard
}
143 7a5ca864 bellard
144 7a5ca864 bellard
static int find_partition(BlockDriverState *bs, int partition,
145 7a5ca864 bellard
                          off_t *offset, off_t *size)
146 7a5ca864 bellard
{
147 7a5ca864 bellard
    struct partition_record mbr[4];
148 7a5ca864 bellard
    uint8_t data[512];
149 7a5ca864 bellard
    int i;
150 7a5ca864 bellard
    int ext_partnum = 4;
151 cb7cf0e3 Ryota Ozaki
    int ret;
152 7a5ca864 bellard
153 cb7cf0e3 Ryota Ozaki
    if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
154 cb7cf0e3 Ryota Ozaki
        errno = -ret;
155 cb7cf0e3 Ryota Ozaki
        err(EXIT_FAILURE, "error while reading");
156 cb7cf0e3 Ryota Ozaki
    }
157 7a5ca864 bellard
158 7a5ca864 bellard
    if (data[510] != 0x55 || data[511] != 0xaa) {
159 185b4338 Paolo Bonzini
        return -EINVAL;
160 7a5ca864 bellard
    }
161 7a5ca864 bellard
162 7a5ca864 bellard
    for (i = 0; i < 4; i++) {
163 7a5ca864 bellard
        read_partition(&data[446 + 16 * i], &mbr[i]);
164 7a5ca864 bellard
165 7a5ca864 bellard
        if (!mbr[i].nb_sectors_abs)
166 7a5ca864 bellard
            continue;
167 7a5ca864 bellard
168 7a5ca864 bellard
        if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
169 7a5ca864 bellard
            struct partition_record ext[4];
170 7a5ca864 bellard
            uint8_t data1[512];
171 7a5ca864 bellard
            int j;
172 7a5ca864 bellard
173 cb7cf0e3 Ryota Ozaki
            if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
174 cb7cf0e3 Ryota Ozaki
                errno = -ret;
175 cb7cf0e3 Ryota Ozaki
                err(EXIT_FAILURE, "error while reading");
176 cb7cf0e3 Ryota Ozaki
            }
177 7a5ca864 bellard
178 7a5ca864 bellard
            for (j = 0; j < 4; j++) {
179 7a5ca864 bellard
                read_partition(&data1[446 + 16 * j], &ext[j]);
180 7a5ca864 bellard
                if (!ext[j].nb_sectors_abs)
181 7a5ca864 bellard
                    continue;
182 7a5ca864 bellard
183 7a5ca864 bellard
                if ((ext_partnum + j + 1) == partition) {
184 7a5ca864 bellard
                    *offset = (uint64_t)ext[j].start_sector_abs << 9;
185 7a5ca864 bellard
                    *size = (uint64_t)ext[j].nb_sectors_abs << 9;
186 7a5ca864 bellard
                    return 0;
187 7a5ca864 bellard
                }
188 7a5ca864 bellard
            }
189 7a5ca864 bellard
            ext_partnum += 4;
190 7a5ca864 bellard
        } else if ((i + 1) == partition) {
191 7a5ca864 bellard
            *offset = (uint64_t)mbr[i].start_sector_abs << 9;
192 7a5ca864 bellard
            *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
193 7a5ca864 bellard
            return 0;
194 7a5ca864 bellard
        }
195 7a5ca864 bellard
    }
196 7a5ca864 bellard
197 185b4338 Paolo Bonzini
    return -ENOENT;
198 7a5ca864 bellard
}
199 7a5ca864 bellard
200 bb345110 Paolo Bonzini
static void termsig_handler(int signum)
201 bb345110 Paolo Bonzini
{
202 7860a380 Paolo Bonzini
    state = TERMINATE;
203 a61c6782 Paolo Bonzini
    qemu_notify_event();
204 bb345110 Paolo Bonzini
}
205 bb345110 Paolo Bonzini
206 537b41f5 Paolo Bonzini
static void combine_addr(char *buf, size_t len, const char* address,
207 537b41f5 Paolo Bonzini
                         uint16_t port)
208 537b41f5 Paolo Bonzini
{
209 537b41f5 Paolo Bonzini
    /* If the address-part contains a colon, it's an IPv6 IP so needs [] */
210 537b41f5 Paolo Bonzini
    if (strstr(address, ":")) {
211 537b41f5 Paolo Bonzini
        snprintf(buf, len, "[%s]:%u", address, port);
212 537b41f5 Paolo Bonzini
    } else {
213 537b41f5 Paolo Bonzini
        snprintf(buf, len, "%s:%u", address, port);
214 537b41f5 Paolo Bonzini
    }
215 537b41f5 Paolo Bonzini
}
216 537b41f5 Paolo Bonzini
217 537b41f5 Paolo Bonzini
static int tcp_socket_incoming(const char *address, uint16_t port)
218 537b41f5 Paolo Bonzini
{
219 537b41f5 Paolo Bonzini
    char address_and_port[128];
220 537b41f5 Paolo Bonzini
    Error *local_err = NULL;
221 537b41f5 Paolo Bonzini
222 537b41f5 Paolo Bonzini
    combine_addr(address_and_port, 128, address, port);
223 537b41f5 Paolo Bonzini
    int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err);
224 537b41f5 Paolo Bonzini
225 537b41f5 Paolo Bonzini
    if (local_err != NULL) {
226 537b41f5 Paolo Bonzini
        qerror_report_err(local_err);
227 537b41f5 Paolo Bonzini
        error_free(local_err);
228 537b41f5 Paolo Bonzini
    }
229 537b41f5 Paolo Bonzini
    return fd;
230 537b41f5 Paolo Bonzini
}
231 537b41f5 Paolo Bonzini
232 537b41f5 Paolo Bonzini
static int unix_socket_incoming(const char *path)
233 537b41f5 Paolo Bonzini
{
234 537b41f5 Paolo Bonzini
    Error *local_err = NULL;
235 537b41f5 Paolo Bonzini
    int fd = unix_listen(path, NULL, 0, &local_err);
236 537b41f5 Paolo Bonzini
237 537b41f5 Paolo Bonzini
    if (local_err != NULL) {
238 537b41f5 Paolo Bonzini
        qerror_report_err(local_err);
239 537b41f5 Paolo Bonzini
        error_free(local_err);
240 537b41f5 Paolo Bonzini
    }
241 537b41f5 Paolo Bonzini
    return fd;
242 537b41f5 Paolo Bonzini
}
243 537b41f5 Paolo Bonzini
244 537b41f5 Paolo Bonzini
static int unix_socket_outgoing(const char *path)
245 537b41f5 Paolo Bonzini
{
246 537b41f5 Paolo Bonzini
    Error *local_err = NULL;
247 537b41f5 Paolo Bonzini
    int fd = unix_connect(path, &local_err);
248 537b41f5 Paolo Bonzini
249 537b41f5 Paolo Bonzini
    if (local_err != NULL) {
250 537b41f5 Paolo Bonzini
        qerror_report_err(local_err);
251 537b41f5 Paolo Bonzini
        error_free(local_err);
252 537b41f5 Paolo Bonzini
    }
253 537b41f5 Paolo Bonzini
    return fd;
254 537b41f5 Paolo Bonzini
}
255 537b41f5 Paolo Bonzini
256 a517e88b Paolo Bonzini
static void *show_parts(void *arg)
257 cd831bd7 ths
{
258 a6ac2313 Paolo Bonzini
    char *device = arg;
259 a517e88b Paolo Bonzini
    int nbd;
260 a517e88b Paolo Bonzini
261 a517e88b Paolo Bonzini
    /* linux just needs an open() to trigger
262 a517e88b Paolo Bonzini
     * the partition table update
263 a517e88b Paolo Bonzini
     * but remember to load the module with max_part != 0 :
264 a517e88b Paolo Bonzini
     *     modprobe nbd max_part=63
265 a517e88b Paolo Bonzini
     */
266 a517e88b Paolo Bonzini
    nbd = open(device, O_RDWR);
267 fc19f8a0 Paolo Bonzini
    if (nbd >= 0) {
268 a517e88b Paolo Bonzini
        close(nbd);
269 a517e88b Paolo Bonzini
    }
270 a517e88b Paolo Bonzini
    return NULL;
271 a517e88b Paolo Bonzini
}
272 cd831bd7 ths
273 a517e88b Paolo Bonzini
static void *nbd_client_thread(void *arg)
274 a517e88b Paolo Bonzini
{
275 a6ac2313 Paolo Bonzini
    char *device = arg;
276 a517e88b Paolo Bonzini
    off_t size;
277 a517e88b Paolo Bonzini
    size_t blocksize;
278 a517e88b Paolo Bonzini
    uint32_t nbdflags;
279 a6ac2313 Paolo Bonzini
    int fd, sock;
280 a517e88b Paolo Bonzini
    int ret;
281 a517e88b Paolo Bonzini
    pthread_t show_parts_thread;
282 a517e88b Paolo Bonzini
283 dc10e8b3 Stefan Hajnoczi
    sock = unix_socket_outgoing(sockpath);
284 fc19f8a0 Paolo Bonzini
    if (sock < 0) {
285 dc10e8b3 Stefan Hajnoczi
        goto out;
286 dc10e8b3 Stefan Hajnoczi
    }
287 a517e88b Paolo Bonzini
288 a517e88b Paolo Bonzini
    ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
289 a517e88b Paolo Bonzini
                                &size, &blocksize);
290 fc19f8a0 Paolo Bonzini
    if (ret < 0) {
291 a517e88b Paolo Bonzini
        goto out;
292 a517e88b Paolo Bonzini
    }
293 a517e88b Paolo Bonzini
294 a6ac2313 Paolo Bonzini
    fd = open(device, O_RDWR);
295 fc19f8a0 Paolo Bonzini
    if (fd < 0) {
296 a6ac2313 Paolo Bonzini
        /* Linux-only, we can use %m in printf.  */
297 a6ac2313 Paolo Bonzini
        fprintf(stderr, "Failed to open %s: %m", device);
298 a6ac2313 Paolo Bonzini
        goto out;
299 a6ac2313 Paolo Bonzini
    }
300 a6ac2313 Paolo Bonzini
301 a517e88b Paolo Bonzini
    ret = nbd_init(fd, sock, nbdflags, size, blocksize);
302 fc19f8a0 Paolo Bonzini
    if (ret < 0) {
303 a517e88b Paolo Bonzini
        goto out;
304 a517e88b Paolo Bonzini
    }
305 a517e88b Paolo Bonzini
306 a517e88b Paolo Bonzini
    /* update partition table */
307 a6ac2313 Paolo Bonzini
    pthread_create(&show_parts_thread, NULL, show_parts, device);
308 a517e88b Paolo Bonzini
309 c1f8fdc3 Paolo Bonzini
    if (verbose) {
310 c1f8fdc3 Paolo Bonzini
        fprintf(stderr, "NBD device %s is now connected to %s\n",
311 c1f8fdc3 Paolo Bonzini
                device, srcpath);
312 c1f8fdc3 Paolo Bonzini
    } else {
313 c1f8fdc3 Paolo Bonzini
        /* Close stderr so that the qemu-nbd process exits.  */
314 c1f8fdc3 Paolo Bonzini
        dup2(STDOUT_FILENO, STDERR_FILENO);
315 c1f8fdc3 Paolo Bonzini
    }
316 a517e88b Paolo Bonzini
317 a517e88b Paolo Bonzini
    ret = nbd_client(fd);
318 a517e88b Paolo Bonzini
    if (ret) {
319 a517e88b Paolo Bonzini
        goto out;
320 cd831bd7 ths
    }
321 a517e88b Paolo Bonzini
    close(fd);
322 a517e88b Paolo Bonzini
    kill(getpid(), SIGTERM);
323 a517e88b Paolo Bonzini
    return (void *) EXIT_SUCCESS;
324 a517e88b Paolo Bonzini
325 a517e88b Paolo Bonzini
out:
326 a517e88b Paolo Bonzini
    kill(getpid(), SIGTERM);
327 a517e88b Paolo Bonzini
    return (void *) EXIT_FAILURE;
328 cd831bd7 ths
}
329 cd831bd7 ths
330 a61c6782 Paolo Bonzini
static int nbd_can_accept(void *opaque)
331 a61c6782 Paolo Bonzini
{
332 a61c6782 Paolo Bonzini
    return nb_fds < shared;
333 a61c6782 Paolo Bonzini
}
334 a61c6782 Paolo Bonzini
335 7860a380 Paolo Bonzini
static void nbd_export_closed(NBDExport *exp)
336 7860a380 Paolo Bonzini
{
337 7860a380 Paolo Bonzini
    assert(state == TERMINATING);
338 7860a380 Paolo Bonzini
    state = TERMINATED;
339 7860a380 Paolo Bonzini
}
340 7860a380 Paolo Bonzini
341 1743b515 Paolo Bonzini
static void nbd_client_closed(NBDClient *client)
342 a61c6782 Paolo Bonzini
{
343 1743b515 Paolo Bonzini
    nb_fds--;
344 7860a380 Paolo Bonzini
    if (nb_fds == 0 && !persistent && state == RUNNING) {
345 7860a380 Paolo Bonzini
        state = TERMINATE;
346 7860a380 Paolo Bonzini
    }
347 1743b515 Paolo Bonzini
    qemu_notify_event();
348 7860a380 Paolo Bonzini
    nbd_client_put(client);
349 a61c6782 Paolo Bonzini
}
350 a61c6782 Paolo Bonzini
351 a61c6782 Paolo Bonzini
static void nbd_accept(void *opaque)
352 a61c6782 Paolo Bonzini
{
353 a61c6782 Paolo Bonzini
    int server_fd = (uintptr_t) opaque;
354 a61c6782 Paolo Bonzini
    struct sockaddr_in addr;
355 a61c6782 Paolo Bonzini
    socklen_t addr_len = sizeof(addr);
356 a61c6782 Paolo Bonzini
357 a61c6782 Paolo Bonzini
    int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
358 7860a380 Paolo Bonzini
    if (state >= TERMINATE) {
359 7860a380 Paolo Bonzini
        close(fd);
360 7860a380 Paolo Bonzini
        return;
361 7860a380 Paolo Bonzini
    }
362 7860a380 Paolo Bonzini
363 fc19f8a0 Paolo Bonzini
    if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
364 a61c6782 Paolo Bonzini
        nb_fds++;
365 a61c6782 Paolo Bonzini
    }
366 a61c6782 Paolo Bonzini
}
367 a61c6782 Paolo Bonzini
368 7a5ca864 bellard
int main(int argc, char **argv)
369 7a5ca864 bellard
{
370 7a5ca864 bellard
    BlockDriverState *bs;
371 e6b63677 Daniel P. Berrange
    BlockDriver *drv;
372 7a5ca864 bellard
    off_t dev_offset = 0;
373 b90fb4b8 Paolo Bonzini
    uint32_t nbdflags = 0;
374 cd831bd7 ths
    bool disconnect = false;
375 7a5ca864 bellard
    const char *bindto = "0.0.0.0";
376 a6ac2313 Paolo Bonzini
    char *device = NULL;
377 c2e2872b Laurent Vivier
    int port = NBD_DEFAULT_PORT;
378 7a5ca864 bellard
    off_t fd_size;
379 8c116b0e Wenchao Xia
    QemuOpts *sn_opts = NULL;
380 8c116b0e Wenchao Xia
    const char *sn_id_or_name = NULL;
381 8c116b0e Wenchao Xia
    const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:";
382 7a5ca864 bellard
    struct option lopt[] = {
383 660f11be Blue Swirl
        { "help", 0, NULL, 'h' },
384 660f11be Blue Swirl
        { "version", 0, NULL, 'V' },
385 660f11be Blue Swirl
        { "bind", 1, NULL, 'b' },
386 660f11be Blue Swirl
        { "port", 1, NULL, 'p' },
387 660f11be Blue Swirl
        { "socket", 1, NULL, 'k' },
388 660f11be Blue Swirl
        { "offset", 1, NULL, 'o' },
389 660f11be Blue Swirl
        { "read-only", 0, NULL, 'r' },
390 660f11be Blue Swirl
        { "partition", 1, NULL, 'P' },
391 660f11be Blue Swirl
        { "connect", 1, NULL, 'c' },
392 660f11be Blue Swirl
        { "disconnect", 0, NULL, 'd' },
393 660f11be Blue Swirl
        { "snapshot", 0, NULL, 's' },
394 8c116b0e Wenchao Xia
        { "load-snapshot", 1, NULL, 'l' },
395 660f11be Blue Swirl
        { "nocache", 0, NULL, 'n' },
396 39a5235c Paolo Bonzini
        { "cache", 1, NULL, QEMU_NBD_OPT_CACHE },
397 39a5235c Paolo Bonzini
#ifdef CONFIG_LINUX_AIO
398 39a5235c Paolo Bonzini
        { "aio", 1, NULL, QEMU_NBD_OPT_AIO },
399 39a5235c Paolo Bonzini
#endif
400 ded9d2d5 Paolo Bonzini
        { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD },
401 660f11be Blue Swirl
        { "shared", 1, NULL, 'e' },
402 e6b63677 Daniel P. Berrange
        { "format", 1, NULL, 'f' },
403 660f11be Blue Swirl
        { "persistent", 0, NULL, 't' },
404 660f11be Blue Swirl
        { "verbose", 0, NULL, 'v' },
405 660f11be Blue Swirl
        { NULL, 0, NULL, 0 }
406 7a5ca864 bellard
    };
407 7a5ca864 bellard
    int ch;
408 7a5ca864 bellard
    int opt_ind = 0;
409 7a5ca864 bellard
    int li;
410 7a5ca864 bellard
    char *end;
411 f5edb014 Naphtali Sprei
    int flags = BDRV_O_RDWR;
412 7a5ca864 bellard
    int partition = -1;
413 cd831bd7 ths
    int ret;
414 3b05a8e9 ths
    int fd;
415 39a5235c Paolo Bonzini
    bool seen_cache = false;
416 ded9d2d5 Paolo Bonzini
    bool seen_discard = false;
417 39a5235c Paolo Bonzini
#ifdef CONFIG_LINUX_AIO
418 39a5235c Paolo Bonzini
    bool seen_aio = false;
419 39a5235c Paolo Bonzini
#endif
420 a517e88b Paolo Bonzini
    pthread_t client_thread;
421 e6b63677 Daniel P. Berrange
    const char *fmt = NULL;
422 34b5d2c6 Max Reitz
    Error *local_err = NULL;
423 7a5ca864 bellard
424 a517e88b Paolo Bonzini
    /* The client thread uses SIGTERM to interrupt the server.  A signal
425 a517e88b Paolo Bonzini
     * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
426 a517e88b Paolo Bonzini
     */
427 bb345110 Paolo Bonzini
    struct sigaction sa_sigterm;
428 bb345110 Paolo Bonzini
    memset(&sa_sigterm, 0, sizeof(sa_sigterm));
429 bb345110 Paolo Bonzini
    sa_sigterm.sa_handler = termsig_handler;
430 bb345110 Paolo Bonzini
    sigaction(SIGTERM, &sa_sigterm, NULL);
431 10f5bff6 Fam Zheng
    qemu_init_exec_dir(argv[0]);
432 bb345110 Paolo Bonzini
433 7a5ca864 bellard
    while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
434 7a5ca864 bellard
        switch (ch) {
435 7a5ca864 bellard
        case 's':
436 2f726488 ths
            flags |= BDRV_O_SNAPSHOT;
437 2f726488 ths
            break;
438 2f726488 ths
        case 'n':
439 39a5235c Paolo Bonzini
            optarg = (char *) "none";
440 39a5235c Paolo Bonzini
            /* fallthrough */
441 39a5235c Paolo Bonzini
        case QEMU_NBD_OPT_CACHE:
442 39a5235c Paolo Bonzini
            if (seen_cache) {
443 39a5235c Paolo Bonzini
                errx(EXIT_FAILURE, "-n and --cache can only be specified once");
444 39a5235c Paolo Bonzini
            }
445 39a5235c Paolo Bonzini
            seen_cache = true;
446 39a5235c Paolo Bonzini
            if (bdrv_parse_cache_flags(optarg, &flags) == -1) {
447 39a5235c Paolo Bonzini
                errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg);
448 39a5235c Paolo Bonzini
            }
449 7a5ca864 bellard
            break;
450 39a5235c Paolo Bonzini
#ifdef CONFIG_LINUX_AIO
451 39a5235c Paolo Bonzini
        case QEMU_NBD_OPT_AIO:
452 39a5235c Paolo Bonzini
            if (seen_aio) {
453 39a5235c Paolo Bonzini
                errx(EXIT_FAILURE, "--aio can only be specified once");
454 39a5235c Paolo Bonzini
            }
455 39a5235c Paolo Bonzini
            seen_aio = true;
456 39a5235c Paolo Bonzini
            if (!strcmp(optarg, "native")) {
457 39a5235c Paolo Bonzini
                flags |= BDRV_O_NATIVE_AIO;
458 39a5235c Paolo Bonzini
            } else if (!strcmp(optarg, "threads")) {
459 39a5235c Paolo Bonzini
                /* this is the default */
460 39a5235c Paolo Bonzini
            } else {
461 39a5235c Paolo Bonzini
               errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg);
462 39a5235c Paolo Bonzini
            }
463 39a5235c Paolo Bonzini
            break;
464 39a5235c Paolo Bonzini
#endif
465 ded9d2d5 Paolo Bonzini
        case QEMU_NBD_OPT_DISCARD:
466 ded9d2d5 Paolo Bonzini
            if (seen_discard) {
467 ded9d2d5 Paolo Bonzini
                errx(EXIT_FAILURE, "--discard can only be specified once");
468 ded9d2d5 Paolo Bonzini
            }
469 ded9d2d5 Paolo Bonzini
            seen_discard = true;
470 ded9d2d5 Paolo Bonzini
            if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
471 ded9d2d5 Paolo Bonzini
                errx(EXIT_FAILURE, "Invalid discard mode `%s'", optarg);
472 ded9d2d5 Paolo Bonzini
            }
473 ded9d2d5 Paolo Bonzini
            break;
474 7a5ca864 bellard
        case 'b':
475 7a5ca864 bellard
            bindto = optarg;
476 7a5ca864 bellard
            break;
477 7a5ca864 bellard
        case 'p':
478 7a5ca864 bellard
            li = strtol(optarg, &end, 0);
479 7a5ca864 bellard
            if (*end) {
480 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
481 7a5ca864 bellard
            }
482 7a5ca864 bellard
            if (li < 1 || li > 65535) {
483 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
484 7a5ca864 bellard
            }
485 7a5ca864 bellard
            port = (uint16_t)li;
486 7a5ca864 bellard
            break;
487 7a5ca864 bellard
        case 'o':
488 7a5ca864 bellard
                dev_offset = strtoll (optarg, &end, 0);
489 7a5ca864 bellard
            if (*end) {
490 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
491 7a5ca864 bellard
            }
492 7a5ca864 bellard
            if (dev_offset < 0) {
493 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
494 7a5ca864 bellard
            }
495 7a5ca864 bellard
            break;
496 8c116b0e Wenchao Xia
        case 'l':
497 8c116b0e Wenchao Xia
            if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
498 8c116b0e Wenchao Xia
                sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0);
499 8c116b0e Wenchao Xia
                if (!sn_opts) {
500 8c116b0e Wenchao Xia
                    errx(EXIT_FAILURE, "Failed in parsing snapshot param `%s'",
501 8c116b0e Wenchao Xia
                         optarg);
502 8c116b0e Wenchao Xia
                }
503 8c116b0e Wenchao Xia
            } else {
504 8c116b0e Wenchao Xia
                sn_id_or_name = optarg;
505 8c116b0e Wenchao Xia
            }
506 8c116b0e Wenchao Xia
            /* fall through */
507 7a5ca864 bellard
        case 'r':
508 b90fb4b8 Paolo Bonzini
            nbdflags |= NBD_FLAG_READ_ONLY;
509 07108b29 Naphtali Sprei
            flags &= ~BDRV_O_RDWR;
510 7a5ca864 bellard
            break;
511 7a5ca864 bellard
        case 'P':
512 7a5ca864 bellard
            partition = strtol(optarg, &end, 0);
513 7a5ca864 bellard
            if (*end)
514 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
515 7a5ca864 bellard
            if (partition < 1 || partition > 8)
516 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid partition %d", partition);
517 7a5ca864 bellard
            break;
518 cd831bd7 ths
        case 'k':
519 b32f6c28 Paolo Bonzini
            sockpath = optarg;
520 b32f6c28 Paolo Bonzini
            if (sockpath[0] != '/')
521 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "socket path must be absolute\n");
522 cd831bd7 ths
            break;
523 cd831bd7 ths
        case 'd':
524 cd831bd7 ths
            disconnect = true;
525 cd831bd7 ths
            break;
526 cd831bd7 ths
        case 'c':
527 cd831bd7 ths
            device = optarg;
528 cd831bd7 ths
            break;
529 3b05a8e9 ths
        case 'e':
530 3b05a8e9 ths
            shared = strtol(optarg, &end, 0);
531 3b05a8e9 ths
            if (*end) {
532 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
533 3b05a8e9 ths
            }
534 3b05a8e9 ths
            if (shared < 1) {
535 b6353bea Ryota Ozaki
                errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
536 3b05a8e9 ths
            }
537 3b05a8e9 ths
            break;
538 e6b63677 Daniel P. Berrange
        case 'f':
539 e6b63677 Daniel P. Berrange
            fmt = optarg;
540 e6b63677 Daniel P. Berrange
            break;
541 75818250 ths
        case 't':
542 75818250 ths
            persistent = 1;
543 75818250 ths
            break;
544 7a5ca864 bellard
        case 'v':
545 7a5ca864 bellard
            verbose = 1;
546 7a5ca864 bellard
            break;
547 7a5ca864 bellard
        case 'V':
548 7a5ca864 bellard
            version(argv[0]);
549 7a5ca864 bellard
            exit(0);
550 7a5ca864 bellard
            break;
551 7a5ca864 bellard
        case 'h':
552 7a5ca864 bellard
            usage(argv[0]);
553 7a5ca864 bellard
            exit(0);
554 7a5ca864 bellard
            break;
555 7a5ca864 bellard
        case '?':
556 b6353bea Ryota Ozaki
            errx(EXIT_FAILURE, "Try `%s --help' for more information.",
557 7a5ca864 bellard
                 argv[0]);
558 7a5ca864 bellard
        }
559 7a5ca864 bellard
    }
560 7a5ca864 bellard
561 7a5ca864 bellard
    if ((argc - optind) != 1) {
562 b6353bea Ryota Ozaki
        errx(EXIT_FAILURE, "Invalid number of argument.\n"
563 7a5ca864 bellard
             "Try `%s --help' for more information.",
564 7a5ca864 bellard
             argv[0]);
565 7a5ca864 bellard
    }
566 7a5ca864 bellard
567 cd831bd7 ths
    if (disconnect) {
568 cd831bd7 ths
        fd = open(argv[optind], O_RDWR);
569 fc19f8a0 Paolo Bonzini
        if (fd < 0) {
570 cb7cf0e3 Ryota Ozaki
            err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
571 fc19f8a0 Paolo Bonzini
        }
572 cd831bd7 ths
        nbd_disconnect(fd);
573 cd831bd7 ths
574 cd831bd7 ths
        close(fd);
575 cd831bd7 ths
576 cd831bd7 ths
        printf("%s disconnected\n", argv[optind]);
577 cd831bd7 ths
578 cd831bd7 ths
        return 0;
579 cd831bd7 ths
    }
580 cd831bd7 ths
581 c1f8fdc3 Paolo Bonzini
    if (device && !verbose) {
582 c1f8fdc3 Paolo Bonzini
        int stderr_fd[2];
583 c1f8fdc3 Paolo Bonzini
        pid_t pid;
584 c1f8fdc3 Paolo Bonzini
        int ret;
585 c1f8fdc3 Paolo Bonzini
586 fc19f8a0 Paolo Bonzini
        if (qemu_pipe(stderr_fd) < 0) {
587 c1f8fdc3 Paolo Bonzini
            err(EXIT_FAILURE, "Error setting up communication pipe");
588 c1f8fdc3 Paolo Bonzini
        }
589 c1f8fdc3 Paolo Bonzini
590 c1f8fdc3 Paolo Bonzini
        /* Now daemonize, but keep a communication channel open to
591 c1f8fdc3 Paolo Bonzini
         * print errors and exit with the proper status code.
592 c1f8fdc3 Paolo Bonzini
         */
593 c1f8fdc3 Paolo Bonzini
        pid = fork();
594 c1f8fdc3 Paolo Bonzini
        if (pid == 0) {
595 c1f8fdc3 Paolo Bonzini
            close(stderr_fd[0]);
596 9faf31b6 Michael Tokarev
            ret = qemu_daemon(1, 0);
597 c1f8fdc3 Paolo Bonzini
598 c1f8fdc3 Paolo Bonzini
            /* Temporarily redirect stderr to the parent's pipe...  */
599 c1f8fdc3 Paolo Bonzini
            dup2(stderr_fd[1], STDERR_FILENO);
600 fc19f8a0 Paolo Bonzini
            if (ret < 0) {
601 c1f8fdc3 Paolo Bonzini
                err(EXIT_FAILURE, "Failed to daemonize");
602 c1f8fdc3 Paolo Bonzini
            }
603 c1f8fdc3 Paolo Bonzini
604 c1f8fdc3 Paolo Bonzini
            /* ... close the descriptor we inherited and go on.  */
605 c1f8fdc3 Paolo Bonzini
            close(stderr_fd[1]);
606 c1f8fdc3 Paolo Bonzini
        } else {
607 c1f8fdc3 Paolo Bonzini
            bool errors = false;
608 c1f8fdc3 Paolo Bonzini
            char *buf;
609 c1f8fdc3 Paolo Bonzini
610 c1f8fdc3 Paolo Bonzini
            /* In the parent.  Print error messages from the child until
611 c1f8fdc3 Paolo Bonzini
             * it closes the pipe.
612 c1f8fdc3 Paolo Bonzini
             */
613 c1f8fdc3 Paolo Bonzini
            close(stderr_fd[1]);
614 c1f8fdc3 Paolo Bonzini
            buf = g_malloc(1024);
615 c1f8fdc3 Paolo Bonzini
            while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
616 c1f8fdc3 Paolo Bonzini
                errors = true;
617 c1f8fdc3 Paolo Bonzini
                ret = qemu_write_full(STDERR_FILENO, buf, ret);
618 fc19f8a0 Paolo Bonzini
                if (ret < 0) {
619 c1f8fdc3 Paolo Bonzini
                    exit(EXIT_FAILURE);
620 c1f8fdc3 Paolo Bonzini
                }
621 c1f8fdc3 Paolo Bonzini
            }
622 fc19f8a0 Paolo Bonzini
            if (ret < 0) {
623 c1f8fdc3 Paolo Bonzini
                err(EXIT_FAILURE, "Cannot read from daemon");
624 c1f8fdc3 Paolo Bonzini
            }
625 c1f8fdc3 Paolo Bonzini
626 c1f8fdc3 Paolo Bonzini
            /* Usually the daemon should not print any message.
627 c1f8fdc3 Paolo Bonzini
             * Exit with zero status in that case.
628 c1f8fdc3 Paolo Bonzini
             */
629 c1f8fdc3 Paolo Bonzini
            exit(errors);
630 c1f8fdc3 Paolo Bonzini
        }
631 c1f8fdc3 Paolo Bonzini
    }
632 c1f8fdc3 Paolo Bonzini
633 a6ac2313 Paolo Bonzini
    if (device != NULL && sockpath == NULL) {
634 a6ac2313 Paolo Bonzini
        sockpath = g_malloc(128);
635 a6ac2313 Paolo Bonzini
        snprintf(sockpath, 128, SOCKET_PATH, basename(device));
636 cd831bd7 ths
    }
637 cd831bd7 ths
638 7e7f4a0e Paolo Bonzini
    qemu_init_main_loop();
639 802ddc37 Paolo Bonzini
    bdrv_init();
640 802ddc37 Paolo Bonzini
    atexit(bdrv_close_all);
641 802ddc37 Paolo Bonzini
642 e6b63677 Daniel P. Berrange
    if (fmt) {
643 e6b63677 Daniel P. Berrange
        drv = bdrv_find_format(fmt);
644 e6b63677 Daniel P. Berrange
        if (!drv) {
645 e6b63677 Daniel P. Berrange
            errx(EXIT_FAILURE, "Unknown file format '%s'", fmt);
646 e6b63677 Daniel P. Berrange
        }
647 e6b63677 Daniel P. Berrange
    } else {
648 e6b63677 Daniel P. Berrange
        drv = NULL;
649 e6b63677 Daniel P. Berrange
    }
650 e6b63677 Daniel P. Berrange
651 802ddc37 Paolo Bonzini
    bs = bdrv_new("hda");
652 802ddc37 Paolo Bonzini
    srcpath = argv[optind];
653 ddf5636d Max Reitz
    ret = bdrv_open(&bs, srcpath, NULL, NULL, flags, drv, &local_err);
654 e6b63677 Daniel P. Berrange
    if (ret < 0) {
655 802ddc37 Paolo Bonzini
        errno = -ret;
656 34b5d2c6 Max Reitz
        err(EXIT_FAILURE, "Failed to bdrv_open '%s': %s", argv[optind],
657 34b5d2c6 Max Reitz
            error_get_pretty(local_err));
658 802ddc37 Paolo Bonzini
    }
659 802ddc37 Paolo Bonzini
660 8c116b0e Wenchao Xia
    if (sn_opts) {
661 8c116b0e Wenchao Xia
        ret = bdrv_snapshot_load_tmp(bs,
662 8c116b0e Wenchao Xia
                                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
663 8c116b0e Wenchao Xia
                                     qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
664 8c116b0e Wenchao Xia
                                     &local_err);
665 8c116b0e Wenchao Xia
    } else if (sn_id_or_name) {
666 8c116b0e Wenchao Xia
        ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
667 8c116b0e Wenchao Xia
                                                   &local_err);
668 8c116b0e Wenchao Xia
    }
669 8c116b0e Wenchao Xia
    if (ret < 0) {
670 8c116b0e Wenchao Xia
        errno = -ret;
671 8c116b0e Wenchao Xia
        err(EXIT_FAILURE,
672 8c116b0e Wenchao Xia
            "Failed to load snapshot: %s",
673 8c116b0e Wenchao Xia
            error_get_pretty(local_err));
674 8c116b0e Wenchao Xia
    }
675 8c116b0e Wenchao Xia
676 38ceff04 Paolo Bonzini
    fd_size = bdrv_getlength(bs);
677 802ddc37 Paolo Bonzini
678 185b4338 Paolo Bonzini
    if (partition != -1) {
679 185b4338 Paolo Bonzini
        ret = find_partition(bs, partition, &dev_offset, &fd_size);
680 185b4338 Paolo Bonzini
        if (ret < 0) {
681 185b4338 Paolo Bonzini
            errno = -ret;
682 185b4338 Paolo Bonzini
            err(EXIT_FAILURE, "Could not find partition %d", partition);
683 185b4338 Paolo Bonzini
        }
684 802ddc37 Paolo Bonzini
    }
685 802ddc37 Paolo Bonzini
686 7860a380 Paolo Bonzini
    exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags, nbd_export_closed);
687 3b05a8e9 ths
688 b32f6c28 Paolo Bonzini
    if (sockpath) {
689 a61c6782 Paolo Bonzini
        fd = unix_socket_incoming(sockpath);
690 cd831bd7 ths
    } else {
691 a61c6782 Paolo Bonzini
        fd = tcp_socket_incoming(bindto, port);
692 cd831bd7 ths
    }
693 cd831bd7 ths
694 fc19f8a0 Paolo Bonzini
    if (fd < 0) {
695 7a5ca864 bellard
        return 1;
696 a61c6782 Paolo Bonzini
    }
697 f1ef5555 Paolo Bonzini
698 f1ef5555 Paolo Bonzini
    if (device) {
699 f1ef5555 Paolo Bonzini
        int ret;
700 f1ef5555 Paolo Bonzini
701 a6ac2313 Paolo Bonzini
        ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
702 f1ef5555 Paolo Bonzini
        if (ret != 0) {
703 f1ef5555 Paolo Bonzini
            errx(EXIT_FAILURE, "Failed to create client thread: %s",
704 f1ef5555 Paolo Bonzini
                 strerror(ret));
705 f1ef5555 Paolo Bonzini
        }
706 f1ef5555 Paolo Bonzini
    } else {
707 f1ef5555 Paolo Bonzini
        /* Shut up GCC warnings.  */
708 f1ef5555 Paolo Bonzini
        memset(&client_thread, 0, sizeof(client_thread));
709 f1ef5555 Paolo Bonzini
    }
710 f1ef5555 Paolo Bonzini
711 a61c6782 Paolo Bonzini
    qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
712 a61c6782 Paolo Bonzini
                         (void *)(uintptr_t)fd);
713 7a5ca864 bellard
714 9faf31b6 Michael Tokarev
    /* now when the initialization is (almost) complete, chdir("/")
715 9faf31b6 Michael Tokarev
     * to free any busy filesystems */
716 9faf31b6 Michael Tokarev
    if (chdir("/") < 0) {
717 9faf31b6 Michael Tokarev
        err(EXIT_FAILURE, "Could not chdir to root directory");
718 9faf31b6 Michael Tokarev
    }
719 9faf31b6 Michael Tokarev
720 7860a380 Paolo Bonzini
    state = RUNNING;
721 3b05a8e9 ths
    do {
722 a61c6782 Paolo Bonzini
        main_loop_wait(false);
723 7860a380 Paolo Bonzini
        if (state == TERMINATE) {
724 7860a380 Paolo Bonzini
            state = TERMINATING;
725 7860a380 Paolo Bonzini
            nbd_export_close(exp);
726 7860a380 Paolo Bonzini
            nbd_export_put(exp);
727 7860a380 Paolo Bonzini
            exp = NULL;
728 7860a380 Paolo Bonzini
        }
729 7860a380 Paolo Bonzini
    } while (state != TERMINATED);
730 7a5ca864 bellard
731 a4aab7b4 Paolo Bonzini
    bdrv_close(bs);
732 b32f6c28 Paolo Bonzini
    if (sockpath) {
733 b32f6c28 Paolo Bonzini
        unlink(sockpath);
734 b32f6c28 Paolo Bonzini
    }
735 7a5ca864 bellard
736 8c116b0e Wenchao Xia
    if (sn_opts) {
737 8c116b0e Wenchao Xia
        qemu_opts_del(sn_opts);
738 8c116b0e Wenchao Xia
    }
739 8c116b0e Wenchao Xia
740 a517e88b Paolo Bonzini
    if (device) {
741 a517e88b Paolo Bonzini
        void *ret;
742 a517e88b Paolo Bonzini
        pthread_join(client_thread, &ret);
743 a517e88b Paolo Bonzini
        exit(ret != NULL);
744 a517e88b Paolo Bonzini
    } else {
745 a517e88b Paolo Bonzini
        exit(EXIT_SUCCESS);
746 a517e88b Paolo Bonzini
    }
747 7a5ca864 bellard
}