From: Alexandre Derumier <aderumier@odiso.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH v2 qemu-server 4/9] ipam : add update/delete support
Date: Mon, 12 Jul 2021 00:47:02 +0200 [thread overview]
Message-ID: <20210711224707.63089-5-aderumier@odiso.com> (raw)
In-Reply-To: <20210711224707.63089-1-aderumier@odiso.com>
Signed-off-by: Alexandre Derumier <aderumier@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
next prev parent reply other threads:[~2021-07-11 22:47 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-07-11 22:46 [pve-devel] [PATCH v2 qemu-server 0/9] RFC: sdn: add ipam support Alexandre Derumier
2021-07-11 22:46 ` [pve-devel] [PATCH v2 qemu-server 1/9] add ipam module Alexandre Derumier
2021-07-11 22:47 ` [pve-devel] [PATCH v2 qemu-server 2/9] add print_ipconfig Alexandre Derumier
2021-07-11 22:47 ` [pve-devel] [PATCH v2 qemu-server 3/9] add ip options to netdescr Alexandre Derumier
2021-07-11 22:47 ` Alexandre Derumier [this message]
2021-07-11 22:47 ` [pve-devel] [PATCH v2 qemu-server 5/9] ipam : add revert ip support Alexandre Derumier
2021-07-11 22:47 ` [pve-devel] [PATCH v2 qemu-server 6/9] ipam : add snapshot rollback support Alexandre Derumier
2021-07-11 22:47 ` [pve-devel] [PATCH v2 qemu-server 7/9] ipam : add snaphot delete support Alexandre Derumier
2021-07-11 22:47 ` [pve-devel] [PATCH v2 qemu-server 8/9] ipam : add create vm support Alexandre Derumier
2021-07-11 22:47 ` [pve-devel] [PATCH v2 qemu-server 9/9] ipam : add destroy " Alexandre Derumier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20210711224707.63089-5-aderumier@odiso.com \
--to=aderumier@odiso.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.