Centos7一键搭建kvm环境

作者: ropon 分类: Kvm 发布时间: 2017-04-13 23:23

1、自动检测是否支持硬件虚拟化
2、自动创建br0、br1网桥,用于内网和外网
3、自动安装kvm所需组件

以下自动化shell脚本

#!/bin/bash

export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin"

ip=192.168.199.68
mip=172.16.199.68
hostname=kvm-node1
Check_OS() {
	if [ -e /etc/redhat-release ]; then
		OS=CentOS
		[ -n "$(grep ' 7\.' /etc/redhat-release 2> /dev/null)" ] && CentOS_Ver=7
		[ -n "$(grep ' 6\.' /etc/redhat-release 2> /dev/null)" ] && CentOS_Ver=6
	elif [ -n "$(grep -i 'Debian' /etc/issue 2> /dev/null)" ]; then
		OS=Debian
	elif [ -n "$(grep -i 'Ubuntu' /etc/issue 2> /dev/null)" ]; then
		OS=Ubuntu
	else
		OS=UnknownOS
	fi
}
Check_OS
[ $CentOS_Ver -ne 7 ] && echo "Only not support CentOS7" && exit
egrep '(vmx|svm)' /proc/cpuinfo>/dev/null 2>&1
[ $? -ne 0 ] && echo "Hardware virtualization is not supported" &&exit 1
if ! which wget > /dev/null ;then
	yum install -y wget
fi
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum makecache
yum install -y net-tools vim lrzsz tree screen lsof tcpdump
systemctl disable firewalld
systemctl stop firewalld
systemctl disable NetworkManager
systemctl stop NetworkManager
sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
grep SELINUX=disabled /etc/selinux/config
setenforce 0 
echo '  *  -  nofile   100000  '  >>/etc/security/limits.conf
yum update -y
hostnamectl --static set-hostname $hostname
cat > /etc/sysconfig/network-scripts/ifcfg-br0 <<EOF
TYPE="Bridge"
BOOTPROTO="static"
NAME="br0"
DEVICE="br0"
ONBOOT="yes"
IPADDR="${ip}"
NETMASK="255.255.255.0"
EOF

cat > /etc/sysconfig/network-scripts/ifcfg-br1 <<EOF
TYPE="Bridge"
BOOTPROTO="static"
NAME="br1"
DEVICE="br1"
ONBOOT="yes"
IPADDR="${nip}"
NETMASK="255.255.255.0"
EOF

mv /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network-scripts/ifcfg-eth0.bak
mv /etc/sysconfig/network-scripts/ifcfg-eth1 /etc/sysconfig/network-scripts/ifcfg-eth1.bak

cat > /etc/sysconfig/network-scripts/ifcfg-eth0 <<EOF
TYPE="Ethernet"
BOOTPROTO="none"
NAME="eth0"
BRIDGE="br0"
DEVICE="eth0"
ONBOOT="yes"
EOF

cat > /etc/sysconfig/network-scripts/ifcfg-eth1 <<EOF
TYPE="Ethernet"
BOOTPROTO="none"
NAME="eth1"
BRIDGE="br1"
DEVICE="eth1"
ONBOOT="yes"
EOF

yum -y install kvm
lsmod | grep kvm

yum -y install python-virtinst libvirt tunctl bridge-utils virt-manager qemu-kvm-tools virt-viewer virt-install virt-v2v libguestfs-tools acpid
yum install libguest* -y
yum install libvirt* -y
yum install virt* -y

cat > /etc/sysconfig/grub <<EOF
GRUB_TIMEOUT=5
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto rhgb net.ifnames=0 biosdevname=0 quiet"
GRUB_DISABLE_RECOVERY="true"
EOF

grub2-mkconfig -o /boot/grub2/grub.cfg

[ $? -eq 0 ] && echo "Success,15s reboot." 
sleep 15
reboot

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表评论

电子邮件地址不会被公开。 必填项已用*标注