diff mbox series

systemd generic startup

Message ID 20191008154932.26394-1-adrian.freihofer@siemens.com
State Changes Requested
Headers show
Series systemd generic startup | expand

Commit Message

Freihofer, Adrian Oct. 8, 2019, 3:49 p.m. UTC
If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make
install, a generic set of systemd service and socket files gets
installed.

For most projects starting swupdate requires some logic implemented
in shell scripts. Therefore swupdate is started by a new shell
script /usr/bin/swupdate-pre. swupdate-pre evaluates code snippets
from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets
started. All modes (file, webserver, webclient) of swupdate are
supported. Users or distro maintainers are enabled to add simple
code snippets adding arguments for swupdate to the following three
variables:
- SWUPDATE_ARGS
- SWUPDATE_WEBSERVER_ARGS
- SWUPDATE_DOWNLOAD_ARGS

This allows to provide a "just works" configuration for Mongoose
if it gets compiled. This was not the case with systemd before.

Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
---
 Makefile                                       | 20 ++++++++++++++++++
 doc/source/swupdate.rst                        | 29 +++++++++++++++-----------
 startup/systemd/10-mongoose-args               |  1 +
 startup/systemd/swupdate-pre                   | 28 +++++++++++++++++++++++++
 startup/systemd/swupdate-progress.service      |  9 ++++++++
 startup/systemd/swupdate-usb.rules             |  2 ++
 startup/systemd/swupdate-usb@.service          |  8 +++++++
 startup/systemd/swupdate.service               | 10 +++++++++
 startup/systemd/swupdate.socket                | 11 ++++++++++
 startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++
 10 files changed, 109 insertions(+), 12 deletions(-)
 create mode 100644 startup/systemd/10-mongoose-args
 create mode 100644 startup/systemd/swupdate-pre
 create mode 100644 startup/systemd/swupdate-progress.service
 create mode 100644 startup/systemd/swupdate-usb.rules
 create mode 100644 startup/systemd/swupdate-usb@.service
 create mode 100644 startup/systemd/swupdate.service
 create mode 100644 startup/systemd/swupdate.socket
 create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf

Comments

Stefan Herbrechtsmeier Oct. 8, 2019, 7:47 p.m. UTC | #1
Hi Adrian,

do you really need the dynamic of an script? Otherwise you could use the 
systemd "drop-in" directory [1] to override the environment variables.

[1] https://www.freedesktop.org/software/systemd/man/systemd.unit.html

Regards
   Stefan

Am 08.10.19 um 17:49 schrieb Adrian Freihofer:
> If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make
> install, a generic set of systemd service and socket files gets
> installed.
> 
> For most projects starting swupdate requires some logic implemented
> in shell scripts. Therefore swupdate is started by a new shell
> script /usr/bin/swupdate-pre. swupdate-pre evaluates code snippets
> from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets
> started. All modes (file, webserver, webclient) of swupdate are
> supported. Users or distro maintainers are enabled to add simple
> code snippets adding arguments for swupdate to the following three
> variables:
> - SWUPDATE_ARGS
> - SWUPDATE_WEBSERVER_ARGS
> - SWUPDATE_DOWNLOAD_ARGS
> 
> This allows to provide a "just works" configuration for Mongoose
> if it gets compiled. This was not the case with systemd before.
> 
> Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
> ---
>   Makefile                                       | 20 ++++++++++++++++++
>   doc/source/swupdate.rst                        | 29 +++++++++++++++-----------
>   startup/systemd/10-mongoose-args               |  1 +
>   startup/systemd/swupdate-pre                   | 28 +++++++++++++++++++++++++
>   startup/systemd/swupdate-progress.service      |  9 ++++++++
>   startup/systemd/swupdate-usb.rules             |  2 ++
>   startup/systemd/swupdate-usb@.service          |  8 +++++++
>   startup/systemd/swupdate.service               | 10 +++++++++
>   startup/systemd/swupdate.socket                | 11 ++++++++++
>   startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++
>   10 files changed, 109 insertions(+), 12 deletions(-)
>   create mode 100644 startup/systemd/10-mongoose-args
>   create mode 100644 startup/systemd/swupdate-pre
>   create mode 100644 startup/systemd/swupdate-progress.service
>   create mode 100644 startup/systemd/swupdate-usb.rules
>   create mode 100644 startup/systemd/swupdate-usb@.service
>   create mode 100644 startup/systemd/swupdate.service
>   create mode 100644 startup/systemd/swupdate.socket
>   create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf
> 
> diff --git a/Makefile b/Makefile
> index d60c58e..e067009 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -461,6 +461,26 @@ install: all
>   		install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \
>   		install -m 0755 ${lua_swupdate} $(DESTDIR)/usr/lib/lua/$(LUAVER); \
>   	fi
> +	if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \
> +		install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -d ${DESTDIR}/usr/lib/swupdate.d; \
> +		install -m 755 startup/systemd/swupdate-pre ${DESTDIR}/usr/bin; \
> +		install -m 644 startup/systemd/swupdate-progress.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -m 644 startup/systemd/swupdate-usb@.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -m 644 startup/systemd/swupdate.socket ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		ctrl_path=${CONFIG_SOCKET_CTRL_PATH}; progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \
> +		sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" \
> +			-e "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" \
> +			${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket; \
> +		install -m 644 startup/systemd/swupdate.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -d ${DESTDIR}/usr/lib/tmpfiles.d; \
> +		install -m 0644 startup/systemd/systemd-tmpfiles-swupdate.conf ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \
> +		install -d ${DESTDIR}/etc/udev/rules.d; \
> +		install -m 0644 startup/systemd/swupdate-usb.rules ${DESTDIR}/etc/udev/rules.d/; \
> +		if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \
> +			install -m 644 startup/systemd/10-mongoose-args ${DESTDIR}/usr/lib/swupdate.d/; \
> +		fi; \
> +	fi
>   
>   PHONY += run-tests
>   tests: \
> diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst
> index 1ff08ef..ff7783e 100644
> --- a/doc/source/swupdate.rst
> +++ b/doc/source/swupdate.rst
> @@ -520,22 +520,27 @@ configuration switch ``CONFIG_SYSTEMD``. If enabled, SWUpdate
>   signals systemd about start-up completion and can make optional
>   use of systemd's socket-based activation feature.
>   
> -A sample systemd service unit file ``/etc/systemd/system/swupdate.service``
> -may look like the following starting SWUpdate in suricatta daemon mode:
> +To install a generic set of service and socket files, pass the
> +`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to `/lib/systed/system`
> +for example, to the "make install" command.
>   
> -::
> +For further runtime adjustments, some shell code snippets can be added
> +to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The
> +code snippets are processed by a full-featured shell in alphabetical order.
> +Files from /etc overwrite files from /usr/ with the same name.
>   
> -	[Unit]
> -	Description=SWUpdate daemon
> -	Documentation=https://github.com/sbabic/swupdate
> -	Documentation=https://sbabic.github.io/swupdate
> +The purpose of these code snippets is to assign reasonable command line
> +parameters to swupdate. The following variables may be set:
>   
> -	[Service]
> -	Type=notify
> -	ExecStart=/usr/bin/swupdate -u '-t default -u http://localhost -i 25'
> +- SWUPDATE_ARGS
> +- SWUPDATE_WEBSERVER_ARGS
> +- SWUPDATE_DOWNLOAD_ARGS
>   
> -	[Install]
> -	WantedBy=multi-user.target
> +A sample configuration file ``/usr/lib/swupdate.d/10-suricatta``
> +may look like the following starting SWUpdate in suricatta daemon mode:
> +::
> +
> +	SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 25'
>   
>   Started via ``systemctl start swupdate.service``, SWUpdate
>   (re)creates its sockets on startup. For using socket-based
> diff --git a/startup/systemd/10-mongoose-args b/startup/systemd/10-mongoose-args
> new file mode 100644
> index 0000000..896e66b
> --- /dev/null
> +++ b/startup/systemd/10-mongoose-args
> @@ -0,0 +1 @@
> +SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p 8080}"
> diff --git a/startup/systemd/swupdate-pre b/startup/systemd/swupdate-pre
> new file mode 100644
> index 0000000..2d29a12
> --- /dev/null
> +++ b/startup/systemd/swupdate-pre
> @@ -0,0 +1,28 @@
> +#!/bin/sh
> +
> +# Override these variables in sourced script(s) located
> +# in /usr/lib/swupdate.d or /etc/swupdate.d
> +SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}"
> +SWUPDATE_WEBSERVER_ARGS=""
> +SWUPDATE_DOWNLOAD_ARGS=""
> +
> +# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/
> +# A file found in /etc replaces the same file in /usr
> +for f in `(test -d /usr/lib/swupdate.d/ && ls -1 /usr/lib/swupdate.d/; test -d /etc/swupdate.d && ls -1 /etc/swupdate.d) | sort -u`; do
> +  if [ -f /etc/swupdate.d/$f ]; then
> +    . /etc/swupdate.d/$f
> +  else
> +    . /usr/lib/swupdate.d/$f
> +  fi
> +done
> +
> +#  handle variable escaping in a simmple way. Use exec to forward open filedescriptors from systemd open.
> +if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS" -u "$SWUPDATE_DOWNLOAD_ARGS"
> +elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS"
> +elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -d "$SWUPDATE_DOWNLOAD_ARGS"
> +else
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS
> +fi
> diff --git a/startup/systemd/swupdate-progress.service b/startup/systemd/swupdate-progress.service
> new file mode 100644
> index 0000000..212d151
> --- /dev/null
> +++ b/startup/systemd/swupdate-progress.service
> @@ -0,0 +1,9 @@
> +[Unit]
> +Description=swupdate progress service
> +Before=swupdate.service
> +
> +[Service]
> +ExecStart=/usr/bin/swupdate-progress -r -w
> +
> +[Install]
> +WantedBy=swupdate.service
> diff --git a/startup/systemd/swupdate-usb.rules b/startup/systemd/swupdate-usb.rules
> new file mode 100644
> index 0000000..7bba227
> --- /dev/null
> +++ b/startup/systemd/swupdate-usb.rules
> @@ -0,0 +1,2 @@
> +ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd", ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service"
> +
> diff --git a/startup/systemd/swupdate-usb@.service b/startup/systemd/swupdate-usb@.service
> new file mode 100644
> index 0000000..eda9d15
> --- /dev/null
> +++ b/startup/systemd/swupdate-usb@.service
> @@ -0,0 +1,8 @@
> +[Unit]
> +Description=usb media swupdate service
> +Requires=swupdate-progress.service
> +
> +[Service]
> +ExecStartPre=/bin/mount /dev/%I /mnt
> +ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu"
> +ExecStopPost=/bin/umount /mnt
> diff --git a/startup/systemd/swupdate.service b/startup/systemd/swupdate.service
> new file mode 100644
> index 0000000..a96617a
> --- /dev/null
> +++ b/startup/systemd/swupdate.service
> @@ -0,0 +1,10 @@
> +[Unit]
> +Description=SWUpdate daemon
> +Documentation=https://github.com/sbabic/swupdate
> +Documentation=https://sbabic.github.io/swupdate
> +
> +[Service]
> +ExecStart=/usr/bin/swupdate-pre
> +
> +[Install]
> +WantedBy=multi-user.target
> diff --git a/startup/systemd/swupdate.socket b/startup/systemd/swupdate.socket
> new file mode 100644
> index 0000000..aeb7310
> --- /dev/null
> +++ b/startup/systemd/swupdate.socket
> @@ -0,0 +1,11 @@
> +[Unit]
> +Description=SWUpdate socket listener
> +Documentation=https://github.com/sbabic/swupdate
> +Documentation=https://sbabic.github.io/swupdate
> +
> +[Socket]
> +ListenStream=@@SOCKET_CTRL_PATH@@
> +ListenStream=@@SOCKET_PROGRESS_PATH@@
> +
> +[Install]
> +WantedBy=sockets.target
> diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf b/startup/systemd/systemd-tmpfiles-swupdate.conf
> new file mode 100644
> index 0000000..b374ddc
> --- /dev/null
> +++ b/startup/systemd/systemd-tmpfiles-swupdate.conf
> @@ -0,0 +1,3 @@
> +X /tmp/datadst
> +X /tmp/scripts
> +X /tmp/swupdate.env
>
Stefano Babic Oct. 8, 2019, 9:11 p.m. UTC | #2
Hi Adrian,

On 08/10/19 17:49, Adrian Freihofer wrote:
> If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make
> install, a generic set of systemd service and socket files gets
> installed.
> 
> For most projects starting swupdate requires some logic implemented
> in shell scripts. Therefore swupdate is started by a new shell
> script /usr/bin/swupdate-pre.

This is one way to do things. It has the drawback that SWUpdate is not
started by systemd but by the script. Then systemd will monitor the
script instead of SWUpdate.

Another way to start is to use a script to set a systemd environment
file, while systemd will go on to start SWUpdate.

[Service]
ExecStartPre=-/usr/bin/swupdate-env
EnvironmentFile=-/tmp/swupdate.env
ExecStart=/usr/bin/swupdate <variables set in swupdate.env>

I am not sure how your patch combines with meta-swupdate. Until now,
SystemV init script or systemd service are outside SWUpdate itself and
set in the build system. In OE thes lets to easy overwrite them by
adding a swupdate_%.bbappend. But I guess that if SYSTEMD_SYSTEM_UNITDIR
is set this conflicts with the unit in meta-swupdate.

> swupdate-pre evaluates code snippets
> from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets
> started. All modes (file, webserver, webclient) of swupdate are
> supported. Users or distro maintainers are enabled to add simple
> code snippets adding arguments for swupdate to the following three
> variables:
> - SWUPDATE_ARGS
> - SWUPDATE_WEBSERVER_ARGS
> - SWUPDATE_DOWNLOAD_ARGS
> 
> This allows to provide a "just works" configuration for Mongoose
> if it gets compiled. This was not the case with systemd before.
> 
> Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
> ---
>  Makefile                                       | 20 ++++++++++++++++++
>  doc/source/swupdate.rst                        | 29 +++++++++++++++-----------
>  startup/systemd/10-mongoose-args               |  1 +
>  startup/systemd/swupdate-pre                   | 28 +++++++++++++++++++++++++
>  startup/systemd/swupdate-progress.service      |  9 ++++++++
>  startup/systemd/swupdate-usb.rules             |  2 ++
>  startup/systemd/swupdate-usb@.service          |  8 +++++++
>  startup/systemd/swupdate.service               | 10 +++++++++
>  startup/systemd/swupdate.socket                | 11 ++++++++++
>  startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++
>  10 files changed, 109 insertions(+), 12 deletions(-)
>  create mode 100644 startup/systemd/10-mongoose-args
>  create mode 100644 startup/systemd/swupdate-pre
>  create mode 100644 startup/systemd/swupdate-progress.service
>  create mode 100644 startup/systemd/swupdate-usb.rules
>  create mode 100644 startup/systemd/swupdate-usb@.service
>  create mode 100644 startup/systemd/swupdate.service
>  create mode 100644 startup/systemd/swupdate.socket
>  create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf
> 
> diff --git a/Makefile b/Makefile
> index d60c58e..e067009 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -461,6 +461,26 @@ install: all
>  		install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \
>  		install -m 0755 ${lua_swupdate} $(DESTDIR)/usr/lib/lua/$(LUAVER); \
>  	fi
> +	if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \
> +		install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -d ${DESTDIR}/usr/lib/swupdate.d; \
> +		install -m 755 startup/systemd/swupdate-pre ${DESTDIR}/usr/bin; \
> +		install -m 644 startup/systemd/swupdate-progress.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -m 644 startup/systemd/swupdate-usb@.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -m 644 startup/systemd/swupdate.socket ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		ctrl_path=${CONFIG_SOCKET_CTRL_PATH}; progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \
> +		sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" \
> +			-e "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" \
> +			${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket; \
> +		install -m 644 startup/systemd/swupdate.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -d ${DESTDIR}/usr/lib/tmpfiles.d; \
> +		install -m 0644 startup/systemd/systemd-tmpfiles-swupdate.conf ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \
> +		install -d ${DESTDIR}/etc/udev/rules.d; \
> +		install -m 0644 startup/systemd/swupdate-usb.rules ${DESTDIR}/etc/udev/rules.d/; \
> +		if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \
> +			install -m 644 startup/systemd/10-mongoose-args ${DESTDIR}/usr/lib/swupdate.d/; \
> +		fi; \
> +	fi
>  
>  PHONY += run-tests
>  tests: \
> diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst
> index 1ff08ef..ff7783e 100644
> --- a/doc/source/swupdate.rst
> +++ b/doc/source/swupdate.rst
> @@ -520,22 +520,27 @@ configuration switch ``CONFIG_SYSTEMD``. If enabled, SWUpdate
>  signals systemd about start-up completion and can make optional
>  use of systemd's socket-based activation feature.
>  
> -A sample systemd service unit file ``/etc/systemd/system/swupdate.service``
> -may look like the following starting SWUpdate in suricatta daemon mode:
> +To install a generic set of service and socket files, pass the
> +`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to `/lib/systed/system`
> +for example, to the "make install" command.
>  
> -::
> +For further runtime adjustments, some shell code snippets can be added
> +to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The
> +code snippets are processed by a full-featured shell in alphabetical order.
> +Files from /etc overwrite files from /usr/ with the same name.
>  
> -	[Unit]
> -	Description=SWUpdate daemon
> -	Documentation=https://github.com/sbabic/swupdate
> -	Documentation=https://sbabic.github.io/swupdate
> +The purpose of these code snippets is to assign reasonable command line
> +parameters to swupdate. The following variables may be set:
>  
> -	[Service]
> -	Type=notify
> -	ExecStart=/usr/bin/swupdate -u '-t default -u http://localhost -i 25'
> +- SWUPDATE_ARGS
> +- SWUPDATE_WEBSERVER_ARGS
> +- SWUPDATE_DOWNLOAD_ARGS
>  
> -	[Install]
> -	WantedBy=multi-user.target
> +A sample configuration file ``/usr/lib/swupdate.d/10-suricatta``
> +may look like the following starting SWUpdate in suricatta daemon mode:
> +::
> +
> +	SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 25'
>  
>  Started via ``systemctl start swupdate.service``, SWUpdate
>  (re)creates its sockets on startup. For using socket-based
> diff --git a/startup/systemd/10-mongoose-args b/startup/systemd/10-mongoose-args
> new file mode 100644
> index 0000000..896e66b
> --- /dev/null
> +++ b/startup/systemd/10-mongoose-args
> @@ -0,0 +1 @@
> +SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p 8080}"
> diff --git a/startup/systemd/swupdate-pre b/startup/systemd/swupdate-pre
> new file mode 100644
> index 0000000..2d29a12
> --- /dev/null
> +++ b/startup/systemd/swupdate-pre
> @@ -0,0 +1,28 @@
> +#!/bin/sh
> +
> +# Override these variables in sourced script(s) located
> +# in /usr/lib/swupdate.d or /etc/swupdate.d
> +SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}"
> +SWUPDATE_WEBSERVER_ARGS=""
> +SWUPDATE_DOWNLOAD_ARGS=""
> +
> +# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/
> +# A file found in /etc replaces the same file in /usr
> +for f in `(test -d /usr/lib/swupdate.d/ && ls -1 /usr/lib/swupdate.d/; test -d /etc/swupdate.d && ls -1 /etc/swupdate.d) | sort -u`; do
> +  if [ -f /etc/swupdate.d/$f ]; then
> +    . /etc/swupdate.d/$f
> +  else
> +    . /usr/lib/swupdate.d/$f
> +  fi
> +done
> +
> +#  handle variable escaping in a simmple way. Use exec to forward open filedescriptors from systemd open.
> +if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS" -u "$SWUPDATE_DOWNLOAD_ARGS"
> +elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS"
> +elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -d "$SWUPDATE_DOWNLOAD_ARGS"
> +else
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS
> +fi
> diff --git a/startup/systemd/swupdate-progress.service b/startup/systemd/swupdate-progress.service
> new file mode 100644
> index 0000000..212d151
> --- /dev/null
> +++ b/startup/systemd/swupdate-progress.service
> @@ -0,0 +1,9 @@
> +[Unit]
> +Description=swupdate progress service
> +Before=swupdate.service
> +
> +[Service]
> +ExecStart=/usr/bin/swupdate-progress -r -w
> +
> +[Install]
> +WantedBy=swupdate.service
> diff --git a/startup/systemd/swupdate-usb.rules b/startup/systemd/swupdate-usb.rules
> new file mode 100644
> index 0000000..7bba227
> --- /dev/null
> +++ b/startup/systemd/swupdate-usb.rules
> @@ -0,0 +1,2 @@
> +ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd", ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service"
> +
> diff --git a/startup/systemd/swupdate-usb@.service b/startup/systemd/swupdate-usb@.service
> new file mode 100644
> index 0000000..eda9d15
> --- /dev/null
> +++ b/startup/systemd/swupdate-usb@.service
> @@ -0,0 +1,8 @@
> +[Unit]
> +Description=usb media swupdate service
> +Requires=swupdate-progress.service
> +
> +[Service]
> +ExecStartPre=/bin/mount /dev/%I /mnt
> +ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu"
> +ExecStopPost=/bin/umount /mnt
> diff --git a/startup/systemd/swupdate.service b/startup/systemd/swupdate.service
> new file mode 100644
> index 0000000..a96617a
> --- /dev/null
> +++ b/startup/systemd/swupdate.service
> @@ -0,0 +1,10 @@
> +[Unit]
> +Description=SWUpdate daemon
> +Documentation=https://github.com/sbabic/swupdate
> +Documentation=https://sbabic.github.io/swupdate
> +
> +[Service]
> +ExecStart=/usr/bin/swupdate-pre
> +
> +[Install]
> +WantedBy=multi-user.target
> diff --git a/startup/systemd/swupdate.socket b/startup/systemd/swupdate.socket
> new file mode 100644
> index 0000000..aeb7310
> --- /dev/null
> +++ b/startup/systemd/swupdate.socket
> @@ -0,0 +1,11 @@
> +[Unit]
> +Description=SWUpdate socket listener
> +Documentation=https://github.com/sbabic/swupdate
> +Documentation=https://sbabic.github.io/swupdate
> +
> +[Socket]
> +ListenStream=@@SOCKET_CTRL_PATH@@
> +ListenStream=@@SOCKET_PROGRESS_PATH@@
> +
> +[Install]
> +WantedBy=sockets.target
> diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf b/startup/systemd/systemd-tmpfiles-swupdate.conf
> new file mode 100644
> index 0000000..b374ddc
> --- /dev/null
> +++ b/startup/systemd/systemd-tmpfiles-swupdate.conf
> @@ -0,0 +1,3 @@
> +X /tmp/datadst
> +X /tmp/scripts
> +X /tmp/swupdate.env

These files are in meta-swupdate - how do they work together ?

Best regards,
Stefano
Adrian Freihofer Oct. 9, 2019, 8:20 a.m. UTC | #3
Hi Stefano, hi Stefan

The crucial detail is the "exec swupdate" (not "swupdate" which would fork) 
in the main script. I tested the solution also with socket activation. 
Without the exec it does not work and swupdate is complaining that it did 
not get any sockets from systemd. With the exec in front of swupdate, 
everything seems to work. swupdate notes to take over the sockets from 
systemd.

Regarding the question if we need a shell script or not in general: So far 
I always needed a few lines of shell code in front of swupdate. Once in the 
past we asked Stefano for doing a customized implementation for one of our 
projects. What we got was a shell script starting swupdate. Usually we have 
to find out on which rootfs we are running (rdev /), read the HW version 
from an eeprom and similar hardware specific things. Tasks which are pretty 
easy if a few lines of shell code are supported but really hard without.

Also the systemd author recommends to use a script for complicated things 
(http://0pointer.de/blog/projects/on-etc-sysinit.html). Using ExecStartPre 
to call a script writing the environment into a file which gets sourced by 
systemd before starting swupdate looks more like a workaround to me. 
Another detail which I was struggling with this approach, was escaping the 
parameters for the mongoose and the suricatta command line arguments, if I 
tried to store them into variables. This is now solved in a simple, 
reliable way.

Regarding the changes required on meta-swupdate to make use of this change: 
A pre-view is avaliable here: 
https://github.com/afreof/meta-swupdate/tree/systemd-improvements. A final 
implementation is not possible until this one here got merged. 

Thank you for the review.
Regards,
Adrian

Am Dienstag, 8. Oktober 2019 17:49:47 UTC+2 schrieb Adrian Freihofer:
>
> If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make 
> install, a generic set of systemd service and socket files gets 
> installed. 
>
> For most projects starting swupdate requires some logic implemented 
> in shell scripts. Therefore swupdate is started by a new shell 
> script /usr/bin/swupdate-pre. swupdate-pre evaluates code snippets 
> from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets 
> started. All modes (file, webserver, webclient) of swupdate are 
> supported. Users or distro maintainers are enabled to add simple 
> code snippets adding arguments for swupdate to the following three 
> variables: 
> - SWUPDATE_ARGS 
> - SWUPDATE_WEBSERVER_ARGS 
> - SWUPDATE_DOWNLOAD_ARGS 
>
> This allows to provide a "just works" configuration for Mongoose 
> if it gets compiled. This was not the case with systemd before. 
>
> Signed-off-by: Adrian Freihofer <adrian....@siemens.com <javascript:>> 
> --- 
>  Makefile                                       | 20 ++++++++++++++++++ 
>  doc/source/swupdate.rst                        | 29 
> +++++++++++++++----------- 
>  startup/systemd/10-mongoose-args               |  1 + 
>  startup/systemd/swupdate-pre                   | 28 
> +++++++++++++++++++++++++ 
>  startup/systemd/swupdate-progress.service      |  9 ++++++++ 
>  startup/systemd/swupdate-usb.rules             |  2 ++ 
>  startup/systemd/swupdate-usb@.service          |  8 +++++++ 
>  startup/systemd/swupdate.service               | 10 +++++++++ 
>  startup/systemd/swupdate.socket                | 11 ++++++++++ 
>  startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++ 
>  10 files changed, 109 insertions(+), 12 deletions(-) 
>  create mode 100644 startup/systemd/10-mongoose-args 
>  create mode 100644 startup/systemd/swupdate-pre 
>  create mode 100644 startup/systemd/swupdate-progress.service 
>  create mode 100644 startup/systemd/swupdate-usb.rules 
>  create mode 100644 startup/systemd/swupdate-usb@.service 
>  create mode 100644 startup/systemd/swupdate.service 
>  create mode 100644 startup/systemd/swupdate.socket 
>  create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf 
>
> diff --git a/Makefile b/Makefile 
> index d60c58e..e067009 100644 
> --- a/Makefile 
> +++ b/Makefile 
> @@ -461,6 +461,26 @@ install: all 
>                  install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \ 
>                  install -m 0755 ${lua_swupdate} 
> $(DESTDIR)/usr/lib/lua/$(LUAVER); \ 
>          fi 
> +        if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \ 
> +                install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> +                install -d ${DESTDIR}/usr/lib/swupdate.d; \ 
> +                install -m 755 startup/systemd/swupdate-pre 
> ${DESTDIR}/usr/bin; \ 
> +                install -m 644 startup/systemd/swupdate-progress.service 
> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> +                install -m 644 startup/systemd/swupdate-usb@.service 
> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> +                install -m 644 startup/systemd/swupdate.socket 
> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> +                ctrl_path=${CONFIG_SOCKET_CTRL_PATH}; 
> progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \ 
> +                sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" \ 
> +                        -e "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" 
> \ 
> +                        ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket; 
> \ 
> +                install -m 644 startup/systemd/swupdate.service 
> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> +                install -d ${DESTDIR}/usr/lib/tmpfiles.d; \ 
> +                install -m 0644 
> startup/systemd/systemd-tmpfiles-swupdate.conf 
> ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \ 
> +                install -d ${DESTDIR}/etc/udev/rules.d; \ 
> +                install -m 0644 startup/systemd/swupdate-usb.rules 
> ${DESTDIR}/etc/udev/rules.d/; \ 
> +                if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \ 
> +                        install -m 644 startup/systemd/10-mongoose-args 
> ${DESTDIR}/usr/lib/swupdate.d/; \ 
> +                fi; \ 
> +        fi 
>   
>  PHONY += run-tests 
>  tests: \ 
> diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst 
> index 1ff08ef..ff7783e 100644 
> --- a/doc/source/swupdate.rst 
> +++ b/doc/source/swupdate.rst 
> @@ -520,22 +520,27 @@ configuration switch ``CONFIG_SYSTEMD``. If enabled, 
> SWUpdate 
>  signals systemd about start-up completion and can make optional 
>  use of systemd's socket-based activation feature. 
>   
> -A sample systemd service unit file 
> ``/etc/systemd/system/swupdate.service`` 
> -may look like the following starting SWUpdate in suricatta daemon mode: 
> +To install a generic set of service and socket files, pass the 
> +`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to `/lib/systed/system` 
> +for example, to the "make install" command. 
>   
> -:: 
> +For further runtime adjustments, some shell code snippets can be added 
> +to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The 
> +code snippets are processed by a full-featured shell in alphabetical 
> order. 
> +Files from /etc overwrite files from /usr/ with the same name. 
>   
> -        [Unit] 
> -        Description=SWUpdate daemon 
> -        Documentation=https://github.com/sbabic/swupdate 
> -        Documentation=https://sbabic.github.io/swupdate 
> +The purpose of these code snippets is to assign reasonable command line 
> +parameters to swupdate. The following variables may be set: 
>   
> -        [Service] 
> -        Type=notify 
> -        ExecStart=/usr/bin/swupdate -u '-t default -u http://localhost 
> -i 25' 
> +- SWUPDATE_ARGS 
> +- SWUPDATE_WEBSERVER_ARGS 
> +- SWUPDATE_DOWNLOAD_ARGS 
>   
> -        [Install] 
> -        WantedBy=multi-user.target 
> +A sample configuration file ``/usr/lib/swupdate.d/10-suricatta`` 
> +may look like the following starting SWUpdate in suricatta daemon mode: 
> +:: 
> + 
> +        SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 25' 
>   
>  Started via ``systemctl start swupdate.service``, SWUpdate 
>  (re)creates its sockets on startup. For using socket-based 
> diff --git a/startup/systemd/10-mongoose-args 
> b/startup/systemd/10-mongoose-args 
> new file mode 100644 
> index 0000000..896e66b 
> --- /dev/null 
> +++ b/startup/systemd/10-mongoose-args 
> @@ -0,0 +1 @@ 
> +SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p 
> 8080}" 
> diff --git a/startup/systemd/swupdate-pre b/startup/systemd/swupdate-pre 
> new file mode 100644 
> index 0000000..2d29a12 
> --- /dev/null 
> +++ b/startup/systemd/swupdate-pre 
> @@ -0,0 +1,28 @@ 
> +#!/bin/sh 
> + 
> +# Override these variables in sourced script(s) located 
> +# in /usr/lib/swupdate.d or /etc/swupdate.d 
> +SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}" 
> +SWUPDATE_WEBSERVER_ARGS="" 
> +SWUPDATE_DOWNLOAD_ARGS="" 
> + 
> +# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/ 
> +# A file found in /etc replaces the same file in /usr 
> +for f in `(test -d /usr/lib/swupdate.d/ && ls -1 /usr/lib/swupdate.d/; 
> test -d /etc/swupdate.d && ls -1 /etc/swupdate.d) | sort -u`; do 
> +  if [ -f /etc/swupdate.d/$f ]; then 
> +    . /etc/swupdate.d/$f 
> +  else 
> +    . /usr/lib/swupdate.d/$f 
> +  fi 
> +done 
> + 
> +#  handle variable escaping in a simmple way. Use exec to forward open 
> filedescriptors from systemd open. 
> +if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS" != "" 
> ]; then 
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS" -u 
> "$SWUPDATE_DOWNLOAD_ARGS" 
> +elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then 
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS" 
> +elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then 
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -d "$SWUPDATE_DOWNLOAD_ARGS" 
> +else 
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS 
> +fi 
> diff --git a/startup/systemd/swupdate-progress.service 
> b/startup/systemd/swupdate-progress.service 
> new file mode 100644 
> index 0000000..212d151 
> --- /dev/null 
> +++ b/startup/systemd/swupdate-progress.service 
> @@ -0,0 +1,9 @@ 
> +[Unit] 
> +Description=swupdate progress service 
> +Before=swupdate.service 
> + 
> +[Service] 
> +ExecStart=/usr/bin/swupdate-progress -r -w 
> + 
> +[Install] 
> +WantedBy=swupdate.service 
> diff --git a/startup/systemd/swupdate-usb.rules 
> b/startup/systemd/swupdate-usb.rules 
> new file mode 100644 
> index 0000000..7bba227 
> --- /dev/null 
> +++ b/startup/systemd/swupdate-usb.rules 
> @@ -0,0 +1,2 @@ 
> +ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block", 
> ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd", 
> ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service" 
> + 
> diff --git a/startup/systemd/swupdate-usb@.service 
> b/startup/systemd/swupdate-usb@.service 
> new file mode 100644 
> index 0000000..eda9d15 
> --- /dev/null 
> +++ b/startup/systemd/swupdate-usb@.service 
> @@ -0,0 +1,8 @@ 
> +[Unit] 
> +Description=usb media swupdate service 
> +Requires=swupdate-progress.service 
> + 
> +[Service] 
> +ExecStartPre=/bin/mount /dev/%I /mnt 
> +ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu" 
> +ExecStopPost=/bin/umount /mnt 
> diff --git a/startup/systemd/swupdate.service 
> b/startup/systemd/swupdate.service 
> new file mode 100644 
> index 0000000..a96617a 
> --- /dev/null 
> +++ b/startup/systemd/swupdate.service 
> @@ -0,0 +1,10 @@ 
> +[Unit] 
> +Description=SWUpdate daemon 
> +Documentation=https://github.com/sbabic/swupdate 
> +Documentation=https://sbabic.github.io/swupdate 
> <https://github.com/sbabic/swupdate+Documentation=https://sbabic.github.io/swupdate> 
> + 
> +[Service] 
> +ExecStart=/usr/bin/swupdate-pre 
> + 
> +[Install] 
> +WantedBy=multi-user.target 
> diff --git a/startup/systemd/swupdate.socket 
> b/startup/systemd/swupdate.socket 
> new file mode 100644 
> index 0000000..aeb7310 
> --- /dev/null 
> +++ b/startup/systemd/swupdate.socket 
> @@ -0,0 +1,11 @@ 
> +[Unit] 
> +Description=SWUpdate socket listener 
> +Documentation=https://github.com/sbabic/swupdate 
> +Documentation=https://sbabic.github.io/swupdate 
> <https://github.com/sbabic/swupdate+Documentation=https://sbabic.github.io/swupdate> 
> + 
> +[Socket] 
> +ListenStream=@@SOCKET_CTRL_PATH@@ 
> +ListenStream=@@SOCKET_PROGRESS_PATH@@ 
> + 
> +[Install] 
> +WantedBy=sockets.target 
> diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf 
> b/startup/systemd/systemd-tmpfiles-swupdate.conf 
> new file mode 100644 
> index 0000000..b374ddc 
> --- /dev/null 
> +++ b/startup/systemd/systemd-tmpfiles-swupdate.conf 
> @@ -0,0 +1,3 @@ 
> +X /tmp/datadst 
> +X /tmp/scripts 
> +X /tmp/swupdate.env 
> -- 
> 2.11.0 
>
>
Stefano Babic Oct. 9, 2019, 8:34 a.m. UTC | #4
Hi Adrian,

On 09/10/19 10:20, adrian.freihofer@gmail.com wrote:
> Hi Stefano, hi Stefan
> 
> The crucial detail is the "exec swupdate" (not "swupdate" which would
> fork) in the main script. I tested the solution also with socket
> activation. Without the exec it does not work and swupdate is
> complaining that it did not get any sockets from systemd. With the exec
> in front of swupdate, everything seems to work. swupdate notes to take
> over the sockets from systemd.

ok, nice to know - thanks for researching.

> 
> Regarding the question if we need a shell script or not in general: So
> far I always needed a few lines of shell code in front of swupdate. Once
> in the past we asked Stefano for doing a customized implementation for
> one of our projects. What we got was a shell script starting swupdate.

True, this is one way I do. I use also to set an environment file, and I
am sure someone else has some other solutions, too. What I would
maintain is the flexibility for the user to decide and not force anyone
to a fix solutions.

> Usually we have to find out on which rootfs we are running (rdev /),
> read the HW version from an eeprom and similar hardware specific things.
> Tasks which are pretty easy if a few lines of shell code are supported
> but really hard without.

True - both ways startup script and encironment file are suitable for me.

Not to forget that systemd is not in all projects and there should be no
negative changes for projects with SystemV or if there is no init system
at all (for rescue just a rcS can start SWUpdate).

> 
> Also the systemd author recommends to use a script for complicated
> things (http://0pointer.de/blog/projects/on-etc-sysinit.html). Using
> ExecStartPre to call a script writing the environment into a file which
> gets sourced by systemd before starting swupdate looks more like a
> workaround to me.

Well, why ? It is just like swupdate-pre. It finds root device, hardware
revision, and so on, and can prepare the arguments for swupdate.

> Another detail which I was struggling with this
> approach, was escaping the parameters for the mongoose and the suricatta
> command line arguments, if I tried to store them into variables.

True, this is also due to a different interpretation of variables by
systemd. For example, it evaluates correctly $var but not ${var}, as any
shell script does.

> This is
> now solved in a simple, reliable way.
>

I am not against if user can still choose and we maintain flexibility, sure.

> Regarding the changes required on meta-swupdate to make use of this
> change: A pre-view is avaliable here:
> https://github.com/afreof/meta-swupdate/tree/systemd-improvements. A
> final implementation is not possible until this one here got merged.

ok, nice that you take care of meta-swupdate, too.

Besr regards,
Stefano

> 
> Thank you for the review.
> Regards,
> Adrian
> 
> Am Dienstag, 8. Oktober 2019 17:49:47 UTC+2 schrieb Adrian Freihofer:
> 
>     If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make
>     install, a generic set of systemd service and socket files gets
>     installed.
> 
>     For most projects starting swupdate requires some logic implemented
>     in shell scripts. Therefore swupdate is started by a new shell
>     script /usr/bin/swupdate-pre. swupdate-pre evaluates code snippets
>     from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets
>     started. All modes (file, webserver, webclient) of swupdate are
>     supported. Users or distro maintainers are enabled to add simple
>     code snippets adding arguments for swupdate to the following three
>     variables:
>     - SWUPDATE_ARGS
>     - SWUPDATE_WEBSERVER_ARGS
>     - SWUPDATE_DOWNLOAD_ARGS
> 
>     This allows to provide a "just works" configuration for Mongoose
>     if it gets compiled. This was not the case with systemd before.
> 
>     Signed-off-by: Adrian Freihofer <adrian....@siemens.com <javascript:>>
>     ---
>      Makefile                                       | 20 ++++++++++++++++++
>      doc/source/swupdate.rst                        | 29
>     +++++++++++++++-----------
>      startup/systemd/10-mongoose-args               |  1 +
>      startup/systemd/swupdate-pre                   | 28
>     +++++++++++++++++++++++++
>      startup/systemd/swupdate-progress.service      |  9 ++++++++
>      startup/systemd/swupdate-usb.rules             |  2 ++
>      startup/systemd/swupdate-usb@.service          |  8 +++++++
>      startup/systemd/swupdate.service               | 10 +++++++++
>      startup/systemd/swupdate.socket                | 11 ++++++++++
>      startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++
>      10 files changed, 109 insertions(+), 12 deletions(-)
>      create mode 100644 startup/systemd/10-mongoose-args
>      create mode 100644 startup/systemd/swupdate-pre
>      create mode 100644 startup/systemd/swupdate-progress.service
>      create mode 100644 startup/systemd/swupdate-usb.rules
>      create mode 100644 startup/systemd/swupdate-usb@.service
>      create mode 100644 startup/systemd/swupdate.service
>      create mode 100644 startup/systemd/swupdate.socket
>      create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf
> 
>     diff --git a/Makefile b/Makefile
>     index d60c58e..e067009 100644
>     --- a/Makefile
>     +++ b/Makefile
>     @@ -461,6 +461,26 @@ install: all
>                      install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \
>                      install -m 0755 ${lua_swupdate}
>     $(DESTDIR)/usr/lib/lua/$(LUAVER); \
>              fi
>     +        if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \
>     +                install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>     +                install -d ${DESTDIR}/usr/lib/swupdate.d; \
>     +                install -m 755 startup/systemd/swupdate-pre
>     ${DESTDIR}/usr/bin; \
>     +                install -m 644
>     startup/systemd/swupdate-progress.service
>     ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>     +                install -m 644
>     startup/systemd/swupdate-usb@.service
>     ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>     +                install -m 644 startup/systemd/swupdate.socket
>     ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>     +                ctrl_path=${CONFIG_SOCKET_CTRL_PATH};
>     progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \
>     +                sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" \
>     +                        -e
>     "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" \
>     +                        ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket;
>     \
>     +                install -m 644 startup/systemd/swupdate.service
>     ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>     +                install -d ${DESTDIR}/usr/lib/tmpfiles.d; \
>     +                install -m 0644
>     startup/systemd/systemd-tmpfiles-swupdate.conf
>     ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \
>     +                install -d ${DESTDIR}/etc/udev/rules.d; \
>     +                install -m 0644 startup/systemd/swupdate-usb.rules
>     ${DESTDIR}/etc/udev/rules.d/; \
>     +                if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \
>     +                        install -m 644
>     startup/systemd/10-mongoose-args ${DESTDIR}/usr/lib/swupdate.d/; \
>     +                fi; \
>     +        fi
>      
>      PHONY += run-tests
>      tests: \
>     diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst
>     index 1ff08ef..ff7783e 100644
>     --- a/doc/source/swupdate.rst
>     +++ b/doc/source/swupdate.rst
>     @@ -520,22 +520,27 @@ configuration switch ``CONFIG_SYSTEMD``. If
>     enabled, SWUpdate
>      signals systemd about start-up completion and can make optional
>      use of systemd's socket-based activation feature.
>      
>     -A sample systemd service unit file
>     ``/etc/systemd/system/swupdate.service``
>     -may look like the following starting SWUpdate in suricatta daemon
>     mode:
>     +To install a generic set of service and socket files, pass the
>     +`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to
>     `/lib/systed/system`
>     +for example, to the "make install" command.
>      
>     -::
>     +For further runtime adjustments, some shell code snippets can be added
>     +to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The
>     +code snippets are processed by a full-featured shell in
>     alphabetical order.
>     +Files from /etc overwrite files from /usr/ with the same name.
>      
>     -        [Unit]
>     -        Description=SWUpdate daemon
>     -        Documentation=https://github.com/sbabic/swupdate
>     <https://github.com/sbabic/swupdate>
>     -        Documentation=https://sbabic.github.io/swupdate
>     <https://sbabic.github.io/swupdate>
>     +The purpose of these code snippets is to assign reasonable command
>     line
>     +parameters to swupdate. The following variables may be set:
>      
>     -        [Service]
>     -        Type=notify
>     -        ExecStart=/usr/bin/swupdate -u '-t default -u
>     http://localhost -i 25'
>     +- SWUPDATE_ARGS
>     +- SWUPDATE_WEBSERVER_ARGS
>     +- SWUPDATE_DOWNLOAD_ARGS
>      
>     -        [Install]
>     -        WantedBy=multi-user.target
>     +A sample configuration file ``/usr/lib/swupdate.d/10-suricatta``
>     +may look like the following starting SWUpdate in suricatta daemon
>     mode:
>     +::
>     +
>     +        SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 25'
>      
>      Started via ``systemctl start swupdate.service``, SWUpdate
>      (re)creates its sockets on startup. For using socket-based
>     diff --git a/startup/systemd/10-mongoose-args
>     b/startup/systemd/10-mongoose-args
>     new file mode 100644
>     index 0000000..896e66b
>     --- /dev/null
>     +++ b/startup/systemd/10-mongoose-args
>     @@ -0,0 +1 @@
>     +SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p
>     8080}"
>     diff --git a/startup/systemd/swupdate-pre
>     b/startup/systemd/swupdate-pre
>     new file mode 100644
>     index 0000000..2d29a12
>     --- /dev/null
>     +++ b/startup/systemd/swupdate-pre
>     @@ -0,0 +1,28 @@
>     +#!/bin/sh
>     +
>     +# Override these variables in sourced script(s) located
>     +# in /usr/lib/swupdate.d or /etc/swupdate.d
>     +SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}"
>     +SWUPDATE_WEBSERVER_ARGS=""
>     +SWUPDATE_DOWNLOAD_ARGS=""
>     +
>     +# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/
>     +# A file found in /etc replaces the same file in /usr
>     +for f in `(test -d /usr/lib/swupdate.d/ && ls -1
>     /usr/lib/swupdate.d/; test -d /etc/swupdate.d && ls -1
>     /etc/swupdate.d) | sort -u`; do
>     +  if [ -f /etc/swupdate.d/$f ]; then
>     +    . /etc/swupdate.d/$f
>     +  else
>     +    . /usr/lib/swupdate.d/$f
>     +  fi
>     +done
>     +
>     +#  handle variable escaping in a simmple way. Use exec to forward
>     open filedescriptors from systemd open.
>     +if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS"
>     != "" ]; then
>     +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w
>     "$SWUPDATE_WEBSERVER_ARGS" -u "$SWUPDATE_DOWNLOAD_ARGS"
>     +elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then
>     +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS"
>     +elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
>     +  exec /usr/bin/swupdate $SWUPDATE_ARGS -d "$SWUPDATE_DOWNLOAD_ARGS"
>     +else
>     +  exec /usr/bin/swupdate $SWUPDATE_ARGS
>     +fi
>     diff --git a/startup/systemd/swupdate-progress.service
>     b/startup/systemd/swupdate-progress.service
>     new file mode 100644
>     index 0000000..212d151
>     --- /dev/null
>     +++ b/startup/systemd/swupdate-progress.service
>     @@ -0,0 +1,9 @@
>     +[Unit]
>     +Description=swupdate progress service
>     +Before=swupdate.service
>     +
>     +[Service]
>     +ExecStart=/usr/bin/swupdate-progress -r -w
>     +
>     +[Install]
>     +WantedBy=swupdate.service
>     diff --git a/startup/systemd/swupdate-usb.rules
>     b/startup/systemd/swupdate-usb.rules
>     new file mode 100644
>     index 0000000..7bba227
>     --- /dev/null
>     +++ b/startup/systemd/swupdate-usb.rules
>     @@ -0,0 +1,2 @@
>     +ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block",
>     ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd",
>     ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service"
>     +
>     diff --git a/startup/systemd/swupdate-usb@.service
>     b/startup/systemd/swupdate-usb@.service
>     new file mode 100644
>     index 0000000..eda9d15
>     --- /dev/null
>     +++ b/startup/systemd/swupdate-usb@.service
>     @@ -0,0 +1,8 @@
>     +[Unit]
>     +Description=usb media swupdate service
>     +Requires=swupdate-progress.service
>     +
>     +[Service]
>     +ExecStartPre=/bin/mount /dev/%I /mnt
>     +ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu"
>     +ExecStopPost=/bin/umount /mnt
>     diff --git a/startup/systemd/swupdate.service
>     b/startup/systemd/swupdate.service
>     new file mode 100644
>     index 0000000..a96617a
>     --- /dev/null
>     +++ b/startup/systemd/swupdate.service
>     @@ -0,0 +1,10 @@
>     +[Unit]
>     +Description=SWUpdate daemon
>     +Documentation=https://github.com/sbabic/swupdate
>     +Documentation=https://sbabic.github.io/swupdate
>     <https://github.com/sbabic/swupdate+Documentation=https://sbabic.github.io/swupdate>
> 
>     +
>     +[Service]
>     +ExecStart=/usr/bin/swupdate-pre
>     +
>     +[Install]
>     +WantedBy=multi-user.target
>     diff --git a/startup/systemd/swupdate.socket
>     b/startup/systemd/swupdate.socket
>     new file mode 100644
>     index 0000000..aeb7310
>     --- /dev/null
>     +++ b/startup/systemd/swupdate.socket
>     @@ -0,0 +1,11 @@
>     +[Unit]
>     +Description=SWUpdate socket listener
>     +Documentation=https://github.com/sbabic/swupdate
>     +Documentation=https://sbabic.github.io/swupdate
>     <https://github.com/sbabic/swupdate+Documentation=https://sbabic.github.io/swupdate>
> 
>     +
>     +[Socket]
>     +ListenStream=@@SOCKET_CTRL_PATH@@
>     +ListenStream=@@SOCKET_PROGRESS_PATH@@
>     +
>     +[Install]
>     +WantedBy=sockets.target
>     diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf
>     b/startup/systemd/systemd-tmpfiles-swupdate.conf
>     new file mode 100644
>     index 0000000..b374ddc
>     --- /dev/null
>     +++ b/startup/systemd/systemd-tmpfiles-swupdate.conf
>     @@ -0,0 +1,3 @@
>     +X /tmp/datadst
>     +X /tmp/scripts
>     +X /tmp/swupdate.env
>     -- 
>     2.11.0
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "swupdate" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to swupdate+unsubscribe@googlegroups.com
> <mailto:swupdate+unsubscribe@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/swupdate/6421c83e-2019-4fc7-9934-6214c9a666e8%40googlegroups.com
> <https://groups.google.com/d/msgid/swupdate/6421c83e-2019-4fc7-9934-6214c9a666e8%40googlegroups.com?utm_medium=email&utm_source=footer>.
Adrian Freihofer Oct. 9, 2019, 3:13 p.m. UTC | #5
Hi Stefano

Am Mittwoch, 9. Oktober 2019 10:34:14 UTC+2 schrieb Stefano Babic:
>
> Hi Adrian, 
>
> On 09/10/19 10:20, adrian....@gmail.com <javascript:> wrote: 
> > Hi Stefano, hi Stefan 
> > 
> > The crucial detail is the "exec swupdate" (not "swupdate" which would 
> > fork) in the main script. I tested the solution also with socket 
> > activation. Without the exec it does not work and swupdate is 
> > complaining that it did not get any sockets from systemd. With the exec 
> > in front of swupdate, everything seems to work. swupdate notes to take 
> > over the sockets from systemd. 
>
> ok, nice to know - thanks for researching. 
>
> > 
> > Regarding the question if we need a shell script or not in general: So 
> > far I always needed a few lines of shell code in front of swupdate. Once 
> > in the past we asked Stefano for doing a customized implementation for 
> > one of our projects. What we got was a shell script starting swupdate. 
>
> True, this is one way I do. I use also to set an environment file, and I 
> am sure someone else has some other solutions, too. What I would 
> maintain is the flexibility for the user to decide and not force anyone 
> to a fix solutions. 
>
For swupdate in general: As long as make install is not executed with the 
environment variable SYSTEMD_SYSTEM_UNITDIR set, nothing changes, no unit 
files get installed. Regarding swupdate this change is completely opt-in.

For meta-swupdate I would like to use the SYSTEMD_SYSTEM_UNITDIR variable 
by default, if systemd is in use. The idea is to enable the user to add 
simple drop in files to /etc/swupdate.d or /usr/lib/swupdate.d with 
bbappends. The default bb will already install a drop in for mongoose, if 
it gets compiled. I think this would significantly simplify the setup of 
swupdate.

But I have to mention, that the current patch for meta-swupdate is not 100% 
backward compatible. It removes support for the environment file. I did not 
add the fallowing two lines to the service file yet.
ExecStartPre=-/usr/bin/swupdate-env
EnvironmentFile=-/tmp/swupdate.env
The reason for that is, that I was struggling several times with this 
approach as already mentioned in the previous mail.

But of course, these two lines could be added and complete backward 
compatibility could be provided. If I would add something like the 
following to swupdate-pre it would work:
if [ -f /tmp/swupdate.env ]; then
  /usr/bin/swupdate -v -w "-r /www" $SWUPDATE_EXTRA_ARGS
else
  ...all my new stuff
fi

But running swupdate with slightly different configuration would require to 
replace the ExecStart line and hence the service file. I doubt that many 
users are running swupdate with this service file right now. Most users 
probably replace this default service file with a bbappend. But I think it 
should be possible to ship a service file which is generic for different 
configurations.

Best regards,
Adrian

> Usually we have to find out on which rootfs we are running (rdev /), 
> > read the HW version from an eeprom and similar hardware specific things. 
> > Tasks which are pretty easy if a few lines of shell code are supported 
> > but really hard without. 
>
> True - both ways startup script and encironment file are suitable for me. 
>
> Not to forget that systemd is not in all projects and there should be no 
> negative changes for projects with SystemV or if there is no init system 
> at all (for rescue just a rcS can start SWUpdate). 
>
 
As long as make install is not executed with the environment variable 
SYSTEMD_SYSTEM_UNITDIR set, nothing changes for sysv systems. Regarding 
meta-swupdate I will check again that there is no change if systemd is not 
enabled.

>
> > 
> > Also the systemd author recommends to use a script for complicated 
> > things (http://0pointer.de/blog/projects/on-etc-sysinit.html). Using 
> > ExecStartPre to call a script writing the environment into a file which 
> > gets sourced by systemd before starting swupdate looks more like a 
> > workaround to me. 
>
> Well, why ? It is just like swupdate-pre. It finds root device, hardware 
> revision, and so on, and can prepare the arguments for swupdate. 
>
There is no real reason to write this stuff to a file. It's just to get the 
information from one line in the service file to the next line.

> Another detail which I was struggling with this 
> > approach, was escaping the parameters for the mongoose and the suricatta 
> > command line arguments, if I tried to store them into variables. 
>
> True, this is also due to a different interpretation of variables by 
> systemd. For example, it evaluates correctly $var but not ${var}, as any 
> shell script does. 
>
> > This is 
> > now solved in a simple, reliable way. 
> > 
>
> I am not against if user can still choose and we maintain flexibility, 
> sure. 
>
> > Regarding the changes required on meta-swupdate to make use of this 
> > change: A pre-view is avaliable here: 
> > https://github.com/afreof/meta-swupdate/tree/systemd-improvements. A 
> > final implementation is not possible until this one here got merged. 
>
> ok, nice that you take care of meta-swupdate, too. 
>
> Besr regards, 
> Stefano 
>
> > 
> > Thank you for the review. 
> > Regards, 
> > Adrian 
> > 
> > Am Dienstag, 8. Oktober 2019 17:49:47 UTC+2 schrieb Adrian Freihofer: 
> > 
> >     If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make 
> >     install, a generic set of systemd service and socket files gets 
> >     installed. 
> > 
> >     For most projects starting swupdate requires some logic implemented 
> >     in shell scripts. Therefore swupdate is started by a new shell 
> >     script /usr/bin/swupdate-pre. swupdate-pre evaluates code snippets 
> >     from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets 
> >     started. All modes (file, webserver, webclient) of swupdate are 
> >     supported. Users or distro maintainers are enabled to add simple 
> >     code snippets adding arguments for swupdate to the following three 
> >     variables: 
> >     - SWUPDATE_ARGS 
> >     - SWUPDATE_WEBSERVER_ARGS 
> >     - SWUPDATE_DOWNLOAD_ARGS 
> > 
> >     This allows to provide a "just works" configuration for Mongoose 
> >     if it gets compiled. This was not the case with systemd before. 
> > 
> >     Signed-off-by: Adrian Freihofer <adrian....@siemens.com 
> <javascript:>> 
> >     --- 
> >      Makefile                                       | 20 
> ++++++++++++++++++ 
> >      doc/source/swupdate.rst                        | 29 
> >     +++++++++++++++----------- 
> >      startup/systemd/10-mongoose-args               |  1 + 
> >      startup/systemd/swupdate-pre                   | 28 
> >     +++++++++++++++++++++++++ 
> >      startup/systemd/swupdate-progress.service      |  9 ++++++++ 
> >      startup/systemd/swupdate-usb.rules             |  2 ++ 
> >      startup/systemd/swupdate-usb@.service          |  8 +++++++ 
> >      startup/systemd/swupdate.service               | 10 +++++++++ 
> >      startup/systemd/swupdate.socket                | 11 ++++++++++ 
> >      startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++ 
> >      10 files changed, 109 insertions(+), 12 deletions(-) 
> >      create mode 100644 startup/systemd/10-mongoose-args 
> >      create mode 100644 startup/systemd/swupdate-pre 
> >      create mode 100644 startup/systemd/swupdate-progress.service 
> >      create mode 100644 startup/systemd/swupdate-usb.rules 
> >      create mode 100644 startup/systemd/swupdate-usb@.service 
> >      create mode 100644 startup/systemd/swupdate.service 
> >      create mode 100644 startup/systemd/swupdate.socket 
> >      create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf 
> > 
> >     diff --git a/Makefile b/Makefile 
> >     index d60c58e..e067009 100644 
> >     --- a/Makefile 
> >     +++ b/Makefile 
> >     @@ -461,6 +461,26 @@ install: all 
> >                      install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \ 
> >                      install -m 0755 ${lua_swupdate} 
> >     $(DESTDIR)/usr/lib/lua/$(LUAVER); \ 
> >              fi 
> >     +        if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \ 
> >     +                install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> >     +                install -d ${DESTDIR}/usr/lib/swupdate.d; \ 
> >     +                install -m 755 startup/systemd/swupdate-pre 
> >     ${DESTDIR}/usr/bin; \ 
> >     +                install -m 644 
> >     startup/systemd/swupdate-progress.service 
> >     ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> >     +                install -m 644 
> >     startup/systemd/swupdate-usb@.service 
> >     ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> >     +                install -m 644 startup/systemd/swupdate.socket 
> >     ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> >     +                ctrl_path=${CONFIG_SOCKET_CTRL_PATH}; 
> >     progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \ 
> >     +                sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" 
> \ 
> >     +                        -e 
> >     "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" \ 
> >     
> +                        ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket; 
>
> >     \ 
> >     +                install -m 644 startup/systemd/swupdate.service 
> >     ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> >     +                install -d ${DESTDIR}/usr/lib/tmpfiles.d; \ 
> >     +                install -m 0644 
> >     startup/systemd/systemd-tmpfiles-swupdate.conf 
> >     ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \ 
> >     +                install -d ${DESTDIR}/etc/udev/rules.d; \ 
> >     +                install -m 0644 startup/systemd/swupdate-usb.rules 
> >     ${DESTDIR}/etc/udev/rules.d/; \ 
> >     +                if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \ 
> >     +                        install -m 644 
> >     startup/systemd/10-mongoose-args ${DESTDIR}/usr/lib/swupdate.d/; \ 
> >     +                fi; \ 
> >     +        fi 
> >       
> >      PHONY += run-tests 
> >      tests: \ 
> >     diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst 
> >     index 1ff08ef..ff7783e 100644 
> >     --- a/doc/source/swupdate.rst 
> >     +++ b/doc/source/swupdate.rst 
> >     @@ -520,22 +520,27 @@ configuration switch ``CONFIG_SYSTEMD``. If 
> >     enabled, SWUpdate 
> >      signals systemd about start-up completion and can make optional 
> >      use of systemd's socket-based activation feature. 
> >       
> >     -A sample systemd service unit file 
> >     ``/etc/systemd/system/swupdate.service`` 
> >     -may look like the following starting SWUpdate in suricatta daemon 
> >     mode: 
> >     +To install a generic set of service and socket files, pass the 
> >     +`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to 
> >     `/lib/systed/system` 
> >     +for example, to the "make install" command. 
> >       
> >     -:: 
> >     +For further runtime adjustments, some shell code snippets can be 
> added 
> >     +to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The 
> >     +code snippets are processed by a full-featured shell in 
> >     alphabetical order. 
> >     +Files from /etc overwrite files from /usr/ with the same name. 
> >       
> >     -        [Unit] 
> >     -        Description=SWUpdate daemon 
> >     -        Documentation=https://github.com/sbabic/swupdate 
> >     <https://github.com/sbabic/swupdate> 
> >     -        Documentation=https://sbabic.github.io/swupdate 
> >     <https://sbabic.github.io/swupdate> 
> >     +The purpose of these code snippets is to assign reasonable command 
> >     line 
> >     +parameters to swupdate. The following variables may be set: 
> >       
> >     -        [Service] 
> >     -        Type=notify 
> >     -        ExecStart=/usr/bin/swupdate -u '-t default -u 
> >     http://localhost -i 25' 
> >     +- SWUPDATE_ARGS 
> >     +- SWUPDATE_WEBSERVER_ARGS 
> >     +- SWUPDATE_DOWNLOAD_ARGS 
> >       
> >     -        [Install] 
> >     -        WantedBy=multi-user.target 
> >     +A sample configuration file ``/usr/lib/swupdate.d/10-suricatta`` 
> >     +may look like the following starting SWUpdate in suricatta daemon 
> >     mode: 
> >     +:: 
> >     + 
> >     +        SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 
> 25' 
> >       
> >      Started via ``systemctl start swupdate.service``, SWUpdate 
> >      (re)creates its sockets on startup. For using socket-based 
> >     diff --git a/startup/systemd/10-mongoose-args 
> >     b/startup/systemd/10-mongoose-args 
> >     new file mode 100644 
> >     index 0000000..896e66b 
> >     --- /dev/null 
> >     +++ b/startup/systemd/10-mongoose-args 
> >     @@ -0,0 +1 @@ 
> >     +SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p 
> >     8080}" 
> >     diff --git a/startup/systemd/swupdate-pre 
> >     b/startup/systemd/swupdate-pre 
> >     new file mode 100644 
> >     index 0000000..2d29a12 
> >     --- /dev/null 
> >     +++ b/startup/systemd/swupdate-pre 
> >     @@ -0,0 +1,28 @@ 
> >     +#!/bin/sh 
> >     + 
> >     +# Override these variables in sourced script(s) located 
> >     +# in /usr/lib/swupdate.d or /etc/swupdate.d 
> >     +SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}" 
> >     +SWUPDATE_WEBSERVER_ARGS="" 
> >     +SWUPDATE_DOWNLOAD_ARGS="" 
> >     + 
> >     +# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/ 
> >     +# A file found in /etc replaces the same file in /usr 
> >     +for f in `(test -d /usr/lib/swupdate.d/ && ls -1 
> >     /usr/lib/swupdate.d/; test -d /etc/swupdate.d && ls -1 
> >     /etc/swupdate.d) | sort -u`; do 
> >     +  if [ -f /etc/swupdate.d/$f ]; then 
> >     +    . /etc/swupdate.d/$f 
> >     +  else 
> >     +    . /usr/lib/swupdate.d/$f 
> >     +  fi 
> >     +done 
> >     + 
> >     +#  handle variable escaping in a simmple way. Use exec to forward 
> >     open filedescriptors from systemd open. 
> >     +if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS" 
> >     != "" ]; then 
> >     +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w 
> >     "$SWUPDATE_WEBSERVER_ARGS" -u "$SWUPDATE_DOWNLOAD_ARGS" 
> >     +elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then 
> >     +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w 
> "$SWUPDATE_WEBSERVER_ARGS" 
> >     +elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then 
> >     +  exec /usr/bin/swupdate $SWUPDATE_ARGS -d 
> "$SWUPDATE_DOWNLOAD_ARGS" 
> >     +else 
> >     +  exec /usr/bin/swupdate $SWUPDATE_ARGS 
> >     +fi 
> >     diff --git a/startup/systemd/swupdate-progress.service 
> >     b/startup/systemd/swupdate-progress.service 
> >     new file mode 100644 
> >     index 0000000..212d151 
> >     --- /dev/null 
> >     +++ b/startup/systemd/swupdate-progress.service 
> >     @@ -0,0 +1,9 @@ 
> >     +[Unit] 
> >     +Description=swupdate progress service 
> >     +Before=swupdate.service 
> >     + 
> >     +[Service] 
> >     +ExecStart=/usr/bin/swupdate-progress -r -w 
> >     + 
> >     +[Install] 
> >     +WantedBy=swupdate.service 
> >     diff --git a/startup/systemd/swupdate-usb.rules 
> >     b/startup/systemd/swupdate-usb.rules 
> >     new file mode 100644 
> >     index 0000000..7bba227 
> >     --- /dev/null 
> >     +++ b/startup/systemd/swupdate-usb.rules 
> >     @@ -0,0 +1,2 @@ 
> >     +ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block", 
> >     ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd", 
> >     ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service" 
> >     + 
> >     diff --git a/startup/systemd/swupdate-usb@.service 
> >     b/startup/systemd/swupdate-usb@.service 
> >     new file mode 100644 
> >     index 0000000..eda9d15 
> >     --- /dev/null 
> >     +++ b/startup/systemd/swupdate-usb@.service 
> >     @@ -0,0 +1,8 @@ 
> >     +[Unit] 
> >     +Description=usb media swupdate service 
> >     +Requires=swupdate-progress.service 
> >     + 
> >     +[Service] 
> >     +ExecStartPre=/bin/mount /dev/%I /mnt 
> >     +ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu" 
> >     +ExecStopPost=/bin/umount /mnt 
> >     diff --git a/startup/systemd/swupdate.service 
> >     b/startup/systemd/swupdate.service 
> >     new file mode 100644 
> >     index 0000000..a96617a 
> >     --- /dev/null 
> >     +++ b/startup/systemd/swupdate.service 
> >     @@ -0,0 +1,10 @@ 
> >     +[Unit] 
> >     +Description=SWUpdate daemon 
> >     +Documentation=https://github.com/sbabic/swupdate 
> >     +Documentation=https://sbabic.github.io/swupdate 
> >     <
> https://github.com/sbabic/swupdate+Documentation=https://sbabic.github.io/swupdate> 
>
> > 
> >     + 
> >     +[Service] 
> >     +ExecStart=/usr/bin/swupdate-pre 
> >     + 
> >     +[Install] 
> >     +WantedBy=multi-user.target 
> >     diff --git a/startup/systemd/swupdate.socket 
> >     b/startup/systemd/swupdate.socket 
> >     new file mode 100644 
> >     index 0000000..aeb7310 
> >     --- /dev/null 
> >     +++ b/startup/systemd/swupdate.socket 
> >     @@ -0,0 +1,11 @@ 
> >     +[Unit] 
> >     +Description=SWUpdate socket listener 
> >     +Documentation=https://github.com/sbabic/swupdate 
> >     +Documentation=https://sbabic.github.io/swupdate 
> >     <
> https://github.com/sbabic/swupdate+Documentation=https://sbabic.github.io/swupdate> 
>
> > 
> >     + 
> >     +[Socket] 
> >     +ListenStream=@@SOCKET_CTRL_PATH@@ 
> >     +ListenStream=@@SOCKET_PROGRESS_PATH@@ 
> >     + 
> >     +[Install] 
> >     +WantedBy=sockets.target 
> >     diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf 
> >     b/startup/systemd/systemd-tmpfiles-swupdate.conf 
> >     new file mode 100644 
> >     index 0000000..b374ddc 
> >     --- /dev/null 
> >     +++ b/startup/systemd/systemd-tmpfiles-swupdate.conf 
> >     @@ -0,0 +1,3 @@ 
> >     +X /tmp/datadst 
> >     +X /tmp/scripts 
> >     +X /tmp/swupdate.env 
> >     -- 
> >     2.11.0 
> > 
> > -- 
> > You received this message because you are subscribed to the Google 
> > Groups "swupdate" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> > an email to swup...@googlegroups.com <javascript:> 
> > <mailto:swup...@googlegroups.com <javascript:>>. 
> > To view this discussion on the web visit 
> > 
> https://groups.google.com/d/msgid/swupdate/6421c83e-2019-4fc7-9934-6214c9a666e8%40googlegroups.com 
> > <
> https://groups.google.com/d/msgid/swupdate/6421c83e-2019-4fc7-9934-6214c9a666e8%40googlegroups.com?utm_medium=email&utm_source=footer>. 
>
>
>
> -- 
> ===================================================================== 
> DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk 
> HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany 
> Phone: +49-8142-66989-53 Fax: +49-8142-66989-80 Email: sba...@denx.de 
> <javascript:> 
> ===================================================================== 
>
Stefan Herbrechtsmeier Oct. 9, 2019, 7:20 p.m. UTC | #6
Am 08.10.19 um 17:49 schrieb Adrian Freihofer:
> If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make
> install, a generic set of systemd service and socket files gets
> installed.
> 
> For most projects starting swupdate requires some logic implemented
> in shell scripts. Therefore swupdate is started by a new shell
> script /usr/bin/swupdate-pre. 

I would prefer an other name because the pre suggest that it is run 
before swupdate and not execute swupdate by itself. Furthermore I would 
suggest to move the script out of the normal path:

/usr/lib/swupdate/swupdate.sh

Or is it expected that the user run this executable via command line?

> swupdate-pre evaluates code snippets
> from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets

Maybe /usr/lib/swupdate/conf.d?


> started. All modes (file, webserver, webclient) of swupdate are
> supported. Users or distro maintainers are enabled to add simple
> code snippets adding arguments for swupdate to the following three
> variables:
> - SWUPDATE_ARGS
> - SWUPDATE_WEBSERVER_ARGS
> - SWUPDATE_DOWNLOAD_ARGS
> 
> This allows to provide a "just works" configuration for Mongoose
> if it gets compiled. This was not the case with systemd before.
> 
> Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
> ---
>   Makefile                                       | 20 ++++++++++++++++++
>   doc/source/swupdate.rst                        | 29 +++++++++++++++-----------
>   startup/systemd/10-mongoose-args               |  1 +
>   startup/systemd/swupdate-pre                   | 28 +++++++++++++++++++++++++
>   startup/systemd/swupdate-progress.service      |  9 ++++++++
>   startup/systemd/swupdate-usb.rules             |  2 ++
>   startup/systemd/swupdate-usb@.service          |  8 +++++++
>   startup/systemd/swupdate.service               | 10 +++++++++
>   startup/systemd/swupdate.socket                | 11 ++++++++++
>   startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++
>   10 files changed, 109 insertions(+), 12 deletions(-)
>   create mode 100644 startup/systemd/10-mongoose-args
>   create mode 100644 startup/systemd/swupdate-pre
>   create mode 100644 startup/systemd/swupdate-progress.service
>   create mode 100644 startup/systemd/swupdate-usb.rules
>   create mode 100644 startup/systemd/swupdate-usb@.service
>   create mode 100644 startup/systemd/swupdate.service
>   create mode 100644 startup/systemd/swupdate.socket
>   create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf
> 
> diff --git a/Makefile b/Makefile
> index d60c58e..e067009 100644
> --- a/Makefile
> +++ b/Makefile
> @@ -461,6 +461,26 @@ install: all
>   		install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \
>   		install -m 0755 ${lua_swupdate} $(DESTDIR)/usr/lib/lua/$(LUAVER); \
>   	fi
> +	if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \

Could we please use a config option to enable (and configure) this 
feature during configuration.

> +		install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -d ${DESTDIR}/usr/lib/swupdate.d; \
> +		install -m 755 startup/systemd/swupdate-pre ${DESTDIR}/usr/bin; \
> +		install -m 644 startup/systemd/swupdate-progress.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -m 644 startup/systemd/swupdate-usb@.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -m 644 startup/systemd/swupdate.socket ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		ctrl_path=${CONFIG_SOCKET_CTRL_PATH}; progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \
> +		sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" \
> +			-e "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" \
> +			${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket; \
> +		install -m 644 startup/systemd/swupdate.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
> +		install -d ${DESTDIR}/usr/lib/tmpfiles.d; \
> +		install -m 0644 startup/systemd/systemd-tmpfiles-swupdate.conf ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \
> +		install -d ${DESTDIR}/etc/udev/rules.d; \
> +		install -m 0644 startup/systemd/swupdate-usb.rules ${DESTDIR}/etc/udev/rules.d/; \
> +		if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \
> +			install -m 644 startup/systemd/10-mongoose-args ${DESTDIR}/usr/lib/swupdate.d/; \
> +		fi; \
> +	fi
>   
>   PHONY += run-tests
>   tests: \
> diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst
> index 1ff08ef..ff7783e 100644
> --- a/doc/source/swupdate.rst
> +++ b/doc/source/swupdate.rst
> @@ -520,22 +520,27 @@ configuration switch ``CONFIG_SYSTEMD``. If enabled, SWUpdate
>   signals systemd about start-up completion and can make optional
>   use of systemd's socket-based activation feature.
>   
> -A sample systemd service unit file ``/etc/systemd/system/swupdate.service``
> -may look like the following starting SWUpdate in suricatta daemon mode:
> +To install a generic set of service and socket files, pass the
> +`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to `/lib/systed/system`
> +for example, to the "make install" command.
>   
> -::
> +For further runtime adjustments, some shell code snippets can be added
> +to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The
> +code snippets are processed by a full-featured shell in alphabetical order.
> +Files from /etc overwrite files from /usr/ with the same name.
>   
> -	[Unit]
> -	Description=SWUpdate daemon
> -	Documentation=https://github.com/sbabic/swupdate
> -	Documentation=https://sbabic.github.io/swupdate
> +The purpose of these code snippets is to assign reasonable command line
> +parameters to swupdate. The following variables may be set:
>   
> -	[Service]
> -	Type=notify
> -	ExecStart=/usr/bin/swupdate -u '-t default -u http://localhost -i 25'
> +- SWUPDATE_ARGS
> +- SWUPDATE_WEBSERVER_ARGS
> +- SWUPDATE_DOWNLOAD_ARGS
>   
> -	[Install]
> -	WantedBy=multi-user.target
> +A sample configuration file ``/usr/lib/swupdate.d/10-suricatta``
> +may look like the following starting SWUpdate in suricatta daemon mode:
> +::
> +
> +	SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 25'
>   
>   Started via ``systemctl start swupdate.service``, SWUpdate
>   (re)creates its sockets on startup. For using socket-based
> diff --git a/startup/systemd/10-mongoose-args b/startup/systemd/10-mongoose-args
> new file mode 100644
> index 0000000..896e66b
> --- /dev/null
> +++ b/startup/systemd/10-mongoose-args
> @@ -0,0 +1 @@
> +SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p 8080}"
> diff --git a/startup/systemd/swupdate-pre b/startup/systemd/swupdate-pre
> new file mode 100644
> index 0000000..2d29a12
> --- /dev/null
> +++ b/startup/systemd/swupdate-pre
> @@ -0,0 +1,28 @@
> +#!/bin/sh
> +
> +# Override these variables in sourced script(s) located
> +# in /usr/lib/swupdate.d or /etc/swupdate.d
> +SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}"
> +SWUPDATE_WEBSERVER_ARGS=""
> +SWUPDATE_DOWNLOAD_ARGS=""
> +
> +# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/
> +# A file found in /etc replaces the same file in /usr

Is it common practice to skip the /usr/lib file?

> +for f in `(test -d /usr/lib/swupdate.d/ && ls -1 /usr/lib/swupdate.d/; test -d /etc/swupdate.d && ls -1 /etc/swupdate.d) | sort -u`; do
> +  if [ -f /etc/swupdate.d/$f ]; then
> +    . /etc/swupdate.d/$f
> +  else
> +    . /usr/lib/swupdate.d/$f
> +  fi
> +done
> +
> +#  handle variable escaping in a simmple way. Use exec to forward open filedescriptors from systemd open.
> +if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS" -u "$SWUPDATE_DOWNLOAD_ARGS"
> +elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS"
> +elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -d "$SWUPDATE_DOWNLOAD_ARGS"
> +else
> +  exec /usr/bin/swupdate $SWUPDATE_ARGS
> +fi

Could you check the webserver and download args once and append them to 
the swupdate args?


> diff --git a/startup/systemd/swupdate-progress.service b/startup/systemd/swupdate-progress.service
> new file mode 100644
> index 0000000..212d151
> --- /dev/null
> +++ b/startup/systemd/swupdate-progress.service
> @@ -0,0 +1,9 @@
> +[Unit]
> +Description=swupdate progress service
> +Before=swupdate.service
> +
> +[Service]
> +ExecStart=/usr/bin/swupdate-progress -r -w
> +
> +[Install]
> +WantedBy=swupdate.service
> diff --git a/startup/systemd/swupdate-usb.rules b/startup/systemd/swupdate-usb.rules
> new file mode 100644
> index 0000000..7bba227
> --- /dev/null
> +++ b/startup/systemd/swupdate-usb.rules
> @@ -0,0 +1,2 @@
> +ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd", ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service"
> +
> diff --git a/startup/systemd/swupdate-usb@.service b/startup/systemd/swupdate-usb@.service
> new file mode 100644
> index 0000000..eda9d15
> --- /dev/null
> +++ b/startup/systemd/swupdate-usb@.service
> @@ -0,0 +1,8 @@
> +[Unit]
> +Description=usb media swupdate service
> +Requires=swupdate-progress.service
> +
> +[Service]
> +ExecStartPre=/bin/mount /dev/%I /mnt
> +ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu"
> +ExecStopPost=/bin/umount /mnt
> diff --git a/startup/systemd/swupdate.service b/startup/systemd/swupdate.service
> new file mode 100644
> index 0000000..a96617a
> --- /dev/null
> +++ b/startup/systemd/swupdate.service
> @@ -0,0 +1,10 @@
> +[Unit]
> +Description=SWUpdate daemon
> +Documentation=https://github.com/sbabic/swupdate
> +Documentation=https://sbabic.github.io/swupdate
> +
> +[Service]
> +ExecStart=/usr/bin/swupdate-pre
> +
> +[Install]
> +WantedBy=multi-user.target
> diff --git a/startup/systemd/swupdate.socket b/startup/systemd/swupdate.socket
> new file mode 100644
> index 0000000..aeb7310
> --- /dev/null
> +++ b/startup/systemd/swupdate.socket
> @@ -0,0 +1,11 @@
> +[Unit]
> +Description=SWUpdate socket listener
> +Documentation=https://github.com/sbabic/swupdate
> +Documentation=https://sbabic.github.io/swupdate
> +
> +[Socket]
> +ListenStream=@@SOCKET_CTRL_PATH@@
> +ListenStream=@@SOCKET_PROGRESS_PATH@@
> +
> +[Install]
> +WantedBy=sockets.target
> diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf b/startup/systemd/systemd-tmpfiles-swupdate.conf
> new file mode 100644
> index 0000000..b374ddc
> --- /dev/null
> +++ b/startup/systemd/systemd-tmpfiles-swupdate.conf

You could remove the systemd in the file name.

> @@ -0,0 +1,3 @@
> +X /tmp/datadst
> +X /tmp/scripts
> +X /tmp/swupdate.env
>
Stefano Babic Oct. 10, 2019, 10:22 a.m. UTC | #7
On 09/10/19 21:20, Stefan Herbrechtsmeier wrote:
> Am 08.10.19 um 17:49 schrieb Adrian Freihofer:
>> If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make
>> install, a generic set of systemd service and socket files gets
>> installed.
>>
>> For most projects starting swupdate requires some logic implemented
>> in shell scripts. Therefore swupdate is started by a new shell
>> script /usr/bin/swupdate-pre. 
> 
> I would prefer an other name because the pre suggest that it is run
> before swupdate and not execute swupdate by itself.

+1

> Furthermore I would
> suggest to move the script out of the normal path:
> 
> /usr/lib/swupdate/swupdate.sh
> 
> Or is it expected that the user run this executable via command line?

You're right. A "user" could start it just for debug purposes, but the
script is supposed to executed by systemd.

> 
>> swupdate-pre evaluates code snippets
>> from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets
> 
> Maybe /usr/lib/swupdate/conf.d?

Is there a "standard" here ? I do not know, each package seems to make
on its own. Anyway, a conf.d looks a better place.

Regards,
Stefano

> 
> 
>> started. All modes (file, webserver, webclient) of swupdate are
>> supported. Users or distro maintainers are enabled to add simple
>> code snippets adding arguments for swupdate to the following three
>> variables:
>> - SWUPDATE_ARGS
>> - SWUPDATE_WEBSERVER_ARGS
>> - SWUPDATE_DOWNLOAD_ARGS
>>
>> This allows to provide a "just works" configuration for Mongoose
>> if it gets compiled. This was not the case with systemd before.
>>
>> Signed-off-by: Adrian Freihofer <adrian.freihofer@siemens.com>
>> ---
>>   Makefile                                       | 20 ++++++++++++++++++
>>   doc/source/swupdate.rst                        | 29
>> +++++++++++++++-----------
>>   startup/systemd/10-mongoose-args               |  1 +
>>   startup/systemd/swupdate-pre                   | 28
>> +++++++++++++++++++++++++
>>   startup/systemd/swupdate-progress.service      |  9 ++++++++
>>   startup/systemd/swupdate-usb.rules             |  2 ++
>>   startup/systemd/swupdate-usb@.service          |  8 +++++++
>>   startup/systemd/swupdate.service               | 10 +++++++++
>>   startup/systemd/swupdate.socket                | 11 ++++++++++
>>   startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++
>>   10 files changed, 109 insertions(+), 12 deletions(-)
>>   create mode 100644 startup/systemd/10-mongoose-args
>>   create mode 100644 startup/systemd/swupdate-pre
>>   create mode 100644 startup/systemd/swupdate-progress.service
>>   create mode 100644 startup/systemd/swupdate-usb.rules
>>   create mode 100644 startup/systemd/swupdate-usb@.service
>>   create mode 100644 startup/systemd/swupdate.service
>>   create mode 100644 startup/systemd/swupdate.socket
>>   create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf
>>
>> diff --git a/Makefile b/Makefile
>> index d60c58e..e067009 100644
>> --- a/Makefile
>> +++ b/Makefile
>> @@ -461,6 +461,26 @@ install: all
>>           install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \
>>           install -m 0755 ${lua_swupdate}
>> $(DESTDIR)/usr/lib/lua/$(LUAVER); \
>>       fi
>> +    if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \
> 
> Could we please use a config option to enable (and configure) this
> feature during configuration.
> 
>> +        install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>> +        install -d ${DESTDIR}/usr/lib/swupdate.d; \
>> +        install -m 755 startup/systemd/swupdate-pre
>> ${DESTDIR}/usr/bin; \
>> +        install -m 644 startup/systemd/swupdate-progress.service
>> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>> +        install -m 644 startup/systemd/swupdate-usb@.service
>> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>> +        install -m 644 startup/systemd/swupdate.socket
>> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>> +        ctrl_path=${CONFIG_SOCKET_CTRL_PATH};
>> progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \
>> +        sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" \
>> +            -e "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" \
>> +            ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket; \
>> +        install -m 644 startup/systemd/swupdate.service
>> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
>> +        install -d ${DESTDIR}/usr/lib/tmpfiles.d; \
>> +        install -m 0644
>> startup/systemd/systemd-tmpfiles-swupdate.conf
>> ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \
>> +        install -d ${DESTDIR}/etc/udev/rules.d; \
>> +        install -m 0644 startup/systemd/swupdate-usb.rules
>> ${DESTDIR}/etc/udev/rules.d/; \
>> +        if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \
>> +            install -m 644 startup/systemd/10-mongoose-args
>> ${DESTDIR}/usr/lib/swupdate.d/; \
>> +        fi; \
>> +    fi
>>     PHONY += run-tests
>>   tests: \
>> diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst
>> index 1ff08ef..ff7783e 100644
>> --- a/doc/source/swupdate.rst
>> +++ b/doc/source/swupdate.rst
>> @@ -520,22 +520,27 @@ configuration switch ``CONFIG_SYSTEMD``. If
>> enabled, SWUpdate
>>   signals systemd about start-up completion and can make optional
>>   use of systemd's socket-based activation feature.
>>   -A sample systemd service unit file
>> ``/etc/systemd/system/swupdate.service``
>> -may look like the following starting SWUpdate in suricatta daemon mode:
>> +To install a generic set of service and socket files, pass the
>> +`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to `/lib/systed/system`
>> +for example, to the "make install" command.
>>   -::
>> +For further runtime adjustments, some shell code snippets can be added
>> +to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The
>> +code snippets are processed by a full-featured shell in alphabetical
>> order.
>> +Files from /etc overwrite files from /usr/ with the same name.
>>   -    [Unit]
>> -    Description=SWUpdate daemon
>> -    Documentation=https://github.com/sbabic/swupdate
>> -    Documentation=https://sbabic.github.io/swupdate
>> +The purpose of these code snippets is to assign reasonable command line
>> +parameters to swupdate. The following variables may be set:
>>   -    [Service]
>> -    Type=notify
>> -    ExecStart=/usr/bin/swupdate -u '-t default -u http://localhost -i
>> 25'
>> +- SWUPDATE_ARGS
>> +- SWUPDATE_WEBSERVER_ARGS
>> +- SWUPDATE_DOWNLOAD_ARGS
>>   -    [Install]
>> -    WantedBy=multi-user.target
>> +A sample configuration file ``/usr/lib/swupdate.d/10-suricatta``
>> +may look like the following starting SWUpdate in suricatta daemon mode:
>> +::
>> +
>> +    SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 25'
>>     Started via ``systemctl start swupdate.service``, SWUpdate
>>   (re)creates its sockets on startup. For using socket-based
>> diff --git a/startup/systemd/10-mongoose-args
>> b/startup/systemd/10-mongoose-args
>> new file mode 100644
>> index 0000000..896e66b
>> --- /dev/null
>> +++ b/startup/systemd/10-mongoose-args
>> @@ -0,0 +1 @@
>> +SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p
>> 8080}"
>> diff --git a/startup/systemd/swupdate-pre b/startup/systemd/swupdate-pre
>> new file mode 100644
>> index 0000000..2d29a12
>> --- /dev/null
>> +++ b/startup/systemd/swupdate-pre
>> @@ -0,0 +1,28 @@
>> +#!/bin/sh
>> +
>> +# Override these variables in sourced script(s) located
>> +# in /usr/lib/swupdate.d or /etc/swupdate.d
>> +SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}"
>> +SWUPDATE_WEBSERVER_ARGS=""
>> +SWUPDATE_DOWNLOAD_ARGS=""
>> +
>> +# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/
>> +# A file found in /etc replaces the same file in /usr
> 
> Is it common practice to skip the /usr/lib file?
> 
>> +for f in `(test -d /usr/lib/swupdate.d/ && ls -1
>> /usr/lib/swupdate.d/; test -d /etc/swupdate.d && ls -1
>> /etc/swupdate.d) | sort -u`; do
>> +  if [ -f /etc/swupdate.d/$f ]; then
>> +    . /etc/swupdate.d/$f
>> +  else
>> +    . /usr/lib/swupdate.d/$f
>> +  fi
>> +done
>> +
>> +#  handle variable escaping in a simmple way. Use exec to forward
>> open filedescriptors from systemd open.
>> +if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS"
>> != "" ]; then
>> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS"
>> -u "$SWUPDATE_DOWNLOAD_ARGS"
>> +elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then
>> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS"
>> +elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
>> +  exec /usr/bin/swupdate $SWUPDATE_ARGS -d "$SWUPDATE_DOWNLOAD_ARGS"
>> +else
>> +  exec /usr/bin/swupdate $SWUPDATE_ARGS
>> +fi
> 
> Could you check the webserver and download args once and append them to
> the swupdate args?
> 
> 
>> diff --git a/startup/systemd/swupdate-progress.service
>> b/startup/systemd/swupdate-progress.service
>> new file mode 100644
>> index 0000000..212d151
>> --- /dev/null
>> +++ b/startup/systemd/swupdate-progress.service
>> @@ -0,0 +1,9 @@
>> +[Unit]
>> +Description=swupdate progress service
>> +Before=swupdate.service
>> +
>> +[Service]
>> +ExecStart=/usr/bin/swupdate-progress -r -w
>> +
>> +[Install]
>> +WantedBy=swupdate.service
>> diff --git a/startup/systemd/swupdate-usb.rules
>> b/startup/systemd/swupdate-usb.rules
>> new file mode 100644
>> index 0000000..7bba227
>> --- /dev/null
>> +++ b/startup/systemd/swupdate-usb.rules
>> @@ -0,0 +1,2 @@
>> +ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block",
>> ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd",
>> ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service"
>> +
>> diff --git a/startup/systemd/swupdate-usb@.service
>> b/startup/systemd/swupdate-usb@.service
>> new file mode 100644
>> index 0000000..eda9d15
>> --- /dev/null
>> +++ b/startup/systemd/swupdate-usb@.service
>> @@ -0,0 +1,8 @@
>> +[Unit]
>> +Description=usb media swupdate service
>> +Requires=swupdate-progress.service
>> +
>> +[Service]
>> +ExecStartPre=/bin/mount /dev/%I /mnt
>> +ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu"
>> +ExecStopPost=/bin/umount /mnt
>> diff --git a/startup/systemd/swupdate.service
>> b/startup/systemd/swupdate.service
>> new file mode 100644
>> index 0000000..a96617a
>> --- /dev/null
>> +++ b/startup/systemd/swupdate.service
>> @@ -0,0 +1,10 @@
>> +[Unit]
>> +Description=SWUpdate daemon
>> +Documentation=https://github.com/sbabic/swupdate
>> +Documentation=https://sbabic.github.io/swupdate
>> +
>> +[Service]
>> +ExecStart=/usr/bin/swupdate-pre
>> +
>> +[Install]
>> +WantedBy=multi-user.target
>> diff --git a/startup/systemd/swupdate.socket
>> b/startup/systemd/swupdate.socket
>> new file mode 100644
>> index 0000000..aeb7310
>> --- /dev/null
>> +++ b/startup/systemd/swupdate.socket
>> @@ -0,0 +1,11 @@
>> +[Unit]
>> +Description=SWUpdate socket listener
>> +Documentation=https://github.com/sbabic/swupdate
>> +Documentation=https://sbabic.github.io/swupdate
>> +
>> +[Socket]
>> +ListenStream=@@SOCKET_CTRL_PATH@@
>> +ListenStream=@@SOCKET_PROGRESS_PATH@@
>> +
>> +[Install]
>> +WantedBy=sockets.target
>> diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf
>> b/startup/systemd/systemd-tmpfiles-swupdate.conf
>> new file mode 100644
>> index 0000000..b374ddc
>> --- /dev/null
>> +++ b/startup/systemd/systemd-tmpfiles-swupdate.conf
> 
> You could remove the systemd in the file name.
> 
>> @@ -0,0 +1,3 @@
>> +X /tmp/datadst
>> +X /tmp/scripts
>> +X /tmp/swupdate.env
>>
>
Adrian Freihofer Oct. 10, 2019, 6:38 p.m. UTC | #8
Hi Stefan

Am Mittwoch, 9. Oktober 2019 21:20:03 UTC+2 schrieb Stefan Herbrechtsmeier:
>
> Am 08.10.19 um 17:49 schrieb Adrian Freihofer: 
> > If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make 
> > install, a generic set of systemd service and socket files gets 
> > installed. 
> > 
> > For most projects starting swupdate requires some logic implemented 
> > in shell scripts. Therefore swupdate is started by a new shell 
> > script /usr/bin/swupdate-pre. 
>
> I would prefer an other name because the pre suggest that it is run 
> before swupdate and not execute swupdate by itself. Furthermore I would 
> suggest to move the script out of the normal path: 
>
> /usr/lib/swupdate/swupdate.sh 
>
> Or is it expected that the user run this executable via command line? 
>
Agreed, will be changed with V2. 

>
> > swupdate-pre evaluates code snippets 
> > from /usr/lib/swupdate.d and /etc/swupdate.d before swupdate gets 
>
> Maybe /usr/lib/swupdate/conf.d? 
>
Agreed, this is much better. 

>
>
> > started. All modes (file, webserver, webclient) of swupdate are 
> > supported. Users or distro maintainers are enabled to add simple 
> > code snippets adding arguments for swupdate to the following three 
> > variables: 
> > - SWUPDATE_ARGS 
> > - SWUPDATE_WEBSERVER_ARGS 
> > - SWUPDATE_DOWNLOAD_ARGS 
> > 
> > This allows to provide a "just works" configuration for Mongoose 
> > if it gets compiled. This was not the case with systemd before. 
> > 
> > Signed-off-by: Adrian Freihofer <adrian....@siemens.com <javascript:>> 
> > --- 
> >   Makefile                                       | 20 ++++++++++++++++++ 
> >   doc/source/swupdate.rst                        | 29 
> +++++++++++++++----------- 
> >   startup/systemd/10-mongoose-args               |  1 + 
> >   startup/systemd/swupdate-pre                   | 28 
> +++++++++++++++++++++++++ 
> >   startup/systemd/swupdate-progress.service      |  9 ++++++++ 
> >   startup/systemd/swupdate-usb.rules             |  2 ++ 
> >   startup/systemd/swupdate-usb@.service          |  8 +++++++ 
> >   startup/systemd/swupdate.service               | 10 +++++++++ 
> >   startup/systemd/swupdate.socket                | 11 ++++++++++ 
> >   startup/systemd/systemd-tmpfiles-swupdate.conf |  3 +++ 
> >   10 files changed, 109 insertions(+), 12 deletions(-) 
> >   create mode 100644 startup/systemd/10-mongoose-args 
> >   create mode 100644 startup/systemd/swupdate-pre 
> >   create mode 100644 startup/systemd/swupdate-progress.service 
> >   create mode 100644 startup/systemd/swupdate-usb.rules 
> >   create mode 100644 startup/systemd/swupdate-usb@.service 
> >   create mode 100644 startup/systemd/swupdate.service 
> >   create mode 100644 startup/systemd/swupdate.socket 
> >   create mode 100644 startup/systemd/systemd-tmpfiles-swupdate.conf 
> > 
> > diff --git a/Makefile b/Makefile 
> > index d60c58e..e067009 100644 
> > --- a/Makefile 
> > +++ b/Makefile 
> > @@ -461,6 +461,26 @@ install: all 
> >                   install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \ 
> >                   install -m 0755 ${lua_swupdate} 
> $(DESTDIR)/usr/lib/lua/$(LUAVER); \ 
> >           fi 
> > +        if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \ 
>
> Could we please use a config option to enable (and configure) this 
> feature during configuration. 
>
At least when thinking about meta-swupdate, I would prefer to stay without 
a config option in menuconfig. I'm thinking about a default behavior of 
meta-swupdate which sets SYSTEMD_SYSTEM_UNITDIR and tries to provide the 
best possible run-time configuration depending on the settings from the 
swupdate menuconfig file, DISTRO, MACHINE config and may be others. If the 
user needs something slightly different its easy to add some snippets to 
the conf.d folder with a bbappend. If something completely different is 
required, the do_install task can be over written. This would also allow to 
use it without defining SYSTEMD_SYSTEM_UNITDIR. But what would be the 
behavior if the user disables the conf.d feature in the menuconfig?

> +                install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> > +                install -d ${DESTDIR}/usr/lib/swupdate.d; \ 
> > +                install -m 755 startup/systemd/swupdate-pre 
> ${DESTDIR}/usr/bin; \ 
> > +                install -m 644 
> startup/systemd/swupdate-progress.service 
> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> > +                install -m 644 startup/systemd/swupdate-usb@.service 
> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> > +                install -m 644 startup/systemd/swupdate.socket 
> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> > +                ctrl_path=${CONFIG_SOCKET_CTRL_PATH}; 
> progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \ 
> > +                sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" \ 
> > +                        -e 
> "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" \ 
> > 
> +                        ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket; 
> \ 
> > +                install -m 644 startup/systemd/swupdate.service 
> ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \ 
> > +                install -d ${DESTDIR}/usr/lib/tmpfiles.d; \ 
> > +                install -m 0644 
> startup/systemd/systemd-tmpfiles-swupdate.conf 
> ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \ 
> > +                install -d ${DESTDIR}/etc/udev/rules.d; \ 
> > +                install -m 0644 startup/systemd/swupdate-usb.rules 
> ${DESTDIR}/etc/udev/rules.d/; \ 
> > +                if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \ 
> > +                        install -m 644 startup/systemd/10-mongoose-args 
> ${DESTDIR}/usr/lib/swupdate.d/; \ 
> > +                fi; \ 
> > +        fi 
> >   
> >   PHONY += run-tests 
> >   tests: \ 
> > diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst 
> > index 1ff08ef..ff7783e 100644 
> > --- a/doc/source/swupdate.rst 
> > +++ b/doc/source/swupdate.rst 
> > @@ -520,22 +520,27 @@ configuration switch ``CONFIG_SYSTEMD``. If 
> enabled, SWUpdate 
> >   signals systemd about start-up completion and can make optional 
> >   use of systemd's socket-based activation feature. 
> >   
> > -A sample systemd service unit file 
> ``/etc/systemd/system/swupdate.service`` 
> > -may look like the following starting SWUpdate in suricatta daemon mode: 
> > +To install a generic set of service and socket files, pass the 
> > +`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to 
> `/lib/systed/system` 
> > +for example, to the "make install" command. 
> >   
> > -:: 
> > +For further runtime adjustments, some shell code snippets can be added 
> > +to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The 
> > +code snippets are processed by a full-featured shell in alphabetical 
> order. 
> > +Files from /etc overwrite files from /usr/ with the same name. 
> >   
> > -        [Unit] 
> > -        Description=SWUpdate daemon 
> > -        Documentation=https://github.com/sbabic/swupdate 
> > -        Documentation=https://sbabic.github.io/swupdate 
> > +The purpose of these code snippets is to assign reasonable command line 
> > +parameters to swupdate. The following variables may be set: 
> >   
> > -        [Service] 
> > -        Type=notify 
> > -        ExecStart=/usr/bin/swupdate -u '-t default -u http://localhost 
> -i 25' 
> > +- SWUPDATE_ARGS 
> > +- SWUPDATE_WEBSERVER_ARGS 
> > +- SWUPDATE_DOWNLOAD_ARGS 
> >   
> > -        [Install] 
> > -        WantedBy=multi-user.target 
> > +A sample configuration file ``/usr/lib/swupdate.d/10-suricatta`` 
> > +may look like the following starting SWUpdate in suricatta daemon mode: 
> > +:: 
> > + 
> > +        SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 25' 
> >   
> >   Started via ``systemctl start swupdate.service``, SWUpdate 
> >   (re)creates its sockets on startup. For using socket-based 
> > diff --git a/startup/systemd/10-mongoose-args 
> b/startup/systemd/10-mongoose-args 
> > new file mode 100644 
> > index 0000000..896e66b 
> > --- /dev/null 
> > +++ b/startup/systemd/10-mongoose-args 
> > @@ -0,0 +1 @@ 
> > +SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p 
> 8080}" 
> > diff --git a/startup/systemd/swupdate-pre b/startup/systemd/swupdate-pre 
> > new file mode 100644 
> > index 0000000..2d29a12 
> > --- /dev/null 
> > +++ b/startup/systemd/swupdate-pre 
> > @@ -0,0 +1,28 @@ 
> > +#!/bin/sh 
> > + 
> > +# Override these variables in sourced script(s) located 
> > +# in /usr/lib/swupdate.d or /etc/swupdate.d 
> > +SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}" 
> > +SWUPDATE_WEBSERVER_ARGS="" 
> > +SWUPDATE_DOWNLOAD_ARGS="" 
> > + 
> > +# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/ 
> > +# A file found in /etc replaces the same file in /usr 
>
> Is it common practice to skip the /usr/lib file? 
>
It's at least an easy way to support overriding a default configuration 
completely.  

>
> > +for f in `(test -d /usr/lib/swupdate.d/ && ls -1 /usr/lib/swupdate.d/; 
> test -d /etc/swupdate.d && ls -1 /etc/swupdate.d) | sort -u`; do 
> > +  if [ -f /etc/swupdate.d/$f ]; then 
> > +    . /etc/swupdate.d/$f 
> > +  else 
> > +    . /usr/lib/swupdate.d/$f 
> > +  fi 
> > +done 
> > + 
> > +#  handle variable escaping in a simmple way. Use exec to forward open 
> filedescriptors from systemd open. 
> > +if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS" != 
> "" ]; then 
> > +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS" 
> -u "$SWUPDATE_DOWNLOAD_ARGS" 
> > +elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then 
> > +  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS" 
> > +elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then 
> > +  exec /usr/bin/swupdate $SWUPDATE_ARGS -d "$SWUPDATE_DOWNLOAD_ARGS" 
> > +else 
> > +  exec /usr/bin/swupdate $SWUPDATE_ARGS 
> > +fi 
>
> Could you check the webserver and download args once and append them to 
> the swupdate args? 
> I spent too much time on that. I did not find a clean solution where 
> escaping worked without pitfalls for the drop in snippets. This looks ugly 
> but it seems to work reliable.
>
> > diff --git a/startup/systemd/swupdate-progress.service 
> b/startup/systemd/swupdate-progress.service 
> > new file mode 100644 
> > index 0000000..212d151 
> > --- /dev/null 
> > +++ b/startup/systemd/swupdate-progress.service 
> > @@ -0,0 +1,9 @@ 
> > +[Unit] 
> > +Description=swupdate progress service 
> > +Before=swupdate.service 
> > + 
> > +[Service] 
> > +ExecStart=/usr/bin/swupdate-progress -r -w 
> > + 
> > +[Install] 
> > +WantedBy=swupdate.service 
> > diff --git a/startup/systemd/swupdate-usb.rules 
> b/startup/systemd/swupdate-usb.rules 
> > new file mode 100644 
> > index 0000000..7bba227 
> > --- /dev/null 
> > +++ b/startup/systemd/swupdate-usb.rules 
> > @@ -0,0 +1,2 @@ 
> > +ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block", 
> ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd", 
> ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service" 
> > + 
> > diff --git a/startup/systemd/swupdate-usb@.service 
> b/startup/systemd/swupdate-usb@.service 
> > new file mode 100644 
> > index 0000000..eda9d15 
> > --- /dev/null 
> > +++ b/startup/systemd/swupdate-usb@.service 
> > @@ -0,0 +1,8 @@ 
> > +[Unit] 
> > +Description=usb media swupdate service 
> > +Requires=swupdate-progress.service 
> > + 
> > +[Service] 
> > +ExecStartPre=/bin/mount /dev/%I /mnt 
> > +ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu" 
> > +ExecStopPost=/bin/umount /mnt 
> > diff --git a/startup/systemd/swupdate.service 
> b/startup/systemd/swupdate.service 
> > new file mode 100644 
> > index 0000000..a96617a 
> > --- /dev/null 
> > +++ b/startup/systemd/swupdate.service 
> > @@ -0,0 +1,10 @@ 
> > +[Unit] 
> > +Description=SWUpdate daemon 
> > +Documentation=https://github.com/sbabic/swupdate 
> > +Documentation=https://sbabic.github.io/swupdate 
> > + 
> > +[Service] 
> > +ExecStart=/usr/bin/swupdate-pre 
> > + 
> > +[Install] 
> > +WantedBy=multi-user.target 
> > diff --git a/startup/systemd/swupdate.socket 
> b/startup/systemd/swupdate.socket 
> > new file mode 100644 
> > index 0000000..aeb7310 
> > --- /dev/null 
> > +++ b/startup/systemd/swupdate.socket 
> > @@ -0,0 +1,11 @@ 
> > +[Unit] 
> > +Description=SWUpdate socket listener 
> > +Documentation=https://github.com/sbabic/swupdate 
> > +Documentation=https://sbabic.github.io/swupdate 
> > + 
> > +[Socket] 
> > +ListenStream=@@SOCKET_CTRL_PATH@@ 
> > +ListenStream=@@SOCKET_PROGRESS_PATH@@ 
> > + 
> > +[Install] 
> > +WantedBy=sockets.target 
> > diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf 
> b/startup/systemd/systemd-tmpfiles-swupdate.conf 
> > new file mode 100644 
> > index 0000000..b374ddc 
> > --- /dev/null 
> > +++ b/startup/systemd/systemd-tmpfiles-swupdate.conf 
>
> You could remove the systemd in the file name. 
>
> > @@ -0,0 +1,3 @@ 
> > +X /tmp/datadst 
> > +X /tmp/scripts 
> > +X /tmp/swupdate.env 
> > 
>
> Thank you, Stefan.
Regards,
Adrian
Stefan Herbrechtsmeier Oct. 10, 2019, 8:55 p.m. UTC | #9
Hi Adrian,

Am 10.10.19 um 20:38 schrieb adrian.freihofer@gmail.com:
> Am Mittwoch, 9. Oktober 2019 21:20:03 UTC+2 schrieb Stefan Herbrechtsmeier:
> 
>     Am 08.10.19 um 17:49 schrieb Adrian Freihofer:
>      > If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make
>      > install, a generic set of systemd service and socket files gets
>      > installed.
>      >

[snip]

>      > diff --git a/Makefile b/Makefile
>      > index d60c58e..e067009 100644
>      > --- a/Makefile
>      > +++ b/Makefile
>      > @@ -461,6 +461,26 @@ install: all
>      >                   install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \
>      >                   install -m 0755 ${lua_swupdate}
>     $(DESTDIR)/usr/lib/lua/$(LUAVER); \
>      >           fi
>      > +        if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \
> 
>     Could we please use a config option to enable (and configure) this
>     feature during configuration.
> 
> At least when thinking about meta-swupdate, I would prefer to stay 
> without a config option in menuconfig. I'm thinking about a default 
> behavior of meta-swupdate which sets SYSTEMD_SYSTEM_UNITDIR and tries to 
> provide the best possible run-time configuration depending on the 
> settings from the swupdate menuconfig file, DISTRO, MACHINE config and 
> may be others.

You could also do this with a config option by pass the options to the 
.config file.

> If the user needs something slightly different its easy 
> to add some snippets to the conf.d folder with a bbappend. If something 
> completely different is required, the do_install task can be over 
> written. This would also allow to use it without defining 
> SYSTEMD_SYSTEM_UNITDIR. But what would be the behavior if the user 
> disables the conf.d feature in the menuconfig?

You enable the feature at compile time. What happens if you run make 
without the SYSTEMD_SYSTEM_UNITDIR?

Regards
   Stefan
Adrian Freihofer Oct. 11, 2019, 2:32 p.m. UTC | #10
Am Donnerstag, 10. Oktober 2019 22:55:27 UTC+2 schrieb Stefan 
Herbrechtsmeier:
>
> Hi Adrian, 
>
> Am 10.10.19 um 20:38 schrieb adrian....@gmail.com <javascript:>: 
> > Am Mittwoch, 9. Oktober 2019 21:20:03 UTC+2 schrieb Stefan 
> Herbrechtsmeier: 
> > 
> >     Am 08.10.19 um 17:49 schrieb Adrian Freihofer: 
> >      > If SYSTEMD_SYSTEM_UNITDIR environment variable is passed to make 
> >      > install, a generic set of systemd service and socket files gets 
> >      > installed. 
> >      > 
>
> [snip] 
>
> >      > diff --git a/Makefile b/Makefile 
> >      > index d60c58e..e067009 100644 
> >      > --- a/Makefile 
> >      > +++ b/Makefile 
> >      > @@ -461,6 +461,26 @@ install: all 
> >      >                   install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \ 
> >      >                   install -m 0755 ${lua_swupdate} 
> >     $(DESTDIR)/usr/lib/lua/$(LUAVER); \ 
> >      >           fi 
> >      > +        if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \ 
> > 
> >     Could we please use a config option to enable (and configure) this 
> >     feature during configuration. 
> > 
> > At least when thinking about meta-swupdate, I would prefer to stay 
> > without a config option in menuconfig. I'm thinking about a default 
> > behavior of meta-swupdate which sets SYSTEMD_SYSTEM_UNITDIR and tries to 
> > provide the best possible run-time configuration depending on the 
> > settings from the swupdate menuconfig file, DISTRO, MACHINE config and 
> > may be others. 
>
> You could also do this with a config option by pass the options to the 
> .config file. 
>
I cannot see the advantage. But this could be added in another commit.

>  

> If the user needs something slightly different its easy 
> > to add some snippets to the conf.d folder with a bbappend. If something 
> > completely different is required, the do_install task can be over 
> > written. This would also allow to use it without defining 
> > SYSTEMD_SYSTEM_UNITDIR. But what would be the behavior if the user 
> > disables the conf.d feature in the menuconfig? 
>
> You enable the feature at compile time. What happens if you run make 
> without the SYSTEMD_SYSTEM_UNITDIR? 
>
Exactly the same as without this patch applied. No systemd configuration 
gets installed.

>
> Regards 
>    Stefan 
>
Stefan Herbrechtsmeier Oct. 11, 2019, 7:07 p.m. UTC | #11
Hi Adrian,

Am 11.10.19 um 16:32 schrieb adrian.freihofer@gmail.com:
> 
> 
> Am Donnerstag, 10. Oktober 2019 22:55:27 UTC+2 schrieb Stefan 
> Herbrechtsmeier:
> 
>     Hi Adrian,
> 
>     Am 10.10.19 um 20:38 schrieb adrian....@gmail.com <javascript:>:
>      > Am Mittwoch, 9. Oktober 2019 21:20:03 UTC+2 schrieb Stefan
>     Herbrechtsmeier:
>      >
>      >     Am 08.10.19 um 17:49 schrieb Adrian Freihofer:
>      >      > If SYSTEMD_SYSTEM_UNITDIR environment variable is passed
>     to make
>      >      > install, a generic set of systemd service and socket files
>     gets
>      >      > installed.
>      >      >
> 
>     [snip]
> 
>      >      > diff --git a/Makefile b/Makefile
>      >      > index d60c58e..e067009 100644
>      >      > --- a/Makefile
>      >      > +++ b/Makefile
>      >      > @@ -461,6 +461,26 @@ install: all
>      >      >                   install -d
>     ${DESTDIR}/usr/lib/lua/$(LUAVER); \
>      >      >                   install -m 0755 ${lua_swupdate}
>      >     $(DESTDIR)/usr/lib/lua/$(LUAVER); \
>      >      >           fi
>      >      > +        if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \
>      >
>      >     Could we please use a config option to enable (and configure)
>     this
>      >     feature during configuration.
>      >
>      > At least when thinking about meta-swupdate, I would prefer to stay
>      > without a config option in menuconfig. I'm thinking about a default
>      > behavior of meta-swupdate which sets SYSTEMD_SYSTEM_UNITDIR and
>     tries to
>      > provide the best possible run-time configuration depending on the
>      > settings from the swupdate menuconfig file, DISTRO, MACHINE
>     config and
>      > may be others.
> 
>     You could also do this with a config option by pass the options to the
>     .config file.
> 
> I cannot see the advantage. But this could be added in another commit.

There is a common feature in OE for this called PACKAGECONFIG and this 
default values often depend on DISTRO or MACHINE feature. We already 
have an uncommon configuration solution and should not extend it if we 
could avoid this.

>      > If the user needs something slightly different its easy
>      > to add some snippets to the conf.d folder with a bbappend. If
>     something
>      > completely different is required, the do_install task can be over
>      > written. This would also allow to use it without defining
>      > SYSTEMD_SYSTEM_UNITDIR. But what would be the behavior if the user
>      > disables the conf.d feature in the menuconfig?
> 
>     You enable the feature at compile time. What happens if you run make
>     without the SYSTEMD_SYSTEM_UNITDIR?
> 
> Exactly the same as without this patch applied. No systemd configuration 
> gets installed.

You move a configuration to the build / install time. Please don't do 
this. If the user want to enable this feature he should do this at 
configuration time. The same is true for the SYSTEMD_SYSTEM_UNITDIR. 
Furthermore this should have a default value which could be override at 
configuration time.

I think the common use is the following:
make menuconfig
make
make install

We shouldn't introduce an uncommon behavior if we could avoid it.

Regards
   Stefan
diff mbox series

Patch

diff --git a/Makefile b/Makefile
index d60c58e..e067009 100644
--- a/Makefile
+++ b/Makefile
@@ -461,6 +461,26 @@  install: all
 		install -d ${DESTDIR}/usr/lib/lua/$(LUAVER); \
 		install -m 0755 ${lua_swupdate} $(DESTDIR)/usr/lib/lua/$(LUAVER); \
 	fi
+	if [ "x${SYSTEMD_SYSTEM_UNITDIR}" != "x" ]; then \
+		install -d ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
+		install -d ${DESTDIR}/usr/lib/swupdate.d; \
+		install -m 755 startup/systemd/swupdate-pre ${DESTDIR}/usr/bin; \
+		install -m 644 startup/systemd/swupdate-progress.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
+		install -m 644 startup/systemd/swupdate-usb@.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
+		install -m 644 startup/systemd/swupdate.socket ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
+		ctrl_path=${CONFIG_SOCKET_CTRL_PATH}; progr_path=${CONFIG_SOCKET_PROGRESS_PATH}; \
+		sed -i -e "s,@@SOCKET_CTRL_PATH@@,$${ctrl_path},g" \
+			-e "s,@@SOCKET_PROGRESS_PATH@@,$${progr_path},g" \
+			${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}/swupdate.socket; \
+		install -m 644 startup/systemd/swupdate.service ${DESTDIR}${SYSTEMD_SYSTEM_UNITDIR}; \
+		install -d ${DESTDIR}/usr/lib/tmpfiles.d; \
+		install -m 0644 startup/systemd/systemd-tmpfiles-swupdate.conf ${DESTDIR}/usr/lib/tmpfiles.d/swupdate.conf; \
+		install -d ${DESTDIR}/etc/udev/rules.d; \
+		install -m 0644 startup/systemd/swupdate-usb.rules ${DESTDIR}/etc/udev/rules.d/; \
+		if [ "x$(CONFIG_MONGOOSE)" == "xy" ]; then \
+			install -m 644 startup/systemd/10-mongoose-args ${DESTDIR}/usr/lib/swupdate.d/; \
+		fi; \
+	fi
 
 PHONY += run-tests
 tests: \
diff --git a/doc/source/swupdate.rst b/doc/source/swupdate.rst
index 1ff08ef..ff7783e 100644
--- a/doc/source/swupdate.rst
+++ b/doc/source/swupdate.rst
@@ -520,22 +520,27 @@  configuration switch ``CONFIG_SYSTEMD``. If enabled, SWUpdate
 signals systemd about start-up completion and can make optional
 use of systemd's socket-based activation feature.
 
-A sample systemd service unit file ``/etc/systemd/system/swupdate.service``
-may look like the following starting SWUpdate in suricatta daemon mode:
+To install a generic set of service and socket files, pass the
+`SYSTEMD_SYSTEM_UNITDIR` parameter, which points to `/lib/systed/system`
+for example, to the "make install" command.
 
-::
+For further runtime adjustments, some shell code snippets can be added
+to the `/usr/lib/swupdate.d` or `/etc/swupdate.d` folders. The
+code snippets are processed by a full-featured shell in alphabetical order.
+Files from /etc overwrite files from /usr/ with the same name.
 
-	[Unit]
-	Description=SWUpdate daemon
-	Documentation=https://github.com/sbabic/swupdate
-	Documentation=https://sbabic.github.io/swupdate
+The purpose of these code snippets is to assign reasonable command line
+parameters to swupdate. The following variables may be set:
 
-	[Service]
-	Type=notify
-	ExecStart=/usr/bin/swupdate -u '-t default -u http://localhost -i 25'
+- SWUPDATE_ARGS
+- SWUPDATE_WEBSERVER_ARGS
+- SWUPDATE_DOWNLOAD_ARGS
 
-	[Install]
-	WantedBy=multi-user.target
+A sample configuration file ``/usr/lib/swupdate.d/10-suricatta``
+may look like the following starting SWUpdate in suricatta daemon mode:
+::
+
+	SWUPDATE_DOWNLOAD_ARGS='-t default -u http://localhost -i 25'
 
 Started via ``systemctl start swupdate.service``, SWUpdate
 (re)creates its sockets on startup. For using socket-based
diff --git a/startup/systemd/10-mongoose-args b/startup/systemd/10-mongoose-args
new file mode 100644
index 0000000..896e66b
--- /dev/null
+++ b/startup/systemd/10-mongoose-args
@@ -0,0 +1 @@ 
+SWUPDATE_WEBSERVER_ARGS="-r /www ${SWUPDATE_MONGOOSE_EXTRA_ARGS:--p 8080}"
diff --git a/startup/systemd/swupdate-pre b/startup/systemd/swupdate-pre
new file mode 100644
index 0000000..2d29a12
--- /dev/null
+++ b/startup/systemd/swupdate-pre
@@ -0,0 +1,28 @@ 
+#!/bin/sh
+
+# Override these variables in sourced script(s) located
+# in /usr/lib/swupdate.d or /etc/swupdate.d
+SWUPDATE_ARGS="-v ${SWUPDATE_EXTRA_ARGS}"
+SWUPDATE_WEBSERVER_ARGS=""
+SWUPDATE_DOWNLOAD_ARGS=""
+
+# source all files from /etc/swupdate.d and /usr/lib/swupdate.d/
+# A file found in /etc replaces the same file in /usr
+for f in `(test -d /usr/lib/swupdate.d/ && ls -1 /usr/lib/swupdate.d/; test -d /etc/swupdate.d && ls -1 /etc/swupdate.d) | sort -u`; do
+  if [ -f /etc/swupdate.d/$f ]; then
+    . /etc/swupdate.d/$f
+  else
+    . /usr/lib/swupdate.d/$f
+  fi
+done
+
+#  handle variable escaping in a simmple way. Use exec to forward open filedescriptors from systemd open.
+if [ "$SWUPDATE_WEBSERVER_ARGS" != "" -a  "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
+  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS" -u "$SWUPDATE_DOWNLOAD_ARGS"
+elif [ "$SWUPDATE_WEBSERVER_ARGS" != "" ]; then
+  exec /usr/bin/swupdate $SWUPDATE_ARGS -w "$SWUPDATE_WEBSERVER_ARGS"
+elif [ "$SWUPDATE_DOWNLOAD_ARGS" != "" ]; then
+  exec /usr/bin/swupdate $SWUPDATE_ARGS -d "$SWUPDATE_DOWNLOAD_ARGS"
+else
+  exec /usr/bin/swupdate $SWUPDATE_ARGS
+fi
diff --git a/startup/systemd/swupdate-progress.service b/startup/systemd/swupdate-progress.service
new file mode 100644
index 0000000..212d151
--- /dev/null
+++ b/startup/systemd/swupdate-progress.service
@@ -0,0 +1,9 @@ 
+[Unit]
+Description=swupdate progress service
+Before=swupdate.service
+
+[Service]
+ExecStart=/usr/bin/swupdate-progress -r -w
+
+[Install]
+WantedBy=swupdate.service
diff --git a/startup/systemd/swupdate-usb.rules b/startup/systemd/swupdate-usb.rules
new file mode 100644
index 0000000..7bba227
--- /dev/null
+++ b/startup/systemd/swupdate-usb.rules
@@ -0,0 +1,2 @@ 
+ACTION=="add", KERNEL=="sd*", SUBSYSTEM=="block", ENV{ID_FS_USAGE}=="filesystem", TAG+="systemd", ENV{SYSTEMD_WANTS}+="swupdate-usb@%k.service"
+
diff --git a/startup/systemd/swupdate-usb@.service b/startup/systemd/swupdate-usb@.service
new file mode 100644
index 0000000..eda9d15
--- /dev/null
+++ b/startup/systemd/swupdate-usb@.service
@@ -0,0 +1,8 @@ 
+[Unit]
+Description=usb media swupdate service
+Requires=swupdate-progress.service
+
+[Service]
+ExecStartPre=/bin/mount /dev/%I /mnt
+ExecStart=/bin/sh -c "swupdate-client -v /mnt/*.swu"
+ExecStopPost=/bin/umount /mnt
diff --git a/startup/systemd/swupdate.service b/startup/systemd/swupdate.service
new file mode 100644
index 0000000..a96617a
--- /dev/null
+++ b/startup/systemd/swupdate.service
@@ -0,0 +1,10 @@ 
+[Unit]
+Description=SWUpdate daemon
+Documentation=https://github.com/sbabic/swupdate
+Documentation=https://sbabic.github.io/swupdate
+
+[Service]
+ExecStart=/usr/bin/swupdate-pre
+
+[Install]
+WantedBy=multi-user.target
diff --git a/startup/systemd/swupdate.socket b/startup/systemd/swupdate.socket
new file mode 100644
index 0000000..aeb7310
--- /dev/null
+++ b/startup/systemd/swupdate.socket
@@ -0,0 +1,11 @@ 
+[Unit]
+Description=SWUpdate socket listener
+Documentation=https://github.com/sbabic/swupdate
+Documentation=https://sbabic.github.io/swupdate
+
+[Socket]
+ListenStream=@@SOCKET_CTRL_PATH@@
+ListenStream=@@SOCKET_PROGRESS_PATH@@
+
+[Install]
+WantedBy=sockets.target
diff --git a/startup/systemd/systemd-tmpfiles-swupdate.conf b/startup/systemd/systemd-tmpfiles-swupdate.conf
new file mode 100644
index 0000000..b374ddc
--- /dev/null
+++ b/startup/systemd/systemd-tmpfiles-swupdate.conf
@@ -0,0 +1,3 @@ 
+X /tmp/datadst
+X /tmp/scripts
+X /tmp/swupdate.env