ipw 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. #!/bin/bash
  2. # Copyright (C) 2013 Artyom A Anikeev <anikeev@ut.mephi.ru> 0xB5385841,
  3. # Dmitry Yu Okunev <dyokunev@ut.mephi.ru> 0x8E30679C
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. CMD="$1"; shift;
  18. DEFAULT_BONDING_MODE='balance-tlb'
  19. modprobe bonding
  20. eerror() {
  21. echo "$@" >&2
  22. }
  23. newconfig_open() {
  24. echo -n > "$NEWCONFIG_TMPPATH"
  25. }
  26. newconfig_write() {
  27. LINE="$1"; shift;
  28. [[ "$NEWCONFIG" -eq 0 ]] && return
  29. echo "$LINE" >> "$NEWCONFIG_TMPPATH"
  30. }
  31. newconfig_commit() {
  32. rm -f "$CONFIG_PATH"
  33. mv "$NEWCONFIG_TMPPATH" "$CONFIG_PATH"
  34. }
  35. newconfig_push() {
  36. BUFNUM="$1"; shift;
  37. LINE="$1"; shift;
  38. BUF[$BUFNUM]="${BUF[$BUFNUM]}
  39. $LINE"
  40. }
  41. newconfig_pushpopall() {
  42. BUFNUMTO="$1"; shift;
  43. BUFNUMFROM="$1"; shift;
  44. newconfig_push "$BUFNUMTO" "${BUF[$BUFNUMFROM]}"
  45. newconfig_popall "$BUFNUMFROM"
  46. }
  47. newconfig_popall() {
  48. BUFNUM="$1"; shift;
  49. BUF[$BUFNUM]=""
  50. }
  51. newconfig_popallwrite() {
  52. BUFNUM="$1"; shift;
  53. newconfig_write "${BUF[$BUFNUM]}"
  54. BUF[$BUFNUM]=""
  55. }
  56. # INPUT: 10.2:thor.ext; OUTPUT: environments: VLAN, SUBVLAN, BRNAME
  57. parse_vlan_subvlan_bridge() {
  58. VLAN_N_BRIDGE="$1"; shift
  59. if [[ "${VLAN_N_BRIDGE:0:1}" = ":" ]]; then
  60. VLAN=""
  61. SUBVLAN=""
  62. VLANNAME=""
  63. BRNAME="${VLAN_N_BRIDGE:1}"
  64. else
  65. # Splitting by ":"
  66. WORDS=( ${VLAN_N_BRIDGE//[:]/ } )
  67. # The second word defines interface name. If it's empty, "vlan$VLAN.$SUBVLAN" is used.
  68. VLANNAME="${WORDS[0]}"
  69. BRNAME="${WORDS[1]}"
  70. # Splitting by "."
  71. WORDS=( ${VLANNAME//[.]/ } )
  72. # The second word defines subvlan (1q-in-1q) if it's used. It's empty if no subvlan is used.
  73. VLAN="${WORDS[0]}"
  74. SUBVLAN="${WORDS[1]}"
  75. fi
  76. # Checking if no "vlan(.subvlan)?" is set
  77. if ! [[ "${VLAN_N_BRIDGE: -1}" = ":" ]]; then
  78. # Setting default values
  79. [[ "$SUBVLAN" = "" ]] && BRNAME="${BRNAME:-vlan${VLAN}}" || BRNAME="${BRNAME:-vlan${VLAN}.${SUBVLAN}}"
  80. fi
  81. return;
  82. }
  83. declare -A get_brname_byslave_assoc
  84. for VLAN_N_BRIDGE in $(find /sys/devices/virtual/net -wholename '*/brif/*' | awk -F '/' '{print $8":"$6}'); do
  85. oldIFS="$IFS"; IFS=':'; words=($VLAN_N_BRIDGE); IFS="$oldIFS"
  86. get_brname_byslave_assoc[${words[0]}]=${words[1]}
  87. done
  88. get_brname_byslave() {
  89. _SLAVE="$1"; shift
  90. echo ${get_brname_byslave_assoc[$_SLAVE]}
  91. }
  92. get_vlan_n_bridges() {
  93. awk '{if(NR>2) if( system( "[ -d /sys/devices/virtual/net/*/brif/"$1" ] " ) != 0 ) print $1":"}' < /proc/1/net/vlan/config
  94. find /sys/devices/virtual/net -wholename '*/brif' -empty | awk -F '/' '{print ":"$6}'
  95. find /sys/devices/virtual/net -wholename '*/brif/*' | awk -F '/' '{print $8":"$6}'
  96. }
  97. iface_isexists() {
  98. _IFACE="$1"; shift;
  99. [ -e /sys/devices/virtual/net/"$_IFACE" ]
  100. return $?
  101. }
  102. ip_cmds() {
  103. _IFACE="$1"; shift
  104. ip addr flush dev "$_IFACE"
  105. IP_CMDS_VARIABLE="${_IFACE//[.]/_}_IP"
  106. IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
  107. for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
  108. [[ "$IP_CMD" != "" ]] &&
  109. ip $IP_CMD dev "$_IFACE"
  110. done
  111. }
  112. case "$CMD" in
  113. down)
  114. IPW_CONFIG_PATH=/dev/null
  115. CMD='fix'
  116. ;;
  117. esac
  118. IPW_CONFIG_PATH="${IPW_CONFIG_PATH:-/etc/ipw.conf}"
  119. BRIDGE_FORWARD_DELAY=0
  120. source "$IPW_CONFIG_PATH"
  121. NEWCONFIG_TMPPATH="/tmp/.~ipw"
  122. NEWCONFIG=0
  123. case "$CMD" in
  124. add1qif) #allow to add slave interface in bonding
  125. NEWCONFIG=1 # we need to update the config after the command
  126. NEW_D1Q_IFACE="$1"; shift
  127. NEW_SLAVE="$1"; shift
  128. # checking for duplicated slaves or d1q_ifaces
  129. D1Q_IFACE_USED=0
  130. for D1Q_IFACE in ${D1Q_IFACES[@]}; do
  131. if [[ "$D1Q_IFACE" = "$NEW_D1Q_IFACE" ]]; then
  132. D1Q_IFACE_USED=1
  133. fi
  134. SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
  135. SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
  136. for SLAVE in "${!SLAVES_VARIABLE}"; do
  137. if [[ "$SLAVE" = "$NEW_SLAVE" ]]; then
  138. eerror "This interface have already been enslaved"
  139. exit -1
  140. fi
  141. done
  142. done
  143. if [[ $D1Q_IFACE_USED -eq 0 ]]; then
  144. D1Q_IFACES[ ${#D1Q_IFACES} ]="$NEW_D1Q_IFACE"
  145. fi
  146. SLAVES_VARIABLE="${NEW_D1Q_IFACE}_SLAVES"
  147. SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}"
  148. eval "${SLAVES_VARIABLE}[\${#${SLAVES_VARIABLE}}]=\"$NEW_SLAVE\""
  149. ;;
  150. del1qif)
  151. ;;
  152. addbrif) #allow to add bridge/vlan in bonding
  153. NEWCONFIG=1 # we need to update the config after the command
  154. NEW_D1Q_IFACE="$1"; shift
  155. NEW_VLAN_SUBVLAN="$1"; shift
  156. NEW_BRIDGE="$1"; shift
  157. [[ -z $NEW_BRIDGE ]] && NEW_VLAN_N_BRIDGE="${NEW_VLAN_SUBVLAN}" || NEW_VLAN_N_BRIDGE="${NEW_VLAN_SUBVLAN}:${NEW_BRIDGE}"
  158. D1Q_IFACE_USED=0
  159. for D1Q_IFACE in ${D1Q_IFACES[@]}; do
  160. if [[ "$D1Q_IFACE" = "$NEW_D1Q_IFACE" ]]; then
  161. D1Q_IFACE_USED=1
  162. break;
  163. fi
  164. done
  165. parse_vlan_subvlan_bridge "${NEW_VLAN_N_BRIDGE}"
  166. NEW_VLAN=$VLAN
  167. NEW_SUBVLAN=$SUBVLAN
  168. if [[ $D1Q_IFACE_USED -eq 0 ]]; then
  169. eerror "There's no such interface: $NEW_D1Q_IFACE"
  170. exit -1
  171. fi
  172. VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
  173. VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
  174. for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
  175. parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
  176. if [[ "$VLAN" = "$NEW_VLAN" && "$SUBVLAN" = "$NEW_SUBVLAN" ]]; then
  177. eerror "This vlan is alredy used"
  178. exit -1
  179. fi
  180. done
  181. VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
  182. VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}"
  183. eval "${VLAN_N_BRIDGE_VARIABLE}[\${#${VLAN_N_BRIDGE_VARIABLE}}]=\"$NEW_VLAN_N_BRIDGE\""
  184. #D1Q_IFACES[ ${#D1Q_IFACES}]
  185. ;;
  186. delbrif)
  187. ;;
  188. addbr) #allow to add bridge without interfaces
  189. NEWCONFIG=1 # we need to update the config after the command
  190. NEW_D1Q_IFACE="$1"; shift
  191. NEW_BRIDGE="$1"; shift
  192. VLAN_N_BRIDGE_VARIABLE="${NEW_D1Q_IFACE}_VLAN_N_BRIDGES"
  193. VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}"
  194. eval "${VLAN_N_BRIDGE_VARIABLE}[\${#${VLAN_N_BRIDGE_VARIABLE}}]=\":${NEW_BRIDGE}\""
  195. ;;
  196. delbr)
  197. ;;
  198. addif) #allow to add interface without bridging
  199. ;;
  200. delif)
  201. ;;
  202. addipcmd) #allow to add ip commnds for bridges/vlans
  203. NEWCONFIG=1 # we need to update the config after the command
  204. NEW_BRIDGE="$1"; shift
  205. NEW_IP_CMD="$@"
  206. if [[ "${NEW_BRIDGE}" != "" ]]; then
  207. # Getting commands for "ip"
  208. IP_CMDS_VARIABLE="${NEW_BRIDGE//[.]/_}_IP"
  209. IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}"
  210. eval "${IP_CMDS_VARIABLE}[\${#${IP_CMDS_VARIABLE}}]=\"$NEW_IP_CMD\""
  211. fi
  212. ;;
  213. delipcmd)
  214. NEWCONFIG=1 # we need to update the config after the command
  215. NEW_BRIDGE="$1"; shift
  216. NEW_IP_CMD="$@"
  217. if [[ "${NEW_BRIDGE}" != "" ]]; then
  218. IP_CMDS_VARIABLE="${NEW_BRIDGE//[.]/_}_IP"
  219. IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}"
  220. i=0
  221. CMD_founded=0
  222. for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
  223. if [[ "$IP_CMD" = "$NEW_IP_CMD" ]]; then
  224. CMD_founded=1
  225. else
  226. TMP[$i]=$IP_CMD
  227. (( i++ ))
  228. fi
  229. done
  230. if [[ $CMD_founded -eq 0 ]]; then
  231. eerror "There's no such command"
  232. exit -1
  233. fi
  234. i=0
  235. for IP_CMD in "${!TMP}"; do
  236. IP_CMDS_VARIABLE[$i]=$IP_CMD
  237. (( i++ ))
  238. done
  239. #eval "${IP_CMDS_VARIABLE}[\${#${IP_CMDS_VARIABLE}}]=\"$NEW_IP_CMD\""
  240. #echo $IP_CMDS_VARIABLE
  241. fi ;;
  242. fix) #fix all interfaces to config state
  243. #brctl addif bond0 eth2
  244. #GETTING INFORMATION ABOUT LXC INTERFACES IN BRIDGES
  245. if [[ "$LXC_DIR" != "" ]]; then
  246. LXC_BRIDGE_SLAVES=($(cat /srv/lxc/*/config | tr -d " \t" | awk -F '=' '{if($1=="lxc.network.link") {BRIDGE=$2}; if($1=="lxc.network.veth.pair") {if(BRIDGE!="") {print $2":"BRIDGE; BRIDGE=""}} }'))
  247. fi
  248. #REMOVING UNNECESSARY D1Q
  249. OLD_D1Q_IFACES="`cat /sys/class/net/bonding_masters`"
  250. for OLD_D1Q_IFACE in ${OLD_D1Q_IFACES[@]}; do
  251. D1Q_IS_USED=0
  252. OLD_SLAVES="`cat /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves`"
  253. for D1Q_IFACE in ${D1Q_IFACES[@]}; do
  254. if [[ "$D1Q_IFACE" == "$OLD_D1Q_IFACE" ]]; then #let's kick out useless slaves
  255. SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
  256. SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
  257. for OLD_SLAVE in ${OLD_SLAVES[@]}; do
  258. SLAVE_IS_USED=0
  259. for SLAVE in "${!SLAVES_VARIABLE}"; do
  260. if [[ "$SLAVE" == "$OLD_SLAVE" ]]; then
  261. SLAVE_IS_USED=1
  262. break;
  263. fi
  264. done
  265. if [[ "$SLAVE_IS_USED" == 0 ]]; then
  266. echo "-$OLD_SLAVE" > /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves
  267. fi
  268. done
  269. D1Q_IS_USED=1
  270. break;
  271. fi
  272. done
  273. if [[ "$D1Q_IS_USED" == 0 ]]; then #let's kick out useless d1q
  274. echo "-$OLD_D1Q_IFACE" > /sys/class/net/bonding_masters
  275. fi
  276. done
  277. #REMEMBERING REQUIRED INTERFACES
  278. declare -A REQUIRED_FULLNAME_HASH
  279. declare -A REQUIRED_BRNAME_HASH
  280. declare -A REQUIRED_IFACENAME_HASH
  281. for D1Q_IFACE_TMP in ${D1Q_IFACES[@]}; do #we should keep slave ifaces
  282. VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE_TMP}_VLAN_N_BRIDGES"
  283. VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
  284. for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
  285. parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
  286. REQUIRED_FULLNAME_HASH["$D1Q_IFACE_TMP"."$VLAN_N_BRIDGE"]="$D1Q_IFACE_TMP"."$VLAN_N_BRIDGE"
  287. if [[ "$BRNAME" != "" ]]; then
  288. REQUIRED_BRNAME_HASH["$BRNAME"]="$BRNAME"
  289. fi
  290. if [[ "$VLANNAME" != "" ]]; then
  291. REQUIRED_IFACENAME_HASH["$VLANNAME"]="$VLANNAME"
  292. REQUIRED_IFACENAME_HASH["$D1Q_IFACE_TMP"."$VLAN"]="$D1Q_IFACE_TMP"."$VLAN"
  293. fi
  294. done
  295. done
  296. for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
  297. REQUIRED_FULLNAME_HASH["$LXCIF_N_BRIDGE"]="$LXCIF_N_BRIDGE"
  298. done
  299. #CONFIGURING AND REMEMBERING ACCESS INTERFACES (NOT D1Q)
  300. for ACCESS_IFACE in ${ACCESS_IFACES[@]}; do
  301. oldIFS="$IFS"; IFS=":"
  302. WORDS=(${ACCESS_IFACE})
  303. IFS="$oldIFS"
  304. IFACE="${WORDS[0]}"
  305. BRNAME="${WORDS[1]}"
  306. if [[ "$BRNAME" != "" ]]; then
  307. REQUIRED_BRNAME_HASH["$BRNAME"]="$BRNAME"
  308. if ! iface_isexists "$BRNAME"; then
  309. ip link add "$BRNAME" type bridge
  310. ip link set dev "$BRNAME" up
  311. fi
  312. ip link set "$IFACE" master "$BRNAME"
  313. ip_cmds "$BRNAME"
  314. fi
  315. REQUIRED_IFACENAME_HASH["$IFACE"]="$IFACE"
  316. ip_cmds "$IFACE"
  317. done
  318. #REMOVING UNNECESSARY BRIDGES AND VLANS
  319. OLD_VLAN_N_BRIDGES=(`get_vlan_n_bridges`)
  320. for LINE in ${OLD_VLAN_N_BRIDGES[@]}; do
  321. #echo ITERATION
  322. parse_vlan_subvlan_bridge "$LINE"
  323. OLD_BRNAME="$BRNAME"
  324. OLD_VLANNAME="$VLANNAME"
  325. IS_SLAVE=0
  326. BRIDGE_IN_USE=0
  327. IFACE_IN_USE=0
  328. VLAN_N_BRIDGE_IN_USE=0
  329. if [[ "${REQUIRED_FULLNAME_HASH[$LINE]}" != "" ]]; then
  330. BRIDGE_IN_USE=1
  331. IFACE_IN_USE=1
  332. VLAN_N_BRIDGE_IN_USE=1
  333. else
  334. [[ "$OLD_BRNAME" != "" ]] &&
  335. if [[ "${REQUIRED_BRNAME_HASH[$OLD_BRNAME]}" != "" ]]; then
  336. BRIDGE_IN_USE=1
  337. fi
  338. [[ "$OLD_VLANNAME" != "" ]] &&
  339. if [[ "${REQUIRED_IFACENAME_HASH[$OLD_VLANNAME]}" != "" ]]; then
  340. IFACE_IN_USE=1
  341. fi
  342. fi
  343. if [[ "$BRIDGE_IN_USE" = 0 ]]; then
  344. [[ "$OLD_BRNAME" != '' ]] &&
  345. ip link delete "$OLD_BRNAME"
  346. fi
  347. if [[ "$IFACE_IN_USE" = 0 ]]; then
  348. if [[ "$OLD_VLANNAME" != '' ]]; then
  349. ip link set "$OLD_VLANNAME" nomaster
  350. ip link delete "$OLD_VLANNAME"
  351. fi
  352. fi
  353. done
  354. #CREATING OF NECESSARY D1Q
  355. for D1Q_IFACE in ${D1Q_IFACES[@]}; do
  356. # Fixing bond-ifaces if exists
  357. SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
  358. SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
  359. MODE_VARIABLE="${D1Q_IFACE}_MODE"
  360. MODE="${!MODE_VARIABLE}"; [[ "$MODE" = "" ]] && MODE="$DEFAULT_BONDING_MODE"
  361. D1Q_EXISTS=0
  362. for OLD_D1Q_IFACE in ${OLD_D1Q_IFACES[@]}; do
  363. if [[ "$D1Q_IFACE" == "$OLD_D1Q_IFACE" ]]; then #it's here. Let's fix it.
  364. OLD_SLAVES="`cat /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves`"
  365. OLD_BONDING_MODE=(`cat /sys/class/net/"$OLD_D1Q_IFACE"/bonding/mode`)
  366. if [ "${OLD_BONDING_MODE[0]}" != "${MODE}" -a "${OLD_BONDING_MODE[1]}" != "${MODE}" ]; then
  367. for OLD_SLAVE in ${OLD_SLAVES[@]}; do
  368. echo "-$OLD_SLAVE" > /sys/class/net/"$OLD_D1Q_IFACE"/bonding/slaves
  369. done
  370. ip link set dev "$OLD_D1Q_IFACE" down
  371. echo "$MODE" > /sys/class/net/"$OLD_D1Q_IFACE"/bonding/mode
  372. OLD_SLAVES=()
  373. fi
  374. for SLAVE in "${!SLAVES_VARIABLE}"; do
  375. SLAVE_EXISTS=0
  376. for OLD_SLAVE in ${OLD_SLAVES[@]}; do
  377. if [[ "$SLAVE" == "$OLD_SLAVE" ]]; then
  378. SLAVE_EXISTS=1
  379. break;
  380. fi
  381. done
  382. if [[ "$SLAVE_EXISTS" == 0 ]]; then
  383. ip link set dev "$SLAVE" down #enslaved int should be shutted down
  384. echo "+$SLAVE" > /sys/class/net/"$D1Q_IFACE"/bonding/slaves
  385. fi
  386. done
  387. D1Q_EXISTS=1
  388. break;
  389. fi
  390. done
  391. # Creating bond-ifaces
  392. if [[ "$D1Q_EXISTS" == 0 ]]; then #make all
  393. echo "+$D1Q_IFACE" > /sys/class/net/bonding_masters
  394. echo "$MODE" > /sys/class/net/"$D1Q_IFACE"/bonding/mode
  395. for SLAVE in "${!SLAVES_VARIABLE}"; do
  396. ip link set dev "$SLAVE" down #enslaved int should be shutted down
  397. echo "+$SLAVE" > /sys/class/net/"$D1Q_IFACE"/bonding/slaves
  398. done
  399. fi
  400. ip link set dev "$D1Q_IFACE" up
  401. for SLAVE in "${!SLAVES_VARIABLE}"; do #slave starts after bond
  402. ip link set dev "$SLAVE" up
  403. done
  404. VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE}_VLAN_N_BRIDGES"
  405. VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
  406. #CREATING NECESSARY BRIDGES AND VLANS
  407. for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
  408. parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
  409. FULL_VLAN_NAME="$D1Q_IFACE"."$VLANNAME"
  410. #VLAN STUFF
  411. if [[ "$VLAN" != "" ]]; then
  412. if ! iface_isexists "$FULL_VLAN_NAME"; then #should we create a new vlan?
  413. if [[ "$SUBVLAN" != "" ]]; then #is it subvlan?
  414. FULL_PARVLAN_NAME="$D1Q_IFACE"."$VLAN"
  415. if ! iface_isexists "$FULL_PARVLAN_NAME"; then #should we create a new parent vlan?
  416. ip link add link "$D1Q_IFACE" name "$FULL_PARVLAN_NAME" type vlan id "$VLAN" #vlan in bond
  417. ip link set dev "$FULL_PARVLAN_NAME" up
  418. fi
  419. ip link add link "$FULL_PARVLAN_NAME" name "$FULL_VLAN_NAME" type vlan id "$SUBVLAN" #subvlan in vlan
  420. ip link set dev "$FULL_VLAN_NAME" mtu 1496
  421. ip link set dev "$FULL_VLAN_NAME" up
  422. else
  423. ip link add link "$D1Q_IFACE" name "$FULL_VLAN_NAME" type vlan id "$VLANNAME" #vlan in bond
  424. ip link set dev "$FULL_VLAN_NAME" up
  425. fi
  426. fi
  427. ip_cmds "$FULL_VLAN_NAME"
  428. fi
  429. #BRIDGE STUFF
  430. if [[ "$BRNAME" != "" ]]; then
  431. if ! iface_isexists "$BRNAME"; then #do this bridge exists?
  432. ip link add "$BRNAME" type bridge
  433. echo "$BRIDGE_FORWARD_DELAY" > /sys/devices/virtual/net/"$BRNAME"/bridge/forward_delay
  434. ip link set dev "$BRNAME" up
  435. fi
  436. if [[ "$VLAN" != "" ]]; then
  437. OLD_BRNAME="`get_brname_byslave "$FULL_VLAN_NAME"`"
  438. if [[ "$OLD_BRNAME" = "" ]]; then #is this vlan have been already enslaved?
  439. ip link set "$FULL_VLAN_NAME" master "$BRNAME"
  440. else
  441. if [[ "$BRNAME" != "$OLD_BRNAME" ]]; then
  442. ip link set "$FULL_VLAN_NAME" nomaster
  443. ip link set "$FULL_VLAN_NAME" master "$BRNAME"
  444. fi
  445. fi
  446. fi
  447. ip_cmds "$BRNAME"
  448. fi
  449. done
  450. done
  451. # ADDING LXC IFACES TO BRIDGES IF REQUIRED
  452. for LXCIF_N_BRIDGE in ${LXC_BRIDGE_SLAVES[@]}; do
  453. parse_vlan_subvlan_bridge "$LINE"
  454. brctl addif "$BRNAME" "$VLANNAME" 2>/dev/null
  455. done
  456. ;;
  457. *)
  458. ;;
  459. esac
  460. newconfig_open
  461. newconfig_write "#"
  462. newconfig_write ""
  463. newconfig_write "D1Q_IFACES=("
  464. for D1Q_IFACE in ${D1Q_IFACES[@]}; do
  465. newconfig_write " $D1Q_IFACE"
  466. newconfig_push 128 "${D1Q_IFACE}_SLAVES=("
  467. haveaslave=0
  468. SLAVES_VARIABLE="${D1Q_IFACE}_SLAVES"
  469. SLAVES_VARIABLE="${SLAVES_VARIABLE//[.]/_}[@]"
  470. for SLAVE in "${!SLAVES_VARIABLE}"; do
  471. newconfig_push 128 " $SLAVE";
  472. haveaslave=1
  473. done
  474. newconfig_push 128 ")"
  475. # If there're no slaves then don't write anything about them
  476. [[ $haveaslave -ne 0 ]] && newconfig_pushpopall 1 128 || newconfig_popall 128
  477. VLAN_N_BRIDGE_VARIABLE="${D1Q_IFACE}_VLAN_N_BRIDGES"
  478. VLAN_N_BRIDGE_VARIABLE="${VLAN_N_BRIDGE_VARIABLE//[.]/_}[@]"
  479. newconfig_push 1 "${D1Q_IFACE}_VLAN_N_BRIGES=("
  480. for VLAN_N_BRIDGE in "${!VLAN_N_BRIDGE_VARIABLE}"; do
  481. newconfig_push 1 " ${VLAN_N_BRIDGE}"
  482. parse_vlan_subvlan_bridge "${VLAN_N_BRIDGE}"
  483. # Checking if no "vlan(.subvlan)?" is set
  484. if [[ "${BRNAME}" != "" ]]; then
  485. #echo $BRNAME
  486. # Getting commands for "ip"
  487. IP_CMDS_VARIABLE="${BRNAME//[.]/_}_IP"
  488. newconfig_push 128 "${IP_CMDS_VARIABLE}=("
  489. IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
  490. hasanIP_CMD=0
  491. for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
  492. newconfig_push 128 " \"$IP_CMD\""
  493. #echo ip $IP_CMD dev $BRNAME
  494. hasanIP_CMD=1
  495. done
  496. newconfig_push 128 ")"
  497. newconfig_push 128 ""
  498. [[ $hasanIP_CMD -ne 0 ]] && newconfig_pushpopall 2 128 || newconfig_popall 128
  499. else
  500. # Getting commands for "ip"
  501. #echo OK
  502. IP_CMDS_VARIABLE="vlan${VLANNAME//[.]/_}_IP"
  503. newconfig_push 128 "${IP_CMDS_VARIABLE}=("
  504. IP_CMDS_VARIABLE="${IP_CMDS_VARIABLE}[@]"
  505. hasanIP_CMD=0
  506. for IP_CMD in "${!IP_CMDS_VARIABLE}"; do
  507. newconfig_push 128 " \"$IP_CMD\""
  508. #echo ip $IP_CMD dev $VLANNAME
  509. hasanIP_CMD=1
  510. done
  511. newconfig_push 128 ")"
  512. newconfig_push 128 ""
  513. [[ $hasanIP_CMD -ne 0 ]] && newconfig_pushpopall 2 128 || newconfig_popall 128
  514. fi
  515. printf "br: %-16s vlan:%5s subvlan:%5s\n" "$BRNAME" "$VLAN" "$SUBVLAN"
  516. done
  517. newconfig_push 1 ")"
  518. done
  519. for ACCESS_IFACE in ${ACCESS_IFACES[@]}; do
  520. oldIFS="$IFS"; IFS=":"
  521. WORDS=(${ACCESS_IFACE})
  522. IFS="$oldIFS"
  523. IFACE="${WORDS[0]}"
  524. BRNAME="${WORDS[1]}"
  525. printf "access: %-12s bridge: %-8s\n" "$IFACE" "$BRNAME"
  526. done
  527. newconfig_write ")"
  528. newconfig_popallwrite 1
  529. newconfig_popallwrite 2
  530. newconfig_popallwrite 3
  531. #[[ $NEWCONFIG -ne 0 ]] && newconfig_commit
  532. exit 0