Installing your own openhab cloud on raspbian buster - MongoDB Issues

I just upgraded to OH3 and taken the opportunity to upgrade my raspberry and openhab-cloud to the last stable versions (March 2021 - raspbian buster). Not that I had an option, anyway, due to some dependencies (i.e. Java 11).

To my despair, openhab cloud won’t start (almost silently), and I could not find any working solution googling around. So to save others some days of trial and error, here a description of the problem, and the solution (at least the one that worked for me).

2021-03-24 14:31:42:4242 error: openHAB-cloud: Error while connecting from openHAB-cloud to mongodb: MongoNetworkError: failed to connect to server [127.0.0.1:27017] on first connect [MongoError: Server at 127.0.0.1:27017 reports maximum wire version 0, but this version of the Node.js Driver requires at least 2 (MongoDB 2.6)]

Problem:

  • Openhab cloud won’t start in debian buster after an upgrade to the latest version.
  • You may find in the log something similar to the error above. It looks like the updated nodejs module needs MongoDB 2.6 or above but:
  • Last avaliable version of MongoDB in raspbian (debian) buster - armhf is 2.4.
  • There are sources in the repo for 3.2 (last version supporting 32bit) and 3.4 (not usable here as it requires 64bit arch).
  • The 3.2 source package won’t compile, under the latest versions of gcc, glibc, and other dependencies on buster.

So here the step by step guide to compile it and get it working, unless I missed something. Be aware that compiling it in a Raspberry Pi 3 will take hours, even using an external SSD drive.

1. Set-up your build environment:

mkdir -p /opt/install/mongodb
cd /opt/install/mongodb

apt-get install dpkg-dev devscripts
apt-get install libssl1.0-dev libssl1.0.2=1.0.2q-2

2. Download the rasbian source packages:

wget http://archive.raspbian.org/raspbian/pool/main/m/mongo-tools/mongo-tools_3.2.11-1_armhf.deb
wget http://archive.raspbian.org/raspbian/pool/main/m/mongodb/mongodb_3.2.11-2%2Bdeb9u2.debian.tar.xz
wget http://archive.raspbian.org/raspbian/pool/main/m/mongodb/mongodb_3.2.11-2%2Bdeb9u2.dsc
wget http://archive.raspbian.org/raspbian/pool/main/m/mongodb/mongodb_3.2.11.orig.tar.gz

dpkg-source --extract mongodb_3.2.11-2+deb9u2.dsc

3. Install build dependecies:

mk-build-deps --install mongodb_3.2.11-2+deb9u2.dsc

4. Patch the source to compile for armhf and raspbian buster:

cd mongodb-3.2.11/

patch -p1 << 'EOF'
diff -cr orig/SConstruct armhf/SConstruct
*** orig/SConstruct     2021-03-27 16:56:06.510784721 +0100
--- armhf/SConstruct    2021-03-27 16:55:33.601016436 +0100
***************
*** 933,938 ****
--- 933,939 ----
  # element tuples, or your configure checks will fail in strange ways.
  processor_macros = {
      'arm'    : { 'endian': 'little', 'defines': ('__arm__',) },
+     'armhf'    : { 'endian': 'little', 'defines': ('__arm__',) },
      'arm64'  : { 'endian': 'little', 'defines': ('__arm64__', '__aarch64__')},
      'i386'   : { 'endian': 'little', 'defines': ('__i386', '_M_IX86')},
      'ppc64le': { 'endian': 'little', 'defines': ('__powerpc64__',)},
diff -cr orig/debian/control armhf/debian/control
*** orig/debian/control 2021-03-27 16:56:47.860494223 +0100
--- armhf/debian/control        2021-03-27 16:54:14.221577298 +0100
***************
*** 36,42 ****
  Homepage: https://www.mongodb.org

  Package: mongodb
! Architecture: amd64 i386 arm64 ppc64el
  Depends: mongodb-server (>= 1:2.4.1-2), ${misc:Depends}, ${shlibs:Depends}
  Description: object/document-oriented database (metapackage)
   MongoDB is a high-performance, open source, schema-free
--- 36,42 ----
  Homepage: https://www.mongodb.org

  Package: mongodb
! Architecture: amd64 i386 arm64 ppc64el armhf
  Depends: mongodb-server (>= 1:2.4.1-2), ${misc:Depends}, ${shlibs:Depends}
  Description: object/document-oriented database (metapackage)
   MongoDB is a high-performance, open source, schema-free
***************
*** 58,64 ****
   the server, the clients and the development files (headers and library).

  Package: mongodb-server
! Architecture: amd64 i386 arm64 ppc64el
  Depends:
   mongodb-clients,
   adduser,
--- 58,64 ----
   the server, the clients and the development files (headers and library).

  Package: mongodb-server
! Architecture: amd64 i386 arm64 ppc64el armhf
  Depends:
   mongodb-clients,
   adduser,
***************
*** 87,93 ****
   server/load-balancer (mongos).

  Package: mongodb-clients
! Architecture: amd64 i386 arm64 ppc64el
  Depends:
   ${misc:Depends},
   ${shlibs:Depends}
--- 87,93 ----
   server/load-balancer (mongos).

  Package: mongodb-clients
! Architecture: amd64 i386 arm64 ppc64el armhf
  Depends:
   ${misc:Depends},
   ${shlibs:Depends}
diff -cr orig/debian/rules armhf/debian/rules
*** orig/debian/rules   2021-03-27 16:56:28.180632394 +0100
--- armhf/debian/rules  2021-03-27 16:54:37.771410607 +0100
***************
*** 38,44 ****

  # Wiredtiger does not build on 32-bit systems
  ifeq (32, $(DEB_HOST_ARCH_BITS))
! COMMON_OPTIONS += --wiredtiger=off
  endif

  override_dh_auto_clean:
--- 38,44 ----

  # Wiredtiger does not build on 32-bit systems
  ifeq (32, $(DEB_HOST_ARCH_BITS))
! COMMON_OPTIONS += --wiredtiger=off --mmapv1=on
  endif

  override_dh_auto_clean:
diff -cr orig/src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp armhf/src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp
*** orig/src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp        2021-03-27 18:38:20.321293117 +0100
--- armhf/src/mongo/db/storage/mmap_v1/mmap_v1_engine.cpp       2021-03-27 00:39:47.568367459 +0100
***************
*** 35,40 ****
--- 35,41 ----
  #include <boost/filesystem/path.hpp>
  #include <boost/filesystem/operations.hpp>
  #include <fstream>
+ #include <sys/sysmacros.h>

  #include "mongo/db/mongod_options.h"
  #include "mongo/db/storage/mmap_v1/mmap.h"
EOF

5. Configure swap:

Note for the skeptics: building mongodb requires a lot of memory. I assume you already have swap configured on a rpi 3, but if you leave standard swappiness behaviour configured while compiling, your system will start swapping and trashing. For the same reason, the build is done without parallel compiling: this will “underuse” 3 of your 4 CPUs while compìling, but several files in the build already eat all your ram when being compiled, with just one single process, so if you try to do a parallel build (I have tried) your system will trash for hours swapping instead of compiling. You have been warned :slight_smile: .

echo '1' >/proc/sys/vm/swappiness

6. Compile:

cd src/third_party/mozjs-38/
./get_sources.sh
./gen-config.sh armhf linux
cd -

mkdir tmp

TMPDIR="/opt/install/mongodb/mongodb-3.2.11/tmp" DEB_BUILD_OPTIONS="nocheck parallel=1" CXXFLAGS="-Wno-catch-value -Wno-format-truncation -Wno-class-memaccess -Wno-int-in-bool-context" dpkg-buildpackage -rfakeroot -b -uc -us

If the compile fails for whatever reason, and you fix it, you may want to resume where it stopped (or try to) instead of starting from the beginning: add ‘-nc’ at the end of the ‘dpkg-buildpackage’ command parameter list when you retry.

7. Install the newly created package and enjoy…

cd ..

dpkg -i mongodb_3.2.11-2+deb9u2_armhf.deb mongodb-server_3.2.11-2+deb9u2_armhf.deb mongodb-clients_3.2.11-2+deb9u2_armhf.deb

systemctl enable mongodb
systemctl start mongodb

8. You may want to restore default swappiness:

Note for raspberry 3 users: A system like a rpi 3, very low on memory resources, really needs swap, but you want to use it the less possible, as disks are either over USB (slow) or on a SD card (slow and worn by swap). I have checked that my system works better with low swappiness (maybe ‘1’ is too low, but good for compiling on the rpi), but this is more of a personal choice.

echo '60' >/proc/sys/vm/swappiness

Final note:

This worked for me, but happy to update it if you find some other issues compiling, and want to share. I hope this saves some time (days, indeed), as I had to dedicate those to get it working.

Pedro

What‘s the reason for running openHAB cloud instance on a local raspi ?
You need to open your local network for outside access which I think is a bad idea.

I do not like having external dependecies on my home automation, specially third party clouds. This means that:

  • If I lose my internet connection everything will still work from inside my house.
  • I still want to be able to control it from outside, and I also want Alexa and Google assistant integrations, so I need the cloud component, at least for the latter.
  • I do not want to depend on third parties (both for security and resiliency reasons) for accessing my house from the outside (of course no choice for Alexa and Google integration, but that’s an extra)

So I need my own instance. BTW, I have been using it for years :slight_smile:

1 Like