dev、devfs、 udev和sysfs之間的關係

一、/dev存儲的是linux可以認識的所有的文件設備名,無論這個設備有沒有在系統裏。爲了解決/dev目錄過大的問題,就 產生了devfs,但devfs是基因內核的,有很多無法解決的問題(參考前面的文章),所以就出現了udev。而udev對設備在/dev目錄下的命名 依賴於sysfs所提供的規則信息,這些規則信息由用戶提供。sysfs是在2.6內核時加進linux內核的。sysfs就是我們系統加載的/sys。

二、devfs
       linux下有專門的文件系統用來對設備進行管理,devfs和sysfs就是其中兩種。 
在2.6內核以前一直使用的是devfs,devfs掛載於/dev目錄下,提供了一種類似於文件的方法來管理位於/dev目錄下的所有設備,我們知道/dev目錄下的每一個文件都對應的是一個設備,至於當前該設備存在與否先且不論,而且這些特殊文件是位於根文件系統上的,在製作文件系統的時候我們就已經建立了這些設備文件,因此通過操作這些特殊文件,可以實現與內核進行交互。但是devfs文件系統有一些缺點,例如:不確定的設備映射,有時一個設備映射的設備文件可能不同,例如我的U盤可能對應sda有可能對應sdb;沒有足夠的主/輔設備號,當設備過多的時候,顯然這會成爲一個問題;/dev目錄下文件太多而且不能表示當前系統上的實際設備;命名不夠靈活,不能任意指定等等。


三、sysfs 
      正因爲上述這些問題的存在,在linux2.6內核以後,引入了一個新的文件系統sysfs,它掛載於/sys目錄下,跟devfs一樣它也是一個虛擬文件系統,也是用來對系統的設備進行管理的,它把實際連接到系統上的設備和總線組織成一個分級的文件,用戶空間的程序同樣可以利用這些信息以實現和內核的交互,該文件系統是當前系統上實際設備樹的一個直觀反應,它是通過kobject子系統來建立這個信息的,當一個kobject被創建的時候,對應的文件和目錄也就被創建了,位於/sys下的相關目錄下,既然每個設備在sysfs中都有唯一對應的目錄,那麼也就可以被用戶空間讀寫了。用戶空間的工具udev就是利用了sysfs提供的信息來實現所有devfs的功能的,但不同的是udev運行在用戶空間中,而devfs卻運行在內核空間,而且udev不存在devfs那些先天的缺陷。很顯然,sysfs將是未來發展的方向。
The top level sysfs directory looks like:
block/
bus/
class/
devices/
firmware/
net/
fs/

devices/ contains a filesystem representation of the device tree. It maps
directly to the internal kernel device tree, which is a hierarchy of
struct device. 

bus/ contains flat directory layout of the various bus types in the
kernel. Each bus's directory contains two subdirectories:
       devices/
       drivers/
devices/ contains symlinks for each device discovered in the system
that point to the device's directory under root/.

drivers/ contains a directory for each device driver that is loaded
for devices on that particular bus (this assumes that drivers do not
span multiple bus types).

fs/ contains a directory for some filesystems. Currently each
filesystem wanting to export attributes must create its own hierarchy
below fs/ (see ./fuse.txt for an example).


四、udev
      udev是一種工具,它能夠根據系統中的硬件設備的狀況動態更新設備文件,包括設備文件的創建,刪除等。設備文件通常放在/dev目錄下,使用udev後,在/dev下面只包含系統中真實存在的設備。它於硬件平臺無關的,位於用戶空間,需要內核sysfs和tmpfs的支持,sysfs爲udev提供設備入口和uevent通道,tmpfs爲udev設備文件提供存放空間。


On typical Linux-based systems, the /dev directory is used to store file-like device nodes which refer to certain devices in the system. Each node points to a part of the system (a device), which might or might not exist. Userspace applications can use these device nodes to interface with the systems hardware, for example, the X server will "listen to" /dev/input/mice so that it can relate the user's mouse movements to moving the visual mouse pointer.

      

       The original /dev directories were just populated with every device that might possibly appear in the system. /dev directories were typically very large because of this. devfs came along to provide a more manageable approach (noticeably, it only populated /dev with hardware that is plugged into the system), as well as some other functionality, but the system proved to have problems which could not be easily fixed.


       udev is the "new" way of managing /dev directories, designed to clear up some issues with previous /devimplementations, and provide a robust path forward. In order to create and name /dev device nodes corresponding to devices that are present in the system, udev relies on matching information provided by sysfs with rules provided by the user. This documentation aims to detail the process of rule-writing, one of the only udev-related tasks that must (optionally) be performed by the user.


       sysfs is a new filesystem to the 2.6 kernels. It is managed by the kernel, and exports basic information about the devices currently plugged into your system. udev can use this information to create device nodes corresponding to your hardware. sysfs is mounted at /sys and is browseable. You may wish to investigate some of the files stored there before getting to grips with udev. Throughout this document, I will use the terms /sys and sysfs interchangeably.



發佈了53 篇原創文章 · 獲贊 12 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章