Search code examples
embedded-linuxyoctobitbake

How to conditionally install and ship files in yocto bb recipe?


My yocto build/conf/auto.conf file contains a variable :

READ_ONLY_FS ?= "true"

I want to install a configuration file that can be modified, for that I want that if READ_ONLY_FS is "true", my.conf file is directly installed in /etc. But if READ_ONLY_FS is "false", I want that my.conf file is installed in /data/etc and then soft linked to /etc. (/data is a read write partition)

Currently my recipe contains this as an attempt to achieve what I wanted:

FILES_${PN} += " ${@bb.utils.contains('READ_ONLY_FS', 'true', '', '/data/${sysconfdir}/my.conf', d)}"

do_install_append() {
    install -d ${D}/${sysconfdir}
    if [ "${@bb.utils.contains('READ_ONLY_FS', 'true', 'true', 'false', d)} == "true" ]; then
        install -d ${D}/data/${sysconfdir}
        install -m 0755 ${S}/my.conf ${D}/data/${sysconfdir}/my.conf
        ln -sf ${D}/data/${sysconfdir}/my.conf ${D}/${sysconfdir}/my.conf
    else
        install -m 0755 ${S}/my.conf ${D}/${sysconfdir}/my.conf
    fi    
}

But I get an error: Files/directories were installed but not shipped. What am I doing wrong?


Solution

  • I typically create a bbappend in my own layer to append to volatile-binds.bb, eg: meta-mylayer/recipes-core/volatile-binds/volatile-binds.bbappend contains

    VOLATILE_BINDS += "\
        /data/${sysconfdir}/my.conf /${sysconfdir}/my.conf\n\
    "
    

    I have to ensure my rootfs pulls in the volatile-binds package via a package-group or IMAGE_INSTALL.

    The recipe installing my.conf does not need to be aware of this redirection, just install into ${sysconfdir}/my.conf.

    volatile-binds generates a startup script that only takes action if ${sysconfdir}/my.conf is read-only. In that case it will copy ${sysconfdir}/my.conf to /data/${sysconfdir}/my.conf (if it doesn't exist there yet) and bind-mount the latter on top of the former.

    I use

    IMAGE_FEATURES += "read-only-rootfs"
    

    in my 'read-only image' recipe.