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