0
$ cat /proc/partitions 
major minor  #blocks  name

   8        0  976762584 sda
   8        1   99998720 sda1
   8        2          1 sda2
   8        3  103561216 sda3
   8        4  291514368 sda4
   8        5    1998848 sda5
   8       16   31506432 sdb
   8       17   31505439 sdb1

I want to differentiate between physical disks (/dev/sda) and logical disks (/dev/sda1) from /proc/partitions output.

  1. One easy way I see is to check with some regex pattern whether it ends with integer or not
  2. As I see the output on my machine sda has least minor value than all sdaN entries
  3. check for existence of /sys/block/[NAME] So I'll try to read /sys/block/sdaN directories (which will fail) and /sys/block/sda (which will succeed)

I don't know which one is reliable and Is there any other ways ?

Also how can I reliably make a tree structure like sda : {sda1, sda2, sda3, sda4, sda4}, sdb : {sdb1}

  1. just by its name
  2. by minor numbers
  3. again look into /sys/block
  4. by position (Is it confirmed that sda1 will always come after sda ? e.g. would a logical partition entry always come after the physical disk entry ?)
Neel Basu
  • 12,638
  • 12
  • 82
  • 146

1 Answers1

1

It looks like you don't differentiate well between the primary and secondary partitions:

Partitions generally can be of type primary (maximum four), extended (maximum one) or logical (contained within the extended partition). Each partition has a type field that contains a code. This determines the computers operating system or the partitions file system.

Primary (max 4)        1-4, 
Extended (max 1)       1-4, 
Logical                5-

When it comes to the partitions naming, you usually see that hard disk devices are named /dev/hdx or /dev/sdx with x depending on the hardware configuration.

The partition number, starting the count at 1. Hence the four (possible) primary partitions are numbered 1 to 4. Logical partition counting always starts at 5. Thus /dev/hda2 is the second partition on the first ATA hard disk device, and /dev/hdb5 is the first logical partition on the second ATA hard disk device. Same for SCSI, /dev/sdb3 is the third partition on the second SCSI disk.

For you to create new partitions on disks, fdisk shall always be your friend and you just add the space starting from the last taken block, where you can always check them either through fdisk -l, or cat /proc/partitions

If you can't create devices using fdisk or parted tools, just mentioned it?!

αғsнιη
  • 2,627
  • 2
  • 25
  • 38
Abdel Hegazi
  • 368
  • 3
  • 14