Update init.d support

This commit is contained in:
Nicola Fontana 2021-12-13 10:00:16 +00:00
parent 4ce0c74d35
commit 4dc942d789
9 changed files with 60 additions and 299 deletions

1
.gitignore vendored
View File

@ -39,6 +39,7 @@ lib/libethercat.la
lib/libethercat.pc
libtool
master/*.o.d
script/ethercat
script/ethercat.service
script/ethercatctl
script/init.d/ethercat

View File

@ -10,12 +10,12 @@ section of the
---
For the impatient: The procedure mainly consists of calling
For the impatient, the procedure mainly consists of calling:
```bash
./bootstrap # to create the configure script, if downloaded from the repo
./configure
./configure --sysconfdir=/etc
make all modules
```
@ -26,13 +26,11 @@ make modules_install install
depmod
```
... and linking the init script and copying the sysconfig file from $PREFIX/etc
to the appropriate locations and customizing the sysconfig file.
... and then customizing the appropriate configuration file:
```bash
ln -s ${PREFIX}/etc/init.d/ethercat /etc/init.d/ethercat
cp ${PREFIX}/etc/sysconfig/ethercat /etc/sysconfig/ethercat
vi /etc/sysconfig/ethercat
# vi /etc/ethercat.conf # For systemd based distro
# vi /etc/sysconfig/ethercat # For init.d based distro
```
Make sure, that the 'udev' package is installed, to automatically create the
@ -47,7 +45,8 @@ echo KERNEL==\"EtherCAT[0-9]*\", MODE=\"0664\" > /etc/udev/rules.d/99-EtherCAT.r
Now you can start the EtherCAT master:
```bash
/etc/init.d/ethercat start
# systemctl start ethercat # For systemd based distro
# /etc/init.d/ethercat start # For init.d based distro
```
Have a look at the [examples subdirectory](examples/) for some application

View File

@ -38,6 +38,7 @@ AC_CONFIG_HEADERS([config.h])
AC_CONFIG_SRCDIR([config.h.in])
AC_CONFIG_MACRO_DIR([m4])
AC_PROG_SED
AC_PROG_MKDIR_P
m4_ifndef([PKG_INSTALLDIR], [m4_fatal([pkgconf or pkg-config not found or too old (`pkg-config --version` must be >= 0.27)])])
PKG_PROG_PKG_CONFIG(0.27)
PKG_INSTALLDIR
@ -1328,6 +1329,16 @@ else
AC_MSG_RESULT([no])
fi
#-----------------------------------------------------------------------------
# init.d support
#-----------------------------------------------------------------------------
AC_ARG_ENABLE([initd],
AS_HELP_STRING([--disable-initd],
[Disable /etc/init.d script support (default: enabled)])
)
AM_CONDITIONAL(HAVE_INITD, test "x$enable_initd" != "xno")
#-----------------------------------------------------------------------------
# systemd service support
#-----------------------------------------------------------------------------
@ -1405,9 +1416,6 @@ AC_CONFIG_FILES([
master/Kbuild
master/Makefile
script/Makefile
script/init.d/Makefile
script/init.d/ethercat
script/sysconfig/Makefile
tool/Makefile
tty/Kbuild
tty/Makefile

View File

@ -23,15 +23,14 @@
#
#------------------------------------------------------------------------------
SUBDIRS = init.d sysconfig
sbin_SCRIPTS = ethercatctl
dist_sysconf_DATA = ethercat.conf
script_templates = \
ethercatctl.in \
ethercat.service.in
ethercat.service.in \
init.d/ethercat.in
EXTRA_DIST = \
$(script_templates) \
@ -40,7 +39,9 @@ EXTRA_DIST = \
CLEANFILES = \
ethercatctl \
ethercat.service
ethercat.service \
init.d/ethercat \
ethercat
if HAVE_SYSTEMD
systemdsystemunit_DATA = ethercat.service
@ -57,6 +58,13 @@ uninstall-hook:
cd $(DESTDIR)$(completionsdir); \
rm ethercat
if HAVE_INITD
initdir = $(sysconfdir)/init.d
init_SCRIPTS = init.d/ethercat
sysdir = $(sysconfdir)/sysconfig
sys_DATA = ethercat
endif
# Any precious variable used inside script_templates should appear here
expand_script = $(SED) \
-e 's,[@]VERSION[@],$(VERSION),g' \
@ -70,4 +78,11 @@ ethercatctl: $(srcdir)/ethercatctl.in Makefile
ethercat.service: $(srcdir)/ethercat.service.in Makefile
$(expand_script) < $< > $@
init.d/ethercat: $(srcdir)/init.d/ethercat.in Makefile
$(MKDIR_P) init.d && \
$(expand_script) < $< > $@
ethercat: $(srcdir)/ethercat.conf Makefile
$(SED) -e 's,for use with ethercatctl,for use with init.d,g' < $< > $@
#------------------------------------------------------------------------------

View File

@ -35,7 +35,15 @@ ETHERCAT=@bindir@/ethercat
#------------------------------------------------------------------------------
ETHERCAT_CONFIG=@sysconfdir@/ethercat.conf
if [ "$1" = "-c" ]; then
ETHERCAT_CONFIG="$2"
COMMAND="$3"
else
ETHERCAT_CONFIG=@sysconfdir@/ethercat.conf
COMMAND="$1"
fi
#------------------------------------------------------------------------------
if [ ! -r ${ETHERCAT_CONFIG} ]; then
echo ${ETHERCAT_CONFIG} not existing;
@ -73,7 +81,7 @@ parse_mac_address() {
#------------------------------------------------------------------------------
case "${1}" in
case "$COMMAND" in
start)
# bring up all updown interfaces before anything else
@ -229,7 +237,7 @@ status)
#------------------------------------------------------------------------------
*)
echo "USAGE: $0 {start|stop|restart|status}"
echo "USAGE: $0 [-c path/to/ethercat.conf] {start|stop|restart|status}"
exit 1
;;
esac

View File

@ -1,27 +0,0 @@
#------------------------------------------------------------------------------
#
# Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH
#
# This file is part of the IgH EtherCAT Master.
#
# The IgH EtherCAT Master is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# The IgH EtherCAT Master is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with the IgH EtherCAT Master; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
#
#------------------------------------------------------------------------------
initdir = $(sysconfdir)/init.d
init_SCRIPTS = ethercat
#------------------------------------------------------------------------------

View File

@ -40,27 +40,7 @@
#------------------------------------------------------------------------------
LSMOD=/sbin/lsmod
MODPROBE=/sbin/modprobe
RMMOD=/sbin/rmmod
MODINFO=/sbin/modinfo
ETHERCAT=@prefix@/bin/ethercat
MASTER_ARGS=
#------------------------------------------------------------------------------
ETHERCAT_CONFIG=/etc/sysconfig/ethercat
if [ ! -r ${ETHERCAT_CONFIG} ]; then
echo ${ETHERCAT_CONFIG} not existing;
if [ "${1}" = "stop" ]; then
exit 0
else
exit 6
fi
fi
. ${ETHERCAT_CONFIG}
ETHERCATCTL="@sbindir@/ethercatctl -c @sysconfdir@/sysconfig/ethercat"
#------------------------------------------------------------------------------
@ -90,41 +70,6 @@ exit_fail() {
#------------------------------------------------------------------------------
print_running() {
if [ -r /etc/rc.status ]; then
rc_reset
rc_status -v
else
echo " running"
fi
}
#------------------------------------------------------------------------------
print_dead() {
if [ -r /etc/rc.status ]; then
rc_failed
rc_status -v
else
echo " dead"
fi
}
#------------------------------------------------------------------------------
parse_mac_address() {
if [ -z "${1}" ]; then
MAC=""
elif echo "${1}" | grep -qE '^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$'; then
MAC=${1}
else
echo Invalid MAC address \""${1}"\" in ${ETHERCAT_CONFIG}
exit_fail
fi
}
#------------------------------------------------------------------------------
if [ -r /etc/rc.status ]; then
. /etc/rc.status
rc_reset
@ -135,85 +80,21 @@ case "${1}" in
start)
echo -n "Starting EtherCAT master @VERSION@ "
# construct DEVICES and BACKUPS from configuration variables
DEVICES=""
BACKUPS=""
MASTER_INDEX=0
while true; do
DEVICE=$(eval echo "\${MASTER${MASTER_INDEX}_DEVICE}")
BACKUP=$(eval echo "\${MASTER${MASTER_INDEX}_BACKUP}")
if [ -z "${DEVICE}" ]; then break; fi
if [ ${MASTER_INDEX} -gt 0 ]; then
DEVICES=${DEVICES},
BACKUPS=${BACKUPS},
fi
parse_mac_address "${DEVICE}"
DEVICES=${DEVICES}${MAC}
parse_mac_address "${BACKUP}"
BACKUPS=${BACKUPS}${MAC}
MASTER_INDEX=$((${MASTER_INDEX} + 1))
done
# load master module
if ! ${MODPROBE} ${MODPROBE_FLAGS} ec_master "${MASTER_ARGS}" \
main_devices="${DEVICES}" backup_devices="${BACKUPS}"; then
if $ETHERCATCTL start; then
exit_success
else
exit_fail
fi
# check for modules to replace
for MODULE in ${DEVICE_MODULES}; do
ECMODULE=ec_${MODULE}
if ! ${MODINFO} "${ECMODULE}" > /dev/null; then
continue # ec_* module not found
fi
if [ "${MODULE}" != "generic" ]; then
if ${LSMOD} | grep "^${MODULE} " > /dev/null; then
if ! ${RMMOD} "${MODULE}"; then
exit_fail
fi
fi
fi
if ! ${MODPROBE} ${MODPROBE_FLAGS} "${ECMODULE}"; then
if [ "${MODULE}" != "generic" ]; then
${MODPROBE} ${MODPROBE_FLAGS} "${MODULE}" # try to restore
fi
exit_fail
fi
done
exit_success
;;
stop)
echo -n "Shutting down EtherCAT master @VERSION@ "
# unload EtherCAT device modules
for MODULE in ${DEVICE_MODULES} master; do
ECMODULE=ec_${MODULE}
if ! ${LSMOD} | grep -q "^${ECMODULE} "; then
continue # ec_* module not loaded
fi
if ! ${RMMOD} "${ECMODULE}"; then
exit_fail
fi;
done
sleep 1
# reload previous modules
for MODULE in ${DEVICE_MODULES}; do
if [ "${MODULE}" != "generic" ]; then
if ! ${MODPROBE} ${MODPROBE_FLAGS} "${MODULE}"; then
echo Warning: Failed to restore "${MODULE}".
fi
fi
done
exit_success
if $ETHERCATCTL stop; then
exit_success
else
exit_fail
fi
;;
restart)
@ -223,35 +104,8 @@ restart)
;;
status)
echo "Checking for EtherCAT master @VERSION@ "
# count masters in configuration file
MASTER_COUNT=0
while true; do
DEVICE=$(eval echo "\${MASTER${MASTER_COUNT}_DEVICE}")
if [ -z "${DEVICE}" ]; then break; fi
MASTER_COUNT=$((${MASTER_COUNT} + 1))
done
RESULT=0
for i in $(seq 0 "$((${MASTER_COUNT} - 1))"); do
echo -n "Master${i} "
# Check if the master is in idle or operation phase
${ETHERCAT} master --master "${i}" 2>/dev/null | \
grep -qE 'Phase:[[:space:]]*Idle|Phase:[[:space:]]*Operation'
EXITCODE=$?
if [ ${EXITCODE} -eq 0 ]; then
print_running
else
print_dead
RESULT=1
fi
done
exit ${RESULT}
$ETHERCATCTL status
exit $?
;;
*)

View File

@ -1,29 +0,0 @@
#------------------------------------------------------------------------------
#
# Copyright (C) 2006-2008 Florian Pose, Ingenieurgemeinschaft IgH
#
# This file is part of the IgH EtherCAT Master.
#
# The IgH EtherCAT Master is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 2, as
# published by the Free Software Foundation.
#
# The IgH EtherCAT Master is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
# Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with the IgH EtherCAT Master; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
#
#------------------------------------------------------------------------------
sysdir = $(sysconfdir)/sysconfig
sys_DATA = ethercat
EXTRA_DIST = ethercat
#------------------------------------------------------------------------------

View File

@ -1,68 +0,0 @@
#------------------------------------------------------------------------------
#
# EtherCAT master sysconfig file
#
# vim: spelllang=en spell tw=78
#
#------------------------------------------------------------------------------
#
# Main Ethernet devices.
#
# The MASTER<X>_DEVICE variable specifies the Ethernet device for a master
# with index 'X'.
#
# Specify the MAC address (hexadecimal with colons) of the Ethernet device to
# use. Example: "00:00:08:44:ab:66"
#
# The broadcast address "ff:ff:ff:ff:ff:ff" has a special meaning: It tells
# the master to accept the first device offered by any Ethernet driver.
#
# The MASTER<X>_DEVICE variables also determine, how many masters will be
# created: A non-empty variable MASTER0_DEVICE will create one master, adding a
# non-empty variable MASTER1_DEVICE will create a second master, and so on.
#
MASTER0_DEVICE=""
#MASTER1_DEVICE=""
#
# Backup Ethernet devices
#
# The MASTER<X>_BACKUP variables specify the devices used for redundancy. They
# behaves nearly the same as the MASTER<X>_DEVICE variable, except that it
# does not interpret the ff:ff:ff:ff:ff:ff address.
#
#MASTER0_BACKUP=""
#
# Ethernet driver modules to use for EtherCAT operation.
#
# Specify a non-empty list of Ethernet drivers, that shall be used for
# EtherCAT operation.
#
# Except for the generic Ethernet driver module, the init script will try to
# unload the usual Ethernet driver modules in the list and replace them with
# the EtherCAT-capable ones. If a certain (EtherCAT-capable) driver is not
# found, a warning will appear.
#
# Possible values: 8139too, e100, e1000, e1000e, r8169, generic, ccat, igb.
# Separate multiple drivers with spaces.
#
# Note: The e100, e1000, e1000e, r8169, ccat and igb drivers are not built by
# default. Enable them with the --enable-<driver> configure switches.
#
# Attention: When using the generic driver, the corresponding Ethernet device
# has to be activated (with OS methods, for example 'ip link set ethX up'),
# before the master is started, otherwise all frames will time out.
#
DEVICE_MODULES=""
#
# Flags for loading kernel modules.
#
# This can usually be left empty. Adjust this variable, if you have problems
# with module loading.
#
#MODPROBE_FLAGS="-b"
#------------------------------------------------------------------------------