Просмотр исходного кода

removing ifconfig for RHEL7 support

Serghey Rodin 11 лет назад
Родитель
Сommit
9de2d15c62
5 измененных файлов с 107 добавлено и 49 удалено
  1. 18 11
      bin/v-add-sys-ip
  2. 30 28
      bin/v-delete-sys-ip
  3. 8 6
      bin/v-update-sys-ip
  4. 50 3
      func/ip.sh
  5. 1 1
      func/main.sh

+ 18 - 11
bin/v-add-sys-ip

@@ -1,6 +1,6 @@
 #!/bin/bash
 # info: add system ip address
-# options: IP MASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]
+# options: IP NETMASK [INTERFACE] [USER] [STATUS] [NAME] [NAT_IP]
 #
 # The function adds ip address into a system. It also creates rc scripts. You
 # can specify ip name which will be used as root domain for temporary aliases.
@@ -16,7 +16,7 @@
 
 # Argument defenition
 ip=${1// /}
-mask=$2
+netmask=$2
 interface="${3-eth0}"
 user="${4-admin}"
 ip_status="${5-shared}"
@@ -34,8 +34,8 @@ source $VESTA/conf/vesta.conf
 #                    Verifications                         #
 #----------------------------------------------------------#
 
-check_args '2' "$#" 'IP MASK [INTERFACE] [USER] [IP_STATUS] [IP_NAME] [NAT_IP]'
-validate_format 'ip' 'mask' 'interface' 'user' 'ip_status'
+check_args '2' "$#" 'IP NETMASK [INTERFACE] [USER] [STATUS] [NAME] [NAT_IP]'
+validate_format 'ip' 'netmask' 'interface' 'user' 'ip_status'
 is_ip_free
 is_object_valid 'user' 'USER' "$user"
 is_object_unsuspended 'user' 'USER' "$user"
@@ -50,11 +50,18 @@ fi
 #----------------------------------------------------------#
 #                       Action                             #
 #----------------------------------------------------------#
-get_ip_iface
-sys_ip_check=$(/sbin/ifconfig | grep "addr:$ip ")
+
+# Converting netmask to CIDR format and calculating broadcast address
+cidr=$(convert_netmask $netmask)
+broadcast=$(get_broadcast $ip $netmask)
+iface=$(get_ip_iface)
+
+sys_ip_check=$(/sbin/ip addr | grep "$ip/$cidr")
 if [ -z "$sys_ip_check" ]; then
-    # Adding sys ip
-    /sbin/ifconfig "$iface" "$ip" netmask "$mask"
+
+    # Adding system ip
+    /sbin/ip addr add $ip/$cidr broadcast $broadcast \
+        dev $interface label $iface
 
     # Adding RHEL/CentOS/Fedora startup script
     if [ -e "/etc/redhat-release" ]; then
@@ -63,7 +70,7 @@ if [ -z "$sys_ip_check" ]; then
         sys_ip="$sys_ip\nBOOTPROTO=static"
         sys_ip="$sys_ip\nONBOOT=yes"
         sys_ip="$sys_ip\nIPADDR=$ip"
-        sys_ip="$sys_ip\nNETMASK=$mask"
+        sys_ip="$sys_ip\nNETMASK=$netmask"
         echo -e $sys_ip > /etc/sysconfig/network-scripts/ifcfg-$iface
     fi
 
@@ -73,7 +80,7 @@ if [ -z "$sys_ip_check" ]; then
         sys_ip="$sys_ip\nauto $iface"
         sys_ip="$sys_ip\niface $iface inet static"
         sys_ip="$sys_ip\naddress $ip"
-        sys_ip="$sys_ip\nnetmask $mask"
+        sys_ip="$sys_ip\nnetmask $netmask"
         echo -e $sys_ip >> /etc/network/interfaces
     fi
 fi
@@ -85,7 +92,7 @@ NAME='$ip_name'
 U_SYS_USERS=''
 U_WEB_DOMAINS='0'
 INTERFACE='$interface'
-NETMASK='$mask'
+NETMASK='$netmask'
 NAT='$nat_ip'
 TIME='$TIME'
 DATE='$DATE'" > $VESTA/data/ips/$ip

+ 30 - 28
bin/v-delete-sys-ip

@@ -35,33 +35,40 @@ is_ip_key_empty '$U_SYS_USERS'
 #                       Action                             #
 #----------------------------------------------------------#
 
-# Get ip owner
-user="$(get_ip_value '$OWNER')"
-ip_status="$(get_ip_value '$STATUS')"
+# Import ip variables
+source $VESTA/data/ips/$ip
+cidr=$(convert_netmask $NETMASK)
 
-# Deleting interface
-interface=$(/sbin/ifconfig | grep -B1 "dr:$ip " | head -n1 | cut -f1 -d \ )
+# Checking main ip on the interface
+interface=$(/sbin/ip addr | grep "$ip/$cidr" | awk '{print $NF}')
 if [ ! -z "$interface" ] && [ -z "$(echo $interface |cut -s -f2 -d :)" ]; then
     echo "Error: can't delete main IP address"
     log_event "$E_FORBIDEN" "$EVENT"
     exit $E_FORBIDEN
 fi
-if [ ! -z "$interface" ]; then
-    /sbin/ifconfig $interface down
 
-    # Deleting startup conf on RHEL/CentOS/Fedora
-    if [ -e "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then
-        rm -f /etc/sysconfig/network-scripts/ifcfg-$interface
+# Deleting system ip
+if [ ! -z "$interface" ]; then
+    /sbin/ip addr del $ip/$cidr dev $INTERFACE
+    if [ "$?" -ne 0 ]; then
+        echo "Error: can't delete system ip"
+        log_event "$E_FORBIDEN" "$EVENT"
+        exit $E_FORBIDEN
     fi
+fi
 
-    # Deleting startup conf on Debian/Ubuntu
-    if [ -e "/etc/network/interfaces" ]; then
-        ip_str=$(grep -n $ip$ /etc/network/interfaces |cut -f1 -d:)
-        if [ ! -z "$ip_str" ]; then
-            first_str=$((ip_str - 3))
-            last_str=$((ip_str + 1))
-            sed -i "$first_str,$last_str d" /etc/network/interfaces
-        fi
+# Deleting startup conf on RHEL/CentOS/Fedora
+if [ -e "/etc/sysconfig/network-scripts/ifcfg-$interface" ]; then
+    rm -f /etc/sysconfig/network-scripts/ifcfg-$interface
+fi
+
+# Deleting startup conf on Debian/Ubuntu
+if [ -e "/etc/network/interfaces" ]; then
+    ip_str=$(grep -n $ip$ /etc/network/interfaces |cut -f1 -d:)
+    if [ ! -z "$ip_str" ]; then
+        first_str=$((ip_str - 3))
+        last_str=$((ip_str + 1))
+        sed -i "$first_str,$last_str d" /etc/network/interfaces
     fi
 fi
 
@@ -92,7 +99,6 @@ if [ ! -z "$PROXY_SYSTEM" ]; then
         new_ips=$(echo "$rpaf_str" | sed "s/$ip//")
         sed -i "s/$ips/$new_ips/g" $rpaf_conf
     fi
-
 fi
 
 
@@ -101,24 +107,20 @@ fi
 #----------------------------------------------------------#
 
 # Updating user conf
-if [ ! -z "$user" ]; then
-    decrease_user_value "$user" '$IP_OWNED'
+if [ ! -z "$OWNER" ]; then
+    decrease_user_value "$OWNER" '$IP_OWNED'
 fi
 
-if [ "$user" = 'admin' ]; then
-    if [ "$ip_status" = 'shared' ]; then
+if [ "$OWNER" = 'admin' ]; then
+    if [ "$STATUS" = 'shared' ]; then
         for user in $(ls $VESTA/data/users/); do
             decrease_user_value "$user" '$IP_AVAIL'
         done
-    else
-        decrease_user_value 'admin' '$IP_AVAIL'
     fi
 else
-    decrease_user_value "$user" '$IP_AVAIL'
-    decrease_user_value 'admin' '$IP_AVAIL'
+    decrease_user_value "$OWNER" '$IP_AVAIL'
 fi
 
-
 # Adding task to the vesta pipe
 $BIN/v-restart-web
 if [ $? -ne 0 ]; then

+ 8 - 6
bin/v-update-sys-ip

@@ -18,6 +18,7 @@ ip_status=${2-shared}
 # Includes
 source /etc/profile.d/vesta.sh
 source $VESTA/func/main.sh
+source $VESTA/func/ip.sh
 source $VESTA/conf/vesta.conf
 
 
@@ -35,8 +36,8 @@ is_object_valid 'user' 'USER' "$user" "$user"
 #----------------------------------------------------------#
 
 # Get list of ip addresses
-ip_list=$(/sbin/ifconfig | grep 'inet addr:' | cut -f 2 -d : | \
-    cut -f 1 -d ' '| grep -v 127.0.0.1 | grep -v "^0.0.0.")
+ip_list=$(/sbin/ip addr |grep "inet "|grep -v "host lo" |awk '{print $2}')
+ip_list=$(echo "$ip_list"|cut -f 1 -d /)
 ip_num=$(echo "$ip_list" | wc -l)
 
 # WorkAround for DHCP IP address
@@ -88,10 +89,11 @@ fi
 # Compare ips
 for ip in $ip_list; do
     if [ ! -e "$VESTA/data/ips/$ip" ]; then
-        iface=$(/sbin/ifconfig |grep -B1 -w $ip |head -n1 |cut -f1 -d ' ')
-        interface=$(echo "$iface" | cut -f 1 -d :)
-        mask=$(/sbin/ifconfig |grep -w $ip |awk -F "Mask:" '{print $2}')
-        $BIN/v-add-sys-ip $ip $mask $interface
+        interface=$(/sbin/ip addr |grep $ip |awk '{print $NF}')
+        interface=$(echo $interface |cut -f 1 -d :)
+        netmask=$(/sbin/ip addr |grep $ip |awk '{print $2}' |cut -f 2 -d /)
+        netmask=$(convert_cidr $netmask)
+        $BIN/v-add-sys-ip $ip $netmask $interface
     fi
 done
 

+ 50 - 3
func/ip.sh

@@ -54,14 +54,14 @@ is_ip_free() {
 
 # Get full interface name
 get_ip_iface() {
-    i=$(/sbin/ifconfig -a |grep -w "$interface"|cut -f1 -d ' '|\
-        tail -n 1|cut -f 2 -d :)
+    i=$(/sbin/ip addr | grep -w $interface |\
+         awk '{print $NF}' | tail -n 1 | cut -f 2 -d :)
     if [ "$i" = "$interface" ]; then
         n=0
     else
         n=$((i + 1))
     fi
-    iface="$interface:$n"
+    echo "$interface:$n"
 }
 
 
@@ -201,3 +201,50 @@ get_user_ip(){
     fi
     echo "$ip"
 }
+
+# Convert CIDR to netmask
+convert_cidr() {
+    set -- $(( 5 - ($1 / 8) )) 255 255 255 255 \
+        $(((255 << (8 - ($1 % 8))) & 255 )) 0 0 0
+    if [[ $1 -gt 1 ]]; then
+        shift $1
+    else
+        shift
+    fi
+    echo ${1-0}.${2-0}.${3-0}.${4-0}
+}
+
+# Convert netmask to CIDR
+convert_netmask() {
+    nbits=0
+    IFS=.
+    for dec in $1 ; do
+        case $dec in
+            255) let nbits+=8;;
+            254) let nbits+=7;;
+            252) let nbits+=6;;
+            248) let nbits+=5;;
+            240) let nbits+=4;;
+            224) let nbits+=3;;
+            192) let nbits+=2;;
+            128) let nbits+=1;;
+            0);;
+        esac
+    done
+    echo "$nbits"
+}
+
+# Calculate broadcast address
+get_broadcast() {
+    OLD_IFS=$IFS
+    IFS=.
+    typeset -a I=($1)
+    typeset -a N=($2)
+    IFS=$OLD_IFS
+
+    echo "$((${I[0]} |\
+        (255 ^ ${N[0]}))).$((${I[1]} |\
+        (255 ^ ${N[1]}))).$((${I[2]} |\
+        (255 ^ ${N[2]}))).$((${I[3]} |\
+        (255 ^ ${N[3]})))"
+}

+ 1 - 1
func/main.sh

@@ -905,11 +905,11 @@ validate_format(){
             key)            validate_format_username "$arg" "$arg_name" ;;
             lname)          validate_format_name_s "$arg" "$arg_name" ;;
             malias)         validate_format_username "$arg" "$arg_name" ;;
-            mask)           validate_format_ip "$arg" ;;
             max_db)         validate_format_int "$arg" 'max db';;
             min)            validate_format_mhdmw "$arg" $arg_name ;;
             month)          validate_format_mhdmw "$arg" $arg_name ;;
             nat_ip)         validate_format_ip "$arg" ;;
+            netmask)        validate_format_ip "$arg" ;;
             newid)          validate_format_int "$arg" 'id' ;;
             ns1)            validate_format_domain "$arg" 'name_server';;
             ns2)            validate_format_domain "$arg" 'name_server';;