Post-install script on Yocto-built linux

久未见 提交于 2019-12-04 14:07:03

问题


I need to run a script on a target OS built by Yocto.

This script needs to be ran as part of the install and thus must be ran only once (either after the entire OS install or on the first boot). It cannot be ran on the host system, as it depends on the hardware IO which exists only on the target.

An additional, minor, constraint is that the rootfs is mounted as read only, but I guess that can be avoided by having the script re-mount as rw and again remount as r after the execution or something along those lines.

Any help is appreciated.


回答1:


I ended up doing what shibley had written. Here's a detailed howto:

Create a new layer

Put the desired layer wherever your other layers are. Mine are in stuff directory, next to the build directory.

Make the following files/directories:

meta_mylayer
├── conf
│   └── layer.conf
└── recipes-core
    └── mylayer-initscript
        ├── initscript.bb
        └── files
            ├── initscript.service
            └── initscript.sh

meta_mylayer is the name of your new layer.

Let's define the layer in conf/layer.conf and tell it where to search for the recipes:

BBPATH .= ":${LAYERDIR}"
BBFILES += "${LAYERDIR}/recipes-*/*/*.bb ${LAYERDIR}/recipes-*/*/*.bbappend"
BBFILE_COLLECTIONS += "meta-mylayer"
BBFILE_PATTERN_meta-mylayer := "^${LAYERDIR}/"
BBFILE_PRIORITY_meta-mylayer = "99"

The recipes are defined by the name of the .bb file. This layer only has one recipe, named initscript.

initscript.bb contains the recipe information. The following recipe will add our initscript service and put the actual install script, initscript.sh, into /usr/sbin/

SUMMARY = "Initial boot script"
DESCRIPTION = "Script to do any first boot init, started as a systemd service which removes itself once finished"
LICENSE = "CLOSED"
PR = "r3"

SRC_URI =  " \
    file://initscript.sh \
    file://initscript.service \
"

do_compile () {
}

do_install () {
    install -d ${D}/${sbindir}
    install -m 0755 ${WORKDIR}/initscript.sh ${D}/${sbindir}

    install -d ${D}${systemd_unitdir}/system/
    install -m 0644 ${WORKDIR}/initscript.service ${D}${systemd_unitdir}/system
}

NATIVE_SYSTEMD_SUPPORT = "1"
SYSTEMD_PACKAGES = "${PN}"
SYSTEMD_SERVICE_${PN} = "initscript.service"

inherit allarch systemd

install -d will create any directories needed for the specified path, while install -m 0644 will copy the specified file with 644 permissions. ${D} is the destination directory, by default it's ${WORKDIR}/image

Create the systemd service definition

I won't go into much details about how systemd works, but will rather paste the service definition:

[Unit]
Description=start initscript upon first boot

[Service]
Type=simple
ExecStart=/bin/sh -c 'sleep 5 ; /usr/sbin/initscript.sh'

Do note the script location at /usr/sbin/ - that's where it will be copied by the last line of our do_install function above.

Lastly, our initscript.sh script itself:

#!/bin/sh

logger "starting initscript"

# do some work here. Mount rootfs as rw if needed.

logger "initscript work done"

#job done, remove it from systemd services
systemctl disable initscript.service

logger "initscript disabled"

Register the layer

We need to register our new layer, so that bitbake knows it's there. Edit the build/conf/bblayers.conf file and add the following line to the BASELAYERS variable:

  ${TOPDIR}/../stuff/meta-mylayer \

Now that the bitbake recognizes our layer, we need to add our recipe to the image. Edit the build/conf/local.conf and add the initscript recipe to the IMAGE_INSTALL_append variable. Here's how it looks like when added next to the python.

IMAGE_INSTALL_append = " python initscript"

Run the build

Run the build like you usually do. For example:

bitbake angstrom-lxde-image

After you install the build and boot for the first time, your initscript.sh will be executed.




回答2:


The basic approach is to write a systemd service. The service can be enabled by default as defined in the yocto recipe systemd configuration. The script or application evoked by the service will disable the service when the script/application completes - ie. systemctl disable foo. Therefore, the service will not run in future boots.

As you mentioned, the rootfs will require mounting as rw for this to work.




回答3:


Thanks, this helped out. I needed to add

[Install] WantedBy=multi-user.target

to the initscript.service to get it working




回答4:


A simple solution is to use a package post/install script that stops itself running at rootfs time (exit 1 if $D is set). This will result in it running at first boot. Yes, the script will need to remount the root fs.



来源:https://stackoverflow.com/questions/40431674/post-install-script-on-yocto-built-linux

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!