Adding a script to the root file system in PetaLinux

And making it run on start-up

Adding a script to the root file system in PetaLinux

When designing custom PetaLinux builds, we often end up with a bunch of commands that we have to run after login to set things up and make initializations. Having a script built into the root filesystem can make this process easier, so in this post I’m going to show you how you can set that up in PetaLinux 2022.1.

These instructions assume that you already have a PetaLinux project created.

Step 1: Create an app

We don’t actually want to create an application, it’s just the best way that PetaLinux allows us to do this.

  1. Source the PetaLinux settings script:

    source <path-to-petalinux-install>/2022.1/settings.sh
    
  2. Cd to your PetaLinux project and create a new application called myscript:

    cd my_project
    petalinux-create -t apps --name myscript --enable
    

Step 2: Look under the hood

Let’s go through what exactly that command has done:

  • To file my_project/project-spec/configs/rootfs_config, it has added:

    CONFIG_myscript=y
    
  • To file my_project/project-spec/meta-user/conf/user-rootfsconfig, it has added:

    CONFIG_myscript
    
  • To folder my_project/project-spec/meta-user/recipes-apps, it has added these files and folders:

    +-- myscript
        +-- files
        |   +-- Makefile
        |   +-- myscript.c
        |
        +-- myscript.bb
        +-- README
        +-- .gdbinit
    

    These files make up a template C application, but we don’t want this, we just want to add a bash script. So let’s continue with the steps.

Step 3: Modify the template

  1. Delete the following files; we don’t need them:

    • my_project/project-spec/meta-user/recipes-apps/myscript/files/Makefile
    • my_project/project-spec/meta-user/recipes-apps/myscript/files/myscript.c
  2. In that same files folder, create the following script and save it as myscript:

    #!/bin/bash
    
    echo "The Matrix has you..."
    
  3. Edit the myscript.bb file and replace it’s contents with the following:

    #
    # This file is the myscript recipe.
    #
    
    SUMMARY = "My script"
    SECTION = "PETALINUX/apps"
    LICENSE = "MIT"
    LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
    
    SRC_URI = "file://myscript \
    		  "
    
    S = "${WORKDIR}"
    
    RDEPENDS:${PN} += "bash"
    
    do_install() {
            install -d ${D}${bindir}
            install -m 0755 ${WORKDIR}/init_cams ${D}${bindir}/
    }
    

    This recipe will copy our myscript script to the /usr/bin folder so that we will be able to execute it from anywhere.

Step 4: Rebuild and test

Rebuild the PetaLinux project:

petalinux-build

Once the PetaLinux project has built, we will be able to boot it on our hardware and login. From the command line, we should be able to run our script by typing myscript:

myproject:~$ myscript
The Matrix has you...

Step 5: Make it run on start-up

Now what if we want this script to run automatically on start-up. The following changes need to made:

  1. Create a file called myscript.service with the following content and place it in my_project/project-spec/meta-user/recipes-apps/myscript/files:

    [Unit]
    Description=myscript
    
    [Service]
    ExecStart=/usr/bin/myscript
    StandardOutput=journal+console
    
    [Install]
    WantedBy=multi-user.target
    
  2. Replace the contents of myscript.bb with the following:

    #
    # This file is the myscript recipe.
    #
    
    SUMMARY = "My script that runs on boot"
    SECTION = "PETALINUX/apps"
    LICENSE = "MIT"
    
    LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302"
    
    SRC_URI = "file://myscript \
            file://myscript.service \
    "
    
    S = "${WORKDIR}"
    
    FILESEXTRAPATHS:prepend := "${THISDIR}/files:"
    
    inherit update-rc.d systemd
    
    RDEPENDS:${PN} += "bash"
    
    INITSCRIPT_NAME = "myscript"
    INITSCRIPT_PARAMS = "start 99 S ."
    
    SYSTEMD_PACKAGES = "${PN}"
    SYSTEMD_SERVICE:${PN} = "myscript.service"
    SYSTEMD_AUTO_ENABLE:${PN} = "enable"
    
    do_install() {
            if ${@bb.utils.contains('DISTRO_FEATURES', 'sysvinit', 'true', 'false', d)}; then
                    install -d ${D}${sysconfdir}/init.d/
                    install -m 0755 ${WORKDIR}/myscript ${D}${sysconfdir}/init.d/
            fi
    
            install -d ${D}${bindir}
            install -m 0755 ${WORKDIR}/myscript ${D}${bindir}/
            install -d ${D}${systemd_system_unitdir}
            install -m 0644 ${WORKDIR}/myscript.service ${D}${systemd_system_unitdir}
    }
    
    FILES:${PN} += "${@bb.utils.contains('DISTRO_FEATURES','sysvinit','${sysconfdir}/*', '', d)}"
    

    This new recipe not only copies the script to /usr/bin, it also sets it up as a service to run on boot.

  3. Rebuild the PetaLinux project:

    petalinux-build
    

Now you should be able to boot the new PetaLinux build and see in the boot log that your script has run.


Comments