Find the answer to your Linux question:
Results 1 to 7 of 7
Hello. I need to write a program that shows disks and their partitions size and types. So i need to read partition table, right? But i don't know how to ...
  1. #1
    Just Joined!
    Join Date
    Feb 2008
    Posts
    19

    Reading partition table.

    Hello.

    I need to write a program that shows disks and their partitions size and types.
    So i need to read partition table, right?

    But i don't know how to do this.

    Please help me.

    Thank you.

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Browse the source code for cfdisk, which can be found in this package.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined!
    Join Date
    Feb 2008
    Posts
    19
    Ok so I look into fdisk source code.

    And I did manage to get disk or partition size with this code.

    Code:
    #define BLKGETSIZE64 _IOR(0x12,114,size_t)	/* size in bytes */
    if ((fd = open(argv[1], O_RDONLY)) < 0) 
    {
    	printf("Napaka pri odpiranju!\n");
    }
    
    err = ioctl(fd, BLKGETSIZE64, &b);
    if (err || b == 0 || b == sz)
    	printf("Napaka!\n");
    else
    	*sectors = b;
    	
    long long bytes = (*sectors);
    printf("Velikost v MB: %lld\n",bytes/1000000);

    But now I don't know how to get partition type.
    I don't understand the code when the program is getting partition type.

    Please help me.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    I've never used fdisk, but I've used cfdisk (which is the code that I recommended you inspect). cfdisk displays each partition type as a small integer. I think there's a hard-coded correspondence between the number and the partition type.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.
    Quote Originally Posted by wje_lf View Post
    ... a hard-coded correspondence between the number and the partition type.
    The page Partition types: List of partition identifiers for PCs looks useful -- referred from Partition (computing - Wikipedia, the free encyclopedia) ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Just Joined!
    Join Date
    Feb 2008
    Posts
    19
    cfdisk displays each partition type as a small integer.
    Yes I noticed that.
    But the problem is that i don't know how to get that small integer.

    Please help me.
    Thank you for your replys.

  7. #7
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    But the problem is that i don't know how to get that small integer.
    That small integer is most likely stored in either the master boot record for the disk, or somewhere in the first few blocks of the actual partition.

    You'll need to read the source code for fdisk (or cfdisk) to find out how it's done. Reading the source code isn't rocket science, but it is time-consuming, which is why I won't do it for you. :)

    One approach is to just skim all source files for the program, glancing at the comments and the name of each function until you find what you're looking for.

    Another, which is more time-consuming but begins to look more attractive if the first approach doesn't work, is to look for each call to function read(). Then see what function that call is in, and look for where those functions are called, and so on.

    Good luck!
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...