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