Linux stat 命令输出中的设备号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4309882/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 00:11:18  来源:igfitidea点击:

Device number in stat command output

linux

提问by Ankur Agarwal

  stat test.log 
  File: `test.log'
  Size: 573         Blocks: 8          IO Block: 4096   regular file
Device: 804h/2052d  Inode: 7091301     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1001/   abc)   Gid: ( 1001/   abc)
Access: 2010-11-29 17:56:22.000000000 -0800
Modify: 2010-11-29 17:56:22.000000000 -0800
Change: 2010-11-29 17:56:22.000000000 -0800 

In the stat o/p above what does the Deviceentry signify ?

在上面的 stat o/p 中,Device条目表示什么?

采纳答案by Huang F. Lei

# stat tool
  File: `tool'
  Size: 4096            Blocks: 8          IO Block: 4096   directory
Device: 801h/2049d      Inode: 671689      Links: 3

# ls -l /dev/sda* 
brw-rw---- 1 root disk 8, 0 2010-08-16 14:43 /dev/sda
brw-rw---- 1 root disk 8, 1 2010-08-16 14:43 /dev/sda1
brw-rw---- 1 root disk 8, 2 2010-08-16 14:43 /dev/sda2
brw-rw---- 1 root disk 8, 5 2010-08-16 14:43 /dev/sda5

In the example, 'tool'(801h) is in /dev/sda1(major device number is 8, minor device number is 1), that's first partition in /dev/sda.

在示例中,'tool'(801h) 位于 /dev/sda1(主要设备编号为 8,次要设备编号为 1)中,即 /dev/sda 中的第一个分区。

回答by paxdiablo

It's the major and minor device number combined into one value (in hex and decimal) of the device on which the file resides.

它是组合成文件所在设备的一个值(十六进制和十进制)的主设备号和次设备号。

For your example, 804his major device 8, minor device 4. if you run df .while you're in the directory where that file is, you'll get the device name such as /dev/sda1. If you were to then do ls -al /dev/sda1, it would show you the device numbers. Here's an example:

对于您的示例,804h是主设备 8,次要设备 4。如果您df .在该文件所在的目录中运行,您将获得设备名称,例如/dev/sda1. 如果您要这样做ls -al /dev/sda1,它会向您显示设备编号。下面是一个例子:

pax$ stat newfile # note device 801h, hex 801 = 2049 decimal
  File: 'newfile'
  Size: 2097152     Blocks: 4096       IO Block: 4096   regular file
Device: 801h/2049d  Inode: 2888080     Links: 1
Access: (0644/-rw-r--r--)  Uid: ( 1000/     pax)   Gid: ( 1000/     pax)
Access: 2010-11-29 07:32:22.011271661 +0800
Modify: 2010-08-30 15:43:14.286796827 +0800
Change: 2010-08-30 15:43:14.286796827 +0800

pax$ df . # to get current device mount
Filesystem           1K-blocks Used Available Use% Mounted on
/dev/sda1            470301088 182471788 263939332  41% /

pax$ ls -al /dev/sda1 # to get major/minor = 8/1
brw-rw---- 1 root disk 8, 1 2010-11-30 07:02 /dev/sda1

回答by thkala

The stat command is simply a frontend to the stat() system call.

stat 命令只是 stat() 系统调用的前端。

From the stat(2) manual page (man 2 stat)

来自 stat(2) 手册页 ( man 2 stat)

The st_dev field describes the device on which this file resides. (The major(3) and minor(3) macros may be useful to decompose the device ID in this field.)

st_dev 字段描述了该文件所在的设备。(major(3) 和 minor(3) 宏可能有助于分解此字段中的设备 ID。)

From the 0804 hex notation you get major=8 (/dev/sd*) minor=4. i.e. /dev/sda4

从 0804 十六进制表示法中,您得到主要 = 8 (/dev/sd*) 次要 = 4。即 /dev/sda4

回答by anshul garg

In this 804h specifies the major number i.e. to associate correct driver code and 2052d is minor number maps each driver to a specific device instance. MAJOR(), MINOR() macro be be used to get device major and minor number together they form device number.

在此 804h 指定主编号,即关联正确的驱动程序代码,2052d 是次编号,将每个驱动程序映射到特定的设备实例。MAJOR(), MINOR() 宏用于获取设备的主次号和次要号,它们一起形成设备号。

回答by Dr. Jan-Philip Gehrcke

As has already been written here, from man 2 stat,

正如这里已经写的那样,从man 2 stat

The st_dev field describes the device on which this file resides. (The major(3) and minor(3) macros may be useful to decompose the device ID in this field.)

st_dev 字段描述了该文件所在的设备。(major(3) 和 minor(3) 宏可能有助于分解此字段中的设备 ID。)

These macros are not defined by POSIX, but implemented in glibc, as can be seen for instance here:

这些宏不是由 POSIX 定义的,而是在 glibc 中实现的,例如可以在这里看到:

https://github.com/jeremie-koenig/glibc/blob/master-beware-rebase/sysdeps/generic/sys/sysmacros.h

https://github.com/jeremie-koenig/glibc/blob/master-beware-rebase/sysdeps/generic/sys/sysmacros.h

The C implementation of these macros is:

这些宏的 C 实现是:

#define major(dev) ((int)(((unsigned int) (dev) >> 8) & 0xff))
#define minor(dev) ((int)((dev) & 0xff))

What you can easily do in e.g. Python then is

您可以在例如 Python 中轻松执行的操作是

>>> import os
>>> minor = int(os.stat("/lib").st_dev & 0xff)
>>> major = int(os.stat("/lib").st_dev >> 8 & 0xff)
>>> major, minor
(8, 1)

The major ID identifies the device driver, the minor ID encodes the physical disk as well as the partition. In case of SCSI disks, the major ID is always 8. Partitions on the first disk have a minor ID between 1 and 15. Partitions on the second disk have a minor ID between 17 and 31, and so on. Reference: https://www.mjmwired.net/kernel/Documentation/devices.txt

主要 ID 标识设备驱动程序,次要 ID 对物理磁盘和分区进行编码。对于 SCSI 磁盘,主要 ID 始终为 8。第一个磁盘上的分区的次要 ID 介于 1 和 15 之间。第二个磁盘上的分区的次要 ID 介于 17 和 31 之间,依此类推。参考:https: //www.mjmwired.net/kernel/Documentation/devices.txt

Hence,

因此,

>>> major, minor
(8, 1)

means sda1: sd(major 8 -> SCSI), a1(minor 1 -> first disk, first partition).

表示sda1:(sd主要 8 -> SCSI),a1(次要 1 -> 第一个磁盘,第一个分区)。