bondvf.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/bin/bash
  2. # This example script creates bonding network devices based on synthetic NIC
  3. # (the virtual network adapter usually provided by Hyper-V) and the matching
  4. # VF NIC (SRIOV virtual function). So the synthetic NIC and VF NIC can
  5. # function as one network device, and fail over to the synthetic NIC if VF is
  6. # down.
  7. #
  8. # Usage:
  9. # - After configured vSwitch and vNIC with SRIOV, start Linux virtual
  10. # machine (VM)
  11. # - Run this scripts on the VM. It will create configuration files in
  12. # distro specific directory.
  13. # - Reboot the VM, so that the bonding config are enabled.
  14. #
  15. # The config files are DHCP by default. You may edit them if you need to change
  16. # to Static IP or change other settings.
  17. #
  18. sysdir=/sys/class/net
  19. netvsc_cls={f8615163-df3e-46c5-913f-f2d2f965ed0e}
  20. bondcnt=0
  21. # Detect Distro
  22. if [ -f /etc/redhat-release ];
  23. then
  24. cfgdir=/etc/sysconfig/network-scripts
  25. distro=redhat
  26. elif grep -q 'Ubuntu' /etc/issue
  27. then
  28. cfgdir=/etc/network
  29. distro=ubuntu
  30. elif grep -q 'SUSE' /etc/issue
  31. then
  32. cfgdir=/etc/sysconfig/network
  33. distro=suse
  34. else
  35. echo "Unsupported Distro"
  36. exit 1
  37. fi
  38. echo Detected Distro: $distro, or compatible
  39. # Get a list of ethernet names
  40. list_eth=(`cd $sysdir && ls -d */ | cut -d/ -f1 | grep -v bond`)
  41. eth_cnt=${#list_eth[@]}
  42. echo List of net devices:
  43. # Get the MAC addresses
  44. for (( i=0; i < $eth_cnt; i++ ))
  45. do
  46. list_mac[$i]=`cat $sysdir/${list_eth[$i]}/address`
  47. echo ${list_eth[$i]}, ${list_mac[$i]}
  48. done
  49. # Find NIC with matching MAC
  50. for (( i=0; i < $eth_cnt-1; i++ ))
  51. do
  52. for (( j=i+1; j < $eth_cnt; j++ ))
  53. do
  54. if [ "${list_mac[$i]}" = "${list_mac[$j]}" ]
  55. then
  56. list_match[$i]=${list_eth[$j]}
  57. break
  58. fi
  59. done
  60. done
  61. function create_eth_cfg_redhat {
  62. local fn=$cfgdir/ifcfg-$1
  63. rm -f $fn
  64. echo DEVICE=$1 >>$fn
  65. echo TYPE=Ethernet >>$fn
  66. echo BOOTPROTO=none >>$fn
  67. echo ONBOOT=yes >>$fn
  68. echo NM_CONTROLLED=no >>$fn
  69. echo PEERDNS=yes >>$fn
  70. echo IPV6INIT=yes >>$fn
  71. echo MASTER=$2 >>$fn
  72. echo SLAVE=yes >>$fn
  73. }
  74. function create_eth_cfg_pri_redhat {
  75. create_eth_cfg_redhat $1 $2
  76. }
  77. function create_bond_cfg_redhat {
  78. local fn=$cfgdir/ifcfg-$1
  79. rm -f $fn
  80. echo DEVICE=$1 >>$fn
  81. echo TYPE=Bond >>$fn
  82. echo BOOTPROTO=dhcp >>$fn
  83. echo ONBOOT=yes >>$fn
  84. echo NM_CONTROLLED=no >>$fn
  85. echo PEERDNS=yes >>$fn
  86. echo IPV6INIT=yes >>$fn
  87. echo BONDING_MASTER=yes >>$fn
  88. echo BONDING_OPTS=\"mode=active-backup miimon=100 primary=$2\" >>$fn
  89. }
  90. function create_eth_cfg_ubuntu {
  91. local fn=$cfgdir/interfaces
  92. echo $'\n'auto $1 >>$fn
  93. echo iface $1 inet manual >>$fn
  94. echo bond-master $2 >>$fn
  95. }
  96. function create_eth_cfg_pri_ubuntu {
  97. local fn=$cfgdir/interfaces
  98. create_eth_cfg_ubuntu $1 $2
  99. echo bond-primary $1 >>$fn
  100. }
  101. function create_bond_cfg_ubuntu {
  102. local fn=$cfgdir/interfaces
  103. echo $'\n'auto $1 >>$fn
  104. echo iface $1 inet dhcp >>$fn
  105. echo bond-mode active-backup >>$fn
  106. echo bond-miimon 100 >>$fn
  107. echo bond-slaves none >>$fn
  108. }
  109. function create_eth_cfg_suse {
  110. local fn=$cfgdir/ifcfg-$1
  111. rm -f $fn
  112. echo BOOTPROTO=none >>$fn
  113. echo STARTMODE=auto >>$fn
  114. }
  115. function create_eth_cfg_pri_suse {
  116. create_eth_cfg_suse $1
  117. }
  118. function create_bond_cfg_suse {
  119. local fn=$cfgdir/ifcfg-$1
  120. rm -f $fn
  121. echo BOOTPROTO=dhcp >>$fn
  122. echo STARTMODE=auto >>$fn
  123. echo BONDING_MASTER=yes >>$fn
  124. echo BONDING_SLAVE_0=$2 >>$fn
  125. echo BONDING_SLAVE_1=$3 >>$fn
  126. echo BONDING_MODULE_OPTS=\'mode=active-backup miimon=100 primary=$2\' >>$fn
  127. }
  128. function create_bond {
  129. local bondname=bond$bondcnt
  130. local primary
  131. local secondary
  132. local class_id1=`cat $sysdir/$1/device/class_id 2>/dev/null`
  133. local class_id2=`cat $sysdir/$2/device/class_id 2>/dev/null`
  134. if [ "$class_id1" = "$netvsc_cls" ]
  135. then
  136. primary=$2
  137. secondary=$1
  138. elif [ "$class_id2" = "$netvsc_cls" ]
  139. then
  140. primary=$1
  141. secondary=$2
  142. else
  143. return 0
  144. fi
  145. echo $'\nBond name:' $bondname
  146. echo configuring $primary
  147. create_eth_cfg_pri_$distro $primary $bondname
  148. echo configuring $secondary
  149. create_eth_cfg_$distro $secondary $bondname
  150. echo creating: $bondname with primary slave: $primary
  151. create_bond_cfg_$distro $bondname $primary $secondary
  152. let bondcnt=bondcnt+1
  153. }
  154. for (( i=0; i < $eth_cnt-1; i++ ))
  155. do
  156. if [ -n "${list_match[$i]}" ]
  157. then
  158. create_bond ${list_eth[$i]} ${list_match[$i]}
  159. fi
  160. done