Mosquitto MQTT on TinyCore Linux

Mosquitto MQTT on TinyCore Linux

author

akeil

date

2017-01-14

version

1

This post describes how to install the Mosquitto 1 MQTT 2 broker on a Raspberry Pi with TinyCore 3 Linux.

Build an Extension

There is no prebuilt extension for Mosquitto which means that have to build the program from source and create a custom extension for TinyCore Linux.

First, download the source and unpack:

$ wget http://mosquitto.org/files/source/mosquitto-1.4.10.tar.gz
$ tar -xzf mosquitto-1.4.10.tar.gz

Next, install build dependencies.

$ tce-load -w make
$ tce-load -w gcc
$ tce-load -w compiletc
$ tce-load -w openssl-dev
$ tce-load -w squashfs-tools

$ tce-load -i make
$ tce-load -i gcc
$ tce-load -i compiletc
$ tce-load -i openssl-dev
$ tce-load -i squashfs-tools

Edit Makefiles

There are several Makefiles in the Mosquitto project directory. Some of these contain the command install -s --strip-program foo but the --strip-program option is not supported by busybox install. Remove that option (not the call) in all affected Makefiles:

./Makefile
./lib/Makefile
./lib/cpp/Makefile
./client/Makefile
./src/Makefile

Build and Install

According to the Mosquitto README, optional dependencies are c-ares and libuuid, both of which are not available. So we will call make WITH_SRV=no WITH_UUID=no to build.

Note

The README says to use WITH_DNS_SRV=no to build without ares. We need to do this if we do not want to build ares ourselves. This is not quite correct, it is WITH_SRV=no.

$ cd mosquitto-1.4.10/
$ make
$ make WITH_SRV=no WITH_UUID=no

Install into /tmp:

$ sudo make install WITH_DOCS=no DESTDIR=/tmp/mosquitto

Installation is done as root so we have the correct permissions on all installed files.

TinyCore Linux recommends to not include the docs in the extension (optionally package them in a separate -docs extension). The WITH_DOCS=no option does exactly this.

The TinyCore Linux guide recommends to strip the created binaries. This has already been done in the Makefiles with install -s.

After installation, all relevant files are located in /tmp/mosquitto. Execute /tmp/mosquitto/usr/local/sbin/mosquitto to make a test-run.

Create the Extension

To pack the extension:

$ mksquashfs /tmp/mosquitto/ /tmp/mosquitto.tcz
$ rm -rf /tmp/mosquitto
$ mv /tmp/mosquitto.tcz /etc/sysconfig/tcedir/optional/

Then to install

$ tce-load -i mosquitto

To load on boot:

$ echo mosquitto.tcz >> /etc/sysconfig/tcedir/onboot.lst

Create User

To run Mosquitto with a dedicated user, create that user:

$ sudo adduser -S mosquitto

And edit configuration accordingly.

Configuration

The config file goes into /usr/local/etc/mosquitto.conf. This is not the default location, so we need to use the --config-file option when starting Mosquitto to tell it where to look for configuration.

Of course, the config file should be included in the list of files to be backed up and restored on boot. Include it in /opt/.filetool.lst:

usr/local/etc/mosquitto.conf

Logging

TinyCore Linux does not come with a syslog daemon. If logging is desired, write to a log file:

log_dest file /var/log/mosquitto.log

On boot, make sure that the file is writable for the mosquitto user.

Persistence

Mosquitto can write connection, subscription and message data to disk and reload it on every restart. To enable this in combination with TinyCore Linux, configure Mosquitto with persistence and include the mosquitto.db in backup/restore.

Configuration:

persistence true
persistence_location /var/lib/mosquitto/
persistence_file mosquitto.db
autosave_interval 1800

The autosave_interval means that data is saved every 1800 seconds (30 minutes). Additionally, state is stored on exit.

The persistence directory should be created and chown`ed to mosquitto on boot.

To include it in backup and restore, include it in /opt/.filetool.lst:

var/lib/mosquitto

PID File

To help with shutdown and monitoring, we let Mosquitto write a pidfile:

pid_file /var/run/mosquitto.pid

Start on boot

To start the service on boot, include this in /opt/bootlocal.sh:

# start Mosquitto in background
touch /var/log/mosquitto.log
chown mosquitto:staff /var/log/mosquitto.log
mkdir -p /var/lib/mosquitto
chown mosquitto:staff /var/lib/mosquitto
mosquitto --daemon --config-file /usr/local/etc/mosquitto.conf

This will start Mosquitto as root unless the configuration file defines the user to run as:

user mosquitto

Which is recommended.

Shutdown

To shutdown gracefully, include this in /opt/shutdown.sh:

# stop mosquitto
mosquitto_pid=$(cat /var/run/mosquitto.pid)
if [ -n "$mosquitto_pid" ]; then
    kill "$pid"
fi

Monitoring with monit

To use monit 6 to monitor Mosquitto, add these checks to monitrc (or in a separate file in monit.d/):

check process mosquitto pidfile /var/run/mosquitto.pid
    group mqtt
    if failed uid mosquitto then alert

check program mqtt-connect
with path "/usr/local/bin/mosquitto_sub --quiet -C 1 --id monit --topic $SYS/broker/version"
    group mqtt
    if status != 0 then alert

In order to check if the broker is running, connect to it and retrieve one message. We request a message from the $SYS hierarchy and pick one that is static so that we can be sure that it exists. The $SYS hierarchy is described in the Mosquitto docs 7.

mosquitto_sub comes with the Mosquitto installation an we can use it like this:

$ mosquitto_sub --quiet -C 1 --id monit --topic \$SYS/broker/version

This should be sufficient, i.e. exit with "0" if the connection was possible. The -C 1 options is necessary to exit the program after the first message. Note that you need to escape the "$" for the command line but not in monitrc.


1

http://mosquitto.org/

2

http://mqtt.org/

3

http://tinycorelinux.net/

4

http://distro.ibiblio.org/tinycorelinux/corebook.pdf

5

http://wiki.tinycorelinux.net/wiki:creating_extensions

6

https://mmonit.com/monit/

7

https://mosquitto.org/man/mosquitto-8.html#idm46187459232016