[Q] get device major/minor number

Discussion in 'Python' started by Thomas Portmann, Nov 30, 2010.

  1. Hello all,

    In a script I would like to extract all device infos from block or
    character device. The "stat" function gives me most of the infos
    (mode, timestamp, user and group id, ...), however I did not find how
    to get the devices major and minor numbers. Of course I could do it by
    calling an external program, but is it possible to stay within python?

    In the example below, I would like to get the major (8) and minor (0,
    1, 2) numbers of /dev/sda{,1,2}. How can I get them?

    user@host:~$ ls -l /dev/sda /dev/sda1 /dev/sda2
    brw-rw---- 1 root disk 8, 0 Nov 30 19:10 /dev/sda
    brw-rw---- 1 root disk 8, 1 Nov 30 19:10 /dev/sda1
    brw-rw---- 1 root disk 8, 2 Nov 30 19:10 /dev/sda2
    user@host:~$ python3.1 -c 'import os
    for el in ["","1","2"]: print(os.stat("/dev/sda"+el));'
    posix.stat_result(st_mode=25008, st_ino=1776, st_dev=5, st_nlink=1,
    st_uid=0, st_gid=6, st_size=0, st_atime=1291140641,
    st_mtime=1291140640, st_ctime=1291140640)
    posix.stat_result(st_mode=25008, st_ino=1780, st_dev=5, st_nlink=1,
    st_uid=0, st_gid=6, st_size=0, st_atime=1291140644,
    st_mtime=1291140641, st_ctime=1291140641)
    posix.stat_result(st_mode=25008, st_ino=1781, st_dev=5, st_nlink=1,
    st_uid=0, st_gid=6, st_size=0, st_atime=1291140644,
    st_mtime=1291140641, st_ctime=1291140641)

    Thanks


    Tom
     
    Thomas Portmann, Nov 30, 2010
    #1
    1. Advertising

  2. On Tue, Nov 30, 2010 at 9:18 PM, Dan M <> wrote:
    > On Tue, 30 Nov 2010 21:09:14 +0100, Thomas Portmann wrote:


    >> In the example below, I would like to get the major (8) and minor (0, 1,
    >> 2) numbers of /dev/sda{,1,2}. How can I get them?

    >
    > I think the os.major() and os.minor() calls ought to do what you want.
    >
    >>>> import os
    >>>> s = os.stat('/dev/sda1')
    >>>> os.major(s.st_rdev)

    > 8
    >>>> os.minor(s.st_rdev)

    > 1


    Thank you very much Dan, this is exactly what I was looking for.


    Tom
     
    Thomas Portmann, Nov 30, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kokwooi

    Major Major Problem With ASP.NET

    kokwooi, Sep 18, 2003, in forum: ASP .Net
    Replies:
    6
    Views:
    693
    NuTcAsE
    Sep 19, 2003
  2. =?Utf-8?B?dGhlV2l6YXJkMQ==?=

    Publish and specify Major, Minor Version numbers using asp.net 2.0

    =?Utf-8?B?dGhlV2l6YXJkMQ==?=, Apr 26, 2006, in forum: ASP .Net
    Replies:
    1
    Views:
    2,753
    Juan T. Llibre
    Apr 26, 2006
  3. Alain Tesio
    Replies:
    1
    Views:
    643
    Erik Max Francis
    Aug 24, 2003
  4. Jef Driesen
    Replies:
    2
    Views:
    1,774
    Jef Driesen
    Jan 12, 2006
  5. Brent S. Elmer Ph.D.
    Replies:
    0
    Views:
    70
    Brent S. Elmer Ph.D.
    Apr 29, 2014
Loading...

Share This Page