From b8d45a8e7b3c0dce80947366a003731f5092325d Mon Sep 17 00:00:00 2001 From: bybai Date: Tue, 12 Jan 2016 02:36:36 -0500 Subject: [PATCH] enhance nicutils.sh --- xCAT/postscripts/nicutils.sh | 1384 ++++++++++++++++++++++++++++++++++ 1 file changed, 1384 insertions(+) create mode 100755 xCAT/postscripts/nicutils.sh diff --git a/xCAT/postscripts/nicutils.sh b/xCAT/postscripts/nicutils.sh new file mode 100755 index 000000000..2abdb7b7c --- /dev/null +++ b/xCAT/postscripts/nicutils.sh @@ -0,0 +1,1384 @@ +#!/bin/bash +# +#This is network lib for confignetwork to configure bond/vlan/bridge + +awk="awk" +sed="sed" +cut="cut" +sleep="sleep" +sort="sort" +ps="ps" +head="head" +readlink="readlink" +basename="basename" +udevadm="udevadm" +touch="touch" +tail="tail" +dmesg="dmesg" +grep="grep" +lspci="lspci" +ifup="ifup" +ifdown="ifdown" +nmcli="nmcli" +dirname="dirname" +ip="ip" +ifconfig="ifconfig" +brctl="brctl" +uniq="uniq" +xargs="xargs" + +###################################################### +# +# log lines +# +# input : info or warn or error or status +# +# output : multiple lines of string +# +###################################################### +function log_lines { + local pcnt=$# + local __level=$1 + shift + local cmd=log_${__level} + + local hit=0 + local OIFS=$IFS + local NIFS=$'\n' + IFS=$NIFS + local __msg + for __msg in $* + do + IFS=$OIFS + $cmd "$__msg" + hit=1 + IFS=$NIFS + done + IFS=$OIFS + + [ $hit -eq 0 -a $pcnt -le 1 ] && \ + while read __msg; + do + $cmd "$__msg" + done +} + +###################################################### +# +# error information +# +# input : string +# +# output : [E]: message +# return : 0 +# +###################################################### +function log_error { + local __msg="$*" + $log_print_cmd $log_print_arg "[E]: $__msg" + return 0 +} + +###################################################### +# +# warning information +# +# input : string +# +# output : [W]: message +# return : 0 +# +###################################################### +function log_warn { + local __msg="$*" + $log_print_cmd $log_print_arg "[W]: $__msg" + return 0 +} + +###################################################### +# +# log information +# +# input : string +# +# output : [I]: message +# return : 0 +# +###################################################### +function log_info { + local __msg="$*" + $log_print_cmd $log_print_arg "[I]: $__msg" + return 0 +} + +#################################################### +# +# print command status +# +# input : message +# +# output : [S]: message +# +################################################### +__my_log_status= +function log_status { + local __msg="$*" + $log_print_cmd $log_print_arg "[S]: $__msg" + + # call my_log_status hook to triger more processing for status messages. + if [ -n "$__my_log_status" ]; then + $__my_log_status "$__msg" + fi + return 0 +} + +#################################################### +# +# print output +# +################################################### +function log_print_default { + printf "%s\n" "$*" +} + +#################################################### +# +# handle command +# +################################################### +function set_log_print { + local cmd="$1" + if [ -z "$cmd" ]; then + return 1 + fi + shift + local args="$*" + + eval "log_print_cmd=\"$cmd\"" + eval "log_print_arg=\"$args\"" +} + +# let log work +[ -z "$log_print" ] && set_log_print "log_print_default" + +##################################################### +# +# uniq line in cfg files +# +# input : -t"str_for_FS" -k"num" +# +# output : text have no duplicate linkes +# +##################################################### +function uniq_per_key { + local fs="" + local keyno=0 + + local opt + while getopts "t:k:" opt; + do + case $opt in + t) fs="$OPTARG";; + k) keyno="$OPTARG";; + esac + done + shift $(($OPTIND - 1)) + + $awk ${fs:+"-F"}"$fs" -v keyno=$keyno ' + BEGIN { cnt=0; } + { + if(!($keyno in keya)) { + keya[$keyno]=cnt; + cnt+=1; + }; + idx=keya[$keyno]; + vala[idx]=$0; + } + END { + for(i = 0; i < cnt; i++) { + print vala[i]; + }; + }' +} + +################################################################################## +# +# load kernel module +# +# input : module= retry= interval=