live-block-operations.rst 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. ..
  2. Copyright (C) 2017 Red Hat Inc.
  3. This work is licensed under the terms of the GNU GPL, version 2 or
  4. later. See the COPYING file in the top-level directory.
  5. ============================
  6. Live Block Device Operations
  7. ============================
  8. QEMU Block Layer currently (as of QEMU 2.9) supports four major kinds of
  9. live block device jobs -- stream, commit, mirror, and backup. These can
  10. be used to manipulate disk image chains to accomplish certain tasks,
  11. namely: live copy data from backing files into overlays; shorten long
  12. disk image chains by merging data from overlays into backing files; live
  13. synchronize data from a disk image chain (including current active disk)
  14. to another target image; and point-in-time (and incremental) backups of
  15. a block device. Below is a description of the said block (QMP)
  16. primitives, and some (non-exhaustive list of) examples to illustrate
  17. their use.
  18. .. note::
  19. The file ``qapi/block-core.json`` in the QEMU source tree has the
  20. canonical QEMU API (QAPI) schema documentation for the QMP
  21. primitives discussed here.
  22. .. todo (kashyapc):: Remove the ".. contents::" directive when Sphinx is
  23. integrated.
  24. .. contents::
  25. Disk image backing chain notation
  26. ---------------------------------
  27. A simple disk image chain. (This can be created live using QMP
  28. ``blockdev-snapshot-sync``, or offline via ``qemu-img``)::
  29. (Live QEMU)
  30. |
  31. .
  32. V
  33. [A] <----- [B]
  34. (backing file) (overlay)
  35. The arrow can be read as: Image [A] is the backing file of disk image
  36. [B]. And live QEMU is currently writing to image [B], consequently, it
  37. is also referred to as the "active layer".
  38. There are two kinds of terminology that are common when referring to
  39. files in a disk image backing chain:
  40. (1) Directional: 'base' and 'top'. Given the simple disk image chain
  41. above, image [A] can be referred to as 'base', and image [B] as
  42. 'top'. (This terminology can be seen in in QAPI schema file,
  43. block-core.json.)
  44. (2) Relational: 'backing file' and 'overlay'. Again, taking the same
  45. simple disk image chain from the above, disk image [A] is referred
  46. to as the backing file, and image [B] as overlay.
  47. Throughout this document, we will use the relational terminology.
  48. .. important::
  49. The overlay files can generally be any format that supports a
  50. backing file, although QCOW2 is the preferred format and the one
  51. used in this document.
  52. Brief overview of live block QMP primitives
  53. -------------------------------------------
  54. The following are the four different kinds of live block operations that
  55. QEMU block layer supports.
  56. (1) ``block-stream``: Live copy of data from backing files into overlay
  57. files.
  58. .. note:: Once the 'stream' operation has finished, three things to
  59. note:
  60. (a) QEMU rewrites the backing chain to remove
  61. reference to the now-streamed and redundant backing
  62. file;
  63. (b) the streamed file *itself* won't be removed by QEMU,
  64. and must be explicitly discarded by the user;
  65. (c) the streamed file remains valid -- i.e. further
  66. overlays can be created based on it. Refer the
  67. ``block-stream`` section further below for more
  68. details.
  69. (2) ``block-commit``: Live merge of data from overlay files into backing
  70. files (with the optional goal of removing the overlay file from the
  71. chain). Since QEMU 2.0, this includes "active ``block-commit``"
  72. (i.e. merge the current active layer into the base image).
  73. .. note:: Once the 'commit' operation has finished, there are three
  74. things to note here as well:
  75. (a) QEMU rewrites the backing chain to remove reference
  76. to now-redundant overlay images that have been
  77. committed into a backing file;
  78. (b) the committed file *itself* won't be removed by QEMU
  79. -- it ought to be manually removed;
  80. (c) however, unlike in the case of ``block-stream``, the
  81. intermediate images will be rendered invalid -- i.e.
  82. no more further overlays can be created based on
  83. them. Refer the ``block-commit`` section further
  84. below for more details.
  85. (3) ``drive-mirror`` (and ``blockdev-mirror``): Synchronize a running
  86. disk to another image.
  87. (4) ``drive-backup`` (and ``blockdev-backup``): Point-in-time (live) copy
  88. of a block device to a destination.
  89. .. _`Interacting with a QEMU instance`:
  90. Interacting with a QEMU instance
  91. --------------------------------
  92. To show some example invocations of command-line, we will use the
  93. following invocation of QEMU, with a QMP server running over UNIX
  94. socket::
  95. $ ./x86_64-softmmu/qemu-system-x86_64 -display none -no-user-config \
  96. -M q35 -nodefaults -m 512 \
  97. -blockdev node-name=node-A,driver=qcow2,file.driver=file,file.node-name=file,file.filename=./a.qcow2 \
  98. -device virtio-blk,drive=node-A,id=virtio0 \
  99. -monitor stdio -qmp unix:/tmp/qmp-sock,server,nowait
  100. The ``-blockdev`` command-line option, used above, is available from
  101. QEMU 2.9 onwards. In the above invocation, notice the ``node-name``
  102. parameter that is used to refer to the disk image a.qcow2 ('node-A') --
  103. this is a cleaner way to refer to a disk image (as opposed to referring
  104. to it by spelling out file paths). So, we will continue to designate a
  105. ``node-name`` to each further disk image created (either via
  106. ``blockdev-snapshot-sync``, or ``blockdev-add``) as part of the disk
  107. image chain, and continue to refer to the disks using their
  108. ``node-name`` (where possible, because ``block-commit`` does not yet, as
  109. of QEMU 2.9, accept ``node-name`` parameter) when performing various
  110. block operations.
  111. To interact with the QEMU instance launched above, we will use the
  112. ``qmp-shell`` utility (located at: ``qemu/scripts/qmp``, as part of the
  113. QEMU source directory), which takes key-value pairs for QMP commands.
  114. Invoke it as below (which will also print out the complete raw JSON
  115. syntax for reference -- examples in the following sections)::
  116. $ ./qmp-shell -v -p /tmp/qmp-sock
  117. (QEMU)
  118. .. note::
  119. In the event we have to repeat a certain QMP command, we will: for
  120. the first occurrence of it, show the ``qmp-shell`` invocation, *and*
  121. the corresponding raw JSON QMP syntax; but for subsequent
  122. invocations, present just the ``qmp-shell`` syntax, and omit the
  123. equivalent JSON output.
  124. Example disk image chain
  125. ------------------------
  126. We will use the below disk image chain (and occasionally spelling it
  127. out where appropriate) when discussing various primitives::
  128. [A] <-- [B] <-- [C] <-- [D]
  129. Where [A] is the original base image; [B] and [C] are intermediate
  130. overlay images; image [D] is the active layer -- i.e. live QEMU is
  131. writing to it. (The rule of thumb is: live QEMU will always be pointing
  132. to the rightmost image in a disk image chain.)
  133. The above image chain can be created by invoking
  134. ``blockdev-snapshot-sync`` commands as following (which shows the
  135. creation of overlay image [B]) using the ``qmp-shell`` (our invocation
  136. also prints the raw JSON invocation of it)::
  137. (QEMU) blockdev-snapshot-sync node-name=node-A snapshot-file=b.qcow2 snapshot-node-name=node-B format=qcow2
  138. {
  139. "execute": "blockdev-snapshot-sync",
  140. "arguments": {
  141. "node-name": "node-A",
  142. "snapshot-file": "b.qcow2",
  143. "format": "qcow2",
  144. "snapshot-node-name": "node-B"
  145. }
  146. }
  147. Here, "node-A" is the name QEMU internally uses to refer to the base
  148. image [A] -- it is the backing file, based on which the overlay image,
  149. [B], is created.
  150. To create the rest of the overlay images, [C], and [D] (omitting the raw
  151. JSON output for brevity)::
  152. (QEMU) blockdev-snapshot-sync node-name=node-B snapshot-file=c.qcow2 snapshot-node-name=node-C format=qcow2
  153. (QEMU) blockdev-snapshot-sync node-name=node-C snapshot-file=d.qcow2 snapshot-node-name=node-D format=qcow2
  154. A note on points-in-time vs file names
  155. --------------------------------------
  156. In our disk image chain::
  157. [A] <-- [B] <-- [C] <-- [D]
  158. We have *three* points in time and an active layer:
  159. - Point 1: Guest state when [B] was created is contained in file [A]
  160. - Point 2: Guest state when [C] was created is contained in [A] + [B]
  161. - Point 3: Guest state when [D] was created is contained in
  162. [A] + [B] + [C]
  163. - Active layer: Current guest state is contained in [A] + [B] + [C] +
  164. [D]
  165. Therefore, be aware with naming choices:
  166. - Naming a file after the time it is created is misleading -- the
  167. guest data for that point in time is *not* contained in that file
  168. (as explained earlier)
  169. - Rather, think of files as a *delta* from the backing file
  170. Live block streaming --- ``block-stream``
  171. -----------------------------------------
  172. The ``block-stream`` command allows you to do live copy data from backing
  173. files into overlay images.
  174. Given our original example disk image chain from earlier::
  175. [A] <-- [B] <-- [C] <-- [D]
  176. The disk image chain can be shortened in one of the following different
  177. ways (not an exhaustive list).
  178. .. _`Case-1`:
  179. (1) Merge everything into the active layer: I.e. copy all contents from
  180. the base image, [A], and overlay images, [B] and [C], into [D],
  181. *while* the guest is running. The resulting chain will be a
  182. standalone image, [D] -- with contents from [A], [B] and [C] merged
  183. into it (where live QEMU writes go to)::
  184. [D]
  185. .. _`Case-2`:
  186. (2) Taking the same example disk image chain mentioned earlier, merge
  187. only images [B] and [C] into [D], the active layer. The result will
  188. be contents of images [B] and [C] will be copied into [D], and the
  189. backing file pointer of image [D] will be adjusted to point to image
  190. [A]. The resulting chain will be::
  191. [A] <-- [D]
  192. .. _`Case-3`:
  193. (3) Intermediate streaming (available since QEMU 2.8): Starting afresh
  194. with the original example disk image chain, with a total of four
  195. images, it is possible to copy contents from image [B] into image
  196. [C]. Once the copy is finished, image [B] can now be (optionally)
  197. discarded; and the backing file pointer of image [C] will be
  198. adjusted to point to [A]. I.e. after performing "intermediate
  199. streaming" of [B] into [C], the resulting image chain will be (where
  200. live QEMU is writing to [D])::
  201. [A] <-- [C] <-- [D]
  202. QMP invocation for ``block-stream``
  203. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  204. For `Case-1`_, to merge contents of all the backing files into the
  205. active layer, where 'node-D' is the current active image (by default
  206. ``block-stream`` will flatten the entire chain); ``qmp-shell`` (and its
  207. corresponding JSON output)::
  208. (QEMU) block-stream device=node-D job-id=job0
  209. {
  210. "execute": "block-stream",
  211. "arguments": {
  212. "device": "node-D",
  213. "job-id": "job0"
  214. }
  215. }
  216. For `Case-2`_, merge contents of the images [B] and [C] into [D], where
  217. image [D] ends up referring to image [A] as its backing file::
  218. (QEMU) block-stream device=node-D base-node=node-A job-id=job0
  219. And for `Case-3`_, of "intermediate" streaming", merge contents of
  220. images [B] into [C], where [C] ends up referring to [A] as its backing
  221. image::
  222. (QEMU) block-stream device=node-C base-node=node-A job-id=job0
  223. Progress of a ``block-stream`` operation can be monitored via the QMP
  224. command::
  225. (QEMU) query-block-jobs
  226. {
  227. "execute": "query-block-jobs",
  228. "arguments": {}
  229. }
  230. Once the ``block-stream`` operation has completed, QEMU will emit an
  231. event, ``BLOCK_JOB_COMPLETED``. The intermediate overlays remain valid,
  232. and can now be (optionally) discarded, or retained to create further
  233. overlays based on them. Finally, the ``block-stream`` jobs can be
  234. restarted at anytime.
  235. Live block commit --- ``block-commit``
  236. --------------------------------------
  237. The ``block-commit`` command lets you merge live data from overlay
  238. images into backing file(s). Since QEMU 2.0, this includes "live active
  239. commit" (i.e. it is possible to merge the "active layer", the right-most
  240. image in a disk image chain where live QEMU will be writing to, into the
  241. base image). This is analogous to ``block-stream``, but in the opposite
  242. direction.
  243. Again, starting afresh with our example disk image chain, where live
  244. QEMU is writing to the right-most image in the chain, [D]::
  245. [A] <-- [B] <-- [C] <-- [D]
  246. The disk image chain can be shortened in one of the following ways:
  247. .. _`block-commit_Case-1`:
  248. (1) Commit content from only image [B] into image [A]. The resulting
  249. chain is the following, where image [C] is adjusted to point at [A]
  250. as its new backing file::
  251. [A] <-- [C] <-- [D]
  252. (2) Commit content from images [B] and [C] into image [A]. The
  253. resulting chain, where image [D] is adjusted to point to image [A]
  254. as its new backing file::
  255. [A] <-- [D]
  256. .. _`block-commit_Case-3`:
  257. (3) Commit content from images [B], [C], and the active layer [D] into
  258. image [A]. The resulting chain (in this case, a consolidated single
  259. image)::
  260. [A]
  261. (4) Commit content from image only image [C] into image [B]. The
  262. resulting chain::
  263. [A] <-- [B] <-- [D]
  264. (5) Commit content from image [C] and the active layer [D] into image
  265. [B]. The resulting chain::
  266. [A] <-- [B]
  267. QMP invocation for ``block-commit``
  268. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  269. For :ref:`Case-1 <block-commit_Case-1>`, to merge contents only from
  270. image [B] into image [A], the invocation is as follows::
  271. (QEMU) block-commit device=node-D base=a.qcow2 top=b.qcow2 job-id=job0
  272. {
  273. "execute": "block-commit",
  274. "arguments": {
  275. "device": "node-D",
  276. "job-id": "job0",
  277. "top": "b.qcow2",
  278. "base": "a.qcow2"
  279. }
  280. }
  281. Once the above ``block-commit`` operation has completed, a
  282. ``BLOCK_JOB_COMPLETED`` event will be issued, and no further action is
  283. required. As the end result, the backing file of image [C] is adjusted
  284. to point to image [A], and the original 4-image chain will end up being
  285. transformed to::
  286. [A] <-- [C] <-- [D]
  287. .. note::
  288. The intermediate image [B] is invalid (as in: no more further
  289. overlays based on it can be created).
  290. Reasoning: An intermediate image after a 'stream' operation still
  291. represents that old point-in-time, and may be valid in that context.
  292. However, an intermediate image after a 'commit' operation no longer
  293. represents any point-in-time, and is invalid in any context.
  294. However, :ref:`Case-3 <block-commit_Case-3>` (also called: "active
  295. ``block-commit``") is a *two-phase* operation: In the first phase, the
  296. content from the active overlay, along with the intermediate overlays,
  297. is copied into the backing file (also called the base image). In the
  298. second phase, adjust the said backing file as the current active image
  299. -- possible via issuing the command ``block-job-complete``. Optionally,
  300. the ``block-commit`` operation can be cancelled by issuing the command
  301. ``block-job-cancel``, but be careful when doing this.
  302. Once the ``block-commit`` operation has completed, the event
  303. ``BLOCK_JOB_READY`` will be emitted, signalling that the synchronization
  304. has finished. Now the job can be gracefully completed by issuing the
  305. command ``block-job-complete`` -- until such a command is issued, the
  306. 'commit' operation remains active.
  307. The following is the flow for :ref:`Case-3 <block-commit_Case-3>` to
  308. convert a disk image chain such as this::
  309. [A] <-- [B] <-- [C] <-- [D]
  310. Into::
  311. [A]
  312. Where content from all the subsequent overlays, [B], and [C], including
  313. the active layer, [D], is committed back to [A] -- which is where live
  314. QEMU is performing all its current writes).
  315. Start the "active ``block-commit``" operation::
  316. (QEMU) block-commit device=node-D base=a.qcow2 top=d.qcow2 job-id=job0
  317. {
  318. "execute": "block-commit",
  319. "arguments": {
  320. "device": "node-D",
  321. "job-id": "job0",
  322. "top": "d.qcow2",
  323. "base": "a.qcow2"
  324. }
  325. }
  326. Once the synchronization has completed, the event ``BLOCK_JOB_READY`` will
  327. be emitted.
  328. Then, optionally query for the status of the active block operations.
  329. We can see the 'commit' job is now ready to be completed, as indicated
  330. by the line *"ready": true*::
  331. (QEMU) query-block-jobs
  332. {
  333. "execute": "query-block-jobs",
  334. "arguments": {}
  335. }
  336. {
  337. "return": [
  338. {
  339. "busy": false,
  340. "type": "commit",
  341. "len": 1376256,
  342. "paused": false,
  343. "ready": true,
  344. "io-status": "ok",
  345. "offset": 1376256,
  346. "device": "job0",
  347. "speed": 0
  348. }
  349. ]
  350. }
  351. Gracefully complete the 'commit' block device job::
  352. (QEMU) block-job-complete device=job0
  353. {
  354. "execute": "block-job-complete",
  355. "arguments": {
  356. "device": "job0"
  357. }
  358. }
  359. {
  360. "return": {}
  361. }
  362. Finally, once the above job is completed, an event
  363. ``BLOCK_JOB_COMPLETED`` will be emitted.
  364. .. note::
  365. The invocation for rest of the cases (2, 4, and 5), discussed in the
  366. previous section, is omitted for brevity.
  367. Live disk synchronization --- ``drive-mirror`` and ``blockdev-mirror``
  368. ----------------------------------------------------------------------
  369. Synchronize a running disk image chain (all or part of it) to a target
  370. image.
  371. Again, given our familiar disk image chain::
  372. [A] <-- [B] <-- [C] <-- [D]
  373. The ``drive-mirror`` (and its newer equivalent ``blockdev-mirror``)
  374. allows you to copy data from the entire chain into a single target image
  375. (which can be located on a different host), [E].
  376. .. note::
  377. When you cancel an in-progress 'mirror' job *before* the source and
  378. target are synchronized, ``block-job-cancel`` will emit the event
  379. ``BLOCK_JOB_CANCELLED``. However, note that if you cancel a
  380. 'mirror' job *after* it has indicated (via the event
  381. ``BLOCK_JOB_READY``) that the source and target have reached
  382. synchronization, then the event emitted by ``block-job-cancel``
  383. changes to ``BLOCK_JOB_COMPLETED``.
  384. Besides the 'mirror' job, the "active ``block-commit``" is the only
  385. other block device job that emits the event ``BLOCK_JOB_READY``.
  386. The rest of the block device jobs ('stream', "non-active
  387. ``block-commit``", and 'backup') end automatically.
  388. So there are two possible actions to take, after a 'mirror' job has
  389. emitted the event ``BLOCK_JOB_READY``, indicating that the source and
  390. target have reached synchronization:
  391. (1) Issuing the command ``block-job-cancel`` (after it emits the event
  392. ``BLOCK_JOB_COMPLETED``) will create a point-in-time (which is at
  393. the time of *triggering* the cancel command) copy of the entire disk
  394. image chain (or only the top-most image, depending on the ``sync``
  395. mode), contained in the target image [E]. One use case for this is
  396. live VM migration with non-shared storage.
  397. (2) Issuing the command ``block-job-complete`` (after it emits the event
  398. ``BLOCK_JOB_COMPLETED``) will adjust the guest device (i.e. live
  399. QEMU) to point to the target image, [E], causing all the new writes
  400. from this point on to happen there.
  401. About synchronization modes: The synchronization mode determines
  402. *which* part of the disk image chain will be copied to the target.
  403. Currently, there are four different kinds:
  404. (1) ``full`` -- Synchronize the content of entire disk image chain to
  405. the target
  406. (2) ``top`` -- Synchronize only the contents of the top-most disk image
  407. in the chain to the target
  408. (3) ``none`` -- Synchronize only the new writes from this point on.
  409. .. note:: In the case of ``drive-backup`` (or ``blockdev-backup``),
  410. the behavior of ``none`` synchronization mode is different.
  411. Normally, a ``backup`` job consists of two parts: Anything
  412. that is overwritten by the guest is first copied out to
  413. the backup, and in the background the whole image is
  414. copied from start to end. With ``sync=none``, it's only
  415. the first part.
  416. (4) ``incremental`` -- Synchronize content that is described by the
  417. dirty bitmap
  418. .. note::
  419. Refer to the :doc:`bitmaps` document in the QEMU source
  420. tree to learn about the detailed workings of the ``incremental``
  421. synchronization mode.
  422. QMP invocation for ``drive-mirror``
  423. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  424. To copy the contents of the entire disk image chain, from [A] all the
  425. way to [D], to a new target (``drive-mirror`` will create the destination
  426. file, if it doesn't already exist), call it [E]::
  427. (QEMU) drive-mirror device=node-D target=e.qcow2 sync=full job-id=job0
  428. {
  429. "execute": "drive-mirror",
  430. "arguments": {
  431. "device": "node-D",
  432. "job-id": "job0",
  433. "target": "e.qcow2",
  434. "sync": "full"
  435. }
  436. }
  437. The ``"sync": "full"``, from the above, means: copy the *entire* chain
  438. to the destination.
  439. Following the above, querying for active block jobs will show that a
  440. 'mirror' job is "ready" to be completed (and QEMU will also emit an
  441. event, ``BLOCK_JOB_READY``)::
  442. (QEMU) query-block-jobs
  443. {
  444. "execute": "query-block-jobs",
  445. "arguments": {}
  446. }
  447. {
  448. "return": [
  449. {
  450. "busy": false,
  451. "type": "mirror",
  452. "len": 21757952,
  453. "paused": false,
  454. "ready": true,
  455. "io-status": "ok",
  456. "offset": 21757952,
  457. "device": "job0",
  458. "speed": 0
  459. }
  460. ]
  461. }
  462. And, as noted in the previous section, there are two possible actions
  463. at this point:
  464. (a) Create a point-in-time snapshot by ending the synchronization. The
  465. point-in-time is at the time of *ending* the sync. (The result of
  466. the following being: the target image, [E], will be populated with
  467. content from the entire chain, [A] to [D])::
  468. (QEMU) block-job-cancel device=job0
  469. {
  470. "execute": "block-job-cancel",
  471. "arguments": {
  472. "device": "job0"
  473. }
  474. }
  475. (b) Or, complete the operation and pivot the live QEMU to the target
  476. copy::
  477. (QEMU) block-job-complete device=job0
  478. In either of the above cases, if you once again run the
  479. `query-block-jobs` command, there should not be any active block
  480. operation.
  481. Comparing 'commit' and 'mirror': In both then cases, the overlay images
  482. can be discarded. However, with 'commit', the *existing* base image
  483. will be modified (by updating it with contents from overlays); while in
  484. the case of 'mirror', a *new* target image is populated with the data
  485. from the disk image chain.
  486. QMP invocation for live storage migration with ``drive-mirror`` + NBD
  487. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  488. Live storage migration (without shared storage setup) is one of the most
  489. common use-cases that takes advantage of the ``drive-mirror`` primitive
  490. and QEMU's built-in Network Block Device (NBD) server. Here's a quick
  491. walk-through of this setup.
  492. Given the disk image chain::
  493. [A] <-- [B] <-- [C] <-- [D]
  494. Instead of copying content from the entire chain, synchronize *only* the
  495. contents of the *top*-most disk image (i.e. the active layer), [D], to a
  496. target, say, [TargetDisk].
  497. .. important::
  498. The destination host must already have the contents of the backing
  499. chain, involving images [A], [B], and [C], visible via other means
  500. -- whether by ``cp``, ``rsync``, or by some storage array-specific
  501. command.)
  502. Sometimes, this is also referred to as "shallow copy" -- because only
  503. the "active layer", and not the rest of the image chain, is copied to
  504. the destination.
  505. .. note::
  506. In this example, for the sake of simplicity, we'll be using the same
  507. ``localhost`` as both source and destination.
  508. As noted earlier, on the destination host the contents of the backing
  509. chain -- from images [A] to [C] -- are already expected to exist in some
  510. form (e.g. in a file called, ``Contents-of-A-B-C.qcow2``). Now, on the
  511. destination host, let's create a target overlay image (with the image
  512. ``Contents-of-A-B-C.qcow2`` as its backing file), to which the contents
  513. of image [D] (from the source QEMU) will be mirrored to::
  514. $ qemu-img create -f qcow2 -b ./Contents-of-A-B-C.qcow2 \
  515. -F qcow2 ./target-disk.qcow2
  516. And start the destination QEMU (we already have the source QEMU running
  517. -- discussed in the section: `Interacting with a QEMU instance`_)
  518. instance, with the following invocation. (As noted earlier, for
  519. simplicity's sake, the destination QEMU is started on the same host, but
  520. it could be located elsewhere)::
  521. $ ./x86_64-softmmu/qemu-system-x86_64 -display none -no-user-config \
  522. -M q35 -nodefaults -m 512 \
  523. -blockdev node-name=node-TargetDisk,driver=qcow2,file.driver=file,file.node-name=file,file.filename=./target-disk.qcow2 \
  524. -device virtio-blk,drive=node-TargetDisk,id=virtio0 \
  525. -S -monitor stdio -qmp unix:./qmp-sock2,server,nowait \
  526. -incoming tcp:localhost:6666
  527. Given the disk image chain on source QEMU::
  528. [A] <-- [B] <-- [C] <-- [D]
  529. On the destination host, it is expected that the contents of the chain
  530. ``[A] <-- [B] <-- [C]`` are *already* present, and therefore copy *only*
  531. the content of image [D].
  532. (1) [On *destination* QEMU] As part of the first step, start the
  533. built-in NBD server on a given host (local host, represented by
  534. ``::``)and port::
  535. (QEMU) nbd-server-start addr={"type":"inet","data":{"host":"::","port":"49153"}}
  536. {
  537. "execute": "nbd-server-start",
  538. "arguments": {
  539. "addr": {
  540. "data": {
  541. "host": "::",
  542. "port": "49153"
  543. },
  544. "type": "inet"
  545. }
  546. }
  547. }
  548. (2) [On *destination* QEMU] And export the destination disk image using
  549. QEMU's built-in NBD server::
  550. (QEMU) nbd-server-add device=node-TargetDisk writable=true
  551. {
  552. "execute": "nbd-server-add",
  553. "arguments": {
  554. "device": "node-TargetDisk"
  555. }
  556. }
  557. (3) [On *source* QEMU] Then, invoke ``drive-mirror`` (NB: since we're
  558. running ``drive-mirror`` with ``mode=existing`` (meaning:
  559. synchronize to a pre-created file, therefore 'existing', file on the
  560. target host), with the synchronization mode as 'top' (``"sync:
  561. "top"``)::
  562. (QEMU) drive-mirror device=node-D target=nbd:localhost:49153:exportname=node-TargetDisk sync=top mode=existing job-id=job0
  563. {
  564. "execute": "drive-mirror",
  565. "arguments": {
  566. "device": "node-D",
  567. "mode": "existing",
  568. "job-id": "job0",
  569. "target": "nbd:localhost:49153:exportname=node-TargetDisk",
  570. "sync": "top"
  571. }
  572. }
  573. (4) [On *source* QEMU] Once ``drive-mirror`` copies the entire data, and the
  574. event ``BLOCK_JOB_READY`` is emitted, issue ``block-job-cancel`` to
  575. gracefully end the synchronization, from source QEMU::
  576. (QEMU) block-job-cancel device=job0
  577. {
  578. "execute": "block-job-cancel",
  579. "arguments": {
  580. "device": "job0"
  581. }
  582. }
  583. (5) [On *destination* QEMU] Then, stop the NBD server::
  584. (QEMU) nbd-server-stop
  585. {
  586. "execute": "nbd-server-stop",
  587. "arguments": {}
  588. }
  589. (6) [On *destination* QEMU] Finally, resume the guest vCPUs by issuing the
  590. QMP command `cont`::
  591. (QEMU) cont
  592. {
  593. "execute": "cont",
  594. "arguments": {}
  595. }
  596. .. note::
  597. Higher-level libraries (e.g. libvirt) automate the entire above
  598. process (although note that libvirt does not allow same-host
  599. migrations to localhost for other reasons).
  600. Notes on ``blockdev-mirror``
  601. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  602. The ``blockdev-mirror`` command is equivalent in core functionality to
  603. ``drive-mirror``, except that it operates at node-level in a BDS graph.
  604. Also: for ``blockdev-mirror``, the 'target' image needs to be explicitly
  605. created (using ``qemu-img``) and attach it to live QEMU via
  606. ``blockdev-add``, which assigns a name to the to-be created target node.
  607. E.g. the sequence of actions to create a point-in-time backup of an
  608. entire disk image chain, to a target, using ``blockdev-mirror`` would be:
  609. (0) Create the QCOW2 overlays, to arrive at a backing chain of desired
  610. depth
  611. (1) Create the target image (using ``qemu-img``), say, ``e.qcow2``
  612. (2) Attach the above created file (``e.qcow2``), run-time, using
  613. ``blockdev-add`` to QEMU
  614. (3) Perform ``blockdev-mirror`` (use ``"sync": "full"`` to copy the
  615. entire chain to the target). And notice the event
  616. ``BLOCK_JOB_READY``
  617. (4) Optionally, query for active block jobs, there should be a 'mirror'
  618. job ready to be completed
  619. (5) Gracefully complete the 'mirror' block device job, and notice the
  620. the event ``BLOCK_JOB_COMPLETED``
  621. (6) Shutdown the guest by issuing the QMP ``quit`` command so that
  622. caches are flushed
  623. (7) Then, finally, compare the contents of the disk image chain, and
  624. the target copy with ``qemu-img compare``. You should notice:
  625. "Images are identical"
  626. QMP invocation for ``blockdev-mirror``
  627. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  628. Given the disk image chain::
  629. [A] <-- [B] <-- [C] <-- [D]
  630. To copy the contents of the entire disk image chain, from [A] all the
  631. way to [D], to a new target, call it [E]. The following is the flow.
  632. Create the overlay images, [B], [C], and [D]::
  633. (QEMU) blockdev-snapshot-sync node-name=node-A snapshot-file=b.qcow2 snapshot-node-name=node-B format=qcow2
  634. (QEMU) blockdev-snapshot-sync node-name=node-B snapshot-file=c.qcow2 snapshot-node-name=node-C format=qcow2
  635. (QEMU) blockdev-snapshot-sync node-name=node-C snapshot-file=d.qcow2 snapshot-node-name=node-D format=qcow2
  636. Create the target image, [E]::
  637. $ qemu-img create -f qcow2 e.qcow2 39M
  638. Add the above created target image to QEMU, via ``blockdev-add``::
  639. (QEMU) blockdev-add driver=qcow2 node-name=node-E file={"driver":"file","filename":"e.qcow2"}
  640. {
  641. "execute": "blockdev-add",
  642. "arguments": {
  643. "node-name": "node-E",
  644. "driver": "qcow2",
  645. "file": {
  646. "driver": "file",
  647. "filename": "e.qcow2"
  648. }
  649. }
  650. }
  651. Perform ``blockdev-mirror``, and notice the event ``BLOCK_JOB_READY``::
  652. (QEMU) blockdev-mirror device=node-B target=node-E sync=full job-id=job0
  653. {
  654. "execute": "blockdev-mirror",
  655. "arguments": {
  656. "device": "node-D",
  657. "job-id": "job0",
  658. "target": "node-E",
  659. "sync": "full"
  660. }
  661. }
  662. Query for active block jobs, there should be a 'mirror' job ready::
  663. (QEMU) query-block-jobs
  664. {
  665. "execute": "query-block-jobs",
  666. "arguments": {}
  667. }
  668. {
  669. "return": [
  670. {
  671. "busy": false,
  672. "type": "mirror",
  673. "len": 21561344,
  674. "paused": false,
  675. "ready": true,
  676. "io-status": "ok",
  677. "offset": 21561344,
  678. "device": "job0",
  679. "speed": 0
  680. }
  681. ]
  682. }
  683. Gracefully complete the block device job operation, and notice the
  684. event ``BLOCK_JOB_COMPLETED``::
  685. (QEMU) block-job-complete device=job0
  686. {
  687. "execute": "block-job-complete",
  688. "arguments": {
  689. "device": "job0"
  690. }
  691. }
  692. {
  693. "return": {}
  694. }
  695. Shutdown the guest, by issuing the ``quit`` QMP command::
  696. (QEMU) quit
  697. {
  698. "execute": "quit",
  699. "arguments": {}
  700. }
  701. Live disk backup --- ``drive-backup`` and ``blockdev-backup``
  702. -------------------------------------------------------------
  703. The ``drive-backup`` (and its newer equivalent ``blockdev-backup``) allows
  704. you to create a point-in-time snapshot.
  705. In this case, the point-in-time is when you *start* the ``drive-backup``
  706. (or its newer equivalent ``blockdev-backup``) command.
  707. QMP invocation for ``drive-backup``
  708. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  709. Yet again, starting afresh with our example disk image chain::
  710. [A] <-- [B] <-- [C] <-- [D]
  711. To create a target image [E], with content populated from image [A] to
  712. [D], from the above chain, the following is the syntax. (If the target
  713. image does not exist, ``drive-backup`` will create it)::
  714. (QEMU) drive-backup device=node-D sync=full target=e.qcow2 job-id=job0
  715. {
  716. "execute": "drive-backup",
  717. "arguments": {
  718. "device": "node-D",
  719. "job-id": "job0",
  720. "sync": "full",
  721. "target": "e.qcow2"
  722. }
  723. }
  724. Once the above ``drive-backup`` has completed, a ``BLOCK_JOB_COMPLETED`` event
  725. will be issued, indicating the live block device job operation has
  726. completed, and no further action is required.
  727. Notes on ``blockdev-backup``
  728. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  729. The ``blockdev-backup`` command is equivalent in functionality to
  730. ``drive-backup``, except that it operates at node-level in a Block Driver
  731. State (BDS) graph.
  732. E.g. the sequence of actions to create a point-in-time backup
  733. of an entire disk image chain, to a target, using ``blockdev-backup``
  734. would be:
  735. (0) Create the QCOW2 overlays, to arrive at a backing chain of desired
  736. depth
  737. (1) Create the target image (using ``qemu-img``), say, ``e.qcow2``
  738. (2) Attach the above created file (``e.qcow2``), run-time, using
  739. ``blockdev-add`` to QEMU
  740. (3) Perform ``blockdev-backup`` (use ``"sync": "full"`` to copy the
  741. entire chain to the target). And notice the event
  742. ``BLOCK_JOB_COMPLETED``
  743. (4) Shutdown the guest, by issuing the QMP ``quit`` command, so that
  744. caches are flushed
  745. (5) Then, finally, compare the contents of the disk image chain, and
  746. the target copy with ``qemu-img compare``. You should notice:
  747. "Images are identical"
  748. The following section shows an example QMP invocation for
  749. ``blockdev-backup``.
  750. QMP invocation for ``blockdev-backup``
  751. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  752. Given a disk image chain of depth 1 where image [B] is the active
  753. overlay (live QEMU is writing to it)::
  754. [A] <-- [B]
  755. The following is the procedure to copy the content from the entire chain
  756. to a target image (say, [E]), which has the full content from [A] and
  757. [B].
  758. Create the overlay [B]::
  759. (QEMU) blockdev-snapshot-sync node-name=node-A snapshot-file=b.qcow2 snapshot-node-name=node-B format=qcow2
  760. {
  761. "execute": "blockdev-snapshot-sync",
  762. "arguments": {
  763. "node-name": "node-A",
  764. "snapshot-file": "b.qcow2",
  765. "format": "qcow2",
  766. "snapshot-node-name": "node-B"
  767. }
  768. }
  769. Create a target image that will contain the copy::
  770. $ qemu-img create -f qcow2 e.qcow2 39M
  771. Then add it to QEMU via ``blockdev-add``::
  772. (QEMU) blockdev-add driver=qcow2 node-name=node-E file={"driver":"file","filename":"e.qcow2"}
  773. {
  774. "execute": "blockdev-add",
  775. "arguments": {
  776. "node-name": "node-E",
  777. "driver": "qcow2",
  778. "file": {
  779. "driver": "file",
  780. "filename": "e.qcow2"
  781. }
  782. }
  783. }
  784. Then invoke ``blockdev-backup`` to copy the contents from the entire
  785. image chain, consisting of images [A] and [B] to the target image
  786. 'e.qcow2'::
  787. (QEMU) blockdev-backup device=node-B target=node-E sync=full job-id=job0
  788. {
  789. "execute": "blockdev-backup",
  790. "arguments": {
  791. "device": "node-B",
  792. "job-id": "job0",
  793. "target": "node-E",
  794. "sync": "full"
  795. }
  796. }
  797. Once the above 'backup' operation has completed, the event,
  798. ``BLOCK_JOB_COMPLETED`` will be emitted, signalling successful
  799. completion.
  800. Next, query for any active block device jobs (there should be none)::
  801. (QEMU) query-block-jobs
  802. {
  803. "execute": "query-block-jobs",
  804. "arguments": {}
  805. }
  806. Shutdown the guest::
  807. (QEMU) quit
  808. {
  809. "execute": "quit",
  810. "arguments": {}
  811. }
  812. "return": {}
  813. }
  814. .. note::
  815. The above step is really important; if forgotten, an error, "Failed
  816. to get shared "write" lock on e.qcow2", will be thrown when you do
  817. ``qemu-img compare`` to verify the integrity of the disk image
  818. with the backup content.
  819. The end result will be the image 'e.qcow2' containing a
  820. point-in-time backup of the disk image chain -- i.e. contents from
  821. images [A] and [B] at the time the ``blockdev-backup`` command was
  822. initiated.
  823. One way to confirm the backup disk image contains the identical content
  824. with the disk image chain is to compare the backup and the contents of
  825. the chain, you should see "Images are identical". (NB: this is assuming
  826. QEMU was launched with ``-S`` option, which will not start the CPUs at
  827. guest boot up)::
  828. $ qemu-img compare b.qcow2 e.qcow2
  829. Warning: Image size mismatch!
  830. Images are identical.
  831. NOTE: The "Warning: Image size mismatch!" is expected, as we created the
  832. target image (e.qcow2) with 39M size.