qemu-nbd.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /*
  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
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301 USA
  18. */
  19. #include <qemu-common.h>
  20. #include "block_int.h"
  21. #include "nbd.h"
  22. #include <stdarg.h>
  23. #include <stdio.h>
  24. #include <getopt.h>
  25. #include <err.h>
  26. #include <sys/types.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29. #include <netinet/tcp.h>
  30. #include <arpa/inet.h>
  31. #include <signal.h>
  32. #define SOCKET_PATH "/var/lock/qemu-nbd-%s"
  33. #define NBD_BUFFER_SIZE (1024*1024)
  34. static int verbose;
  35. static void usage(const char *name)
  36. {
  37. printf(
  38. "Usage: %s [OPTIONS] FILE\n"
  39. "QEMU Disk Network Block Device Server\n"
  40. "\n"
  41. " -p, --port=PORT port to listen on (default `1024')\n"
  42. " -o, --offset=OFFSET offset into the image\n"
  43. " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
  44. " -k, --socket=PATH path to the unix socket\n"
  45. " (default '"SOCKET_PATH"')\n"
  46. " -r, --read-only export read-only\n"
  47. " -P, --partition=NUM only expose partition NUM\n"
  48. " -s, --snapshot use snapshot file\n"
  49. " -n, --nocache disable host cache\n"
  50. " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
  51. " -d, --disconnect disconnect the specified device\n"
  52. " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
  53. " -t, --persistent don't exit on the last connection\n"
  54. " -v, --verbose display extra debugging information\n"
  55. " -h, --help display this help and exit\n"
  56. " -V, --version output version information and exit\n"
  57. "\n"
  58. "Report bugs to <anthony@codemonkey.ws>\n"
  59. , name, "DEVICE");
  60. }
  61. static void version(const char *name)
  62. {
  63. printf(
  64. "%s version 0.0.1\n"
  65. "Written by Anthony Liguori.\n"
  66. "\n"
  67. "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
  68. "This is free software; see the source for copying conditions. There is NO\n"
  69. "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
  70. , name);
  71. }
  72. struct partition_record
  73. {
  74. uint8_t bootable;
  75. uint8_t start_head;
  76. uint32_t start_cylinder;
  77. uint8_t start_sector;
  78. uint8_t system;
  79. uint8_t end_head;
  80. uint8_t end_cylinder;
  81. uint8_t end_sector;
  82. uint32_t start_sector_abs;
  83. uint32_t nb_sectors_abs;
  84. };
  85. static void read_partition(uint8_t *p, struct partition_record *r)
  86. {
  87. r->bootable = p[0];
  88. r->start_head = p[1];
  89. r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
  90. r->start_sector = p[2] & 0x3f;
  91. r->system = p[4];
  92. r->end_head = p[5];
  93. r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
  94. r->end_sector = p[6] & 0x3f;
  95. r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
  96. r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
  97. }
  98. static int find_partition(BlockDriverState *bs, int partition,
  99. off_t *offset, off_t *size)
  100. {
  101. struct partition_record mbr[4];
  102. uint8_t data[512];
  103. int i;
  104. int ext_partnum = 4;
  105. if (bdrv_read(bs, 0, data, 1))
  106. errx(EINVAL, "error while reading");
  107. if (data[510] != 0x55 || data[511] != 0xaa) {
  108. errno = -EINVAL;
  109. return -1;
  110. }
  111. for (i = 0; i < 4; i++) {
  112. read_partition(&data[446 + 16 * i], &mbr[i]);
  113. if (!mbr[i].nb_sectors_abs)
  114. continue;
  115. if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
  116. struct partition_record ext[4];
  117. uint8_t data1[512];
  118. int j;
  119. if (bdrv_read(bs, mbr[i].start_sector_abs, data1, 1))
  120. errx(EINVAL, "error while reading");
  121. for (j = 0; j < 4; j++) {
  122. read_partition(&data1[446 + 16 * j], &ext[j]);
  123. if (!ext[j].nb_sectors_abs)
  124. continue;
  125. if ((ext_partnum + j + 1) == partition) {
  126. *offset = (uint64_t)ext[j].start_sector_abs << 9;
  127. *size = (uint64_t)ext[j].nb_sectors_abs << 9;
  128. return 0;
  129. }
  130. }
  131. ext_partnum += 4;
  132. } else if ((i + 1) == partition) {
  133. *offset = (uint64_t)mbr[i].start_sector_abs << 9;
  134. *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
  135. return 0;
  136. }
  137. }
  138. errno = -ENOENT;
  139. return -1;
  140. }
  141. static void show_parts(const char *device)
  142. {
  143. if (fork() == 0) {
  144. int nbd;
  145. /* linux just needs an open() to trigger
  146. * the partition table update
  147. * but remember to load the module with max_part != 0 :
  148. * modprobe nbd max_part=63
  149. */
  150. nbd = open(device, O_RDWR);
  151. if (nbd != -1)
  152. close(nbd);
  153. exit(0);
  154. }
  155. }
  156. int main(int argc, char **argv)
  157. {
  158. BlockDriverState *bs;
  159. off_t dev_offset = 0;
  160. off_t offset = 0;
  161. bool readonly = false;
  162. bool disconnect = false;
  163. const char *bindto = "0.0.0.0";
  164. int port = 1024;
  165. struct sockaddr_in addr;
  166. socklen_t addr_len = sizeof(addr);
  167. off_t fd_size;
  168. char *device = NULL;
  169. char *socket = NULL;
  170. char sockpath[128];
  171. const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
  172. struct option lopt[] = {
  173. { "help", 0, 0, 'h' },
  174. { "version", 0, 0, 'V' },
  175. { "bind", 1, 0, 'b' },
  176. { "port", 1, 0, 'p' },
  177. { "socket", 1, 0, 'k' },
  178. { "offset", 1, 0, 'o' },
  179. { "read-only", 0, 0, 'r' },
  180. { "partition", 1, 0, 'P' },
  181. { "connect", 1, 0, 'c' },
  182. { "disconnect", 0, 0, 'd' },
  183. { "snapshot", 0, 0, 's' },
  184. { "nocache", 0, 0, 'n' },
  185. { "shared", 1, 0, 'e' },
  186. { "persistent", 0, 0, 't' },
  187. { "verbose", 0, 0, 'v' },
  188. { NULL, 0, 0, 0 }
  189. };
  190. int ch;
  191. int opt_ind = 0;
  192. int li;
  193. char *end;
  194. int flags = 0;
  195. int partition = -1;
  196. int ret;
  197. int shared = 1;
  198. uint8_t *data;
  199. fd_set fds;
  200. int *sharing_fds;
  201. int fd;
  202. int i;
  203. int nb_fds = 0;
  204. int max_fd;
  205. int persistent = 0;
  206. while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
  207. switch (ch) {
  208. case 's':
  209. flags |= BDRV_O_SNAPSHOT;
  210. break;
  211. case 'n':
  212. flags |= BDRV_O_NOCACHE;
  213. break;
  214. case 'b':
  215. bindto = optarg;
  216. break;
  217. case 'p':
  218. li = strtol(optarg, &end, 0);
  219. if (*end) {
  220. errx(EINVAL, "Invalid port `%s'", optarg);
  221. }
  222. if (li < 1 || li > 65535) {
  223. errx(EINVAL, "Port out of range `%s'", optarg);
  224. }
  225. port = (uint16_t)li;
  226. break;
  227. case 'o':
  228. dev_offset = strtoll (optarg, &end, 0);
  229. if (*end) {
  230. errx(EINVAL, "Invalid offset `%s'", optarg);
  231. }
  232. if (dev_offset < 0) {
  233. errx(EINVAL, "Offset must be positive `%s'", optarg);
  234. }
  235. break;
  236. case 'r':
  237. readonly = true;
  238. break;
  239. case 'P':
  240. partition = strtol(optarg, &end, 0);
  241. if (*end)
  242. errx(EINVAL, "Invalid partition `%s'", optarg);
  243. if (partition < 1 || partition > 8)
  244. errx(EINVAL, "Invalid partition %d", partition);
  245. break;
  246. case 'k':
  247. socket = optarg;
  248. if (socket[0] != '/')
  249. errx(EINVAL, "socket path must be absolute\n");
  250. break;
  251. case 'd':
  252. disconnect = true;
  253. break;
  254. case 'c':
  255. device = optarg;
  256. break;
  257. case 'e':
  258. shared = strtol(optarg, &end, 0);
  259. if (*end) {
  260. errx(EINVAL, "Invalid shared device number '%s'", optarg);
  261. }
  262. if (shared < 1) {
  263. errx(EINVAL, "Shared device number must be greater than 0\n");
  264. }
  265. break;
  266. case 't':
  267. persistent = 1;
  268. break;
  269. case 'v':
  270. verbose = 1;
  271. break;
  272. case 'V':
  273. version(argv[0]);
  274. exit(0);
  275. break;
  276. case 'h':
  277. usage(argv[0]);
  278. exit(0);
  279. break;
  280. case '?':
  281. errx(EINVAL, "Try `%s --help' for more information.",
  282. argv[0]);
  283. }
  284. }
  285. if ((argc - optind) != 1) {
  286. errx(EINVAL, "Invalid number of argument.\n"
  287. "Try `%s --help' for more information.",
  288. argv[0]);
  289. }
  290. if (disconnect) {
  291. fd = open(argv[optind], O_RDWR);
  292. if (fd == -1)
  293. errx(errno, "Cannot open %s", argv[optind]);
  294. nbd_disconnect(fd);
  295. close(fd);
  296. printf("%s disconnected\n", argv[optind]);
  297. return 0;
  298. }
  299. bdrv_init();
  300. bs = bdrv_new("hda");
  301. if (bs == NULL)
  302. return 1;
  303. if (bdrv_open(bs, argv[optind], flags) == -1)
  304. return 1;
  305. fd_size = bs->total_sectors * 512;
  306. if (partition != -1 &&
  307. find_partition(bs, partition, &dev_offset, &fd_size))
  308. errx(errno, "Could not find partition %d", partition);
  309. if (device) {
  310. pid_t pid;
  311. int sock;
  312. if (!verbose)
  313. daemon(0, 0); /* detach client and server */
  314. if (socket == NULL) {
  315. sprintf(sockpath, SOCKET_PATH, basename(device));
  316. socket = sockpath;
  317. }
  318. pid = fork();
  319. if (pid < 0)
  320. return 1;
  321. if (pid != 0) {
  322. off_t size;
  323. size_t blocksize;
  324. ret = 0;
  325. bdrv_close(bs);
  326. do {
  327. sock = unix_socket_outgoing(socket);
  328. if (sock == -1) {
  329. if (errno != ENOENT && errno != ECONNREFUSED)
  330. goto out;
  331. sleep(1); /* wait children */
  332. }
  333. } while (sock == -1);
  334. fd = open(device, O_RDWR);
  335. if (fd == -1) {
  336. ret = 1;
  337. goto out;
  338. }
  339. ret = nbd_receive_negotiate(sock, &size, &blocksize);
  340. if (ret == -1) {
  341. ret = 1;
  342. goto out;
  343. }
  344. ret = nbd_init(fd, sock, size, blocksize);
  345. if (ret == -1) {
  346. ret = 1;
  347. goto out;
  348. }
  349. printf("NBD device %s is now connected to file %s\n",
  350. device, argv[optind]);
  351. /* update partition table */
  352. show_parts(device);
  353. nbd_client(fd, sock);
  354. close(fd);
  355. out:
  356. kill(pid, SIGTERM);
  357. unlink(socket);
  358. return ret;
  359. }
  360. /* children */
  361. }
  362. sharing_fds = qemu_malloc((shared + 1) * sizeof(int));
  363. if (socket) {
  364. sharing_fds[0] = unix_socket_incoming(socket);
  365. } else {
  366. sharing_fds[0] = tcp_socket_incoming(bindto, port);
  367. }
  368. if (sharing_fds[0] == -1)
  369. return 1;
  370. max_fd = sharing_fds[0];
  371. nb_fds++;
  372. data = qemu_memalign(512, NBD_BUFFER_SIZE);
  373. if (data == NULL)
  374. errx(ENOMEM, "Cannot allocate data buffer");
  375. do {
  376. FD_ZERO(&fds);
  377. for (i = 0; i < nb_fds; i++)
  378. FD_SET(sharing_fds[i], &fds);
  379. ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
  380. if (ret == -1)
  381. break;
  382. if (FD_ISSET(sharing_fds[0], &fds))
  383. ret--;
  384. for (i = 1; i < nb_fds && ret; i++) {
  385. if (FD_ISSET(sharing_fds[i], &fds)) {
  386. if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
  387. &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
  388. close(sharing_fds[i]);
  389. nb_fds--;
  390. sharing_fds[i] = sharing_fds[nb_fds];
  391. i--;
  392. }
  393. ret--;
  394. }
  395. }
  396. /* new connection ? */
  397. if (FD_ISSET(sharing_fds[0], &fds)) {
  398. if (nb_fds < shared + 1) {
  399. sharing_fds[nb_fds] = accept(sharing_fds[0],
  400. (struct sockaddr *)&addr,
  401. &addr_len);
  402. if (sharing_fds[nb_fds] != -1 &&
  403. nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
  404. if (sharing_fds[nb_fds] > max_fd)
  405. max_fd = sharing_fds[nb_fds];
  406. nb_fds++;
  407. }
  408. }
  409. }
  410. } while (persistent || nb_fds > 1);
  411. qemu_free(data);
  412. close(sharing_fds[0]);
  413. bdrv_close(bs);
  414. qemu_free(sharing_fds);
  415. if (socket)
  416. unlink(socket);
  417. return 0;
  418. }