You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kvm/control/KVMInstall.sh

69 lines
1.9 KiB

#!/usr/bin/bash
<<!
editor : NewRain
date : 2022-08-15
for kvm service install
!
INTERFACE_NAME="ens33"
color(){
unset c
declare -A c=([Error]=31 [Success]=32 [Warning]=33 [Info]=34)
#echo -e "\033[${c[$1]}m[`date +%T`]($1) $2\033[0m"
printf "\033[${c[$1]}m%-10s%-10s %-30s\033[0m\n" "[`date +%T`]" "($1)" "$2"
sleep 0.5
}
clean(){
color Info "开始进行环境清理"
yum remove `rpm -qa | egrep 'qemu|virt|kvm'` -y
rm -rf /var/lib/libvirt /etc/libvirt/
}
upgrade(){
if [ -f /etc/centos-release ];then
color Success "检测到系统为centos"
level=$(awk '{print $4}' /etc/centos-release | awk -F'.' '{print $2}')
if [ $level -le 7 ];then
color Warning "准备进行内核升级,ctrl^c退出"
yum upgrade
reboot
else
color Success "系统版本符合要求,开始进行kvm安装"
fi
else
color Error "请使用centos系统运行次脚本"
exit 1
fi
}
main(){
color Info "开始安装KVM服务"
yum install -y librbd1-devel qemu-kvm libvirt virt-manager virt-install libguestfs-tools
cat > /etc/sysconfig/network-scripts/ifcfg-br0 <<EOF
TYPE=Bridge
NAME=br0
DEVICE=br0
ONBOOT="yes"
BOOTPROTO=static
IPADDR=$(ip a show dev ${INTERFACE_NAME} | awk '/\<inet\>/{print $2}' | awk -F'/' '{print $1}')
GATEWAY=$(ip r show dev ${INTERFACE_NAME} | awk '/default/{print $3}')
PREFIX=$(ip a show dev ${INTERFACE_NAME} | awk '/\<inet\>/{print $2}' | awk -F'/' '{print $2}')
DNS1=114.114.114.114
DNS2=8.8.8.8
EOF
cat > /etc/sysconfig/network-scripts/ifcfg-${INTERFACE_NAME} <<EOF
NAME=${INTERFACE_NAME}
DEVICE=${INTERFACE_NAME}
ONBOOT=yes
BRIDGE=br0
EOF
echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf && sysctl -p
systemctl enable libvirtd --now
color Success "安装并启动完成"
systemctl restart network
}
main