#!/bin/bash
####################################################################
# 初始化KVM宿主机服务器环境
# Create Date :  2015-09-11
# Written by : peter
# Organization:  uxin
####################################################################

SOFT_REPO='http://10.30.0.14'
LOG="/tmp/init_kvm_master.log"
echo "---`date +"%Y%m%d-%H%M%S"`---" >> ${LOG}
#***GLOBAL SET***
Red="\E[31;40m"; Green="\E[32;40m"; Blue="\E[34;40m";Yellow="\E[33;40m"; C_begin0="\033[1m"; C_begin1="\033[1;5m"; C_end="\033[0m"
#获取主机ip地址
IN_Face=`route -n |awk '{if($4~/UG/){print $8}}'|head -n 1` 
ipfull=`/sbin/ifconfig|grep -B1 -C1 -w "${IN_Face}"|grep -w 'inet addr'|awk -F: '{print $2}'|awk '{print $1}'`


#***Some global check.***
WHO=$(/usr/bin/whoami)
if [ "${WHO}" = "root" ]
then
    :
else
    echo -e "${C_begin0}${Blue}Only root can run me.${C_end}"
    exit 1;
fi
if [[ $# -ne 1 ]]
then
        USAGE
else
        WHICH=$1
fi
#add auto backup
echo "2 3 * * *  /bin/sh /xs/scripts/kvm-snapshot-backup.sh >> /xs/scripts/kvm-snapshot-backup.log" >> /var/spool/cron/root

#add dir
mkdir -p /xs/kvm/vm_xs/
mkdir -p /xs/backup/kvm/snapshots/
cd /xs/scripts/
wget ${SOFT_REPO}/kvm-snapshot-backup.sh
cd /xs/kvm/vm_xs/
#wget ${SOFT_REPO}/kvm_model.tgz



#安装kvm依赖包
yum -y install kvm  avahi dmidecode libvirt libvirt-pytho python-virtinst virt-manager virt-viewer bridge-utils qemu-kvm.x86_64 qemu-kvm-tools.x86_64
# Start the services required by libvirt
service messagebus restart
service avahi-daemon restart    
# Start the service during the system startup
chkconfig messagebus on
chkconfig avahi-daemon on

#libvirt 配置启动
cat << EOF > /etc/libvirt/libvirtd.conf
listen_tls=0
listen_tcp=1
auth_tcp="none"
tcp_port="16509"
mdns_adv = 0
LIBVIRTD_ARGS="--listen"
EOF

cat << EOF > /etc/libvirt/qemu.conf
cgroup_controllers=["cpu"]
security_driver="none"
user="root"
group="root"
vnc_listen="0.0.0.0"
EOF

service libvirtd restart
chkconfig libvirtd on 


#openvswitch安装配置
cat << EOF > /etc/yum.repos.d/rdo-release.repo
[openstack-icehouse]
name=OpenStack Icehouse Repository
baseurl=http://repos.fedorapeople.org/repos/openstack/EOL/openstack-icehouse/epel-6/ 
enabled=1
skip_if_unavailable=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-RDO-Icehouse
priority=98
EOF



#yum install -y https://repos.fedorapeople.org/repos/openstack/EOL/openstack-icehouse/epel-6/rdo-release-icehouse-4.noarch.rpm
wget http://ftp.gnu.org/gnu/autoconf/autoconf-2.69.tar.gz
tar -xzvf autoconf-2.69.tar.gz
cd autoconf-2.69
./configure
make -j 4
make install
#yum -y install openvswitch
rmmod bridge
echo 'blacklist bridge' >> /etc/modprobe.d/blacklist.conf

yum install gcc make python-devel openssl-devel kernel-devel graphviz    kernel-debug-devel autoconf automake rpm-build redhat-rpm-config    libtool -y
ln -s /usr/src/kernels/2.6.32-642.1.1.el6.x86_64 /lib/modules/2.6.32-642.el6.x86_64/build

wget http://openvswitch.org/releases/openvswitch-2.3.1.tar.gz 
tar xvf openvswitch-2.3.3.tar.gz
cd openvswitch-2.3.3
cp rhel/etc_init.d_openvswitch /etc/init.d/openvswitch
./configure --prefix=/usr --localstatedir=/var --sysconfdir=/etc
make&& make install
ovsdb-tool create /etc/openvswitch/conf.db vswitch.ovsschema
ovsdb-server  -v --remote=punix:/var/run/openvswitch/db.sock \
        --remote=db:Open_vSwitch,Open_vSwitch,manager_options \
        --private-key=db:Open_vSwitch,SSL,private_key \
        --certificate=db:Open_vSwitch,SSL,certificate \
        --bootstrap-ca-cert=db:Open_vSwitch,SSL,ca_cert \
        --pidfile --detach

ovs-vsctl --no-wait init
ovs-vswitchd --pidfile --detach
ovs-vsctl show
depmod -A openvswitch		

chmod 755 /etc/init.d/openvswitch
/etc/init.d/openvswitch start
chkconfig openvswitch on


ovs-vsctl add-br cloudbr0 
ovs-vsctl add-port cloudbr0 bond0

#ovs-vsctl add-port cloudbr1 em2
#ovs-vsctl set port cloudbr1 trunks=128,60,180
#ovs-vsctl add-br mgmt0 cloudbr1 128
#ovs-vsctl add-br cloudbr2 cloudbr1 60
#ovs-vsctl add-br cloudbr3 cloudbr1 180





cat << EOF > /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
BOOTPROTO=none
IPV6INIT=no
NM_CONTROLLED=no
ONBOOT=yes
TYPE=OVSPort
DEVICETYPE=ovs
OVS_BRIDGE=cloudbr0
EOF



cat << EOF > /etc/sysconfig/network-scripts/ifcfg-cloudbr0
DEVICE=cloudbr0
ONBOOT=yes
DEVICETYPE=ovs
TYPE=OVSBridge
BOOTPROTO=none
HOTPLUG=no
EOF
