KVM虚拟机迁移

来自CloudWiki
跳转至: 导航搜索

新建一台KVM虚拟机

wget http://download.cirros-cloud.net/0.3.4/cirros-0.3.4-i386-disk.img

virt-install -n "mx01" -r 512 --vcpus=2 --disk path=/var/lib/libvirt/images/cirros-0.3.4-i386-disk.img --import

KVM虚拟机静态迁移

确定虚拟机关闭状态

[root@kvm-max ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 -     demo3                           shut off

[root@victory ~]# virsh shutdown demo
Domain demo is being shutdown


迁移主机

准备迁移demo3虚拟机,查看该虚拟机配置的磁盘文件

[root@victory images]# virsh domblklist demo3
Target     Source
------------------------------------------------
hda        /var/lib/libvirt/images/cirros-0.3.3-x86_64-disk.img


导入虚拟机配置文件

[root@victory ~]# virsh dumpxml demo3 > /opt/demo3.xml
[root@victory ~]# ll /opt/
total 8
-rw-r--r--  1 root root 2786 Feb 26 03:39 demo3.xml

复制配置文件到目标虚拟主机上

[root@victory ~]# scp /opt/demo3.xml 10.0.0.49:/etc/libvirt/qemu/
The authenticity of host '10.0.0.49 (10.0.0.49)' can't be established.
RSA key fingerprint is f2:88:1c:ec:52:c8:0a:aa:b8:f8:3a:76:2e:b9:6f:4f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.49' (RSA) to the list of known hosts.
root@10.0.0.49's password:
demo3.xml                                     100% 2786     2.7KB/s   00:00


查看虚拟机磁盘文件并拷贝到目标虚拟主机

[root@victory ~]# cd /var/lib/libvirt/images/
[root@victory images]# ll
total 10598988
-rw-r--r-- 1 root root   14221312 Feb 26 04:40 cirros-0.3.3-x86_64-disk.img


拷贝虚拟磁盘文件:

[root@victory images]# scp cirros-0.3.3-x86_64-disk.img 10.0.0.49:/var/lib/libvirt/images/
root@10.0.0.49's password:
cirros-0.3.3-x86_64-disk.img                  100%   14MB   6.8MB/s   00:02




目标虚拟主机配置

查看目标虚拟主机环境

[root@maxin ~]# virsh list --all
 Id    Name                           State
----------------------------------------------------

[root@maxin ~]# ll /etc/libvirt/qemu
total 12
-rw-r--r-- 1 root root 2786 Mar 11 21:18 demo3.xml
drwx------ 3 root root 4096 Mar  4 15:02 networks


查看虚拟机磁盘文件,目录结构与源虚拟主机一致。

[root@maxin ~]# cd /var/lib/libvirt/images/
[root@maxin images]# ll
total 10599128
-rw-r--r-- 1 root root   14221312 Mar 11 22:07 cirros-0.3.3-x86_64-disk.img



定义注册虚拟主机

[root@maxin images]# virsh define /etc/libvirt/qemu/demo3.xml
Domain demo3 defined from /etc/libvirt/qemu/demo3.xml


启动虚拟主机并确认

[root@maxin images]# virsh list --all
 Id    Name                           State
----------------------------------------------------
 2     demo                           running
 -     demo3                          shut off


[root@maxin images]#  virsh start demo3
Domain demo3 started


[root@maxin images]# virsh console demo3
Connected to domain demo3
Escape character is ^]

login as 'cirros' user. default password: 'cubswin:)'. use 'sudo' for root.
cirros login: cirros
Password:
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 16436 qdisc noqueue
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
    inet6 ::1/128 scope host
       valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast qlen 1000
    link/ether 52:54:00:4d:52:e9 brd ff:ff:ff:ff:ff:ff
    inet 192.168.122.172/24 brd 192.168.122.255 scope global eth0
    inet6 fe80::5054:ff:fe4d:52e9/64 scope link
       valid_lft forever preferred_lft forever

开启Console功能

新安装一台虚拟机后,是无法通过virsh console 命令连入虚拟机中的,这时需要开启虚拟机的console功能。添加ttyS0的许可,允许root登陆

[root@ kvm-2 ~]# echo "ttyS0" >> /etc/securetty