]> git.ipfire.org Git - people/stevee/network.git/blob - hooks/bonding
network: New package.
[people/stevee/network.git] / hooks / bonding
1 #!/bin/sh
2 ########################################################################
3 # Begin $NETWORK_DEVICES/services/bonding
4 #
5 # Description : Bonding Script
6 #
7 # Authors : Michael Tremer - michael.tremer@ipfire.org
8 #
9 # Version : 00.00
10 #
11 # Notes : This script adds bonding support.
12 #
13 ########################################################################
14
15 . /lib/network/hook-header
16
17 HOOK_NAME=bonding
18 HOOK_TYPE=port
19 HOOK_PRIO=50
20
21 DEFAULT_MODE=
22
23 function port_name() {
24 echo "${zone}t+"
25 }
26
27 case "${action}" in
28 help)
29 ;;
30
31 info)
32 echo "HOOK_NAME=${HOOK_NAME}"
33 echo "HOOK_TYPE=${HOOK_TYPE}"
34 ;;
35
36 pre-up)
37 if ! grep -q ^bonding /proc/modules; then
38 modprobe bonding
39 echo "-bond0" > /sys/class/net/bonding_masters
40 fi
41
42 if device_exists ${MAC}; then
43 device=$(devicify ${MAC})
44 if ! device_is_bonding ${device}; then
45 log_failure_msg "Device \"${device}\" is up, but not a bonding device."
46 exit ${EXIT_ERR}
47 fi
48 exit ${EXIT_OK}
49 fi
50
51 device=$(device_get_free $(port_name))
52 echo "+${device}" > /sys/class/net/bonding_masters
53 ip link set ${device} address ${MAC}
54
55 [ -n "${MODE}" ] && \
56 echo "${MODE}" > /sys/class/net/${device}/bonding/mode
57
58 echo "${MIIMON-100}" > /sys/class/net/${device}/bonding/miimon
59
60 for slave in ${SLAVES}; do
61 if device_exists ${slave}; then
62 if device_is_up ${slave}; then
63 log_warning_msg "Cannot enslave device \"${slave}\"."
64 continue
65 fi
66 device_rename "$(devicify ${slave})" "${device}s+"
67 echo "+$(devicify ${slave})" > /sys/class/net/${device}/bonding/slaves
68 else
69 log_warning_msg "Device ${slave} does not exist."
70 fi
71 done
72
73 ip link set ${device} up
74
75 log_success_msg "Setting up trunk ${MAC}..."
76 ;;
77
78 post-up)
79 device=$(devicify ${MAC})
80 if ! zone_has_device_attached ${zone} ${device}; then
81 zone_add_port ${zone} ${device}
82 fi
83 ;;
84
85 pre-down)
86 device=$(devicify ${MAC})
87 if zone_has_device_attached ${zone} ${device}; then
88 zone_del_port ${zone} ${device}
89 fi
90 ;;
91
92 post-down)
93 device=$(devicify ${MAC})
94 if port_is_up ${device}; then
95 MESSAGE="Pulling down trunk ${MAC}..."
96 ip link set ${device} down
97 evaluate_retval
98 echo "-${device}" > /sys/class/net/bonding_masters
99 fi
100 ;;
101
102 add)
103 MAC=$(mac_generate)
104 MODE=${DEFAULT_MODE}
105
106 while [ $# -gt 0 ]; do
107 case "${1}" in
108 --mac=*)
109 MAC=${1#--mac=}
110 ;;
111 --mode=*)
112 MODE=${1#--mode=}
113 ;;
114 *)
115 SLAVES="${SLAVES} $(macify ${1})"
116 ;;
117 esac
118 shift
119 done
120
121 UUID=$(uuid)
122 cat <<EOF > ${CONFIG_UUIDS}/${UUID}
123 HOOK="${HOOK_NAME}"
124 MAC="${MAC}"
125 MODE="${MODE}"
126 SLAVES="$(echo ${SLAVES})"
127 EOF
128 ln -sf ${CONFIG_UUIDS}/${UUID} \
129 ${CONFIG_ZONES}/${zone}/${HOOK_NAME}-${UUID}
130
131 log_success_msg "Configuration successfully saved!"
132 echo " MAC address : ${MAC}"
133 echo " Mode : ${MODE}"
134 echo " Slaves : $(echo ${SLAVES})"
135 ;;
136
137 rem)
138 ;;
139
140 status)
141 DEVICE=$(devicify ${MAC})
142 echo -e "# ${CLR_BOLD_CYN}Trunk ${DEVICE} (${MAC})${NORMAL}"
143 if device_is_up ${MAC}; then
144 echo -e "# State: ${CLR_BOLD_GRN}up${NORMAL}"
145 echo "#"
146 for slave in $(</sys/class/net/${DEVICE}/bonding/slaves); do
147 echo -e "# ${CLR_BOLD_CYN}Slave port ${slave}${NORMAL}"
148
149 echo -n "# State: "
150 if device_is_up ${slave}; then
151 echo -e "${CLR_BOLD_GRN}up${NORMAL}"
152 else
153 echo -e "${CLR_BOLD_RED}down${NORMAL}"
154 fi
155
156 echo -n "# Link : "
157 if device_has_carrier ${slave}; then
158 echo -e "${CLR_BOLD_GRN}yes${NORMAL}"
159 else
160 echo -e "${CLR_BOLD_RED}no${NORMAL}"
161 fi
162 done
163 else
164 echo -e "# State: ${CLR_BOLD_RED}down${NORMAL}"
165 fi
166
167 device_is_up ${MAC}
168 exit ${?}
169 ;;
170
171 *)
172 echo "Usage: ${0} [interface] {up|down|add|remove|attach|detach|status}"
173 exit 1
174 ;;
175 esac
176
177 # End $NETWORK_DEVICES/services/bonding