diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index c38d8f2d2..f28adb365 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -98,6 +98,7 @@ jobs: - "storage-vm lvm-thin" - "storage-vm zfs" - storage-volumes-vm + - tpm-vm - vm-nesting exclude: - test: efi-vars-editor-vm # not compatible with 5.0/* diff --git a/tests/tpm-vm b/tests/tpm-vm new file mode 100644 index 000000000..20ab44eb6 --- /dev/null +++ b/tests/tpm-vm @@ -0,0 +1,54 @@ +#!/bin/sh +set -eu + +# Install LXD +install_lxd + +# Configure LXD +lxd init --auto + +IMAGE="${TEST_IMG:-ubuntu-minimal-daily:24.04}" +vmName="test-vm" + +# Launch test instance +lxc init "${IMAGE}" "${vmName}" --vm + +echo "==> Try starting a VM with two TPM devices" +lxc config device add "${vmName}" tpm1 tpm +lxc config device add "${vmName}" tpm2 tpm +! lxc start "${vmName}" || false + +echo "==> Starting VM with TPM" +lxc config device remove "${vmName}" tpm2 +lxc start "${vmName}" +waitInstanceReady "${vmName}" + +echo "==> Check if TPM files are present" +lxc exec "${vmName}" -- stat /dev/tpm0 +lxc exec "${vmName}" -- stat /dev/tpmrm0 + +echo "==> Try removing TPM from a running VM" +! lxc config device remove "${vmName}" tpm1 || false +lxc exec "${vmName}" -- stat /dev/tpm0 +lxc exec "${vmName}" -- stat /dev/tpmrm0 + +echo "==> Stopping VM and removing TPM" +lxc stop "${vmName}" --force +lxc config device remove "${vmName}" tpm1 + +echo "==> Check if TPM was indeed removed" +lxc start "${vmName}" +waitInstanceReady "${vmName}" +! lxc exec "${vmName}" -- stat /dev/tpm0 || false +! lxc exec "${vmName}" -- stat /dev/tpmrm0 || false +lxc stop "${vmName}" --force + +# TPM names are included on the swtpm socket path and long socket paths can cause problems if not handled correctly. +echo "==> Test handling TPMs with long names" +longName="tpm-device-with-long-name-for-testing" +lxc config device add "${vmName}" "${longName}" tpm +lxc start "${vmName}" +waitInstanceReady "${vmName}" +lxc exec "${vmName}" -- stat /dev/tpm0 +lxc exec "${vmName}" -- stat /dev/tpmrm0 +lxc delete "${vmName}" --force