3
0

nbd-client.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright 2010 Rob Landley <rob@landley.net>
  3. *
  4. * Licensed under GPLv2, see file LICENSE in this source tree.
  5. */
  6. #include "libbb.h"
  7. #include <netinet/tcp.h>
  8. #include <linux/fs.h>
  9. //applet:IF_NBDCLIENT(APPLET_ODDNAME(nbd-client, nbdclient, BB_DIR_USR_SBIN, BB_SUID_DROP, nbdclient))
  10. //kbuild:lib-$(CONFIG_NBDCLIENT) += nbd-client.o
  11. //config:config NBDCLIENT
  12. //config: bool "nbd-client"
  13. //config: default y
  14. //config: help
  15. //config: Network block device client
  16. #define NBD_SET_SOCK _IO(0xab, 0)
  17. #define NBD_SET_BLKSIZE _IO(0xab, 1)
  18. #define NBD_SET_SIZE _IO(0xab, 2)
  19. #define NBD_DO_IT _IO(0xab, 3)
  20. #define NBD_CLEAR_SOCK _IO(0xab, 4)
  21. #define NBD_CLEAR_QUEUE _IO(0xab, 5)
  22. #define NBD_PRINT_DEBUG _IO(0xab, 6)
  23. #define NBD_SET_SIZE_BLOCKS _IO(0xab, 7)
  24. #define NBD_DISCONNECT _IO(0xab, 8)
  25. #define NBD_SET_TIMEOUT _IO(0xab, 9)
  26. //usage:#define nbdclient_trivial_usage
  27. //usage: "HOST PORT BLOCKDEV"
  28. //usage:#define nbdclient_full_usage "\n\n"
  29. //usage: "Connect to HOST and provide a network block device on BLOCKDEV"
  30. //TODO: more compat with nbd-client version 2.9.13 -
  31. //Usage: nbd-client [bs=blocksize] [timeout=sec] host port nbd_device [-swap] [-persist] [-nofork]
  32. //Or : nbd-client -d nbd_device
  33. //Or : nbd-client -c nbd_device
  34. //Default value for blocksize is 1024 (recommended for ethernet)
  35. //Allowed values for blocksize are 512,1024,2048,4096
  36. //Note, that kernel 2.4.2 and older ones do not work correctly with
  37. //blocksizes other than 1024 without patches
  38. int nbdclient_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int nbdclient_main(int argc, char **argv)
  40. {
  41. unsigned long timeout = 0;
  42. #if BB_MMU
  43. int nofork = 0;
  44. #endif
  45. char *host, *port, *device;
  46. struct nbd_header_t {
  47. uint64_t magic1; // "NBDMAGIC"
  48. uint64_t magic2; // 0x420281861253 big endian
  49. uint64_t devsize;
  50. uint32_t flags;
  51. char data[124];
  52. } nbd_header;
  53. BUILD_BUG_ON(offsetof(struct nbd_header_t, data) != 8+8+8+4);
  54. // Parse command line stuff (just a stub now)
  55. if (argc != 4)
  56. bb_show_usage();
  57. #if !BB_MMU
  58. bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
  59. #endif
  60. host = argv[1];
  61. port = argv[2];
  62. device = argv[3];
  63. // Repeat until spanked (-persist behavior)
  64. for (;;) {
  65. int sock, nbd;
  66. int ro;
  67. // Make sure the /dev/nbd exists
  68. nbd = xopen(device, O_RDWR);
  69. // Find and connect to server
  70. sock = create_and_connect_stream_or_die(host, xatou16(port));
  71. setsockopt_1(sock, IPPROTO_TCP, TCP_NODELAY);
  72. // Log on to the server
  73. xread(sock, &nbd_header, 8+8+8+4 + 124);
  74. if (memcmp(&nbd_header.magic1, "NBDMAGIC""\x00\x00\x42\x02\x81\x86\x12\x53", 16) != 0)
  75. bb_error_msg_and_die("login failed");
  76. // Set 4k block size. Everything uses that these days
  77. ioctl(nbd, NBD_SET_BLKSIZE, 4096);
  78. ioctl(nbd, NBD_SET_SIZE_BLOCKS, SWAP_BE64(nbd_header.devsize) / 4096);
  79. ioctl(nbd, NBD_CLEAR_SOCK);
  80. // If the sucker was exported read only, respect that locally
  81. ro = (nbd_header.flags & SWAP_BE32(2)) / SWAP_BE32(2);
  82. if (ioctl(nbd, BLKROSET, &ro) < 0)
  83. bb_perror_msg_and_die("BLKROSET");
  84. if (timeout)
  85. if (ioctl(nbd, NBD_SET_TIMEOUT, timeout))
  86. bb_perror_msg_and_die("NBD_SET_TIMEOUT");
  87. if (ioctl(nbd, NBD_SET_SOCK, sock))
  88. bb_perror_msg_and_die("NBD_SET_SOCK");
  89. // if (swap) mlockall(MCL_CURRENT|MCL_FUTURE);
  90. #if BB_MMU
  91. // Open the device to force reread of the partition table.
  92. // Need to do it in a separate process, since open(device)
  93. // needs some other process to sit in ioctl(nbd, NBD_DO_IT).
  94. if (fork() == 0) {
  95. char *s = strrchr(device, '/');
  96. sprintf(nbd_header.data, "/sys/block/%.32s/pid", s ? s + 1 : device);
  97. // Is it up yet?
  98. for (;;) {
  99. int fd = open(nbd_header.data, O_RDONLY);
  100. if (fd >= 0) {
  101. //close(fd);
  102. break;
  103. }
  104. sleep(1);
  105. }
  106. open(device, O_RDONLY);
  107. return 0;
  108. }
  109. // Daemonize here
  110. if (!nofork) {
  111. daemon(0, 0);
  112. nofork = 1;
  113. }
  114. #endif
  115. // This turns us (the process that calls this ioctl)
  116. // into a dedicated NBD request handler.
  117. // We block here for a long time.
  118. // When exactly ioctl returns? On a signal,
  119. // or if someone does ioctl(NBD_DISCONNECT) [nbd-client -d].
  120. if (ioctl(nbd, NBD_DO_IT) >= 0 || errno == EBADR) {
  121. // Flush queue and exit
  122. ioctl(nbd, NBD_CLEAR_QUEUE);
  123. ioctl(nbd, NBD_CLEAR_SOCK);
  124. break;
  125. }
  126. close(sock);
  127. close(nbd);
  128. }
  129. return 0;
  130. }