backup: use blockdev for TPM state file

Signed-off-by: Fiona Ebner <f.ebner@proxmox.com>
This commit is contained in:
Fiona Ebner 2025-07-02 18:27:59 +02:00 committed by Fabian Grünbichler
parent f92c1fa0f3
commit 439f6e2a1d
2 changed files with 34 additions and 10 deletions

View file

@ -14,6 +14,18 @@ use PVE::Storage;
use PVE::QemuServer::Drive qw(drive_is_cdrom);
use PVE::QemuServer::Monitor qw(mon_cmd);
my sub tpm_backup_node_name {
my ($type, $drive_id) = @_;
if ($type eq 'fmt') {
return "drive-$drive_id-backup"; # this is the top node
} elsif ($type eq 'file') {
return "$drive_id-backup-file"; # drop the "drive-" prefix to be sure, max length is 31
}
die "unknown node type '$type' for TPM backup node";
}
my sub fleecing_node_name {
my ($type, $drive_id) = @_;
@ -36,6 +48,7 @@ my sub get_node_name {
my ($type, $drive_id, $volid, $options) = @_;
return fleecing_node_name($type, $drive_id) if $options->{fleecing};
return tpm_backup_node_name($type, $drive_id) if $options->{'tpm-backup'};
my $snap = $options->{'snapshot-name'};
@ -258,7 +271,8 @@ sub generate_drive_blockdev {
my $child = generate_file_blockdev($storecfg, $drive, $options);
$child = generate_format_blockdev($storecfg, $drive, $child, $options);
return $child if $options->{fleecing}; # for fleecing, this is already the top node
# for fleecing and TPM backup, this is already the top node
return $child if $options->{fleecing} || $options->{'tpm-backup'};
# this is the top filter entry point, use $drive-drive_id as nodename
return {
@ -315,6 +329,9 @@ actual size of the image. The image format must be C<raw>.
=item C<< $options->{'snapshot-name'} >>: Attach this snapshot of the volume C<< $drive->{file} >>,
rather than the volume itself.
=item C<< $options->{'tpm-backup'} >>: Generate and attach a block device for backing up the TPM
state image.
=back
=back
@ -411,6 +428,12 @@ sub detach {
return;
}
sub detach_tpm_backup_node {
my ($vmid) = @_;
detach($vmid, "drive-tpmstate0-backup");
}
sub detach_fleecing_block_nodes {
my ($vmid, $log_func) = @_;

View file

@ -158,7 +158,7 @@ sub prepare {
if ($ds eq 'tpmstate0') {
# TPM drive only exists for backup, which is reflected in the name
$diskinfo->{qmdevice} = 'drive-tpmstate0-backup';
$task->{tpmpath} = $path;
$task->{'tpm-volid'} = $volid;
}
if (-b $path) {
@ -474,24 +474,25 @@ my $query_backup_status_loop = sub {
my $attach_tpmstate_drive = sub {
my ($self, $task, $vmid) = @_;
return if !$task->{tpmpath};
return if !$task->{'tpm-volid'};
# unconditionally try to remove the tpmstate-named drive - it only exists
# for backing up, and avoids errors if left over from some previous event
eval { PVE::QemuServer::qemu_drivedel($vmid, "tpmstate0-backup"); };
eval { PVE::QemuServer::Blockdev::detach_tpm_backup_node($vmid); };
$self->loginfo('attaching TPM drive to QEMU for backup');
my $drive = "file=$task->{tpmpath},if=none,read-only=on,id=drive-tpmstate0-backup";
$drive =~ s/\\/\\\\/g;
my $ret = PVE::QemuServer::Monitor::hmp_cmd($vmid, "drive_add auto \"$drive\"", 60);
die "attaching TPM drive failed - $ret\n" if $ret !~ m/OK/s;
my $drive = { file => $task->{'tpm-volid'}, interface => 'tpmstate', index => 0 };
my $extra_options = { 'tpm-backup' => 1, 'read-only' => 1 };
PVE::QemuServer::Blockdev::attach($self->{storecfg}, $vmid, $drive, $extra_options);
};
my $detach_tpmstate_drive = sub {
my ($task, $vmid) = @_;
return if !$task->{tpmpath} || !PVE::QemuServer::check_running($vmid);
eval { PVE::QemuServer::qemu_drivedel($vmid, "tpmstate0-backup"); };
return if !$task->{'tpm-volid'} || !PVE::QemuServer::Helpers::vm_running_locally($vmid);
eval { PVE::QemuServer::Blockdev::detach_tpm_backup_node($vmid); };
};
my sub add_backup_performance_options {