init.rc启动shell脚本
0. 前言1. 编写脚本 test.sh2. 修改 .mk 配置文件,将创建的 test.sh 编译到系统分区3. 配置 SELinux 权限3.1 创建 test.te3.2 配置 service.te3.3 配置 file_context 4. 配置 init.rc5. 重新编译并刷入6. 注意事项0. 前言
最近在解决客户的一个问题的时候,帮忙调试了一个开机脚本,其中涉及了部分SELinux的权限的配置,因此记录一下,该案例基于 amlogic S905L3A 芯片开发,在Android P上进行的测试,在其他设备上大同小异,请自行查找或替换为对应的路径。
注:Android P上为了区分系统和厂商定制化内容,脚本应编译至vendor/bin下,而不是system/bin下。
1. 编写脚本 test.sh
举个例子:
#!/system/bin/sh# 该脚本只是演示,请根据自己需求编写脚本if [ -f /data/system/test.xml ]; thenecho "test already set"elsecp /system/test.xml /data/system/test.xmlchmod 0600 /data/system/test.xmlchown system:system /data/system/test.xmlfi
脚本具体放置位置可以自行安排
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.te
、service.te
、file_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. 注意事项
案例中的路径可能和你工程路径不一致,请自行查找或替换为对应的路径。
如果觉得《Android init.rc启动shell脚本》对你有帮助,请点赞、收藏,并留下你的观点哦!