17

If I know that a partition is for example /dev/sda1 how can I get the disk name (/dev/sda in this case) that contains the partition ?

  • The output should be only a path to disk (like /dev/sda).
  • It shouldn't require string manipulation, because I need it to work for different disk types.
1

3 Answers 3

23

You can observe in /sys the block device for a given partition name. For example, /dev/sda1:

$ ls -l /sys/class/block/sda1
lrwxrwxrwx 1 root root /sys/class/block/sda1 -> \
 ../../devices/pci0000:00/.../ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1

A script to take arg /dev/sda1 and print /dev/sda is:

part=$1
part=${part#/dev/}
disk=$(readlink /sys/class/block/$part)
disk=${disk%/*}
disk=/dev/${disk##*/}
echo $disk

I don't have lvm etc to try out, but there is probably some similar path.


There is also lsblk:

$ lsblk -as /dev/sde1
NAME  MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
sde1    8:65   1  7.4G  0 part 
`-sde   8:64   1  7.4G  0 disk 

and as @don_crissti said you can get the parent directly by using -o pkname to get just the name column, -n to remove the header, and -d to not include holder devices or slaves:

lsblk -ndo pkname /dev/sda1
6
  • 12
    No need for a script to do that, to print the just the parent device run: lsblk -no pkname /dev/sda1 Commented Aug 30, 2015 at 16:39
  • @don_crissti Thanks! I was reading help and man pages of every single unix utility that can manage partitions or list them, i cant belive that i didnt saw "PKNAME internal parent kernel device name" in lsblk. Commented Aug 30, 2015 at 16:50
  • 1
    pkname is not in my man page, but in the output of lsblk -h, so it's easy to miss!
    – meuh
    Commented Aug 30, 2015 at 17:03
  • meuh, it is not present in any man page version, the man page clearly says (twice): Use lsblk --help to get a list of all available/supported columns. Commented Aug 30, 2015 at 17:19
  • 1
    On my Debian Wheezy I get the error message: lsblk: unknown column: pkname
    – Marco
    Commented Aug 30, 2015 at 18:30
2

It's work only with UTF-8 locale. lvm, zfs, raid tested ok.

parent_tree_disk() {
  lsblk | awk '/^[A-Za-z]/{d0=$1; print d0};/^[└─├─]/{d1=$1; print d0, d1};/^  [└─├─]/{d2=$1; print d0, d1, d2}' | sed 's/[├─└─]//g'
}

alias pd='parent_tree_disk'

shell command:# pd
NAME
sda
sda sda1
sda sda2
sda sda2 cl-root
sda sda2 cl-swap

shell command:# pd | awk '/sda2/{print $1}'
sda

And you can use other filter on pd list output, like sort, uniq...

1
  • seems to miss some of the items $ lsblk -no NAME sda ├─sda1 ├─sda2 ├─sda3 │ └─luks-20ed40f5-be3e-4b02-9373-3c2f8c620ffe ├─sda4 │ └─luks-5fe2e868-ee82-4f02-8611-52368db5df86 ├─sda5 │ └─luks-d4a78f37-2228-49e0-a841-612c4ba70a78 ├─sda6 │ └─luks-8e91e1ae-21f3-42de-b317-cff1da21a015 ├─sda7 │ └─luks-85259675-1f76-4cbf-9a3e-e96c3c3bd39d ├─sda8 │ └─luks-51f5e5c3-439f-4169-85f8-26a50836068b └─sda9 └─luks-7bbcf4e3-2f11-4a4a-84d9-26f9a97c9c2a sdb └─luks-b4abb505-4480-4b43-a4fc-ea376d8737da sdc └─sdc1 sdd ├─sdd1 └─sdd2 Then your output only does 3 items for sda9
    – RDT2
    Commented Sep 21, 2020 at 20:29
0

Perhaps not beautiful:

for d in `fdisk -l 2>/dev/null | grep "^Disk " | cut -d":" -f1 | cut -f2`
do
  if [ `fdisk -l $d 2>/dev/null | grep -c "/dev/sda1"` -gt 0 ]
  then
    echo On disk $d
  fi
done

It works only for 'real' disks not for LVM.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .