Device number in stat command output

前端 未结 5 588
渐次进展
渐次进展 2020-12-09 18:19
  stat test.log 
  File: `test.log\'
  Size: 573         Blocks: 8          IO Block: 4096   regular file
Device: 804h/2052d  Inode: 7091301     Links: 1
Access: (06         


        
相关标签:
5条回答
  • 2020-12-09 18:52

    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.

    0 讨论(0)
  • 2020-12-09 18:59

    As has already been written here, 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, as can be seen for instance here:

    https://github.com/jeremie-koenig/glibc/blob/master-beware-rebase/sysdeps/generic/sys/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).

    0 讨论(0)
  • 2020-12-09 18:59

    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 讨论(0)
  • 2020-12-09 19:00

    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
    
    0 讨论(0)
  • 2020-12-09 19:04
    # 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.

    0 讨论(0)
提交回复
热议问题