Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [Q] get device major/minor number

Reply
Thread Tools

[Q] get device major/minor number

 
 
Thomas Portmann
Guest
Posts: n/a
 
      11-30-2010
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 ( 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
 
Reply With Quote
 
 
 
 
Thomas Portmann
Guest
Posts: n/a
 
      11-30-2010
On Tue, Nov 30, 2010 at 9:18 PM, Dan M <(E-Mail Removed)> wrote:
> On Tue, 30 Nov 2010 21:09:14 +0100, Thomas Portmann wrote:


>> In the example below, I would like to get the major ( 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cisco 1750 Router Cisco QoS Device Manager Cisco VPN Device Manager Rene Kuhn Cisco 0 12-28-2005 08:45 PM
877W - cannot talk wireless device to wireless device Nick Ersdown Cisco 7 10-31-2005 04:20 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
Determine the device is a router or switch given the Device IP kiranreddyd@gmail.com Cisco 14 12-26-2004 04:11 PM
802.11g router / 1 x 802.11b device / 1 x 802.11g device Oli Wireless Networking 3 09-27-2004 11:56 PM



Advertisments