How do I get (reliably) a partition device name knowing block device and partition number?

For example:

_get_part_dev_from_disk_dev /dev/ccis0 3 => /dev/ccis0p3. # notice the p

_get_part_dev_from_disk_dev /dev/sde 2 => /dev/sde2

My current method is the following but it is buggy as it assumes minor number is the partition number which is completely wrong except for 1st disk. minor can be computed, but it has many limitations. one is that after a maximum, dev does dynamic allocation.

_get_part_dev_from_disk_dev() {
        if test -b "$1"
                DEV_MAJOR=$(printf "%d" "0x$(stat -c '%t' $1)")
                shellout "[$1] is no a block device"

        test -n "${2//[0-9]/}" && shellout "[$2] is not a partition number"

        if test ! -r /sys/dev/block/$DEV_MAJOR:$2/uevent
                logerror "Can't read /sys/dev/block/$DEV_MAJOR:$2/uevent"
                shellout "Can't gather $1 partition $2 informations"

        . /sys/dev/block/$DEV_MAJOR:$2/uevent
        test "$DEVTYPE" != "partition" && shellout "/sys/dev/block/$DEV_MAJOR:$2 TYPE=$DEVTYPE is not a partition."

        # echo $(udevadm info --query=name --path=/sys/dev/block/$DEV_MAJOR:$2)
        echo "/dev/$DEVNAME"

This is part of a script that create partitions knowing block device name and partition number. there after I need too create a filesystem, but I can't assume that partition name is the block device name followed by partition number. Sometimes there is a letter p (driver dependent).

maybe udev knows that?


I think I came across a solution, but I'm unsure it's reliable, can someone confirm? I query dev symlinks and I take the 1st one. Are they all equivalent? Is taking the 1st one reliable? If yes, I think that the following line does the job right (getting device name for second partition of disk /dev/sda

readlink -f /dev/$(udevadm info -q symlink /dev/sda|cut -d' ' -f1)-part2

=> /dev/sda2

  • Unfortunately for me, this solution doesn't work in my situation as I'm in early boot in SystetmImager deployment code and udev is stopped during partitioning and raid creation in order to prevent raid auto recovery during lay work....So I need a solution based on /sys.... Jul 29 '21 at 17:46

Sometimes there is a letter p (driver dependent).

The p letter is always added when the disk ends with a digit. So if you have disk name and partition number you can always assume the partition name will be diskname + number or diskname + p + number. There is no need to check major/minor for that.

The hard part is the answer to "what is the third partition". Is it partition number three or third partition on the disk? sda1 can be the third partition on the disk by position.

If you are interested in partition positions on the disk, you need to use tool that understands the partition table so parted or fdisk. sfdisk has option for JSON output which can be easier to parse from a script.

If you simply want partition number three then you can simply add 3 (or p3, see above) to the disk name/path and you'll get the partition name/path.

  • I was speaking about partition number as understood by parted or fdisk or gdisk. Jul 29 '21 at 16:14
  • Also note that for some drivers, the partitions are located in a subdirectory (example: /dev/cciss/c0d0p2) (not /dev/c0d0p2) Jul 29 '21 at 16:18
  • Yes, but full disk path is /dev/cciss/c0d0 (see cciss man page) so the path + pX works here too. Jul 29 '21 at 16:36

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.