2
0

qemu-nbd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  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. #include <pthread.h>
  33. #define SOCKET_PATH "/var/lock/qemu-nbd-%s"
  34. #define NBD_BUFFER_SIZE (1024*1024)
  35. static int sigterm_wfd;
  36. static int verbose;
  37. static char *device;
  38. static char *srcpath;
  39. static char *sockpath;
  40. static void usage(const char *name)
  41. {
  42. printf(
  43. "Usage: %s [OPTIONS] FILE\n"
  44. "QEMU Disk Network Block Device Server\n"
  45. "\n"
  46. " -p, --port=PORT port to listen on (default `%d')\n"
  47. " -o, --offset=OFFSET offset into the image\n"
  48. " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
  49. " -k, --socket=PATH path to the unix socket\n"
  50. " (default '"SOCKET_PATH"')\n"
  51. " -r, --read-only export read-only\n"
  52. " -P, --partition=NUM only expose partition NUM\n"
  53. " -s, --snapshot use snapshot file\n"
  54. " -n, --nocache disable host cache\n"
  55. " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
  56. " -d, --disconnect disconnect the specified device\n"
  57. " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
  58. " -t, --persistent don't exit on the last connection\n"
  59. " -v, --verbose display extra debugging information\n"
  60. " -h, --help display this help and exit\n"
  61. " -V, --version output version information and exit\n"
  62. "\n"
  63. "Report bugs to <anthony@codemonkey.ws>\n"
  64. , name, NBD_DEFAULT_PORT, "DEVICE");
  65. }
  66. static void version(const char *name)
  67. {
  68. printf(
  69. "%s version 0.0.1\n"
  70. "Written by Anthony Liguori.\n"
  71. "\n"
  72. "Copyright (C) 2006 Anthony Liguori <anthony@codemonkey.ws>.\n"
  73. "This is free software; see the source for copying conditions. There is NO\n"
  74. "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
  75. , name);
  76. }
  77. struct partition_record
  78. {
  79. uint8_t bootable;
  80. uint8_t start_head;
  81. uint32_t start_cylinder;
  82. uint8_t start_sector;
  83. uint8_t system;
  84. uint8_t end_head;
  85. uint8_t end_cylinder;
  86. uint8_t end_sector;
  87. uint32_t start_sector_abs;
  88. uint32_t nb_sectors_abs;
  89. };
  90. static void read_partition(uint8_t *p, struct partition_record *r)
  91. {
  92. r->bootable = p[0];
  93. r->start_head = p[1];
  94. r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
  95. r->start_sector = p[2] & 0x3f;
  96. r->system = p[4];
  97. r->end_head = p[5];
  98. r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
  99. r->end_sector = p[6] & 0x3f;
  100. r->start_sector_abs = p[8] | p[9] << 8 | p[10] << 16 | p[11] << 24;
  101. r->nb_sectors_abs = p[12] | p[13] << 8 | p[14] << 16 | p[15] << 24;
  102. }
  103. static int find_partition(BlockDriverState *bs, int partition,
  104. off_t *offset, off_t *size)
  105. {
  106. struct partition_record mbr[4];
  107. uint8_t data[512];
  108. int i;
  109. int ext_partnum = 4;
  110. int ret;
  111. if ((ret = bdrv_read(bs, 0, data, 1)) < 0) {
  112. errno = -ret;
  113. err(EXIT_FAILURE, "error while reading");
  114. }
  115. if (data[510] != 0x55 || data[511] != 0xaa) {
  116. errno = -EINVAL;
  117. return -1;
  118. }
  119. for (i = 0; i < 4; i++) {
  120. read_partition(&data[446 + 16 * i], &mbr[i]);
  121. if (!mbr[i].nb_sectors_abs)
  122. continue;
  123. if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
  124. struct partition_record ext[4];
  125. uint8_t data1[512];
  126. int j;
  127. if ((ret = bdrv_read(bs, mbr[i].start_sector_abs, data1, 1)) < 0) {
  128. errno = -ret;
  129. err(EXIT_FAILURE, "error while reading");
  130. }
  131. for (j = 0; j < 4; j++) {
  132. read_partition(&data1[446 + 16 * j], &ext[j]);
  133. if (!ext[j].nb_sectors_abs)
  134. continue;
  135. if ((ext_partnum + j + 1) == partition) {
  136. *offset = (uint64_t)ext[j].start_sector_abs << 9;
  137. *size = (uint64_t)ext[j].nb_sectors_abs << 9;
  138. return 0;
  139. }
  140. }
  141. ext_partnum += 4;
  142. } else if ((i + 1) == partition) {
  143. *offset = (uint64_t)mbr[i].start_sector_abs << 9;
  144. *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
  145. return 0;
  146. }
  147. }
  148. errno = -ENOENT;
  149. return -1;
  150. }
  151. static void termsig_handler(int signum)
  152. {
  153. static int sigterm_reported;
  154. if (!sigterm_reported) {
  155. sigterm_reported = (write(sigterm_wfd, "", 1) == 1);
  156. }
  157. }
  158. static void *show_parts(void *arg)
  159. {
  160. int nbd;
  161. /* linux just needs an open() to trigger
  162. * the partition table update
  163. * but remember to load the module with max_part != 0 :
  164. * modprobe nbd max_part=63
  165. */
  166. nbd = open(device, O_RDWR);
  167. if (nbd != -1) {
  168. close(nbd);
  169. }
  170. return NULL;
  171. }
  172. static void *nbd_client_thread(void *arg)
  173. {
  174. int fd = *(int *)arg;
  175. off_t size;
  176. size_t blocksize;
  177. uint32_t nbdflags;
  178. int sock;
  179. int ret;
  180. pthread_t show_parts_thread;
  181. do {
  182. sock = unix_socket_outgoing(sockpath);
  183. if (sock == -1) {
  184. goto out;
  185. }
  186. } while (sock == -1);
  187. ret = nbd_receive_negotiate(sock, NULL, &nbdflags,
  188. &size, &blocksize);
  189. if (ret == -1) {
  190. goto out;
  191. }
  192. ret = nbd_init(fd, sock, nbdflags, size, blocksize);
  193. if (ret == -1) {
  194. goto out;
  195. }
  196. /* update partition table */
  197. pthread_create(&show_parts_thread, NULL, show_parts, NULL);
  198. if (verbose) {
  199. fprintf(stderr, "NBD device %s is now connected to %s\n",
  200. device, srcpath);
  201. } else {
  202. /* Close stderr so that the qemu-nbd process exits. */
  203. dup2(STDOUT_FILENO, STDERR_FILENO);
  204. }
  205. ret = nbd_client(fd);
  206. if (ret) {
  207. goto out;
  208. }
  209. close(fd);
  210. kill(getpid(), SIGTERM);
  211. return (void *) EXIT_SUCCESS;
  212. out:
  213. kill(getpid(), SIGTERM);
  214. return (void *) EXIT_FAILURE;
  215. }
  216. int main(int argc, char **argv)
  217. {
  218. BlockDriverState *bs;
  219. off_t dev_offset = 0;
  220. off_t offset = 0;
  221. uint32_t nbdflags = 0;
  222. bool disconnect = false;
  223. const char *bindto = "0.0.0.0";
  224. int port = NBD_DEFAULT_PORT;
  225. struct sockaddr_in addr;
  226. socklen_t addr_len = sizeof(addr);
  227. off_t fd_size;
  228. const char *sopt = "hVb:o:p:rsnP:c:dvk:e:t";
  229. struct option lopt[] = {
  230. { "help", 0, NULL, 'h' },
  231. { "version", 0, NULL, 'V' },
  232. { "bind", 1, NULL, 'b' },
  233. { "port", 1, NULL, 'p' },
  234. { "socket", 1, NULL, 'k' },
  235. { "offset", 1, NULL, 'o' },
  236. { "read-only", 0, NULL, 'r' },
  237. { "partition", 1, NULL, 'P' },
  238. { "connect", 1, NULL, 'c' },
  239. { "disconnect", 0, NULL, 'd' },
  240. { "snapshot", 0, NULL, 's' },
  241. { "nocache", 0, NULL, 'n' },
  242. { "shared", 1, NULL, 'e' },
  243. { "persistent", 0, NULL, 't' },
  244. { "verbose", 0, NULL, 'v' },
  245. { NULL, 0, NULL, 0 }
  246. };
  247. int ch;
  248. int opt_ind = 0;
  249. int li;
  250. char *end;
  251. int flags = BDRV_O_RDWR;
  252. int partition = -1;
  253. int ret;
  254. int shared = 1;
  255. uint8_t *data;
  256. fd_set fds;
  257. int *sharing_fds;
  258. int fd;
  259. int i;
  260. int nb_fds = 0;
  261. int max_fd;
  262. int persistent = 0;
  263. pthread_t client_thread;
  264. /* The client thread uses SIGTERM to interrupt the server. A signal
  265. * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
  266. */
  267. struct sigaction sa_sigterm;
  268. int sigterm_fd[2];
  269. if (qemu_pipe(sigterm_fd) == -1) {
  270. err(EXIT_FAILURE, "Error setting up communication pipe");
  271. }
  272. sigterm_wfd = sigterm_fd[1];
  273. memset(&sa_sigterm, 0, sizeof(sa_sigterm));
  274. sa_sigterm.sa_handler = termsig_handler;
  275. sigaction(SIGTERM, &sa_sigterm, NULL);
  276. while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
  277. switch (ch) {
  278. case 's':
  279. flags |= BDRV_O_SNAPSHOT;
  280. break;
  281. case 'n':
  282. flags |= BDRV_O_NOCACHE | BDRV_O_CACHE_WB;
  283. break;
  284. case 'b':
  285. bindto = optarg;
  286. break;
  287. case 'p':
  288. li = strtol(optarg, &end, 0);
  289. if (*end) {
  290. errx(EXIT_FAILURE, "Invalid port `%s'", optarg);
  291. }
  292. if (li < 1 || li > 65535) {
  293. errx(EXIT_FAILURE, "Port out of range `%s'", optarg);
  294. }
  295. port = (uint16_t)li;
  296. break;
  297. case 'o':
  298. dev_offset = strtoll (optarg, &end, 0);
  299. if (*end) {
  300. errx(EXIT_FAILURE, "Invalid offset `%s'", optarg);
  301. }
  302. if (dev_offset < 0) {
  303. errx(EXIT_FAILURE, "Offset must be positive `%s'", optarg);
  304. }
  305. break;
  306. case 'r':
  307. nbdflags |= NBD_FLAG_READ_ONLY;
  308. flags &= ~BDRV_O_RDWR;
  309. break;
  310. case 'P':
  311. partition = strtol(optarg, &end, 0);
  312. if (*end)
  313. errx(EXIT_FAILURE, "Invalid partition `%s'", optarg);
  314. if (partition < 1 || partition > 8)
  315. errx(EXIT_FAILURE, "Invalid partition %d", partition);
  316. break;
  317. case 'k':
  318. sockpath = optarg;
  319. if (sockpath[0] != '/')
  320. errx(EXIT_FAILURE, "socket path must be absolute\n");
  321. break;
  322. case 'd':
  323. disconnect = true;
  324. break;
  325. case 'c':
  326. device = optarg;
  327. break;
  328. case 'e':
  329. shared = strtol(optarg, &end, 0);
  330. if (*end) {
  331. errx(EXIT_FAILURE, "Invalid shared device number '%s'", optarg);
  332. }
  333. if (shared < 1) {
  334. errx(EXIT_FAILURE, "Shared device number must be greater than 0\n");
  335. }
  336. break;
  337. case 't':
  338. persistent = 1;
  339. break;
  340. case 'v':
  341. verbose = 1;
  342. break;
  343. case 'V':
  344. version(argv[0]);
  345. exit(0);
  346. break;
  347. case 'h':
  348. usage(argv[0]);
  349. exit(0);
  350. break;
  351. case '?':
  352. errx(EXIT_FAILURE, "Try `%s --help' for more information.",
  353. argv[0]);
  354. }
  355. }
  356. if ((argc - optind) != 1) {
  357. errx(EXIT_FAILURE, "Invalid number of argument.\n"
  358. "Try `%s --help' for more information.",
  359. argv[0]);
  360. }
  361. if (disconnect) {
  362. fd = open(argv[optind], O_RDWR);
  363. if (fd == -1)
  364. err(EXIT_FAILURE, "Cannot open %s", argv[optind]);
  365. nbd_disconnect(fd);
  366. close(fd);
  367. printf("%s disconnected\n", argv[optind]);
  368. return 0;
  369. }
  370. if (device && !verbose) {
  371. int stderr_fd[2];
  372. pid_t pid;
  373. int ret;
  374. if (qemu_pipe(stderr_fd) == -1) {
  375. err(EXIT_FAILURE, "Error setting up communication pipe");
  376. }
  377. /* Now daemonize, but keep a communication channel open to
  378. * print errors and exit with the proper status code.
  379. */
  380. pid = fork();
  381. if (pid == 0) {
  382. close(stderr_fd[0]);
  383. ret = qemu_daemon(0, 0);
  384. /* Temporarily redirect stderr to the parent's pipe... */
  385. dup2(stderr_fd[1], STDERR_FILENO);
  386. if (ret == -1) {
  387. err(EXIT_FAILURE, "Failed to daemonize");
  388. }
  389. /* ... close the descriptor we inherited and go on. */
  390. close(stderr_fd[1]);
  391. } else {
  392. bool errors = false;
  393. char *buf;
  394. /* In the parent. Print error messages from the child until
  395. * it closes the pipe.
  396. */
  397. close(stderr_fd[1]);
  398. buf = g_malloc(1024);
  399. while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
  400. errors = true;
  401. ret = qemu_write_full(STDERR_FILENO, buf, ret);
  402. if (ret == -1) {
  403. exit(EXIT_FAILURE);
  404. }
  405. }
  406. if (ret == -1) {
  407. err(EXIT_FAILURE, "Cannot read from daemon");
  408. }
  409. /* Usually the daemon should not print any message.
  410. * Exit with zero status in that case.
  411. */
  412. exit(errors);
  413. }
  414. }
  415. if (device) {
  416. /* Open before spawning new threads. In the future, we may
  417. * drop privileges after opening.
  418. */
  419. fd = open(device, O_RDWR);
  420. if (fd == -1) {
  421. err(EXIT_FAILURE, "Failed to open %s", device);
  422. }
  423. if (sockpath == NULL) {
  424. sockpath = g_malloc(128);
  425. snprintf(sockpath, 128, SOCKET_PATH, basename(device));
  426. }
  427. }
  428. bdrv_init();
  429. atexit(bdrv_close_all);
  430. bs = bdrv_new("hda");
  431. srcpath = argv[optind];
  432. if ((ret = bdrv_open(bs, srcpath, flags, NULL)) < 0) {
  433. errno = -ret;
  434. err(EXIT_FAILURE, "Failed to bdrv_open '%s'", argv[optind]);
  435. }
  436. fd_size = bs->total_sectors * 512;
  437. if (partition != -1 &&
  438. find_partition(bs, partition, &dev_offset, &fd_size)) {
  439. err(EXIT_FAILURE, "Could not find partition %d", partition);
  440. }
  441. sharing_fds = g_malloc((shared + 1) * sizeof(int));
  442. if (sockpath) {
  443. sharing_fds[0] = unix_socket_incoming(sockpath);
  444. } else {
  445. sharing_fds[0] = tcp_socket_incoming(bindto, port);
  446. }
  447. if (sharing_fds[0] == -1)
  448. return 1;
  449. if (device) {
  450. int ret;
  451. ret = pthread_create(&client_thread, NULL, nbd_client_thread, &fd);
  452. if (ret != 0) {
  453. errx(EXIT_FAILURE, "Failed to create client thread: %s",
  454. strerror(ret));
  455. }
  456. } else {
  457. /* Shut up GCC warnings. */
  458. memset(&client_thread, 0, sizeof(client_thread));
  459. }
  460. max_fd = sharing_fds[0];
  461. nb_fds++;
  462. data = qemu_blockalign(bs, NBD_BUFFER_SIZE);
  463. if (data == NULL) {
  464. errx(EXIT_FAILURE, "Cannot allocate data buffer");
  465. }
  466. do {
  467. FD_ZERO(&fds);
  468. FD_SET(sigterm_fd[0], &fds);
  469. for (i = 0; i < nb_fds; i++)
  470. FD_SET(sharing_fds[i], &fds);
  471. do {
  472. ret = select(max_fd + 1, &fds, NULL, NULL, NULL);
  473. } while (ret == -1 && errno == EINTR);
  474. if (ret == -1 || FD_ISSET(sigterm_fd[0], &fds)) {
  475. break;
  476. }
  477. if (FD_ISSET(sharing_fds[0], &fds))
  478. ret--;
  479. for (i = 1; i < nb_fds && ret; i++) {
  480. if (FD_ISSET(sharing_fds[i], &fds)) {
  481. if (nbd_trip(bs, sharing_fds[i], fd_size, dev_offset,
  482. &offset, nbdflags, data, NBD_BUFFER_SIZE) != 0) {
  483. close(sharing_fds[i]);
  484. nb_fds--;
  485. sharing_fds[i] = sharing_fds[nb_fds];
  486. i--;
  487. }
  488. ret--;
  489. }
  490. }
  491. /* new connection ? */
  492. if (FD_ISSET(sharing_fds[0], &fds)) {
  493. if (nb_fds < shared + 1) {
  494. sharing_fds[nb_fds] = accept(sharing_fds[0],
  495. (struct sockaddr *)&addr,
  496. &addr_len);
  497. if (sharing_fds[nb_fds] != -1 &&
  498. nbd_negotiate(sharing_fds[nb_fds], fd_size, nbdflags) != -1) {
  499. if (sharing_fds[nb_fds] > max_fd)
  500. max_fd = sharing_fds[nb_fds];
  501. nb_fds++;
  502. }
  503. }
  504. }
  505. } while (persistent || nb_fds > 1);
  506. qemu_vfree(data);
  507. close(sharing_fds[0]);
  508. g_free(sharing_fds);
  509. if (sockpath) {
  510. unlink(sockpath);
  511. }
  512. if (device) {
  513. void *ret;
  514. pthread_join(client_thread, &ret);
  515. exit(ret != NULL);
  516. } else {
  517. exit(EXIT_SUCCESS);
  518. }
  519. }