xref: /cloud-hypervisor/docs/live_migration.md (revision b9163bf4317cca827c17584080aac35ba0438ca1)
18a22b298SBo Chen# Live Migration
28a22b298SBo Chen
3*b9163bf4SJinrong LiangThis document gives examples of how to use the live migration support
4*b9163bf4SJinrong Liangin Cloud Hypervisor:
58a22b298SBo Chen
68c9b34c9SJulian Stecklina1. local migration - migrating a VM from one Cloud Hypervisor instance to another on the same machine;
78c9b34c9SJulian Stecklina1. remote migration - migrating a VM between two machines;
88c9b34c9SJulian Stecklina
98c9b34c9SJulian Stecklina> :warning: These examples place sockets /tmp. This is done for
108c9b34c9SJulian Stecklina> simplicity and should not be done in production.
118a22b298SBo Chen
12a61302f7SRob Bradford## Local Migration (Suitable for Live Upgrade of VMM)
135b822191SJulian Stecklina
148a22b298SBo ChenLaunch the source VM (on the host machine):
155b822191SJulian Stecklina
165b822191SJulian Stecklina```console
178a22b298SBo Chen$ target/release/cloud-hypervisor
188a22b298SBo Chen    --kernel ~/workloads/vmlinux \
198a22b298SBo Chen    --disk path=~/workloads/focal.raw \
20a61302f7SRob Bradford    --cpus boot=1 --memory size=1G,shared=on \
218a22b298SBo Chen    --cmdline "root=/dev/vda1 console=ttyS0"  \
22fa22cb0bSRavi kumar Veeramally    --serial tty --console off --api-socket=/tmp/api1
238a22b298SBo Chen```
248a22b298SBo Chen
258a22b298SBo ChenLaunch the destination VM from the same directory (on the host machine):
265b822191SJulian Stecklina
275b822191SJulian Stecklina```console
28fa22cb0bSRavi kumar Veeramally$ target/release/cloud-hypervisor --api-socket=/tmp/api2
298a22b298SBo Chen```
308a22b298SBo Chen
318a22b298SBo ChenGet ready for receiving migration for the destination VM (on the host machine):
325b822191SJulian Stecklina
335b822191SJulian Stecklina```console
34fa22cb0bSRavi kumar Veeramally$ target/release/ch-remote --api-socket=/tmp/api2 receive-migration unix:/tmp/sock
358a22b298SBo Chen```
368a22b298SBo Chen
378a22b298SBo ChenStart to send migration for the source VM (on the host machine):
385b822191SJulian Stecklina
395b822191SJulian Stecklina```console
40fa22cb0bSRavi kumar Veeramally$ target/release/ch-remote --api-socket=/tmp/api1 send-migration --local unix:/tmp/sock
418a22b298SBo Chen```
428a22b298SBo Chen
438a22b298SBo ChenWhen the above commands completed, the source VM should be successfully
448a22b298SBo Chenmigrated to the destination VM. Now the destination VM is running while
45001ee679SBo Chenthe source VM is terminated gracefully.
468a22b298SBo Chen
478c9b34c9SJulian Stecklina## Remote Migration
488a22b298SBo Chen
498c9b34c9SJulian StecklinaIn this example, we will migrate a VM from one machine (`src`) to
508c9b34c9SJulian Stecklinaanother (`dst`) across the network. To keep it simple, we will use a
518c9b34c9SJulian Stecklinaminimal VM setup without storage.
525b822191SJulian Stecklina
538c9b34c9SJulian Stecklina### Preparation
548c9b34c9SJulian Stecklina
558c9b34c9SJulian StecklinaMake sure that `src` and `dst` can reach each other via the
568c9b34c9SJulian Stecklinanetwork. You should be able to ping each machine. Also each machine
57*b9163bf4SJinrong Liangshould have an open TCP port.
588c9b34c9SJulian Stecklina
598c9b34c9SJulian StecklinaYou will need a kernel and initramfs for a minimal Linux system. For
608c9b34c9SJulian Stecklinathis example, we will use the Debian netboot image.
618c9b34c9SJulian Stecklina
628c9b34c9SJulian StecklinaPlace the kernel and initramfs into the _same directory_ on both
638c9b34c9SJulian Stecklinamachines. This is important for the migration to succeed. We will use
648c9b34c9SJulian Stecklina`/var/images`:
655b822191SJulian Stecklina
665b822191SJulian Stecklina```console
678c9b34c9SJulian Stecklinasrc $ export DEBIAN=https://ftp.debian.org/debian/dists/stable/main/installer-amd64/current/images/netboot/debian-installer/amd64
688c9b34c9SJulian Stecklinasrc $ mkdir -p /var/images
698c9b34c9SJulian Stecklinasrc $ curl $DEBIAN/linux > /var/images/linux
708c9b34c9SJulian Stecklinasrc $ curl $DEBIAN/initrd.gz > /var/images/initrd
718c9b34c9SJulian Stecklina```
728c9b34c9SJulian Stecklina
738c9b34c9SJulian StecklinaRepeat the above steps on the destination host.
748c9b34c9SJulian Stecklina
75*b9163bf4SJinrong Liang### Unix Socket Migration
76*b9163bf4SJinrong Liang
77*b9163bf4SJinrong LiangIf Unix socket is selected for migration, we can tunnel traffic through "socat".
78*b9163bf4SJinrong Liang
79*b9163bf4SJinrong Liang#### Starting the Receiver VM
808c9b34c9SJulian Stecklina
818c9b34c9SJulian StecklinaOn the receiver side, we prepare an empty VM:
828c9b34c9SJulian Stecklina
838c9b34c9SJulian Stecklina```console
848c9b34c9SJulian Stecklinadst $ cloud-hypervisor --api-socket /tmp/api
858c9b34c9SJulian Stecklina```
868c9b34c9SJulian Stecklina
878c9b34c9SJulian StecklinaIn a different terminal, configure the VM as a migration target:
888c9b34c9SJulian Stecklina
898c9b34c9SJulian Stecklina```console
908c9b34c9SJulian Stecklinadst $ ch-remote --api-socket=/tmp/api receive-migration unix:/tmp/sock
918c9b34c9SJulian Stecklina```
928c9b34c9SJulian Stecklina
938c9b34c9SJulian StecklinaIn yet another terminal, forward TCP connections to the Unix domain socket:
948c9b34c9SJulian Stecklina
958c9b34c9SJulian Stecklina```console
96*b9163bf4SJinrong Liangdst $ socat TCP-LISTEN:{port},reuseaddr UNIX-CLIENT:/tmp/sock
978c9b34c9SJulian Stecklina```
988c9b34c9SJulian Stecklina
99*b9163bf4SJinrong Liang#### Starting the Sender VM
1008c9b34c9SJulian Stecklina
1018c9b34c9SJulian StecklinaLet's start the VM on the source machine:
1028c9b34c9SJulian Stecklina
1038c9b34c9SJulian Stecklina```console
1048c9b34c9SJulian Stecklinasrc $ cloud-hypervisor \
1058a22b298SBo Chen        --serial tty --console off \
1068c9b34c9SJulian Stecklina        --cpus boot=2 --memory size=4G \
1078c9b34c9SJulian Stecklina        --kernel /var/images/linux \
1088c9b34c9SJulian Stecklina        --initramfs /var/images/initrd \
1098c9b34c9SJulian Stecklina        --cmdline "console=ttyS0" \
1108c9b34c9SJulian Stecklina        --api-socket /tmp/api
1118a22b298SBo Chen```
1128a22b298SBo Chen
1138c9b34c9SJulian StecklinaAfter a few seconds the VM should be up and you can interact with it.
1148c9b34c9SJulian Stecklina
115*b9163bf4SJinrong Liang#### Performing the Migration
1168c9b34c9SJulian Stecklina
1178c9b34c9SJulian StecklinaFirst, we start `socat`:
1185b822191SJulian Stecklina
1195b822191SJulian Stecklina```console
120*b9163bf4SJinrong Liangsrc $ socat UNIX-LISTEN:/tmp/sock,reuseaddr TCP:{dst}:{port}
1218a22b298SBo Chen```
1228a22b298SBo Chen
123*b9163bf4SJinrong Liang> Replace {dst}:{port} with the actual IP address and port of your destination host.
124*b9163bf4SJinrong Liang
1258c9b34c9SJulian StecklinaThen we kick-off the migration itself:
1265b822191SJulian Stecklina
1275b822191SJulian Stecklina```console
1288c9b34c9SJulian Stecklinasrc $ ch-remote --api-socket=/tmp/api send-migration unix:/tmp/sock
1298a22b298SBo Chen```
1305b822191SJulian Stecklina
1318c9b34c9SJulian StecklinaWhen the above commands completed, the VM should be successfully
1328c9b34c9SJulian Stecklinamigrated to the destination machine without interrupting the workload.
133*b9163bf4SJinrong Liang
134*b9163bf4SJinrong Liang### TCP Socket Migration
135*b9163bf4SJinrong Liang
136*b9163bf4SJinrong LiangIf TCP socket is selected for migration, we need to consider migrating
137*b9163bf4SJinrong Liangin a trusted network.
138*b9163bf4SJinrong Liang
139*b9163bf4SJinrong Liang#### Starting the Receiver VM
140*b9163bf4SJinrong Liang
141*b9163bf4SJinrong LiangOn the receiver side, we prepare an empty VM:
142*b9163bf4SJinrong Liang
143*b9163bf4SJinrong Liang```console
144*b9163bf4SJinrong Liangdst $ cloud-hypervisor --api-socket /tmp/api
145*b9163bf4SJinrong Liang```
146*b9163bf4SJinrong Liang
147*b9163bf4SJinrong LiangIn a different terminal, prepare to receive the migration:
148*b9163bf4SJinrong Liang
149*b9163bf4SJinrong Liang```console
150*b9163bf4SJinrong Liangdst $ ch-remote --api-socket=/tmp/api receive-migration tcp:0.0.0.0:{port}
151*b9163bf4SJinrong Liang```
152*b9163bf4SJinrong Liang
153*b9163bf4SJinrong Liang#### Starting the Sender VM
154*b9163bf4SJinrong Liang
155*b9163bf4SJinrong LiangLet's start the VM on the source machine:
156*b9163bf4SJinrong Liang
157*b9163bf4SJinrong Liang```console
158*b9163bf4SJinrong Liangsrc $ cloud-hypervisor \
159*b9163bf4SJinrong Liang        --serial tty --console off \
160*b9163bf4SJinrong Liang        --cpus boot=2 --memory size=4G \
161*b9163bf4SJinrong Liang        --kernel /var/images/linux \
162*b9163bf4SJinrong Liang        --initramfs /var/images/initrd \
163*b9163bf4SJinrong Liang        --cmdline "console=ttyS0" \
164*b9163bf4SJinrong Liang        --api-socket /tmp/api
165*b9163bf4SJinrong Liang```
166*b9163bf4SJinrong Liang
167*b9163bf4SJinrong LiangAfter a few seconds the VM should be up and you can interact with it.
168*b9163bf4SJinrong Liang
169*b9163bf4SJinrong Liang#### Performing the Migration
170*b9163bf4SJinrong Liang
171*b9163bf4SJinrong LiangInitiate the Migration over TCP:
172*b9163bf4SJinrong Liang
173*b9163bf4SJinrong Liang```console
174*b9163bf4SJinrong Liangsrc $ ch-remote --api-socket=/tmp/api send-migration  tcp:{dst}:{port}
175*b9163bf4SJinrong Liang```
176*b9163bf4SJinrong Liang
177*b9163bf4SJinrong Liang> Replace {dst}:{port} with the actual IP address and port of your destination host.
178*b9163bf4SJinrong Liang
179*b9163bf4SJinrong LiangAfter completing the above commands, the source VM will be migrated to
180*b9163bf4SJinrong Liangthe destination host and continue running there. The source VM instance
181*b9163bf4SJinrong Liangwill terminate normally. All ongoing processes and connections within
182*b9163bf4SJinrong Liangthe VM should remain intact after the migration.
183