Linux – Get block device parent

block-devicelinux

Given a block device, how can I get its "parent"?

A example would be a LUKS virtual decrypted device (/dev/mapper/decrypted), whose parent would be the encrypted block device (/dev/sdb)

Best Answer

For non-interactive work, this can be done using dmsetup deps, which lists the block device's dependencies. By default, it'll return the major:minor device numbers, but you can provide additional options to output a name:

dmsetup deps -o devname /dev/mapper/decrypted

For doing this interactively, the best way is just to use lsblk, which outputs a nicely formatted graph:

└─sda8                     8:8    0  43.9G  0 part  
  └─sda8_crypt           253:2    0  43.9G  0 crypt 
    ├─vgpersonal-home    253:3    0    83G  0 lvm