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 7BCF575298 for ; Mon, 12 Jul 2021 00:47:50 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id DBDAAEBBE for ; Mon, 12 Jul 2021 00:47:18 +0200 (CEST) Received: from kvmformation3.odiso.net (globalOdiso.M6Lille.odiso.net [89.248.211.242]) by firstgate.proxmox.com (Proxmox) with ESMTP id 1BE76EB10 for ; Mon, 12 Jul 2021 00:47:14 +0200 (CEST) Received: by kvmformation3.odiso.net (Postfix, from userid 0) id BB566A1B; Mon, 12 Jul 2021 00:47:08 +0200 (CEST) From: Alexandre Derumier To: pve-devel@lists.proxmox.com Date: Mon, 12 Jul 2021 00:47:02 +0200 Message-Id: <20210711224707.63089-5-aderumier@odiso.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210711224707.63089-1-aderumier@odiso.com> References: <20210711224707.63089-1-aderumier@odiso.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 1.049 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% HEADER_FROM_DIFFERENT_DOMAINS 0.001 From and EnvelopeFrom 2nd level mail domains are different 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 KHOP_HELO_FCRDNS 0.399 Relay HELO differs from its IP's reverse DNS NO_DNS_FOR_FROM 0.001 Envelope sender has no MX or A DNS records 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 Subject: [pve-devel] [PATCH v2 qemu-server 4/9] ipam : add update/delete support 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: Sun, 11 Jul 2021 22:47:50 -0000 Signed-off-by: Alexandre Derumier --- PVE/API2/Qemu.pm | 12 +++- PVE/QemuServer.pm | 157 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 168 insertions(+), 1 deletion(-) diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm index f2557e3..6bf88b7 100644 --- a/PVE/API2/Qemu.pm +++ b/PVE/API2/Qemu.pm @@ -1255,6 +1255,7 @@ my $update_vm_api = sub { # write updates to pending section my $modified = {}; # record what $option we modify + my $errors = {}; my @bootorder; if (my $boot = $conf->{boot}) { @@ -1371,6 +1372,16 @@ my $update_vm_api = sub { die "only root can modify '$opt' config for real devices\n"; } $conf->{pending}->{$opt} = $param->{$opt}; + } elsif ($opt =~ m/^net(\d+)/) { + my $net = PVE::QemuServer::parse_net($param->{$opt}); + PVE::QemuServer::vmconfig_delete_pendingnet_ip($conf, $vmid, $opt, $net); + eval { + PVE::QemuServer::vmconfig_allocate_pending_ip($conf, $vmid, $opt, $net); + }; + if($@){ + $errors->{$opt} = $@; + } + } else { $conf->{pending}->{$opt} = $param->{$opt}; @@ -1409,7 +1420,6 @@ my $update_vm_api = sub { $conf = PVE::QemuConfig->load_config($vmid); # update/reload - my $errors = {}; if ($running) { PVE::QemuServer::vmconfig_hotplug_pending($vmid, $conf, $storecfg, $modified, $errors); } else { diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm index 18dd1ed..7d34a74 100644 --- a/PVE/QemuServer.pm +++ b/PVE/QemuServer.pm @@ -4659,6 +4659,8 @@ sub vmconfig_hotplug_pending { } elsif ($opt =~ m/^net(\d+)$/) { die "skip\n" if !$hotplug_features->{network}; vm_deviceunplug($vmid, $conf, $opt); + my $net = PVE::QemuServer::parse_net($conf->{$opt}); + vmconfig_delete_net_ip($conf, $net); } elsif (is_valid_drivename($opt)) { die "skip\n" if !$hotplug_features->{disk} || $opt =~ m/(ide|sata)(\d+)/; vm_deviceunplug($vmid, $conf, $opt); @@ -4745,9 +4747,12 @@ sub vmconfig_hotplug_pending { mon_cmd($vmid, "balloon", value => $balloon*1024*1024); } } elsif ($opt =~ m/^net(\d+)$/) { + die "skip" if $errors->{$opt}; # some changes can be done without hotplug vmconfig_update_net($storecfg, $conf, $hotplug_features->{network}, $vmid, $opt, $value, $arch, $machine_type); + + vmconfig_update_net_ip($conf, $vmid, $opt); } elsif (is_valid_drivename($opt)) { die "skip\n" if $opt eq 'efidisk0'; # some changes can be done without hotplug @@ -4842,6 +4847,10 @@ sub vmconfig_apply_pending { } elsif (defined($conf->{$opt}) && is_valid_drivename($opt)) { vmconfig_delete_or_detach_drive($vmid, $storecfg, $conf, $opt, $force); } + if($opt =~ m/net(\d+)$/) { + my $net = PVE::QemuServer::parse_net($conf->{$opt}); + vmconfig_delete_net_ip($conf, $net); + } }; if (my $err = $@) { $add_apply_error->($opt, $err); @@ -4859,6 +4868,11 @@ sub vmconfig_apply_pending { if (defined($conf->{$opt}) && is_valid_drivename($opt)) { vmconfig_register_unused_drive($storecfg, $vmid, $conf, parse_drive($opt, $conf->{$opt})) } + + if($opt =~ m/net(\d+)$/) { + die "skip" if $errors->{$opt}; + vmconfig_update_net_ip($conf, $vmid, $opt); + } }; if (my $err = $@) { $add_apply_error->($opt, $err); @@ -4871,6 +4885,149 @@ sub vmconfig_apply_pending { PVE::QemuConfig->write_config($vmid, $conf); } +sub vmconfig_update_net_ip { + my ($conf, $vmid, $opt) = @_; + + my $ipconfigid = undef; + if($opt =~ m/net(\d+)$/) { + $ipconfigid = "ipconfig$1"; + } + + my $nets = PVE::QemuServer::get_nets_ipconfig($conf); + my $net = PVE::QemuServer::parse_net($conf->{pending}->{$opt}); + my $oldnet = PVE::QemuServer::parse_net($conf->{$opt}) if $conf->{$opt}; + my $hostname = $conf->{name}; + + #delete current ip from ipam if different and not referenced in snapshots + eval { + + if(PVE::QemuServer::Ipam::ip_has_changed(4, $net, $oldnet) && !PVE::QemuServer::Ipam::is_ip_referenced(4, $nets, $oldnet)) { + PVE::QemuServer::Ipam::delete_net_ip(4, $oldnet, $hostname); + } + + if(PVE::QemuServer::Ipam::ip_has_changed(6, $net, $oldnet) && !PVE::QemuServer::Ipam::is_ip_referenced(6, $nets, $oldnet)) { + PVE::QemuServer::Ipam::delete_net_ip(6, $oldnet, $hostname); + } + }; + + #update pending ip info (dns,..) + PVE::QemuServer::Ipam::update_net_ip(4,$net, $hostname, $hostname, "vm:$vmid net:$opt"); + PVE::QemuServer::Ipam::update_net_ip(6,$net, $hostname, $hostname, "vm:$vmid net:$opt"); + + #set ip info as pending in cloud-init config + my $ipconfig = {}; + $ipconfig->{ip} = $net->{ip} if $net->{ip}; + $ipconfig->{ip6} = $net->{ip6} if $net->{ip6}; + $ipconfig->{gw} = $net->{gw} if $net->{gw}; + $ipconfig->{gw6} = $net->{gw6} if $net->{gw6}; + my $value = PVE::QemuServer::print_ipconfig($ipconfig); + $conf->{pending}->{$ipconfigid} = $value; + +} + +sub vmconfig_delete_net_ip { + my ($conf, $net, $snapname, $checkcurrent) = @_; + + my $hostname = $conf->{name}; + + return if !$net; + my $nets = PVE::QemuServer::get_nets_ipconfig($conf); + + eval { + if ($net->{ip} && !PVE::QemuServer::Ipam::is_ip_referenced(4, $nets, $net, $snapname, $checkcurrent)) { + PVE::QemuServer::Ipam::delete_net_ip(4, $net, $hostname); + } + if ($net->{ip6} && !PVE::QemuServer::Ipam::is_ip_referenced(6, $nets, $net, $snapname, $checkcurrent)) { + PVE::QemuServer::Ipam::delete_net_ip(6, $net, $hostname); + } + }; +} + +sub get_nets_ipconfig { + my ($conf) = @_; + + my $nets = {}; + + foreach my $opt (keys %{$conf}) { + next if $opt !~ m/^net(\d+)$/; + my $net = PVE::QemuServer::parse_net($conf->{$opt}); + $nets->{current}->{$opt} = $net; + } + + foreach my $snapname (keys %{$conf->{snapshots}}) { + my $snapconf = $conf->{snapshots}->{$snapname}; + foreach my $opt (keys %{$snapconf}) { + next if $opt !~ m/^net(\d+)$/; + my $net = PVE::QemuServer::parse_net($snapconf->{$opt}); + $nets->{snapshots}->{$snapname}->{$opt} = $net; + } + } + return $nets; +} + +sub vmconfig_allocate_pending_ip { + my ($conf, $vmid, $opt, $pendingnet) = @_; + + my $oldnet = PVE::QemuServer::parse_net($conf->{$opt}) if $conf->{$opt}; + my $nets = PVE::QemuServer::get_nets_ipconfig($conf); + my $hostname = $conf->{pending}->{name} ? $conf->{pending}->{name} : $conf->{name}; + my $description = "vm:$vmid net:$opt pending:1"; + my $skipdns = 1; + + my $errors = ""; + eval { + #add new ip to ipam if different than current and no reference in snapshots + if (PVE::QemuServer::Ipam::ip_has_changed(4, $pendingnet, $oldnet) && !PVE::QemuServer::Ipam::is_ip_referenced(4, $nets, $pendingnet)) { + PVE::QemuServer::Ipam::add_net_ip(4,$pendingnet, $hostname, $description, $skipdns); + } + PVE::QemuServer::Ipam::update_net_gateway(4, $pendingnet); + }; + + if($@) { + $errors = $@; + delete $pendingnet->{ip}; + delete $pendingnet->{gw}; + } + + eval { + #add new ip to ipam if different than current and no reference in snapshots + if (PVE::QemuServer::Ipam::ip_has_changed(6, $pendingnet, $oldnet) && !PVE::QemuServer::Ipam::is_ip_referenced(6, $nets, $pendingnet)) { + PVE::QemuServer::Ipam::add_net_ip(6,$pendingnet, $hostname, $description, $skipdns); + } + PVE::QemuServer::Ipam::update_net_gateway(6, $pendingnet); + }; + if($@) { + $errors = $@; + delete $pendingnet->{ip6}; + delete $pendingnet->{gw6}; + } + $conf->{pending}->{$opt} = PVE::QemuServer::print_net($pendingnet); + + die $errors if $errors; +} + +sub vmconfig_delete_pendingnet_ip { + my ($conf, $vmid, $opt, $net) = @_; + my $pendingnet = PVE::QemuServer::parse_net($conf->{pending}->{$opt}) if $conf->{pending}->{$opt}; + + return if !$pendingnet; + + my $nets = PVE::QemuServer::get_nets_ipconfig($conf); + my $hostname = $conf->{name}; + my $skipdns = 1; + my $skipsnap = undef; + my $checkcurrent = 1; + my $checkpending = 1; + + #delete old pending ips if different and no ip reference in running config or snapshots + if (PVE::QemuServer::Ipam::ip_has_changed(4, $net, $pendingnet) && !PVE::QemuServer::Ipam::is_ip_referenced(4, $nets, $pendingnet, $skipsnap, $checkcurrent)) { + PVE::QemuServer::Ipam::delete_net_ip(4, $pendingnet, $hostname, $skipdns); + } + if (PVE::QemuServer::Ipam::ip_has_changed(6, $net, $pendingnet) && !PVE::QemuServer::Ipam::is_ip_referenced(6, $nets, $pendingnet, $skipsnap, $checkcurrent)) { + PVE::QemuServer::Ipam::delete_net_ip(6, $pendingnet, $hostname, $skipdns); + } +} + sub vmconfig_update_net { my ($storecfg, $conf, $hotplug, $vmid, $opt, $value, $arch, $machine_type) = @_; -- 2.30.2