0

Below is the info I get using (g)parted or fdisk command.

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1  *         2048 196265983 196263936 93.6G 83 Linux
/dev/sda2       196268030 229468159  33200130 15.9G  5 Extended
/dev/sda5       196268032 229468159  33200128 15.9G 82 Linux swap / Solaris

enter image description here

Is there a system call or filesystem(/sys/block, etc) based info available to know if a partition is extended/primary and the hierarchy sda5 under sda2, etc? If yes, is there a way to know if what logical partitions lie under the selected extended partition?

I want to avoid parsing output of command(s). I have tried to read the code of fdisk but it is too complex for me to understand it.

Edit 1: After reading the comments and answers I came to know that I was not aware of MBR/GPT, etc. So, I decided to read about MBR and EBR internals https://thestarman.pcministry.com/asm/mbr/PartTables2.htm#ebr and wrote a code to get the partition details of a disk.

  • First thought is "why do you want to know?" – icarus Feb 23 at 3:13
  • I have a client who needs to know the disk partition hierarchy of his servers. The servers would be with minimal OS, os no fdisk/parted will be available – Nilesh Chate Feb 23 at 3:22
  • Thanks, so if partition size is 1 block in /proc/partitions, that partition is extended but how about the hierarchy ? how to find out which partitions come under this extended partition – Nilesh Chate Feb 23 at 3:36
  • /sys/block/sda/sda1 has the files start and size. By looking at those files for all the partitions you can build a map of the partition offsets. From that you can figure out which logical partition is inside which extended partition. – Emmanuel Rosa Feb 23 at 3:44
0

This question is not well defined.

If the disk is formatted using GPT, then there are no extended partitions, and so there is no heirarchy.

If the disk is formatted using MBR in such a way that a Microsoft OS can access it, then there is at most 1 extended partition, and a total of up to 4 primary and extended partitions. These will have names of the form /dev/sdf[0-3]. If there are any logical partitions, they will have partition numbers greater than 4. In order to have logical partitions you must have an extended partition to hold them.

However there is nothing requiring a linux system to have at most one extended partition, and there is nothing to say that there can be no overlaps of non-extended partitions, just as long as you don't use them.

So in particular you could have partition 1 being a smallish boot partition, partition 2 covering everything that wasn't in partition 1, partition 3 covering the first half of partition 2 and being of type "extended", partition 4 being the second half of partition 2. Inside partition 3 you could have partitions 5 and 6, each taking half of partition 3. This will all work OK provided that you don't actually try and use partition 2. However partitions 5 and 6 are both in both partitions 2 and 3, so the "hierarchy" is not a DAG (Directed Acyclic Graph), and so the "client need" can't be satisfied.

My advice for clients like this is to double your fees.

The "type" of the partition for the primary/extended is recorded in the MBR, so you can use code like dd bs=1 skip=446 count=64 if=/dev/sdf | hexdump -C to get something like this

00000000  00 20 21 00 83 df 13 0c  00 08 00 00 00 20 03 00  |. !.......... ..|
00000010  00 df 14 0c 05 19 0d cc  00 28 03 00 00 e0 2e 00  |.........(......|
00000020  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
00000030  00 19 0e cc 83 15 50 05  00 08 32 00 00 f8 0d 00  |......P...2.....|

and you can pick out the 5th byte (83, 05, 00, 83) to get the 4 values. The usual "type" of an extended partition is 05, but 0f and 85 are also used. See https://www.win.tue.nl/~aeb/partitions/partition_types-1.html for more information.

So with this you see if there is an extended partition (the second one in this case), and you then know that partitions with numbers greater than 4 are in it.

0

When using MBR partitioning style, Linux reserves the first 4 partition slots (e.g. sd*[1-4]) for primary partitions. In order to have logical partitions at all, one of those 4 needs to be used for an extended partition. And then the logical partitions will always appear starting from slot 5 onwards.

A MBR-partitioned disk is generally supposed to have no more than one extended partition: if there is two or more extended partitions with their own chains of logical partitions, I think the right thing to do could be to display a big warning and not proceed any further, as different OSs may have different ideas as to how to parse that.

In the location identified by your /sys/block/sda/sda2/start, you should find an Extended Partition Boot Record (EPBR) which is essentially a stripped-down version of MBR. Its first partition table entry should identify the start and size of the first logical partition. If there are further logical partitions, the second partition entry in the EPBR would then point to another EPBR, and so on. To fully analyze the disk layout, you may need to traverse the chain(s) of EPBRs.

0

It sounds like you just want parted -l. Here's what I get on one of my systems which has MBR tables and extended partitions:

$ sudo parted -l
Model: ASMT ASM1156-PM (scsi)
Disk /dev/sda: 1500GB
Sector size (logical/physical): 512B/4096B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      32.3kB  10.5GB  10.5GB  primary  ntfs         boot
 3      10.5GB  1500GB  1490GB  primary  ext4


Model: ASMT ASM1156-PM (scsi)
Disk /dev/sdb: 2000GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type     File system  Flags
 1      1049kB  1963GB  1963GB  primary  ext4         boot
 2      1963GB  2000GB  37.7GB  primary  ext4


Model: SD USD (sd/mmc)
Disk /dev/mmcblk0: 16.0GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags: 

Number  Start   End     Size    Type      File system  Flags
 1      4194kB  1686MB  1682MB  primary   fat32        lba
 2      1686MB  16.0GB  14.3GB  extended
 5      1686MB  1720MB  33.6MB  logical   ext4
 6      1720MB  1792MB  72.4MB  logical   fat32        lba
 7      1795MB  16.0GB  14.2GB  logical   ext4

That clearly shows you whether a partition is primary, extended or logical (the partitions created within an extended one). Another useful command is lsblk which, on the same system, prints:

$ lsblk 
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sda           8:0    0  1.4T  0 disk 
├─sda1        8:1    0  9.8G  0 part 
└─sda3        8:3    0  1.4T  0 part /media/pi/movies
sdb           8:16   0  1.8T  0 disk 
├─sdb1        8:17   0  1.8T  0 part /media/pi/bigboy
└─sdb2        8:18   0 35.2G  0 part 
mmcblk0     179:0    0 14.9G  0 disk 
├─mmcblk0p1 179:1    0  1.6G  0 part 
├─mmcblk0p2 179:2    0    1K  0 part 
├─mmcblk0p5 179:5    0   32M  0 part 
├─mmcblk0p6 179:6    0   69M  0 part /boot
└─mmcblk0p7 179:7    0 13.2G  0 part /

Here, you can know that a partition is extended since its size is negligible (mmcblk02, the extended partition, is shown as 1K).

  • Thanks :) but I wanted to avoid using commands. I got the info by reading MBR of disk – Nilesh Chate Feb 24 at 12:44
  • @NileshChate I don't understand, surely you need some command to read that too. Or do you only want the system call so you can integrate it into a tool you're writing? – terdon Feb 24 at 12:47
  • As mentioned by @icarus I read the first block of the device and than did further processing after going through thestarman.pcministry.com/asm/mbr/PartTables2.htm#ebr – Nilesh Chate Feb 24 at 12:51
  • @NileshChate OK, it would be great if you could post an answer explainaing what you did or, if possible, including the code. That way the question can be officially answered. – terdon Feb 24 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.