Yocto构建的Linux上的安装后脚本

编程入门 行业动态 更新时间:2024-10-28 01:14:56
本文介绍了Yocto构建的Linux上的安装后脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要在Yocto构建的目标操作系统上运行脚本.

此脚本需要在安装过程中运行,因此只能运行一次(在整个OS安装之后或首次启动时).它不能在主机系统上运行,因为它取决于仅存在于目标上的硬件IO.

另外一个较小的限制是,rootfs只能以只读方式挂载,但是我想可以通过将脚本重新挂载为rw并在执行后或类似的方式重新挂载为r来避免这种情况./p>

感谢您的帮助.

解决方案

我最终完成了 shibley 所写的内容.这是详细的方法:

创建一个新层

将所需的图层放置在其他图层的任何位置.我的位于构建目录旁边的stuff目录中.

制作以下文件/目录:

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

meta_mylayer是新层的名称.

让我们在conf/layer.conf中定义该层,并告诉它在哪里搜索食谱:

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

配方由.bb文件的名称定义.该层只有一个配方,名为 initscript .

initscript.bb包含配方信息.以下食谱将添加我们的initscript服务,并将实际的安装脚本initscript.sh放入/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将创建指定路径所需的任何目录,而install -m 0644将复制具有644权限的指定文件. ${D}是目标目录,默认情况下为${WORKDIR}/image

创建系统服务定义

我不会详细介绍systemd的工作原理,而是粘贴服务定义:

[Unit] Description=start initscript upon first boot [Service] Type=simple ExecStart=/bin/sh -c 'sleep 5 ; /usr/sbin/initscript.sh'

请注意脚本的位置/usr/sbin/-上面的do_install函数的最后一行将复制该位置.

最后,我们的initscript.sh脚本本身:

#!/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"

注册图层

我们需要注册我们的新层,以便bitbake知道它已经存在. 编辑build/conf/bblayers.conf文件,并将以下行添加到BASELAYERS变量:

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

现在,位烘焙可以识别我们的图层了,我们需要将配方添加到图像中. 编辑build/conf/local.conf并将初始化脚本配方添加到IMAGE_INSTALL_append变量.这是添加到python旁边的样子.

IMAGE_INSTALL_append = " python initscript"

运行构建

像平常一样运行构建.例如:

bitbake angstrom-lxde-image

首次安装构建并启动后,将执行initscript.sh.

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.

解决方案

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.

更多推荐

Yocto构建的Linux上的安装后脚本

本文发布于:2023-07-27 14:21:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1222830.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   Yocto   Linux

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!