[pve-devel] [PATCH v2 qemu-server 4/9] ipam : add update/delete support

Alexandre Derumier aderumier at odiso.com
Mon Jul 12 00:47:02 CEST 2021


Signed-off-by: Alexandre Derumier <aderumier at odiso.com>
---
 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





More information about the pve-devel mailing list