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