devfs

在linux设计当中秉承着“一切都是文件”的设计思维,在与各种设备打交道同样如此,内核通过将设备抽象成device file设备文件,device file是一种特殊的文件类型,用户层程序通过设备文件与具体的设备文件进行打交道,内核通过设备文件提供open, read ,ioctl等通用接口操作具体的设备。

Device files usually provide simple interfaces to standard devices (such as printers and serial ports), but can also be used to access specific unique resources on those devices, such as disk partitions. Additionally, device files are useful for accessing system resources that have no connection with any actual device, such as data sinks and random number generators.

Device file通常将各种各样的设备划分成两个文件类型:字符(character)和块(block)两种特殊的文件类型,两者的区别主要在读取数据和写数据的方式。

There are two general kinds of device files in Unix-like operating systems, known as character special files and block special files. The difference between them lies in how much data is read and written by the operating system and hardware. These together can be called device special files in contrast to named pipes, which are not connected to a device but are not ordinary files either.

在类unix系统中(linux 同样如此),device file通长作为 虚拟文件系统(virtual file system)管理的一部分被挂载到/dev目录中,通常在都有一个控制守护进程用来管理设备的添加和删除,以防止/dev目录过大。通常deve file的虚拟文件管理部分称之为devfs,而守护控制进程则为devd.

In some Unix-like systems, most device files are managed as part of a virtual file system traditionally mounted at /dev, possibly associated with a controlling daemon, which monitors hardware addition and removal at run time, making corresponding changes to the device file system if that's not automatically done by the kernel, and possibly invoking scripts in system or user space to handle special device needs. The FreeBSD and DragonFly BSD implementations have named the virtual device file system devfs and the associated daemon devd. Linux primarily uses a user space implementation known as udev, but there are many variants. Darwin, and operating systems such as macOS based on it, have a purely kernel-based device file system.

因此,linux系统在启动过程中通过mount 将所有的设备驱动挂载到dev目录中,用户空间通过/dev目录下的设备文件来访问具体的设备。

devfs 与sysfs区别?

sysfs 模型主要是在2.6之后才开始引入的,而devfs是在之前就已经存在,由于历史原因内核中有两套驱动模型, sysfs目前主要负责的是组织整个设备拓扑,驱动使用哪个总线等,而dev主要负责直接与设备驱动打交道,负责直接访问该设备,下面一句话更能说明两者区别:

  • The /sys filesystem (sysfs) contains files that provide information about devices: whether it's powered on, the vendor name and model, what bus the device is plugged into, etc. It's of interest to applications that manage devices.
  • The /dev filesystem contains files that allow programs to access the devices themselves: write data to a serial port, read a hard disk, etc. It's of interest to applications that access devices.
  • A metaphor is that /sys provides access to the packaging, while /dev provides access to the content of the box.
  • The files in /sys are not device nodes, but symbolic links and regular files. Those regular files are special in that reading or writing to them invokes file-specific functions in the kernel, like device nodes. The difference is that files in /sys work this way because of the filesystem they are on, whereas device nodes work this way due to their device node characteristics (the file type indicating a (block or character) device, and the device major and minor number indicating which device it is).

/dev目录说明

所有device files文件都通过devfs挂载到/dev目录中,可以通过ls /dev来查看/dev下的目录说明:

第1字段:

  • 注意到在每行的开始第一个字母:c或者b 分别代表字符设备或者是块设备,d代表目录,l代表为一个链接,

  • 然后是前三个表示文件拥有者的权限,中间三个表示文件所属组拥有的权限,最后三个表示其他用户拥有的权限.,r、w、x分别代表读、写、执行权限

第2字段的值为1,说明这个文件只有t这一个文件名。即只有一个指向该链接的硬链接,如果使用ln,做一个指向该文件的硬链接再查看该文件,该文件的第2字段就会变成2

第3字段:文件(目录)拥有者

第4字段:文件(目录)拥有者所在的组

第5字段:该设备主设备号

第6字段:为该设备次设备号

第7~9字段:代表时间

第10字段:代表文件名。

Device numbers

按照unix 系统传统 linux同样将每个设备按照 device number进行划分,每个driver和相应硬件都有相对应的实例,该实例会拥有一个唯一的device number。在kernel中,device number被用来作为硬件和驱动实例化的唯一根据,并且被作为key插入到合适的device 节点,该节点被存储在相应的文件系统中。

Conventional Unix systems have the concept of a device number". Each instance of a driver and hardware component is assigned a unique device number. Within the kernel, this device number is used to refer to the hardware and driver instance.The device number is encoded in the appropriate device node, and the device nodes are stored on normal disc-based file-systems.

为了进一步管理device number,该number又被划分为两个部分:major 和 minor number。每个驱动都会被指定一个major number, minor number被驱动用来决定使用哪个硬件实例化。

To aid management of device numbers, they are split into two components. These are called \major" and \minor" numbers. Each driver is assigned a major number. The minor number is used by the driver to determine which particular hardware instance is being accessed

在早期的linux 版本中,device number总共为16个bit,major number占8位, minor number占8为。这样最多支持256个主设备。

To aid management of device numbers, they are split into two components. These are called \major" and \minor" numbers. Each driver is assigned a major number. The minor number is used by the driver to determine which particular hardware instance is being accessed

在最新版本中,device number占用32位,其中major占20~31位,minor占0~19位:

虽然major可以使用达到了12位, minor可以使用20位, 但是为了兼容之前的历史版本,在字符和块驱动中仍然按照8位 255使用。

可以使用cat /proc/devices命令来查看当前系统中已经使用的设备号,该命令会首先将使用的字符设备号打印处理,然后会将块设备打印出来:

major 和 minor number

在内核源码文档Documentation\admin-guide\device.txt中记录了已经使用和分配的设备号状况, 其中字符设备和块设备的设备号两个分别管理不相干,即字符设可以使用1主设备号 也可以使用块设备的设备号:

Major Number

All devices controlled by the same device driver have a common major device number. The major number is actually the offset into the kernel’s device driver table, which tells the kernel what kind of device it is. Each character and Block driver that registered with kernel has associated MAJOR number. There are total 256 [0-255] major number for character and block driver respectively

Minor Number:

The minor number tells the kernel special characteristics of the device to be accessed. The minor number of the device node that represents the whole disk is 0. Minor Number is device “instance”. The minor number is not used by kernel they are used by the driver only.

同一个major number 意味着使用的是同一个驱动,而minor number是该驱动的一个实例化。

特殊/dev文件

以下文件都有一个特性都是虚拟的设备即并没有实际的硬件。

/dev/null :“空”设备就像一个黑洞一样可以吞噬一切,通常将丢弃不需要的数据输出, 或者用于输入流的空文件,永远不会返回数据。当不想让你的command有输出可以使用>/dev/null,当不想command有任何输入则可以使用</dev/null

you want to ignore the output from a command (>/dev/null) or run a command with no input (</dev/null).

/dev/zero:“零”设备,可以无限的提供空字符(0x00,ASCII代码NUL),常用来生成一个特定大小的文件。当我们使用或者读取他的时候,她是会提供无限连续不断的空的数据流 .

/dev/random和/dev/urandom:随机数设备,提供不间断的随机字节流。二者的区别是/dev/random产生随机数据依赖系统中断,当系统中断不足时,/dev/random设备会“挂起”,因而产生数据速度较慢,但随机性好;/dev/urandom不依赖系统中断,数据产生速度快,但随机性较低。

下述文件都是与进程相对应,每个进程一个

/dev/stdout: 标准输出

/dev/stdin:标准输入

/dev/stderr:标准出错

/dev/tty:控制终端

在/dev下面同样可以发现有很多软链接,这些链接的产生主要是一些历史原因,刚开始设计时放到当前目录 ,后来由于设计的变化将其转移到另外一个目录下,但是为了兼容之前旧的位置,只能通过软链接进行兼容:

You'll find a number of symbolic links under /dev. This can occur for historical reasons: a device file was moved from one name to another, but some applications still use the old name. For example, /dev/scd0 is a symbolic link to /dev/sr0 under Linux; both designate the first CD device

mknod命令

整个linux dev驱动模型如下。user space空间通过使用/dev目录下的设备文件与驱动打交道,而在内核层则是使用major和minor进行管理具体设备:

这样就需要一种机制 将/dev目录下的设备文件与 内核中的major和minor建立联系。

而mknod命令就是为了解决次问题而诞生的。mknod 命令建立一个目录项和一个特殊文件的对应索引节点,命令格式如下:

mknod 的标准形式为:       mknod DEVNAME {b | c}  MAJOR  MINOR

        主要常用选项参数列表如下:

选项

说明

--version

显示命令版本信息

--help

显示帮助信息

-m | --mode=MODE

设置权限

-Z |  --context=CTX

设置SELinux的安全上下文

设备类型

b,块设备;c,字符设备;u,没有缓冲的字符设备;p,fifo设备

设备号

只能是十进制和八进制,如果是0x开头,那么还是十进制。对于b、c、u设备,必须给出设备号,对于p设备,必须忽略设备号

b和c 分别表示块设备和字符设备:

当类型为b、c或u时,必须同时指定主设备号和次设备号;当类型为p时,必须省略它们。如果主设备号或次设备号以0x或0x开头,则解释为十六进制;否则,如果以0开头为八进制,其他为十进制。

mknod用例

创建块设备文件

[root@localhost ~]# mknod /dev/sdb4 b 1 1      //创建一个设备

[root@localhost weijie]# ls -l /dev/sdb4 

brw-r--r-- 1 root root 1, 1 9   7 08:21 /dev/sdb4

创建字符设备文件

[root@localhost ~]# mknod /dev/ttywj c 2 1      //创建一个设备

[root@localhost weijie]# ls -l /dev/ttywj 

crw-r--r-- 1 root root 2, 1 9   7 09:42 /dev/ttywj

内核中几个关于设备号重要宏

linux内核中关于设备号是使用 dev_t结构来表示,该结构定义在linux/types.h文件中,定义如下:

typedef u32 __kernel_dev_t;typedef __kernel_fd_set        fd_set;
typedef __kernel_dev_t        dev_t;
typedef __kernel_ino_t        ino_t;

其实是一个u32类型, linux内核还有几个关于major和minor的几个重要的宏主要定义在linux\kdev_t.h头文件中:

#define MINORBITS    20
#define MINORMASK   ((1U << MINORBITS) - 1)#define MAJOR(dev) ((unsigned int) ((dev) >> MINORBITS))
#define MINOR(dev)  ((unsigned int) ((dev) & MINORMASK))

主要是定义了目前major和minor划分状况,还有另外一个重要的宏MKDEV用于将major和minor转换成 dev_t

#define MKDEV(ma,mi) (((ma) << MINORBITS) | (mi))

上述几个宏是内核代码中常用到结构

linux /dev相关推荐

  1. Linux /dev目录详解和Linux系统各个目录的作用

    Linux /dev目录详解和Linux系统各个目录的作用 标签: linuxtcpfunctionclassfirefoxtimer 2012-01-11 23:08 45517人阅读 评论(2) ...

  2. linux dev sda 不存在,我查看linux分区情况为什么不是dev/hda而是dev/sda阿

    该楼层疑似违规已被系统折叠 隐藏此楼查看此楼 [root@localhost ~]# fdisk -l Disk /dev/sda: 40.0 GB, 40020664320 bytes 255 he ...

  3. linux /dev目录

    http://blog.chinaunix.net/u3/94284/showart_1982242.html linux /dev目录 linux /dev目录 dev 是设备(device)的英文 ...

  4. 嵌入式linux防复制,嵌入式linux /dev 下的文件无法复制

    嵌入式linux /dev 下的文件无法复制 关注:234  答案:4  mip版 解决时间 2021-02-06 06:27 提问者一段小時光 2021-02-06 00:38 root下也有文件不 ...

  5. linux shm性能,linux /dev/shm的用途

    /dev/shm/是linux下一个目录,/dev/shm目录不在磁盘上,而是在内存里,因此使用linux /dev/shm/的效率非常高,直接写进内存. 我们可以通过以下两个脚本来验证linux / ...

  6. Linux: /dev/fd/详解

    早上在使用htop查看系统情况时,发现了占用率最高的命令如下: java -jar test.jar /dev/fd/63 2/dev/fd/62 但我运行时输入的命令是这样的: java -jar ...

  7. linux /dev/snd,linux – / dev / tcp去了哪里?

    参见英文答案 > /dev/tcp not found????????????????????????????????????1个 >????????????/dev/tcp not pr ...

  8. linux卸载dev中的设备,Linux /dev 常见特殊设备介绍与应用

    linux是文件型系统,所有硬件如软件都会在对于的目录下面有相应的文件表示.对于dev这个目录,我们知道它下面的文件,表示的是linux的设备.在windows系统中,设备大家很好理解,象硬盘,磁盘指 ...

  9. linux -- /dev/null 21

    shell中可能经常能看到:>/dev/null 2>&1 命令的结果可以通过%>的形式来定义输出 /dev/null 代表空设备文件  > 代表重定向到哪里,例如:e ...

最新文章

  1. Spring Boot定时任务应用实践
  2. 源码包安装和快捷管理nginx,自定义命令管理服务
  3. jQuery控制tab标签页
  4. dart系列之:dart类中的构造函数
  5. zookeeper的ZAB协议学习
  6. 数据分析师如何正确的提意见?(文末赠书)
  7. 运维老哥必须知道的Linux系统文件目录组织结构
  8. 角速度求积分能得到欧拉角吗_一个有趣的反常积分问题
  9. MultiResolution研究
  10. java键盘输入一个数_Java如何实现从键盘输入一个数?
  11. java 设置主线程_Java线程编程中的主线程讲解
  12. 计算机科学与技术范文,计算机科学与技术专业(范文).doc
  13. case 逻辑java,java – 了解CaseInsensitiveComparator中的逻辑
  14. 乘法逆元(ex_gcd和同余定理)
  15. newifi3 高恪魔改_newifi 3 四大路由器固件优劣分析
  16. 大比例尺地图分幅行列编号参照表
  17. 理工专业单身男终极把妹大法
  18. 【实战讲解】数据血缘落地实施
  19. 基于jsp+mysql+Spring+mybatis java的SSM健身房管理系统
  20. 记一次任意用户密码重置漏洞(session覆盖)

热门文章

  1. [四川金信石招人]技术总监架构师测试工程师
  2. 【视频教程】JEECG 入门视频教程
  3. 以Jar形式为Web项目提供资源文件(JS、CSS与图片)
  4. joa-framework 工作流快速开发框架(jeecg官方工作流版本) 发布
  5. Windows 10系统下 MySql 5.7 my.ini 位置
  6. hadoop--HDFS概述
  7. Linux 关闭网络管理服务
  8. elasticsearch实现远程索引复制
  9. 网速变得奇慢说明可能需要安装金山ARP防火墙了
  10. 从java进程里dump出类的class文件的小工具--dumpclass