一种android系统实现动态挂载sdcard或者emmc为系统data分区的方法

一种动态实现挂载系统data分区的实现思路,即开机检测到ext4类的sd卡则挂载sdcard作为android系统的data分区,如果没有检测到sdcard就按照/etc/fstab存放的是系统中的文件系统信息挂载emmc作为系统的data分区,主要涉及到vold fs_mgr 进程的修改;
/system/core/fs_mgr/fs_mgr.c

static int mount_with_alternatives(struct fstab *fstab, int start_idx, int *end_idx, int *attempted_idx, int encryptable)
{
 
    int i, j;     //  modify auto mount sdcard for data partition feature 
    int mount_errno = 0;
    int mounted = 0;
    // modify auto mount sdcard for data partition feature 
    char *token_str[20]; 
    char str[100];    
    //end
  if (!end_idx || !attempted_idx || start_idx >= fstab->num_entries) {
      errno = EINVAL;
      if (end_idx) *end_idx = start_idx;
      if (attempted_idx) *end_idx = start_idx;
      return -1;
    }

    /* Hunt down an fstab entry for the same mount point that might succeed */
    for (i = start_idx;
         /* We required that fstab entries for the same mountpoint be consecutive */
         i < fstab->num_entries && !strcmp(fstab->recs[start_idx].mount_point, fstab->recs[i].mount_point);
         i++) {
            /*
             * Don't try to mount/encrypt the same mount point again.
             * Deal with alternate entries for the same point which are required to be all following
             * each other.
             */
            if (mounted) {
                ERROR("%s(): skipping fstab dup mountpoint=%s rec[%d].fs_type=%s already mounted as %s.\n", __func__,
                     fstab->recs[i].mount_point, i, fstab->recs[i].fs_type, fstab->recs[*attempted_idx].fs_type);
                continue;
            }
        //modify auto mount sdcard for data partition feathure start
        memset(str, 0, sizeof(str)/sizeof(char));
        strncpy(str, fstab->recs[i].blk_device, strlen(fstab->recs[i].blk_device));
        token_str[0] = strtok(str, "/");
        for (j = 1; j < 20; j++) {
            token_str[j] = strtok(NULL, "/");
            if (!token_str[j]) 
                break;
        }
        if(!strncmp(token_str[j-1], "userdata", strlen(token_str[j-1]))) {
            if (!mount("/dev/block/mmcblk1p1", fstab->recs[i].mount_point, fstab->recs[i].fs_type, 
                    MS_NOATIME | MS_NOEXEC | MS_NOSUID, "errors=remount-ro,nomblk_io_submit") 
                && !umount(fstab->recs[i].mount_point)) {
            INFO("%s(): /dev/block/mmcblk1p1 try to mount/unmount(%s) succeeded\n", __func__, fstab->recs[i].mount_point);
            free(fstab->recs[i].blk_device);
            fstab->recs[i].blk_device = strdup("/dev/block/mmcblk1p1");
        }
        } 
        INFO("%s(): blk_device: %s,  mountpoint=%s rec[%d].fs_type=%s mounted as %s.\n", __func__,
                     fstab->recs[i].blk_device, fstab->recs[i].mount_point, i, fstab->recs[i].fs_type, fstab->recs[*attempted_idx].fs_type);
    //end
    #ifdef MTK_FSTAB_FLAGS
            if(fstab->recs[i].fs_mgr_flags & MF_RESIZE) {
                check_fs(fstab->recs[i].blk_device, fstab->recs[i].fs_type,
                         fstab->recs[i].mount_point);
                resize_fs(fstab->recs[i].blk_device, fstab->recs[i].key_loc);
            }
  #endif

修改vendor/mediatek/proprietary/hardware/fstab/platform/fstab.in文件设置data分区默认不加密

# /dev/block/platform/mtk-msdc.0/11230000.MSDC0/by-name/userdata   /data        ext4   noatime,nosuid,nodev,noauto_da_alloc,discard               wait,check,resize

/dev/block/platform/mtk-msdc.0/11230000.MSDC0/by-name/userdata   /data        ext4   noatime,nosuid,nodev,noauto_da_alloc,discard               wait,check,resize,FLAG_FDE_AUTO=/dev/block/platform/mtk-msdc.0/11230000.MSDC0/by-name/metadata,FLAG_FDE_TYPE

注意Google 要求中高端手机都必须强制性启用加密, 只有超低端的机器可以不加密, 在CTS 中有强制性审查项,需要过GMS认证的版本修改此项会影响过认证;

SD卡处理
由于sd卡和emmc读写速度存在较大差异,挂载sd卡作为data分区,android系统起来后系统性能会有一定影响,为了减少这种影响,sd卡必须选用高性能高速卡
以linux系统下gparted工具为例
生成分区表为gpt格式的.
新加一个主分区为ext4格式.

包含ext4分区的sd卡做好后,可以挂在到linux系统下的目录上,对sd卡作一些操作,例如:拷贝apk,等文件到sd卡ext4分区,可以实现预置应用,文件等功能

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。