Signed-off-by: Stefan Lendl <s.le...@proxmox.com>
---
 src/PVE/Network/SDN/Dhcp/Dnsmasq.pm    | 23 +++++++++++++++++++++++
 src/PVE/Network/SDN/Ipams/PVEPlugin.pm | 16 ++++++++++++++++
 2 files changed, 39 insertions(+)

diff --git a/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm 
b/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
index af109b8..6f8b1c4 100644
--- a/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
+++ b/src/PVE/Network/SDN/Dhcp/Dnsmasq.pm
@@ -18,6 +18,29 @@ sub type {
     return 'dnsmasq';
 }
 
+sub generate_config {
+    my ($class, $dhcp_config, $ip_mappings) = @_;
+
+    my $ethers_file = "$DNSMASQ_CONFIG_ROOT/$dhcp_config->{id}/ethers";
+    my $ethers_tmp_file = "$ethers_file.tmp";
+
+    open(my $out, '>', $ethers_file) or die "Could not open file 
'$ethers_file' $!\n";
+
+    foreach my $ip (keys %$ip_mappings) {
+       if (exists $ip_mappings->{$ip}{mac}) {
+           my $mac = $ip_mappings->{$ip}{mac};
+           print $out "$mac,$ip\n";
+       }
+    }
+
+    close $out;
+
+    chmod 0644, $ethers_file;
+
+    my $service_name = "dnsmasq\@$dhcp_config->{id}";
+    PVE::Tools::run_command(['systemctl', 'reload', $service_name]);
+}
+
 sub del_ip_mapping {
     my ($class, $dhcp_config, $mac) = @_;
 
diff --git a/src/PVE/Network/SDN/Ipams/PVEPlugin.pm 
b/src/PVE/Network/SDN/Ipams/PVEPlugin.pm
index fcc8282..40b4a8f 100644
--- a/src/PVE/Network/SDN/Ipams/PVEPlugin.pm
+++ b/src/PVE/Network/SDN/Ipams/PVEPlugin.pm
@@ -161,6 +161,8 @@ sub add_dhcp_ip {
 
     my $cidr = $subnet->{cidr};
     my $zone = $subnet->{zone};
+    my $vmid = $data->{vmid};
+    my $mac = $data->{mac};
 
     cfs_lock_file($ipamdb_file, undef, sub {
        my $db = read_db();
@@ -174,11 +176,25 @@ sub add_dhcp_ip {
        my $ip = new Net::IP ("$dhcp_range->{'start-address'} - 
$dhcp_range->{'end-address'}")
            or die "Invalid IP address(es) in DHCP Range!\n";
 
+       do {
+           my $ip_address = $ip->ip();
+           if (exists $dbsubnet->{ips}->{$ip_address} &&
+               exists $dbsubnet->{ips}->{$ip_address}->{mac} &&
+               $dbsubnet->{ips}->{$ip_address}->{mac} eq $mac) {
+               print "IP '$ip_address' already exist for $mac in $vmid\n";
+
+               return $ip_address;
+           }
+       } while (++$ip);
+
+       $ip = new Net::IP ("$dhcp_range->{'start-address'} - 
$dhcp_range->{'end-address'}");
+
        do {
            my $ip_address = $ip->ip();
            if (!$dbsubnet->{ips}->{$ip_address}) {
                $dbsubnet->{ips}->{$ip_address} = $data;
                write_db($db);
+               print "New IP '$ip_address' added for $mac at $vmid\n";
 
                return $ip_address;
            }
-- 
2.41.0



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel

Reply via email to