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