Android init.rc启动shell脚本

0. 前言

  最近在解决客户的一个问题的时候,帮忙调试了一个开机脚本,其中涉及了部分SELinux的权限的配置,因此记录一下,该案例基于 amlogic S905L3A 芯片开发,在Android P上进行的测试,在其他设备上大同小异,请自行查找或替换为对应的路径。

注:Android P上为了区分系统和厂商定制化内容,脚本应编译至vendor/bin下,而不是system/bin下

1. 编写脚本 test.sh

举个例子:

#!/system/bin/sh
# 该脚本只是演示,请根据自己需求编写脚本
if [ -f /data/system/test.xml ]; then
    echo "test already set"
else
    cp /system/test.xml /data/system/test.xml
    chmod 0600 /data/system/test.xml
    chown system:system /data/system/test.xml

fi

  脚本具体放置位置可以自行安排

2. 修改 .mk 配置文件,将创建的 test.sh 编译到系统分区

PRODUCT_COPY_FILES += \
     device/amlogic/$(PRODUCT_DIR)/files/tcp_control.sh:vendor/bin/tcp_control.sh \

  将工程中的device/amlogic/$(PRODUCT_DIR)/files/tcp_control.sh copy至vendor/bin/tcp_control.sh

3. 配置 SELinux 权限

3.1 创建 test.te

  在 service.te 文件所在的目录下创建 test.te

type testshell, domain;
type testshell_exec, exec_type, vendor_file_type, file_type;
init_daemon_domain(testshell)

#配置脚本中需要的权限,可以无
allow testshell vendor_shell_exec:file { execute_no_trans };
allow testshell device:chr_file { ioctl };
#allow testshell system_file:file { execute };
#allow testshell toolbox_exec:file { map };
allow testshell storage_file:dir { search };
allow testshell storage_file:lnk_file { read };
allow testshell mnt_user_file:lnk_file { read };
allow testshell mnt_user_file:dir { search };
allow testshell sdcardfs:dir { search write add_name create };
#allow testshell media_rw_data_file:dir { read open search write };
allow testshell system_data_file:file { getattr };

3.2 配置 service.te

  在 service.te 中增加一行

...
type test_service, app_api_service, ephemeral_app_api_service, system_server_service, service_manager_type;
...

3.3 配置 file_context

  在 file_contexts 中增加一行

#test
/vendor/bin/test.sh                               u:object_r:testshell_exec:s0

  具体到我的工程,SELinux 配置所在的路径为 device/amlogic/common/sepolicy , test.teservice.tefile_context 都在该目录下。

4. 配置 init.rc

  在 init.rc 文件中找到 on boot,在其中增加一行 exec -- /vendor/bin/test.sh,如下

on boot

    ...
    ...
    # execute test.sh 
    exec -- /vendor/bin/test.sh

  具体到我的工程,target 对应的 init.rc 文件为device/amlogic/p291_iptv/init.amlogic.board.rc

5. 重新编译并刷入

  重新编译并刷入,查看脚本中指令生效(如复制文件,设置属性等),从而验证 test.sh 脚本是否被执行,也可以通过 adb shell dmesg 命令查看开机日志检查是否有脚本中的打印。

6. 注意事项

  案例中的路径可能和你工程路径不一致,请自行查找或替换为对应的路径。

  • 0
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

ʚ兔子的先森ɞ

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

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

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

打赏作者

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

抵扣说明:

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

余额充值