AB升级(3): update_engine的初始化流程

update_engine是AB升级中的核心组件,负责升级的整个流程的控制。它的代码源自Chrome,并针对Android做了对应的适配。

本文介绍AB升级的初始化流程,即使不执行OTA升级的操作,update_engine服务也会启动,做好初始化工作,静候系统更新的发生。

服务的启动

开机后通过init启动,属于late_start

system/update_engine/update_engine.rc

service update_engine /system/bin/update_engine --logtostderr --logtofile --foreground
    class late_start
    user root
    group root system wakelock inet cache media_rw
    writepid /dev/cpuset/system-background/tasks /dev/blkio/background/tasks
    disabled

on property:ro.boot.slot_suffix=*
    enable update_engine

update_engine初始化流程

开机UpdateEngine流程.png

在开机以后会对BootControlAndroid, HardwareAndroid,BinderUpdateEngineAndroidService等相关重要类进行初始化。初始化昨晚之后执行CleanupPreviousUpdateAction清理状态,CleanupPreviousUpdateAction需要等Bootcomplete之后才会真正执行。

CleanupPreviousUpdateAction会在Virtual A/B的设备上清除上次尝试更新的snapshots。对于非Virtual AB的设备,CleanupPreviousUpdateAction将直接返回。

如果是virtual AB设备,需要等sys.boot_completed值设为1,也就是boot complete之后,才进行清除快照的情况,如果没有则等待并2s查询一次。

然后会根据snapshot状态进行相应处理。

update_engine初始化中重要的类说明

类/文件

说明

路径

DaemonAndroid

后台线程,发动重要对象的初始化

system/update_engine/aosp/daemon_android.cc

DaemonStateAndroid

实例化BootControlAndroid,HardwareAndroid等重要的类

system/update_engine/aosp/daemon_state_android.cc

BootControlAndroid

通过hidl接口操作misc分区

system/update_engine/aosp/boot_control_android.cc

HardwareAndroid

Android一些状态工具类

system/update_engine/aosp/hardware_android.cc

UpdateAttempterAndroid

管理BootControlAndroid,HardwareAndroid等实体类

system/update_engine/aosp/update_attempter_android.cc

ActionProcessor

包含将要执行的Action队列,管理这些Action的执行

system/update_engine/common/action_processor.cc

BinderUpdateEngineAndroidService

UpdateAttempterAndroid再客户端的代理。接收UpdateEngine的调用,然后调用UpdateAttempterAndroid

system/update_engine/aosp/binder_service_android.cc

BinderUpdateEngineAndroidStableService

BinderUpdateEngineAndroidService的稳定版接口

system/update_engine/aosp/binder_service_stable_android.cc

update_engine启动的log

启动的log会纪录到data/misc/update_engine_log中,通过log对照着代码去看流程会更容易理解。

[0524/092633.143122] [INFO:main.cc(55)] A/B Update Engine starting
[0524/092634.070803] Binder ioctl to enable oneway spam detection failed: Invalid argument
[0524/092634.092755] Binder ioctl to enable oneway spam detection failed: Invalid argument
[0524/092634.109536] [INFO:boot_control_android.cc(68)] Loaded boot control hidl hal.
[0524/092634.128899] [INFO:daemon_state_android.cc(44)] Booted in dev mode.
[0524/092634.195315] [INFO:update_attempter_android.cc(1070)] Scheduling CleanupPreviousUpdateAction.
[0524/092634.203812] [INFO:action_processor.cc(51)] ActionProcessor: starting CleanupPreviousUpdateAction
[0524/092634.233506] [INFO:cleanup_previous_update_action.cc(149)] Starting/resuming CleanupPreviousUpdateAction
[0524/092634.270598] [INFO:cleanup_previous_update_action.cc(124)] CleanupPreviousUpdateAction scheduled task ID 1 for WaitBootCompleted
[0524/092636.282004] [INFO:cleanup_previous_update_action.cc(112)] Executing task 1
[0524/092636.292106] [INFO:cleanup_previous_update_action.cc(124)] CleanupPreviousUpdateAction scheduled task ID 3 for WaitBootCompleted
[0524/092638.295153] [INFO:cleanup_previous_update_action.cc(112)] Executing task 3
[0524/092638.311039] [INFO:cleanup_previous_update_action.cc(124)] CleanupPreviousUpdateAction scheduled task ID 4 for WaitBootCompleted
[0524/092640.311858] [INFO:cleanup_previous_update_action.cc(112)] Executing task 4
[0524/092640.332783] [INFO:cleanup_previous_update_action.cc(124)] CleanupPreviousUpdateAction scheduled task ID 5 for WaitBootCompleted
... ...
[0524/092712.696368] [INFO:cleanup_previous_update_action.cc(112)] Executing task 20
[0524/092712.773227] [INFO:cleanup_previous_update_action.cc(189)] Boot completed, waiting on markBootSuccessful()
[0524/092712.952938] [INFO:snapshot.cpp(3344)] EnsureMetadataMounted does nothing in Android mode.
[0524/092713.012552] [INFO:snapshot_stats.cpp(37)] Read merge statistics file failed: No such file or directory
[0524/092713.046272] [INFO:cleanup_previous_update_action.cc(261)] Waiting for any previous merge request to complete. This can take up to several minutes.
[0524/092713.060992] [ERROR:snapshot.cpp(2518)] Read state file failed: No such file or directory
[0524/092713.071631] [ERROR:snapshot.cpp(2518)] Read state file failed: No such file or directory
[0524/092713.083641] [INFO:snapshot.cpp(938)] CheckMergeState for snapshots returned: 0
[0524/092713.092585] [INFO:snapshot.cpp(910)] ProcessUpdateState handling state: 0
[0524/092713.100925] [INFO:cleanup_previous_update_action.cc(287)] Can't find any snapshot to merge.
[0524/092713.116482] [ERROR:snapshot.cpp(2518)] Read state file failed: No such file or directory
[0524/092713.139558] [INFO:cleanup_previous_update_action.cc(130)] Stopping/suspending/completing CleanupPreviousUpdateAction
[0524/092713.157895] [INFO:cleanup_previous_update_action.cc(469)] Not reporting merge stats because state is None
[0524/092713.165272] [INFO:cleanup_previous_update_action.cc(130)] Stopping/suspending/completing CleanupPreviousUpdateAction
[0524/092713.183934] [INFO:action_processor.cc(116)] ActionProcessor: finished last action CleanupPreviousUpdateAction with code ErrorCode::kSuccess
[0524/092713.193247] [INFO:update_attempter_android.cc(546)] Processing Done.
[0524/092713.205979] [INFO:update_attempter_android.cc(694)] Terminating cleanup previous update.
个人成就
文章
2
阅读量
17701
获赞
2001
作者排名
1506