Blog

Remotely update Raspberry Pi applications with Mender

23rd Jul 2019

When developing IoT products, there will inevitably be a need to update the software running on them.

This can be done manually or with scripts initially, but once there are more than a few dozen devices out there and the software update is more complex, such as updating an application framework or a full OS distribution, a dedicated OTA update manager is needed.

Mender is an open source OTA update manager that can update all layers of the software stack, from simple applications to robust system updates.

In this blog post, we will show how to get started with Mender on a Raspberry Pi and update a python application, and it should work on most Debian-based devices. If you have a device with a Raspbian, Debian or Ubuntu set up already, creating and deploying your first OTA update should take less than 30 minutes!

Install the Mender package

Mender is packaged as a single .deb package for ARM-v6 and newer architectures. This means that it is very easy to install on top of almost all existing Single Board Computers, such as all versions of the Raspberry Pi, BeagleBone and Rock64.

Assuming you already have your OS (Debian, Raspbian, or Ubuntu) installed on your device, run the following commands in the device terminal (or SSH) to install Mender:

wget https://d1b0l86ne08fsf.cloudfront.net/2.0.1/dist-packages/debian/armhf/mender-client_2.0.1-1_armhf.deb
sudo dpkg -i mender-client_2.0.1-1_armhf.deb

Configure the Mender server

The easiest approach is to use the Hosted Mender service as your Mender server, and this is the approach we will take in the following.

You can alternatively install the Mender server yourself, however note that you then need to configure mender.conf differently and add an entry in /etc/hosts to resolve docker.mender.io to your server. This is more complicated, so we strongly recommend you start out with Hosted Mender; you can always switch later.

Log in to Hosted Mender, go to the My organization page, under Token, click COPY TO CLIPBOARD.

Next, open up you device terminal from above and paste the following:

TENANT_TOKEN= # PASTE HERE (SHIFT + INSERT)
sudo cp /etc/mender/mender.conf.demo /etc/mender/mender.conf  # optional, this will increase polling frequency for faster responses
sudo sed -i "s/Paste your Hosted Mender token here/$TENANT_TOKEN/" /etc/mender/mender.conf
sudo mkdir -p /var/lib/mender
echo "device_type=raspberrypi3" | sudo tee /var/lib/mender/device_type # change to a descriptive device type if you are not using RPi 3
sudo systemctl enable mender && sudo systemctl start mender

Now go to the Pending devices in Hosted Mender. After a minute or two your device should show up. Simply Accept your device.

Accept device

After another minute your device information should populate and your device is now ready to receive software updates!

Download tools to create Artifacts

In Mender, all software updates are packaged in the Mender Artifact format, which has the suffix .mender. Mender Artifact files can contain any kind of software such as files, packages, containers, full system updates. We will download some tools to work with Mender Artifact files.

On your laptop/workstation, download the mender-artifact tool, which is used to create Mender Artifacts:

If you are using Linux:

wget https://d1b0l86ne08fsf.cloudfront.net/mender-artifact/master/linux/mender-artifact
chmod +x mender-artifact

If you are using Mac:

wget https://d1b0l86ne08fsf.cloudfront.net/mender-artifact/master/darwin/mender-artifact
chmod +x mender-artifact

To be able to package directories into a Mender Artifact, we also get a supporting script (compatible with Linux and Mac):

wget https://raw.githubusercontent.com/mendersoftware/mender/master/support/modules-artifact-gen/directory-artifact-gen
chmod +x directory-artifact-gen

Create a Mender Artifact

We will create a Mender Artifact that contains a directory, using the tools from above, so use the same terminal on your laptop/workstation:

Create example content to deploy:

mkdir dir-to-deploy
echo 'print("Hello from app1!")' > dir-to-deploy/app1.py
echo 'print("Hello from app2!")' > dir-to-deploy/app2.py

Now generate a Mender Artifact using the following command:

ARTIFACT_NAME="my-update-1.0"
DEVICE_TYPE="raspberrypi3"  # adjust if you used something different during Mender installation above
OUTPUT_PATH="my-update-1.0.mender"
SRC_DIR="dir-to-deploy"
DEST_DIR="/opt/installed-by-mender/"

./directory-artifact-gen -n ${ARTIFACT_NAME} -t ${DEVICE_TYPE} -d ${DEST_DIR} -o ${OUTPUT_PATH} ${SRC_DIR}

You should now have the Mender Artifact my-update-1.0.mender! For testing it contains two files (app1.py and app2.py) that will be deployed to /opt/installed-by-mender/ on the device, but this can easily be changed by adjusting SRC_DIR and DEST_DIR above.

Deploy the Artifact

Now that the device is connected and the Artifact is created, the remaining steps are to upload and deploy it.

Go to Releases in Hosted Mender and click browse on the top right. Navigate to the file my-update-1.0.mender you created above and upload it.

Next, go to Deployments and click Create a deployment. Select your Release, and All devices, as shown below.

Create deployment

It will start as pending and after a minute or two, the deployment should be successful. You can then see it under the finished deployments tab.

To verify your device has been updated, go to log in to your device and run the following commands:

ls -l /opt/installed-by-mender/
python3 /opt/installed-by-mender/app1.py
python3 /opt/installed-by-mender/app2.py

Contratulations, you have now completed your first OTA update with Mender!

Deploying other types of software

There are many different ways software can be packaged, and they can roughly be categorized as application or system updates. Mender supports deploying all of them!

Application updates

You have just tested the Directory Mender Update Module. For other types of application updates take a look at the Update Module category in the Mender Hub forum, such as the Update Module for Docker, Single File and Deb packages. You can even write your own custom Update Module to deploy different types of software!

Robust system updates

For full OS (system) updates, some board integration is required to achieve robustness (atomic rollback). First check if your device has already been integrated in the Board integration resources for your board and OS, such as the Raspberry Pi 3 for Raspbian. Note that for system/OS updates, this type of board integration is required before you ship the device and is a different way to install Mender than what we did above.