Android Vold

总体框架

plain 复制代码
int main(int argc, char** argv) {
    atrace_set_tracing_enabled(false);
    setenv("ANDROID_LOG_TAGS", "*:d", 1);  // Do not submit with verbose logs enabled
    android::base::InitLogging(argv, &VoldLogger);

    LOG(INFO) << "Vold 3.0 (the awakening) firing up";

    ATRACE_BEGIN("main");

    LOG(DEBUG) << "Detected support for:"
               << (android::vold::IsFilesystemSupported("ext4") ? " ext4" : "")
               << (android::vold::IsFilesystemSupported("f2fs") ? " f2fs" : "")
               << (android::vold::IsFilesystemSupported("vfat") ? " vfat" : "");

    VolumeManager* vm;
    NetlinkManager* nm;

    parse_args(argc, argv);

    sehandle = selinux_android_file_context_handle();
    if (!sehandle) {
        LOG(ERROR) << "Failed to get SELinux file contexts handle";
        exit(1);
    }
    selinux_android_set_sehandle(sehandle);

    mkdir("/dev/block/vold", 0755);

    /* For when cryptfs checks and mounts an encrypted filesystem */
    klog_set_level(6);

    /* Create our singleton managers */
    if (!(vm = VolumeManager::Instance())) {
        LOG(ERROR) << "Unable to create VolumeManager";
        exit(1);
    }

    if (!(nm = NetlinkManager::Instance())) {
        LOG(ERROR) << "Unable to create NetlinkManager";
        exit(1);
    }

    if (android::base::GetBoolProperty("vold.debug", false)) {
        vm->setDebug(true);
    }

    if (vm->start()) {
        PLOG(ERROR) << "Unable to start VolumeManager";
        exit(1);
    }

    VoldConfigs configs = {};
    if (process_config(vm, &configs)) {
        PLOG(ERROR) << "Error reading configuration... continuing anyways";
    }

    android::hardware::configureRpcThreadpool(1, false /* callerWillJoin */);

    ATRACE_BEGIN("VoldNativeService::start");
    if (android::vold::VoldNativeService::start() != android::OK) {
        LOG(ERROR) << "Unable to start VoldNativeService";
        exit(1);
    }
    ATRACE_END();

    LOG(DEBUG) << "VoldNativeService::start() completed OK";

    ATRACE_BEGIN("NetlinkManager::start");
    if (nm->start()) {
        PLOG(ERROR) << "Unable to start NetlinkManager";
        exit(1);
    }
    ATRACE_END();

    // This call should go after listeners are started to avoid
    // a deadlock between vold and init (see b/34278978 for details)
    android::base::SetProperty("vold.has_adoptable", configs.has_adoptable ? "1" : "0");
    android::base::SetProperty("vold.has_quota", configs.has_quota ? "1" : "0");
    android::base::SetProperty("vold.has_reserved", configs.has_reserved ? "1" : "0");
    android::base::SetProperty("vold.has_compress", configs.has_compress ? "1" : "0");

    // Do coldboot here so it won't block booting,
    // also the cold boot is needed in case we have flash drive
    // connected before Vold launched
    coldboot("/sys/block");

    ATRACE_END();

    android::IPCThreadState::self()->joinThreadPool();
    LOG(INFO) << "vold shutting down";

    exit(0);
}

流程分析

存储卡加载流程

总结

从时序图中我们得知挂载相关联的类有VolumeManager、Disk、VolumeBase、StorageManagerService。挂载步骤如下:

1.到VolumeManager中接收到驱动层中的挂载信息后,会创建Disk对象并且调用其Create方法。

2.Disk中的Create方法会先通过binder方式调用onDiskCreated(getId(), mFlags)通知framework层的StorageManagerService,告诉其创建了Disk;r然后调用其成员方法 readMetadata()获取存储卡大小和标签告知framework层的StorageManagerService;再调用其成员方法readPartitions()解析存储卡类型创建Volume对象,通知framework层的StorageManagerService解析完成。

3.VolumeBase是基类,有三种卷,一个是PublicVolume是上面第二步骤解析的存储卡类型为mbr,一种事PrivateVolume解析的是gpt类型,还有一种是内部存储卡EmulatedVolume;中三种都是继承自BaseVolume。我们以PublicVolume为例,创建完volume对象后会调用其create方法,然后通知通知framework层的StorageManagerService volume创建完成。

4.StorageManagerService收到创建volume完成的消息后,会通过binder的方式回调mount的方法,这里首先会回调Binder服务端VoldNativeService中的mount方法,然后再调用VolumeBase中的mount方法。

5.VolumeBase收到mount方法后先发送准备挂载kChecking信息给StorageManagerService、执行挂载方法doMount,挂载成功后发送kMounted消息给StorageManagerService。

参考

https://blog.csdn.net/u014795729/article/details/132355702

相关推荐
API小爬虫39 分钟前
利用 PHP 爬虫获取京东商品详情 API 返回值说明及代码示例
android·爬虫·php
_一条咸鱼_8 小时前
深入剖析 Android Hilt 框架的依赖生命周期管理模块(六)
android·kotlin·android jetpack
_一条咸鱼_8 小时前
深入剖析 Android Hilt 的模块配置与初始化模块(五)
android·kotlin·android jetpack
斗锋在干嘛10 小时前
Android里面开子线程的方法
android
yangshuo128110 小时前
WSA(Windows 安卓子系统)过检测教程
android
jiet_h11 小时前
Kotlin 中 集合 Collection 的扩展方法完全指南
android·开发语言·kotlin
吃饭了呀呀呀12 小时前
🐳 《Android》 安卓开发教程 - 自定义 Toast
android·后端
斗锋在干嘛13 小时前
在Android中问AMS 和 PMS的区别和作用
android
雯0609~15 小时前
PHP:将关联数组转换为索引数组的完整示例
android·oracle·php
Wgllss15 小时前
Android提升开发测试效率,程序员应该多干了些什么?
android·架构·android jetpack