开发者

Device number in stat command output

  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.000开发者_运维知识库000000 -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 Device entry signify ?


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, 804h is 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:

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


# 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 the first partition in /dev/sda.


From 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.)

These macros are not defined by POSIX, but implemented in glibc, in sysmacros.h. The C implementation of these macros is:

#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

>>> 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

Hence,

>>> major, minor
(8, 1)

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


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

From the stat(2) manual page (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.)

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜