I'm looking for a portable way to obtain parent block device name (e.g. /dev/sda
) given the partition device name (e.g. /dev/sda1
). I know I could just drop the last character, but that wouldn't work in some cases:
- MMC card readers typically have names like
/dev/mmcblk0
, while their partitions have names like/dev/mmcblk0p1
(notice the extrap
). - optional: some block devices don't have any partition table at all and are formatted as a single partition. In this case, partition device and parent block device are the same.
LVM volumes are a whole different kettle of fish. I don't need to support them right now, but if taking them into account requires little extra effort, I wouldn't mind.
/dev/sd0a
but there's no/dev/sd0
device (there is a/dev/sd0c
...) – thrig yesterday/dev/sda1
and/dev/mmcblk0p1
are Linux-specific anyway.lsblk
is relatively recent though. – Stéphane Chazelas yesterdaylsblk
also uses the device node numbers to find the correct one, instead of just using the name. seems the simplest solution for Linux and util-linux would likely be always available anyway. – ilkkachu yesterdaylsblk -dpno pkname $devname
passed all my tests! Perhaps you should add an answer. – Dmitry Grigoryev yesterday/dev/sd0c
in that case. Do the answers below work for you? – Dmitry Grigoryev 16 hours ago