通过模板虚拟机克隆新虚拟机
IP规划 宿主机:172.16.30.30
模板虚机:172.16.30.31
克隆虚机:172.16.30.32
1.关闭或者暂停模板虚拟机,再进行克隆安装新虚拟机
#virsh list --all
Id Name State
----------------------------------------------------
2 WMS-test-1 running
#virsh shutdown WMS-test-1
Domain WMS-test-1 is being shutdown
#virsh list --all
Id Name State
----------------------------------------------------
2 WMS-test-1 running
- 注:shutdown命令不生效,虚机中需安装acpid
yum -y install acpid
宿主机安装acpid后执行命令
virsh # list --all
Id Name State
----------------------------------------------------
2 WMS-test-1 running
virsh # shutdown WMS-test-1
Domain WMS-test-1 is being shutdown
virsh # list --all
Id Name State
----------------------------------------------------
- WMS-test-1 shut off
- 通过virt-clone命令克隆虚拟机
virt-clone -o WMS-test-1 -n WMS-test-2 -f /data1/kvmdisk/WMS-test-2.img
Cloning WMS-test-1.img | 50 GB 04:30
Clone 'WMS-test-2' created successfully.
-o 模板主机名称
-n 克隆主机名称
-f 镜像路径
3.手动更改xml配置文件中VNC端口,然后重启libvirtd服务
[root@mrxy-py kvmdisk]# /etc/init.d/libvirtd restart
Stopping libvirtd daemon: [ OK ]
Starting libvirtd daemon: [ OK ]
[root@mrxy-py kvmdisk]# more /etc/libvirt/qemu/WMS-test-2.xml |grep vnc
<graphics type='vnc' port='7789' autoport='no' listen='0.0.0.0'>
4.启动新克隆成功的虚拟机
[root@mrxy-py kvmdisk]# virsh start WMS-test-2
Domain WMS-test-2 started
[root@mrxy-py kvmdisk]# virsh start WMS-test-1
error: Domain is already active
[root@mrxy-py kvmdisk]# virsh list --all
Id Name State
----------------------------------------------------
1 WMS-test-1 running
2 WMS-test-2 running
5.通过VNC客户端链接,配置eth0网卡
#更改MAC地址和UUID(可以随意更改,但是格式一致)
[root@wms-test-2 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
MACADDR=52:54:00:B7:09:87
TYPE=Ethernet
UUID=2858167d-0b69-48c7-a810-1baee86b1d11
ONBOOT=yes
NM_CONTROLLED=yes
BOOTPROTO=static
IPADDR=172.16.30.32
NETMASK=255.255.254.0
GATEWAY=172.16.30.1
#删除MAC绑定
[root@wms-test-2 ~]# rm -f /etc/udev/rules.d/70-persistent-net.rules
[root@wms-test-2 ~]# reboot
通过拷贝xml文件方式安装新虚拟机
- 制作虚拟机镜像
[root@mrxy-py kvmdisk]# cp WMS-test-2.img WMS-test-3.img
[root@mrxy-py kvmdisk]# cd /etc/libvirt/qemu
[root@mrxy-py qemu]# ls
autostart networks WMS-test-1.xml WMS-test-2.xml
#创建xml文件
[root@mrxy-py qemu]# cp WMS-test-2.xml WMS-test-3.xml
- 修改相应的参数 uuid mac vnc 硬盘文件位置 虚拟机的主机名
uuid和mac:可以随意更改,但是格式一致
vnc:指定端口,不能跟其它虚拟机冲突
[root@mrxy-py qemu]# pwd
/etc/libvirt/qemu
[root@mrxy-py qemu]# vim WMS-test-3.xml
<mac address='52:54:00:d0:5a:30'/>
<uuid>85bbf66d-800b-3ddd-7d98-2181662a4164</uuid>
<source file='/data1/kvmdisk/WMS-test-3.img'/>
<graphics type='vnc' port='7790' autoport='no' listen='0.0.0.0'>
- 注册虚拟机,并启动
[root@mrxy-py qemu]# virsh define WMS-test-3.xml
Domain WMS-test-3 defined from WMS-test-3.xml
[root@mrxy-py qemu]# virsh list --all
Id Name State
----------------------------------------------------
1 WMS-test-1 running
2 WMS-test-2 running
- WMS-test-3 shut off
[root@mrxy-py qemu]# virsh start WMS-test-3
Domain WMS-test-3 started
[root@mrxy-py qemu]# virsh list --all
Id Name State
----------------------------------------------------
1 WMS-test-1 running
2 WMS-test-2 running
3 WMS-test-3 running