標籤:android vold volumemanager vold.fstab process_config
在Android—— 4.2 Vold掛載管理_主體構建main (一)中分析了大體的架構,這裡分析一下核心VolumeManager,再次補上結構架構圖如下:
可以看到VolumeManager就是整個Android 磁碟掛載Vold機制的核心調度,上下串連的中轉站!
我從Vold main代碼的順序結構來一次分析,上一篇Android—— 4.2 Vold掛載管理_CommandListener (二) 中分析了與framework層互動的CommandListener的功能作用。
這裡分析VolumeManager在整個體系中所扮演的角色和職能!
撰寫不易,轉載請註明出處:http://blog.csdn.net/jscese/article/details/38581521
一:VolumeManager 初始構建:
依舊在/system/vold/main.cpp中:
/* Create our singleton managers */ if (!(vm = VolumeManager::Instance())) { SLOGE("Unable to create VolumeManager"); exit(1); }; ... if (vm->start()) { SLOGE("Unable to start VolumeManager (%s)", strerror(errno)); exit(1); } if (process_config(vm)) { SLOGE("Error reading configuration (%s)... continuing anyways", strerror(errno)); }
先看同級目錄的VolumeManager.cpp 的建構函式:
VolumeManager::VolumeManager() { mDebug = false; mVolumes = new VolumeCollection();//卷的一個容器 mActiveContainers = new AsecIdCollection();//活動容器,mount/unmount asec obb時的記錄 mBroadcaster = NULL;//指向SocketListener,用於發送掛載事件 mUmsSharingCount = 0; mSavedDirtyRatio = -1; // set dirty ratio to 0 when UMS is active mUmsDirtyRatio = 0; mVolManagerDisabled = 0;}
其中最為重要的就是mVolumes這個容器:
typedef android::List<Volume *> VolumeCollection;
是/system/vold/Volume.cpp的執行個體集合!
vm->start()沒有任何操作!
二:process_config
這個函數用於解析指定的設定檔,根據內容構造DirectVolume以及父類然後儲存進VolumeManager的容器中,供VolumeManager用於掛載事件的管理。
static int process_config(VolumeManager *vm) { FILE *fp; int n = 0; char line[255]; if (!(fp = fopen("/etc/vold.fstab", "r"))) { //開啟設定檔/etc/vold.fstab return -1; } while(fgets(line, sizeof(line), fp)) { const char *delim = " \t";//\t之前有個空格,以空格或者水平定位字元(tab)來用來分割讀出來的line if (!(type = strtok_r(line, delim, &save_ptr))) {//分割讀到的line string,一次擷取規定順尋的參數,使用的是安全執行緒函數strtok_r SLOGE("Error parsing type"); goto out_syntax; } if (!(label = strtok_r(NULL, delim, &save_ptr))) { SLOGE("Error parsing label"); goto out_syntax; } if (!(mount_point = strtok_r(NULL, delim, &save_ptr))) { SLOGE("Error parsing mount point"); goto out_syntax; } if (!strcmp(type, "dev_mount")) { DirectVolume *dv = NULL; char *part; if (!(part = strtok_r(NULL, delim, &save_ptr))) { SLOGE("Error parsing partition"); goto out_syntax; } if (strcmp(part, "auto") && atoi(part) == 0) { SLOGE("Partition must either be 'auto' or 1 based index instead of '%s'", part); goto out_syntax; } if (!strcmp(part, "auto")) { //根據上面解析到的值構建 DirectVolume執行個體 dv = new DirectVolume(vm, label, mount_point, -1); } else { dv = new DirectVolume(vm, label, mount_point, atoi(part)); } while ((sysfs_path = strtok_r(NULL, delim, &save_ptr)))... vm->addVolume(dv);//添加進VolumeManager的容器... }}
可以看VolumeManager中的addVolume函數:
int VolumeManager::addVolume(Volume *v) { mVolumes->push_back(v); return 0;}
繼承關係 DirectVolume ——> Volume,這裡強制轉換了。
三:vold.fstab
這個設定檔的編譯在Android—— 4.2 Vold掛載管理_主體構建main (一)中有詳細分析,先看看這個設定檔的注釋:
######################### Regular device mount#### Format: dev_mount <label> <mount_point> <part> <sysfs_path1...> ## label - Label for the volume## mount_point - Where the volume will be mounted## part - Partition # (1 based), or 'auto' for first usable partition.## <sysfs_path> - List of sysfs paths to source devices######################
每一行對應上面的五個參數,各個參數之間以空格或者tab分開,google有個例子:
dev_mount sdcard /mnt/sdcard auto /devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1
這是掛載sdcard的配置,前面的幾個分別對應上面注釋中的,最後的sysfs_path 就是要掛載的源裝置路徑,這個根據各自的平台修改(我的是/dev/block/platform/*),第一個不行就會用第二個
VolumeManager的功能實現函數(mountVolume,handleBlockEvent等),都是受到其它功能模組調用,如上面的主體架構圖,
這裡不做解析,在後面的DirectVolume/Volume掛載,以及NetlinkEvent事件處理中會分析。