]>
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 | ||
19 | #include <qemu-common.h> | |
20 | #include "block_int.h" | |
21 | #include "nbd.h" | |
22 | ||
7a5ca864 FB |
23 | #include <stdarg.h> |
24 | #include <stdio.h> | |
25 | #include <getopt.h> | |
26 | #include <err.h> | |
cd831bd7 | 27 | #include <sys/types.h> |
7a5ca864 FB |
28 | #include <sys/socket.h> |
29 | #include <netinet/in.h> | |
30 | #include <netinet/tcp.h> | |
31 | #include <arpa/inet.h> | |
cd831bd7 | 32 | #include <signal.h> |
2bff4b6f | 33 | #include <libgen.h> |
cd831bd7 TS |
34 | |
35 | #define SOCKET_PATH "/var/lock/qemu-nbd-%s" | |
7a5ca864 | 36 | |
2f726488 TS |
37 | #define NBD_BUFFER_SIZE (1024*1024) |
38 | ||
b1d8e52e | 39 | static int verbose; |
7a5ca864 FB |
40 | |
41 | static void usage(const char *name) | |
42 | { | |
43 | printf( | |
44 | "Usage: %s [OPTIONS] FILE\n" | |
45 | "QEMU Disk Network Block Device Server\n" | |
46 | "\n" | |
c2e2872b | 47 | " -p, --port=PORT port to listen on (default `%d')\n" |
7a5ca864 FB |
48 | " -o, --offset=OFFSET offset into the image\n" |
49 | " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n" | |
cd831bd7 TS |
50 | " -k, --socket=PATH path to the unix socket\n" |
51 | " (default '"SOCKET_PATH"')\n" | |
7a5ca864 FB |
52 | " -r, --read-only export read-only\n" |
53 | " -P, --partition=NUM only expose partition NUM\n" | |
2f726488 TS |
54 | " -s, --snapshot use snapshot file\n" |
55 | " -n, --nocache disable host cache\n" | |
cd831bd7 TS |
56 | " -c, --connect=DEV connect FILE to the local NBD device DEV\n" |
57 | " -d, --disconnect disconnect the specified device\n" | |
3b05a8e9 | 58 | " -e, --shared=NUM device can be shared by NUM clients (default '1')\n" |
75818250 | 59 | " -t, --persistent don't exit on the last connection\n" |
7a5ca864 FB |
60 | " -v, --verbose display extra debugging information\n" |
61 | " -h, --help display this help and exit\n" | |
62 | " -V, --version output version information and exit\n" | |
63 | "\n" | |
64 | "Report bugs to <[email protected]>\n" | |
c2e2872b | 65 | , name, NBD_DEFAULT_PORT, "DEVICE"); |
7a5ca864 FB |
66 | } |
67 | ||
68 | static void version(const char *name) | |
69 | { | |
70 | printf( | |
315bc7aa | 71 | "%s version 0.0.1\n" |
7a5ca864 FB |
72 | "Written by Anthony Liguori.\n" |
73 | "\n" | |
74 | "Copyright (C) 2006 Anthony Liguori <[email protected]>.\n" | |
75 | "This is free software; see the source for copying conditions. There is NO\n" | |
76 | "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n" | |
315bc7aa | 77 | , name); |
7a5ca864 FB |
78 | } |
79 | ||
80 | struct partition_record | |
81 | { | |
82 | uint8_t bootable; | |
83 | uint8_t start_head; | |
84 | uint32_t start_cylinder; | |
85 | uint8_t start_sector; | |
86 | uint8_t system; | |
87 | uint8_t end_head; | |
88 | uint8_t end_cylinder; | |
89 | uint8_t end_sector; | |
90 | uint32_t start_sector_abs; | |
91 | uint32_t nb_sectors_abs; | |
92 | }; | |
93 | ||
94 | static void read_partition(uint8_t *p, struct partition_record *r) | |
95 | { | |
96 | r->bootable = p[0]; | |
97 | r->start_head = p[1]; | |
98 | r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300); | |
99 | r->start_sector = p[2] & 0x3f; | |
100 | r->system = p[4]; | |
101 | r->end_head = p[5]; | |
102 | r->end_cylinder = p[7] | ((p[6] << 2) & 0x300); | |
103 | r->end_sector = p[6] & 0x3f; | |
104 | r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24; | |
105 | r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24; | |
106 | } | |
107 | ||
108 | static int find_partition(BlockDriverState *bs, int partition, | |
109 | off_t *offset, off_t *size) | |
110 | { | |
111 | struct partition_record mbr[4]; | |
112 | uint8_t data[512]; | |
113 | int i; | |
114 | int ext_partnum = 4; | |
cb7cf0e3 | 115 | int ret; |
7a5ca864 | 116 | |
cb7cf0e3 RO |
117 | if ((ret = bdrv_read(bs, 0, data, 1)) < 0) { |
118 | errno = -ret; | |
119 | err(EXIT_FAILURE, "error while reading"); | |
120 | } | |
7a5ca864 FB |
121 | |
122 | if (data[510] != 0x55 || data[511] != 0xaa) { | |
123 | errno = -EINVAL; | |
124 | return -1; | |
125 | } | |
126 | ||
127 | for (i = 0; i < 4; i++) { | |
128 | read_partition(&data[446 + 16 * i], &mbr[i]); | |
129 | ||
130 | if (!mbr[i].nb_sectors_abs) | |
131 | continue; | |
132 | ||
133 | if (mbr[i].system == 0xF || mbr[i].system == 0x5) { | |
134 | struct partition_record ext[4]; | |
135 | uint8_t data1[512]; | |
136 | int j; | |
137 | ||
cb7cf0e3 RO |
138 | if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) { |
139 | errno = -ret; | |
140 | err(EXIT_FAILURE, "error while reading"); | |
141 | } | |
7a5ca864 FB |
142 | |
143 | for (j = 0; j < 4; j++) { | |
144 | read_partition(&data1[446 + 16 * j], &ext[j]); | |
145 | if (!ext[j].nb_sectors_abs) | |
146 | continue; | |
147 | ||
148 | if ((ext_partnum + j + 1) == partition) { | |
149 | *offset = (uint64_t)ext[j].start_sector_abs << 9; | |
150 | *size = (uint64_t)ext[j].nb_sectors_abs << 9; | |
151 | return 0; | |
152 | } | |
153 | } | |
154 | ext_partnum += 4; | |
155 | } else if ((i + 1) == partition) { | |
156 | *offset = (uint64_t)mbr[i].start_sector_abs << 9; | |
157 | *size = (uint64_t)mbr[i].nb_sectors_abs << 9; | |
158 | return 0; | |
159 | } | |
160 | } | |
161 | ||
162 | errno = -ENOENT; | |
163 | return -1; | |
164 | } | |
165 | ||
cd831bd7 TS |
166 | static void show_parts(const char *device) |
167 | { | |
168 | if (fork() == 0) { | |
169 | int nbd; | |
170 | ||
171 | /* linux just needs an open() to trigger | |
172 | * the partition table update | |
173 | * but remember to load the module with max_part != 0 : | |
174 | * modprobe nbd max_part=63 | |
175 | */ | |
176 | nbd = open(device, O_RDWR); | |
177 | if (nbd != -1) | |
178 | close(nbd); | |
179 | exit(0); | |
180 | } | |
181 | } | |
182 | ||
7a5ca864 FB |
183 | int main(int argc, char **argv) |
184 | { | |
185 | BlockDriverState *bs; | |
186 | off_t dev_offset = 0; | |
187 | off_t offset = 0; | |
188 | bool readonly = false; | |
cd831bd7 | 189 | bool disconnect = false; |
7a5ca864 | 190 | const char *bindto = "0.0.0.0"; |
c2e2872b | 191 | int port = NBD_DEFAULT_PORT; |
7a5ca864 FB |
192 | struct sockaddr_in addr; |
193 | socklen_t addr_len = sizeof(addr); | |
194 | off_t fd_size; | |
cd831bd7 TS |
195 | char *device = NULL; |
196 | char *socket = NULL; | |
197 | char sockpath[128]; | |
d6aa671f | 198 | const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t"; |
7a5ca864 | 199 | struct option lopt[] = { |
660f11be BS |
200 | { "help", 0, NULL, 'h' }, |
201 | { "version", 0, NULL, 'V' }, | |
202 | { "bind", 1, NULL, 'b' }, | |
203 | { "port", 1, NULL, 'p' }, | |
204 | { "socket", 1, NULL, 'k' }, | |
205 | { "offset", 1, NULL, 'o' }, | |
206 | { "read-only", 0, NULL, 'r' }, | |
207 | { "partition", 1, NULL, 'P' }, | |
208 | { "connect", 1, NULL, 'c' }, | |
209 | { "disconnect", 0, NULL, 'd' }, | |
210 | { "snapshot", 0, NULL, 's' }, | |
211 | { "nocache", 0, NULL, 'n' }, | |
212 | { "shared", 1, NULL, 'e' }, | |
213 | { "persistent", 0, NULL, 't' }, | |
214 | { "verbose", 0, NULL, 'v' }, | |
215 | { NULL, 0, NULL, 0 } | |
7a5ca864 FB |
216 | }; |
217 | int ch; | |
218 | int opt_ind = 0; | |
219 | int li; | |
220 | char *end; | |
f5edb014 | 221 | int flags = BDRV_O_RDWR; |
7a5ca864 | 222 | int partition = -1; |
cd831bd7 | 223 | int ret; |
3b05a8e9 | 224 | int shared = 1; |
2f726488 | 225 | uint8_t *data; |
3b05a8e9 TS |
226 | fd_set fds; |
227 | int *sharing_fds; | |
228 | int fd; | |
229 | int i; | |
230 | int nb_fds = 0; | |
231 | int max_fd; | |
75818250 | 232 | int persistent = 0; |
1d45f8b5 | 233 | uint32_t nbdflags; |
7a5ca864 FB |
234 | |
235 | while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) { | |
236 | switch (ch) { | |
237 | case 's': | |
2f726488 TS |
238 | flags |= BDRV_O_SNAPSHOT; |
239 | break; | |
240 | case 'n': | |
a6599793 | 241 | flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB; |
7a5ca864 FB |
242 | break; |
243 | case 'b': | |
244 | bindto = optarg; | |
245 | break; | |
246 | case 'p': | |
247 | li = strtol(optarg, &end, 0); | |
248 | if (*end) { | |
b6353bea | 249 | errx(EXIT_FAILURE, "Invalid port `%s'", optarg); |
7a5ca864 FB |
250 | } |
251 | if (li < 1 || li > 65535) { | |
b6353bea | 252 | errx(EXIT_FAILURE, "Port out of range `%s'", optarg); |
7a5ca864 FB |
253 | } |
254 | port = (uint16_t)li; | |
255 | break; | |
256 | case 'o': | |
257 | dev_offset = strtoll (optarg, &end, 0); | |
258 | if (*end) { | |
b6353bea | 259 | errx(EXIT_FAILURE, "Invalid offset `%s'", optarg); |
7a5ca864 FB |
260 | } |
261 | if (dev_offset < 0) { | |
b6353bea | 262 | errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg); |
7a5ca864 FB |
263 | } |
264 | break; | |
265 | case 'r': | |
266 | readonly = true; | |
07108b29 | 267 | flags &= ~BDRV_O_RDWR; |
7a5ca864 FB |
268 | break; |
269 | case 'P': | |
270 | partition = strtol(optarg, &end, 0); | |
271 | if (*end) | |
b6353bea | 272 | errx(EXIT_FAILURE, "Invalid partition `%s'", optarg); |
7a5ca864 | 273 | if (partition < 1 || partition > 8) |
b6353bea | 274 | errx(EXIT_FAILURE, "Invalid partition %d", partition); |
7a5ca864 | 275 | break; |
cd831bd7 TS |
276 | case 'k': |
277 | socket = optarg; | |
278 | if (socket[0] != '/') | |
b6353bea | 279 | errx(EXIT_FAILURE, "socket path must be absolute\n"); |
cd831bd7 TS |
280 | break; |
281 | case 'd': | |
282 | disconnect = true; | |
283 | break; | |
284 | case 'c': | |
285 | device = optarg; | |
286 | break; | |
3b05a8e9 TS |
287 | case 'e': |
288 | shared = strtol(optarg, &end, 0); | |
289 | if (*end) { | |
b6353bea | 290 | errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg); |
3b05a8e9 TS |
291 | } |
292 | if (shared < 1) { | |
b6353bea | 293 | errx(EXIT_FAILURE, "Shared device number must be greater than 0\n"); |
3b05a8e9 TS |
294 | } |
295 | break; | |
75818250 TS |
296 | case 't': |
297 | persistent = 1; | |
298 | break; | |
7a5ca864 FB |
299 | case 'v': |
300 | verbose = 1; | |
301 | break; | |
302 | case 'V': | |
303 | version(argv[0]); | |
304 | exit(0); | |
305 | break; | |
306 | case 'h': | |
307 | usage(argv[0]); | |
308 | exit(0); | |
309 | break; | |
310 | case '?': | |
b6353bea | 311 | errx(EXIT_FAILURE, "Try `%s --help' for more information.", |
7a5ca864 FB |
312 | argv[0]); |
313 | } | |
314 | } | |
315 | ||
316 | if ((argc - optind) != 1) { | |
b6353bea | 317 | errx(EXIT_FAILURE, "Invalid number of argument.\n" |
7a5ca864 FB |
318 | "Try `%s --help' for more information.", |
319 | argv[0]); | |
320 | } | |
321 | ||
cd831bd7 TS |
322 | if (disconnect) { |
323 | fd = open(argv[optind], O_RDWR); | |
324 | if (fd == -1) | |
cb7cf0e3 | 325 | err(EXIT_FAILURE, "Cannot open %s", argv[optind]); |
cd831bd7 TS |
326 | |
327 | nbd_disconnect(fd); | |
328 | ||
329 | close(fd); | |
330 | ||
331 | printf("%s disconnected\n", argv[optind]); | |
332 | ||
333 | return 0; | |
334 | } | |
335 | ||
7a5ca864 FB |
336 | bdrv_init(); |
337 | ||
338 | bs = bdrv_new("hda"); | |
7a5ca864 | 339 | |
cb7cf0e3 RO |
340 | if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) { |
341 | errno = -ret; | |
342 | err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]); | |
343 | } | |
7a5ca864 FB |
344 | |
345 | fd_size = bs->total_sectors * 512; | |
346 | ||
347 | if (partition != -1 && | |
348 | find_partition(bs, partition, &dev_offset, &fd_size)) | |
cb7cf0e3 | 349 | err(EXIT_FAILURE, "Could not find partition %d", partition); |
7a5ca864 | 350 | |
cd831bd7 | 351 | if (device) { |
3b05a8e9 TS |
352 | pid_t pid; |
353 | int sock; | |
354 | ||
cb7cf0e3 RO |
355 | /* want to fail before daemonizing */ |
356 | if (access(device, R_OK|W_OK) == -1) { | |
357 | err(EXIT_FAILURE, "Could not access '%s'", device); | |
358 | } | |
359 | ||
f5de141b AL |
360 | if (!verbose) { |
361 | /* detach client and server */ | |
f97742d0 | 362 | if (qemu_daemon(0, 0) == -1) { |
cb7cf0e3 | 363 | err(EXIT_FAILURE, "Failed to daemonize"); |
f5de141b AL |
364 | } |
365 | } | |
cd831bd7 TS |
366 | |
367 | if (socket == NULL) { | |
22ff51ee BS |
368 | snprintf(sockpath, sizeof(sockpath), SOCKET_PATH, |
369 | basename(device)); | |
cd831bd7 TS |
370 | socket = sockpath; |
371 | } | |
372 | ||
373 | pid = fork(); | |
374 | if (pid < 0) | |
375 | return 1; | |
376 | if (pid != 0) { | |
377 | off_t size; | |
378 | size_t blocksize; | |
379 | ||
380 | ret = 0; | |
381 | bdrv_close(bs); | |
382 | ||
383 | do { | |
384 | sock = unix_socket_outgoing(socket); | |
385 | if (sock == -1) { | |
cb7cf0e3 RO |
386 | if (errno != ENOENT && errno != ECONNREFUSED) { |
387 | ret = 1; | |
cd831bd7 | 388 | goto out; |
cb7cf0e3 | 389 | } |
cd831bd7 TS |
390 | sleep(1); /* wait children */ |
391 | } | |
392 | } while (sock == -1); | |
393 | ||
394 | fd = open(device, O_RDWR); | |
395 | if (fd == -1) { | |
396 | ret = 1; | |
397 | goto out; | |
398 | } | |
399 | ||
1d45f8b5 LV |
400 | ret = nbd_receive_negotiate(sock, NULL, &nbdflags, |
401 | &size, &blocksize); | |
cd831bd7 TS |
402 | if (ret == -1) { |
403 | ret = 1; | |
404 | goto out; | |
405 | } | |
406 | ||
407 | ret = nbd_init(fd, sock, size, blocksize); | |
408 | if (ret == -1) { | |
409 | ret = 1; | |
410 | goto out; | |
411 | } | |
412 | ||
413 | printf("NBD device %s is now connected to file %s\n", | |
414 | device, argv[optind]); | |
415 | ||
416 | /* update partition table */ | |
417 | ||
418 | show_parts(device); | |
419 | ||
e301b13d JS |
420 | ret = nbd_client(fd); |
421 | if (ret) { | |
422 | ret = 1; | |
423 | } | |
cd831bd7 TS |
424 | close(fd); |
425 | out: | |
426 | kill(pid, SIGTERM); | |
427 | unlink(socket); | |
428 | ||
429 | return ret; | |
430 | } | |
431 | /* children */ | |
432 | } | |
433 | ||
3b05a8e9 | 434 | sharing_fds = qemu_malloc((shared + 1) * sizeof(int)); |
3b05a8e9 | 435 | |
cd831bd7 | 436 | if (socket) { |
3b05a8e9 | 437 | sharing_fds[0] = unix_socket_incoming(socket); |
cd831bd7 | 438 | } else { |
3b05a8e9 | 439 | sharing_fds[0] = tcp_socket_incoming(bindto, port); |
cd831bd7 TS |
440 | } |
441 | ||
3b05a8e9 | 442 | if (sharing_fds[0] == -1) |
7a5ca864 | 443 | return 1; |
3b05a8e9 TS |
444 | max_fd = sharing_fds[0]; |
445 | nb_fds++; | |
7a5ca864 | 446 | |
72aef731 | 447 | data = qemu_blockalign(bs, NBD_BUFFER_SIZE); |
3b05a8e9 | 448 | if (data == NULL) |
b6353bea | 449 | errx(EXIT_FAILURE, "Cannot allocate data buffer"); |
7a5ca864 | 450 | |
3b05a8e9 | 451 | do { |
7a5ca864 | 452 | |
3b05a8e9 TS |
453 | FD_ZERO(&fds); |
454 | for (i = 0; i < nb_fds; i++) | |
455 | FD_SET(sharing_fds[i], &fds); | |
456 | ||
457 | ret = select(max_fd + 1, &fds, NULL, NULL, NULL); | |
458 | if (ret == -1) | |
459 | break; | |
460 | ||
461 | if (FD_ISSET(sharing_fds[0], &fds)) | |
462 | ret--; | |
463 | for (i = 1; i < nb_fds && ret; i++) { | |
464 | if (FD_ISSET(sharing_fds[i], &fds)) { | |
465 | if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset, | |
466 | &offset, readonly, data, NBD_BUFFER_SIZE) != 0) { | |
467 | close(sharing_fds[i]); | |
468 | nb_fds--; | |
469 | sharing_fds[i] = sharing_fds[nb_fds]; | |
470 | i--; | |
471 | } | |
472 | ret--; | |
473 | } | |
474 | } | |
475 | /* new connection ? */ | |
476 | if (FD_ISSET(sharing_fds[0], &fds)) { | |
477 | if (nb_fds < shared + 1) { | |
478 | sharing_fds[nb_fds] = accept(sharing_fds[0], | |
479 | (struct sockaddr *)&addr, | |
480 | &addr_len); | |
481 | if (sharing_fds[nb_fds] != -1 && | |
27982661 | 482 | nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) { |
3b05a8e9 TS |
483 | if (sharing_fds[nb_fds] > max_fd) |
484 | max_fd = sharing_fds[nb_fds]; | |
485 | nb_fds++; | |
486 | } | |
487 | } | |
488 | } | |
75818250 | 489 | } while (persistent || nb_fds > 1); |
f8a83245 | 490 | qemu_vfree(data); |
7a5ca864 | 491 | |
3b05a8e9 | 492 | close(sharing_fds[0]); |
7a5ca864 | 493 | bdrv_close(bs); |
3b05a8e9 | 494 | qemu_free(sharing_fds); |
cd831bd7 TS |
495 | if (socket) |
496 | unlink(socket); | |
7a5ca864 FB |
497 | |
498 | return 0; | |
499 | } |