jimvn.install.sh 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. #!/usr/bin/env bash
  2. #
  3. # JimV-N
  4. #
  5. # Copyright (C) 2017 JimV <james.iter.cn@gmail.com>
  6. #
  7. # Author: James Iter <james.iter.cn@gmail.com>
  8. #
  9. # This script will help you automatically installed JimV-N.
  10. #
  11. export SHOW_WARNING_VTX=false
  12. ARGS=`getopt -o h --long host:,token:,version:,help -n 'INSTALL.sh' -- "$@"`
  13. eval set -- "${ARGS}"
  14. while true
  15. do
  16. case "$1" in
  17. --host)
  18. export HOST=$2
  19. shift 2
  20. ;;
  21. --token)
  22. export TOKEN=$2
  23. shift 2
  24. ;;
  25. --version)
  26. export JIMV_VERSION=$2
  27. shift 2
  28. ;;
  29. -h|--help)
  30. echo 'INSTALL.sh [-h|--help|--version] {--host,--token}'
  31. exit 0
  32. ;;
  33. --)
  34. shift
  35. break
  36. ;;
  37. *)
  38. echo "Internal error!"
  39. exit 1
  40. ;;
  41. esac
  42. done
  43. function check_precondition() {
  44. source /etc/os-release
  45. case ${ID} in
  46. centos|fedora|rhel)
  47. if [[ ${VERSION_ID} -lt 7 ]]; then
  48. echo "系统版本号必须大于等于 7,检测到当前的系统版本号为 ${VERSION_ID}."
  49. exit 1
  50. fi
  51. ;;
  52. *)
  53. echo "系统发行版 ${ID} 未被支持,请手动完成安装。"
  54. exit 1
  55. ;;
  56. esac
  57. if [[ `egrep -c '(vmx|svm)' /proc/cpuinfo` -eq 0 ]]; then
  58. export SHOW_WARNING_VTX=true
  59. fi
  60. if [[ ! ${JIMV_VERSION} ]] || [[ ${#JIMV_VERSION} -eq 0 ]]; then
  61. export JIMV_VERSION='master'
  62. fi
  63. if [[ ! ${HOST} ]] || [[ ${#HOST} -eq 0 ]]; then
  64. echo "你需要指定参数 '--host'"
  65. exit 1
  66. fi
  67. if [[ ! ${TOKEN} ]] || [[ ${#TOKEN} -eq 0 ]]; then
  68. echo "你需要指定参数 '--token'"
  69. exit 1
  70. fi
  71. yum install epel-release -y
  72. yum install jq net-tools bind-utils -y
  73. yum install redis -y
  74. # 代替语句 ifconfig | grep -Eo 'inet (addr:)?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*' | grep -v '127.0.0.1'
  75. SERVER_IP=`hostname -I`; export SERVER_IP=${SERVER_IP%% *}
  76. export SERVER_NETMASK=`ifconfig | grep ${SERVER_IP} | grep -Eo 'netmask ?([0-9]*\.){3}[0-9]*' | grep -Eo '([0-9]*\.){3}[0-9]*'`
  77. export GATEWAY=`route -n | grep '^0.0.0.0' | awk '{ print $2; }'`
  78. export DNS1=`nslookup 127.0.0.1 | grep Server | grep -Eo '([0-9]*\.){3}[0-9]*'`
  79. export NIC=`ifconfig | grep ${SERVER_IP} -B 1 | head -1 | cut -d ':' -f 1`
  80. export NODE_ID=`python -c "with open('/etc/machine-id', 'r') as f: _str = f.read().strip(); import string; import hashlib; m = hashlib.md5(); m.update(_str); print int(string.atoi(m.hexdigest(), 16).__str__()[:16])"`
  81. JOIN_RESPONSE=`curl http://${HOST}/api/misc/_join/${NODE_ID}/${TOKEN}`
  82. export REDIS_HOST=`echo ${JOIN_RESPONSE} | jq -r '.data.redis_host'`
  83. export REDIS_PORT=`echo ${JOIN_RESPONSE} | jq -r '.data.redis_port'`
  84. export REDIS_PSWD=`echo ${JOIN_RESPONSE} | jq -r '.data.redis_password'`
  85. export VM_NETWORK=`echo ${JOIN_RESPONSE} | jq -r '.data.vm_network'`
  86. export VM_NETWORK_MANAGE=`echo ${JOIN_RESPONSE} | jq -r '.data.vm_manage_network'`
  87. if [[ ! ${REDIS_HOST} ]] || [[ ${#REDIS_HOST} -eq 0 ]] || [[ ${REDIS_HOST} == null ]]; then
  88. echo "请确认到 ${HOST} 的连通性,或 /etc/machine-id 与集群中其它节点冲突。"
  89. echo "详情: ${JOIN_RESPONSE}"
  90. exit 1
  91. fi
  92. if [[ ! ${REDIS_PORT} ]] || [[ ${#REDIS_PORT} -eq 0 ]] || [[ ${REDIS_HOST} == null ]]; then
  93. echo "请确认到 ${HOST} 的连通性,或 /etc/machine-id 与集群中其它节点冲突。"
  94. echo "详情: ${JOIN_RESPONSE}"
  95. exit 1
  96. fi
  97. if [[ ! ${REDIS_PSWD} ]] || [[ ${#REDIS_PSWD} -eq 0 ]] || [[ ${REDIS_HOST} == null ]]; then
  98. echo "请确认到 ${HOST} 的连通性,或 /etc/machine-id 与集群中其它节点冲突。"
  99. echo "详情: ${JOIN_RESPONSE}"
  100. exit 1
  101. fi
  102. if [[ ! ${VM_NETWORK} ]] || [[ ${#VM_NETWORK} -eq 0 ]] || [[ ${REDIS_HOST} == null ]]; then
  103. echo "请确认到 ${HOST} 的连通性,或 /etc/machine-id 与集群中其它节点冲突。"
  104. echo "详情: ${JOIN_RESPONSE}"
  105. exit 1
  106. fi
  107. if [[ ! ${VM_NETWORK_MANAGE} ]] || [[ ${#VM_NETWORK_MANAGE} -eq 0 ]] || [[ ${REDIS_HOST} == null ]]; then
  108. echo "请确认到 ${HOST} 的连通性,或 /etc/machine-id 与集群中其它节点冲突。"
  109. echo "详情: ${JOIN_RESPONSE}"
  110. exit 1
  111. fi
  112. REDIS_RESPONSE='x_'`redis-cli -h ${REDIS_HOST} -a ${REDIS_PSWD} -p ${REDIS_PORT} --raw ping`
  113. if [[ ${REDIS_RESPONSE} != 'x_PONG' ]]; then
  114. echo "Redis 连接失败,请检查参数 --host, --token 是否正确。"
  115. exit 1
  116. fi
  117. }
  118. function custom_repository_origin() {
  119. mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
  120. mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.backup
  121. mv /etc/yum.repos.d/epel-testing.repo /etc/yum.repos.d/epel-testing.repo.backup
  122. curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  123. curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
  124. sed -i '/aliyuncs/d' /etc/yum.repos.d/*.repo
  125. yum clean all
  126. yum makecache
  127. }
  128. function clear_up_environment() {
  129. systemctl stop firewalld
  130. systemctl disable firewalld
  131. systemctl stop NetworkManager
  132. systemctl disable NetworkManager
  133. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/sysconfig/selinux
  134. sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
  135. setenforce 0
  136. }
  137. function install_libvirt_and_libguestfish() {
  138. # 安装 libvirt
  139. cat > /etc/yum.repos.d/JimV.repo << EOF
  140. [JimV]
  141. name=JimV - \$basearch
  142. baseurl=http://repo.jimv.cn/centos/7/os/\$basearch
  143. http://repo.jimv.io/centos/7/os/\$basearch
  144. http://repo.iit.im/centos/7/os/\$basearch
  145. failovermethod=priority
  146. enabled=1
  147. gpgcheck=0
  148. gpgkey=https://repo.jimv.cn/RPM-GPG-KEY-JIMV-114EA591
  149. EOF
  150. uname -m | grep -q 'x86_64' && echo 'centos' >/etc/yum/vars/contentdir || echo 'altarch' >/etc/yum/vars/contentdir
  151. yum install centos-release-qemu-ev -y
  152. yum install jimv-node -y
  153. yum install librbd1-10.2.5 -y
  154. }
  155. function tune_linux_kernel_parameters() {
  156. sed -i '/^net.ipv4.ip_local_port_range/d' /etc/sysctl.conf
  157. echo 'net.ipv4.ip_local_port_range = 15900 20000' >> /etc/sysctl.conf
  158. sysctl -p
  159. }
  160. function handle_ssh_client_config() {
  161. # 关闭 SSH 服务器端 Key 校验
  162. sed -i 's@.*StrictHostKeyChecking.*@StrictHostKeyChecking no@' /etc/ssh/ssh_config
  163. }
  164. function handle_net_bonding_bridge() {
  165. # 参考地址: https://access.redhat.com/documentation/zh-cn/red_hat_enterprise_linux/7/html/networking_guide/
  166. cat > /etc/sysconfig/network-scripts/ifcfg-${VM_NETWORK} << EOF
  167. DEVICE=${VM_NETWORK}
  168. NAME=${VM_NETWORK}
  169. TYPE=Bridge
  170. BOOTPROTO=static
  171. ONBOOT=yes
  172. DELAY=0
  173. IPADDR=${SERVER_IP}
  174. NETMASK=${SERVER_NETMASK}
  175. GATEWAY=${GATEWAY}
  176. DNS1=${DNS1}
  177. DNS2=8.8.8.8
  178. IPV6INIT=no
  179. EOF
  180. cat > /etc/sysconfig/network-scripts/ifcfg-bond0 << EOF
  181. DEVICE=bond0
  182. NAME=bond0
  183. TYPE=Bond
  184. BRIDGE=${VM_NETWORK}
  185. BONDING_MASTER=yes
  186. ONBOOT=yes
  187. BOOTPROTO=none
  188. BONDING_OPTS="mode=balance-alb xmit_hash_policy=layer3+4"
  189. EOF
  190. cat > /etc/sysconfig/network-scripts/ifcfg-${NIC} << EOF
  191. DEVICE=${NIC}
  192. NAME=${NIC}
  193. TYPE=Ethernet
  194. BOOTPROTO=none
  195. ONBOOT=yes
  196. MASTER=bond0
  197. SLAVE=yes
  198. EOF
  199. /etc/init.d/network restart
  200. }
  201. function create_network_bridge_in_libvirt() {
  202. cat > /etc/libvirt/qemu/networks/${VM_NETWORK}.xml << EOF
  203. <network>
  204. <uuid>the_uuid</uuid>
  205. <name>${VM_NETWORK}</name>
  206. <forward mode="bridge"/>
  207. <bridge name="${VM_NETWORK}"/>
  208. </network>
  209. EOF
  210. sed -i "s@the_uuid@`uuidgen`@" /etc/libvirt/qemu/networks/${VM_NETWORK}.xml
  211. # 去除默认的 default 网络定义
  212. rm -f /etc/libvirt/qemu/networks/default.xml /etc/libvirt/qemu/networks/autostart/default.xml
  213. # 使其随服务自动创建
  214. cd /etc/libvirt/qemu/networks/autostart/
  215. ln -s ../${VM_NETWORK}.xml ${VM_NETWORK}.xml
  216. }
  217. function start_libvirtd() {
  218. systemctl stop dnsmasq
  219. systemctl disable dnsmasq
  220. systemctl enable libvirtd
  221. systemctl start libvirtd
  222. }
  223. function generate_config_file() {
  224. sed -i "s/\"redis_host\".*$/\"redis_host\": \"${REDIS_HOST}\",/" /etc/jimvn.conf
  225. sed -i "s/\"redis_password\".*$/\"redis_password\": \"${REDIS_PSWD}\",/" /etc/jimvn.conf
  226. sed -i "s/\"redis_port\".*$/\"redis_port\": \"${REDIS_PORT}\",/" /etc/jimvn.conf
  227. }
  228. function start_JimVN() {
  229. systemctl start jimvn.service
  230. systemctl enable jimvn.service
  231. }
  232. function display_summary_information() {
  233. echo
  234. echo "=== 信息汇总"
  235. echo "==========="
  236. echo
  237. if [[ ${SHOW_WARNING_VTX} = true ]]; then
  238. echo "警告:请检查 CPU 是否开启 VT 技术。未开启 VT 技术的计算节点,将以 QEMU 模式运行虚拟机。"
  239. echo
  240. fi
  241. echo "已经通过 'systemctl enable jimvn.service' 把 JimV-N 注册为随系统启动的服务。"
  242. echo "您还可以通过命令 'systemctl [start|stop|status] jimvn.service' 来管理本机的 JimV-N。"
  243. echo
  244. }
  245. function deploy() {
  246. check_precondition
  247. custom_repository_origin
  248. clear_up_environment
  249. install_libvirt_and_libguestfish
  250. tune_linux_kernel_parameters
  251. handle_ssh_client_config
  252. handle_net_bonding_bridge
  253. create_network_bridge_in_libvirt
  254. start_libvirtd
  255. generate_config_file
  256. start_JimVN
  257. display_summary_information
  258. }
  259. deploy