script/ethercat.sh
changeset 289 3c121228ebe9
parent 285 d6145fc87a74
child 299 911f0aed65a9
child 1715 e675450f2174
equal deleted inserted replaced
288:89d3c61ef3e6 289:3c121228ebe9
    57 
    57 
    58 . $ETHERCAT_CONFIG
    58 . $ETHERCAT_CONFIG
    59 
    59 
    60 #------------------------------------------------------------------------------
    60 #------------------------------------------------------------------------------
    61 
    61 
       
    62 #
       
    63 #  Function for setting up the EoE bridge
       
    64 #
       
    65 build_eoe_bridge() {
       
    66         if [ -z "$EOE_BRIDGE" ]; then return; fi
       
    67 
       
    68 	EOE_INTERFACES=`/sbin/ifconfig -a | grep -o -E "^eoe[0-9]+ "`
       
    69 
       
    70 	# add bridge, if it does not already exist
       
    71 	if ! /sbin/brctl show | grep -E -q "^$EOE_BRIDGE"; then
       
    72 	        if ! /sbin/brctl addbr $EOE_BRIDGE; then
       
    73 		        /bin/false
       
    74 			rc_status -v
       
    75 			rc_exit
       
    76 		fi
       
    77 	fi
       
    78 
       
    79        	# check if specified interfaces are bridged
       
    80 	for interf in $EOE_INTERFACES $EOE_EXTRA_INTERFACES; do
       
    81 	        # interface is already part of the bridge 
       
    82 	        if /sbin/brctl show $EOE_BRIDGE | grep -E -q $interf
       
    83 		        then continue
       
    84 		fi
       
    85 		# clear IP address and open interface
       
    86 		if ! /sbin/ifconfig $interf 0.0.0.0 up; then
       
    87 		        /bin/false
       
    88 			rc_status -v
       
    89 			rc_exit
       
    90 		fi
       
    91 		# add interface to the bridge
       
    92 		if ! /sbin/brctl addif $EOE_BRIDGE $interf; then
       
    93 		        /bin/false
       
    94 			rc_status -v
       
    95 			rc_exit
       
    96 		fi
       
    97 	done
       
    98 
       
    99 	# configure IP on bridge
       
   100 	if [ -n "$EOE_IP_ADDRESS" -a -n "$EOE_IP_NETMASK" ]; then
       
   101 	        if ! /sbin/ifconfig $EOE_BRIDGE $EOE_IP_ADDRESS \
       
   102 		        netmask $EOE_IP_NETMASK; then
       
   103 		        /bin/false
       
   104 			rc_status -v
       
   105 			rc_exit
       
   106 		fi
       
   107 	fi
       
   108 
       
   109 	# open bridge
       
   110 	if ! /sbin/ifconfig $EOE_BRIDGE up; then
       
   111 	        /bin/false
       
   112 		rc_status -v
       
   113 		rc_exit
       
   114 	fi
       
   115 
       
   116 	# install new default gateway
       
   117 	if [ -n "$EOE_GATEWAY" ]; then
       
   118 	        while /sbin/route -n | grep -E -q "^0.0.0.0"; do
       
   119 		        if ! /sbin/route del default; then
       
   120 			        echo "Failed to remove route!" 1>&2
       
   121 				/bin/false
       
   122 				rc_status -v
       
   123 				rc_exit
       
   124 			fi
       
   125 		done
       
   126 		if ! /sbin/route add default gw $EOE_GATEWAY; then
       
   127 		        /bin/false
       
   128 			rc_status -v
       
   129 			rc_exit
       
   130 		fi
       
   131 	fi
       
   132 }
       
   133 
       
   134 #------------------------------------------------------------------------------
       
   135 
    62 . /etc/rc.status
   136 . /etc/rc.status
    63 rc_reset
   137 rc_reset
    64 
   138 
    65 case "$1" in
   139 case "$1" in
    66     start)
   140     start)
    75 
   149 
    76 	if [ -z "$EOE_DEVICES" ]; then
   150 	if [ -z "$EOE_DEVICES" ]; then
    77 	    EOE_DEVICES=0
   151 	    EOE_DEVICES=0
    78 	fi
   152 	fi
    79 
   153 
       
   154 	# unload conflicting modules at first
    80 	for mod in 8139too 8139cp; do
   155 	for mod in 8139too 8139cp; do
    81 		if lsmod | grep "^$mod " > /dev/null; then
   156 		if lsmod | grep "^$mod " > /dev/null; then
    82 			if ! rmmod $mod; then
   157 			if ! rmmod $mod; then
    83 				/bin/false
   158 				/bin/false
    84 				rc_status -v
   159 				rc_status -v
    85 				rc_exit
   160 				rc_exit
    86 			fi
   161 			fi
    87 		fi
   162 		fi
    88 	done
   163 	done
    89 
   164 
       
   165 	# load master module
    90 	if ! modprobe ec_master ec_eoe_devices=$EOE_DEVICES; then
   166 	if ! modprobe ec_master ec_eoe_devices=$EOE_DEVICES; then
    91 	    /bin/false
   167 	    /bin/false
    92 	    rc_status -v
   168 	    rc_status -v
    93 	    rc_exit
   169 	    rc_exit
    94 	fi
   170 	fi
    95 
   171 
       
   172 	# load device module
    96 	if ! modprobe ec_8139too ec_device_index=$DEVICE_INDEX; then
   173 	if ! modprobe ec_8139too ec_device_index=$DEVICE_INDEX; then
    97 	    /bin/false
   174 	    /bin/false
    98 	    rc_status -v
   175 	    rc_status -v
    99 	    rc_exit
   176 	    rc_exit
   100 	fi
   177 	fi
   101 
   178 
   102 	# Build EoE bridge
   179 	# build EoE bridge
   103 	if [ -n "$EOE_BRIDGE" ]; then
   180 	build_eoe_bridge
   104 
       
   105 		EOE_INTERFACES=`/sbin/ifconfig -a | grep -o -E "^eoe[0-9]+ "`
       
   106 
       
   107 		# add bridge, if it does not already exist
       
   108 		if ! /sbin/brctl show | grep -E -q "^$EOE_BRIDGE"; then
       
   109 			if ! /sbin/brctl addbr $EOE_BRIDGE; then
       
   110 				/bin/false
       
   111 				rc_status -v
       
   112 				rc_exit
       
   113 			fi
       
   114 		fi
       
   115 
       
   116 		# free all interfaces of their addresses and add them to the bridge
       
   117 		for interface in $EOE_INTERFACES $EOE_EXTRA_INTERFACES; do
       
   118 			if ! /sbin/ifconfig $interface 0.0.0.0 up; then
       
   119 				/bin/false
       
   120 				rc_status -v
       
   121 				rc_exit
       
   122 			fi
       
   123 			if ! /sbin/brctl show | grep -E -q "^$EOE_BRIDGE.*$interface"; then
       
   124 				if ! /sbin/brctl addif $EOE_BRIDGE $interface; then
       
   125 					/bin/false
       
   126 					rc_status -v
       
   127 					rc_exit
       
   128 				fi
       
   129 			fi
       
   130 		done
       
   131 		if [ -n "$EOE_IP_ADDRESS" -a -n "$EOE_IP_NETMASK" ]; then
       
   132 			if ! /sbin/ifconfig $EOE_BRIDGE $EOE_IP_ADDRESS \
       
   133 				netmask $EOE_IP_NETMASK; then
       
   134 				/bin/false
       
   135 				rc_status -v
       
   136 				rc_exit
       
   137 			fi
       
   138 		fi
       
   139 		if ! /sbin/ifconfig $EOE_BRIDGE up; then
       
   140 			/bin/false
       
   141 			rc_status -v
       
   142 			rc_exit
       
   143 		fi
       
   144 
       
   145 		# install new default gateway
       
   146 		if [ -n "$EOE_GATEWAY" ]; then
       
   147 			while /sbin/route -n | grep -E -q "^0.0.0.0"; do
       
   148 				if ! /sbin/route del default; then
       
   149 					echo "Failed to remove default route!" 1>&2
       
   150 					/bin/false
       
   151 					rc_status -v
       
   152 					rc_exit
       
   153 				fi
       
   154 			done
       
   155 			if ! /sbin/route add default gw $EOE_GATEWAY; then
       
   156 				/bin/false
       
   157 				rc_status -v
       
   158 				rc_exit
       
   159 			fi
       
   160 		fi
       
   161 	fi
       
   162 
   181 
   163 	rc_status -v
   182 	rc_status -v
   164 	;;
   183 	;;
   165 
   184 
   166     stop)
   185     stop)
   167 	echo -n "Shutting down EtherCAT master "
   186 	echo -n "Shutting down EtherCAT master "
   168 
   187 
       
   188 	# unload modules
   169 	for mod in ec_8139too ec_master; do
   189 	for mod in ec_8139too ec_master; do
   170 		if lsmod | grep "^$mod " > /dev/null; then
   190 		if lsmod | grep "^$mod " > /dev/null; then
   171 			if ! rmmod $mod; then
   191 			if ! rmmod $mod; then
   172 				/bin/false
   192 				/bin/false
   173 				rc_status -v
   193 				rc_status -v
   174 				rc_exit
   194 				rc_exit
   175 			fi;
   195 			fi;
   176 		fi;
   196 		fi;
   177 	done
   197 	done
   178 
   198 
       
   199 	# reload previous modules
   179 	if ! modprobe 8139too; then
   200 	if ! modprobe 8139too; then
   180 	    echo "Warning: Failed to restore 8139too module."
   201 	    echo "Warning: Failed to restore 8139too module."
   181 	fi
   202 	fi
   182 
   203 
   183 	rc_status -v
   204 	rc_status -v
   184 	;;
   205 	;;
   185 
   206 
   186     restart)
   207     restart)
   187 	$0 stop
   208 	$0 stop || exit 1
   188 	$0 start
   209 	$0 start
   189 
   210 
   190 	rc_status
   211 	rc_status
   191 	;;
   212 	;;
   192 
   213 
   195 
   216 
   196 	lsmod | grep "^ec_master " > /dev/null
   217 	lsmod | grep "^ec_master " > /dev/null
   197 	master_running=$?
   218 	master_running=$?
   198 	lsmod | grep "^ec_8139too " > /dev/null
   219 	lsmod | grep "^ec_8139too " > /dev/null
   199 	device_running=$?
   220 	device_running=$?
       
   221 
       
   222 	# master module and device module loaded?
   200 	test $master_running -eq 0 -a $device_running -eq 0
   223 	test $master_running -eq 0 -a $device_running -eq 0
   201 
   224 
   202 	rc_status -v
   225 	rc_status -v
       
   226 	;;
       
   227 
       
   228     bridge)
       
   229 	echo -n "Building EoE bridge "
       
   230 
       
   231 	build_eoe_bridge
       
   232 
       
   233 	rc_status -v
       
   234 	;;
       
   235 
       
   236     *)
       
   237 	echo "USAGE: $0 {start|stop|restart|status|bridge}"
   203 	;;
   238 	;;
   204 esac
   239 esac
   205 rc_exit
   240 rc_exit
   206 
   241 
   207 #------------------------------------------------------------------------------
   242 #------------------------------------------------------------------------------