You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1238 lines
38KB

  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/osdep.h"
  19. #include <getopt.h>
  20. #include <libgen.h>
  21. #include <pthread.h>
  22. #include "qapi/error.h"
  23. #include "qemu/cutils.h"
  24. #include "sysemu/block-backend.h"
  25. #include "block/block_int.h"
  26. #include "block/nbd.h"
  27. #include "qemu/main-loop.h"
  28. #include "qemu/option.h"
  29. #include "qemu/error-report.h"
  30. #include "qemu/config-file.h"
  31. #include "qemu/bswap.h"
  32. #include "qemu/log.h"
  33. #include "qemu/systemd.h"
  34. #include "block/snapshot.h"
  35. #include "qapi/qmp/qdict.h"
  36. #include "qapi/qmp/qstring.h"
  37. #include "qom/object_interfaces.h"
  38. #include "io/channel-socket.h"
  39. #include "io/net-listener.h"
  40. #include "crypto/init.h"
  41. #include "trace/control.h"
  42. #include "qemu-version.h"
  43. #ifdef __linux__
  44. #define HAVE_NBD_DEVICE 1
  45. #else
  46. #define HAVE_NBD_DEVICE 0
  47. #endif
  48. #define SOCKET_PATH "/var/lock/qemu-nbd-%s"
  49. #define QEMU_NBD_OPT_CACHE 256
  50. #define QEMU_NBD_OPT_AIO 257
  51. #define QEMU_NBD_OPT_DISCARD 258
  52. #define QEMU_NBD_OPT_DETECT_ZEROES 259
  53. #define QEMU_NBD_OPT_OBJECT 260
  54. #define QEMU_NBD_OPT_TLSCREDS 261
  55. #define QEMU_NBD_OPT_IMAGE_OPTS 262
  56. #define QEMU_NBD_OPT_FORK 263
  57. #define QEMU_NBD_OPT_TLSAUTHZ 264
  58. #define MBR_SIZE 512
  59. static NBDExport *export;
  60. static int verbose;
  61. static char *srcpath;
  62. static SocketAddress *saddr;
  63. static int persistent = 0;
  64. static enum { RUNNING, TERMINATE, TERMINATING, TERMINATED } state;
  65. static int shared = 1;
  66. static int nb_fds;
  67. static QIONetListener *server;
  68. static QCryptoTLSCreds *tlscreds;
  69. static const char *tlsauthz;
  70. static void usage(const char *name)
  71. {
  72. (printf) (
  73. "Usage: %s [OPTIONS] FILE\n"
  74. " or: %s -L [OPTIONS]\n"
  75. "QEMU Disk Network Block Device Utility\n"
  76. "\n"
  77. " -h, --help display this help and exit\n"
  78. " -V, --version output version information and exit\n"
  79. "\n"
  80. "Connection properties:\n"
  81. " -p, --port=PORT port to listen on (default `%d')\n"
  82. " -b, --bind=IFACE interface to bind to (default `0.0.0.0')\n"
  83. " -k, --socket=PATH path to the unix socket\n"
  84. " (default '"SOCKET_PATH"')\n"
  85. " -e, --shared=NUM device can be shared by NUM clients (default '1')\n"
  86. " -t, --persistent don't exit on the last connection\n"
  87. " -v, --verbose display extra debugging information\n"
  88. " -x, --export-name=NAME expose export by name (default is empty string)\n"
  89. " -D, --description=TEXT export a human-readable description\n"
  90. "\n"
  91. "Exposing part of the image:\n"
  92. " -o, --offset=OFFSET offset into the image\n"
  93. " -P, --partition=NUM only expose partition NUM\n"
  94. " -B, --bitmap=NAME expose a persistent dirty bitmap\n"
  95. "\n"
  96. "General purpose options:\n"
  97. " -L, --list list exports available from another NBD server\n"
  98. " --object type,id=ID,... define an object such as 'secret' for providing\n"
  99. " passwords and/or encryption keys\n"
  100. " --tls-creds=ID use id of an earlier --object to provide TLS\n"
  101. " --tls-authz=ID use id of an earlier --object to provide\n"
  102. " authorization\n"
  103. " -T, --trace [[enable=]<pattern>][,events=<file>][,file=<file>]\n"
  104. " specify tracing options\n"
  105. " --fork fork off the server process and exit the parent\n"
  106. " once the server is running\n"
  107. #if HAVE_NBD_DEVICE
  108. "\n"
  109. "Kernel NBD client support:\n"
  110. " -c, --connect=DEV connect FILE to the local NBD device DEV\n"
  111. " -d, --disconnect disconnect the specified device\n"
  112. #endif
  113. "\n"
  114. "Block device options:\n"
  115. " -f, --format=FORMAT set image format (raw, qcow2, ...)\n"
  116. " -r, --read-only export read-only\n"
  117. " -s, --snapshot use FILE as an external snapshot, create a temporary\n"
  118. " file with backing_file=FILE, redirect the write to\n"
  119. " the temporary one\n"
  120. " -l, --load-snapshot=SNAPSHOT_PARAM\n"
  121. " load an internal snapshot inside FILE and export it\n"
  122. " as an read-only device, SNAPSHOT_PARAM format is\n"
  123. " 'snapshot.id=[ID],snapshot.name=[NAME]', or\n"
  124. " '[ID_OR_NAME]'\n"
  125. " -n, --nocache disable host cache\n"
  126. " --cache=MODE set cache mode (none, writeback, ...)\n"
  127. " --aio=MODE set AIO mode (native or threads)\n"
  128. " --discard=MODE set discard mode (ignore, unmap)\n"
  129. " --detect-zeroes=MODE set detect-zeroes mode (off, on, unmap)\n"
  130. " --image-opts treat FILE as a full set of image options\n"
  131. "\n"
  132. QEMU_HELP_BOTTOM "\n"
  133. , name, name, NBD_DEFAULT_PORT, "DEVICE");
  134. }
  135. static void version(const char *name)
  136. {
  137. printf(
  138. "%s " QEMU_FULL_VERSION "\n"
  139. "Written by Anthony Liguori.\n"
  140. "\n"
  141. QEMU_COPYRIGHT "\n"
  142. "This is free software; see the source for copying conditions. There is NO\n"
  143. "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
  144. , name);
  145. }
  146. struct partition_record
  147. {
  148. uint8_t bootable;
  149. uint8_t start_head;
  150. uint32_t start_cylinder;
  151. uint8_t start_sector;
  152. uint8_t system;
  153. uint8_t end_head;
  154. uint8_t end_cylinder;
  155. uint8_t end_sector;
  156. uint32_t start_sector_abs;
  157. uint32_t nb_sectors_abs;
  158. };
  159. static void read_partition(uint8_t *p, struct partition_record *r)
  160. {
  161. r->bootable = p[0];
  162. r->start_head = p[1];
  163. r->start_cylinder = p[3] | ((p[2] << 2) & 0x0300);
  164. r->start_sector = p[2] & 0x3f;
  165. r->system = p[4];
  166. r->end_head = p[5];
  167. r->end_cylinder = p[7] | ((p[6] << 2) & 0x300);
  168. r->end_sector = p[6] & 0x3f;
  169. r->start_sector_abs = ldl_le_p(p + 8);
  170. r->nb_sectors_abs = ldl_le_p(p + 12);
  171. }
  172. static int find_partition(BlockBackend *blk, int partition,
  173. uint64_t *offset, uint64_t *size)
  174. {
  175. struct partition_record mbr[4];
  176. uint8_t data[MBR_SIZE];
  177. int i;
  178. int ext_partnum = 4;
  179. int ret;
  180. ret = blk_pread(blk, 0, data, sizeof(data));
  181. if (ret < 0) {
  182. error_report("error while reading: %s", strerror(-ret));
  183. exit(EXIT_FAILURE);
  184. }
  185. if (data[510] != 0x55 || data[511] != 0xaa) {
  186. return -EINVAL;
  187. }
  188. for (i = 0; i < 4; i++) {
  189. read_partition(&data[446 + 16 * i], &mbr[i]);
  190. if (!mbr[i].system || !mbr[i].nb_sectors_abs) {
  191. continue;
  192. }
  193. if (mbr[i].system == 0xF || mbr[i].system == 0x5) {
  194. struct partition_record ext[4];
  195. uint8_t data1[MBR_SIZE];
  196. int j;
  197. ret = blk_pread(blk, mbr[i].start_sector_abs * MBR_SIZE,
  198. data1, sizeof(data1));
  199. if (ret < 0) {
  200. error_report("error while reading: %s", strerror(-ret));
  201. exit(EXIT_FAILURE);
  202. }
  203. for (j = 0; j < 4; j++) {
  204. read_partition(&data1[446 + 16 * j], &ext[j]);
  205. if (!ext[j].system || !ext[j].nb_sectors_abs) {
  206. continue;
  207. }
  208. if ((ext_partnum + j + 1) == partition) {
  209. *offset = (uint64_t)ext[j].start_sector_abs << 9;
  210. *size = (uint64_t)ext[j].nb_sectors_abs << 9;
  211. return 0;
  212. }
  213. }
  214. ext_partnum += 4;
  215. } else if ((i + 1) == partition) {
  216. *offset = (uint64_t)mbr[i].start_sector_abs << 9;
  217. *size = (uint64_t)mbr[i].nb_sectors_abs << 9;
  218. return 0;
  219. }
  220. }
  221. return -ENOENT;
  222. }
  223. static void termsig_handler(int signum)
  224. {
  225. atomic_cmpxchg(&state, RUNNING, TERMINATE);
  226. qemu_notify_event();
  227. }
  228. static int qemu_nbd_client_list(SocketAddress *saddr, QCryptoTLSCreds *tls,
  229. const char *hostname)
  230. {
  231. int ret = EXIT_FAILURE;
  232. int rc;
  233. Error *err = NULL;
  234. QIOChannelSocket *sioc;
  235. NBDExportInfo *list;
  236. int i, j;
  237. sioc = qio_channel_socket_new();
  238. if (qio_channel_socket_connect_sync(sioc, saddr, &err) < 0) {
  239. error_report_err(err);
  240. return EXIT_FAILURE;
  241. }
  242. rc = nbd_receive_export_list(QIO_CHANNEL(sioc), tls, hostname, &list,
  243. &err);
  244. if (rc < 0) {
  245. if (err) {
  246. error_report_err(err);
  247. }
  248. goto out;
  249. }
  250. printf("exports available: %d\n", rc);
  251. for (i = 0; i < rc; i++) {
  252. printf(" export: '%s'\n", list[i].name);
  253. if (list[i].description && *list[i].description) {
  254. printf(" description: %s\n", list[i].description);
  255. }
  256. if (list[i].flags & NBD_FLAG_HAS_FLAGS) {
  257. printf(" size: %" PRIu64 "\n", list[i].size);
  258. printf(" flags: 0x%x (", list[i].flags);
  259. if (list[i].flags & NBD_FLAG_READ_ONLY) {
  260. printf(" readonly");
  261. }
  262. if (list[i].flags & NBD_FLAG_SEND_FLUSH) {
  263. printf(" flush");
  264. }
  265. if (list[i].flags & NBD_FLAG_SEND_FUA) {
  266. printf(" fua");
  267. }
  268. if (list[i].flags & NBD_FLAG_ROTATIONAL) {
  269. printf(" rotational");
  270. }
  271. if (list[i].flags & NBD_FLAG_SEND_TRIM) {
  272. printf(" trim");
  273. }
  274. if (list[i].flags & NBD_FLAG_SEND_WRITE_ZEROES) {
  275. printf(" zeroes");
  276. }
  277. if (list[i].flags & NBD_FLAG_SEND_DF) {
  278. printf(" df");
  279. }
  280. if (list[i].flags & NBD_FLAG_CAN_MULTI_CONN) {
  281. printf(" multi");
  282. }
  283. if (list[i].flags & NBD_FLAG_SEND_RESIZE) {
  284. printf(" resize");
  285. }
  286. if (list[i].flags & NBD_FLAG_SEND_CACHE) {
  287. printf(" cache");
  288. }
  289. printf(" )\n");
  290. }
  291. if (list[i].min_block) {
  292. printf(" min block: %u\n", list[i].min_block);
  293. printf(" opt block: %u\n", list[i].opt_block);
  294. printf(" max block: %u\n", list[i].max_block);
  295. }
  296. if (list[i].n_contexts) {
  297. printf(" available meta contexts: %d\n", list[i].n_contexts);
  298. for (j = 0; j < list[i].n_contexts; j++) {
  299. printf(" %s\n", list[i].contexts[j]);
  300. }
  301. }
  302. }
  303. nbd_free_export_list(list, rc);
  304. ret = EXIT_SUCCESS;
  305. out:
  306. object_unref(OBJECT(sioc));
  307. return ret;
  308. }
  309. #if HAVE_NBD_DEVICE
  310. static void *show_parts(void *arg)
  311. {
  312. char *device = arg;
  313. int nbd;
  314. /* linux just needs an open() to trigger
  315. * the partition table update
  316. * but remember to load the module with max_part != 0 :
  317. * modprobe nbd max_part=63
  318. */
  319. nbd = open(device, O_RDWR);
  320. if (nbd >= 0) {
  321. close(nbd);
  322. }
  323. return NULL;
  324. }
  325. static void *nbd_client_thread(void *arg)
  326. {
  327. char *device = arg;
  328. NBDExportInfo info = { .request_sizes = false, .name = g_strdup("") };
  329. QIOChannelSocket *sioc;
  330. int fd;
  331. int ret;
  332. pthread_t show_parts_thread;
  333. Error *local_error = NULL;
  334. sioc = qio_channel_socket_new();
  335. if (qio_channel_socket_connect_sync(sioc,
  336. saddr,
  337. &local_error) < 0) {
  338. error_report_err(local_error);
  339. goto out;
  340. }
  341. ret = nbd_receive_negotiate(QIO_CHANNEL(sioc),
  342. NULL, NULL, NULL, &info, &local_error);
  343. if (ret < 0) {
  344. if (local_error) {
  345. error_report_err(local_error);
  346. }
  347. goto out_socket;
  348. }
  349. fd = open(device, O_RDWR);
  350. if (fd < 0) {
  351. /* Linux-only, we can use %m in printf. */
  352. error_report("Failed to open %s: %m", device);
  353. goto out_socket;
  354. }
  355. ret = nbd_init(fd, sioc, &info, &local_error);
  356. if (ret < 0) {
  357. error_report_err(local_error);
  358. goto out_fd;
  359. }
  360. /* update partition table */
  361. pthread_create(&show_parts_thread, NULL, show_parts, device);
  362. if (verbose) {
  363. fprintf(stderr, "NBD device %s is now connected to %s\n",
  364. device, srcpath);
  365. } else {
  366. /* Close stderr so that the qemu-nbd process exits. */
  367. dup2(STDOUT_FILENO, STDERR_FILENO);
  368. }
  369. ret = nbd_client(fd);
  370. if (ret) {
  371. goto out_fd;
  372. }
  373. close(fd);
  374. object_unref(OBJECT(sioc));
  375. g_free(info.name);
  376. kill(getpid(), SIGTERM);
  377. return (void *) EXIT_SUCCESS;
  378. out_fd:
  379. close(fd);
  380. out_socket:
  381. object_unref(OBJECT(sioc));
  382. out:
  383. g_free(info.name);
  384. kill(getpid(), SIGTERM);
  385. return (void *) EXIT_FAILURE;
  386. }
  387. #endif /* HAVE_NBD_DEVICE */
  388. static int nbd_can_accept(void)
  389. {
  390. return state == RUNNING && nb_fds < shared;
  391. }
  392. static void nbd_export_closed(NBDExport *export)
  393. {
  394. assert(state == TERMINATING);
  395. state = TERMINATED;
  396. }
  397. static void nbd_update_server_watch(void);
  398. static void nbd_client_closed(NBDClient *client, bool negotiated)
  399. {
  400. nb_fds--;
  401. if (negotiated && nb_fds == 0 && !persistent && state == RUNNING) {
  402. state = TERMINATE;
  403. }
  404. nbd_update_server_watch();
  405. nbd_client_put(client);
  406. }
  407. static void nbd_accept(QIONetListener *listener, QIOChannelSocket *cioc,
  408. gpointer opaque)
  409. {
  410. if (state >= TERMINATE) {
  411. return;
  412. }
  413. nb_fds++;
  414. nbd_update_server_watch();
  415. nbd_client_new(cioc, tlscreds, tlsauthz, nbd_client_closed);
  416. }
  417. static void nbd_update_server_watch(void)
  418. {
  419. if (nbd_can_accept()) {
  420. qio_net_listener_set_client_func(server, nbd_accept, NULL, NULL);
  421. } else {
  422. qio_net_listener_set_client_func(server, NULL, NULL, NULL);
  423. }
  424. }
  425. static SocketAddress *nbd_build_socket_address(const char *sockpath,
  426. const char *bindto,
  427. const char *port)
  428. {
  429. SocketAddress *saddr;
  430. saddr = g_new0(SocketAddress, 1);
  431. if (sockpath) {
  432. saddr->type = SOCKET_ADDRESS_TYPE_UNIX;
  433. saddr->u.q_unix.path = g_strdup(sockpath);
  434. } else {
  435. InetSocketAddress *inet;
  436. saddr->type = SOCKET_ADDRESS_TYPE_INET;
  437. inet = &saddr->u.inet;
  438. inet->host = g_strdup(bindto);
  439. if (port) {
  440. inet->port = g_strdup(port);
  441. } else {
  442. inet->port = g_strdup_printf("%d", NBD_DEFAULT_PORT);
  443. }
  444. }
  445. return saddr;
  446. }
  447. static QemuOptsList file_opts = {
  448. .name = "file",
  449. .implied_opt_name = "file",
  450. .head = QTAILQ_HEAD_INITIALIZER(file_opts.head),
  451. .desc = {
  452. /* no elements => accept any params */
  453. { /* end of list */ }
  454. },
  455. };
  456. static QemuOptsList qemu_object_opts = {
  457. .name = "object",
  458. .implied_opt_name = "qom-type",
  459. .head = QTAILQ_HEAD_INITIALIZER(qemu_object_opts.head),
  460. .desc = {
  461. { }
  462. },
  463. };
  464. static QCryptoTLSCreds *nbd_get_tls_creds(const char *id, bool list,
  465. Error **errp)
  466. {
  467. Object *obj;
  468. QCryptoTLSCreds *creds;
  469. obj = object_resolve_path_component(
  470. object_get_objects_root(), id);
  471. if (!obj) {
  472. error_setg(errp, "No TLS credentials with id '%s'",
  473. id);
  474. return NULL;
  475. }
  476. creds = (QCryptoTLSCreds *)
  477. object_dynamic_cast(obj, TYPE_QCRYPTO_TLS_CREDS);
  478. if (!creds) {
  479. error_setg(errp, "Object with id '%s' is not TLS credentials",
  480. id);
  481. return NULL;
  482. }
  483. if (list) {
  484. if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_CLIENT) {
  485. error_setg(errp,
  486. "Expecting TLS credentials with a client endpoint");
  487. return NULL;
  488. }
  489. } else {
  490. if (creds->endpoint != QCRYPTO_TLS_CREDS_ENDPOINT_SERVER) {
  491. error_setg(errp,
  492. "Expecting TLS credentials with a server endpoint");
  493. return NULL;
  494. }
  495. }
  496. object_ref(obj);
  497. return creds;
  498. }
  499. static void setup_address_and_port(const char **address, const char **port)
  500. {
  501. if (*address == NULL) {
  502. *address = "0.0.0.0";
  503. }
  504. if (*port == NULL) {
  505. *port = stringify(NBD_DEFAULT_PORT);
  506. }
  507. }
  508. /*
  509. * Check socket parameters compatibility when socket activation is used.
  510. */
  511. static const char *socket_activation_validate_opts(const char *device,
  512. const char *sockpath,
  513. const char *address,
  514. const char *port,
  515. bool list)
  516. {
  517. if (device != NULL) {
  518. return "NBD device can't be set when using socket activation";
  519. }
  520. if (sockpath != NULL) {
  521. return "Unix socket can't be set when using socket activation";
  522. }
  523. if (address != NULL) {
  524. return "The interface can't be set when using socket activation";
  525. }
  526. if (port != NULL) {
  527. return "TCP port number can't be set when using socket activation";
  528. }
  529. if (list) {
  530. return "List mode is incompatible with socket activation";
  531. }
  532. return NULL;
  533. }
  534. static void qemu_nbd_shutdown(void)
  535. {
  536. job_cancel_sync_all();
  537. bdrv_close_all();
  538. }
  539. int main(int argc, char **argv)
  540. {
  541. BlockBackend *blk;
  542. BlockDriverState *bs;
  543. uint64_t dev_offset = 0;
  544. uint16_t nbdflags = 0;
  545. bool disconnect = false;
  546. const char *bindto = NULL;
  547. const char *port = NULL;
  548. char *sockpath = NULL;
  549. char *device = NULL;
  550. int64_t fd_size;
  551. QemuOpts *sn_opts = NULL;
  552. const char *sn_id_or_name = NULL;
  553. const char *sopt = "hVb:o:p:rsnP:c:dvk:e:f:tl:x:T:D:B:L";
  554. struct option lopt[] = {
  555. { "help", no_argument, NULL, 'h' },
  556. { "version", no_argument, NULL, 'V' },
  557. { "bind", required_argument, NULL, 'b' },
  558. { "port", required_argument, NULL, 'p' },
  559. { "socket", required_argument, NULL, 'k' },
  560. { "offset", required_argument, NULL, 'o' },
  561. { "read-only", no_argument, NULL, 'r' },
  562. { "partition", required_argument, NULL, 'P' },
  563. { "bitmap", required_argument, NULL, 'B' },
  564. { "connect", required_argument, NULL, 'c' },
  565. { "disconnect", no_argument, NULL, 'd' },
  566. { "list", no_argument, NULL, 'L' },
  567. { "snapshot", no_argument, NULL, 's' },
  568. { "load-snapshot", required_argument, NULL, 'l' },
  569. { "nocache", no_argument, NULL, 'n' },
  570. { "cache", required_argument, NULL, QEMU_NBD_OPT_CACHE },
  571. { "aio", required_argument, NULL, QEMU_NBD_OPT_AIO },
  572. { "discard", required_argument, NULL, QEMU_NBD_OPT_DISCARD },
  573. { "detect-zeroes", required_argument, NULL,
  574. QEMU_NBD_OPT_DETECT_ZEROES },
  575. { "shared", required_argument, NULL, 'e' },
  576. { "format", required_argument, NULL, 'f' },
  577. { "persistent", no_argument, NULL, 't' },
  578. { "verbose", no_argument, NULL, 'v' },
  579. { "object", required_argument, NULL, QEMU_NBD_OPT_OBJECT },
  580. { "export-name", required_argument, NULL, 'x' },
  581. { "description", required_argument, NULL, 'D' },
  582. { "tls-creds", required_argument, NULL, QEMU_NBD_OPT_TLSCREDS },
  583. { "tls-authz", required_argument, NULL, QEMU_NBD_OPT_TLSAUTHZ },
  584. { "image-opts", no_argument, NULL, QEMU_NBD_OPT_IMAGE_OPTS },
  585. { "trace", required_argument, NULL, 'T' },
  586. { "fork", no_argument, NULL, QEMU_NBD_OPT_FORK },
  587. { NULL, 0, NULL, 0 }
  588. };
  589. int ch;
  590. int opt_ind = 0;
  591. int flags = BDRV_O_RDWR;
  592. int partition = 0;
  593. int ret = 0;
  594. bool seen_cache = false;
  595. bool seen_discard = false;
  596. bool seen_aio = false;
  597. pthread_t client_thread;
  598. const char *fmt = NULL;
  599. Error *local_err = NULL;
  600. BlockdevDetectZeroesOptions detect_zeroes = BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF;
  601. QDict *options = NULL;
  602. const char *export_name = NULL; /* defaults to "" later for server mode */
  603. const char *export_description = NULL;
  604. const char *bitmap = NULL;
  605. const char *tlscredsid = NULL;
  606. bool imageOpts = false;
  607. bool writethrough = true;
  608. char *trace_file = NULL;
  609. bool fork_process = false;
  610. bool list = false;
  611. int old_stderr = -1;
  612. unsigned socket_activation;
  613. /* The client thread uses SIGTERM to interrupt the server. A signal
  614. * handler ensures that "qemu-nbd -v -c" exits with a nice status code.
  615. */
  616. struct sigaction sa_sigterm;
  617. memset(&sa_sigterm, 0, sizeof(sa_sigterm));
  618. sa_sigterm.sa_handler = termsig_handler;
  619. sigaction(SIGTERM, &sa_sigterm, NULL);
  620. #ifdef CONFIG_POSIX
  621. signal(SIGPIPE, SIG_IGN);
  622. #endif
  623. error_init(argv[0]);
  624. module_call_init(MODULE_INIT_TRACE);
  625. qcrypto_init(&error_fatal);
  626. module_call_init(MODULE_INIT_QOM);
  627. qemu_add_opts(&qemu_object_opts);
  628. qemu_add_opts(&qemu_trace_opts);
  629. qemu_init_exec_dir(argv[0]);
  630. while ((ch = getopt_long(argc, argv, sopt, lopt, &opt_ind)) != -1) {
  631. switch (ch) {
  632. case 's':
  633. flags |= BDRV_O_SNAPSHOT;
  634. break;
  635. case 'n':
  636. optarg = (char *) "none";
  637. /* fallthrough */
  638. case QEMU_NBD_OPT_CACHE:
  639. if (seen_cache) {
  640. error_report("-n and --cache can only be specified once");
  641. exit(EXIT_FAILURE);
  642. }
  643. seen_cache = true;
  644. if (bdrv_parse_cache_mode(optarg, &flags, &writethrough) == -1) {
  645. error_report("Invalid cache mode `%s'", optarg);
  646. exit(EXIT_FAILURE);
  647. }
  648. break;
  649. case QEMU_NBD_OPT_AIO:
  650. if (seen_aio) {
  651. error_report("--aio can only be specified once");
  652. exit(EXIT_FAILURE);
  653. }
  654. seen_aio = true;
  655. if (!strcmp(optarg, "native")) {
  656. flags |= BDRV_O_NATIVE_AIO;
  657. } else if (!strcmp(optarg, "threads")) {
  658. /* this is the default */
  659. } else {
  660. error_report("invalid aio mode `%s'", optarg);
  661. exit(EXIT_FAILURE);
  662. }
  663. break;
  664. case QEMU_NBD_OPT_DISCARD:
  665. if (seen_discard) {
  666. error_report("--discard can only be specified once");
  667. exit(EXIT_FAILURE);
  668. }
  669. seen_discard = true;
  670. if (bdrv_parse_discard_flags(optarg, &flags) == -1) {
  671. error_report("Invalid discard mode `%s'", optarg);
  672. exit(EXIT_FAILURE);
  673. }
  674. break;
  675. case QEMU_NBD_OPT_DETECT_ZEROES:
  676. detect_zeroes =
  677. qapi_enum_parse(&BlockdevDetectZeroesOptions_lookup,
  678. optarg,
  679. BLOCKDEV_DETECT_ZEROES_OPTIONS_OFF,
  680. &local_err);
  681. if (local_err) {
  682. error_reportf_err(local_err,
  683. "Failed to parse detect_zeroes mode: ");
  684. exit(EXIT_FAILURE);
  685. }
  686. if (detect_zeroes == BLOCKDEV_DETECT_ZEROES_OPTIONS_UNMAP &&
  687. !(flags & BDRV_O_UNMAP)) {
  688. error_report("setting detect-zeroes to unmap is not allowed "
  689. "without setting discard operation to unmap");
  690. exit(EXIT_FAILURE);
  691. }
  692. break;
  693. case 'b':
  694. bindto = optarg;
  695. break;
  696. case 'p':
  697. port = optarg;
  698. break;
  699. case 'o':
  700. if (qemu_strtou64(optarg, NULL, 0, &dev_offset) < 0) {
  701. error_report("Invalid offset '%s'", optarg);
  702. exit(EXIT_FAILURE);
  703. }
  704. break;
  705. case 'l':
  706. if (strstart(optarg, SNAPSHOT_OPT_BASE, NULL)) {
  707. sn_opts = qemu_opts_parse_noisily(&internal_snapshot_opts,
  708. optarg, false);
  709. if (!sn_opts) {
  710. error_report("Failed in parsing snapshot param `%s'",
  711. optarg);
  712. exit(EXIT_FAILURE);
  713. }
  714. } else {
  715. sn_id_or_name = optarg;
  716. }
  717. /* fall through */
  718. case 'r':
  719. nbdflags |= NBD_FLAG_READ_ONLY;
  720. flags &= ~BDRV_O_RDWR;
  721. break;
  722. case 'P':
  723. warn_report("The '-P' option is deprecated; use --image-opts with "
  724. "a raw device wrapper for subset exports instead");
  725. if (qemu_strtoi(optarg, NULL, 0, &partition) < 0 ||
  726. partition < 1 || partition > 8) {
  727. error_report("Invalid partition '%s'", optarg);
  728. exit(EXIT_FAILURE);
  729. }
  730. break;
  731. case 'B':
  732. bitmap = optarg;
  733. break;
  734. case 'k':
  735. sockpath = optarg;
  736. if (sockpath[0] != '/') {
  737. error_report("socket path must be absolute");
  738. exit(EXIT_FAILURE);
  739. }
  740. break;
  741. case 'd':
  742. disconnect = true;
  743. break;
  744. case 'c':
  745. device = optarg;
  746. break;
  747. case 'e':
  748. if (qemu_strtoi(optarg, NULL, 0, &shared) < 0 ||
  749. shared < 1) {
  750. error_report("Invalid shared device number '%s'", optarg);
  751. exit(EXIT_FAILURE);
  752. }
  753. break;
  754. case 'f':
  755. fmt = optarg;
  756. break;
  757. case 't':
  758. persistent = 1;
  759. break;
  760. case 'x':
  761. export_name = optarg;
  762. break;
  763. case 'D':
  764. export_description = optarg;
  765. break;
  766. case 'v':
  767. verbose = 1;
  768. break;
  769. case 'V':
  770. version(argv[0]);
  771. exit(0);
  772. break;
  773. case 'h':
  774. usage(argv[0]);
  775. exit(0);
  776. break;
  777. case '?':
  778. error_report("Try `%s --help' for more information.", argv[0]);
  779. exit(EXIT_FAILURE);
  780. case QEMU_NBD_OPT_OBJECT: {
  781. QemuOpts *opts;
  782. opts = qemu_opts_parse_noisily(&qemu_object_opts,
  783. optarg, true);
  784. if (!opts) {
  785. exit(EXIT_FAILURE);
  786. }
  787. } break;
  788. case QEMU_NBD_OPT_TLSCREDS:
  789. tlscredsid = optarg;
  790. break;
  791. case QEMU_NBD_OPT_IMAGE_OPTS:
  792. imageOpts = true;
  793. break;
  794. case 'T':
  795. g_free(trace_file);
  796. trace_file = trace_opt_parse(optarg);
  797. break;
  798. case QEMU_NBD_OPT_TLSAUTHZ:
  799. tlsauthz = optarg;
  800. break;
  801. case QEMU_NBD_OPT_FORK:
  802. fork_process = true;
  803. break;
  804. case 'L':
  805. list = true;
  806. break;
  807. }
  808. }
  809. if (list) {
  810. if (argc != optind) {
  811. error_report("List mode is incompatible with a file name");
  812. exit(EXIT_FAILURE);
  813. }
  814. if (export_name || export_description || dev_offset || partition ||
  815. device || disconnect || fmt || sn_id_or_name || bitmap ||
  816. seen_aio || seen_discard || seen_cache) {
  817. error_report("List mode is incompatible with per-device settings");
  818. exit(EXIT_FAILURE);
  819. }
  820. if (fork_process) {
  821. error_report("List mode is incompatible with forking");
  822. exit(EXIT_FAILURE);
  823. }
  824. } else if ((argc - optind) != 1) {
  825. error_report("Invalid number of arguments");
  826. error_printf("Try `%s --help' for more information.\n", argv[0]);
  827. exit(EXIT_FAILURE);
  828. } else if (!export_name) {
  829. export_name = "";
  830. }
  831. qemu_opts_foreach(&qemu_object_opts,
  832. user_creatable_add_opts_foreach,
  833. NULL, &error_fatal);
  834. if (!trace_init_backends()) {
  835. exit(1);
  836. }
  837. trace_init_file(trace_file);
  838. qemu_set_log(LOG_TRACE);
  839. socket_activation = check_socket_activation();
  840. if (socket_activation == 0) {
  841. setup_address_and_port(&bindto, &port);
  842. } else {
  843. /* Using socket activation - check user didn't use -p etc. */
  844. const char *err_msg = socket_activation_validate_opts(device, sockpath,
  845. bindto, port,
  846. list);
  847. if (err_msg != NULL) {
  848. error_report("%s", err_msg);
  849. exit(EXIT_FAILURE);
  850. }
  851. /* qemu-nbd can only listen on a single socket. */
  852. if (socket_activation > 1) {
  853. error_report("qemu-nbd does not support socket activation with %s > 1",
  854. "LISTEN_FDS");
  855. exit(EXIT_FAILURE);
  856. }
  857. }
  858. if (tlscredsid) {
  859. if (sockpath) {
  860. error_report("TLS is only supported with IPv4/IPv6");
  861. exit(EXIT_FAILURE);
  862. }
  863. if (device) {
  864. error_report("TLS is not supported with a host device");
  865. exit(EXIT_FAILURE);
  866. }
  867. if (tlsauthz && list) {
  868. error_report("TLS authorization is incompatible with export list");
  869. exit(EXIT_FAILURE);
  870. }
  871. tlscreds = nbd_get_tls_creds(tlscredsid, list, &local_err);
  872. if (local_err) {
  873. error_report("Failed to get TLS creds %s",
  874. error_get_pretty(local_err));
  875. exit(EXIT_FAILURE);
  876. }
  877. } else {
  878. if (tlsauthz) {
  879. error_report("--tls-authz is not permitted without --tls-creds");
  880. exit(EXIT_FAILURE);
  881. }
  882. }
  883. if (list) {
  884. saddr = nbd_build_socket_address(sockpath, bindto, port);
  885. return qemu_nbd_client_list(saddr, tlscreds, bindto);
  886. }
  887. #if !HAVE_NBD_DEVICE
  888. if (disconnect || device) {
  889. error_report("Kernel /dev/nbdN support not available");
  890. exit(EXIT_FAILURE);
  891. }
  892. #else /* HAVE_NBD_DEVICE */
  893. if (disconnect) {
  894. int nbdfd = open(argv[optind], O_RDWR);
  895. if (nbdfd < 0) {
  896. error_report("Cannot open %s: %s", argv[optind],
  897. strerror(errno));
  898. exit(EXIT_FAILURE);
  899. }
  900. nbd_disconnect(nbdfd);
  901. close(nbdfd);
  902. printf("%s disconnected\n", argv[optind]);
  903. return 0;
  904. }
  905. #endif
  906. if ((device && !verbose) || fork_process) {
  907. int stderr_fd[2];
  908. pid_t pid;
  909. int ret;
  910. if (qemu_pipe(stderr_fd) < 0) {
  911. error_report("Error setting up communication pipe: %s",
  912. strerror(errno));
  913. exit(EXIT_FAILURE);
  914. }
  915. /* Now daemonize, but keep a communication channel open to
  916. * print errors and exit with the proper status code.
  917. */
  918. pid = fork();
  919. if (pid < 0) {
  920. error_report("Failed to fork: %s", strerror(errno));
  921. exit(EXIT_FAILURE);
  922. } else if (pid == 0) {
  923. close(stderr_fd[0]);
  924. ret = qemu_daemon(1, 0);
  925. /* Temporarily redirect stderr to the parent's pipe... */
  926. old_stderr = dup(STDERR_FILENO);
  927. dup2(stderr_fd[1], STDERR_FILENO);
  928. if (ret < 0) {
  929. error_report("Failed to daemonize: %s", strerror(errno));
  930. exit(EXIT_FAILURE);
  931. }
  932. /* ... close the descriptor we inherited and go on. */
  933. close(stderr_fd[1]);
  934. } else {
  935. bool errors = false;
  936. char *buf;
  937. /* In the parent. Print error messages from the child until
  938. * it closes the pipe.
  939. */
  940. close(stderr_fd[1]);
  941. buf = g_malloc(1024);
  942. while ((ret = read(stderr_fd[0], buf, 1024)) > 0) {
  943. errors = true;
  944. ret = qemu_write_full(STDERR_FILENO, buf, ret);
  945. if (ret < 0) {
  946. exit(EXIT_FAILURE);
  947. }
  948. }
  949. if (ret < 0) {
  950. error_report("Cannot read from daemon: %s",
  951. strerror(errno));
  952. exit(EXIT_FAILURE);
  953. }
  954. /* Usually the daemon should not print any message.
  955. * Exit with zero status in that case.
  956. */
  957. exit(errors);
  958. }
  959. }
  960. if (device != NULL && sockpath == NULL) {
  961. sockpath = g_malloc(128);
  962. snprintf(sockpath, 128, SOCKET_PATH, basename(device));
  963. }
  964. server = qio_net_listener_new();
  965. if (socket_activation == 0) {
  966. saddr = nbd_build_socket_address(sockpath, bindto, port);
  967. if (qio_net_listener_open_sync(server, saddr, &local_err) < 0) {
  968. object_unref(OBJECT(server));
  969. error_report_err(local_err);
  970. exit(EXIT_FAILURE);
  971. }
  972. } else {
  973. size_t i;
  974. /* See comment in check_socket_activation above. */
  975. for (i = 0; i < socket_activation; i++) {
  976. QIOChannelSocket *sioc;
  977. sioc = qio_channel_socket_new_fd(FIRST_SOCKET_ACTIVATION_FD + i,
  978. &local_err);
  979. if (sioc == NULL) {
  980. object_unref(OBJECT(server));
  981. error_report("Failed to use socket activation: %s",
  982. error_get_pretty(local_err));
  983. exit(EXIT_FAILURE);
  984. }
  985. qio_net_listener_add(server, sioc);
  986. object_unref(OBJECT(sioc));
  987. }
  988. }
  989. if (qemu_init_main_loop(&local_err)) {
  990. error_report_err(local_err);
  991. exit(EXIT_FAILURE);
  992. }
  993. bdrv_init();
  994. atexit(qemu_nbd_shutdown);
  995. srcpath = argv[optind];
  996. if (imageOpts) {
  997. QemuOpts *opts;
  998. if (fmt) {
  999. error_report("--image-opts and -f are mutually exclusive");
  1000. exit(EXIT_FAILURE);
  1001. }
  1002. opts = qemu_opts_parse_noisily(&file_opts, srcpath, true);
  1003. if (!opts) {
  1004. qemu_opts_reset(&file_opts);
  1005. exit(EXIT_FAILURE);
  1006. }
  1007. options = qemu_opts_to_qdict(opts, NULL);
  1008. qemu_opts_reset(&file_opts);
  1009. blk = blk_new_open(NULL, NULL, options, flags, &local_err);
  1010. } else {
  1011. if (fmt) {
  1012. options = qdict_new();
  1013. qdict_put_str(options, "driver", fmt);
  1014. }
  1015. blk = blk_new_open(srcpath, NULL, options, flags, &local_err);
  1016. }
  1017. if (!blk) {
  1018. error_reportf_err(local_err, "Failed to blk_new_open '%s': ",
  1019. argv[optind]);
  1020. exit(EXIT_FAILURE);
  1021. }
  1022. bs = blk_bs(blk);
  1023. blk_set_enable_write_cache(blk, !writethrough);
  1024. if (sn_opts) {
  1025. ret = bdrv_snapshot_load_tmp(bs,
  1026. qemu_opt_get(sn_opts, SNAPSHOT_OPT_ID),
  1027. qemu_opt_get(sn_opts, SNAPSHOT_OPT_NAME),
  1028. &local_err);
  1029. } else if (sn_id_or_name) {
  1030. ret = bdrv_snapshot_load_tmp_by_id_or_name(bs, sn_id_or_name,
  1031. &local_err);
  1032. }
  1033. if (ret < 0) {
  1034. error_reportf_err(local_err, "Failed to load snapshot: ");
  1035. exit(EXIT_FAILURE);
  1036. }
  1037. bs->detect_zeroes = detect_zeroes;
  1038. fd_size = blk_getlength(blk);
  1039. if (fd_size < 0) {
  1040. error_report("Failed to determine the image length: %s",
  1041. strerror(-fd_size));
  1042. exit(EXIT_FAILURE);
  1043. }
  1044. if (dev_offset >= fd_size) {
  1045. error_report("Offset (%" PRIu64 ") has to be smaller than the image "
  1046. "size (%" PRId64 ")", dev_offset, fd_size);
  1047. exit(EXIT_FAILURE);
  1048. }
  1049. fd_size -= dev_offset;
  1050. if (partition) {
  1051. uint64_t limit;
  1052. if (dev_offset) {
  1053. error_report("Cannot request partition and offset together");
  1054. exit(EXIT_FAILURE);
  1055. }
  1056. ret = find_partition(blk, partition, &dev_offset, &limit);
  1057. if (ret < 0) {
  1058. error_report("Could not find partition %d: %s", partition,
  1059. strerror(-ret));
  1060. exit(EXIT_FAILURE);
  1061. }
  1062. /*
  1063. * MBR partition limits are (32-bit << 9); this assert lets
  1064. * the compiler know that we can't overflow 64 bits.
  1065. */
  1066. assert(dev_offset + limit >= dev_offset);
  1067. if (dev_offset + limit > fd_size) {
  1068. error_report("Discovered partition %d at offset %" PRIu64
  1069. " size %" PRIu64 ", but size exceeds file length %"
  1070. PRId64, partition, dev_offset, limit, fd_size);
  1071. exit(EXIT_FAILURE);
  1072. }
  1073. fd_size = limit;
  1074. }
  1075. export = nbd_export_new(bs, dev_offset, fd_size, export_name,
  1076. export_description, bitmap, nbdflags,
  1077. nbd_export_closed, writethrough, NULL,
  1078. &error_fatal);
  1079. if (device) {
  1080. #if HAVE_NBD_DEVICE
  1081. int ret;
  1082. ret = pthread_create(&client_thread, NULL, nbd_client_thread, device);
  1083. if (ret != 0) {
  1084. error_report("Failed to create client thread: %s", strerror(ret));
  1085. exit(EXIT_FAILURE);
  1086. }
  1087. #endif
  1088. } else {
  1089. /* Shut up GCC warnings. */
  1090. memset(&client_thread, 0, sizeof(client_thread));
  1091. }
  1092. nbd_update_server_watch();
  1093. /* now when the initialization is (almost) complete, chdir("/")
  1094. * to free any busy filesystems */
  1095. if (chdir("/") < 0) {
  1096. error_report("Could not chdir to root directory: %s",
  1097. strerror(errno));
  1098. exit(EXIT_FAILURE);
  1099. }
  1100. if (fork_process) {
  1101. dup2(old_stderr, STDERR_FILENO);
  1102. close(old_stderr);
  1103. }
  1104. state = RUNNING;
  1105. do {
  1106. main_loop_wait(false);
  1107. if (state == TERMINATE) {
  1108. state = TERMINATING;
  1109. nbd_export_close(export);
  1110. nbd_export_put(export);
  1111. export = NULL;
  1112. }
  1113. } while (state != TERMINATED);
  1114. blk_unref(blk);
  1115. if (sockpath) {
  1116. unlink(sockpath);
  1117. }
  1118. qemu_opts_del(sn_opts);
  1119. if (device) {
  1120. void *ret;
  1121. pthread_join(client_thread, &ret);
  1122. exit(ret != NULL);
  1123. } else {
  1124. exit(EXIT_SUCCESS);
  1125. }
  1126. }