1

Given a block partition (such as /dev/sdf3), I'd like to look up an open LUKS device on that partition. E.g.:

$ some_command /dev/sdf3
/dev/mapper/luks-01234567-89ab-cdef-0123-456789abcdef

Or if no LUKS device is open on that partition, it would return nothing, or perhaps an error return value.

What command could be used to achieve the above?

1
#!/bin/bash

dev_name="$1"

test -z "$dev_name" && exit 2
test -b "/dev/${dev_name}" || exit 2
test -d "/sys/block/${dev_name}" || exit 2

majmin=$(<"/sys/block/${dev_name}/dev")
cryptsetup isLuks "/dev/${dev_name}" ||
    exit 1 # not correct with external LUKS header
dmsetup table |
    awk -v dev="$majmin" '$4 == "crypt" && $8 == dev { name=$1;sub(":$","",name); print name }'
0

Could lsblk do the job? For instance:

$ lsblk /dev/sda2
NAME         MAJ:MIN RM  SIZE RO TYPE  MOUNTPOINT
sda2           8:2    0 96.9G  0 part  
└─sda2_crypt 253:0    0 96.9G  0 crypt /home

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.