1. 程式人生 > 其它 >Android Init 檔案系統掛載

Android Init 檔案系統掛載

首先是init載入rc檔案: /vendor/etc/init/hw/init.rockchip.rc
在其中引入import /vendor/etc/init/hw/init.mount_all.rc

// cat /vendor/etc/init/hw/init.mount_all.rc
# do mount_all early can imporve boot time when FBE
# is enabled
on early-fs
    start vold

on fs
    mount_all /vendor/etc/fstab.${ro.hardware} --early

on late-fs
    # Start services for bootanim
    start servicemanager
    start hwcomposer-2-1
    start gralloc-2-0
    start surfaceflinger
    start bootanim

    exec_start wait_for_keymaster
    # Mount RW partitions which need run fsck
    mount_all /vendor/etc/fstab.${ro.hardware} --late

我的原始碼是Android 12

其中的mount_all對應函式system/core/init/builtins.cpp中的do_mount_all函式
並且指定了fstab檔案/vendor/etc/fstab.${ro.hardware}

這裡看一部分關鍵程式碼:

//
static Result<void> do_mount_all(const BuiltinArguments& args) {
    ... //省略
    Fstab fstab;
    if (mount_all->fstab_path.empty()) {
        if (!ReadDefaultFstab(&fstab)) {
            return Error() << "Could not read default fstab";
        }
    } else {
        if (!ReadFstabFromFile(mount_all->fstab_path, &fstab)) {
            return Error() << "Could not read fstab";
        }
    }
    
    auto mount_fstab_result = fs_mgr_mount_all(&fstab, mount_all->mode);
}

fatab檔案內容

# Android fstab file.
#<src>                                          <mnt_point>         <type>    <mnt_flags and options>                       <fs_mgr_flags>
# The filesystem that contains the filesystem checker binary (typically /system) cannot
# specify MF_CHECK, and must come before any filesystems that do specify MF_CHECK
system  /system   ext4 ro,barrier=1 wait,logical,first_stage_mount
vendor  /vendor   ext4 ro,barrier=1 wait,logical,first_stage_mount
odm     /odm      ext4 ro,barrier=1 wait,logical,first_stage_mount

流程大致如下:

 流程:
 do_mount_all() -->  ReadFstabFromFile()  --->  fs_mgr_mount_all()  --->  mount_with_alternatives()
	
			mount_with_alternatives() -- > prepare_fs_for_mount() -->  __mount()
			
			mount_with_alternatives() --> 失敗(搞些修復操作) -->  fs_mgr_do_format() --> format_f2fs() || format_ext4()


ReadFstabFromFile() //主要是讀取fstab檔案資訊,填充FstabEntry結構體
    ParseFsMgrFlags()
    ParseMountFlags()

fs_mgr_mount_all() //這是fs_mgr共享庫的程式碼了,主要是判斷fs_mgr_flags

mount_with_alternatives() //最終呼叫到__mount  

 
format_ext4()      //執行命令
	"/system/bin/mke2fs", "-t", "ext4", "-b", "4096" -I 512
	"/system/bin/e2fsdroid", "-e", "-a", fs_mnt_point.c_str(), fs_blkdev.c_str(), nullptr
 
tune_quota() //如果是ext4,並且開啟了fstable裡有quota
	tune2fs, -Oquota, -Qusrquota,grpquota,prjquota, blk_device.c_str()
 
format_f2fs()    //執行命令
	"/system/bin/make_f2fs", "-g", "android" -O project_quota,extra_attr -O casefold -C utf8 -O compression -O extra_attr
	
prepare_fs_for_mount() //主要是檔案系統裝置塊檢查
	read_f2fs_superblock()		
	read_ext4_superblock()  // 如果有 quota需要開啟 tune_quota ext4 比如:tune_reserved_size,tune_verity 都是通過tune2fs修改ext引數
	
 
__mount()  //呼叫底層mount,這裡做兩兩次,一次失敗了就fsck後再做一次
	
ReadFstabFromFile()  //init程序  讀取fstab,填充結構提fstab,