如何在 dracut 为 centOS 7 创建的 initramfs 映像中执行 tpm2_nvread?

如何在 dracut 为 centOS 7 创建的 initramfs 映像中执行 tpm2_nvread?

我想在启动时使用 TPM 2.0 加载解锁我的 LUKS 分区(根文件系统)。

keyscript=/path/to/script 我在我的/etc/crypttab文件中使用 a 没有成功,但是我使用我发现的方法取得了进展这里

我正在使用 dracut 构建初始 ram fs 映像。

因此/usr/lib/dracut/modules.d/90crypt,我对几个文件进行了修改(根据我链接的指南):

模块设置.sh

# gives me access to these binaries at boot time in initramfs
function install() {
    # existing code
    # ...
    inst /sbin/tpm2_nvread
    inst /bin/tail
    inst /bin/perl
    inst /sbin/resourcemgr
}

cryptroot-ask.sh

resourcemgr &
# yum is only at tpm2-tools 1.1.0, so I can't read keys to a file
# this is my solution to grab from tpm, and convert the spaced out hex to binary
function gettpmkeyfile() {
    key=`tpm2_nvread -x 0x1500001 -a 0x40000001 -s 32 -o 0 | tail -n 1`
    key=${key//[[:blank:]]/}
    key=`echo $key | /bin/perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie'`
    printf $key
}
gettpmkeyfile | cryptsetup luksOpen $device $luksname --key-file=-

/etc/dracut.conf

omit_dracutmodules+="systemd"
add_dracutmodules+="crypt"

我知道二进制文件已正确加载,并且我已使用 TPM 添加了密钥luksAddKey,并且在使用密码启动后,我在 shell 中的命令行上测试了我的功能。

我遇到的问题是tpm2_nvread抛出有关资源管理器无法初始化的错误(错误0x1)。

然而,我注意到在正常启动时,资源管理器也在这里失败,但这并不妨碍我使用命令tpm2-tools

我尝试从 elrepo (4.something) 升级到最新的内核,并且我添加了带有 dracut 的内核 drievrs,如下所示:

dracut --add-drivers tpm_crb --force

这似乎没有帮助。

关于如何tpm2_nvread在 initrd 中工作有什么建议吗?

答案1

我找到了解决办法!

我添加/sbin/strace到已安装的二进制文件中,module-setup.sh以便我可以手动检查tpm2_nvreaddracut shell 中的命令。结果错误是我的网络无法访问。

tpm2 命令用于libtcti与 tpm 进行通信,tpm 使用 处的套接字127.0.0.1:2323

现在,至于环回失败的原因,我不确定。我的猜测是90crypt在网络可用之前 dracut 运行,或者与我禁用systemd.

所以我添加/sbin/ifupmodule-setup.sh,并将其添加到我的cryptroot-ask.sh

ifup lo inet loopback
sleep 3

不确定我是否需要睡眠,但我还是睡了。

答案2

您还可以将“-T device”添加到 tpm2_nvread 中,以便它直接与 /dev/tpm0 对话,而无需通过资源管理器。

相关内容