这个脚本有限地方还需要完善下
设计思路:
1、创建检查内核函数 check_kernel()
2、创建升级内核函数 update_kernel()
3、创建检查是否支持虚拟化函数 check_virtual()
4、创建检查操作系统函数 check_system()
5、创建检查网络函数 check_network()
6、创建检查YUM源函数 check_yum()
7、创建安装KVM依赖包函数 install_kvm()
8、创建卸载清理KVM依赖包函数 uninstall_kvm()
9、创建加载启动支持nmcli的模块或工具函数 restart_nmcli_set()
CentOS中自动加载802.1q模块:modprobe 8021q
启动nmcli服务:systemctl restart NetworkManager
Centos7网卡配置命令nmcli
https://blog.csdn.net/xdy762024688/article/details/132085304?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22132085304%22%2C%22source%22%3A%22xdy762024688%22%7Dhttps://blog.csdn.net/xdy762024688/article/details/132085304?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22132085304%22%2C%22source%22%3A%22xdy762024688%22%7D
10、创建安装KVM运行环境包函数 install_kvm_runtime()
11、创建卸载清理KVM运行环境包函数 uninstall_kvm_runtime()
12、创建提示信息函数 Usage()
13、创建case 选项 case $arg in
case $arg in
i|install)
echo "一键部署KVM虚拟化环境"
install_kvm_runtime
;;
u|uninstall)
echo "一键清理KVM虚拟化环境"
uninstall_kvm_runtime
;;
h|help)
Usage
;;
*)
Usage
;;
esac
cat kvm-install-deploy.sh
#!/bin/sh #Author:norman.li #Data: 2022-01-24 #Descript:该脚本为一键部署KVM环境 #检查内核 check_kernel() { kernel_version=`uname -r | awk -F '-' '{print $2}' | awk -F '.' '{print $1}'` echo "${kernel_version}" } #升级内核 update_kernel() { yum -y update kernel* } #检查是否支持虚拟化 check_virtual() { vmx=`grep -E -o 'svm|vmx' /proc/cpuinfo` echo ${vmx} } #检查操作系统 check_system() { system_name=`cat /etc/redhat-release` echo "${system_name}" } #检查网络 check_network() { echo "check network" } #检查YUM源 check_yum() { echo "check yum list" } #安装KVM依赖包 install_kvm() { yum -y install qemu-kvm libvirt libvirt-python libguestfs-tools libguestfs-tools-c virt-install chkconfig --level 35 libvirtd on service libvirtd start } #卸载清理KVM环境包 uninstall_kvm() { chkconfig --level 35 libvirtd off service libvirtd stop yum -y remove qemu-kvm libvirt libvirt-python libguestfs-tools libguestfs-tools-c virt-install } #加载启动支持nmcli的模块或工具 restart_nmcli_set() { modprobe 8021q systemctl restart NetworkManager } #安装KVM运行环境包 install_kvm_runtime() { echo "install kvm runtime" check_system check_kernel check_virtual check_network check_yum install_kvm restart_nmcli_set } uninstall_kvm_runtime() { echo "uninstall kvm runtime" uninstall_kvm } Usage() { echo "命令格式: $0 [功能选项] 详见示例!" echo "功能选项:" echo " [-i] 一键部署KVM虚拟化环境" echo " [-u] 一键清理KVM虚拟化环境" echo "使用示例:" echo " 1.一键部署KVM虚拟化环境: $0 -i" echo " 2.一键清理KVM虚拟化环境: $0 -u" } arg=`echo $1 | tr -d '-'` case $arg in i|install) echo "一键部署KVM虚拟化环境" install_kvm_runtime ;; u|uninstall) echo "一键清理KVM虚拟化环境" uninstall_kvm_runtime ;; h|help) Usage ;; *) Usage ;; esac