From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by lists.proxmox.com (Postfix) with ESMTPS id B554A91391 for ; Thu, 6 Oct 2022 14:45:27 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 87AD91C68D for ; Thu, 6 Oct 2022 14:44:57 +0200 (CEST) Received: from lana.proxmox.com (unknown [94.136.29.99]) by firstgate.proxmox.com (Proxmox) with ESMTP for ; Thu, 6 Oct 2022 14:44:54 +0200 (CEST) Received: by lana.proxmox.com (Postfix, from userid 10043) id BE0E22C2327; Thu, 6 Oct 2022 14:44:48 +0200 (CEST) From: Stefan Hanreich To: pve-devel@lists.proxmox.com Date: Thu, 6 Oct 2022 14:44:42 +0200 Message-Id: <20221006124447.120701-4-s.hanreich@proxmox.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20221006124447.120701-1-s.hanreich@proxmox.com> References: <20221006124447.120701-1-s.hanreich@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.381 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery methods NO_DNS_FOR_FROM 0.001 Envelope sender has no MX or A DNS records RDNS_NONE 0.793 Delivered to internal network by a host with no rDNS SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_NONE 0.001 SPF: sender does not publish an SPF Record URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more information. [migrate.pm] Subject: [pve-devel] [PATCH v2 pve-container 2/2] add migration hooks to container migration process 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: , X-List-Received-Date: Thu, 06 Oct 2022 12:45:27 -0000 Using the pct commands implemented in the previous commit, this commit adds running the migration-hooks during the container migration process. I am redirecting STDERR from the pct mtunnel to /dev/null since it is not captured by the current fork_ssh_tunnel function and can then pollute the output of the migration task. The STDERR of the migrate-hookscript is not affected, since it is captured by the remote mtunnel command and gets transferred via the query-migrate-hook command. Signed-off-by: Stefan Hanreich --- src/PVE/LXC/Migrate.pm | 119 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) diff --git a/src/PVE/LXC/Migrate.pm b/src/PVE/LXC/Migrate.pm index 2ef1cce..fdde180 100644 --- a/src/PVE/LXC/Migrate.pm +++ b/src/PVE/LXC/Migrate.pm @@ -89,6 +89,8 @@ sub prepare { if !$target_scfg->{content}->{rootdir}; }); + $self->migration_hook($vmid, 'pre'); + # todo: test if VM uses local resources # test ssh connection @@ -384,6 +386,15 @@ sub phase3 { } } +# only called when phase1 was successful +sub phase3_cleanup { + my ($self, $vmid, $err) = @_; + + if (!$self->{errors}) { + $self->migration_hook($vmid, 'post'); + } +} + sub final_cleanup { my ($self, $vmid) = @_; @@ -413,7 +424,115 @@ sub final_cleanup { $self->cmd($cmd); } } +} + +sub fork_tunnel { + my ($self, $ssh_forward_info) = @_; + + my $cmd = ['/usr/sbin/pct', 'mtunnel', '2>', '/dev/null']; + my $log = sub { + my ($level, $msg) = @_; + $self->log($level, $msg); + }; + + return PVE::Tunnel::fork_ssh_tunnel($self->{rem_ssh}, $cmd, $ssh_forward_info, $log); +} + +sub migration_hook { + my ($self, $vmid, $phase) = @_; + + if (!$self->{vmconf}->{hookscript}) { + return; + } + + my $stop_on_error = $phase eq 'pre'; + + PVE::GuestHelpers::exec_hookscript( + $self->{vmconf}, + $vmid, + "$phase-migrate", + $stop_on_error, + ); + + my $tunnel; + + eval { + $tunnel = $self->{tunnel} // $self->fork_tunnel(); + }; + if ($@ =~ /can't open tunnel/) { + $self->log('warn', 'Target node does not support mtunnel. Not running hookscript on target, but still continuing with migration.'); + return; + } elsif ($@) { + die $@; + } + + my $close_tunnel = sub { + if (!$self->{tunnel}) { + eval { + $self->log('info', "closing tunnel for migration hook"); + PVE::Tunnel::finish_tunnel($tunnel); + }; + if ($@) { + $self->log('warn', 'could not close tunnel to remote host'); + } + } + }; + + my $result; + + eval { + $self->log('info', "starting hook $phase-migrate on target"); + + $result = PVE::Tunnel::write_tunnel($tunnel, 30, "migrate-hook", { + vmid => $vmid, + phase => $phase, + source => PVE::INotify::nodename(), + target => $self->{node}, + }); + }; + my $err = $@; + + if ($err) { + $close_tunnel->(); + die $err; + } + + $self->log('info', "successfully started hook $phase-migrate on target"); + + my $running = 1; + + while ($running) { + eval { + $result = PVE::Tunnel::write_tunnel($tunnel, 30, "query-migrate-hook"); + + if (!exists $result->{status}) { + die "Invalid response!"; + } elsif ($result->{status} eq 'running') { + sleep(5); + } elsif ($result->{status} eq 'finished') { + $self->log('info', "$phase-migrate hook ran successfully on target:\n$result->{output}"); + } elsif ($result->{status} eq 'error') { + my $msg = "An error occured during running the hookscript:\n" . $result->{output}; + + if ($stop_on_error) { + die $msg; + } else { + $self->log('warn', $msg) + } + } else { + die "Invalid response!"; + } + + $running = $result->{status} eq 'running'; + }; + if ($@) { + $err = $@; + last; + } + } + $close_tunnel->(); + die $err if $err; } 1; -- 2.30.2