Device number in stat command output

前端 未结 5 591
渐次进展
渐次进展 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: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).

提交回复
热议问题