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