Fork me on GitHub

[Android][Recovery]自动挂载system分区

前一篇Recovery打开adb shell里提到system目录是用来挂载系统/system分区的,所以是一个空目录。这一点是通过打开adb shell后,查看文件目录知道的。

因为原本cache目录也是空的,但是通过shell能看到/cache目录里的文件和Android系统是一致的,也就说明,这个目录是挂载进来的。因此推测system也应该是用来挂载的。

关于挂载/system,在Recovery里有实现代码动态挂载:

// bootable/recovery/recovery.cpp
case Device::MOUNT_SYSTEM:
char system_root_image[PROPERTY_VALUE_MAX];
property_get("ro.build.system_root_image", system_root_image, "");

// For a system image built with the root directory (i.e.
// system_root_image == "true"), we mount it to /system_root, and symlink /system
// to /system_root/system to make adb shell work (the symlink is created through
// the build system).
// Bug: 22855115
if (strcmp(system_root_image, "true") == 0) {
if (ensure_path_mounted_at("/", "/system_root") != -1) {
ui->Print("Mounted /system.\n");
}
} else {
if (ensure_path_mounted("/system") != -1) {
ui->Print("Mounted /system.\n");
}
}

break;

但是实现开机挂载,我们就要去看init.rc了。

mvim /bootable/recovery/etc/init.rc

import /init.recovery.${ro.hardware}.rc

on early-init
# Set the security context of /postinstall if present.
restorecon /postinstall

start ueventd
start healthd

on init
export ANDROID_ROOT /system
export ANDROID_DATA /data
export EXTERNAL_STORAGE /sdcard

symlink /system/etc /etc

mkdir /sdcard
mkdir /udisk
mkdir /system
mkdir /data
mkdir /cache
mkdir /sideload
mkdir /firmware
mount tmpfs tmpfs /tmp

chown root shell /tmp
chmod 0775 /tmp

write /proc/sys/kernel/panic_on_oops 1
write /proc/sys/vm/max_map_count 1000000

on fs
mkdir /dev/usb-ffs 0770 shell shell
mkdir /dev/usb-ffs/adb 0770 shell shell
mount functionfs adb /dev/usb-ffs/adb uid=2000,gid=2000

write /sys/class/android_usb/android0/enable 0
write /sys/class/android_usb/android0/idVendor 18D1
write /sys/class/android_usb/android0/idProduct D001
write /sys/class/android_usb/android0/f_ffs/aliases adb
write /sys/class/android_usb/android0/functions adb
write /sys/class/android_usb/android0/iManufacturer ${ro.product.manufacturer}
write /sys/class/android_usb/android0/iProduct ${ro.product.model}
write /sys/class/android_usb/android0/iSerial ${ro.serialno}

首先会去访问init.recovery.${ro.hardware}.rc的内容,这个文件不存在,然后后面的流程也没有做mount操作。

作为对比,看一下Android系统的init文件

mvim /device/qcom/xxxx/imit.target.rc

on fs
wait /dev/block/bootdevice
mount_all fstab.qcom
write /proc/sys/vm/swappiness 100

好了,Android系统启动的时候做了一个mount_all操作。来看一下fstab.qcom文件

# Android fstab file.
# 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

#TODO: Add 'check' as fs_mgr_flags with data partition.
# Currently we dont have e2fsck compiled. So fs check would failed.

#<src> <mnt_point> <type> <mnt_flags and options> <fs_mgr_flags>
/dev/block/bootdevice/by-name/system /system ext4 ro,barrier=1,discard wait, verify
/dev/block/bootdevice/by-name/userdata /data ext4 nosuid,nodev,barrier=1,noauto_da_alloc,discard wait,forceencrypt=footer
/devices/soc/7864900.sdhci/mmc_host* /storage/sdcard1 vfat nosuid,nodev wait,voldmanaged=sdcard1:auto,noemulatedsd,encryptable=footer
/devices/soc/78db000.usb/msm_hsusb_host* /storage/usbotg vfat nosuid,nodev wait,voldmanaged=usbotg:auto
/dev/block/bootdevice/by-name/config /frp emmc defaults defaults
/dev/block/bootdevice/by-name/misc /misc emmc defaults defaults

我们的system就在这个表里。所以要挂载system,只需要在Recovery的init.rc添加:

wait /dev/block/bootdevice/by-name/system
mount ext4 /dev/block/bootdevice/by-name/system /system wait rw barrier=1

然后还要注意,挂载点应该(没专门验证)是空目录,所以如果在Recovery开启了shell,要把它删掉。

坚持原创技术分享,您的支持将鼓励我继续创作!