一、VolumeManager的启动
Vold使用VolumeManager的过程和NetlinkManager类似,也是三步:
system/vold/VolumeManager.cpp
1、vm= VolumeManager::Instance() //创建VolumeManager
2、vm->setBroadcaster((SocketListener *) cl) //设置事件广播监听
3、vm->start() //启动VolumeManager
VolumeManager采用单例模式创建对象:
VolumeManager *VolumeManager::Instance() {
//单例模式
if (!sInstance)
sInstance = new VolumeManager();
return sInstance;
}
VolumeManager::VolumeManager() {
mDebug = true;
mHandleUevent = true;
mActiveContainers = new AsecIdCollection();
mBroadcaster = NULL;
mUmsSharingCount = 0;
mSavedDirtyRatio = -1;
mUmsDirtyRatio = dirtyRatio();
mSkipUmountForResetCommand = false;
}
下面接着查看VolumeManager::start()所做的任务:
int VolumeManager::start() {
// Always start from a clean slate by unmounting everything in
// directories that we own, in case we crashed.
unmountAll();//在处理外部设备事件之前,先重置所有状态
// Assume that we always have an emulated volume on internal
// storage; the framework will decide if it should be mounted.
CHECK(mInternalEmulated == nullptr);
mInternalEmulated = std::shared_ptr<android::vold::VolumeBase>(
new android::vold::EmulatedVolume("/data/media"));
//预先设定/data/media,由framework决定是否mount;
//EmulatedVolume和VolumeBase之间是继承关系,代表不同类型的Volume
mInternalEmulated->create();
return 0;
}
二、VolumeManager与NetlinkHandler交互
上面简单介绍了VolumeManager的启动,下面分析与NetlinkHandler交互过程,由上文 Android vold进程一其中操作的接口为VolumeManager的handleBlockEvent方法。
void VolumeManager::handleBlockEvent(NetlinkEvent *evt) {
std::lock_guard<std::mutex> lock(mLock);
//设备路径
std::string eventPath(evt->findParam("DEVPATH")?evt->findParam("DEVPATH"):"");
//设备类型
std::string devType(evt->findParam("DEVTYPE")?evt->findParam("DEVTYPE"):"");
if (devType != "disk") return;
//主次设备号,两者可以描述一个具体设备
int major = atoi(evt->findParam("MAJOR"));
int minor = atoi(evt->findParam("MINOR"