Centos Set Macvlan NIC script

作者: admin 分类: docker 发布时间: 2018-09-08 23:27 ė 6 没有评论

Centos Set Macvlan NIC script
macvlan使用桥的模式,默认是不可以和容器进行通信,但是我们可以把活动网卡也加入到桥中,这样宿主机和容器就可以进行通信,下面是设置脚本在centos7下面使用

#!/bin/bash
ifcfg_macvlan=/etc/sysconfig/network-scripts/ifcfg-macvlan
ifdown_macvlan=/etc/sysconfig/network-scripts/ifdown-macvlan
ifup_macvlan=/etc/sysconfig/network-scripts/ifup-macvlan
#ip address
IpAddr=`ip route get 8.8.8.8 |awk '{print $7'}`
#gateway
GateWay=`ip route get 8.8.8.8 |awk '{print $3'}`
#network card name
NetworkCard=`ip route get 8.8.8.8 |awk '{print $5'}`
ifctg_network_card=/etc/sysconfig/network-scripts/ifcfg-$NetworkCard
#netmask
Prefix=`ip addr | grep inet | grep -v 127.0.0.1 |grep -v 172.17.0.1 |awk {'print $2'}| awk -F '/' {'print $2'}`
#valid ip address func
function Valid_Ip()
{
  local ip=$1
  local stat=1
  if [[ $ip =~ ^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$ ]]; then
      OIFS=$IFS
      IFS='.'
      ip=($ip)
      IFS=$OIFS
      [[ ${ip[0]} -le 255 && ${ip[1]} -le 255 && ${ip[2]} -le 255 && ${ip[3]} -le 255 ]]
      stat=$?
  fi
  return $stat
}

#vaild ip address
if Valid_Ip $IpAddr;then
     echo "Ip Address: $IpAddr"
else
     echo "Error Ip Address "
     exit
fi

if Valid_Ip $GateWay;then
    echo "GateWay: $GateWay"
else
    echo "Error GateWay"
    exit
fi

#Deciding whether to count
expr $Prefix "+" 10 &> /dev/null
if [ $? -eq 0 ];then
  echo "Netmask: $Prefix"
else
  echo "Error Netmask"
  exit
fi

#Network card name
echo "Network Card Name: $NetworkCard"
if [ $NetworkCard = "macvlan" ]; then
   echo "Error Network Card Name: $NetworkCard"
   exit
else
   echo "Network Card Name: $NetworkCard"
fi

#create ifcfg-macvlan
echo "start create ifcfg-macvlan.."
echo "DEVICE=macvlan
NAME=macvlan
BOOTPROTO=none
ONBOOT=yes
TYPE=macvlan
DEVICETYPE=macvlan
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
PREFIX=$Prefix
GATEWAY=$GateWay
IPADDR=$IpAddr
DOMAIN=tendcloud.com
MACVLAN_PARENT=$NetworkCard
MACVLAN_MODE=bridge"
> $ifcfg_macvlan

if [ $? -ne 0 ]; then
   echo "create ifcfg-macvlan failed"
else
   echo "create ifcfg-macvlan succeed"
fi
#create ifdown-macvlan
echo "start create ifdown-macvlan.."
echo "#!/bin/bash
. /etc/init.d/functions

cd /etc/sysconfig/network-scripts
. ./network-functions

[ -f ../network ] && . ../network

CONFIG=\${1}

need_config \${CONFIG}

source_config

OTHERSCRIPT="
/etc/sysconfig/network-scripts/ifdown-\${REAL_DEVICETYPE}"

if [ ! -x \${OTHERSCRIPT} ]; then
        OTHERSCRIPT="
/etc/sysconfig/network-scripts/ifdown-eth"
fi

\${OTHERSCRIPT} \${CONFIG}

ip link del \${DEVICE} type \${TYPE:-macvlan}"
> $ifdown_macvlan

if [ $? -ne 0 ]; then
   echo "create ifdown-macvlan failed"
else
   echo "create ifdown-macvlan succeed"
fi
#create ifup-macvlan
echo "start ctrate ifup-macvlan.."
echo "#!/bin/bash
. /etc/init.d/functions


cd /etc/sysconfig/network-scripts
. ./network-functions

[ -f ../network ] && . ../network

CONFIG=\${1}

need_config \${CONFIG}

source_config

OTHERSCRIPT="
/etc/sysconfig/network-scripts/ifup-\${REAL_DEVICETYPE}"

if [ ! -x \${OTHERSCRIPT} ]; then
        OTHERSCRIPT="
/etc/sysconfig/network-scripts/ifup-eth"
fi

ip link add link $NetworkCard name macvlan  type macvlan  mode bridge

\${OTHERSCRIPT} \${CONFIG}"
> $ifup_macvlan

if [ $? -ne 0 ]; then
   echo "create ifup-macvlan failed"
   exit
else
   echo "create ifup-macvlan succeed"
fi

#update localhost network card config
echo "update local network card config"
echo "DEVICE=$NetworkCard
TYPE=Ethernet
NAME=$NetworkCard
BOOTPROTO=none
ONBOOT=yes"
> $ifctg_network_card
echo $ifctg_network_card
if [ $? -ne 0 ]; then
   echo "update localhost network card faild"
   exit
else
   echo "update localhost network card  succeed"
fi

chmod +x $ifdown_macvlan
chmod +x $ifup_macvlan

如果你需要购买新疆阿克苏冰糖心苹果/新疆库尔勒香梨,我们自己家种的,一件发货,全国包邮,比超市便宜,坏了给赔、从库尔勒铁门关和阿克苏红旗坡发货,有需要扫码下方我的二维码

本文出自 小Q,转载时请注明出处及相应链接。

本文永久链接: http://www.linuxqq.com/archives/1932.html

0
更多
Ɣ回顶部