克隆虚拟机
KVM虚拟机的克隆分为两种
直接克隆
查看虚拟机状态
1 2 3 4 5
| [root@chobon networks] Id 名称 状态 ---------------------------------------------------- 1 vm1_centos7 running 3 vm2_centos7 running
|
关闭虚拟机vm2_centos7
1 2
| [root@chobon networks] 域 vm2_centos7 被关闭
|
执行克隆指令,以vm2_cnetos7
为源,克隆新虚拟机名称vm3_centos7
,新虚拟机磁盘文件存储地址/mnt/kvm/vfs/vm3.qcow2
1 2 3 4
| [root@chobon networks] 正在分配 'vm3.qcow2' | 40 GB 00:00:00
成功克隆 'vm3_centos7'。
|
复制克隆
第一步还是关闭虚拟机
virsh shutdown vm1_centos7
查看需要复制的虚拟机配置,这里复制可以通过任意方法
虚拟机配置文件存储在 /etc/libvirt/qemu
文件夹里
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| <domain type='kvm'> <name>vm1_centos7</name> <uuid>19854718-3319-4a9a-8e44-23c1e884af08</uuid> <memory unit='KiB'>2097152</memory> <currentMemory unit='KiB'>2097152</currentMemory> <vcpu placement='static'>2</vcpu> <os> <type arch='x86_64' machine='pc-i440fx-rhel7.0.0'>hvm</type> <boot dev='hd'/> </os> <features> <acpi/> <apic/> </features> <cpu mode='custom' match='exact' check='partial'> <model fallback='allow'>Westmere-IBRS</model> <feature policy='require' name='md-clear'/> <feature policy='require' name='spec-ctrl'/> <feature policy='require' name='ssbd'/> </cpu> <clock offset='utc'> <timer name='rtc' tickpolicy='catchup'/> <timer name='pit' tickpolicy='delay'/> <timer name='hpet' present='no'/> </clock> <on_poweroff>destroy</on_poweroff> <on_reboot>restart</on_reboot> <on_crash>destroy</on_crash> <pm> <suspend-to-mem enabled='no'/> <suspend-to-disk enabled='no'/> </pm> <devices> <emulator>/usr/libexec/qemu-kvm</emulator> <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/kvm/vfs/vm1.qcow2'/> <target dev='vda' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/> </disk> <disk type='file' device='cdrom'> <driver name='qemu' type='raw'/> <target dev='hda' bus='ide'/> <readonly/> <address type='drive' controller='0' bus='0' target='0' unit='0'/> </disk> <controller type='usb' index='0' model='ich9-ehci1'> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x7'/> </controller> <controller type='usb' index='0' model='ich9-uhci1'> <master startport='0'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0' multifunction='on'/> </controller> <controller type='usb' index='0' model='ich9-uhci2'> <master startport='2'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x1'/> </controller> <controller type='usb' index='0' model='ich9-uhci3'> <master startport='4'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x2'/> </controller> <controller type='pci' index='0' model='pci-root'/> <controller type='ide' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x01' function='0x1'/> </controller> <controller type='virtio-serial' index='0'> <address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/> </controller> <interface type='network'> <mac address='52:54:00:39:c5:c0'/> <source network='default'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <serial type='pty'> <target type='isa-serial' port='0'> <model name='isa-serial'/> </target> </serial> <console type='pty'> <target type='serial' port='0'/> </console> <channel type='unix'> <target type='virtio' name='org.qemu.guest_agent.0'/> <address type='virtio-serial' controller='0' bus='0' port='1'/> </channel> <input type='mouse' bus='ps2'/> <input type='keyboard' bus='ps2'/> <memballoon model='virtio'> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </memballoon> <rng model='virtio'> <backend model='random'>/dev/urandom</backend> <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/> </rng> </devices> </domain>
|
也可以直接用命令virsh dumpxml vm1_centos7 > /etc/libvirt/qemu/vm3_centos7.xml
进到磁盘文件夹 /kvm/vfs
修改虚拟磁盘位置 <source file='/kvm/vfs/vm3.qcow2'/>
修改虚拟机uuid <uuid>19854718-3319-4a9a-8e44-23c1e884af08</uuid>
修改虚拟机名字 <name>vm3_centos7</name>
修改虚拟机mac地址 <mac address='52:54:00:39:60:ce'/>
1 2
| [root@chobon ~] 定义域 vm4_centos7(从 /etc/libvirt/qemu/vm3_centos7.xml)
|
配置新虚拟网络
详情可以参考《CentOS学习KVM笔记-网络篇》
查看虚拟机
1 2 3 4 5 6
| [root@chobon networks] Id 名称 状态 ---------------------------------------------------- 1 vm1_centos7 running - vm2_centos7 关闭 - vm3_centos7 关闭
|
查看虚拟机mac地址
1 2
| [root@chobon ~] <mac address='52:54:00:39:60:ce'/>
|
配置vm3_centos7
的dhcp网络
绑定虚拟机mac和ip
1
| <host mac='52:54:00:39:60:ce' name='vm3' ip='192.168.122.103'/>
|
启动虚拟机
1 2
| [root@chobon networks] 域 vm3_centos7 已开始
|
连接上虚拟机把网络设置为开机启动ONBOOT=yes
,再重启虚拟机内部通信网络就配置好了,再可以通过端口映射到宿主机。
常用命令
1
| virsh create /etc/libvirt/qemu/linux-xx.xml
|
1
| virsh autostart linux-xx
|
1
| virsh auto start --disable linux-xx
|
- 导出KVM虚拟机配置文件,KVM虚拟机配置文件可以通过这种方式进行备份。
1
| virsh dumpxml linux-xx1 > /etc/libvirt/qemu/linux-xx2.xml
|
1
| virt-clone -o linux-xx1 -n linux-xx2 -f /linux-xx.qcow2
|
相关参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| autostart connect console create start destroy define domid domuuid dominfo domstate domblkstat domifstat dumpxml edit list migrate quit reboot resume save dump shutdown setmem setmaxmem suspend vcpuinfo version
|