yocto实战之-用户和组的添加

1.简介:

poky提供的useradd

源目录中的poky/meta-skeleton/recipes-skeleton/useradd/useradd-example.bb 配方提供了一个简单的示例,展示了如何将三个用户和组添加到两个包中。

2.边看边做

2.1.学前知识

在了解该配方文件之前,需要先了解useradd的相关参数

useradd --help
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]
 
Options:
      --badnames                do not check for bad names
  -b, --base-dir BASE_DIR       base directory for the home directory of the
                                new account
      --btrfs-subvolume-home    use BTRFS subvolume for home directory
  -c, --comment COMMENT         GECOS field of the new account
  -d, --home-dir HOME_DIR       用户登录后的home目录
  -D, --defaults                print or change default useradd configuration
  -e, --expiredate EXPIRE_DATE  expiration date of the new account
  -f, --inactive INACTIVE       password inactivity period of the new account
  -g, --gid GROUP               name or ID of the primary group of the new
    
                             account
  -G, --groups GROUPS           list of supplementary groups of the new
                                account
  -h, --help                    display this help message and exit
  -k, --skel SKEL_DIR           use this alternative skeleton directory
  -K, --key KEY=VALUE           override /etc/login.defs defaults
  -l, --no-log-init             do not add the user to the lastlog and
                                faillog databases
  -m, --create-home             create the user's home directory
  -M, --no-create-home          do not create the user's home directory
  -N, --no-user-group           do not create a group with the same name as
                                the user
  -o, --non-unique              allow to create users with duplicate
                                (non-unique) UID
  -p, --password PASSWORD       加密后的用户密码
  -r, --system                  系统账户
  -R, --root CHROOT_DIR         directory to chroot into
  -P, --prefix PREFIX_DIR       prefix directory where are located the /etc/* files
  -s, --shell SHELL             用户登录后的shell
  -u, --uid UID                 用户ID
  -U, --user-group              create a group with the same name as the user
  -Z, --selinux-user SEUSER     use a specific SEUSER for the SELinux user mapping
      --extrausers              Use the extra users database

2.2.学习官网例子

接下来我们看yocto的官方例子:

SUMMARY = "Example recipe for using inherit useradd"
DESCRIPTION = "This recipe serves as an example for using features from useradd.bbclass"
SECTION = "examples"
LICENSE = "MIT"
LIC_FILES_CHKSUM = "file://${COREBASE}/meta/COPYING.MIT;md5=3da9cfbcb788c80a0384361b4de20420"

SRC_URI = "file://file1 \
           file://file2 \
           file://file3 \
           file://file4"

S = "${WORKDIR}"

PACKAGES =+ "${PN}-user3"

EXCLUDE_FROM_WORLD = "1"

inherit useradd

# You must set USERADD_PACKAGES when you inherit useradd. This
# lists which output packages will include the user/group
# creation code.
USERADD_PACKAGES = "${PN} ${PN}-user3"

# You must also set USERADD_PARAM and/or GROUPADD_PARAM when
# you inherit useradd.

# USERADD_PARAM specifies command line options to pass to the
# useradd command. Multiple users can be created by separating
# the commands with a semicolon. Here we'll create two users,
# user1 and user2:
USERADD_PARAM:${PN} = "-u 1200 -d /home/user1 -r -s /bin/bash user1; -u 1201 -d /home/user2 -r -s /bin/bash user2"

# user3 will be managed in the useradd-example-user3 package:
# As an example, we use the -p option to set password ('user3') for user3
USERADD_PARAM:${PN}-user3 = "-u 1202 -d /home/user3 -r -s /bin/bash -p '\$6\$XAWr.8nc\$bUE4pYYaVb8n6BbnBitU0zeJMtfhTpFpiOBLL9zRl4e4YQo88UU4r/1kjRzmTimCy.BvDh4xoFwVqcO.pihLa1' user3"

# GROUPADD_PARAM works the same way, which you set to the options
# you'd normally pass to the groupadd command. This will create
# groups group1 and group2:
GROUPADD_PARAM:${PN} = "-g 880 group1; -g 890 group2"

# Likewise, we'll manage group3 in the useradd-example-user3 package:
GROUPADD_PARAM:${PN}-user3 = "-g 900 group3"

do_install () {
	install -d -m 755 ${D}${datadir}/user1
	install -d -m 755 ${D}${datadir}/user2
	install -d -m 755 ${D}${datadir}/user3

	install -p -m 644 file1 ${D}${datadir}/user1/
	install -p -m 644 file2 ${D}${datadir}/user1/

	install -p -m 644 file2 ${D}${datadir}/user2/
	install -p -m 644 file3 ${D}${datadir}/user2/

	install -p -m 644 file3 ${D}${datadir}/user3/
	install -p -m 644 file4 ${D}${datadir}/user3/

	# The new users and groups are created before the do_install
	# step, so you are now free to make use of them:
	chown -R user1 ${D}${datadir}/user1
	chown -R user2 ${D}${datadir}/user2
	chown -R user3 ${D}${datadir}/user3

	chgrp -R group1 ${D}${datadir}/user1
	chgrp -R group2 ${D}${datadir}/user2
	chgrp -R group3 ${D}${datadir}/user3
}

FILES:${PN} = "${datadir}/user1/* ${datadir}/user2/*"
FILES:${PN}-user3 = "${datadir}/user3/*"

# Prevents do_package failures with:
# debugsources.list: No such file or directory:
INHIBIT_PACKAGE_DEBUG_SPLIT = "1"

这个 Yocto Recipe 示例用来演示如何使用 useradd.bbclass 类进行用户和组的管理。以下是代码的详细解释:

2.2.1.概要

  • SUMMARYDESCRIPTION: 这两个字段简要说明了这个配方的作用,即演示如何使用 useradd.bbclass 来创建用户和组。

2.2.2.主要部分

  • SECTION: 表示这个配方属于 "examples" 部分。
  • LICENSELIC_FILES_CHKSUM: 定义了许可类型为 MIT,并指定了一个校验和,用于验证许可证文件的完整性。

2.2.3.源文件

  • SRC_URI: 列出了四个源文件 file1, file2, file3, 和 file4,这些文件将被包括在配方的构建中。
  • S: 指定了构建目录为 ${WORKDIR}

2.2.4.软件包配置

  • PACKAGES: 定义了额外的输出包 ${PN}-user3,其中 ${PN} 是配方的名称。
  • EXCLUDE_FROM_WORLD: 将这个配方从默认的构建列表中排除。

2.2.5.继承与用户管理

  • inherit useradd: 继承 useradd.bbclass,用于处理用户和组的创建。

  • USERADD_PACKAGES: 指定了哪些包将包含用户和组的创建逻辑。在这里是 ${PN}(配方的主包)和 ${PN}-user3

  • USERADD_PARAM: 定义了创建用户的参数。

    • 对于 ${PN} 包,将创建两个用户 user1user2,分别使用 UID 1200 和 1201,并指定了它们的主目录和默认 Shell。
    • 对于 ${PN}-user3 包,将创建用户 user3,并设置了其加密的密码。
      • 加密方法参考(-6是使用SHA512进行加密,-5是使用SHA256进行加密,-1是使用MD5进行加密):
        openssl passwd -6 123456
        $6$wncBdJ0QEjsQFVM4$pjZdOAzUOu4ZkVynaOD6XuUOCspFxH.jKXh3dD9p8aYpEPdJqm1aSD20cI7DKtDjUrNkbpHLZMFkADXbZKNMN0
  • GROUPADD_PARAM: 类似于 USERADD_PARAM,定义了创建组的参数。

    • 对于 ${PN} 包,将创建两个组 group1group2,分别使用 GID 880 和 890。
    • 对于 ${PN}-user3 包,将创建组 group3,使用 GID 900。

2.2.6.安装阶段

  • do_install(): 安装操作的函数。
    • 创建了 /usr/share/user1, /usr/share/user2, /usr/share/user3 目录,并将相应的文件安装到这些目录下。
    • 使用 chownchgrp 命令设置了目录的所有者和组,分别为 user1group1user2group2,以及 user3group3

2.2.7.文件打包

  • FILES: 定义了包中包含的文件。
    • ${PN} 包含 /usr/share/user1//usr/share/user2/ 下的所有文件。
    • ${PN}-user3 包含 /usr/share/user3/ 下的所有文件。

2.2.8.其他

  • INHIBIT_PACKAGE_DEBUG_SPLIT: 设置为 "1" 来避免在 do_package 阶段出现与调试信息相关的错误。

2.3.添加自己的功能

2.3.1.方法一:

假如我们想添加exampleuser用户和examplegroup组

  • 1.在自己的层添加一个.bb文件,例如add-user-group_1.0.bb
SUMMARY = "Custom users and groups for the system"
DESCRIPTION = "This recipe creates custom users and groups for the system"
LICENSE = "CLOSED"
PR = "r0"

inherit useradd

USERADD_PACKAGES = "${PN}"
USERADD_PARAM:${PN} = "-u 1000 -d /home/exampleuser -r -s /bin/bash exampleuser"
GROUPADD_PARAM:${PN} = "examplegroup"

do_install() {
    install -d ${D}/home/exampleuser
}

FILES:${PN} += "/home/exampleuser"

  • 2.同时在生成的image.bb中添加对应的行,确保制作image的时候能将add-user-group_1.0.bb添加到image中
...

CORE_IMAGE_BASE_INSTALL += "add-user-group"
  • 3.编译验证
cat /etc/group

cat /etc/passwd

2.3.2.方法二:

使用openssl生成想要的密码

openssl passwd "123456"

添加设置到local.conf中

INHERIT += "extrausers"
EXTRA_USERS_PARAMS += "\
     usermod -p '\$1\$txora4AG\$bLTnl8BoM32cgNUsqWMc6.' root; \
     useradd test; \
     usermod -p '\$1\$txora4AG\$bLTnl8BoM32cgNUsqWMc6.' test; \
"

  • 12
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Yocto Project 中配置 camera-gl-wl 软件包时,需要进行以下步骤: 1. 在你的 `local.conf` 文件中添加以下内容: ``` DISTRO_FEATURES_append += " opengl" PREFERRED_PROVIDER_virtual/egl ?= "mesa" PREFERRED_PROVIDER_virtual/libgles1 ?= "mesa" PREFERRED_PROVIDER_virtual/libgles2 ?= "mesa" ``` 这些配置将启用 OpenGL 特性并设置默认的 EGL 和 GLES 库为 Mesa。 2. 执行以下命令以安装必要的库: ``` $ sudo apt-get install libgles2-mesa-dev libegl1-mesa-dev ``` 3. 在你的 `bblayers.conf` 文件中添加以下内容: ``` BBLAYERS ?= " \ /path/to/yocto/meta \ /path/to/yocto/meta-poky \ /path/to/yocto/meta-yocto-bsp \ /path/to/yocto/meta-oe \ /path/to/yocto/meta-qt5 \ /path/to/yocto/meta-openembedded/meta-oe \ /path/to/yocto/meta-openembedded/meta-multimedia \ /path/to/yocto/meta-openembedded/meta-egl \ /path/to/yocto/meta-openembedded/meta-python \ /path/to/yocto/meta-openembedded/meta-networking \ " ``` 这些配置将添加必要的 OpenEmbedded 层以支持 EGL 和 GLES 库。 4. 添加 camera-gl-wl 软件包的 recipe 文件到你的 Yocto 工程中。 在 recipe 文件中,你需要定义软件包的名称、版本、源代码地址、依赖项等。具体实现可以参考其他已有的 recipe 文件。 例如: ``` SUMMARY = "Camera application with OpenGL and Wayland support" DESCRIPTION = "This package provides a camera application that supports OpenGL and Wayland." LICENSE = "MIT" LIC_FILES_CHKSUM = "file://${COMMON_LICENSE_DIR}/MIT;md5=0835ade698e0bcf8506ecda2f7b4f302" SRC_URI = "git://github.com/user/camera-gl-wl.git;protocol=https" DEPENDS = "wayland egl gles2" S = "${WORKDIR}/git" inherit meson EXTRA_OEMESON += "-Denable_opengl=true" EXTRA_OEMESON += "-Denable_wayland=true" do_install_append() { install -d ${D}${bindir} install -m 0755 ${S}/build/camera-gl-wl ${D}${bindir} } ``` 5. 在你的 Yocto 工程中构建并部署 camera-gl-wl 软件包。 你可以使用 `bitbake` 命令来构建和部署软件包。 ``` $ bitbake camera-gl-wl ``` 构建完成后,软件包将被部署到你的目标系统中,你可以在目标系统上运行 camera-gl-wl 应用程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

甜航一直在

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值