]> git.proxmox.com Git - qemu.git/blame - qemu-nbd.c
qemu-nbd: reorganize help message
[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"
38ceff04 20#include "block.h"
7a5ca864
FB
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>
a517e88b 34#include <pthread.h>
cd831bd7
TS
35
36#define SOCKET_PATH "/var/lock/qemu-nbd-%s"
7a5ca864 37
af49bbbe 38static NBDExport *exp;
b1d8e52e 39static int verbose;
a517e88b
PB
40static char *srcpath;
41static char *sockpath;
a61c6782
PB
42static bool sigterm_reported;
43static bool nbd_started;
44static int shared = 1;
45static int nb_fds;
7a5ca864
FB
46
47static void usage(const char *name)
48{
b033cd86 49 (printf) (
7a5ca864
FB
50"Usage: %s [OPTIONS] FILE\n"
51"QEMU Disk Network Block Device Server\n"
52"\n"
b033cd86
PB
53" -h, --help display this help and exit\n"
54" -V, --version output version information and exit\n"
55"\n"
56"Connection properties:\n"
c2e2872b 57" -p, --port=PORT port to listen on (default `%d')\n"
7a5ca864 58" -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
cd831bd7
TS
59" -k, --socket=PATH path to the unix socket\n"
60" (default '"SOCKET_PATH"')\n"
3b05a8e9 61" -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
75818250 62" -t, --persistent don't exit on the last connection\n"
7a5ca864 63" -v, --verbose display extra debugging information\n"
7a5ca864 64"\n"
b033cd86
PB
65"Exposing part of the image:\n"
66" -o, --offset=OFFSET offset into the image\n"
67" -P, --partition=NUM only expose partition NUM\n"
68"\n"
69#ifdef __linux__
70"Kernel NBD client support:\n"
71" -c, --connect=DEV connect FILE to the local NBD device DEV\n"
72" -d, --disconnect disconnect the specified device\n"
73"\n"
74#endif
75"\n"
76"Block device options:\n"
77" -r, --read-only export read-only\n"
78" -s, --snapshot use snapshot file\n"
79" -n, --nocache disable host cache\n"
80"\n"
81"Report bugs to <qemu-devel@nongnu.org>\n"
c2e2872b 82 , name, NBD_DEFAULT_PORT, "DEVICE");
7a5ca864
FB
83}
84
85static void version(const char *name)
86{
87 printf(
315bc7aa 88"%s version 0.0.1\n"
7a5ca864
FB
89"Written by Anthony Liguori.\n"
90"\n"
91"Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
92"This is free software; see the source for copying conditions. There is NO\n"
93"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
315bc7aa 94 , name);
7a5ca864
FB
95}
96
97struct partition_record
98{
99 uint8_t bootable;
100 uint8_t start_head;
101 uint32_t start_cylinder;
102 uint8_t start_sector;
103 uint8_t system;
104 uint8_t end_head;
105 uint8_t end_cylinder;
106 uint8_t end_sector;
107 uint32_t start_sector_abs;
108 uint32_t nb_sectors_abs;
109};
110
111static void read_partition(uint8_t *p, struct partition_record *r)
112{
113 r->bootable = p[0];
114 r->start_head = p[1];
115 r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
116 r->start_sector = p[2] & 0x3f;
117 r->system = p[4];
118 r->end_head = p[5];
119 r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
120 r->end_sector = p[6] & 0x3f;
121 r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
122 r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
123}
124
125static int find_partition(BlockDriverState *bs, int partition,
126 off_t *offset, off_t *size)
127{
128 struct partition_record mbr[4];
129 uint8_t data[512];
130 int i;
131 int ext_partnum = 4;
cb7cf0e3 132 int ret;
7a5ca864 133
cb7cf0e3
RO
134 if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
135 errno = -ret;
136 err(EXIT_FAILURE, "error while reading");
137 }
7a5ca864
FB
138
139 if (data[510] != 0x55 || data[511] != 0xaa) {
185b4338 140 return -EINVAL;
7a5ca864
FB
141 }
142
143 for (i = 0; i < 4; i++) {
144 read_partition(&data[446 + 16 * i], &mbr[i]);
145
146 if (!mbr[i].nb_sectors_abs)
147 continue;
148
149 if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
150 struct partition_record ext[4];
151 uint8_t data1[512];
152 int j;
153
cb7cf0e3
RO
154 if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
155 errno = -ret;
156 err(EXIT_FAILURE, "error while reading");
157 }
7a5ca864
FB
158
159 for (j = 0; j < 4; j++) {
160 read_partition(&data1[446 + 16 * j], &ext[j]);
161 if (!ext[j].nb_sectors_abs)
162 continue;
163
164 if ((ext_partnum + j + 1) == partition) {
165 *offset = (uint64_t)ext[j].start_sector_abs << 9;
166 *size = (uint64_t)ext[j].nb_sectors_abs << 9;
167 return 0;
168 }
169 }
170 ext_partnum += 4;
171 } else if ((i + 1) == partition) {
172 *offset = (uint64_t)mbr[i].start_sector_abs << 9;
173 *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
174 return 0;
175 }
176 }
177
185b4338 178 return -ENOENT;
7a5ca864
FB
179}
180
bb345110
PB
181static void termsig_handler(int signum)
182{
a61c6782
PB
183 sigterm_reported = true;
184 qemu_notify_event();
bb345110
PB
185}
186
a517e88b 187static void *show_parts(void *arg)
cd831bd7 188{
a6ac2313 189 char *device = arg;
a517e88b
PB
190 int nbd;
191
192 /* linux just needs an open() to trigger
193 * the partition table update
194 * but remember to load the module with max_part != 0 :
195 * modprobe nbd max_part=63
196 */
197 nbd = open(device, O_RDWR);
fc19f8a0 198 if (nbd >= 0) {
a517e88b
PB
199 close(nbd);
200 }
201 return NULL;
202}
cd831bd7 203
a517e88b
PB
204static void *nbd_client_thread(void *arg)
205{
a6ac2313 206 char *device = arg;
a517e88b
PB
207 off_t size;
208 size_t blocksize;
209 uint32_t nbdflags;
a6ac2313 210 int fd, sock;
a517e88b
PB
211 int ret;
212 pthread_t show_parts_thread;
213
dc10e8b3 214 sock = unix_socket_outgoing(sockpath);
fc19f8a0 215 if (sock < 0) {
dc10e8b3
SH
216 goto out;
217 }
a517e88b
PB
218
219 ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
220 &size, &blocksize);
fc19f8a0 221 if (ret < 0) {
a517e88b
PB
222 goto out;
223 }
224
a6ac2313 225 fd = open(device, O_RDWR);
fc19f8a0 226 if (fd < 0) {
a6ac2313
PB
227 /* Linux-only, we can use %m in printf. */
228 fprintf(stderr, "Failed to open %s: %m", device);
229 goto out;
230 }
231
a517e88b 232 ret = nbd_init(fd, sock, nbdflags, size, blocksize);
fc19f8a0 233 if (ret < 0) {
a517e88b
PB
234 goto out;
235 }
236
237 /* update partition table */
a6ac2313 238 pthread_create(&show_parts_thread, NULL, show_parts, device);
a517e88b 239
c1f8fdc3
PB
240 if (verbose) {
241 fprintf(stderr, "NBD device %s is now connected to %s\n",
242 device, srcpath);
243 } else {
244 /* Close stderr so that the qemu-nbd process exits. */
245 dup2(STDOUT_FILENO, STDERR_FILENO);
246 }
a517e88b
PB
247
248 ret = nbd_client(fd);
249 if (ret) {
250 goto out;
cd831bd7 251 }
a517e88b
PB
252 close(fd);
253 kill(getpid(), SIGTERM);
254 return (void *) EXIT_SUCCESS;
255
256out:
257 kill(getpid(), SIGTERM);
258 return (void *) EXIT_FAILURE;
cd831bd7
TS
259}
260
a61c6782
PB
261static int nbd_can_accept(void *opaque)
262{
263 return nb_fds < shared;
264}
265
1743b515 266static void nbd_client_closed(NBDClient *client)
a61c6782 267{
1743b515
PB
268 nb_fds--;
269 qemu_notify_event();
a61c6782
PB
270}
271
272static void nbd_accept(void *opaque)
273{
274 int server_fd = (uintptr_t) opaque;
275 struct sockaddr_in addr;
276 socklen_t addr_len = sizeof(addr);
277
278 int fd = accept(server_fd, (struct sockaddr *)&addr, &addr_len);
279 nbd_started = true;
fc19f8a0 280 if (fd >= 0 && nbd_client_new(exp, fd, nbd_client_closed)) {
a61c6782
PB
281 nb_fds++;
282 }
283}
284
7a5ca864
FB
285int main(int argc, char **argv)
286{
287 BlockDriverState *bs;
288 off_t dev_offset = 0;
b90fb4b8 289 uint32_t nbdflags = 0;
cd831bd7 290 bool disconnect = false;
7a5ca864 291 const char *bindto = "0.0.0.0";
a6ac2313 292 char *device = NULL;
c2e2872b 293 int port = NBD_DEFAULT_PORT;
7a5ca864 294 off_t fd_size;
d6aa671f 295 const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
7a5ca864 296 struct option lopt[] = {
660f11be
BS
297 { "help", 0, NULL, 'h' },
298 { "version", 0, NULL, 'V' },
299 { "bind", 1, NULL, 'b' },
300 { "port", 1, NULL, 'p' },
301 { "socket", 1, NULL, 'k' },
302 { "offset", 1, NULL, 'o' },
303 { "read-only", 0, NULL, 'r' },
304 { "partition", 1, NULL, 'P' },
305 { "connect", 1, NULL, 'c' },
306 { "disconnect", 0, NULL, 'd' },
307 { "snapshot", 0, NULL, 's' },
308 { "nocache", 0, NULL, 'n' },
309 { "shared", 1, NULL, 'e' },
310 { "persistent", 0, NULL, 't' },
311 { "verbose", 0, NULL, 'v' },
312 { NULL, 0, NULL, 0 }
7a5ca864
FB
313 };
314 int ch;
315 int opt_ind = 0;
316 int li;
317 char *end;
f5edb014 318 int flags = BDRV_O_RDWR;
7a5ca864 319 int partition = -1;
cd831bd7 320 int ret;
3b05a8e9 321 int fd;
75818250 322 int persistent = 0;
a517e88b 323 pthread_t client_thread;
7a5ca864 324
a517e88b
PB
325 /* The client thread uses SIGTERM to interrupt the server. A signal
326 * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
327 */
bb345110 328 struct sigaction sa_sigterm;
bb345110
PB
329 memset(&sa_sigterm, 0, sizeof(sa_sigterm));
330 sa_sigterm.sa_handler = termsig_handler;
331 sigaction(SIGTERM, &sa_sigterm, NULL);
332
7a5ca864
FB
333 while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
334 switch (ch) {
335 case 's':
2f726488
TS
336 flags |= BDRV_O_SNAPSHOT;
337 break;
338 case 'n':
a6599793 339 flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
7a5ca864
FB
340 break;
341 case 'b':
342 bindto = optarg;
343 break;
344 case 'p':
345 li = strtol(optarg, &end, 0);
346 if (*end) {
b6353bea 347 errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
7a5ca864
FB
348 }
349 if (li < 1 || li > 65535) {
b6353bea 350 errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
7a5ca864
FB
351 }
352 port = (uint16_t)li;
353 break;
354 case 'o':
355 dev_offset = strtoll (optarg, &end, 0);
356 if (*end) {
b6353bea 357 errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
7a5ca864
FB
358 }
359 if (dev_offset < 0) {
b6353bea 360 errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
7a5ca864
FB
361 }
362 break;
363 case 'r':
b90fb4b8 364 nbdflags |= NBD_FLAG_READ_ONLY;
07108b29 365 flags &= ~BDRV_O_RDWR;
7a5ca864
FB
366 break;
367 case 'P':
368 partition = strtol(optarg, &end, 0);
369 if (*end)
b6353bea 370 errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
7a5ca864 371 if (partition < 1 || partition > 8)
b6353bea 372 errx(EXIT_FAILURE, "Invalid partition %d", partition);
7a5ca864 373 break;
cd831bd7 374 case 'k':
b32f6c28
PB
375 sockpath = optarg;
376 if (sockpath[0] != '/')
b6353bea 377 errx(EXIT_FAILURE, "socket path must be absolute\n");
cd831bd7
TS
378 break;
379 case 'd':
380 disconnect = true;
381 break;
382 case 'c':
383 device = optarg;
384 break;
3b05a8e9
TS
385 case 'e':
386 shared = strtol(optarg, &end, 0);
387 if (*end) {
b6353bea 388 errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
3b05a8e9
TS
389 }
390 if (shared < 1) {
b6353bea 391 errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
3b05a8e9
TS
392 }
393 break;
75818250
TS
394 case 't':
395 persistent = 1;
396 break;
7a5ca864
FB
397 case 'v':
398 verbose = 1;
399 break;
400 case 'V':
401 version(argv[0]);
402 exit(0);
403 break;
404 case 'h':
405 usage(argv[0]);
406 exit(0);
407 break;
408 case '?':
b6353bea 409 errx(EXIT_FAILURE, "Try `%s --help' for more information.",
7a5ca864
FB
410 argv[0]);
411 }
412 }
413
414 if ((argc - optind) != 1) {
b6353bea 415 errx(EXIT_FAILURE, "Invalid number of argument.\n"
7a5ca864
FB
416 "Try `%s --help' for more information.",
417 argv[0]);
418 }
419
cd831bd7
TS
420 if (disconnect) {
421 fd = open(argv[optind], O_RDWR);
fc19f8a0 422 if (fd < 0) {
cb7cf0e3 423 err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
fc19f8a0 424 }
cd831bd7
TS
425 nbd_disconnect(fd);
426
427 close(fd);
428
429 printf("%s disconnected\n", argv[optind]);
430
431 return 0;
432 }
433
c1f8fdc3
PB
434 if (device && !verbose) {
435 int stderr_fd[2];
436 pid_t pid;
437 int ret;
438
fc19f8a0 439 if (qemu_pipe(stderr_fd) < 0) {
c1f8fdc3
PB
440 err(EXIT_FAILURE, "Error setting up communication pipe");
441 }
442
443 /* Now daemonize, but keep a communication channel open to
444 * print errors and exit with the proper status code.
445 */
446 pid = fork();
447 if (pid == 0) {
448 close(stderr_fd[0]);
9faf31b6 449 ret = qemu_daemon(1, 0);
c1f8fdc3
PB
450
451 /* Temporarily redirect stderr to the parent's pipe... */
452 dup2(stderr_fd[1], STDERR_FILENO);
fc19f8a0 453 if (ret < 0) {
c1f8fdc3
PB
454 err(EXIT_FAILURE, "Failed to daemonize");
455 }
456
457 /* ... close the descriptor we inherited and go on. */
458 close(stderr_fd[1]);
459 } else {
460 bool errors = false;
461 char *buf;
462
463 /* In the parent. Print error messages from the child until
464 * it closes the pipe.
465 */
466 close(stderr_fd[1]);
467 buf = g_malloc(1024);
468 while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
469 errors = true;
470 ret = qemu_write_full(STDERR_FILENO, buf, ret);
fc19f8a0 471 if (ret < 0) {
c1f8fdc3
PB
472 exit(EXIT_FAILURE);
473 }
474 }
fc19f8a0 475 if (ret < 0) {
c1f8fdc3
PB
476 err(EXIT_FAILURE, "Cannot read from daemon");
477 }
478
479 /* Usually the daemon should not print any message.
480 * Exit with zero status in that case.
481 */
482 exit(errors);
483 }
484 }
485
a6ac2313
PB
486 if (device != NULL && sockpath == NULL) {
487 sockpath = g_malloc(128);
488 snprintf(sockpath, 128, SOCKET_PATH, basename(device));
cd831bd7
TS
489 }
490
802ddc37
PB
491 bdrv_init();
492 atexit(bdrv_close_all);
493
494 bs = bdrv_new("hda");
495 srcpath = argv[optind];
496 if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) {
497 errno = -ret;
498 err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
499 }
500
38ceff04 501 fd_size = bdrv_getlength(bs);
802ddc37 502
185b4338
PB
503 if (partition != -1) {
504 ret = find_partition(bs, partition, &dev_offset, &fd_size);
505 if (ret < 0) {
506 errno = -ret;
507 err(EXIT_FAILURE, "Could not find partition %d", partition);
508 }
802ddc37
PB
509 }
510
af49bbbe 511 exp = nbd_export_new(bs, dev_offset, fd_size, nbdflags);
3b05a8e9 512
b32f6c28 513 if (sockpath) {
a61c6782 514 fd = unix_socket_incoming(sockpath);
cd831bd7 515 } else {
a61c6782 516 fd = tcp_socket_incoming(bindto, port);
cd831bd7
TS
517 }
518
fc19f8a0 519 if (fd < 0) {
7a5ca864 520 return 1;
a61c6782 521 }
f1ef5555
PB
522
523 if (device) {
524 int ret;
525
a6ac2313 526 ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
f1ef5555
PB
527 if (ret != 0) {
528 errx(EXIT_FAILURE, "Failed to create client thread: %s",
529 strerror(ret));
530 }
531 } else {
532 /* Shut up GCC warnings. */
533 memset(&client_thread, 0, sizeof(client_thread));
534 }
535
a61c6782
PB
536 qemu_init_main_loop();
537 qemu_set_fd_handler2(fd, nbd_can_accept, nbd_accept, NULL,
538 (void *)(uintptr_t)fd);
7a5ca864 539
9faf31b6
MT
540 /* now when the initialization is (almost) complete, chdir("/")
541 * to free any busy filesystems */
542 if (chdir("/") < 0) {
543 err(EXIT_FAILURE, "Could not chdir to root directory");
544 }
545
3b05a8e9 546 do {
a61c6782
PB
547 main_loop_wait(false);
548 } while (!sigterm_reported && (persistent || !nbd_started || nb_fds > 0));
7a5ca864 549
af49bbbe 550 nbd_export_close(exp);
b32f6c28
PB
551 if (sockpath) {
552 unlink(sockpath);
553 }
7a5ca864 554
a517e88b
PB
555 if (device) {
556 void *ret;
557 pthread_join(client_thread, &ret);
558 exit(ret != NULL);
559 } else {
560 exit(EXIT_SUCCESS);
561 }
7a5ca864 562}