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/sd0abut there's no/dev/sd0device (there is a/dev/sd0c...)/dev/sda1and/dev/mmcblk0p1are Linux-specific anyway.lsblkis relatively recent though.lsblkalso 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.lsblk -dpno pkname $devnamepassed all my tests! Perhaps you should add an answer./dev/sd0cin that case. Do the answers below work for you?