将只读 LUKS 分区重新映射为读写

将只读 LUKS 分区重新映射为读写

cryptsetup 可以使用--readonly-r选项调用,这将设置一个只读映射:

cryptsetup --readonly luksOpen /dev/sdb1 sdb1

将设备打开为只读后,我可以稍后将其重新映射为读写吗?显然,我的意思是将其映射为读写,而不先关闭它,然后再次打开它。我可以重新映射它而无需再次输入密码吗?

如果这是不可能的,这只是 cryptsetup 不支持这一点,还是有一些更基本的级别?

答案1

用命令好像不行cryptsetup。不幸的cryptsetup是有一些这样的不可变标志......--allow-discards也是其中之一。如果您在打开容器时未设置此项,则以后无法添加它。

至少,不是用cryptsetup命令。但是,由于cryptsetup创建了常规设备映射器目标,因此您可以对其dmsetup进行修改。当然,出于各种原因,不建议这样做:这就像更改正在使用的分区的分区表 - 如果搞砸了,您可能会丢失所有数据。

设备映射器允许在运行时动态重新映射所有设备,并且它根本不关心数据的安全性;这就是为什么此功能通常包含在 LVM 层后面,LVM 层保留必要的元数据以确保其安全。

创建只读 LUKS 设备:

# truncate -s 100M foobar.img
# cryptsetup luksFormat foobar.img
# cryptsetup luksOpen --read-only foobar.img foobar

方式dmsetup看到它:

# dmsetup info foobar
Name:              foobar
State:             ACTIVE (READ-ONLY)
Read Ahead:        256
Tables present:    LIVE
[...]
# dmsetup table --showkeys foobar
0 200704 crypt aes-xts-plain64 ef434503c1874d65d33b1c23a088bdbbf52cb76c7f7771a23ce475f8823f47df 0 7:0 4096

请注意通常不应泄​​露的主密钥,因为它会破坏 LUKS 提供的任何强力保护。不幸的是,我还没有找到一种不使用它的方法,因为dmsetup也缺乏直接的--make-this-read-write选项。但是dmsetup reload允许完全替换映射,因此我们将在读写模式下将其替换为自身。

# dmsetup table --showkeys foobar | dmsetup reload foobar
# dmsetup info foobar
Name:              foobar
State:             ACTIVE (READ-ONLY)
Read Ahead:        256
Tables present:    LIVE & INACTIVE

重新加载后它仍然是只读的,因为重新加载会进入非活动表。

要使非活动表处于活动状态,请使用dmsetup resume

# dmsetup resume foobar
# dmsetup info foobar
Name:              foobar
State:             ACTIVE
Read Ahead:        256
Tables present:    LIVE

这样我们就有了一个可读写的 LUKS 设备。

它可以与实时文件系统一起使用吗?

# cryptsetup luksOpen --readonly foobar.img foobar
# mount /dev/mapper/foobar /mnt/foobar
mount: /mnt/foobar: WARNING: device write-protected, mounted read-only.
# mount -o remount,rw /mnt/foobar
mount: /mnt/foobar: cannot remount /dev/mapper/foobar read-write, is write-protected.

所以它是只读的。使其可读写并重新挂载:

# dmsetup table --showkeys foobar | dmsetup reload foobar
# dmsetup resume foobar
# mount -o remount,rw /mnt/foobar
# echo hey it works > /mnt/foobar/amazing.txt

我们可以回到只读状态吗?

# mount -o remount,ro /mnt/foobar
# dmsetup table --showkeys foobar | dmsetup reload foobar --readonly
# dmsetup resume foobar
# mount -o remount,rw /mnt/foobar
mount: /mnt/foobar: cannot remount /dev/mapper/foobar read-write, is write-protected.

所以它可能有效。将标志添加到现有 crypt 映射的过程allow_discards类似 - 您必须重新加载包含此标志的表。然而,已经检测到不存在丢弃支持的文件系统可能不会被说服动态地重新检测到这一点。所以目前还不清楚它的实用性如何。


不过,除非您有充分的理由不这样做,否则您应该坚持使用常规命令重新打开cryptsetup,即使这意味着卸载并重新提供密码。它更安全,更重要的是,不会规避 LUKS 安全概念。

答案2

打开卷后无法从只读更改为读写。我在 cryptsetup 源代码中没有看到任何选项可以做到这一点。

相关内容