]>
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 | ||
453b07b1 | 170 | if (!mbr[i].system || !mbr[i].nb_sectors_abs) { |
7a5ca864 | 171 | continue; |
453b07b1 | 172 | } |
7a5ca864 FB |
173 | |
174 | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { | |
175 | struct partition_record ext[4]; | |
176 | uint8_t data1[512]; | |
177 | int j; | |
178 | ||
4c58e80a | 179 | if ((ret = blk_read(blk, mbr[i].start_sector_abs, data1, 1)) < 0) { |
cb7cf0e3 RO |
180 | errno = -ret; |
181 | err(EXIT_FAILURE, "error while reading"); | |
182 | } | |
7a5ca864 FB |
183 | |
184 | for (j = 0; j < 4; j++) { | |
185 | read_partition(&data1[446 + 16 * j], &ext[j]); | |
453b07b1 | 186 | if (!ext[j].system || !ext[j].nb_sectors_abs) { |
7a5ca864 | 187 | continue; |
453b07b1 | 188 | } |
7a5ca864 FB |
189 | |
190 | if ((ext_partnum + j + 1) == partition) { | |
191 | *offset = (uint64_t)ext[j].start_sector_abs << 9; | |
192 | *size = (uint64_t)ext[j].nb_sectors_abs << 9; | |
193 | return 0; | |
194 | } | |
195 | } | |
196 | ext_partnum += 4; | |
197 | } else if ((i + 1) == partition) { | |
198 | *offset = (uint64_t)mbr[i].start_sector_abs << 9; | |
199 | *size = (uint64_t)mbr[i].nb_sectors_abs << 9; | |
200 | return 0; | |
201 | } | |
202 | } | |
203 | ||
185b4338 | 204 | return -ENOENT; |
7a5ca864 FB |
205 | } |
206 | ||
bb345110 PB |
207 | static void termsig_handler(int signum) |
208 | { | |
7860a380 | 209 | state = TERMINATE; |
a61c6782 | 210 | qemu_notify_event(); |
bb345110 PB |
211 | } |
212 | ||
537b41f5 PB |
213 | static void combine_addr(char *buf, size_t len, const char* address, |
214 | uint16_t port) | |
215 | { | |
216 | /* If the address-part contains a colon, it's an IPv6 IP so needs [] */ | |
217 | if (strstr(address, ":")) { | |
218 | snprintf(buf, len, "[%s]:%u", address, port); | |
219 | } else { | |
220 | snprintf(buf, len, "%s:%u", address, port); | |
221 | } | |
222 | } | |
223 | ||
224 | static int tcp_socket_incoming(const char *address, uint16_t port) | |
225 | { | |
226 | char address_and_port[128]; | |
227 | Error *local_err = NULL; | |
228 | ||
229 | combine_addr(address_and_port, 128, address, port); | |
230 | int fd = inet_listen(address_and_port, NULL, 0, SOCK_STREAM, 0, &local_err); | |
231 | ||
232 | if (local_err != NULL) { | |
565f65d2 | 233 | error_report_err(local_err); |
537b41f5 PB |
234 | } |
235 | return fd; | |
236 | } | |
237 | ||
238 | static int unix_socket_incoming(const char *path) | |
239 | { | |
240 | Error *local_err = NULL; | |
241 | int fd = unix_listen(path, NULL, 0, &local_err); | |
242 | ||
243 | if (local_err != NULL) { | |
565f65d2 | 244 | error_report_err(local_err); |
537b41f5 PB |
245 | } |
246 | return fd; | |
247 | } | |
248 | ||
249 | static int unix_socket_outgoing(const char *path) | |
250 | { | |
251 | Error *local_err = NULL; | |
252 | int fd = unix_connect(path, &local_err); | |
253 | ||
254 | if (local_err != NULL) { | |
565f65d2 | 255 | error_report_err(local_err); |
537b41f5 PB |
256 | } |
257 | return fd; | |
258 | } | |
259 | ||
a517e88b | 260 | static void *show_parts(void *arg) |
cd831bd7 | 261 | { |
a6ac2313 | 262 | char *device = arg; |
a517e88b PB |
263 | int nbd; |
264 | ||
265 | /* linux just needs an open() to trigger | |
266 | * the partition table update | |
267 | * but remember to load the module with max_part != 0 : | |
268 | * modprobe nbd max_part=63 | |
269 | */ | |
270 | nbd = open(device, O_RDWR); | |
fc19f8a0 | 271 | if (nbd >= 0) { |
a517e88b PB |
272 | close(nbd); |
273 | } | |
274 | return NULL; | |
275 | } | |
cd831bd7 | 276 | |
a517e88b PB |
277 | static void *nbd_client_thread(void *arg) |
278 | { | |
a6ac2313 | 279 | char *device = arg; |
a517e88b PB |
280 | off_t size; |
281 | size_t blocksize; | |
282 | uint32_t nbdflags; | |
a6ac2313 | 283 | int fd, sock; |
a517e88b PB |
284 | int ret; |
285 | pthread_t show_parts_thread; | |
1ce52846 | 286 | Error *local_error = NULL; |
a517e88b | 287 | |
dc10e8b3 | 288 | sock = unix_socket_outgoing(sockpath); |
fc19f8a0 | 289 | if (sock < 0) { |
dc10e8b3 SH |
290 | goto out; |
291 | } | |
a517e88b PB |
292 | |
293 | ret = nbd_receive_negotiate(sock, NULL, &nbdflags, | |
1ce52846 | 294 | &size, &blocksize, &local_error); |
fc19f8a0 | 295 | if (ret < 0) { |
1ce52846 HR |
296 | if (local_error) { |
297 | fprintf(stderr, "%s\n", error_get_pretty(local_error)); | |
298 | error_free(local_error); | |
299 | } | |
0c544d73 | 300 | goto out_socket; |
a517e88b PB |
301 | } |
302 | ||
a6ac2313 | 303 | fd = open(device, O_RDWR); |
fc19f8a0 | 304 | if (fd < 0) { |
a6ac2313 | 305 | /* Linux-only, we can use %m in printf. */ |
5672ee54 | 306 | fprintf(stderr, "Failed to open %s: %m\n", device); |
0c544d73 | 307 | goto out_socket; |
a6ac2313 PB |
308 | } |
309 | ||
a517e88b | 310 | ret = nbd_init(fd, sock, nbdflags, size, blocksize); |
fc19f8a0 | 311 | if (ret < 0) { |
0c544d73 | 312 | goto out_fd; |
a517e88b PB |
313 | } |
314 | ||
315 | /* update partition table */ | |
a6ac2313 | 316 | pthread_create(&show_parts_thread, NULL, show_parts, device); |
a517e88b | 317 | |
c1f8fdc3 PB |
318 | if (verbose) { |
319 | fprintf(stderr, "NBD device %s is now connected to %s\n", | |
320 | device, srcpath); | |
321 | } else { | |
322 | /* Close stderr so that the qemu-nbd process exits. */ | |
323 | dup2(STDOUT_FILENO, STDERR_FILENO); | |
324 | } | |
a517e88b PB |
325 | |
326 | ret = nbd_client(fd); | |
327 | if (ret) { | |
0c544d73 | 328 | goto out_fd; |
cd831bd7 | 329 | } |
a517e88b PB |
330 | close(fd); |
331 | kill(getpid(), SIGTERM); | |
332 | return (void *) EXIT_SUCCESS; | |
333 | ||
0c544d73 PB |
334 | out_fd: |
335 | close(fd); | |
336 | out_socket: | |
337 | closesocket(sock); | |
a517e88b PB |
338 | out: |
339 | kill(getpid(), SIGTERM); | |
340 | return (void *) EXIT_FAILURE; | |
cd831bd7 TS |
341 | } |
342 | ||
a61c6782 PB |
343 | static int nbd_can_accept(void *opaque) |
344 | { | |
345 | return nb_fds < shared; | |
346 | } | |
347 | ||
7860a380 PB |
348 | static void nbd_export_closed(NBDExport *exp) |
349 | { | |
350 | assert(state == TERMINATING); | |
351 | state = TERMINATED; | |
352 | } | |
353 | ||
1743b515 | 354 | static void nbd_client_closed(NBDClient *client) |
a61c6782 | 355 | { |
1743b515 | 356 | nb_fds--; |
7860a380 PB |
357 | if (nb_fds == 0 && !persistent && state == RUNNING) { |
358 | state = TERMINATE; | |
359 | } | |
1743b515 | 360 | qemu_notify_event(); |
7860a380 | 361 | nbd_client_put(client); |
a61c6782 PB |
362 | } |
363 | ||
364 | static void nbd_accept(void *opaque) | |
365 | { | |
366 | int server_fd = (uintptr_t) opaque; | |
367 | struct sockaddr_in addr; | |
368 | socklen_t addr_len = sizeof(addr); | |
369 | ||
370 | int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len); | |
0c544d73 PB |
371 | if (fd < 0) { |
372 | perror("accept"); | |
373 | return; | |
374 | } | |
375 | ||
7860a380 PB |
376 | if (state >= TERMINATE) { |
377 | close(fd); | |
378 | return; | |
379 | } | |
380 | ||
36af5994 | 381 | if (nbd_client_new(exp, fd, nbd_client_closed)) { |
a61c6782 | 382 | nb_fds++; |
36af5994 | 383 | } else { |
27e5eae4 | 384 | shutdown(fd, 2); |
36af5994 | 385 | close(fd); |
a61c6782 PB |
386 | } |
387 | } | |
388 | ||
7a5ca864 FB |
389 | int main(int argc, char **argv) |
390 | { | |
26f54e9a | 391 | BlockBackend *blk; |
7a5ca864 FB |
392 | BlockDriverState *bs; |
393 | off_t dev_offset = 0; | |
b90fb4b8 | 394 | uint32_t nbdflags = 0; |
cd831bd7 | 395 | bool disconnect = false; |
7a5ca864 | 396 | const char *bindto = "0.0.0.0"; |
a6ac2313 | 397 | char *device = NULL; |
c2e2872b | 398 | int port = NBD_DEFAULT_PORT; |
7a5ca864 | 399 | off_t fd_size; |
8c116b0e WX |
400 | QemuOpts *sn_opts = NULL; |
401 | const char *sn_id_or_name = NULL; | |
402 | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:"; | |
7a5ca864 | 403 | struct option lopt[] = { |
660f11be BS |
404 | { "help", 0, NULL, 'h' }, |
405 | { "version", 0, NULL, 'V' }, | |
406 | { "bind", 1, NULL, 'b' }, | |
407 | { "port", 1, NULL, 'p' }, | |
408 | { "socket", 1, NULL, 'k' }, | |
409 | { "offset", 1, NULL, 'o' }, | |
410 | { "read-only", 0, NULL, 'r' }, | |
411 | { "partition", 1, NULL, 'P' }, | |
412 | { "connect", 1, NULL, 'c' }, | |
413 | { "disconnect", 0, NULL, 'd' }, | |
414 | { "snapshot", 0, NULL, 's' }, | |
8c116b0e | 415 | { "load-snapshot", 1, NULL, 'l' }, |
660f11be | 416 | { "nocache", 0, NULL, 'n' }, |
39a5235c PB |
417 | { "cache", 1, NULL, QEMU_NBD_OPT_CACHE }, |
418 | #ifdef CONFIG_LINUX_AIO | |
419 | { "aio", 1, NULL, QEMU_NBD_OPT_AIO }, | |
420 | #endif | |
ded9d2d5 | 421 | { "discard", 1, NULL, QEMU_NBD_OPT_DISCARD }, |
b3838a40 | 422 | { "detect-zeroes", 1, NULL, QEMU_NBD_OPT_DETECT_ZEROES }, |
660f11be | 423 | { "shared", 1, NULL, 'e' }, |
e6b63677 | 424 | { "format", 1, NULL, 'f' }, |
660f11be BS |
425 | { "persistent", 0, NULL, 't' }, |
426 | { "verbose", 0, NULL, 'v' }, | |
427 | { NULL, 0, NULL, 0 } | |
7a5ca864 FB |
428 | }; |
429 | int ch; | |
430 | int opt_ind = 0; | |
431 | int li; | |
432 | char *end; | |
f5edb014 | 433 | int flags = BDRV_O_RDWR; |
7a5ca864 | 434 | int partition = -1; |
4fbec260 | 435 | int ret = 0; |
3b05a8e9 | 436 | int fd; |
39a5235c | 437 | bool seen_cache = false; |
ded9d2d5 | 438 | bool seen_discard = false; |
39a5235c PB |
439 | #ifdef CONFIG_LINUX_AIO |
440 | bool seen_aio = false; | |
441 | #endif | |
a517e88b | 442 | pthread_t client_thread; |
e6b63677 | 443 | const char *fmt = NULL; |
34b5d2c6 | 444 | Error *local_err = NULL; |
b3838a40 | 445 | BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF; |
4fbec260 | 446 | QDict *options = NULL; |
7a5ca864 | 447 | |
a517e88b PB |
448 | /* The client thread uses SIGTERM to interrupt the server. A signal |
449 | * handler ensures that "qemu-nbd -v -c" exits with a nice status code. | |
450 | */ | |
bb345110 | 451 | struct sigaction sa_sigterm; |
bb345110 PB |
452 | memset(&sa_sigterm, 0, sizeof(sa_sigterm)); |
453 | sa_sigterm.sa_handler = termsig_handler; | |
454 | sigaction(SIGTERM, &sa_sigterm, NULL); | |
10f5bff6 | 455 | qemu_init_exec_dir(argv[0]); |
bb345110 | 456 | |
7a5ca864 FB |
457 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { |
458 | switch (ch) { | |
459 | case 's': | |
2f726488 TS |
460 | flags |= BDRV_O_SNAPSHOT; |
461 | break; | |
462 | case 'n': | |
39a5235c PB |
463 | optarg = (char *) "none"; |
464 | /* fallthrough */ | |
465 | case QEMU_NBD_OPT_CACHE: | |
466 | if (seen_cache) { | |
467 | errx(EXIT_FAILURE, "-n and --cache can only be specified once"); | |
468 | } | |
469 | seen_cache = true; | |
470 | if (bdrv_parse_cache_flags(optarg, &flags) == -1) { | |
471 | errx(EXIT_FAILURE, "Invalid cache mode `%s'", optarg); | |
472 | } | |
7a5ca864 | 473 | break; |
39a5235c PB |
474 | #ifdef CONFIG_LINUX_AIO |
475 | case QEMU_NBD_OPT_AIO: | |
476 | if (seen_aio) { | |
477 | errx(EXIT_FAILURE, "--aio can only be specified once"); | |
478 | } | |
479 | seen_aio = true; | |
480 | if (!strcmp(optarg, "native")) { | |
481 | flags |= BDRV_O_NATIVE_AIO; | |
482 | } else if (!strcmp(optarg, "threads")) { | |
483 | /* this is the default */ | |
484 | } else { | |
485 | errx(EXIT_FAILURE, "invalid aio mode `%s'", optarg); | |
486 | } | |
487 | break; | |
488 | #endif | |
ded9d2d5 PB |
489 | case QEMU_NBD_OPT_DISCARD: |
490 | if (seen_discard) { | |
491 | errx(EXIT_FAILURE, "--discard can only be specified once"); | |
492 | } | |
493 | seen_discard = true; | |
494 | if (bdrv_parse_discard_flags(optarg, &flags) == -1) { | |
495 | errx(EXIT_FAILURE, "Invalid discard mode `%s'", optarg); | |
496 | } | |
497 | break; | |
b3838a40 PL |
498 | case QEMU_NBD_OPT_DETECT_ZEROES: |
499 | detect_zeroes = | |
500 | qapi_enum_parse(BlockdevDetectZeroesOptions_lookup, | |
501 | optarg, | |
502 | BLOCKDEV_DETECT_ZEROES_OPTIONS_MAX, | |
503 | BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF, | |
504 | &local_err); | |
505 | if (local_err) { | |
506 | errx(EXIT_FAILURE, "Failed to parse detect_zeroes mode: %s", | |
507 | error_get_pretty(local_err)); | |
508 | } | |
509 | if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP && | |
510 | !(flags & BDRV_O_UNMAP)) { | |
511 | errx(EXIT_FAILURE, "setting detect-zeroes to unmap is not allowed " | |
512 | "without setting discard operation to unmap"); | |
513 | } | |
514 | break; | |
7a5ca864 FB |
515 | case 'b': |
516 | bindto = optarg; | |
517 | break; | |
518 | case 'p': | |
519 | li = strtol(optarg, &end, 0); | |
520 | if (*end) { | |
b6353bea | 521 | errx(EXIT_FAILURE, "Invalid port `%s'", optarg); |
7a5ca864 FB |
522 | } |
523 | if (li < 1 || li > 65535) { | |
b6353bea | 524 | errx(EXIT_FAILURE, "Port out of range `%s'", optarg); |
7a5ca864 FB |
525 | } |
526 | port = (uint16_t)li; | |
527 | break; | |
528 | case 'o': | |
529 | dev_offset = strtoll (optarg, &end, 0); | |
530 | if (*end) { | |
b6353bea | 531 | errx(EXIT_FAILURE, "Invalid offset `%s'", optarg); |
7a5ca864 FB |
532 | } |
533 | if (dev_offset < 0) { | |
b6353bea | 534 | errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg); |
7a5ca864 FB |
535 | } |
536 | break; | |
8c116b0e WX |
537 | case 'l': |
538 | if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) { | |
539 | sn_opts = qemu_opts_parse(&internal_snapshot_opts, optarg, 0); | |
540 | if (!sn_opts) { | |
541 | errx(EXIT_FAILURE, "Failed in parsing snapshot param `%s'", | |
542 | optarg); | |
543 | } | |
544 | } else { | |
545 | sn_id_or_name = optarg; | |
546 | } | |
547 | /* fall through */ | |
7a5ca864 | 548 | case 'r': |
b90fb4b8 | 549 | nbdflags |= NBD_FLAG_READ_ONLY; |
07108b29 | 550 | flags &= ~BDRV_O_RDWR; |
7a5ca864 FB |
551 | break; |
552 | case 'P': | |
553 | partition = strtol(optarg, &end, 0); | |
713cc671 | 554 | if (*end) { |
b6353bea | 555 | errx(EXIT_FAILURE, "Invalid partition `%s'", optarg); |
713cc671 PL |
556 | } |
557 | if (partition < 1 || partition > 8) { | |
b6353bea | 558 | errx(EXIT_FAILURE, "Invalid partition %d", partition); |
713cc671 | 559 | } |
7a5ca864 | 560 | break; |
cd831bd7 | 561 | case 'k': |
b32f6c28 | 562 | sockpath = optarg; |
713cc671 | 563 | if (sockpath[0] != '/') { |
b6353bea | 564 | errx(EXIT_FAILURE, "socket path must be absolute\n"); |
713cc671 | 565 | } |
cd831bd7 TS |
566 | break; |
567 | case 'd': | |
568 | disconnect = true; | |
569 | break; | |
570 | case 'c': | |
571 | device = optarg; | |
572 | break; | |
3b05a8e9 TS |
573 | case 'e': |
574 | shared = strtol(optarg, &end, 0); | |
575 | if (*end) { | |
b6353bea | 576 | errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg); |
3b05a8e9 TS |
577 | } |
578 | if (shared < 1) { | |
b6353bea | 579 | errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); |
3b05a8e9 TS |
580 | } |
581 | break; | |
e6b63677 DB |
582 | case 'f': |
583 | fmt = optarg; | |
584 | break; | |
713cc671 PL |
585 | case 't': |
586 | persistent = 1; | |
587 | break; | |
7a5ca864 FB |
588 | case 'v': |
589 | verbose = 1; | |
590 | break; | |
591 | case 'V': | |
592 | version(argv[0]); | |
593 | exit(0); | |
594 | break; | |
595 | case 'h': | |
596 | usage(argv[0]); | |
597 | exit(0); | |
598 | break; | |
599 | case '?': | |
b6353bea | 600 | errx(EXIT_FAILURE, "Try `%s --help' for more information.", |
7a5ca864 FB |
601 | argv[0]); |
602 | } | |
603 | } | |
604 | ||
605 | if ((argc - optind) != 1) { | |
b6353bea | 606 | errx(EXIT_FAILURE, "Invalid number of argument.\n" |
7a5ca864 FB |
607 | "Try `%s --help' for more information.", |
608 | argv[0]); | |
609 | } | |
610 | ||
cd831bd7 TS |
611 | if (disconnect) { |
612 | fd = open(argv[optind], O_RDWR); | |
fc19f8a0 | 613 | if (fd < 0) { |
cb7cf0e3 | 614 | err(EXIT_FAILURE, "Cannot open %s", argv[optind]); |
fc19f8a0 | 615 | } |
cd831bd7 TS |
616 | nbd_disconnect(fd); |
617 | ||
618 | close(fd); | |
619 | ||
620 | printf("%s disconnected\n", argv[optind]); | |
621 | ||
713cc671 | 622 | return 0; |
cd831bd7 TS |
623 | } |
624 | ||
c1f8fdc3 PB |
625 | if (device && !verbose) { |
626 | int stderr_fd[2]; | |
627 | pid_t pid; | |
628 | int ret; | |
629 | ||
fc19f8a0 | 630 | if (qemu_pipe(stderr_fd) < 0) { |
c1f8fdc3 PB |
631 | err(EXIT_FAILURE, "Error setting up communication pipe"); |
632 | } | |
633 | ||
634 | /* Now daemonize, but keep a communication channel open to | |
635 | * print errors and exit with the proper status code. | |
636 | */ | |
637 | pid = fork(); | |
638 | if (pid == 0) { | |
639 | close(stderr_fd[0]); | |
9faf31b6 | 640 | ret = qemu_daemon(1, 0); |
c1f8fdc3 PB |
641 | |
642 | /* Temporarily redirect stderr to the parent's pipe... */ | |
643 | dup2(stderr_fd[1], STDERR_FILENO); | |
fc19f8a0 | 644 | if (ret < 0) { |
c1f8fdc3 PB |
645 | err(EXIT_FAILURE, "Failed to daemonize"); |
646 | } | |
647 | ||
648 | /* ... close the descriptor we inherited and go on. */ | |
649 | close(stderr_fd[1]); | |
650 | } else { | |
651 | bool errors = false; | |
652 | char *buf; | |
653 | ||
654 | /* In the parent. Print error messages from the child until | |
655 | * it closes the pipe. | |
656 | */ | |
657 | close(stderr_fd[1]); | |
658 | buf = g_malloc(1024); | |
659 | while ((ret = read(stderr_fd[0], buf, 1024)) > 0) { | |
660 | errors = true; | |
661 | ret = qemu_write_full(STDERR_FILENO, buf, ret); | |
fc19f8a0 | 662 | if (ret < 0) { |
c1f8fdc3 PB |
663 | exit(EXIT_FAILURE); |
664 | } | |
665 | } | |
fc19f8a0 | 666 | if (ret < 0) { |
c1f8fdc3 PB |
667 | err(EXIT_FAILURE, "Cannot read from daemon"); |
668 | } | |
669 | ||
670 | /* Usually the daemon should not print any message. | |
671 | * Exit with zero status in that case. | |
672 | */ | |
673 | exit(errors); | |
674 | } | |
675 | } | |
676 | ||
a6ac2313 PB |
677 | if (device != NULL && sockpath == NULL) { |
678 | sockpath = g_malloc(128); | |
679 | snprintf(sockpath, 128, SOCKET_PATH, basename(device)); | |
cd831bd7 TS |
680 | } |
681 | ||
2f78e491 | 682 | if (qemu_init_main_loop(&local_err)) { |
565f65d2 | 683 | error_report_err(local_err); |
2f78e491 CN |
684 | exit(EXIT_FAILURE); |
685 | } | |
802ddc37 PB |
686 | bdrv_init(); |
687 | atexit(bdrv_close_all); | |
688 | ||
e6b63677 | 689 | if (fmt) { |
4fbec260 HR |
690 | options = qdict_new(); |
691 | qdict_put(options, "driver", qstring_from_str(fmt)); | |
e6b63677 DB |
692 | } |
693 | ||
802ddc37 | 694 | srcpath = argv[optind]; |
4fbec260 HR |
695 | blk = blk_new_open("hda", srcpath, NULL, options, flags, &local_err); |
696 | if (!blk) { | |
697 | errx(EXIT_FAILURE, "Failed to blk_new_open '%s': %s", argv[optind], | |
698 | error_get_pretty(local_err)); | |
802ddc37 | 699 | } |
4fbec260 | 700 | bs = blk_bs(blk); |
802ddc37 | 701 | |
8c116b0e WX |
702 | if (sn_opts) { |
703 | ret = bdrv_snapshot_load_tmp(bs, | |
704 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID), | |
705 | qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME), | |
706 | &local_err); | |
707 | } else if (sn_id_or_name) { | |
708 | ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name, | |
709 | &local_err); | |
710 | } | |
711 | if (ret < 0) { | |
712 | errno = -ret; | |
713 | err(EXIT_FAILURE, | |
714 | "Failed to load snapshot: %s", | |
715 | error_get_pretty(local_err)); | |
716 | } | |
717 | ||
b3838a40 | 718 | bs->detect_zeroes = detect_zeroes; |
4c58e80a | 719 | fd_size = blk_getlength(blk); |
802ddc37 | 720 | |
185b4338 | 721 | if (partition != -1) { |
4c58e80a | 722 | ret = find_partition(blk, partition, &dev_offset, &fd_size); |
185b4338 PB |
723 | if (ret < 0) { |
724 | errno = -ret; | |
725 | err(EXIT_FAILURE, "Could not find partition %d", partition); | |
726 | } | |
802ddc37 PB |
727 | } |
728 | ||
e140177d | 729 | exp = nbd_export_new(blk, dev_offset, fd_size, nbdflags, nbd_export_closed); |
3b05a8e9 | 730 | |
b32f6c28 | 731 | if (sockpath) { |
a61c6782 | 732 | fd = unix_socket_incoming(sockpath); |
cd831bd7 | 733 | } else { |
a61c6782 | 734 | fd = tcp_socket_incoming(bindto, port); |
cd831bd7 TS |
735 | } |
736 | ||
fc19f8a0 | 737 | if (fd < 0) { |
7a5ca864 | 738 | return 1; |
a61c6782 | 739 | } |
f1ef5555 PB |
740 | |
741 | if (device) { | |
742 | int ret; | |
743 | ||
a6ac2313 | 744 | ret = pthread_create(&client_thread, NULL, nbd_client_thread, device); |
f1ef5555 PB |
745 | if (ret != 0) { |
746 | errx(EXIT_FAILURE, "Failed to create client thread: %s", | |
747 | strerror(ret)); | |
748 | } | |
749 | } else { | |
750 | /* Shut up GCC warnings. */ | |
751 | memset(&client_thread, 0, sizeof(client_thread)); | |
752 | } | |
753 | ||
a61c6782 PB |
754 | qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL, |
755 | (void *)(uintptr_t)fd); | |
7a5ca864 | 756 | |
9faf31b6 MT |
757 | /* now when the initialization is (almost) complete, chdir("/") |
758 | * to free any busy filesystems */ | |
759 | if (chdir("/") < 0) { | |
760 | err(EXIT_FAILURE, "Could not chdir to root directory"); | |
761 | } | |
762 | ||
7860a380 | 763 | state = RUNNING; |
3b05a8e9 | 764 | do { |
a61c6782 | 765 | main_loop_wait(false); |
7860a380 PB |
766 | if (state == TERMINATE) { |
767 | state = TERMINATING; | |
768 | nbd_export_close(exp); | |
769 | nbd_export_put(exp); | |
770 | exp = NULL; | |
771 | } | |
772 | } while (state != TERMINATED); | |
7a5ca864 | 773 | |
26f54e9a | 774 | blk_unref(blk); |
b32f6c28 PB |
775 | if (sockpath) { |
776 | unlink(sockpath); | |
777 | } | |
7a5ca864 | 778 | |
fbf28a43 | 779 | qemu_opts_del(sn_opts); |
8c116b0e | 780 | |
a517e88b PB |
781 | if (device) { |
782 | void *ret; | |
783 | pthread_join(client_thread, &ret); | |
784 | exit(ret != NULL); | |
785 | } else { | |
786 | exit(EXIT_SUCCESS); | |
787 | } | |
7a5ca864 | 788 | } |