04-KVM虚拟机克隆和网络

    科技2022-07-16  93

    一、虚拟机完整克隆

    [root@kvm opt]# virt-clone -o web01 -n web02 --auto-clone #克隆虚拟机web01为web02 ERROR Domain with devices to clone must be paused or shutoff. #克隆需要先关闭虚拟机 [root@kvm opt]# virsh destroy web01 Domain web01 destroyed [root@kvm opt]# virt-clone -o web01 -n web02 --auto-clone #再次进行克隆虚拟机web01 Allocating 'web02.qcow2' | 10 GB 00:00:02 Clone 'web02' created successfully. [root@kvm opt]# virsh list --all #查看已经克隆的虚拟机 Id Name State ---------------------------------------------------- 7 centos7 running - web01 shut off - web02 shut off

    二、虚拟机链接克隆

    [root@kvm opt]# qemu-img create -f qcow2 -b web02.qcow2 web03.qcow2 #创建链接克隆 Formatting 'web03.qcow2', fmt=qcow2 size=10737418240 backing_file='web02.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off [root@kvm opt]# ll -h total 8.3G -rw------- 1 root root 1.3G Oct 4 18:14 web02.qcow2 -rw-r--r-- 1 root root 193K Oct 4 19:59 web03.qcow2 [root@kvm opt]# qemu-img info web03.qcow2 #查看已克隆的web03信息 image: web03.qcow2 file format: qcow2 virtual size: 10G (10737418240 bytes) disk size: 196K cluster_size: 65536 backing file: web02.qcow2 Format specific information: compat: 1.1 lazy refcounts: false [root@kvm opt]# virsh dumpxml web02 >web03.xml #生成虚拟机配置文件 [root@kvm opt]# vim web03.xml #编辑配置文件 <name>web03</name> #将web02改为web03 <uuid>16a6812d-1992-4389-93e7-c74397ac8a4c</uuid> #uuid删除掉,执行dd <source file='/opt/web03.qcow2'/> #磁盘路径修改,web02改为web03 <mac address='52:54:00:0b:34:e7'/> #将mac地址删除掉,执行dd [root@kvm opt]# virsh define /opt/web03.xml #导入web03配置文件 [root@kvm opt]# ls /etc/libvirt/qemu #查看配置文件是否导入成功 [root@kvm opt]# virsh start web03 #启动web03 Domain web03 started 链接克隆虚拟机磁盘;生成新的虚拟机配置文件;编辑配置文件(修改磁盘名字、删除uuid、磁盘路径修改、删除mac地址)

    三、全自动链接克隆脚本

    [root@kvm opt]# vim auto_clone.sh #!/bin/bash old_vm=$1 new_vm=$2 new_xml="/tmp/${new_vm}.xml" virsh dumpxml $old_vm >$new_xml old_disk=`grep qcow2 $new_xml|awk -F "'" '/source file/{print $2}'` tmp_dir=`dirname $old_disk` new_disk=${tmp_dir}/${new_vm}.qcow2 #1.创建基于链接克隆的虚拟磁盘文件 qemu-img create -f qcow2 -b $old_disk $new_disk #2.修改xml配置文件 sed -i '/uuid/d' $new_xml sed -i '/mac address/d' $new_xml sed -i '2s#'$old_vm'#'$new_vm'#' $new_xml sed -i 's#'$old_disk'#'$new_disk'#g' $new_xml #3.导入要被克隆的虚拟机 virsh define $new_xml #4.测试启动 virsh start $new_vm [root@kvm opt]# sh -x auto_clone.sh web01 web04 #创建新的链接克隆web04 + old_vm=web01 + new_vm=web04 + new_xml=/tmp/web04.xml + virsh dumpxml web01 ++ grep qcow2 /tmp/web04.xml ++ awk -F ''\''' '/source file/{print $2}' + old_disk=/opt/web01.qcow2 ++ dirname /opt/web01.qcow2 + tmp_dir=/opt + new_disk=/opt/web04.qcow2 + qemu-img create -f qcow2 -b /opt/web01.qcow2 /opt/web04.qcow2 Formatting '/opt/web04.qcow2', fmt=qcow2 size=10737418240 backing_file='/opt/web01.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off + sed -i /uuid/d /tmp/web04.xml + sed -i '/mac address/d' /tmp/web04.xml + sed -i 2s#web01#web04# /tmp/web04.xml + sed -i s#/opt/web01.qcow2#/opt/web04.qcow2#g /tmp/web04.xml + virsh define /tmp/web04.xml Domain web04 defined from /tmp/web04.xml + virsh start web04 Domain web04 started [root@kvm opt]# virsh list --all #查看链接克隆的web04 Id Name State ---------------------------------------------------- 7 centos7 running 9 web02 running 10 web01 running 11 web03 running 12 web04 running

    四、两条命令完成链接克隆

    [root@kvm opt]# qemu-img create -f qcow2 -b web01.qcow2 web05.qcow2 #创建虚拟机web05磁盘 Formatting 'web05.qcow2', fmt=qcow2 size=10737418240 backing_file='web01.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off [root@kvm opt]# virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web05 --memory 1024 --vcpus 1 --disk /opt/web05.qcow2 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole #生成web05配置文件及定义web05基础配置 Starting install... Domain creation completed. [root@kvm opt]# virsh list --all #查看已经运行的web05 Id Name State ---------------------------------------------------- 9 web02 running 10 web01 running 11 web03 running 12 web04 running 13 web05 running

    五、kvm虚拟机网络

    kvm虚拟机默认为NAT模式 当虚拟机处于NAT模式,外网与虚拟机内网网络不能进行通讯

    virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 1024 --vcpus 1 --disk /opt/web04.qcow2 --boot hd --network network=default --graphics vnc,listen=0.0.0.0 --noautoconsole

    kvm虚拟机桥接模式 桥接模式需要先在服务器上创建一个网桥,通过宿主机网桥连接到虚拟机,自动分配IP地址,但需要注意的是,如果拿eth0作为网桥,eth0地址会被清空,这时你要是连接的eth0将会被强制断开,建议使用双网卡或多网卡,其中一张或多张网卡作为网桥使用,方便外网访问到kvm虚拟机。

    [root@kvm opt]# brctl show #查看网卡信息 bridge name bridge id STP enabled interfaces virbr0 8000.525400e2ac9a yes virbr0-nic vnet0 vnet1 vnet2 vnet3 vnet4 vnet5 virsh iface-bridge eth0 br0 #创建桥接网卡 virsh iface-unbridge br0 #取消桥接网卡 virt-install --virt-type kvm --os-type=linux --os-variant rhel7 --name web04 --memory 1024 --vcpus 1 --disk /data/web04.qcow2 --boot hd --network bridge=br0 --graphics vnc,listen=0.0.0.0 --noautoconsole
    Processed: 0.009, SQL: 8