[Ilugc] How to read partition table details with c program

Bhasker C V bhasker at unixindia.com
Sun Jun 24 21:46:51 IST 2007


Hi,

You can try sysfs and proc file system combined to get the information

1. /sys/block/sda
   read all sda* directories using readdir (this gives all the
   partitions in the sda disk (man readdir)

2. now using that list go one by one into the induvidual directories
   /sys/block/sda1/
   There is a file start. That contains the start sector of the
   partition 
   There is a file size. That is the size (in number of sectors)
   so End sector = <start> + <size> -1 
3. The number of blocks of that parition can be got
    from /proc/partitions

4. continue for all other values in the struct dirent 

If the partition number is <5 then it is safely a primary parition
and anything >= 5 is extended partition.

Is there any other easier method to do this ? i think i am complicating
things toomuch and it must not be so tough to get the information in
linux.


OR

you can use fdisk ( popen to fdisk and then send the command 
  "fdisk -l" ) 


On Sun, 2007-06-24 at 18:19 +0530, Senthil Murugan wrote:
> Hi,
> 
> I want to read the partition information of a device say /dev/sda. I tried
> reading the /proc/partitions file,  however i couldn't able to get the
> start, end,  blocks, partition type, etc from the proc file system. Can any
> plz help me in getting the list of partition and its size, partition
> type(extended,primary,etc) and filesystem(ext2,ext3...) with a simple c
> program.
> 
> Thanks,
> Senthil
> _______________________________________________
> To unsubscribe, email ilugc-request at ae.iitm.ac.in with 
> "unsubscribe <password> <address>"
> in the subject or body of the message.  
> http://www.ae.iitm.ac.in/mailman/listinfo/ilugc
-- 
Bhasker C V
Registered Linux user: #306349 (counter.li.org)
The box said "Requires Windows 95, NT, or better", so I installed Linux.



More information about the ilugc mailing list