1、安装
2、配置br0网桥
#新建bro网卡配置
[root@kvm01-80 network-scripts]# cat ifcfg-br0
TYPE=Bridge
NAME=br0
DEVICE=br0
ONBOOT="yes"
BOOTPROTO=static
IPADDR=10.1.20.80
GATEWAY=10.1.20.2
NETMASK=255.255.255.0
DNS1=8.8.8.8
DNS2=114.114.114.114
#修改网卡配置
[root@kvm01-80 network-scripts]# cat ifcfg-ens33
DEVICE=ens33
TYPE=Ethernet
ONBOOT=yes
NM_CONTROLLED=no
BRIDGE=br0
3、模板机创建
#模板配置文件
[root@kvm01-80 network-scripts]# cat /vmhost/base/modo.xml
<domain type='kvm'>
<name>vm_name</name>
<uuid>vm_uuid</uuid>
<memory unit='KiB'>2097152</memory>
<currentMemory unit='KiB'>2097152</currentMemory>
<vcpu placement='static'>1</vcpu>
<os>
<type arch='x86_64' machine='pc-i440fx-8.2'>hvm</type>
</os>
<features>
<acpi/>
<apic/>
</features>
<cpu mode='host-passthrough' check='none' migratable='on'/>
<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' discard='unmap'/>
<source file='vm_img'/>
<target dev='hda' bus='ide'/>
<boot order='1'/>
<address type='drive' controller='0' bus='0' target='0' unit='0'/>
</disk>
<disk type='file' device='cdrom'>
<driver name='qemu' type='raw'/>
<target dev='hdb' bus='ide'/>
<readonly/>
<address type='drive' controller='0' bus='0' target='0' unit='1'/>
</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>
<interface type='bridge'>
<mac address='vm_mac'/>
<source bridge='br0'/>
<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>
<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='vm_vnc_port' autoport='no' listen='0.0.0.0'>
<listen type='address' address='0.0.0.0'/>
</graphics>
<audio id='1' type='none'/>
<video>
<model type='vga' vram='16384' heads='1' primary='yes'/>
<address type='pci' domain='0x0000' bus='0x00' slot='0x02' function='0x0'/>
</video>
<memballoon model='virtio'>
<address type='pci' domain='0x0000' bus='0x00' slot='0x05' function='0x0'/>
</memballoon>
</devices>
</domain>
4、shell脚本管理KVM
#脚本程序
[root@kvm01-80 function]# cat kvm.sh
#!/bin/bash
#主菜单
function menu {
echo -e "\e[36m******************KVM主管理程序******************\e[0m"
echo -e "\e[36m*1. 创建虚拟机 2. 虚拟机管理 *\e[0m"
echo -e "\e[36m*3. 快照管理 4. 存储池管理 *\e[0m"
echo -e "\e[36m*5. 退出 *\e[0m"
echo -e "\e[36m*************************************************\e[0m"
read -p "请输入选项编号(1-5): " choice
case $choice in
1) create_vm ;;
2) manage_sys ;;
3) snapshot_mgr ;;
4) storage_pool_mgr ;;
5) exit 0 ;;
*) echo "无效输入"; sleep 1; menu ;;
esac
}
#1、创建虚拟机
create_vm() {
echo -e "\e[36m=============== 列出已安装的虚拟机 ================\e[0m"
virsh list --all
echo -e "\e[36m===================================================\e[0m"
read -p "请输入创建的虚拟机名称、虚拟机域名称不可重复: " vm_name
read -p "请输入创建的虚拟机vnc端口: " vm_vnc_port
#源镜像
vm_img=/vmhost/base/openeuler22.03.qcow2
#模板文件
vm_template=/vmhost/base/modo.xml
#新镜像存放位置
new_img=/vmhost/img/${vm_name}.qcow2
qemu-img convert -f qcow2 -O qcow2 ${vm_img} ${new_img} &>/dev/null
cp -rf ${vm_template} /tmp/${vm_name}.xml
vm_uuid=$(uuidgen)
vm_mac="0A:0A:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum \
| sed -r 's/^(..)(..)(..)(..).*$/\1:\2:\3:\4/')"
sed -i "s/vm_name/${vm_name}/g" /tmp/${vm_name}.xml
sed -i "s/vm_uuid/${vm_uuid}/g" /tmp/${vm_name}.xml
sed -i "s/vm_mac/${vm_mac}/g" /tmp/${vm_name}.xml
sed -i "s/vm_vnc_port/${vm_vnc_port}/g" /tmp/${vm_name}.xml
sed -i "s#vm_img#${new_img}#g" /tmp/${vm_name}.xml
virsh define /tmp/${vm_name}.xml
if [ $? -eq 0 ];then
echo -e "\e[36msuccee...虚拟机${vm_name}创建成功\e[0m"
echo -e "\e[36m返回主菜单\e[0m"
else
echo -e "\e[31mERROR...虚拟机${vm_name}创建失败\e[0m"
fi
menu
}
#虚拟机管理
manage_sys() {
echo -e "\e[36m=============== 列出所有虚拟机=================\e[0m"
virsh list --all
read -p "输入虚拟机名称: " vm_name
echo -e "\e[36m=============== 虚拟机管理菜单 ================\e[0m"
echo -e "\e[36m1.启动 2.关闭 3.删除 4.返回主菜单\e[0m"
read -p "选择操作序号(1-3): " opt
case $opt in
1) virsh start ${vm_name} ;;
2) virsh shutdown ${vm_name} ;;
3) virsh destroy ${vm_name}; virsh undefine ${vm_name} ;;
4) menu ;;
*) echo "无效操作"; sleep 1; manage_vm ;;
esac
}
snapshot_mgr() {
echo -e "\e[36m=============== 虚拟机列表 ================\e[0m"
virsh list --all
read -p "输入虚拟机名称: " vm_name
echo -e "\e[36m1.创建快照 2.恢复快照 3.列出快照 4.删除快照\e[0m"
read -p "选择操作序号(1-4): " op
case $op in
1) virsh snapshot-create-as $vm_name --name "${vm_name}_snap$(date +%F)" ;;
2) virsh snapshot-list $vm_name; read -p "输入快照名: " snap; virsh snapshot-revert $vm_name $snap ;;
3) virsh snapshot-list $vm_name && snapshot_mgr; ;;
4) virsh snapshot-list $vm_name; read -p "输入要删除的快照名: " snap; virsh snapshot-delete $vm_name $snap ;;
*) echo "无效操作" ;;
esac
sleep 2
#menu
}
menu