u 盤會當成 usb storage 在 Settings Storage 裏面顯示。node
內核需支持 usb host,需支持 FUSE 文件系統,在 FS 選項裏面配置便可(CONFIG_FUSE_FS=y)。android
mkdir /storage/udisk0 0000 system system socket
mkdir /mnt/media_rw/udisk0 0700 media_rw media_rw ide
service fuse_udisk0 /system/bin/sdcard -u 1023 -g 1023 -w 1023 -d /mnt/media_rw/udisk0 /storage/udisk0 測試
class late_start ui
disabled spa
/block/sda /mnt/media_rw/udisk0 vfat defaults voldmanaged=udisk0:auto .net
<storage android:mountPoint="/storage/udisk0" code
android:storageDescription="@string/storage_usb" orm
android:primary="false"
android:removable="true" />
/dev/block/vold/8:1 /mnt/media_rw/udisk0 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0
/dev/fuse /storage/udisk0 fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0
root@android :/ # logcat -s Vold MountService
--------- beginning of /dev/log/main
--------- beginning of /dev/log/system
I/Vold ( 820): Vold 2.1 (the revenge) firing up
D/Vold ( 820): Volume sdcard1 state changing -1 (Initializing) -> 0 (No-Media)
D/Vold ( 820): Volume sdcard1 state changing -1 (Initializing) -> 0 (No-Media)
D/Vold ( 820): Volume udisk0 state changing -1 (Initializing) -> 0 (No-Media)
D/Vold ( 820): Volume udisk0 state changing 0 (No-Media) -> 2 (Pending)
D/Vold ( 820): Volume udisk0 state changing 2 (Pending) -> 1 (Idle-Unmounted)
D/Vold ( 820): Volume sdcard1 state changing 0 (No-Media) -> 2 (Pending)
D/Vold ( 820): Volume sdcard1 state changing 2 (Pending) -> 1 (Idle-Unmounted)
D/MountService( 1587): got storage path: /storage/sdcard0 description: Internal storage primary: true removable: false emulated: true mtpReserve: 50 allowMassStorage: false maxFileSize: 0
D/MountService( 1587): addVolumeLocked() StorageVolume:
D/MountService( 1587): mStorageId=65537 mPath=/storage/emulated/0 mDescriptionId=17040662
D/MountService( 1587): mPrimary=true mRemovable=false mEmulated=true mMtpReserveSpace=50
D/MountService( 1587): mAllowMassStorage=false mMaxFileSize=0 mOwner=UserHandle{0} mUuid=null
D/MountService( 1587): mUserLabel=null mState=null
D/MountService( 1587): got storage path: /storage/sdcard1 description: SD card primary: false removable: true emulated: false mtpReserve: 0 allowMassStorage: false maxFileSize: 4294967296
D/MountService( 1587): addVolumeLocked() StorageVolume:
D/MountService( 1587): mStorageId=0 mPath=/storage/sdcard1 mDescriptionId=17040663 mPrimary=false
D/MountService( 1587): mRemovable=true mEmulated=false mMtpReserveSpace=0 mAllowMassStorage=false
D/MountService( 1587): mMaxFileSize=4294967296 mOwner=null mUuid=null mUserLabel=null mState=null
D/MountService( 1587): got storage path: /storage/udisk0 description: USB storage primary: false removable: true emulated: false mtpReserve: 0 allowMassStorage: false maxFileSize: 0
D/MountService( 1587): addVolumeLocked() StorageVolume:
D/MountService( 1587): mStorageId=0 mPath=/storage/udisk0 mDescriptionId=17040664 mPrimary=false
D/MountService( 1587): mRemovable=true mEmulated=false mMtpReserveSpace=0 mAllowMassStorage=false
D/MountService( 1587): mMaxFileSize=0 mOwner=null mUuid=null mUserLabel=null mState=null
W/MountService( 1587): Duplicate state transition (unmounted -> unmounted) for /storage/sdcard1
D/MountService( 1587): volume state changed for /storage/sdcard1 (unmounted -> removed)
W/MountService( 1587): Duplicate state transition (unmounted -> unmounted) for /storage/udisk0
I/Vold ( 820): /dev/block/vold/8:1 being considered for volume udisk0
D/Vold ( 820): Volume udisk0 state changing 1 (Idle-Unmounted) -> 3 (Checking)
D/MountService( 1587): volume state changed for /storage/udisk0 (unmounted -> checking)
D/MountService( 1587): sendStorageIntent Intent { act=android.intent.action.MEDIA_CHECKING dat=file:///storage/udisk0 (has extras) } to UserHandle{-1}
I/Vold ( 820): Filesystem check completed OK
D/Vold ( 820): blkid identified as /dev/block/vold/8:1: UUID="402E-0EE3" TYPE="vfat"
D/Vold ( 820): Volume udisk0 state changing 3 (Checking) -> 4 (Mounted)
D/MountService( 1587): volume state changed for /storage/udisk0 (checking -> mounted)
D/MountService( 1587): sendStorageIntent Intent { act=android.intent.action.MEDIA_MOUNTED dat=file:///storage/udisk0 (has extras) } to UserHandle{-1}
W/MountService( 1587): Duplicate state transition (mounted -> mounted) for /storage/emulated/0
在 Android 4.4 中, Vold 會解析 vold.<hardware> 文件
property_get("ro.hardware", propbuf, "");
snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);
fstab = fs_mgr_read_fstab(fstab_filename);
if (!fstab) {
SLOGE("failed to open %s\n", fstab_filename);
return -1;
}
其中 #define FSTAB_PREFIX "/fstab." 因此,配置的 fatab 文件必須放在根目錄, Vold 是 android 的後臺進程,將會一直監聽 fatab 裏面標記爲 voldmanaged 的項目
若是沒有標記,則略過。沒有標記的項目。能夠交給 init 的 mount 命令處理,可是不會監聽,因此你能夠將recovery 和 cache 目錄這樣只需啓動是掛載的目錄項放在 fatab
裏面交給 init 的 mount 命令處理,例如三星 device/samsung/manta/fstab.manta
# Android fstab file.
#<src> <mnt_point> <type> <mnt_flags and opt
ions> <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
/dev/block/platform/dw_mmc.0/by-name/system /system ext4 ro
wait
/dev/block/platform/dw_mmc.0/by-name/cache /cache ext4 noatime,nosuid,nodev,nombl
k_io_submit,errors=panic wait,check
/dev/block/platform/dw_mmc.0/by-name/userdata /data ext4 noatime,nosuid,nodev,nombl
k_io_submit,errors=panic wait,check,encryptable=/dev/block/platform/dw_mmc.0/by-name/metadata
/dev/block/platform/dw_mmc.0/by-name/efs /factory ext4 noatime,nosuid,nodev,ro
wait
/dev/block/platform/dw_mmc.0/by-name/boot /boot emmc defaults
defaults
/dev/block/platform/dw_mmc.0/by-name/recovery /recovery emmc defaults
defaults
/dev/block/platform/dw_mmc.0/by-name/misc /misc emmc defaults
defaults
/dev/block/mmcblk0boot0 /bootloader emmc defaults
defaults
在系統啓動調用 mount :
on fs
mkdir /factory 0775 radio radio
mount_all /fstab.manta
同理, init 的 mount 也會忽略標記爲 voldmanaged 的項目
init 的 mount 命令與 vold 的 mount 管理大有不一樣,二者不能混用。
MountService 與 Vold 會創建 socket 通訊,具體的掛載操做都是下發命令,交給 Vold 去完成。 MountService 會解析咱們配置的 storage_list.xml
private void readStorageListLocked() {
mVolumes.clear();
mVolumeStates.clear();
Resources resources = mContext.getResources();
int id = com.android.internal.R.xml.storage_list;
XmlResourceParser parser = resources.getXml(id);
AttributeSet attrs = Xml.asAttributeSet(parser);
try {
XmlUtils.beginDocument(parser, TAG_STORAGE_LIST);
while (true) {
XmlUtils.nextElement(parser);
Vold 獲取到 list 裏面的設備變化時將會發送 state change event, MountService 在 onEvent 處理各類不一樣 state ,其中獲取到設備插入,將會執行掛載操做:
if (code == VoldResponseCode.VolumeDiskInserted) {
new Thread("MountService#VolumeDiskInserted") {
@Override
public void run() {
try {
int rc;
if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {
Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));
}
} catch (Exception ex) {
Slog.w(TAG, "Failed to mount media on insertion", ex);
}
}
}.start();
doMountVolume 只是發送 mount 命令給 Vold
private int doMountVolume(String path) {
int rc = StorageResultCode.OperationSucceeded;
final StorageVolume volume;
synchronized (mVolumesLock) {
volume = mVolumesByPath.get(path);
}
if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);
try {
mConnector.execute("volume", "mount", path);
最終 Vold 裏面執行相應的 mount 操做,咱們的 u 盤是 vfat 格式,我只測試了這個格式, Linux 下的格式沒有測試,由於 Vold 只支持 fat 和 ext4
int VolumeManager::mountVolume(const char *label) {
Volume *v = lookupVolume(label);
if (!v) {
errno = ENOENT;
return -1;
}
return v->mountVol();
}
if (Fat::doMount(devicePath, getMountpoint(), false, false, false,
AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) {
SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));
continue;