Android 下做開機啓動shell腳本的大致流程如下:
目錄
寫shell腳本
比如新建一個init.test.sh,內容如下:
#!/system/bin/sh
###注意這裏的開頭一定要寫正確,android一般是/system/bin/sh 或/system/xbin/sh, linux下的是/bin/sh。寫錯了是執行不了的,不要以爲有#沒有影響。###
##這裏建議測試時直接設置一個屬性,不建議新建文件啥的,避免引起權限等的干擾##
setprop test.prop 111
建議寫完後,先push到手機裏面手動執行下,看是否正常運行。
爲腳本寫te文件
新建test_service.te 內容如下:
type test_service, coredomain;
type test_service_exec, exec_type, vendor_file_type, file_type;
#permissive test_service;
init_daemon_domain(test_service);
#allow shell test_service_exec:file { read open getattr execute };
我所做的是MTK平臺,添加在了device/mediatek/sepolicy/basic/non_plat
接着在device/mediatek/sepolicy/basic/non_plat/file_contexts 下添加如下代碼:
/(system\/vendor|vendor)/bin/ init.test.sh u:object_r:test_service_exec:s0
注意:這一步即使你關閉了selinux還是要加的,不然開機啓動不了。倘若你手機能接串口(不是adb),那調試起來將會相當爽。
在init.rc中啓動腳本
service test_service /system/bin/ init.test.sh
class main
user root
group root
oneshot
seclabel u:object_r:test_service_exec:s0
這裏一般芯片廠商一般都有專門的一個init.XXX.rc 或XXX.sh 用於客戶啓動自定義的腳本,不建議直接加在init.rc裏面。
添加Selinux權限
這步根據你的實際報錯來進行添加,可另行百度。
Ps:本文是對 https://blog.csdn.net/u012975242/article/details/83274693 理解與實際使用的優化