Skip to main content

Mosquitto Broker Docker Cross-Compile

Guangzhou, China

In the previous article I compiled the Mosquitto broker on Arch Linux and was able to get it to work with Websocket support. The target system - an INSTAR IP camera - uses an ARM processor which means that I have to find a way to cross-compile a binary that I can run on that system:

uname -m
armv7l

There are several guides on how to install special compilers on your system. But I would prefer to run the compiler inside a docker container instead.

Preparation

Download the dockcross repository:

git clone https://github.com/dockcross/dockcross.git
cd dockcross

The repository offers containers for a variety of environments:

android-arm                 linux-armv5-musl    linux-riscv32          README.md
android-arm64 linux-armv5-uclibc linux-riscv64 test
android-x86 linux-armv6 linux-s390x tools
android-x86_64 linux-armv6-lts linux-x64 web-wasi
bare-armv7emhf-nano_newlib linux-armv6-musl linux-x64-clang web-wasm
common linux-armv7 linux-x64-tinycc windows-arm64
CONTRIBUTING.md linux-armv7a linux-x86 windows-armv7
Dockerfile.in linux-armv7a-lts linux-x86_64-full windows-shared-x64
imagefiles linux-armv7l-musl linux-xtensa-uclibc windows-shared-x64-posix
LICENSE linux-armv7-lts Makefile windows-shared-x86
linux-arm64 linux-m68k-uclibc manylinux2014-aarch64 windows-static-x64
linux-arm64-full linux-mips manylinux2014-x64 windows-static-x64-posix
linux-arm64-lts linux-mipsel-lts manylinux2014-x86 windows-static-x86
linux-arm64-musl linux-mips-lts manylinux_2_28-x64
linux-armv5 linux-ppc64le manylinux-common

You are now supposed to just run the included scripts to download the correct docker image and run an compiler script inside the container:

docker run --rm dockcross/linux-armv7l-musl > ./dockcross-linux-armv7l-musl
chmod +x ./dockcross-linux-armv7l-musl
./dockcross-linux-armv7l-musl bash -c '$CC test/C/hello.c -o hello_arm'

The test C code is a simple hello world:

#include <stdio.h>

int main(int argc, char *argv[])
{
printf("Hello cross-compilation world!\n");
return 0;
}

Running the command generates a binary inside the root dir that is ARM'ed:

file hello_arm
hello_arm: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-musl-armhf.so.1, not stripped

Compiling Mosquitto for ARM

Using an ARM platform to compile your binaries.

Platform used: Linux kali-raspberry-pi 5.15.44-Re4son-v7+ #1 SMP Debian kali-pi (2022-07-03) armv7l

Preparation

sudo apt-get update && apt-get upgrade
sudo apt-get install build-essential quilt python-setuptools python3
sudo apt-get install libssl-dev
sudo apt-get install cmake
sudo apt-get install libc-ares-dev
sudo apt-get install uuid-dev
sudo apt-get install daemon
sudo apt-get install libwebsockets-dev
sudo apt install libcjson1 libcjson-dev
sudo apt autoremove
sudo adduser mosquitto

Download the Source Code

wget https://mosquitto.org/files/source/mosquitto-2.0.15.tar.gz
tar -xvf mosquitto-2.0.15.tar.gz
rm -rf mosquitto-2.0.15.tar.gz
cd ./mosquitto-2.0.15

Configuration

sudo nano config.mk
WITH_TLS:=yes
WITH_WEBSOCKETS:=yes
WITH_STATIC_LIBRARIES:=yes
WITH_SHARED_LIBRARIES:=no
WITH_BUNDLED_DEPS:=yes
WITH_CJSON:=yes
WITH_DOCS=no

Build

make

The Build process now ran through without a hitch and returned a ARM'ed version of Mosquitto:

file src/mosquitto
src/mosquitto: ELF 32-bit LSB pie executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, BuildID[sha1]=43039f3446c03a8e22c3ca0c2378ca6bffa5ccd0, for GNU/Linux 3.2.0, with debug_info, not stripped

Installation

To install the broker run:

sudo make install

Configure the Broker with a default configuration file:

sudo nano /etc/mosquitto/mosquitto.conf
auto_id_prefix zeroid-
persistent_client_expiration 1d
queue_qos0_messages true
user mosquitto
listener 1883
protocol mqtt
listener 1885
protocol websockets
socket_domain ipv4
autosave_interval 7200
persistence true
persistence_file mosquitto.db
persistence_location /etc/mosquitto
log_dest file /etc/mosquitto/mosquitto.log
log_type error
log_type warning
log_type notice
log_type information
connection_messages true
log_timestamp true
log_timestamp_format %Y-%m-%dT%H:%M:%S
allow_anonymous false
password_file /etc/mosquitto/passwordfile

Now we need to add the passwordfile defined above using the password tool:

sudo ./apps/mosquitto_passwd/mosquitto_passwd -H sha512-pbkdf2 -b -c /etc/mosquitto/passwordfile admin instar
sudo touch /etc/mosquitto/mosquitto.log
sudo touch /etc/mosquitto/mosquitto.db
sudo chown mosquitto:mosquitto /etc/mosquitto
sudo chown mosquitto:mosquitto /etc/mosquitto/*
sudo chmod 777 /etc/mosquitto/*

And now I should be able to execute the broker:

mosquitto -c /etc/mosquitto/mosquitto.conf

./mosquitto -c /home/ipc/mosquitto.conf