![给定一个块分区,查找打开的 LUKS 设备](https://linux22.com/image/120454/%E7%BB%99%E5%AE%9A%E4%B8%80%E4%B8%AA%E5%9D%97%E5%88%86%E5%8C%BA%EF%BC%8C%E6%9F%A5%E6%89%BE%E6%89%93%E5%BC%80%E7%9A%84%20LUKS%20%E8%AE%BE%E5%A4%87.png)
给定一个块分区(例如/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