qemu-nbd.rst 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. .. _qemu-nbd:
  2. =====================================
  3. QEMU Disk Network Block Device Server
  4. =====================================
  5. Synopsis
  6. --------
  7. **qemu-nbd** [*OPTION*]... *filename*
  8. **qemu-nbd** -L [*OPTION*]...
  9. **qemu-nbd** -d *dev*
  10. Description
  11. -----------
  12. Export a QEMU disk image using the NBD protocol.
  13. Other uses:
  14. - Bind a /dev/nbdX block device to a QEMU server (on Linux).
  15. - As a client to query exports of a remote NBD server.
  16. Options
  17. -------
  18. .. program:: qemu-nbd
  19. *filename* is a disk image filename, or a set of block
  20. driver options if :option:`--image-opts` is specified.
  21. *dev* is an NBD device.
  22. .. option:: --object type,id=ID,...
  23. Define a new instance of the *type* object class identified by *ID*.
  24. See the :manpage:`qemu(1)` manual page for full details of the properties
  25. supported. The common object types that it makes sense to define are the
  26. ``secret`` object, which is used to supply passwords and/or encryption
  27. keys, and the ``tls-creds`` object, which is used to supply TLS
  28. credentials for the ``qemu-nbd`` server or client.
  29. .. option:: -p, --port=PORT
  30. TCP port to listen on as a server, or connect to as a client
  31. (default ``10809``).
  32. .. option:: -o, --offset=OFFSET
  33. The offset into the image.
  34. .. option:: -b, --bind=IFACE
  35. The interface to bind to as a server, or connect to as a client
  36. (default ``0.0.0.0``).
  37. .. option:: -k, --socket=PATH
  38. Use a unix socket with path *PATH*.
  39. .. option:: --image-opts
  40. Treat *filename* as a set of image options, instead of a plain
  41. filename. If this flag is specified, the ``-f`` flag should
  42. not be used, instead the :option:`format=` option should be set.
  43. .. option:: -f, --format=FMT
  44. Force the use of the block driver for format *FMT* instead of
  45. auto-detecting.
  46. .. option:: -r, --read-only
  47. Export the disk as read-only.
  48. .. option:: -A, --allocation-depth
  49. Expose allocation depth information via the
  50. ``qemu:allocation-depth`` metadata context accessible through
  51. NBD_OPT_SET_META_CONTEXT.
  52. .. option:: -B, --bitmap=NAME
  53. If *filename* has a qcow2 persistent bitmap *NAME*, expose
  54. that bitmap via the ``qemu:dirty-bitmap:NAME`` metadata context
  55. accessible through NBD_OPT_SET_META_CONTEXT.
  56. .. option:: -s, --snapshot
  57. Use *filename* as an external snapshot, create a temporary
  58. file with ``backing_file=``\ *filename*, redirect the write to
  59. the temporary one.
  60. .. option:: -l, --load-snapshot=SNAPSHOT_PARAM
  61. Load an internal snapshot inside *filename* and export it
  62. as an read-only device, SNAPSHOT_PARAM format is
  63. ``snapshot.id=[ID],snapshot.name=[NAME]`` or ``[ID_OR_NAME]``
  64. .. option:: --cache=CACHE
  65. The cache mode to be used with the file. Valid values are:
  66. ``none``, ``writeback`` (the default), ``writethrough``,
  67. ``directsync`` and ``unsafe``. See the documentation of
  68. the emulator's ``-drive cache=...`` option for more info.
  69. .. option:: -n, --nocache
  70. Equivalent to :option:`--cache=none`.
  71. .. option:: --aio=AIO
  72. Set the asynchronous I/O mode between ``threads`` (the default),
  73. ``native`` (Linux only), and ``io_uring`` (Linux 5.1+).
  74. .. option:: --discard=DISCARD
  75. Control whether ``discard`` (also known as ``trim`` or ``unmap``)
  76. requests are ignored or passed to the filesystem. *DISCARD* is one of
  77. ``ignore`` (or ``off``), ``unmap`` (or ``on``). The default is
  78. ``ignore``.
  79. .. option:: --detect-zeroes=DETECT_ZEROES
  80. Control the automatic conversion of plain zero writes by the OS to
  81. driver-specific optimized zero write commands. *DETECT_ZEROES* is one of
  82. ``off``, ``on``, or ``unmap``. ``unmap``
  83. converts a zero write to an unmap operation and can only be used if
  84. *DISCARD* is set to ``unmap``. The default is ``off``.
  85. .. option:: -c, --connect=DEV
  86. Connect *filename* to NBD device *DEV* (Linux only).
  87. .. option:: -d, --disconnect
  88. Disconnect the device *DEV* (Linux only).
  89. .. option:: -e, --shared=NUM
  90. Allow up to *NUM* clients to share the device (default
  91. ``1``), 0 for unlimited.
  92. .. option:: -t, --persistent
  93. Don't exit on the last connection.
  94. .. option:: -x, --export-name=NAME
  95. Set the NBD volume export name (default of a zero-length string).
  96. .. option:: -D, --description=DESCRIPTION
  97. Set the NBD volume export description, as a human-readable
  98. string.
  99. .. option:: --handshake-limit=N
  100. Set the timeout for a client to successfully complete its handshake
  101. to N seconds (default 10), or 0 for no limit.
  102. .. option:: -L, --list
  103. Connect as a client and list all details about the exports exposed by
  104. a remote NBD server. This enables list mode, and is incompatible
  105. with options that change behavior related to a specific export (such as
  106. :option:`--export-name`, :option:`--offset`, ...).
  107. .. option:: --tls-creds=ID
  108. Enable mandatory TLS encryption for the server by setting the ID
  109. of the TLS credentials object previously created with the
  110. :option:`--object` option; or provide the credentials needed for
  111. connecting as a client in list mode.
  112. .. option:: --tls-hostname=hostname
  113. When validating an x509 certificate received over a TLS connection,
  114. the hostname that the NBD client used to connect will be checked
  115. against information in the server provided certificate. Sometimes
  116. it might be required to override the hostname used to perform this
  117. check. For example, if the NBD client is using a tunnel from localhost
  118. to connect to the remote server, the :option:`--tls-hostname` option should
  119. be used to set the officially expected hostname of the remote NBD
  120. server. This can also be used if accessing NBD over a UNIX socket
  121. where there is no inherent hostname available. This is only permitted
  122. when acting as a NBD client with the :option:`--list` option.
  123. .. option:: --fork
  124. Fork off the server process and exit the parent once the server is running.
  125. .. option:: --pid-file=PATH
  126. Store the server's process ID in the given file.
  127. .. option:: --tls-authz=ID
  128. Specify the ID of a qauthz object previously created with the
  129. :option:`--object` option. This will be used to authorize connecting users
  130. against their x509 distinguished name.
  131. .. option:: -v, --verbose
  132. Display extra debugging information. This option also keeps the original
  133. *STDERR* stream open if the ``qemu-nbd`` process is daemonized due to
  134. other options like :option:`--fork` or :option:`-c`.
  135. .. option:: -h, --help
  136. Display this help and exit.
  137. .. option:: -V, --version
  138. Display version information and exit.
  139. .. option:: -T, --trace [[enable=]PATTERN][,events=FILE][,file=FILE]
  140. .. include:: ../qemu-option-trace.rst.inc
  141. Examples
  142. --------
  143. Start a server listening on port 10809 that exposes only the
  144. guest-visible contents of a qcow2 file, with no TLS encryption, and
  145. with the default export name (an empty string). The command is
  146. one-shot, and will block until the first successful client
  147. disconnects:
  148. ::
  149. qemu-nbd -f qcow2 file.qcow2
  150. Start a long-running server listening with encryption on port 10810,
  151. and allow clients with a specific X.509 certificate to connect to
  152. a 1 megabyte subset of a raw file, using the export name 'subset':
  153. ::
  154. qemu-nbd \
  155. --object tls-creds-x509,id=tls0,endpoint=server,dir=/path/to/qemutls \
  156. --object 'authz-simple,id=auth0,identity=CN=laptop.example.com,,\
  157. O=Example Org,,L=London,,ST=London,,C=GB' \
  158. --tls-creds tls0 --tls-authz auth0 \
  159. -t -x subset -p 10810 \
  160. --image-opts driver=raw,offset=1M,size=1M,file.driver=file,file.filename=file.raw
  161. Serve a read-only copy of a guest image over a Unix socket with as
  162. many as 5 simultaneous readers, with a persistent process forked as a
  163. daemon:
  164. ::
  165. qemu-nbd --fork --persistent --shared=5 --socket=/path/to/sock \
  166. --read-only --format=qcow2 file.qcow2
  167. Expose the guest-visible contents of a qcow2 file via a block device
  168. /dev/nbd0 (and possibly creating /dev/nbd0p1 and friends for
  169. partitions found within), then disconnect the device when done.
  170. Access to bind ``qemu-nbd`` to a /dev/nbd device generally requires root
  171. privileges, and may also require the execution of ``modprobe nbd``
  172. to enable the kernel NBD client module. *CAUTION*: Do not use
  173. this method to mount filesystems from an untrusted guest image - a
  174. malicious guest may have prepared the image to attempt to trigger
  175. kernel bugs in partition probing or file system mounting.
  176. ::
  177. qemu-nbd -c /dev/nbd0 -f qcow2 file.qcow2
  178. qemu-nbd -d /dev/nbd0
  179. Query a remote server to see details about what export(s) it is
  180. serving on port 10809, and authenticating via PSK:
  181. ::
  182. qemu-nbd \
  183. --object tls-creds-psk,id=tls0,dir=/tmp/keys,username=eblake,endpoint=client \
  184. --tls-creds tls0 -L -b remote.example.com
  185. See also
  186. --------
  187. :manpage:`qemu(1)`, :manpage:`qemu-img(1)`