2015年11月9日 星期一

USB Auto Mounting 實現

mdev 是一隻相當於udev 精簡版的busybox applet,適合在嵌入式系統下管理設備,你需要修改定義 mdev.conf, 以及布署一支處理程式(用shell script 也好,用C 也可) 已執行 掛在處理細節,包括 race condition, 或 critical section re-entrant code 的保護。這些其實在 Windows 也是有先例,Windows 有時對於某頑強情況也同樣不能完善地掛載。

mdev 將所有的設備檔都動態地建立在 /dev/目錄下,如果程式使用dev目錄子目錄下的設備,就必須修改程式,比較不建議這樣做,當然也可以在mdev運行之後,寫事件偵測程式動態地創建子目錄,然後再用 ln -s 做設備檔的軟鏈結。

Usage
mdev 有兩個基本的應用:初始化物件和動態更新。
Linux OS 核心要 enable sysfs support、hotplugging (動態熱插拔(更新))。
require sysfs support in the kernel and have it mounted at /sys.
內核 sysfs 必須掛載到 /sys
以下是系統初始化腳本中使用mdev:
mount -t tmpfs mdev /dev
mkdir /dev/pts
mount -t devpts devpts /dev/pts
mount -t sysfs sysfs /sys                //必須在執行mdev 前掛載 /sys
echo /bin/mdev > /proc/sys/kernel/hotplug
mdev -s    //
執行mdev

Hot-plug and auto-mounting:

Auto-mounting part has both front-end scripts based and backend mdev hot-plug calling. The principle of USB disk detection/mount is to use a front-end utility called “blkid” to identify exactly the type of file system to do continuously matching mount multiple partitions at one time per looking up “/proc/mount” beforehand, and simultaneously excludes double mounting for a disk partition, especially, also use the concept of mutex to protect the mount code block to prohibit the event coming at second time for every sub-partition mdev event. These are some methods implementation on the usbeventhandling.sh to support a USB disk with multiple partitions, for example, ecclusively access safely, not-delay method of removing transient state and equivalent timing budget.

Design Concept of automatically mounting a USB disk:
The design of USB Hot-Plug and auto-mounting mechanism for a USB storage, besides that use a small size 'mdevd', is a miniature version of udevd, so that, also design some methods to handle automatically disk-mounting task with multiple partitions and different type of file system when user insert or remove a USB storage.

The principle of USB disk detection/mount is to use a front-end utility called “blkid” to identify exactly the type of file system to do continuously matching mount multiple partitions at one time per looking up “/proc/mount” beforehand, and simultaneously excludes double mounting for a disk partitionThe method improve the protection of critical section for auto mounting codeblock with exclusively access safely and reserve the traditional trial method, and also collect device and status information at initial time for speeding up parsing process.

mdev calls auto mount helper when it is detecting the USB device existence and create the device node for it.

以下流程圖是筆者之實作。


沒有留言:

張貼留言