1 import ./make-test-python.nix ({ lib, pkgs, ... }: {
2 name = "systemd-initrd-luks-tpm2";
4 nodes.machine = { pkgs, ... }: {
7 emptyDiskImages = [ 512 ];
10 qemu.options = ["-chardev socket,id=chrtpm,path=/tmp/mytpm1/swtpm-sock -tpmdev emulator,id=tpm0,chardev=chrtpm -device tpm-tis,tpmdev=tpm0"];
12 boot.loader.systemd-boot.enable = true;
14 boot.initrd.availableKernelModules = [ "tpm_tis" ];
16 environment.systemPackages = with pkgs; [ cryptsetup ];
17 boot.initrd.systemd = {
21 specialisation.boot-luks.configuration = {
22 boot.initrd.luks.devices = lib.mkVMOverride {
25 crypttabExtraOpts = [ "tpm2-device=auto" ];
28 virtualisation.bootDevice = "/dev/mapper/cryptroot";
40 os.mkdir("/tmp/mytpm1")
44 self.proc = subprocess.Popen(["${pkgs.swtpm}/bin/swtpm", "socket", "--tpmstate", "dir=/tmp/mytpm1", "--ctrl", "type=unixio,path=/tmp/mytpm1/swtpm-sock", "--log", "level=20", "--tpm2"])
46 def wait_for_death_then_restart(self):
47 while self.proc.poll() is None:
48 print("waiting for tpm to die")
50 assert self.proc.returncode == 0
56 # Create encrypted volume
57 machine.wait_for_unit("multi-user.target")
58 machine.succeed("echo -n supersecret | cryptsetup luksFormat -q --iter-time=1 /dev/vdc -")
59 machine.succeed("PASSWORD=supersecret SYSTEMD_LOG_LEVEL=debug systemd-cryptenroll --tpm2-pcrs= --tpm2-device=auto /dev/vdc |& systemd-cat")
61 # Boot from the encrypted disk
62 machine.succeed("bootctl set-default nixos-generation-1-specialisation-boot-luks.conf")
63 machine.succeed("sync")
66 tpm.wait_for_death_then_restart()
68 # Boot and decrypt the disk
69 machine.wait_for_unit("multi-user.target")
70 assert "/dev/mapper/cryptroot on / type ext4" in machine.succeed("mount")