mirror of
https://github.com/retspen/webvirtcloud
synced 2026-12-03 17:36:05 +00:00
feat(vmbackups): restore a backup's TPM state
The restore writes the backup's TPM state files into libvirt's swtpm directory of the new VM's UUID before the VM is defined; libvirt makes them swtpm's when the VM starts and keeps the state, so the guest keeps its TPM identity. A file name from a manifest that is not a plain name is refused before anything is made, a host without swtpm is refused, and recovery of a restore that did not define its VM removes the files.
This commit is contained in:
parent
52b6096efe
commit
da52a23ed8
10 changed files with 253 additions and 42 deletions
|
|
@ -258,7 +258,7 @@ def restore_definition(definition, *, name, uuid, macs, disks, nvram, connect):
|
|||
|
||||
def check_restore_host(conn, definition):
|
||||
"""Refuses a host that cannot run the backup's definition: its emulator,
|
||||
machine type and firmware, the networks, bridges and network filters
|
||||
machine type, firmware and TPM, the networks, bridges and network filters
|
||||
its NICs name."""
|
||||
tree = etree.fromstring(definition)
|
||||
emulator = tree.findtext("devices/emulator")
|
||||
|
|
@ -272,6 +272,13 @@ def check_restore_host(conn, definition):
|
|||
loader = (tree.findtext("os/loader") or "").strip()
|
||||
if loader and loader not in caps.xpath("os/loader/value/text()"):
|
||||
raise OperationError(gettext_noop("This host has no firmware %(loader)s"), loader=loader)
|
||||
if (tpm := tree.find("devices/tpm")) is not None:
|
||||
offered = "devices/tpm[@supported='yes']/enum[@name='{}']/value/text()"
|
||||
if ("emulator" not in caps.xpath(offered.format("backendModel"))
|
||||
or "2.0" not in caps.xpath(offered.format("backendVersion"))
|
||||
or tpm.get("model") not in caps.xpath(offered.format("model"))):
|
||||
raise OperationError(gettext_noop("This host offers no TPM 2.0 %(model)s: install swtpm on it"),
|
||||
model=tpm.get("model"))
|
||||
for network in sorted(set(tree.xpath("devices/interface[@type='network']/source/@network")) - set(conn.get_networks())):
|
||||
raise OperationError(gettext_noop("This host has no network %(name)s"), name=network)
|
||||
for bridge in sorted(set(tree.xpath("devices/interface[@type='bridge']/source/@bridge")) - set(conn.get_ifaces())):
|
||||
|
|
|
|||
|
|
@ -350,14 +350,14 @@ class RestoreHostTestCase(unittest.TestCase):
|
|||
"<domainCapabilities><os><loader><value>/usr/share/edk2/ovmf/OVMF_CODE.fd</value></loader></os></domainCapabilities>")
|
||||
return conn
|
||||
|
||||
def check(self, conn, *texts):
|
||||
def check(self, conn, *texts, definition=SOURCE):
|
||||
from vrtManager.backup import check_restore_host
|
||||
|
||||
if not texts:
|
||||
check_restore_host(conn, SOURCE)
|
||||
check_restore_host(conn, definition)
|
||||
return
|
||||
with self.assertRaises(util.OperationError) as caught:
|
||||
check_restore_host(conn, SOURCE)
|
||||
check_restore_host(conn, definition)
|
||||
for text in texts:
|
||||
self.assertIn(text, str(caught.exception))
|
||||
|
||||
|
|
@ -390,3 +390,25 @@ class RestoreHostTestCase(unittest.TestCase):
|
|||
conn = self.conn()
|
||||
conn.wvm.getDomainCapabilities.return_value = "<domainCapabilities><os><loader/></os></domainCapabilities>"
|
||||
self.check(conn, "OVMF_CODE.fd")
|
||||
|
||||
def test_a_tpm_needs_the_host_to_offer_its_model_with_swtpm(self):
|
||||
conn = self.conn()
|
||||
self.check(conn) # no TPM in the definition
|
||||
with_tpm = SOURCE.replace("<devices>", "<devices>" + TPM)
|
||||
self.check(conn, "TPM 2.0 tpm-crb", "swtpm", definition=with_tpm)
|
||||
|
||||
def offers(models, backends="<value>emulator</value>", versions="<value>2.0</value>"):
|
||||
conn.wvm.getDomainCapabilities.return_value = (
|
||||
"<domainCapabilities><os><loader><value>/usr/share/edk2/ovmf/OVMF_CODE.fd</value></loader></os>"
|
||||
f"<devices><tpm supported='yes'><enum name='model'>{models}</enum>"
|
||||
f"<enum name='backendModel'>{backends}</enum><enum name='backendVersion'>{versions}</enum>"
|
||||
"</tpm></devices></domainCapabilities>")
|
||||
|
||||
offers("<value>tpm-tis</value>")
|
||||
self.check(conn, "tpm-crb", definition=with_tpm)
|
||||
offers("<value>tpm-crb</value>", backends="<value>passthrough</value>")
|
||||
self.check(conn, "swtpm", definition=with_tpm)
|
||||
offers("<value>tpm-crb</value>", versions="<value>1.2</value>")
|
||||
self.check(conn, "swtpm", definition=with_tpm)
|
||||
offers("<value>tpm-tis</value><value>tpm-crb</value>")
|
||||
self.check(conn, definition=with_tpm)
|
||||
|
|
|
|||
92
vrtManager/tests/test_live_tpm_backup.py
Normal file
92
vrtManager/tests/test_live_tpm_backup.py
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
"""Live check that a shut-off VM's TPM state is read through libvirt, and
|
||||
that state written under another VM's UUID is the one that VM starts with:
|
||||
libvirt makes it swtpm's and does not manufacture a new TPM over it.
|
||||
|
||||
Runs only with TEST_LIBVIRT_HOST set (see vrtManager/tests/livetest.py), on
|
||||
a host with swtpm.
|
||||
"""
|
||||
|
||||
import os
|
||||
import unittest
|
||||
import uuid
|
||||
|
||||
import libvirt
|
||||
from django.test import SimpleTestCase
|
||||
from lxml import etree
|
||||
|
||||
from vrtManager.backup import wvmBackup
|
||||
from vrtManager.connection import connection_manager, wvmConnect
|
||||
from vrtManager.tests import livetest
|
||||
|
||||
TPM = "<tpm model='tpm-crb'><backend type='emulator' version='2.0'/></tpm>"
|
||||
|
||||
|
||||
class LiveTpmBackupTestCase(SimpleTestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
if not livetest.enabled():
|
||||
raise unittest.SkipTest("Set TEST_LIBVIRT_HOST to run the live libvirt tests")
|
||||
cls.args = (
|
||||
os.environ["TEST_LIBVIRT_HOST"],
|
||||
os.environ.get("TEST_LIBVIRT_LOGIN", ""),
|
||||
os.environ.get("TEST_LIBVIRT_PASSWORD", ""),
|
||||
int(os.environ.get("TEST_LIBVIRT_TYPE", 4)),
|
||||
)
|
||||
cls.conn = connection_manager.get_connection(*cls.args)
|
||||
if wvmConnect(*cls.args).get_tpm_model("x86_64", "q35") is None:
|
||||
raise unittest.SkipTest("The host has no swtpm")
|
||||
livetest.cleanup(cls.conn)
|
||||
cls.inventory = livetest.inventory(cls.conn)
|
||||
cls.pools = {p.name() for p in cls.conn.listAllStoragePools()}
|
||||
super().setUpClass()
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
livetest.cleanup(cls.conn)
|
||||
super().tearDownClass()
|
||||
if livetest.inventory(cls.conn) != cls.inventory:
|
||||
raise AssertionError("Live tests changed objects outside the test namespace")
|
||||
if {p.name() for p in cls.conn.listAllStoragePools()} != cls.pools:
|
||||
raise AssertionError("A storage pool was left behind")
|
||||
|
||||
def tearDown(self):
|
||||
# the TPM state goes with the VM
|
||||
for dom in self.conn.listAllDomains():
|
||||
if dom.name().startswith(livetest.PREFIX + "tpm"):
|
||||
if dom.isActive():
|
||||
dom.destroy()
|
||||
dom.undefineFlags(livetest.UNDEFINE_FLAGS | libvirt.VIR_DOMAIN_UNDEFINE_TPM)
|
||||
|
||||
def run_once(self, name):
|
||||
dom = livetest.define_vm(self.conn, name, [], extra_devices=TPM)
|
||||
dom.createWithFlags(libvirt.VIR_DOMAIN_START_PAUSED)
|
||||
dom.destroy()
|
||||
return dom
|
||||
|
||||
def test_the_state_is_kept_by_the_vm_it_is_restored_to(self):
|
||||
source = self.run_once(livetest.PREFIX + "tpm-a")
|
||||
proxy = wvmBackup(*self.args, source.name(), uuid=source.UUIDString())
|
||||
self.assertTrue(proxy.backup_plan()["tpm"])
|
||||
state = proxy.read_tpm()
|
||||
self.assertIn("tpm2-00.permall", state)
|
||||
self.assertNotIn(".lock", state)
|
||||
|
||||
restored = livetest.define_vm(self.conn, livetest.PREFIX + "tpm-b", [], extra_devices=TPM)
|
||||
host = wvmConnect(*self.args)
|
||||
with host._tpm_pool(restored.UUIDString(), build=True) as pool:
|
||||
for name, data in state.items():
|
||||
host.upload_volume(pool, name, data, owner=(0, 0), mode="0600")
|
||||
restored.createWithFlags(libvirt.VIR_DOMAIN_START_PAUSED)
|
||||
restored.destroy()
|
||||
|
||||
copy = wvmBackup(*self.args, restored.name(), uuid=restored.UUIDString())
|
||||
self.assertEqual(copy.read_tpm(), state)
|
||||
with host._tpm_pool(restored.UUIDString()) as pool:
|
||||
permissions = etree.fromstring(pool.storageVolLookupByName("tpm2-00.permall").XMLDesc(0))
|
||||
self.assertNotEqual(permissions.findtext("target/permissions/owner"), "0") # swtpm's now
|
||||
|
||||
def test_a_vm_that_never_started_has_no_state(self):
|
||||
dom = livetest.define_vm(self.conn, livetest.PREFIX + "tpm-c", [], extra_devices=TPM)
|
||||
self.assertEqual(wvmBackup(*self.args, dom.name(), uuid=dom.UUIDString()).read_tpm(), {})
|
||||
with wvmConnect(*self.args)._tpm_pool(str(uuid.uuid4())) as pool:
|
||||
self.assertIsNone(pool)
|
||||
Loading…
Add table
Add a link
Reference in a new issue