一.环境准备
- 登录宿主机
- 安装虚拟化包kvm组件
yum -y install qemu-kvm qemu-kvm-tools virt-manager libvirt virt-install
rpm -qa|grep -E 'qemu|libvirt|virt'
systemctl enable libvirtd.service 加入开机自启动
systemctl start libvirtd.service 启动服务
systemctl status libvirtd.service 查看状态
- 准备虚拟化目录
根据宿主机数据盘情况,创建 .../images/ .../disk/ .../xml/ 三个目录用于存放虚机文件
[xxxxxxxx@xxxxxxxx ~]$ ll /xxxx/data/images/ 镜像存放目录
-rw-r--r-- 1 root root 1221197824 Oct 1 10:35 centos.qcow2
-rw-r--r-- 1 qemu qemu 2461204480 Oct 11 09:18 111.qcow2
-rw-r--r-- 1 qemu qemu 2458976256 Oct 11 09:18 222.qcow2
-rw-r--r-- 1 qemu qemu 2458779648 Oct 11 09:18 333.qcow2
-rw-r--r-- 1 qemu qemu 5991825408 Oct 11 09:02 444.qcow2
-rw-r--r-- 1 qemu qemu 8140423168 Oct 11 09:18 555.qcow2
[xxxxxxxx@xxxxxxxx ~ ~]$ ll /xxxx/data/disk/ 磁盘存放目录
-rw-r--r-- 1 qemu qemu 198208 Oct 1 14:55 111-data.qcow2
-rw-r--r-- 1 qemu qemu 198208 Oct 1 14:55 222-data.qcow2
-rw-r--r-- 1 qemu qemu 198208 Oct 1 14:55 333-data.qcow2
-rw-r--r-- 1 qemu qemu 197632 Oct 1 14:05 444-data.qcow2
-rw-r--r-- 1 qemu qemu 197632 Oct 1 14:06 555-data.qcow2
-rw-r--r-- 1 root root 197632 Oct 1 14:01 vmdisk.qcow2
[xxxxxxxx@xxxxxxxx ~ ~]$ ll /xxxx/data/xml/ 启动文件存放目录
total 20
-rw-r--r-- 1 root root 2419 Oct 1 14:55 01.xml
-rw-r--r-- 1 root root 2419 Oct 1 14:55 02.xml
-rw-r--r-- 1 root root 2419 Oct 1 14:55 03.xml
-rw-r--r-- 1 root root 2305 Oct 1 16:03 04.xml
-rw-r--r-- 1 root root 2305 Oct 1 16:03 05.xml
- 准备虚机系统盘(已经安装好的centos 镜像.qcow2文件)
wget http://(下载镜像的地址)/xxxx/xxxx/centos.qcow2
cp centos.qcow2 /xxxx/data/images/centos.qcow2 ###复制镜像文件作为新虚机的系统盘,可复制多个给多个新虚机使用
- 创建虚机数据盘(根据要创建的虚机需求设置)
qemu-img create -f qcow2 /xxxx/data/disk/vmdisk.qcow2 64G (64GB一块数据盘)
- 准备网卡桥接配置
修改宿主机网卡指定桥接网卡名
DEVICE=xxxx
NAME=xxxx
PHYSDEV=xxxx
VID=xxx
VLAN=yes
ISALIAS=no
BOOTPROTO=none
ONBOOT=yes
TYPE=Vlan
MTU=1500
GATEWAY=
DNS1=
BRIDGE=br1
新建桥接网卡ifcfg-br1:
DEVICE=br1
ONBOOT=yes
TYPE=Bridge
BOOTPROTO=static
HOTPLUG=no
- 准备虚机启动文件(xml配置文件)
# vi 01.xml
注意标黄色的地方配置需要根据实际情况进行修改:
<domain type='kvm'>
<name>test01</name> <!-- 虚机名称(xml文件名) -->
<memory unit='KiB'>16777216</memory> <!-- 虚机内存设置 -->
<currentMemory unit='KiB'>16777216</currentMemory> <!-- 虚机内存设置 -->
<vcpu cpuset="4-47">8</vcpu> <!-- 虚机CPU核数设置 -->
<resource>
<partition>/machine</partition>
</resource>
<sysinfo type='smbios'>
<system>
<entry name='manufacturer'>China Telecom</entry>
<entry name='family'>Virtual Machine</entry>
</system>
</sysinfo>
<os>
<type>hvm</type>
<boot dev="hd"/>
<smbios mode="sysinfo"/>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='host-passthrough'/>
<clock offset='utc'/>
<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='disk'>
<driver name='qemu' type='qcow2' cache='none' io='native'/>
<source file='/xxxx/data/images/centos.qcow2'/> <!-- 系统盘文件 -->
<target dev='vda' bus='virtio'/>
<alias name='virtio-disk0'/>
</disk>
<disk type='file' device='disk'>
<driver name='qemu' type='qcow2' cache='none' io='native'/>
<source file='/xxxx/data/disk/vmdisk.qcow2'/> <!-- 数据盘文件 -->
<target dev='vdb' bus='virtio'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x06' function='0x0'/>
</disk>
<controller type='usb' index='0' model='piix3-uhci'>
<alias name='usb'/>
</controller>
<controller type='pci' index='0' model='pci-root'>
<alias name='pci.0'/>
</controller>
<interface type='bridge'>
<source bridge='br0'/> <!-- 宿主机的桥接网卡名 -->
<model type='virtio'/>
</interface>
<interface type='bridge'>
<source bridge='br1/> <!-- 宿主机的桥接网卡名 -->
<model type='virtio'/>
</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>
<input type='tablet' bus='usb'>
<alias name='input0'/>
</input>
<input type='mouse' bus='ps2'>
<alias name='input1'/>
</input>
<input type='keyboard' bus='ps2'>
<alias name='input2'/>
</input>
<graphics type="vnc" autoport="yes" keymap="en-us" listen="0.0.0.0"/>
<video>
<model type="cirrus"/>
</video>
</devices>
</domain>
- 启动虚机
virsh define /xxxx/data/xml/test01.xml ##部署虚机
virsh start test01 ### 启动虚机
virsh list --all ###查看虚机