From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 4195D1FF2CA for ; Tue, 23 Jul 2024 11:58:46 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 80632157; Tue, 23 Jul 2024 11:57:54 +0200 (CEST) From: Fiona Ebner To: pve-devel@lists.proxmox.com Date: Tue, 23 Jul 2024 11:56:18 +0200 Message-Id: <20240723095624.53621-18-f.ebner@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240723095624.53621-1-f.ebner@proxmox.com> References: <20240723095624.53621-1-f.ebner@proxmox.com> MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL -0.061 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [RFC qemu-server 17/23] backup: implement backup for external providers X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" The state of the VM's disk images at the time the backup is started is preserved via a snapshot-access block node. Old data is moved to the fleecing image when new guest writes come in. The snapshot-access block node, as well as the associated bitmap in case of incremental backup, will be exported via NBD to the external provider. The provider can indicate that it wants to do an incremental backup by returning the bitmap ID that was used for a previous backup and it will then be told if the bitmap was newly created (either first backup or old bitmap was invalid) or if the bitmap can be reused. The provider then reads the parts of the NBD image it needs, either the full disk for full backup, or the dirty parts according to the bitmap for incremental backup. The bitmap has to be respected, reads to other parts of the image will return an error. After backing up each part of the disk, it should be discarded in the export to avoid unnecessary space usage in the fleecing image (requires the storage underlying the fleecing image to support discard too). Signed-off-by: Fiona Ebner --- PVE/VZDump/QemuServer.pm | 176 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) diff --git a/PVE/VZDump/QemuServer.pm b/PVE/VZDump/QemuServer.pm index a138ebcf..7141d9e4 100644 --- a/PVE/VZDump/QemuServer.pm +++ b/PVE/VZDump/QemuServer.pm @@ -277,6 +277,8 @@ sub archive { if ($self->{vzdump}->{opts}->{pbs}) { $self->archive_pbs($task, $vmid); + } elsif ($self->{vzdump}->{'backup-provider'}) { + $self->archive_external($task, $vmid); } else { $self->archive_vma($task, $vmid, $filename, $comp); } @@ -1122,6 +1124,23 @@ sub cleanup { # If VM was started only for backup, it is already stopped now. if (PVE::QemuServer::Helpers::vm_running_locally($vmid)) { + if ($task->{cleanup}->{'nbd-stop'}) { + eval { PVE::QemuServer::QMPHelpers::nbd_stop($vmid); }; + $self->logerr($@) if $@; + } + + if (my $info = $task->{cleanup}->{'backup-access-teardown'}) { + my $params = { + 'target-id' => $info->{'target-id'}, + timeout => 60, + success => $info->{success} ? JSON::true : JSON::false, + }; + + $self->loginfo("tearing down backup-access"); + eval { mon_cmd($vmid, "backup-access-teardown", $params->%*) }; + $self->logerr($@) if $@; + } + $detach_tpmstate_drive->($task, $vmid); detach_fleecing_images($task->{disks}, $vmid) if $task->{'use-fleecing'}; } @@ -1133,4 +1152,161 @@ sub cleanup { } } +sub archive_external { + my ($self, $task, $vmid) = @_; + + my $config_file = "$task->{tmpdir}/qemu-server.conf"; + my $firewall_file = "$task->{tmpdir}/qemu-server.fw"; + + my $opts = $self->{vzdump}->{opts}; + + my $backup_provider = $self->{vzdump}->{'backup-provider'}; + + $self->loginfo("starting external backup via " . $backup_provider->provider_name()); + + $backup_provider->backup_guest_config($vmid, $config_file); + $backup_provider->backup_firewall_config($vmid, $firewall_file) if -e $firewall_file; + + my $starttime = time(); + + # get list early so we die on unkown drive types before doing anything + my $devlist = _get_task_devlist($task); + + $self->enforce_vm_running_for_backup($vmid); + $self->{qmeventd_fh} = PVE::QemuServer::register_qmeventd_handle($vmid); + + eval { + $SIG{INT} = $SIG{TERM} = $SIG{QUIT} = $SIG{HUP} = $SIG{PIPE} = sub { + die "interrupted by signal\n"; + }; + + my $qemu_support = mon_cmd($vmid, "query-proxmox-support"); + + $attach_tpmstate_drive->($self, $task, $vmid); + + my $is_template = PVE::QemuConfig->is_template($self->{vmlist}->{$vmid}); + + my $fleecing = check_and_prepare_fleecing( + $self, $vmid, $opts->{fleecing}, $task->{disks}, $is_template, $qemu_support, 1); + die "cannot setup backup access without fleecing\n" if !$fleecing; + + $task->{'use-fleecing'} = 1; + + my $fs_frozen = $self->qga_fs_freeze($task, $vmid); + + my $target_id = $opts->{storage}; + + my $params = { + 'target-id' => $target_id, + devlist => $devlist, + timeout => 60, + }; + + my ($mechanism, $bitmap_name) = $backup_provider->backup_get_mechanism($vmid, 'qemu'); + die "mechanism '$mechanism' requested by backup provider is not supported for VMs\n" + if $mechanism ne 'nbd'; + + if ($bitmap_name) { + # prepend storage ID so different providers can never cause clashes + $bitmap_name = "$opts->{storage}-" . $bitmap_name; + $params->{'bitmap-name'} = $bitmap_name; + } + + $self->loginfo("setting up snapshot-access for backup"); + + my $backup_access_info = eval { mon_cmd($vmid, "backup-access-setup", $params->%*) }; + my $qmperr = $@; + + $task->{cleanup}->{'backup-access-teardown'} = { 'target-id' => $target_id, success => 0 }; + + if ($fs_frozen) { + $self->qga_fs_thaw($vmid); + } + + die $qmperr if $qmperr; + + $self->resume_vm_after_job_start($task, $vmid); + + my $bitmap_info = mon_cmd($vmid, 'query-pbs-bitmap-info'); + for my $info (sort { $a->{drive} cmp $b->{drive} } $bitmap_info->@*) { + my $text = $bitmap_action_to_human->($self, $info); + my $drive = $info->{drive}; + $drive =~ s/^drive-//; # for consistency + $self->loginfo("$drive: dirty-bitmap status: $text"); + } + + $self->loginfo("starting NBD server"); + + my $nbd_path = "/run/qemu-server/$vmid\_nbd.backup_access"; + mon_cmd( + $vmid, "nbd-server-start", addr => { type => 'unix', data => { path => $nbd_path } } ); + $task->{cleanup}->{'nbd-stop'} = 1; + + for my $info ($backup_access_info->@*) { + $self->loginfo("adding NBD export for $info->{device}"); + + my $export_params = { + id => $info->{device}, + 'node-name' => $info->{'node-name'}, + writable => JSON::true, # for discard + type => "nbd", + name => $info->{device}, # NBD export name + }; + + if ($info->{'bitmap-name'}) { + $export_params->{bitmaps} = [{ + node => $info->{'bitmap-node-name'}, + name => $info->{'bitmap-name'}, + }], + } + + mon_cmd($vmid, "block-export-add", $export_params->%*); + } + + for my $info ($backup_access_info->@*) { + $self->loginfo("starting external backup for '$info->{device}'"); + + my $bitmap_status = 'none'; + my $bitmap_name; + if (my $bitmap_action = $info->{'bitmap-action'}) { + my $bitmap_action_to_status = { + 'not-used' => 'none', + 'not-used-removed' => 'none', + 'new' => 'new', + 'used' => 'reuse', + 'invalid' => 'new', + }; + + $bitmap_status = $bitmap_action_to_status->{$bitmap_action} + or die "got unexpected bitmap action '$bitmap_action'\n"; + + $bitmap_name = $info->{'bitmap-name'} or die "bitmap-name is not present\n"; + } + + $backup_provider->backup_nbd( + $vmid, + $info->{device}, + $nbd_path, + $bitmap_status, + $bitmap_name, + $opts->{bwlimit} * 1024, + ); + + $self->loginfo("backup for '$info->{device}' done"); + } + }; + my $err = $@; + + if ($err) { + $self->logerr($err); + $self->resume_vm_after_job_start($task, $vmid); + } else { + $task->{size} = $backup_provider->backup_get_task_size($vmid); + $task->{cleanup}->{'backup-access-teardown'}->{success} = 1; + } + $self->restore_vm_power_state($vmid); + + die $err if $err; +} + 1; -- 2.39.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel