CPR.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. CheckPoint and Restart (CPR)
  2. ============================
  3. CPR is the umbrella name for a set of migration modes in which the
  4. VM is migrated to a new QEMU instance on the same host. It is
  5. intended for use when the goal is to update host software components
  6. that run the VM, such as QEMU or even the host kernel. At this time,
  7. the cpr-reboot and cpr-transfer modes are available.
  8. Because QEMU is restarted on the same host, with access to the same
  9. local devices, CPR is allowed in certain cases where normal migration
  10. would be blocked. However, the user must not modify the contents of
  11. guest block devices between quitting old QEMU and starting new QEMU.
  12. CPR unconditionally stops VM execution before memory is saved, and
  13. thus does not depend on any form of dirty page tracking.
  14. cpr-reboot mode
  15. ---------------
  16. In this mode, QEMU stops the VM, and writes VM state to the migration
  17. URI, which will typically be a file. After quitting QEMU, the user
  18. resumes by running QEMU with the ``-incoming`` option. Because the
  19. old and new QEMU instances are not active concurrently, the URI cannot
  20. be a type that streams data from one instance to the other.
  21. Guest RAM can be saved in place if backed by shared memory, or can be
  22. copied to a file. The former is more efficient and is therefore
  23. preferred.
  24. After state and memory are saved, the user may update userland host
  25. software before restarting QEMU and resuming the VM. Further, if
  26. the RAM is backed by persistent shared memory, such as a DAX device,
  27. then the user may reboot to a new host kernel before restarting QEMU.
  28. This mode supports VFIO devices provided the user first puts the
  29. guest in the suspended runstate, such as by issuing the
  30. ``guest-suspend-ram`` command to the QEMU guest agent. The agent
  31. must be pre-installed in the guest, and the guest must support
  32. suspend to RAM. Beware that suspension can take a few seconds, so
  33. the user should poll to see the suspended state before proceeding
  34. with the CPR operation.
  35. Usage
  36. ^^^^^
  37. It is recommended that guest RAM be backed with some type of shared
  38. memory, such as ``memory-backend-file,share=on``, and that the
  39. ``x-ignore-shared`` capability be set. This combination allows memory
  40. to be saved in place. Otherwise, after QEMU stops the VM, all guest
  41. RAM is copied to the migration URI.
  42. Outgoing:
  43. * Set the migration mode parameter to ``cpr-reboot``.
  44. * Set the ``x-ignore-shared`` capability if desired.
  45. * Issue the ``migrate`` command. It is recommended the URI be a
  46. ``file`` type, but one can use other types such as ``exec``,
  47. provided the command captures all the data from the outgoing side,
  48. and provides all the data to the incoming side.
  49. * Quit when QEMU reaches the postmigrate state.
  50. Incoming:
  51. * Start QEMU with the ``-incoming defer`` option.
  52. * Set the migration mode parameter to ``cpr-reboot``.
  53. * Set the ``x-ignore-shared`` capability if desired.
  54. * Issue the ``migrate-incoming`` command.
  55. * If the VM was running when the outgoing ``migrate`` command was
  56. issued, then QEMU automatically resumes VM execution.
  57. Example 1
  58. ^^^^^^^^^
  59. ::
  60. # qemu-kvm -monitor stdio
  61. -object memory-backend-file,id=ram0,size=4G,mem-path=/dev/dax0.0,align=2M,share=on -m 4G
  62. ...
  63. (qemu) info status
  64. VM status: running
  65. (qemu) migrate_set_parameter mode cpr-reboot
  66. (qemu) migrate_set_capability x-ignore-shared on
  67. (qemu) migrate -d file:vm.state
  68. (qemu) info status
  69. VM status: paused (postmigrate)
  70. (qemu) quit
  71. ### optionally update kernel and reboot
  72. # systemctl kexec
  73. kexec_core: Starting new kernel
  74. ...
  75. # qemu-kvm ... -incoming defer
  76. (qemu) info status
  77. VM status: paused (inmigrate)
  78. (qemu) migrate_set_parameter mode cpr-reboot
  79. (qemu) migrate_set_capability x-ignore-shared on
  80. (qemu) migrate_incoming file:vm.state
  81. (qemu) info status
  82. VM status: running
  83. Example 2: VFIO
  84. ^^^^^^^^^^^^^^^
  85. ::
  86. # qemu-kvm -monitor stdio
  87. -object memory-backend-file,id=ram0,size=4G,mem-path=/dev/dax0.0,align=2M,share=on -m 4G
  88. -device vfio-pci, ...
  89. -chardev socket,id=qga0,path=qga.sock,server=on,wait=off
  90. -device virtserialport,chardev=qga0,name=org.qemu.guest_agent.0
  91. ...
  92. (qemu) info status
  93. VM status: running
  94. # echo '{"execute":"guest-suspend-ram"}' | ncat --send-only -U qga.sock
  95. (qemu) info status
  96. VM status: paused (suspended)
  97. (qemu) migrate_set_parameter mode cpr-reboot
  98. (qemu) migrate_set_capability x-ignore-shared on
  99. (qemu) migrate -d file:vm.state
  100. (qemu) info status
  101. VM status: paused (postmigrate)
  102. (qemu) quit
  103. ### optionally update kernel and reboot
  104. # systemctl kexec
  105. kexec_core: Starting new kernel
  106. ...
  107. # qemu-kvm ... -incoming defer
  108. (qemu) info status
  109. VM status: paused (inmigrate)
  110. (qemu) migrate_set_parameter mode cpr-reboot
  111. (qemu) migrate_set_capability x-ignore-shared on
  112. (qemu) migrate_incoming file:vm.state
  113. (qemu) info status
  114. VM status: paused (suspended)
  115. (qemu) system_wakeup
  116. (qemu) info status
  117. VM status: running
  118. Caveats
  119. ^^^^^^^
  120. cpr-reboot mode may not be used with postcopy, background-snapshot,
  121. or COLO.
  122. cpr-transfer mode
  123. -----------------
  124. This mode allows the user to transfer a guest to a new QEMU instance
  125. on the same host with minimal guest pause time, by preserving guest
  126. RAM in place, albeit with new virtual addresses in new QEMU. Devices
  127. and their pinned memory pages will also be preserved in a future QEMU
  128. release.
  129. The user starts new QEMU on the same host as old QEMU, with command-
  130. line arguments to create the same machine, plus the ``-incoming``
  131. option for the main migration channel, like normal live migration.
  132. In addition, the user adds a second -incoming option with channel
  133. type ``cpr``. This CPR channel must support file descriptor transfer
  134. with SCM_RIGHTS, i.e. it must be a UNIX domain socket.
  135. To initiate CPR, the user issues a migrate command to old QEMU,
  136. adding a second migration channel of type ``cpr`` in the channels
  137. argument. Old QEMU stops the VM, saves state to the migration
  138. channels, and enters the postmigrate state. Execution resumes in
  139. new QEMU.
  140. New QEMU reads the CPR channel before opening a monitor, hence
  141. the CPR channel cannot be specified in the list of channels for a
  142. migrate-incoming command. It may only be specified on the command
  143. line.
  144. Usage
  145. ^^^^^
  146. Memory backend objects must have the ``share=on`` attribute.
  147. The VM must be started with the ``-machine aux-ram-share=on``
  148. option. This causes implicit RAM blocks (those not described by
  149. a memory-backend object) to be allocated by mmap'ing a memfd.
  150. Examples include VGA and ROM.
  151. Outgoing:
  152. * Set the migration mode parameter to ``cpr-transfer``.
  153. * Issue the ``migrate`` command, containing a main channel and
  154. a cpr channel.
  155. Incoming:
  156. * Start new QEMU with two ``-incoming`` options.
  157. * If the VM was running when the outgoing ``migrate`` command was
  158. issued, then QEMU automatically resumes VM execution.
  159. Caveats
  160. ^^^^^^^
  161. cpr-transfer mode may not be used with postcopy, background-snapshot,
  162. or COLO.
  163. memory-backend-epc is not supported.
  164. The main incoming migration channel address cannot be a file type.
  165. If the main incoming channel address is an inet socket, then the port
  166. cannot be 0 (meaning dynamically choose a port).
  167. When using ``-incoming defer``, you must issue the migrate command to
  168. old QEMU before issuing any monitor commands to new QEMU, because new
  169. QEMU blocks waiting to read from the cpr channel before starting its
  170. monitor, and old QEMU does not write to the channel until the migrate
  171. command is issued. However, new QEMU does not open and read the
  172. main migration channel until you issue the migrate incoming command.
  173. Example 1: incoming channel
  174. ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  175. In these examples, we simply restart the same version of QEMU, but
  176. in a real scenario one would start new QEMU on the incoming side.
  177. Note that new QEMU does not print the monitor prompt until old QEMU
  178. has issued the migrate command. The outgoing side uses QMP because
  179. HMP cannot specify a CPR channel. Some QMP responses are omitted for
  180. brevity.
  181. ::
  182. Outgoing: Incoming:
  183. # qemu-kvm -qmp stdio
  184. -object memory-backend-file,id=ram0,size=4G,
  185. mem-path=/dev/shm/ram0,share=on -m 4G
  186. -machine memory-backend=ram0
  187. -machine aux-ram-share=on
  188. ...
  189. # qemu-kvm -monitor stdio
  190. -incoming tcp:0:44444
  191. -incoming '{"channel-type": "cpr",
  192. "addr": { "transport": "socket",
  193. "type": "unix", "path": "cpr.sock"}}'
  194. ...
  195. {"execute":"qmp_capabilities"}
  196. {"execute": "query-status"}
  197. {"return": {"status": "running",
  198. "running": true}}
  199. {"execute":"migrate-set-parameters",
  200. "arguments":{"mode":"cpr-transfer"}}
  201. {"execute": "migrate", "arguments": { "channels": [
  202. {"channel-type": "main",
  203. "addr": { "transport": "socket", "type": "inet",
  204. "host": "0", "port": "44444" }},
  205. {"channel-type": "cpr",
  206. "addr": { "transport": "socket", "type": "unix",
  207. "path": "cpr.sock" }}]}}
  208. QEMU 10.0.50 monitor
  209. (qemu) info status
  210. VM status: running
  211. {"execute": "query-status"}
  212. {"return": {"status": "postmigrate",
  213. "running": false}}
  214. Example 2: incoming defer
  215. ^^^^^^^^^^^^^^^^^^^^^^^^^
  216. This example uses ``-incoming defer`` to hot plug a device before
  217. accepting the main migration channel. Again note you must issue the
  218. migrate command to old QEMU before you can issue any monitor
  219. commands to new QEMU.
  220. ::
  221. Outgoing: Incoming:
  222. # qemu-kvm -monitor stdio
  223. -object memory-backend-file,id=ram0,size=4G,
  224. mem-path=/dev/shm/ram0,share=on -m 4G
  225. -machine memory-backend=ram0
  226. -machine aux-ram-share=on
  227. ...
  228. # qemu-kvm -monitor stdio
  229. -incoming defer
  230. -incoming '{"channel-type": "cpr",
  231. "addr": { "transport": "socket",
  232. "type": "unix", "path": "cpr.sock"}}'
  233. ...
  234. {"execute":"qmp_capabilities"}
  235. {"execute": "device_add",
  236. "arguments": {"driver": "pcie-root-port"}}
  237. {"execute":"migrate-set-parameters",
  238. "arguments":{"mode":"cpr-transfer"}}
  239. {"execute": "migrate", "arguments": { "channels": [
  240. {"channel-type": "main",
  241. "addr": { "transport": "socket", "type": "inet",
  242. "host": "0", "port": "44444" }},
  243. {"channel-type": "cpr",
  244. "addr": { "transport": "socket", "type": "unix",
  245. "path": "cpr.sock" }}]}}
  246. QEMU 10.0.50 monitor
  247. (qemu) info status
  248. VM status: paused (inmigrate)
  249. (qemu) device_add pcie-root-port
  250. (qemu) migrate_incoming tcp:0:44444
  251. (qemu) info status
  252. VM status: running
  253. {"execute": "query-status"}
  254. {"return": {"status": "postmigrate",
  255. "running": false}}
  256. Futures
  257. ^^^^^^^
  258. cpr-transfer mode is based on a capability to transfer open file
  259. descriptors from old to new QEMU. In the future, descriptors for
  260. vfio, iommufd, vhost, and char devices could be transferred,
  261. preserving those devices and their kernel state without interruption,
  262. even if they do not explicitly support live migration.