本文是基于android4.2.2的代码进行的调查分析,其他版本的android代码可能类似,但不一定会相同。
模拟器中/system/etc/目录下本应该有一个文件叫vold.fstab,但是某些情况下这个文件会缺失
那么这个文件是如何生成到system.img中的呢,下面我们来详细解释一下。
首先,在system/core/rootdir/etc/目录下有一个文件叫做vold.fstab,这个文件是一个教我们写vold.fstab的模板。模板内容如下:
## Vold 2.0 Generic fstab
## - San Mehat (san@android.com)
##
#######################
## 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
######################
## Example of a standard sdcard mount for the emulator / Dream
# Mounts the first usable partition of the specified device
dev_mount sdcard /mnt/sdcard auto /devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1
## Example of a dual card setup
# dev_mount left_sdcard /sdcard1 auto /devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1
# dev_mount right_sdcard /sdcard2 auto /devices/platform/goldfish_mmc.1 /devices/platform/msm_sdcc.3/mmc_host/mmc1
## Example of specifying a specific partition for mounts
# dev_mount sdcard /sdcard 2 /devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1
关键的两句如下
Format: dev_mount <label> <mount_point> <part> <sysfs_path1...>
dev_mount sdcard /mnt/sdcard auto /devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1
dev_mount:挂载命令
<label>:sdcard 是挂载标签
<mount_point>: 挂载点,这里是/mnt/sdcard
<part> :子分区,这里是auto,为自定义选项可以为任何,但必须在system/vold/main.cpp里面自己判断,比如这里的意思为自动挂载
<sysfs_path1...>:设备在sysfs文件系统下的路径(可多个),这里是
/devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1
两个目录为设备路径,第一个如果被占用会选择第二个
main.cpp中代码如下:
static int process_config(VolumeManager *vm) {
......
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")) {
dv = new DirectVolume(vm, label, mount_point, -1);
} else {
dv = new DirectVolume(vm, label, mount_point, atoi(part));
}
......
}
其次,系统会拷贝这个vold.fstab到out路径下,以备system.img生成时打包进去
在build/target/product/sdk.mk文件中有如下描述
PRODUCT_COPY_FILES := \
device/generic/goldfish/data/etc/apns-conf.xml:system/etc/apns-conf.xml \
system/core/rootdir/etc/vold.fstab:system/etc/vold.fstab \
......
这些文件都是从android源码中直接拷贝到out/target/product/generic/system/等目录中,打包system.img时就会打包到android的文件系统中。
在system/vold/main.cpp里,process_config函数中会调用如下代码
static int process_config(VolumeManager *vm) {
......
if (!(fp = fopen("/etc/vold.fstab", "r"))) {
return -1;
}
......
}
而system/core/rootdir/init.rc文件中有如下代码,将android文件系统中的/system/etc做成符号链接,链接到/etc目录
# Backward compatibility
# symlink /system/etc /etc
# symlink /sys/kernel/debug /d
详细查看system/vold/main.cpp里的process_config函数可以知道,sdcard是读取vold.fstab文件后根据配置挂载到android文件系统中的。
也就做到了自动挂载sdcard