Yocto: Conditionally setting a variable based on the image name

With one of my current build systems, I have two different build images – a production and a development one. For the production system, I want to have the root filesystem installed on the on-board eMMC storage but for the development image I want to have it installed on a removable SD card.

It is easy enough to set the value of ROOTFS_DISK in my local.conf, but (a) I’m lazy and (b) I’m prone to forgetting to change the variable and end up cursing and having to re-build the image when I realise my mistake.

What I wanted therefore was for my Yocto system to automatically use the right value depending on which image I am building.

First Steps – Export the Image Name

In order to be able to determine which image I am building, I needed to ensure that it was tagged correctly. To do this, all I needed to do was ensure I exported the IMAGE_BASENAME correctly in my image.bb files.

When you export a variable, that makes that variable available to BitBake for the rest of your recipes to use. So in my production-image.bb:

export IMAGE_BASENAME = "ming-production-image"

and in my development-image.bb:

export IMAGE_BASENAME = "ming-development-image"

Make Use of the Image Name

Now, coming back to my local.conf file, I can make use of the Yocto conditional values

ROOTFS_DEVICE = "${@'/dev/mmcblk0' if d.getVar('IMAGE_BASENAME') == 'ming-development-image' else '/dev/mmcblk1'}"

What this is doing is making use of the format

VARIABLE = "${@ 'value' if something else 'other value'}"

Firstly – the quotes are important! The whole conditional must be wrapped in double quotes ( " ) and the values in single quotes ( ' ).

The d.getVar() function returns the value of a BitBake variable (which is why the correct value had to be exported above).

I am now seeing a whole new set of places where I can be lazy (or more efficient if I’m talking to a client!)