一、使用 virt-clone 命令直接克隆
1. 查看虛擬機
[root@localhost ~]# virsh list --all
Id 名稱 狀態
----------------------------------------------------
1 centos7.0-kvm1 暫停
2. 查看 centos7.0-kvm1 虛擬機的配置文件
[root@localhost ~]# ll /etc/libvirt/qemu
總用量 8
drwxr-xr-x. 2 root root 26 6月 14 20:26 autostart
-rw-------. 1 root root 4775 6月 13 17:41 centos7.0-kvm1
drwx------. 3 root root 42 8月 5 2017 networks
3. 查看 centos7.0-kvm1 虛擬機磁盤文件
[root@localhost ~]# ls /home/images
villian.qcow2
4. 開始克隆 kvm 虛擬機
[root@localhost ~]# virt-clone -o centos7.0-kvm1 -n centos7.0-kvm2 -f /home/images/villian2.qcow2
正在分配 'villian2.qcow2' | 100 GB 00:00:20
成功克隆 'centos7.0-kvm2'。
選項:
-o:原虛擬機名
-n:新生成的虛擬機名
-f:新生成的虛擬機的磁盤文件的路徑
5. 查看有沒有生成 centos7.0-kvm2 的配置文件
[root@localhost ~]# ll /etc/libvirt/qemu
總用量 16
drwxr-xr-x. 2 root root 26 6月 14 20:26 autostart
-rw-------. 1 root root 4775 6月 13 17:41 centos7.0-kvm1.xml
-rw-------. 1 root root 4775 6月 15 07:32 centos7.0-kvm2.xml
drwx------. 3 root root 42 8月 5 2017 networks
6. 查看有沒有生成 centos7.0-kvm2 的磁盤文件
[root@localhost ~]# ls /home/images/
villian1.qcow2 villian2.qcow2
7.查看新克隆虛擬機
[root@localhost ~]# virsh list --all
Id 名稱 狀態
----------------------------------------------------
1 centos7.0-kvm1 暫停
2 centos7.0-kvm2 暫停
8. 啓動虛擬機
[root@localhost ~]# virsh start centos7.0-kvm2
[root@localhost ~]# virsh list --all
Id 名稱 狀態
----------------------------------------------------
- centos7.0-kvm1 關閉
- centos7.0-kvm2 running
二、通過複製磁盤文件和配置文件克隆
1.複製 centos7.0-kvm1 的磁盤文件
[root@localhost images]# ll
-rw-------. 1 root root 140019892224 6月 15 08:02 villian.qcow2
[root@openstack images]# cp villian1.qcow2 villian2.qcow2 -a
[root@localhost images]# ll
-rw-------. 1 qemu qemu 140019892224 6月 15 08:43 villian1.qcow2
-rw-------. 1 qemu qemu 2097217536 6月 15 08:43 villian2.qcow2
2.複製 centos7.0-kvm1 的配置文件
[root@localhost ~]# ll /etc/libvirt/qemu
drwxr-xr-x. 2 root root 26 6月 14 20:26 autostart
-rw-------. 1 root root 4775 6月 13 17:41 villian1.xml
drwx------. 3 root root 42 8月 5 2017 networks
[root@localhost ~]# virsh dumpxml villian1 > /etc/libvirt/qemu/villian2.xml
[root@localhost ~]# ll /etc/libvirt/qemu
總用量 24
drwxr-xr-x. 2 root root 26 6月 14 20:26 autostart
-rw-------. 1 root root 4775 6月 13 17:41 villian1.xml
-rw-------. 1 root root 4775 6月 15 07:32 villian2.xml
drwx------. 3 root root 42 8月 5 2017 networks
PS:因爲是直接複製 centos7.0-kvm1 的配置文件,所以需要修改該配置文件
修改name,uuid,disk,mac,vnc端口
[root@openstack images]# cd /etc/libvirt/qemu
[root@openstack qemu]# vim compute3.xml
<domain type='kvm'>
<name>compute3</name>
<uuid>70f56803-f813-4d34-861a-996ef0ab74b5</uuid>
<memory unit='KiB'>10485760</memory>
<currentMemory unit='KiB'>10485760</currentMemory>
<vcpu placement='static'>2</vcpu>
<resource>
<partition>/machine</partition>
</resource>
<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='full'>
<model fallback='forbid'>Nehalem</model>
<feature policy='require' name='x2apic'/>
<feature policy='require' name='hypervisor'/>
</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='/home/images/compute3.qcow2'/>
<target dev='vda' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x07' 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='0x05' function='0x7'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci1'>
<master startport='0'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0' multifunction='on'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci2'>
<master startport='2'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x1'/>
</controller>
<controller type='usb' index='0' model='ich9-uhci3'>
<master startport='4'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x2'/>
</controller>
<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='0x06' function='0x0'/>
</controller>
<controller type='pci' index='0' model='pci-root'/>
<interface type='network'>
<mac address='52:54:00:c3:4e:ec'/>
<source network='default'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/>
</interface>
<interface type='bridge'>
<mac address='52:54:00:d8:c6:00'/>
<source bridge='br0'/>
<model type='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x09' function='0x0'/>
</interface>
<serial type='pty'>
<target port='0'/>
</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>
<channel type='spicevmc'>
<target type='virtio' name='com.redhat.spice.0'/>
<address type='virtio-serial' controller='0' bus='0' port='2'/>
</channel>
<input type='tablet' bus='usb'>
<address type='usb' bus='0' port='1'/>
</input>
<input type='mouse' bus='ps2'/>
<input type='keyboard' bus='ps2'/>
<graphics type='vnc' port='-1' autoport='yes'>
<listen type='address'/>
</graphics>
<sound model='ich6'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x04' function='0x0'/>
</sound>
<video>
<model type='qxl' ram='65536' vram='65536' vgamem='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='2'/>
</redirdev>
<redirdev bus='usb' type='spicevmc'>
<address type='usb' bus='0' port='3'/>
</redirdev>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/>
</memballoon>
</devices>
<seclabel type='dynamic' model='selinux' relabel='yes'/>
</domain>
3、定義配置文件,否則克隆失敗
[root@localhost qemu]# virsh define villian2.xml
定義域 villian2(從 villian2.xml)
4.查看新克隆虛擬機
[root@localhost ~]# virsh list --all
Id 名稱 狀態
----------------------------------------------------
1 centos7.0-kvm1 暫停
2 centos7.0-kvm2 暫停