yocto系列讲解 (技巧篇) 67 - 在etc/passwd和etc/group文件中添加用户和组等信息

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

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

问题简介

我发现有些程序喜欢设置成自己的用户和组,比如gps的程序,就喜欢弄成gps的用户和组,audio的也类似

 2109 gps        0:00 loc-server
 2111 gps        0:00 audio-server
  • 1
  • 2

程序设置的用户和组,有对应的用户和组id,密码,以及home目录使用那个shell等都在这两个文件中

# cat /etc/passwd
audio:x:1005:29::/home/audio:/bin/sh 
gps:x:1021:1021::/home/gps:/bin/sh
  • 1
  • 2
  • 3
# cat /etc/group
audio:x:29:
gps:x:1021:
  • 1
  • 2
  • 3

修改passwd和group文件

/etc/passwd和/etc/group两个文件是我们编译目标镜像中就自动生成的,我们一起也很少接触或者修改他们,我们要如何修改呢?
首先,我们参考下yocto官方提供的示例:

#meta-skeleton/recipes-skeleton/useradd/useradd-example.bb

SUMMARY = "Example recipe for using inherit useradd"
DESCRIPTION = "This recipe serves as an example for using features from useradd.bbclass"
SECTION = "examples"
PR = "r1"
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 pacakge:
# As an example, we use the -P option to set clear text password for user3
USERADD_PARAM_${PN}-user3 = "-u 1202 -d /home/user3 -r -s /bin/bash -P 'user3' 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"
  • 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
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79

如果想知道这个实际的效果,可以将meta-skeleton/recipes-skeleton/useradd复制一份到自己的meta layer中然后编译运行,可以在/etc/passwd和/etc/group两个文件看到新增内容。

添加相同的用户和组ID方法

这里我列举一个,我们需要添加一个用户abc和组abc,ID都为1000,这么添加可以参考这个:

#某个bb文件中添加这些
inherit useradd
USERADD_PACKAGES = "${PN}"
USERADD_PARAM_${PN} = "-u 1000 -U abc"
INHIBIT_PACKAGE_DEBUG_SPLIT = "1"
  • 1
  • 2
  • 3
  • 4
  • 5

编译之后就可以看到这些内容了:

cat tmp/work/xxxx-poky-linux-gnueabi/core-image-sato/1.0-r0/rootfs/etc/passwd 
...
abc:x:1000:1000::/home/abc:/bin/sh
...
vim tmp/work/xxxx-poky-linux-gnueabi/core-image-sato/1.0-r0/rootfs/etc/group
abc:x:1000:
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

参考示例

poky/meta-mylayer/conf/distro/mypermissions.conf
USERADD_PACKAGES ?= "${PN}"

USERADD_PARAM_${PN} ?=  " \
            -u 5002 -U leprop; \
            -u 1000 -U system; \
            -u 1001 -U radio; \
            -G leprop -u 1002 -U bluetooth; \
            -u 1003 -U graphics; \
            -u 1004 -g input input; \
            -u 1005 -g audio audio; \
            -G inet,system,leprop -u 1006 -U camera; \
            -u 1010 -U wifi; \
            -u 1013 -U media; \
            -u 1036 -U logd; \
            -u 2000 -U adb; \ 
            -G diag,inet,system,leprop -u 3011 -U sensors; \
            -u 4000 -U apps; \
            -u 9999 -U nobody; \
            -G system,leprop -u 5000 -U qmmfsvr; \
            -G system,inet,leprop -u 5001 -U qmmfwebsvr; \
            -u 4021 -U locclient; \
            -G system,locclient -u 1021 -U gps; \ 
            "      

GROUPADD_PARAM_${PN} ?= " \ 
            -g 3005 net_admin; \
            -g 3009 readproc; \
            -g 3010 wakelock; \
            -g 3012 powermgr; \
            -g 3013 qwes; \
  • 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
  • 29
  • 30
  • 31

然后这个配置文件在这里被引用:

poky/meta-mylayer/classes/mypermissions.bbclass
require conf/distro/mypermissions.conf
...
  • 1
  • 2
  • 3

然后其他的文件中继承这个类即可:

poky/meta-mylayer/conf/distro/include/xxxx.inc
...
USERADDEXTENSION = "mypermissions"
  • 1
  • 2
  • 3