通过KVM创建qcow2镜像文件 实例为制作CentOS 6.9镜像,其他镜像该方法同样适用。
1 2 3 4 [root@hyhive01 infinityfs1]# ll CentOS-6.9-x86_64-bin-DVD1.iso -rw-r--r-- 1 root root 3972005888 Sep 4 08:27 CentOS-6.9-x86_64-bin-DVD1.iso [root@hyhive01 infinityfs1]# md5sum CentOS-6.9-x86_64-bin-DVD1.iso 2e227fa14c8a9791293b6525289c7dad CentOS-6.9-x86_64-bin-DVD1.iso
创建一个qcow2格式的文件,用于将这个文件制作成qcow2镜像,文件大小为制作之后的镜像最大使用容量,该容量在创建虚拟机的时候可调整
1 2 [root@hyhive01 infinityfs1]# qemu-img create -f qcow2 centos6.9.qcow2 20G Formatting 'centos6.9.qcow2', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off
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 [root@hyhive01 infinityfs1]# cat centos6.9.xml <domain type ='kvm' > <name > centos6.9</name > <memory ballonable ='no' > 8388608</memory > <vcpu > 6</vcpu > <os > <type arch ='x86_64' machine ='pc' > hvm</type > <boot dev ='cdrom' /> <boot dev ='hd' /> </os > <clock offset ='localtime' > <timer name ='hypervclock' present ='yes' /> </clock > <on_poweroff > destroy</on_poweroff > <on_reboot > restart</on_reboot > <on_crash > destroy</on_crash > <devices > <emulator > /usr/libexec/qemu-kvm</emulator > <disk type ='file' device ='cdrom' > <source file ='/infinityfs1/CentOS-6.9-x86_64-bin-DVD1.iso' /> <target dev ='hda' bus ='ide' /> </disk > <disk type ='file' device ='disk' > <driver name ='qemu' type ='qcow2' /> <source file ='/infinityfs1/centos6.9.qcow2' /> <target dev ='vda' bus ='virtio' /> </disk > <interface type ='bridge' > <model type ="virtio" /> <virtualport type ='openvswitch' /> <source bridge ='br-ex' /> </interface > <input type ='tablet' bus ='usb' /> <video > <model type ='cirrus' vram ='16384' heads ='1' primary ='yes' /> </video > <graphics type ='vnc' autoport ='yes' listen ='0.0.0.0' keymap ='en-us' > <listen type ='address' address ='0.0.0.0' /> </graphics > </devices > </domain >
xml文件解读:
1 2 3 4 5 6 7 8 <name > centos6.9</name > <memory ballonable ='no' > 8388608</memory > <vcpu > 6</vcpu > <os > <type arch ='x86_64' machine ='pc' > hvm</type > <boot dev ='cdrom' /> <boot dev ='hd' /> </os >
虚拟机的名称、内存、CPU配置、启动项
1 2 3 4 5 6 7 8 9 <disk type ='file' device ='cdrom' > <source file ='/infinityfs1/CentOS-6.9-x86_64-bin-DVD1.iso' /> <target dev ='hda' bus ='ide' /> </disk > <disk type ='file' device ='disk' > <driver name ='qemu' type ='qcow2' /> <source file ='/infinityfs1/centos6.9.qcow2' /> <target dev ='vda' bus ='virtio' /> </disk >
虚拟机的虚拟硬盘,需要注意的是qcow2
格式文件名称为qwmu
,bus
为virtio
。
另:windows
平台在安装过程中无法识别虚拟硬盘,需要安装专门的驱动。
1 2 3 4 5 <interface type ='bridge' > <model type ="virtio" /> <virtualport type ='openvswitch' /> <source bridge ='br-ex' /> </interface >
虚拟机的网卡配置,该网卡会直接桥接在openvswitch
的br-ex
网卡上。
1 2 3 <graphics type='vnc' autoport='yes' listen='0.0.0.0' keymap='en-us'> <listen type='address' address='0.0.0.0'/> </graphics>
虚拟机远程桌面vnc的配置,虚拟机启动之后,可直接通过vnc打开虚拟机图形界面。
1 2 [root@hyhive01 infinityfs1]# virsh create centos6.9.xml Domain centos6.9 created from centos6.9.xml
1 2 3 4 5 6 [root@hyhive01 infinityfs1]# virsh list --all Id Name State ---------------------------------------------------- 1 instance-00000012 running 10 instance-00000016 running 32 centos6.9 running
1 2 [root@hyhive01 infinityfs1]# virsh domdisplay centos6.9 vnc://localhost:0
剩下的过程如同CentOS安装过程,安装结束之后,文件路径中/infinityfs1/centos6.9.qcow2
即为做好的qcow2镜像。