给定一个块分区(例如/dev/sdf3
),我想查找该分区上打开的 LUKS 设备。例如:
$ some_command /dev/sdf3
/dev/mapper/luks-01234567-89ab-cdef-0123-456789abcdef
或者,如果该分区上没有打开 LUKS 设备,则它不会返回任何内容,或者可能返回错误返回值。
可以使用什么命令来实现上述目的?
答案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 }'
答案2
能lsblk
胜任这份工作吗?例如:
$ 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