qemu-nbd.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <qemu-common.h>
  19. #include "block_int.h"
  20. #include "nbd.h"
  21. #include <stdarg.h>
  22. #include <stdio.h>
  23. #include <getopt.h>
  24. #include <err.h>
  25. #include <sys/types.h>
  26. #include <sys/socket.h>
  27. #include <netinet/in.h>
  28. #include <netinet/tcp.h>
  29. #include <arpa/inet.h>
  30. #include <signal.h>
  31. #include <libgen.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 `%d')\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, NBD_DEFAULT_PORT, "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. int ret;
  106. if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
  107. errno = -ret;
  108. err(EXIT_FAILURE, "error while reading");
  109. }
  110. if (data[510] != 0x55 || data[511] != 0xaa) {
  111. errno = -EINVAL;
  112. return -1;
  113. }
  114. for (i = 0; i < 4; i++) {
  115. read_partition(&data[446 + 16 * i], &mbr[i]);
  116. if (!mbr[i].nb_sectors_abs)
  117. continue;
  118. if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
  119. struct partition_record ext[4];
  120. uint8_t data1[512];
  121. int j;
  122. if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
  123. errno = -ret;
  124. err(EXIT_FAILURE, "error while reading");
  125. }
  126. for (j = 0; j < 4; j++) {
  127. read_partition(&data1[446 + 16 * j], &ext[j]);
  128. if (!ext[j].nb_sectors_abs)
  129. continue;
  130. if ((ext_partnum + j + 1) == partition) {
  131. *offset = (uint64_t)ext[j].start_sector_abs << 9;
  132. *size = (uint64_t)ext[j].nb_sectors_abs << 9;
  133. return 0;
  134. }
  135. }
  136. ext_partnum += 4;
  137. } else if ((i + 1) == partition) {
  138. *offset = (uint64_t)mbr[i].start_sector_abs << 9;
  139. *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
  140. return 0;
  141. }
  142. }
  143. errno = -ENOENT;
  144. return -1;
  145. }
  146. static void show_parts(const char *device)
  147. {
  148. if (fork() == 0) {
  149. int nbd;
  150. /* linux just needs an open() to trigger
  151. * the partition table update
  152. * but remember to load the module with max_part != 0 :
  153. * modprobe nbd max_part=63
  154. */
  155. nbd = open(device, O_RDWR);
  156. if (nbd != -1)
  157. close(nbd);
  158. exit(0);
  159. }
  160. }
  161. int main(int argc, char **argv)
  162. {
  163. BlockDriverState *bs;
  164. off_t dev_offset = 0;
  165. off_t offset = 0;
  166. bool readonly = false;
  167. bool disconnect = false;
  168. const char *bindto = "0.0.0.0";
  169. int port = NBD_DEFAULT_PORT;
  170. struct sockaddr_in addr;
  171. socklen_t addr_len = sizeof(addr);
  172. off_t fd_size;
  173. char *device = NULL;
  174. char *socket = NULL;
  175. char sockpath[128];
  176. const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
  177. struct option lopt[] = {
  178. { "help", 0, NULL, 'h' },
  179. { "version", 0, NULL, 'V' },
  180. { "bind", 1, NULL, 'b' },
  181. { "port", 1, NULL, 'p' },
  182. { "socket", 1, NULL, 'k' },
  183. { "offset", 1, NULL, 'o' },
  184. { "read-only", 0, NULL, 'r' },
  185. { "partition", 1, NULL, 'P' },
  186. { "connect", 1, NULL, 'c' },
  187. { "disconnect", 0, NULL, 'd' },
  188. { "snapshot", 0, NULL, 's' },
  189. { "nocache", 0, NULL, 'n' },
  190. { "shared", 1, NULL, 'e' },
  191. { "persistent", 0, NULL, 't' },
  192. { "verbose", 0, NULL, 'v' },
  193. { NULL, 0, NULL, 0 }
  194. };
  195. int ch;
  196. int opt_ind = 0;
  197. int li;
  198. char *end;
  199. int flags = BDRV_O_RDWR;
  200. int partition = -1;
  201. int ret;
  202. int shared = 1;
  203. uint8_t *data;
  204. fd_set fds;
  205. int *sharing_fds;
  206. int fd;
  207. int i;
  208. int nb_fds = 0;
  209. int max_fd;
  210. int persistent = 0;
  211. uint32_t nbdflags;
  212. while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
  213. switch (ch) {
  214. case 's':
  215. flags |= BDRV_O_SNAPSHOT;
  216. break;
  217. case 'n':
  218. flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
  219. break;
  220. case 'b':
  221. bindto = optarg;
  222. break;
  223. case 'p':
  224. li = strtol(optarg, &end, 0);
  225. if (*end) {
  226. errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
  227. }
  228. if (li < 1 || li > 65535) {
  229. errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
  230. }
  231. port = (uint16_t)li;
  232. break;
  233. case 'o':
  234. dev_offset = strtoll (optarg, &end, 0);
  235. if (*end) {
  236. errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
  237. }
  238. if (dev_offset < 0) {
  239. errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
  240. }
  241. break;
  242. case 'r':
  243. readonly = true;
  244. flags &= ~BDRV_O_RDWR;
  245. break;
  246. case 'P':
  247. partition = strtol(optarg, &end, 0);
  248. if (*end)
  249. errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
  250. if (partition < 1 || partition > 8)
  251. errx(EXIT_FAILURE, "Invalid partition %d", partition);
  252. break;
  253. case 'k':
  254. socket = optarg;
  255. if (socket[0] != '/')
  256. errx(EXIT_FAILURE, "socket path must be absolute\n");
  257. break;
  258. case 'd':
  259. disconnect = true;
  260. break;
  261. case 'c':
  262. device = optarg;
  263. break;
  264. case 'e':
  265. shared = strtol(optarg, &end, 0);
  266. if (*end) {
  267. errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
  268. }
  269. if (shared < 1) {
  270. errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
  271. }
  272. break;
  273. case 't':
  274. persistent = 1;
  275. break;
  276. case 'v':
  277. verbose = 1;
  278. break;
  279. case 'V':
  280. version(argv[0]);
  281. exit(0);
  282. break;
  283. case 'h':
  284. usage(argv[0]);
  285. exit(0);
  286. break;
  287. case '?':
  288. errx(EXIT_FAILURE, "Try `%s --help' for more information.",
  289. argv[0]);
  290. }
  291. }
  292. if ((argc - optind) != 1) {
  293. errx(EXIT_FAILURE, "Invalid number of argument.\n"
  294. "Try `%s --help' for more information.",
  295. argv[0]);
  296. }
  297. if (disconnect) {
  298. fd = open(argv[optind], O_RDWR);
  299. if (fd == -1)
  300. err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
  301. nbd_disconnect(fd);
  302. close(fd);
  303. printf("%s disconnected\n", argv[optind]);
  304. return 0;
  305. }
  306. bdrv_init();
  307. bs = bdrv_new("hda");
  308. if ((ret = bdrv_open(bs, argv[optind], flags, NULL)) < 0) {
  309. errno = -ret;
  310. err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
  311. }
  312. fd_size = bs->total_sectors * 512;
  313. if (partition != -1 &&
  314. find_partition(bs, partition, &dev_offset, &fd_size))
  315. err(EXIT_FAILURE, "Could not find partition %d", partition);
  316. if (device) {
  317. pid_t pid;
  318. int sock;
  319. /* want to fail before daemonizing */
  320. if (access(device, R_OK|W_OK) == -1) {
  321. err(EXIT_FAILURE, "Could not access '%s'", device);
  322. }
  323. if (!verbose) {
  324. /* detach client and server */
  325. if (qemu_daemon(0, 0) == -1) {
  326. err(EXIT_FAILURE, "Failed to daemonize");
  327. }
  328. }
  329. if (socket == NULL) {
  330. snprintf(sockpath, sizeof(sockpath), SOCKET_PATH,
  331. basename(device));
  332. socket = sockpath;
  333. }
  334. pid = fork();
  335. if (pid < 0)
  336. return 1;
  337. if (pid != 0) {
  338. off_t size;
  339. size_t blocksize;
  340. ret = 0;
  341. bdrv_close(bs);
  342. do {
  343. sock = unix_socket_outgoing(socket);
  344. if (sock == -1) {
  345. if (errno != ENOENT && errno != ECONNREFUSED) {
  346. ret = 1;
  347. goto out;
  348. }
  349. sleep(1); /* wait children */
  350. }
  351. } while (sock == -1);
  352. fd = open(device, O_RDWR);
  353. if (fd == -1) {
  354. ret = 1;
  355. goto out;
  356. }
  357. ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
  358. &size, &blocksize);
  359. if (ret == -1) {
  360. ret = 1;
  361. goto out;
  362. }
  363. ret = nbd_init(fd, sock, size, blocksize);
  364. if (ret == -1) {
  365. ret = 1;
  366. goto out;
  367. }
  368. printf("NBD device %s is now connected to file %s\n",
  369. device, argv[optind]);
  370. /* update partition table */
  371. show_parts(device);
  372. ret = nbd_client(fd);
  373. if (ret) {
  374. ret = 1;
  375. }
  376. close(fd);
  377. out:
  378. kill(pid, SIGTERM);
  379. unlink(socket);
  380. return ret;
  381. }
  382. /* children */
  383. }
  384. sharing_fds = qemu_malloc((shared + 1) * sizeof(int));
  385. if (socket) {
  386. sharing_fds[0] = unix_socket_incoming(socket);
  387. } else {
  388. sharing_fds[0] = tcp_socket_incoming(bindto, port);
  389. }
  390. if (sharing_fds[0] == -1)
  391. return 1;
  392. max_fd = sharing_fds[0];
  393. nb_fds++;
  394. data = qemu_blockalign(bs, NBD_BUFFER_SIZE);
  395. if (data == NULL)
  396. errx(EXIT_FAILURE, "Cannot allocate data buffer");
  397. do {
  398. FD_ZERO(&fds);
  399. for (i = 0; i < nb_fds; i++)
  400. FD_SET(sharing_fds[i], &fds);
  401. ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
  402. if (ret == -1)
  403. break;
  404. if (FD_ISSET(sharing_fds[0], &fds))
  405. ret--;
  406. for (i = 1; i < nb_fds && ret; i++) {
  407. if (FD_ISSET(sharing_fds[i], &fds)) {
  408. if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
  409. &offset, readonly, data, NBD_BUFFER_SIZE) != 0) {
  410. close(sharing_fds[i]);
  411. nb_fds--;
  412. sharing_fds[i] = sharing_fds[nb_fds];
  413. i--;
  414. }
  415. ret--;
  416. }
  417. }
  418. /* new connection ? */
  419. if (FD_ISSET(sharing_fds[0], &fds)) {
  420. if (nb_fds < shared + 1) {
  421. sharing_fds[nb_fds] = accept(sharing_fds[0],
  422. (struct sockaddr *)&addr,
  423. &addr_len);
  424. if (sharing_fds[nb_fds] != -1 &&
  425. nbd_negotiate(sharing_fds[nb_fds], fd_size) != -1) {
  426. if (sharing_fds[nb_fds] > max_fd)
  427. max_fd = sharing_fds[nb_fds];
  428. nb_fds++;
  429. }
  430. }
  431. }
  432. } while (persistent || nb_fds > 1);
  433. qemu_vfree(data);
  434. close(sharing_fds[0]);
  435. bdrv_close(bs);
  436. qemu_free(sharing_fds);
  437. if (socket)
  438. unlink(socket);
  439. return 0;
  440. }