2
0

qemu-nbd.c 39 KB

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