live-block-operations.rst 37 KB

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