5

In linux, I am using the blkid apis to get most information about disk partitions. I am unable to find an API that would let me find out the mapping between a partition name and its drive.

For example, the disk /dev/sda is partitioned into /dev/sda1 and /dev/sda2. Given two partition names, how do I know whether it comes from the same drive ? By looking for a common prefix (sda), one could see if they belong to the same drive or not. But is this fool proof ? Will this work in all cases ? What if this is part of a volume ? Also, can the drive name/volume name ("sda") contain numbers ?

I am assuming that there should be an easy way to determine this mapping.

user3092576
  • 51
  • 1
  • 2
  • 2
    This [1] might help to find an answer.No you can't just remove the last numeric value(i.e. sda1 removing 1) and get the divece node which sda1 belongs to.In device mapping scenarios they name multipath devices as "dm-1","dm-2" so just removing the last digit will not give the device node which it belongs to. [1]:https://github.com/GNOME/gparted/blob/master/src/Proc_Partitions_Info.cc#L90-L112 – TMKasun Jul 18 '14 at 06:15

2 Answers2

2

I found two methods to get the kernal name of the parent disk of a partition,

  1. Straightforward method , you can use lsblk command to get the

    sudo lsblk /dev/sda1 -o SIZE,KNAME,PKNAME,MOUNTPOINT,FSTYPE

    You can give PKNAME in options list and it will give the kernel names of parent device.

  2. Second method is little bit tricky ,you can use udevadmn tool to find the MINOR:MAJOR pair of the parent disk/device, and search for thos MAJOR:MINOR pair among the attached devices.

sudo udevadm info --query=property --name=/dev/sda1 | grep ID_PART_ENTRY_DISK

This list is the Linux Device List, the official registry of allocated device numbers and /dev directory nodes for the Linux operating system.

TMKasun
  • 784
  • 7
  • 24
  • PKNAME gets your the parent. But if you create nested partitions it gives you the immediate parent. How do you find the root parent (the actual hard disk)? – CMCDragonkai Feb 02 '16 at 09:30
0

checkout /dev/disk/by-xxxxx files