yocto系列讲解 (实战篇) 62 - 文件系统中配置文件修改和systemd分区挂载方法

By: fu linux
E-mail: fulinux@sina.com
Blog: https://blog.csdn.net/fulinus
喜欢的盆友欢迎订阅!
你的喜欢就是我写作的动力!

返回总目录 Yocto开发讲解系列 - 总目录

如何文件系统中的配置文件

比如我们需要修改rootfs/etc/fstab配置文件,该文件位于一个叫base-files的项目中,该项目中还有hosts、profile、shells等文件。
因为base-files是第三方开源项目可以使用devtool工具对其进行修改:

build]$ devtool modify base-files
build]$ cd workspace/sources/base-files/
base-files]$ ls
fstab      hosts  issue.net  motd           oe-local-files  rotation  shells
host.conf  issue  licenses   nsswitch.conf  profile         share     source-date-epoch
base-files]$ vim fstab
#进行修改
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

根据自身的需要修改其他配置文件。
接着,我们提交修改,但是git那里的配置将fstab文件的改动给过滤掉了,记得使用-f配置

base-files]$ git add oe-local-files/fstab 
The following paths are ignored by one of your .gitignore files:
oe-local-files/fstab
Use -f if you really want to add them.
base-files]$ git add -f oe-local-files/fstab
base-files]$ git commit -m "modify fstab for example"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

验证无误后,将其集成到自己的meta-mylayer或者其他的meta目录中去:

build]$ devtool finish base-files meta-mybsp
  • 1

bitbake编译验证下:

build]$ cat tmp-msm8909/work/msm8909-poky-linux-gnueabi/base-files/3.0.14-r89/image/etc/fstab
#确认改动是否存在
  • 1
  • 2

分区挂载

当前我的首进程是systemd,挂载分区的方式也有自己的特点,需要添加相应的配置文件,例如:

# ls -l /lib/systemd/system/local-fs.target.wants/tmp.mount 
/lib/systemd/system/local-fs.target.wants/tmp.mount -> ../tmp.mount
/# cat /lib/systemd/system/local-fs.target.wants/tmp.mount 
#  SPDX-License-Identifier: LGPL-2.1+
#
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.

[Unit]
Description=Temporary Directory (/tmp)
Documentation=https://systemd.io/TEMPORARY_DIRECTORIES
Documentation=man:file-hierarchy(7)
Documentation=https://www.freedesktop.org/wiki/Software/systemd/APIFileSystems
ConditionPathIsSymbolicLink=!/tmp
DefaultDependencies=no
Conflicts=umount.target
Before=local-fs.target umount.target
After=swap.target

[Mount]
What=tmpfs
Where=/tmp
Type=tmpfs
Options=mode=1777,strictatime,nosuid,nodev,size=50%,nr_inodes=400k
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

因此,我们也需要针对自己的分区添加一个相应的配置文件。那么问题来了,我们怎么添加分区挂载的配置文件呢?
起初我的想法是在systemd服务之中去添加,但是我认真查看了一下这个项目,并不认为mount的配置文件应该放到systemd相关的程序中年。于是我想到了应该建立一个独立的recipe项目来负责处理分区mount的任务,就像在其他普通的程序之中添加systemd的启动服务脚本一样。我的过程大体如下:

meta-mybsp]$ cd recipes-core/
recipes-core]$ mkdir -p partitions/files #取名叫分区哈
recipes-core]$ cd partitions/
partitions]$ vim partitions_1.0.bb
# mount partitions
SUMMARY = "Mount partitions"
LICENSE = "CLOSED"

SRC_URI = "file://firmware.mount"

inherit systemd bin_package

do_install () {
        if ${@bb.utils.contains('DISTRO_FEATURES', 'systemd', 'true', 'false', d)}; then
                install -m 0644 ${WORKDIR}/firmware.mount -D ${D}${systemd_unitdir}/system/firmware.mount
                install -d ${D}${systemd_unitdir}/system/local-fs.target.wants/
                ln -sf ../firmware.mount ${D}${systemd_unitdir}/system/local-fs.target.wants/firmware.mount
        fi
}

FILES_${PN} = "${systemd_unitdir}"

#PACKAGE_STRIP = "no"
INSANE_SKIP_${PN} := "already-stripped"
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24

编译firmware.mount文件:

partitions]$ vim files/firmware.mount
[Unit]
Before=local-fs.target

[Mount]
What=/dev/disk/by-partlabel/modem
Where=/firmware
Options=noexec,nodev,ro
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

这个/dev/disk/by-partlabel/这个路径如果没有看下你的linux内核有没有打开devtmpfs的选项。
然后就是将该recipe添加进目标镜像中,参考:

meta-mybsp]$ vim recipes-sato/images/core-image-sato.bbappend
...
IMAGE_INSTALL += "partitions"
  • 1
  • 2
  • 3

编译和验证

build]$ bitbake partitions
build]$ bitbake core-image-sato
build]$ ls -l tmp/work/msm8909-poky-linux-gnueabi/core-image-sato/1.0-r0/rootfs/lib/systemd/system/local-fs.target.wants/firmware.mount 
tmp/work/msm8909-poky-linux-gnueabi/core-image-sato/1.0-r0/rootfs/lib/systemd/system/local-fs.target.wants/firmware.mount -> ../firmware.mount
  • 1
  • 2
  • 3
  • 4

然后运行再次验证下是否成功,通常是可以在实际的运行环境中先将mount文件等手动创建出来,然后验证没有问题了再集成到yocto中去。

待续~

感谢阅读!您的点赞加收藏就是我持续更新的动力!