nbd-client.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. struct bug_check {
  54. char c[offsetof(struct nbd_header_t, data) == 8+8+8+4 ? 1 : -1];
  55. };
  56. // Parse command line stuff (just a stub now)
  57. if (argc != 4)
  58. bb_show_usage();
  59. #if !BB_MMU
  60. bb_daemonize_or_rexec(DAEMON_CLOSE_EXTRA_FDS, argv);
  61. #endif
  62. host = argv[1];
  63. port = argv[2];
  64. device = argv[3];
  65. // Repeat until spanked (-persist behavior)
  66. for (;;) {
  67. int sock, nbd;
  68. int ro;
  69. // Make sure the /dev/nbd exists
  70. nbd = xopen(device, O_RDWR);
  71. // Find and connect to server
  72. sock = create_and_connect_stream_or_die(host, xatou16(port));
  73. setsockopt_1(sock, IPPROTO_TCP, TCP_NODELAY);
  74. // Log on to the server
  75. xread(sock, &nbd_header, 8+8+8+4 + 124);
  76. if (memcmp(&nbd_header.magic1, "NBDMAGIC""\x00\x00\x42\x02\x81\x86\x12\x53", 16) != 0)
  77. bb_error_msg_and_die("login failed");
  78. // Set 4k block size. Everything uses that these days
  79. ioctl(nbd, NBD_SET_BLKSIZE, 4096);
  80. ioctl(nbd, NBD_SET_SIZE_BLOCKS, SWAP_BE64(nbd_header.devsize) / 4096);
  81. ioctl(nbd, NBD_CLEAR_SOCK);
  82. // If the sucker was exported read only, respect that locally
  83. ro = (nbd_header.flags & SWAP_BE32(2)) / SWAP_BE32(2);
  84. if (ioctl(nbd, BLKROSET, &ro) < 0)
  85. bb_perror_msg_and_die("BLKROSET");
  86. if (timeout)
  87. if (ioctl(nbd, NBD_SET_TIMEOUT, timeout))
  88. bb_perror_msg_and_die("NBD_SET_TIMEOUT");
  89. if (ioctl(nbd, NBD_SET_SOCK, sock))
  90. bb_perror_msg_and_die("NBD_SET_SOCK");
  91. // if (swap) mlockall(MCL_CURRENT|MCL_FUTURE);
  92. #if BB_MMU
  93. // Open the device to force reread of the partition table.
  94. // Need to do it in a separate process, since open(device)
  95. // needs some other process to sit in ioctl(nbd, NBD_DO_IT).
  96. if (fork() == 0) {
  97. char *s = strrchr(device, '/');
  98. sprintf(nbd_header.data, "/sys/block/%.32s/pid", s ? s + 1 : device);
  99. // Is it up yet?
  100. for (;;) {
  101. int fd = open(nbd_header.data, O_RDONLY);
  102. if (fd >= 0) {
  103. //close(fd);
  104. break;
  105. }
  106. sleep(1);
  107. }
  108. open(device, O_RDONLY);
  109. return 0;
  110. }
  111. // Daemonize here
  112. if (!nofork) {
  113. daemon(0, 0);
  114. nofork = 1;
  115. }
  116. #endif
  117. // This turns us (the process that calls this ioctl)
  118. // into a dedicated NBD request handler.
  119. // We block here for a long time.
  120. // When exactly ioctl returns? On a signal,
  121. // or if someone does ioctl(NBD_DISCONNECT) [nbd-client -d].
  122. if (ioctl(nbd, NBD_DO_IT) >= 0 || errno == EBADR) {
  123. // Flush queue and exit
  124. ioctl(nbd, NBD_CLEAR_QUEUE);
  125. ioctl(nbd, NBD_CLEAR_SOCK);
  126. break;
  127. }
  128. close(sock);
  129. close(nbd);
  130. }
  131. return 0;
  132. }