X7ROOT File Manager
Current Path:
/usr/local/VMOptimizationTools_2.69.0
usr
/
local
/
VMOptimizationTools_2.69.0
/
📁
..
📄
LinuxUpdate.XML
(647 B)
📄
SangforVMSTool
(3.11 KB)
📄
SangforVMSTool.service
(194 B)
📄
action_common.sh
(13.34 KB)
📄
busybox_cmd_ln.sh
(2.24 KB)
📄
collect_test_data.sh
(1.16 KB)
📄
common_config.sh
(278 B)
📄
data_test_tool.sh
(16.62 KB)
📄
debian_vmconfig.sh
(13.48 KB)
📁
driver
📄
guest_system_config.sh
(1.82 KB)
📄
hostname_config
(1 B)
📄
ipv6_network_config
(94 B)
📄
log.sh
(1.36 KB)
📄
mod_test.sh
(3.69 KB)
📄
mod_test1.sh
(914 B)
📄
net_common.sh
(915 B)
📄
network_compat_flag
(0 B)
📄
network_config
(86 B)
📄
pwd_info_config
(1 B)
📄
redhat_vmconfig.sh
(17.93 KB)
📄
reset_duid_config
(21 B)
📄
sangfor_guest_datareport
(25.63 KB)
📄
sangfor_heartalive.sh
(2.37 KB)
📄
sangfor_module_update
(28.11 KB)
📄
sangfor_set_network.sh
(504 B)
📄
sangfor_sfping
(4.23 KB)
📄
sangfor_sshkey_gen
(1.29 KB)
📄
sangfor_update_ipc_callback
(5.29 KB)
📄
sangfor_vm_proxyd
(3.45 KB)
📄
sangfor_vm_proxyd_r
(4.4 KB)
📄
sangfor_vm_proxyd_w
(1.32 KB)
📄
sangfor_vmconfig
(3.55 KB)
📄
sangfor_vmconfig_common
(38.34 KB)
📄
sangfor_vmconfig_ipc_callback
(4.75 KB)
📄
sangfor_watchdog
(3.95 KB)
📄
sfping
(17.7 KB)
📄
sfping.pl
(8.57 KB)
📄
startall
(1.78 KB)
📄
stopall
(1.11 KB)
📄
suse_vmconfig.sh
(10.07 KB)
📁
tools_x64
📁
tools_x86
📄
uni_test.sh
(2.59 KB)
📄
update_common_func
(4.35 KB)
📄
update_common_header
(4.86 KB)
📄
update_config
(2.89 KB)
📄
update_install
(33.63 KB)
📄
update_network.flag
(1 B)
📄
virtio_nic_attr_set.sh
(2.42 KB)
📄
virtio_nic_compat.sh.old1610337499
(5.64 KB)
📄
vm_ip_recover.sh
(16.66 KB)
📄
vm_ipc.sh
(9.06 KB)
📄
vmconfig_common_func
(23.11 KB)
📄
vmconfig_common_header
(3.37 KB)
📄
vmconfig_config
(1.02 KB)
📄
vmtools-install.sh
(31.16 KB)
📄
watch_test.sh
(313 B)
📄
watchdog.sh
(1.39 KB)
📁
x64
📁
x86
Editing: sangfor_vmconfig_common
#!/bin/bash # fixme: 临近发布,先屏蔽问题,后续本再修改 # shellcheck disable=SC2086,SC2154,SC2128,SC2013,SC2003,SC2068,SC2126,SC2155,SC2046,SC2115,SC1083,SC2129 shell_dir=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd ) . "${shell_dir}/log.sh" . "${shell_dir}/vmconfig_config" . "${shell_dir}/vm_ipc.sh" . "${shell_dir}/vmconfig_common_header" . "${shell_dir}/update_common_func" . "${shell_dir}/watchdog.sh" . "${shell_dir}/vmconfig_common_func" . "${shell_dir}/redhat_vmconfig.sh" . "${shell_dir}/suse_vmconfig.sh" . "${shell_dir}/debian_vmconfig.sh" . "${shell_dir}/action_common.sh" . "${shell_dir}/net_common.sh" #current directory declare g_current_dir="" #temp directory declare g_temp_dir="" # Get vmconfig file function # $1 The file name in the host # $2 The file path to save in the guest # return 0 is success and 1 is failed get_vm_config_file(){ if [ $# -ne 2 ];then log_error "argument number is error!" return 1 fi test -z "${g_temp_dir}" && return 1 test -z "${g_get_file_flag}" && return 1 local file_name="$1" local save_file_path="$2" local exit_code=1 log_debug "get_file:file_name:${file_name},save_file_path:${save_file_path}" #init local request_get_file_data_header="${g_header_json}" local request_get_file_data_json="${g_file_data_request_json}" #assign request_get_file_data_header="${request_get_file_data_header/"${g_replace_version_name}"/8208}" request_get_file_data_header="${request_get_file_data_header/"${g_replace_type_name}"/${MESSAGE_GET_VM_IP_CONFIG_REQ_LINUX}}" request_get_file_data_header="${request_get_file_data_header/"${g_replace_number_name}"/0}" request_get_file_data_header="${request_get_file_data_header/"${g_replace_response_packages_name}"/0}" request_get_file_data_json="${request_get_file_data_json/"${g_replace_header_name}"/${request_get_file_data_header}}" request_get_file_data_json="${request_get_file_data_json/"${g_replace_filename_name}"/${file_name}}" request_get_file_data_json="${request_get_file_data_json/"${g_replace_offset_name}"/0}" log_debug "request_get_file_data_json:${request_get_file_data_json}" #send request vm_ipc_to_local_host ${g_mode_id} 0 ${MESSAGE_GET_VM_IP_CONFIG_REQ_LINUX} "${request_get_file_data_json}" || return 1 local time_out=${g_req_config_timeout} local use_time=0 while [ ! -e "${g_temp_dir}/${g_get_file_flag}" ] do log_debug "${g_temp_dir}/${g_get_file_flag} not exist!" if [ ${use_time} -ge ${time_out} ];then log_warn "get file time out!" break; fi sleep 1 use_time=$((use_time+1)) done if [ ! -e "${g_temp_dir}/${g_get_file_flag}" ];then log_error "get file:${file_name} failed!" return 1 fi log_debug "${g_temp_dir}/${g_get_file_flag} exist!" mv -f "${g_temp_dir}/${g_save_file_temp_name}" "${save_file_path}" >/dev/null 2>&1 exit_code=$? log_info "remove file ${g_temp_dir}/${g_get_file_flag}" rm -f "${g_temp_dir}/${g_get_file_flag}" 2>/dev/null if [ $? -ne 0 ];then log_warn "get_file:rm -f ${g_temp_dir}/${g_get_file_flag} failed!" fi if [ ${exit_code} -ne 0 ];then log_error "get_file:move file failed!src file is:${g_temp_dir}/${g_save_file_temp_name} des file is:${save_file_path}" return 1 fi return 0 } #Get the current directory. #failed 1 #success 0 get_current_dir(){ g_current_dir="${shell_dir}" if [ x"${g_current_dir}" = x"" ];then log_error "get_current_dir() failed!" return 1 fi return 0 } #Init environment. init_env(){ get_current_dir exit_code=$? if [ ${exit_code} -eq 1 ];then log_error "get_current_dir() failed! " return 1 fi #temp directory. g_temp_dir="$( cd "${g_current_dir}/../" && pwd )/${g_temp_dir_name}" test -z "${g_current_dir}" && return 1 test -z "${g_temp_dir}" && return 1 create_dir "${g_temp_dir}" exit_code=$? if [ ${exit_code} -ne 0 ];then log_error "create dir $g_temp_dir...failed" return 1 fi return 0 } # set hostname # param $1: hostname # return 0(success),1(fail) set_hostname() { local host_name=$1 local config_file="" test -z "$host_name" && return 1 if [ ${#host_name} -gt 64 ];then log_error "hostname is too long ${#host_name}" return 1 fi if [ -e "/etc/hostname" ];then echo "${host_name}" >/etc/hostname elif [ -e "/etc/HOSTNAME" ];then echo "${host_name}" >/etc/HOSTNAME else # centos6 config_file="/etc/sysconfig/network" # format: HOSTNAME=xxx sed -i "/^\s*HOSTNAME/d" $config_file echo "HOSTNAME=${host_name}" >>$config_file fi hostname "$host_name" log_info "set hostname $host_name...success" return 0 } # read config and set hostname # param $1: hostname config file # return 0(success),1(fail) host_config_hostname() { local config_file_name=$1 local hostname="" if [ ! -e $config_file_name ];then log_error "no such file ${config_file_name}" return 1 fi hostname=`cat $config_file_name | grep hostname | awk -F "," '{print $1}'|awk -F ":" '{print $2}'` log_info "###hostname=$hostname" if test -z "$hostname";then log_error "hostname is not set" return 1 fi set_hostname $hostname return $? } # read config and set password # param $1: password config file # return 0(success),1(fail) host_config_password() { local config_file_name=$1 local root_pwd="" if [ ! -e $config_file_name ];then log_error "no such file ${config_file_name}" return 1 fi root_pwd=`cat $config_file_name | grep pwd_info | awk -F, '{print $1}'` root_pwd=`expr "$root_pwd" : '.*guest_pwd=\(.*\).*'` if [ ! $root_pwd ];then log_error "root_pwd is not set" return 1 fi set_root_pwd $root_pwd return $? } update_vmconfig(){ local exit_code=1; #Clean the environment test -z "${g_temp_dir}" && return 1 log_info "clear dir ${g_temp_dir}" rm -rf "${g_temp_dir}"/* 2>/dev/null exit_code=$? if [ ${exit_code} -ne 0 ];then log_error "clear dir $g_temp_dir...failed" return 1 fi create_dir "${g_temp_dir}" exit_code=$? if [ ${exit_code} -ne 0 ];then log_error "create dir $g_temp_dir...failed" return 1 fi #Download the vmconfig file. get_vm_config_file "${g_vm_config_file_name}" "${g_temp_dir}/${g_vm_config_file_name}" exit_code=$? if [ ${exit_code} -eq 1 ];then log_error "get_vm_config_file failed!" return 1 fi return 0 } # 检测密码是否改变 # param: none # return 0(changed),1(not changed) check_passwd_config_changed(){ local vmconfig_file="${g_temp_dir}/${g_vm_config_file_name}" local password_file="${g_temp_dir}/${g_password_config_file_name}.readfile" local cur_password_file="${shell_dir}/${g_password_config_file_name}" if [ ! -e $vmconfig_file ];then log_error "no such file ${vmconfig_file}" return 1 fi echo "" > ${password_file} cat ${vmconfig_file} |awk 'BEGIN{RS="\n\n"}NR==1{print}' |grep pwd_info >> ${password_file} diff ${cur_password_file} ${password_file} >/dev/null 2>&1 if [ $? -ne 0 ];then if [ -e ${cur_password_file} ];then unlink ${cur_password_file} fi log_info "${password_file} and ${cur_password_file} is diff" cp ${password_file} ${cur_password_file} return 0 fi return 1 } # 检测hostname配置是否改变 # param: none # return 0(changed),1(not changed) check_hostname_config_changed(){ local vmconfig_file="${g_temp_dir}/${g_vm_config_file_name}" local hostname_file="${g_temp_dir}/${g_hostname_config_file_name}.readfile" local cur_hostname_file="${shell_dir}/${g_hostname_config_file_name}" if [ ! -e $vmconfig_file ];then log_error "no such file ${vmconfig_file}" return 1 fi echo "" > ${hostname_file} #get hostname config cat ${vmconfig_file} |awk 'BEGIN{RS="\n\n"}NR==1{print}' |grep hostname >> ${hostname_file} diff ${cur_hostname_file} ${hostname_file} >/dev/null 2>&1 if [ $? -ne 0 ];then if [ -e ${cur_hostname_file} ];then unlink ${cur_hostname_file} fi cp ${hostname_file} ${cur_hostname_file} log_info "${hostname_file} and ${cur_hostname_file} is diff" return 0 fi return 1 } #检测该虚拟机为克隆的虚拟机还是模板虚拟机 check_clone_or_mould_changed() { local vmconfig_file="${g_temp_dir}/${g_vm_config_file_name}" local reset_duid_file="${g_temp_dir}/${g_reset_duid_file_name}.readfile" local cur_reset_duid_file="${shell_dir}/${g_reset_duid_file_name}" if [ ! -e $vmconfig_file ];then log_error "no such file ${vmconfig_file}" return 1 fi echo "" > $reset_duid_file cat $vmconfig_file | awk 'BEGIN{RS="\n\n"}NR==1{print}' |grep "reset_duid_count" >> ${reset_duid_file} diff ${cur_reset_duid_file} ${reset_duid_file} >/dev/null 2>&1 if [ $? -ne 0 ];then if [ -e $cur_reset_duid_file ];then unlink $cur_reset_duid_file fi cp $reset_duid_file $cur_reset_duid_file log_info "$reset_duid_file and $cur_reset_duid_file is diff" return 0 fi return 1 } #克隆虚拟机或者模板虚拟机获取duid时,需要先删除文件dhclient6.leases,再获取duid del_dhclient6_file() { local config_type=0 local duid_dhclient6_file="" local duid_file="" local dhclient_dir="/var/lib" local suse_dhclient6_file="/var/lib/dhcp6/dhclient.leases" get_network_config_type config_type=$? if [ $config_type -eq $REDHAT_SERIES_CONFIG_TYPE ] || [ $config_type -eq $DEBIAN_SERIES_CONFIG_TYPE ];then duid_dhclient6_file=`find $dhclient_dir -name dhclient6.leases 2>/dev/null` if [ x"$duid_dhclient6_file" != x"" ];then for duid_file in ${duid_dhclient6_file} do rm -f $duid_file done log_info "clone linux del dhclient6 file $duid_dhclient6_file" fi elif [ $config_type -eq $SUSE_SERIES_CONFIG_TYPE ];then if [ -e $suse_dhclient6_file ];then rm -f $suse_dhclient6_file log_info "clone suse linux del dhclient6 file $suse_dhclient6_file" fi else log_error "invalid config type $config_type" return 1 fi } # Fix TD 92167 获取当前网卡和上次网卡之间的变化信息 # param: $1 新获取的网卡信息文件 # param: $2 当前系统正在使用的网卡信息文件 # param: $3 网卡信息变化信息的保存文件 # return 返回0表示没有发生变化,1表示发生了变化 get_network_config_changed(){ local new_network_file=$1 local cur_network_file=$2 local changed_network_file=$3 local ip_type=$4 local ip_net="" local has_changed="" if [ -z $new_network_file ] || [ -z $cur_network_file ] || [ -z $changed_network_file ];then log_error "[get_network_config_changed]Param err" return 0 fi #如果当前网卡信息文件不存在,打印日志.当做网卡信息没有变化 if [ ! -e $new_network_file ];then log_error "[get_network_config_changed]no find cur network info file: ${new_network_file}" return 0 fi #如果上次的网卡信息文件不存在,打印日志。当做网卡信息全部有变化 if [ ! -e $cur_network_file ];then log_warn "[get_network_config_changed]no find last network info file: ${cur_network_file}" cp $new_network_file $changed_network_file return 1 fi ip_net=`trans_ip_type "$ip_type"` if [ x"$ip_net" == x"" ];then log_error "get_network_config_changed no ip_type $ip_type ip_net $ip_net" return 0 fi sort "$new_network_file" -o "$new_network_file" if [ $? -ne 0 ]; then log_error "failed to sort file ($new_network_file)." return 0 fi sort "$cur_network_file" -o "$cur_network_file" if [ $? -ne 0 ]; then log_error "failed to sort file ($cur_network_file)." return 0 fi #检查上次网卡信息和当前网卡信息是否发生变化 has_changed=$(diff $new_network_file $cur_network_file) if [ x"$has_changed" != x"" ];then #只获取修改或者新增的网卡信息 diff $new_network_file $cur_network_file | grep ${ip_net} | grep '<' |awk -F '<' '{print $2}' |sed 's/^\s*//g' > $changed_network_file return 1 fi return 0 } # 检查虚拟机配置文件是否无效 # 这里通过检查虚拟机配置文件如果没有 memory 字眼,我们就认为虚拟机配置无效 # return 1表示valid,0表示not valid check_vmconf_valid() { local vmconfig_file=$1 if [ ! -f $vmconfig_file ];then log_error "[check_vmconf_valid] Not valid: no such file ${vmconfig_file}" return 0 fi local has_memory=$(cat $vmconfig_file |grep memory) if [ x"$has_memory" == x"" ];then log_error "[check_vmconf_valid] Not valid: no find memory: ${vmconfig_file}" return 0 fi return 1 } # 检测网络配置是否改变 # param: $1 ip_type ipv6 or ipv4 # return 0(changed),1(not changed) check_network_config_changed(){ local vmconfig_file="${g_temp_dir}/${g_vm_config_file_name}" local ip_type=$1 local ip_net="" local network_file="" local cur_network_file="" local changed_network_file="" if [ x"$ip_type" == x"ipv4" ];then ip_net="ip_net" network_file="${g_temp_dir}/${g_network_config_file_name}.readfile" cur_network_file="${shell_dir}/${g_network_config_file_name}" changed_network_file="${g_temp_dir}/${g_network_config_file_name}.changed" elif [ x"$ip_type" == x"ipv6" ];then ip_net="ip6_net" network_file="${g_temp_dir}/${g_ipv6_network_config_file_name}.readfile" cur_network_file="${shell_dir}/${g_ipv6_network_config_file_name}" changed_network_file="${g_temp_dir}/${g_ipv6_network_config_file_name}.changed" else log_error "ip_type $ip_type is not ipv4 or ipv6" return 1 fi if [ ! -e $vmconfig_file ];then log_error "no such file ${vmconfig_file}" return 1 fi echo "" > ${network_file} cat ${vmconfig_file} |awk 'BEGIN{RS="\n\n"}NR==1{print}' |grep ${ip_net} >> ${network_file} #获取那些网卡的信息发生了变化,后面设置只对发生变化的网卡设置IP即可 get_network_config_changed "${network_file}" "${cur_network_file}" "${changed_network_file}" "$ip_type" if [ $? -ne 0 ];then if [ -e ${cur_network_file}} ];then unlink ${cur_network_file} fi log_info "${network_file} and ${cur_network_file} is diff" cp ${network_file} ${cur_network_file} return 0 fi return 1 } check_networkmanager_support_manage() { main_version=`NetworkManager --version|awk -F. '{print $1}'` test -z "$main_version" && return 1 if [ $main_version -ne 0 ];then return 0 fi sub_version=`NetworkManager --version|awk -F. '{if(NF>2) print $2"."$3}'` test -z "$sub_version" && return 1 log_debug "main=$main_version,sub=$sub_version" if [ `echo "$sub_version >= 9.8"| bc` -eq 1 ];then return 0 else return 1 fi } # param $1: config_type # return 0(success),1(fail) restart_network() { local config_type=$1 log_info "begin restart network,type=$config_type" if [ $config_type -eq $REDHAT_SERIES_CONFIG_TYPE ];then /etc/init.d/network restart >/dev/null 2>&1 elif [ $config_type -eq $DEBIAN_SERIES_CONFIG_TYPE ];then ifdown -a >/dev/null 2>&1 ifup -a >/dev/null 2>&1 elif [ $config_type -eq $SUSE_SERIES_CONFIG_TYPE ];then service network restart >/dev/null 2>&1 else log_error "invalid config type $config_type" return 1 fi log_info "restart network...ok" return 0 } network_manager_handle() { local config_type=$1 local res1="" local res2="" log_info "network_manager_handle,type=$config_type" if [ $config_type -eq $DEBIAN_SERIES_CONFIG_TYPE ];then #如果安装了NetworkManager,需要取消接管 if [ -d "/etc/NetworkManager" ];then log_debug "##handle NetworkManager disable NetworkManager" disable_network_manager killall -9 NetworkManager >/dev/null 2>&1 fi #Fix TD 87612, 有些版本的NetworkManager启用存在下面两个问题: # 1、可能存在多个网卡设置同个IP的问题。 # 原因是我们虚拟化工具设置IP配置文件时候,没有设置HWADDR字段。存在问题的版本是centos6.x。例如centos6.5。 # 2: 链路不通的网卡,IP设置不上去的问题。但如果链路通的时候 NetwokManager 会重新自动设置上去。存在问题的版本是centos7.1。 #经过讨论,这里设置网卡配置时候添加HWADDR字段,NetwokManager服务就不停用了。所以这里注释掉 elif [ $config_type -eq $REDHAT_SERIES_CONFIG_TYPE ];then # TD 2019120600142,redhat系统7.2之前的版本(桌面版),networkmanger服务与network两个网络管理冲突,链路再次开启时,导致v4失效 res1=`cat /etc/redhat-release | grep -e "release 6"` if [ x"" != x"${res1}" ];then log_info "## stop network manager..." service NetworkManager stop >/dev/null 2>&1 chkconfig NetworkManager off >/dev/null 2>&1 fi res2=`cat /etc/redhat-release | grep -e "release 7.1" -e "release 7.0"` if [ x"" != x"${res2}" ];then log_info "## stop network manager..." systemctl stop NetworkManager >/dev/null 2>&1 systemctl disable NetworkManager >/dev/null 2>&1 fi else log_debug "not need to handle networkmanager $config_type" return fi log_info "networkmanager handle...ok" } #配置dns dns_config() { local dns_list="" local config_type=0 local net_dns_list="" local config_dns_list="" local write_resolv_dns_list="" config_dns_list=`save_new_dns_file` if [ x"$config_dns_list" == x"" ];then return 1 fi # 获取network的类型 get_network_config_type config_type=$? # 清理一次网卡中的dns,并且保存网卡中的dns net_dns_list=`clear_net_config_dns "$config_type"` # 将下发配置中的dns和网卡中的dns进行拼接取前三个 write_resolv_dns_list=`joint_config_net_dns "$config_dns_list" "$net_dns_list"` # 配置dns update_resolv_dns "$write_resolv_dns_list" "$config_type" } # 更新resolv文件中的dns update_resolv_dns() { local dns_list=$1 local config_type=$2 local debian_dns_cfg_file="" test -z "$dns_list" && return 1 if [ $config_type -eq $REDHAT_SERIES_CONFIG_TYPE ];then update_linux_dns "$dns_list" "$g_dns_config_file" log_info "redhat dns $dns_list config" elif [ $config_type -eq $DEBIAN_SERIES_CONFIG_TYPE ];then debian_dns_cfg_file=`check_debian_dns_file` update_linux_dns "$dns_list" "$debian_dns_cfg_file" log_info "debian dns $dns_list config" elif [ $config_type -eq $SUSE_SERIES_CONFIG_TYPE ];then update_suse_dns "$dns_list" log_info "suse dns $dns_list config" else log_error "error,system is not support." return 1 fi } #判断debian的dns配置文件 check_debian_dns_file() { local debian_dns_cfg_file="/etc/resolvconf/resolv.conf.d/base" if [ -e $debian_dns_cfg_file ];then echo "$debian_dns_cfg_file" return 0 else echo "$g_dns_config_file" return 0 fi return 1 } # 清理一次网卡中的dns clear_net_config_dns() { local flag="" # 判断是否处理过网卡中的dns,处理过之后不再处理 flag=`cat $shell_dir/network_compat_flag 2>/dev/null | grep "reset_dns" | awk -F= '{print $2}'` if [ x"$flag" == x"1" ];then log_info "ifcfg dns has already been cleared" return 1 fi local config_type=$1 local net_name=`get_net_name` local dns_list="" test -z "$net_name" && return 1 if [ $config_type -eq $REDHAT_SERIES_CONFIG_TYPE ];then dns_list=`clear_redhat_net_config_dns "$net_name"` log_info "clear redhat $net_name config" elif [ $config_type -eq $DEBIAN_SERIES_CONFIG_TYPE ];then dns_list=`clear_debian_net_config_dns "$net_name"` log_info "clear debian $net_name config" else return 1 fi echo "reset_dns=1" >> $shell_dir/network_compat_flag 2>/dev/null echo "$dns_list" return 0 } #dns排序 save_new_dns_file() { local vmconfig_file="${g_temp_dir}/${g_vm_config_file_name}" local dns_file="${g_temp_dir}/${g_vm_dns_config_file_name}.readfile" local config_net_num=0 local net_num=0 local v4_dns="" local v6_dns="" local ip_net="" local ip6_net="" local config_dns_list="" local three_dns="" local if_ipnet_exist="" local dns_list="" local all_ifname_dns="" local cur_ip6_config="" local cur_ip_config="" if [ ! -e $vmconfig_file ];then log_error "no such file ${vmconfig_file}" return 1 fi echo "" > ${dns_file} cat ${vmconfig_file} |awk 'BEGIN{RS="\n\n"}NR==1{print}' |grep ip >> ${dns_file} # 获取配置中网卡数 config_net_num=`cat ${dns_file} | grep ip_net | wc -l` # 获取ipv4的网卡信息 cur_ip_config=`cat ${dns_file} | grep ip_net` # 获取ipv6的网卡信息 cur_ip6_config=`cat ${dns_file} | grep ip6_net` rm -f $dns_file 2>/dev/null while [[ ${config_net_num} -gt 0 ]] do ip_net="ip_net$net_num" if_ipnet_exist=$(echo $cur_ip_config | grep "$ip_net") # 判断ip_net0等是否存在,不存在则加1(eg:ip_net0,ip_net1,ip_net2,可能存在ip_net1不存在的情况) if [ -z $if_ipnet_exist ];then net_num=`expr $net_num + 1` continue fi # 获取v4的dns v4_dns=`from_config_get_dns "$ip_net" "$cur_ip_config"` ip6_net="ip6_net$net_num" # 获取v6的dns v6_dns=`from_config_get_dns "$ip6_net" "$cur_ip6_config"` #判断dns是否为空并且拼接 dns_list=`joint_dns "$v4_dns" "$v6_dns"` if [ x"$dns_list" != x"" ];then all_ifname_dns=`joint_dns "$all_ifname_dns" "$dns_list"` fi net_num=`expr $net_num + 1` config_net_num=`expr $config_net_num - 1` done #获取前面三个dns three_dns=`get_three_dns "$all_ifname_dns"` echo $three_dns return 0 } #从配置中获取dns from_config_get_dns() { local ip_net=$1 local cur_ip_config=$2 test -z "$cur_ip_config" && return 1 dns1=`echo "$cur_ip_config" | grep $ip_net | awk -F, '{print $5}' | awk -F= '{print $2}'` dns2=`echo "$cur_ip_config" | grep $ip_net | awk -F, '{print $6}' | awk -F= '{print $2}'` dns_list=`joint_dns "$dns1" "$dns2"` echo "$dns_list" return 0 } # 检测网络是否正常 # param $1: "${network_config_file}" # param $2: "$ip_type" ipv4 or ipv6 network_diagnose() { local file_name=$1 local ip_type=$2 local ip_tmp="" local macaddr="" local ifname="" local ip="" local mask="" local gateway="" local config_type=0 local valid_ifname_list=() log_info "##begin network diagnose" get_network_config_type config_type=$? if [ $config_type -eq 255 ]; then log_error "error,system is not support" return 1 fi if [ ! -e ${file_name} ];then log_error "no such file ${file_name}" return 1 fi ip_tmp=`trans_ip_type "$ip_type"` if [ x"$ip_tmp" == x"" ];then log_error "no ip_type $ip_type ip_tmp $ip_tmp" return 1 fi #format: ip_net1:net1=FE:FC:FE:13:B9:01,ip=192.168.1.10,mask=255.255.255.0,gateway=192.168.1.1,dns1=8.8.8.8,dns2= for i in `cat ${file_name} |awk 'BEGIN{RS="\n\n"}NR==1{print}' |grep ${ip_tmp} |awk '{print $2}'` do macaddr=`echo $i|awk -F, '{print $1}'|awk -F= '{print $2}'` if [ x"${macaddr}" == x"" ];then log_error "invalid macaddr" continue fi ifname=$(get_ifname_by_mac ${macaddr}) if [ x"${ifname}" == x"" ];then log_error "invalid ifname" continue fi ip=`echo $i|awk -F, '{print $2}'|awk -F= '{print $2}'` mask=`echo $i|awk -F, '{print $3}'|awk -F= '{print $2}'` gateway=`echo $i|awk -F, '{print $4}'|awk -F= '{print $2}'` #判断ip与网关是否改变 check_if_config_changed "$ip_type" "$ifname" "$ip" "$mask" "$gateway" if [ $? -eq 0 ];then log_warn "$ip_type is diff, valid ifname list=${valid_ifname_list}" valid_ifname_list=("${valid_ifname_list[@]}" "$ifname") fi done if [ -n "$valid_ifname_list" ];then log_warn "$ip_type is diff, restart network again" # 重启网络 restart_network "$config_type" fi return 0 } # return: 1(redhat series system) # 2(debian series system) # 3(suse series system) # 255(not support) get_network_config_type() { #redhat series cat /proc/version | grep -i "Red Hat" >/dev/null 2>&1 if [ $? -eq 0 ]; then log_info "redhat system" return $REDHAT_SERIES_CONFIG_TYPE fi #debian series cat /proc/version | grep -i "ubuntu" >/dev/null 2>&1 if [ $? -eq 0 ]; then log_info "ubuntu system" return $DEBIAN_SERIES_CONFIG_TYPE fi cat /proc/version | grep -i "suse" >/dev/null 2>&1 if [ $? -eq 0 ]; then log_info "suse system" return $SUSE_SERIES_CONFIG_TYPE fi #get type by check network config dir or file if [ -d $g_redhat_network_cfg_dir ];then log_info "redhat series system" return $REDHAT_SERIES_CONFIG_TYPE fi if [ -d $g_debian_network_cfg_dir ];then log_info "debian series system" return $DEBIAN_SERIES_CONFIG_TYPE fi if [ -d $g_suse_network_cfg_dir ];then log_info "redhat series system" return $SUSE_SERIES_CONFIG_TYPE fi log_error "system is not support." return 255 } # 判断ip等信息的是否为空 # param $1: ipaddr,eg:192.168.100.100 # param $2: netmask,eg:255.255.255.0 # param $3: gateway,eg:192.168.100.1 # param $4: "$ip_type" ipv4 or ipv6 # return 0(success),1(fail) address_validate() { local ipaddr=$1 local netmask=$2 local gateway=$3 local ip_type=$4 local parameter_num=1 check_iptype_is_ipv4_or_ipv6 "$ip_type" if [ $? -eq 1 ]; then log_error "ip_type $ip_type is not ipv4 or ipv6" return 1 fi if ! test -z "$ipaddr";then if [ x"$netmask" == x"" ];then log_error "netmask is none" return 1 fi else if [ x"$netmask" != x"" ] || [ x"$gateway" != x"" ];then log_error "without ip,netmask and gateway should not be none{$netmask,$gateway}" return 1 fi fi for addr in $@; do if [ x"$addr" == x"$ip_type" ];then continue fi if [ x"$addr" != x"" ];then if [ x"$ip_type" == x"ipv4" ];then is_valid_inet_address $addr if [ $? -ne 0 ];then #日志错误打印:ipv4参数1代表ipaddr,参数2代表netmask,参数3代表gateway log_error "ipv4 parameters $parameter_num is invalid inet addr {$addr}" return 1 fi elif [ x"$ip_type" == x"ipv6" ];then if [ x"$addr" == x"$netmask" ];then is_valid_inet6_prefixlen "$netmask" if [ $? -ne 0 ];then log_error "netmask $netmask is invalid" return 1 fi else is_valid_inet6_address "$addr" if [ $? -ne 0 ];then #日志错误打印:ipv6参数1代表ipaddr,参数3代表gateway log_error "ipv6 parameters $parameter_num is invalid inet addr {$addr}" return 1 fi fi fi fi parameter_num=`expr $parameter_num + 1` done return 0 } reset_network_compat_flag() { if [ -f $shell_dir/network_compat_flag ];then echo "" > "$shell_dir/network_compat_flag" 2>/dev/null return 0 fi } # config network # param $1: ifname,eg:eth0 # param $2: ip,eg:192.168.100.100 # param $3: net mask,eg:255.255.255.0 # param $4: gateway,eg:192.168.100.1 # param $5: system network config type 1(redhat) 2(debian) 3(suse) # param $6: ip类型 ipv6 or ipv4 # param $7: macaddr, eg:FE:FC:FE:66:82:31. Fix TD87612,Centos6.5的NetworkManager根据macaddr设置指定网卡IP,如果不设置可能导致其他网卡都设置为同一个IP # return 0(success),1(fail) config_network() { local ifname=$1 local ipaddr=$2 local netmask=$3 local gateway=$4 local config_type=$5 local ip_type=$6 local macaddr=$7 if [ x"${ifname}" == x"" ];then log_error "invalid ifname" return 1 fi if [ $config_type -eq $REDHAT_SERIES_CONFIG_TYPE ];then # 为适配老版本网络问题,在配置ip时进行适配处理,启动配置ip处理一次 redhat_network_cfg_handle_once redhat_network_cfg_handle "$ifname" "$ipaddr" "$netmask" "$gateway" "$ip_type" "$macaddr" log_info "redhat config" elif [ $config_type -eq $DEBIAN_SERIES_CONFIG_TYPE ];then debian_network_cfg_handle "$ifname" "$ipaddr" "$netmask" "$gateway" "$ip_type" log_info "debian config" elif [ $config_type -eq $SUSE_SERIES_CONFIG_TYPE ];then suse_network_cfg_handle "$ifname" "$ipaddr" "$netmask" "$gateway" "$ip_type" log_info "suse config" else log_error "error,system is not support." return 1 fi # sync防止修改配置的时候断电丢数据 log_info "sync data" sync return 0 } # param $1: network config file # param $2: ip_type # return 0(success),1(fail) host_config_network() { local file_name=$1 local ip_type=$2 local ip_tmp="" local macaddr="" local ifname="" local ip="" local mask="" local gateway="" local config_type=0 local valid_ifname_list=() get_network_config_type config_type=$? if [ $config_type -eq 255 ]; then log_error "error,system is not support" return 1 fi if [ ! -e ${file_name} ];then log_error "no such file ${file_name}" return 1 fi ip_tmp=`trans_ip_type "$ip_type"` if [ x"$ip_tmp" == x"" ];then log_error "host_config_network no ip_type $ip_type ip_tmp $ip_tmp" return 1 fi #format: ip_net1:net1=FE:FC:FE:13:B9:01,ip=192.168.1.10,mask=255.255.255.0,gateway=192.168.1.1,dns1=8.8.8.8,dns2= for i in `cat ${file_name} |awk 'BEGIN{RS="\n\n"}NR==1{print}' |grep "${ip_tmp}" |awk '{print $2}'` do log_info "[host_config_network]read $ip_type from file ${file_name}: $i" macaddr=`echo $i|awk -F, '{print $1}'|awk -F= '{print $2}'` if [ x"${macaddr}" == x"" ];then log_error "invalid macaddr" continue fi ifname=$(get_ifname_by_mac ${macaddr}) if [ x"${ifname}" == x"" ];then log_error "invalid ifname" continue fi ip=`echo $i|awk -F, '{print $2}'|awk -F= '{print $2}'` mask=`echo $i|awk -F, '{print $3}'|awk -F= '{print $2}'` gateway=`echo $i|awk -F, '{print $4}'|awk -F= '{print $2}'` #判断ip等信息是否为空 address_validate "$ip" "$mask" "$gateway" "$ip_type" if [ $? -ne 0 ]; then log_error "address is invalid {$ip,$mask,$gateway,$ip_type}" continue fi # 判断ip等信息是否改变,如果改变加入改变列表中 check_if_config_changed "$ip_type" "$ifname" "$ip" "$mask" "$gateway" if [ $? -eq 0 ]; then config_network "$ifname" "$ip" "$mask" "$gateway" "$config_type" "$ip_type" "$macaddr" ifconfig_interfaces "$ifname" "$ip" "$mask" "$gateway" "$ip_type" valid_ifname_list=("${valid_ifname_list[@]}" "$ifname") fi done log_info "valid ifname list=${valid_ifname_list}" test -z "$valid_ifname_list" && return 0 # 需要等写入配置文件后操作networkmanager,$config_type类型有suse、debian、redhat network_manager_handle "$config_type" return 0 } #使用临时命令使ip生效 # param $1: ifname,eg:eth0 # param $2: ipaddr,eg:2001:d02::14:0:0:9 # param $3: netmask,eg:64 # param $4: gateway,eg:2001:d02::14:0:0:9 # param $5: ip_type ip类型 ipv4 or ipv6 # return 0(success),1(fail) ifconfig_interfaces() { local ifname=$1 local ipaddr=$2 local netmask=$3 local gateway=$4 local ip_type=$5 local metric="" local mask="" check_iptype_is_ipv4_or_ipv6 "$ip_type" if [ $? -eq 1 ]; then log_error "ip_type $ip_type is not ipv4 or ipv6" return 1 fi metric=`get_metric "$ifname"` if [ x"$ip_type" == x"ipv4" ];then if [ x"$ipaddr" != x"" ] && [ x"$netmask" != x"" ];then log_info "ip addr $ip_type $ipaddr" delete_old_ipv4 "$ifname" mask=`mask2cdr "$netmask"` if [ x"$mask" != x"" ];then ip addr add $ipaddr/$mask dev $ifname 2>/dev/null fi fi if [ x"$gateway" != x"" ];then log_info "$ip_type add route $gateway $metric" if [ x"$metric" != x"" ];then route add default gw $gateway metric $metric dev $ifname 2>/dev/null else route add default gw $gateway dev $ifname 2>/dev/null fi fi elif [ x"$ip_type" == x"ipv6" ];then if [ x"$ipaddr" != x"" ] && [ x"$netmask" != x"" ];then log_info "ip addr $ip_type $ipaddr" delete_old_ipv6 "$ifname" ip addr add $ipaddr/$netmask dev $ifname 2>/dev/null fi if [ x"$gateway" != x"" ];then log_info "$ip_type add route $gateway $metric" if [ x"$metric" != x"" ];then route -A inet6 add default gw $gateway metric $metric dev $ifname 2>/dev/null else route -A inet6 add default gw $gateway dev $ifname 2>/dev/null fi fi fi } #获取metric get_metric() { local ifname=$1 local metric="" metric=`cat /proc/net/dev | sed -n "/$ifname:/="` if ! test -z $metric;then metric="10$metric" fi echo "$metric" } #删除指定网卡的ipv4地址等信息 delete_old_ipv4() { local ifname=$1 local get_ipv4="" get_ipv4=`ip addr show dev ${ifname} 2>/dev/null | awk '{if($1~/^inet$/)print $2}'` for ipv4 in $get_ipv4;do if [ x"$ipv4" != x"" ];then log_info "ip addr $ip_type del $get_ipv4" ip addr del $ipv4 dev $ifname 2>/dev/null fi done } #删除指定网卡的ipv6地址等信息 delete_old_ipv6() { local ifname=$1 local get_ipv6="" get_ipv6=`ip addr show dev ${ifname} 2>/dev/null | awk '{if($1~/^inet6$/)print $2}' | sed '/^fe80/d'` for ipv6 in $get_ipv6;do if [ x"$ipv6" != x"" ];then log_info "ip addr $ip_type del $get_ipv6" ip addr del $get_ipv6 dev $ifname 2>/dev/null fi done } #判断linux系统是否开启v6协议,未开启则开启 check_linux_is_support_v6() { local support_v6_all=0 local support_v6_default=0 local support_v6_lo=0 local support_v6_cfg_file="/etc/sysctl.conf" local file_is_net6="" if [ ! -f "$support_v6_cfg_file" ]; then touch "$support_v6_cfg_file" fi support_v6_all=`sysctl -a 2>/dev/null |grep "net.ipv6.conf.all.disable_ipv6" |awk -F= '{if (NR==1) print $2}'` support_v6_default=`sysctl -a 2>/dev/null |grep "net.ipv6.conf.default.disable_ipv6" |awk -F= '{if (NR==1) print $2}'` support_v6_lo=`sysctl -a 2>/dev/null |grep "net.ipv6.conf.lo.disable_ipv6" |awk -F= '{if (NR==1) print $2}'` if [ $support_v6_all -eq 1 ] || [ $support_v6_default -eq 1 ] || [ $support_v6_lo -eq 1 ];then file_is_net6=`cat $support_v6_cfg_file | grep "net.ipv6.conf"` if [ x"$file_is_net6" != x"" ];then sed -i "/^net.ipv6.conf/d" $support_v6_cfg_file fi echo "net.ipv6.conf.all.disable_ipv6 = 0" >> $support_v6_cfg_file echo "net.ipv6.conf.default.disable_ipv6 = 0" >> $support_v6_cfg_file echo "net.ipv6.conf.lo.disable_ipv6 = 0" >> $support_v6_cfg_file sysctl -p 2>/dev/null fi return 0 } # networkconfig # param $1: ip类型 ipv4 or ipv6 # param $2: ip_net or ip6_net 改变的配置文件 network_config() { local ip_type=$1 local network_config_file=$2 if [ x"$ip_type" == x"ipv6" ];then check_linux_is_support_v6 fi host_config_network "${network_config_file}" "$ip_type" # 检测网络是否正常 while true do #防止超时 watchdog_check_once network_diagnose "${network_config_file}" "$ip_type" if [ $? -eq 0 ] || [ $check_network_count -ge $g_vmconfig_check_count ];then check_network_count=0 log_info "diagnose network.......ok" echo "1" > "$g_current_dir/update_network.flag" break fi sleep ${g_vmconfig_check_time} check_network_count=`expr $check_network_count + 1` log_info "diagnose count = $check_network_count" done } #编辑虚拟机立即设置网络 immediately_set_network() { local ip_changed=0 log "start immediately_set_network!" update_vmconfig if [ $? -ne 0 ];then log "update_vmconfig failed" return 1 fi log "start immediately_set_network! check_network_config_changed" if [ "$1" == "dns" ]; then #dns配置 dns_config else check_network_config_changed "ipv4" if [ $? -eq 0 ];then ip_changed=1 network_config "ipv4" "${g_temp_dir}/${g_network_config_file_name}.changed" fi check_network_config_changed "ipv6" if [ $? -eq 0 ];then ip_changed=1 network_config "ipv6" "${g_temp_dir}/${g_ipv6_network_config_file_name}.changed" fi if [ $ip_changed -eq 1 ];then #dns配置 dns_config fi fi log "immediately_set_network! end" return 0 }
Upload File
Create Folder