Android14 init.rc各个阶段的主要操作
1. on early-init
类别 | 关键操作 | 目标 |
---|---|---|
安全加固 | 禁用 sysrq、modprobe,设置 SELinux | 降低攻击面 |
内存/Cgroups | 创建 app/system cgroup,设置 swappiness | 为 LMKD 和进程调度做准备 |
文件系统 | 创建 linkerconfig、/dev/net、fscklogs | 搭建基本运行环境 |
资源限制 | 设置 nice、nofile、memlock | 防止资源滥用 |
APEX 初始化 | 启动 apexd-bootstrap,更新链接器配置 | 加载系统模块(如 SSL) |
调试支持 | 挂载 tracefs,创建 selftest 目录 | 支持性能分析和自检 |
守护进程 | start ueventd | 处理设备热插拔事件 |
on early-init...//设置adb_keys的SELinux,防止adb被滥用restorecon /adb_keys //确保OTA后处理脚本的SElinux权限restorecon /postinstall...//设置进程优先级从19到-20setrlimit nice 40 40 start ueventd//初始化APEXexec_start apexd-bootstrap mount tracefs tracefs /sys/kernel/tracing gid=3012# create sys dirctorymkdir /dev/sys 0755 system system...
2. on init
on init阶段主要完成三件事:建调度组cgroups,初始化Binder IPC机制和启动基石服务。
2.1 建调度组cgroups
建调度组cgroups,为任务优先级调度提供底层支持。
- 创建CPU调度性能层级stune(scheduler tuning nodes):包含foreground和foreground/tasks, background和background/tasks, top-app和top-app/tasks, rt和rt/tasks和rt/cgroup.procs, audio-app和audio-app/tasks, tasks, cgroup.procs。
mkdir /dev/stune/foreground chown system system /dev/stune/foreground chmod 0664 /dev/stune/foreground/tasks
- CPU利用率层接cpuctl(cpu utilization clamping):包括foreground, background, top-app, rt, system, system-background, dex2oa, tasks,cgroup.procs等
mkdir /dev/cpuctl/foreground mkdir /dev/cpuctl/system-backgroundchown system system /dev/cpuctl/foreground chown system system /dev/cpuctl/system-backgroundchmod 0664 /dev/cpuctl/foreground/tasks chmod 0664 /dev/cpuctl/system-background/tasks
- 磁盘I/O调度层级blkio:blackground,tasks, background/tasks,cgroup.procs,background/cgroup.procs。
mkdir /dev/blkio/background chown system system /dev/blkio/background chmod 0664 /dev/blkio/background/tasks write /dev/blkio/background/blkio.weight 200
- CPU核绑定cpuset:foreground,blackground,system-background,restricted,top-app,camera-daemon,audio-app
mkdir /dev/cpuset/foreground copy /dev/cpuset/cpus /dev/cpuset/foreground/cpus copy /dev/cpuset/mems /dev/cpuset/foreground/memschown system system /dev/cpuset chown system system /dev/cpuset/foregroundchmod 0775 /dev/cpuset/system-background chmod 0664 /dev/cpuset/foreground/tasks
2.2 初始化Binder IPC机制
# Mount binderfs
mkdir /dev/binderfs
mount binder binder /dev/binderfs stats=global
chmod 0755 /dev/binderfssymlink /dev/binderfs/binder /dev/binder
symlink /dev/binderfs/hwbinder /dev/hwbinder
symlink /dev/binderfs/vndbinder /dev/vndbinderchmod 0666 /dev/binderfs/hwbinder
chmod 0666 /dev/binderfs/binder
chmod 0666 /dev/binderfs/vndbinder
2.3 启动基石服务
启动的服务包括:logd, lmkd, servicemanager, hwservicemanager, vndservicemangaer。
start logd # 日志守护进程,必须最早启动
start lmkd # 低内存杀手,监控内存压力
start servicemanager # Binder 服务注册中心
start hwservicemanager # HAL 服务注册中心
start vndservicemanager # Vendor 服务注册中心
类别 | 关键操作 | 目标 |
---|---|---|
资源调度 | 创建 为nnapi-hal, camera-daemon创建stune和cpuctl group | 为 AMS、LMKD 提供调度基础 |
IPC 通信 | 挂载 binderfs ,创建 /dev/binder 符号链接 | 支持 Binder 通信 |
文件系统 | 挂载 configfs 、bpf 、pstore ,创建 /mnt/* 目录 | 搭建存储与调试环境 |
安全加固 | 设置 ASLR、mmap_min_addr、禁用 ICMP 重定向 | 提升系统安全性 |
内核调优 | 调整调度延迟、唤醒粒度、panic 行为 | 优化性能与稳定性 |
服务启动 | 启动 logd 、lmkd 、servicemanager 等 | 为后续 Zygote 启动铺路 |
2.4 其他操作
on init...# prepare directories for pass_through processmkdir /mnt/pass_through 0700 root rootmkdir /mnt/pass_through/0/emulated/0 0710 root media_rwmkdir /mnt/runtime 0700 root rootmkdir /mnt/runtime/full 0755 root root#挂载虚拟文件系统mount configfs none /configmount bpf bpf /sys/fs/bpf