nbd_client.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* nbd-client.c - network block device client
  2. *
  3. * Copyright 2010 Rob Landley <rob@landley.net>
  4. *
  5. * Not in SUSv4.
  6. // This little dance is because a NEWTOY with - in the name tries to do
  7. // things like prototype "nbd-client_main" which isn't a valid symbol. So
  8. // we hide the underscore name and OLDTOY the name we want.
  9. USE_NBD_CLIENT(NEWTOY(nbd_client, "<3>3ns", 0))
  10. USE_NBD_CLIENT(OLDTOY(nbd-client, nbd_client, TOYFLAG_USR|TOYFLAG_BIN))
  11. config NBD_CLIENT
  12. bool "nbd-client"
  13. depends on TOYBOX_FORK
  14. default y
  15. help
  16. usage: nbd-client [-ns] HOST PORT DEVICE
  17. -n Do not fork into background
  18. -s nbd swap support (lock server into memory)
  19. */
  20. /* TODO:
  21. usage: nbd-client [-sSpn] [-b BLKSZ] [-t SECS] [-N name] HOST PORT DEVICE
  22. -b block size
  23. -t timeout in seconds
  24. -S sdp
  25. -p persist
  26. -n nofork
  27. -d DEVICE
  28. -c DEVICE
  29. */
  30. #define FOR_nbd_client
  31. #include "toys.h"
  32. #include <linux/nbd.h>
  33. void nbd_client_main(void)
  34. {
  35. int sock = -1, nbd, flags;
  36. unsigned long timeout = 0;
  37. char *host=toys.optargs[0], *port=toys.optargs[1], *device=toys.optargs[2];
  38. uint64_t devsize;
  39. // Repeat until spanked
  40. nbd = xopen(device, O_RDWR);
  41. for (;;) {
  42. int temp;
  43. // Find and connect to server
  44. sock = xconnectany(xgetaddrinfo(host, port, AF_UNSPEC, SOCK_STREAM, 0, 0));
  45. temp = 1;
  46. setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, &temp, sizeof(int));
  47. // Read login data
  48. xreadall(sock, toybuf, 152);
  49. if (memcmp(toybuf, "NBDMAGIC\x00\x00\x42\x02\x81\x86\x12\x53", 16))
  50. error_exit("bad login %s:%s", host, port);
  51. devsize = SWAP_BE64(*(uint64_t *)(toybuf+16));
  52. flags = SWAP_BE32(*(int *)(toybuf+24));
  53. // Set 4k block size. Everything uses that these days.
  54. ioctl(nbd, NBD_SET_BLKSIZE, 4096);
  55. ioctl(nbd, NBD_SET_SIZE_BLOCKS, devsize/4096);
  56. ioctl(nbd, NBD_CLEAR_SOCK);
  57. // If the sucker was exported read only, respect that locally.
  58. temp = (flags & 2) ? 1 : 0;
  59. xioctl(nbd, BLKROSET, &temp);
  60. if (timeout && ioctl(nbd, NBD_SET_TIMEOUT, timeout)<0) break;
  61. if (ioctl(nbd, NBD_SET_SOCK, sock) < 0) break;
  62. if (toys.optflags & FLAG_s) mlockall(MCL_CURRENT|MCL_FUTURE);
  63. // Open the device to force reread of the partition table.
  64. if ((toys.optflags & FLAG_n) || !xfork()) {
  65. char *s = strrchr(device, '/');
  66. int i;
  67. sprintf(toybuf, "/sys/block/%.32s/pid", s ? s+1 : device);
  68. // Is it up yet? (Give it 10 seconds.)
  69. for (i=0; i<100; i++) {
  70. temp = open(toybuf, O_RDONLY);
  71. if (temp == -1) msleep(100);
  72. else {
  73. close(temp);
  74. break;
  75. }
  76. }
  77. close(open(device, O_RDONLY));
  78. if (!(toys.optflags & FLAG_n)) exit(0);
  79. }
  80. // Daemonize here.
  81. if (daemon(0,0)) perror_exit("daemonize");
  82. // Process NBD requests until further notice.
  83. if (ioctl(nbd, NBD_DO_IT)>=0 || errno==EBADR) break;
  84. close(sock);
  85. }
  86. // Flush queue and exit.
  87. ioctl(nbd, NBD_CLEAR_QUE);
  88. ioctl(nbd, NBD_CLEAR_SOCK);
  89. if (CFG_TOYBOX_FREE) close(nbd);
  90. }