public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Filip Schauer <f.schauer@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH container 9/9] manage DHCP for containers with custom entrypoint
Date: Tue, 20 May 2025 14:42:57 +0200	[thread overview]
Message-ID: <20250520124257.165949-10-f.schauer@proxmox.com> (raw)
In-Reply-To: <20250520124257.165949-1-f.schauer@proxmox.com>

Containers that do not use the default `/sbin/init` entrypoint may lack
in‑container network management. A previous commit already handles
static IP addresses. Now this commit also handles DHCP. This is done
using a `dhclient` process for each network interface.

Signed-off-by: Filip Schauer <f.schauer@proxmox.com>
---
 src/PVE/LXC.pm        | 74 ++++++++++++++++++++++++++++++++++++++++---
 src/PVE/LXC/Config.pm |  6 +++-
 2 files changed, 74 insertions(+), 6 deletions(-)

diff --git a/src/PVE/LXC.pm b/src/PVE/LXC.pm
index 0131ac3..e91b53a 100644
--- a/src/PVE/LXC.pm
+++ b/src/PVE/LXC.pm
@@ -1004,6 +1004,8 @@ sub vm_stop_cleanup {
 	PVE::Storage::deactivate_volumes($storage_cfg, $vollist);
     };
     warn $@ if $@; # avoid errors - just warn
+
+    kill_dhclients($vmid, '*') if (PVE::LXC::Config->get_entrypoint($conf) ne "/sbin/init");
 }
 
 sub net_tap_plug : prototype($$) {
@@ -1189,6 +1191,34 @@ sub get_interfaces {
     return $res;
 }
 
+sub manage_dhclient {
+    my ($action, $vmid, $ipversion, $eth, $rootdir) = @_;
+
+    File::Path::make_path("/var/lib/lxc/$vmid/hook") if $action eq 'start';
+    my $pidfile = "/var/lib/lxc/$vmid/hook/dhclient$ipversion-$eth.pid";
+    my $leasefile = "/var/lib/lxc/$vmid/hook/dhclient$ipversion-$eth.leases";
+    my $scriptfile = '/usr/share/lxc/hooks/dhclient-script';
+    PVE::Tools::run_command([
+	'lxc-attach', '-n', $vmid, '-s', 'NETWORK|UTSNAME', '--',
+	'aa-exec', '-p', 'unconfined',
+	'/sbin/dhclient', $action eq 'start' ? '-1' : '-r', "-$ipversion",
+	'-pf', $pidfile, '-lf', $leasefile, '-e', "ROOTFS=$rootdir", '-sf', $scriptfile, $eth
+    ]);
+}
+
+sub kill_dhclients {
+    my ($vmid, $eth) = @_;
+
+    foreach my $pidfile (glob("/var/lib/lxc/$vmid/hook/dhclient*-$eth.pid")) {
+	my $pid = eval { file_get_contents($pidfile) };
+	if (!$@) {
+	    chomp $pid;
+	    kill 9, $pid if ($pid =~ m/^\d+$/);
+	    unlink($pidfile);
+	}
+    }
+}
+
 sub update_ipconfig {
     my ($vmid, $conf, $opt, $eth, $newnet, $rootdir) = @_;
 
@@ -1223,11 +1253,21 @@ sub update_ipconfig {
 
 	# step 1: add new IP, if this fails we cancel
 	my $is_real_ip = ($newip && $newip !~ /^(?:auto|dhcp|manual)$/);
-	if ($change_ip && $is_real_ip) {
-	    eval { &$ipcmd($family_opt, 'addr', 'add', $newip, 'dev', $eth); };
-	    if (my $err = $@) {
-		warn $err;
-		return;
+	if ($change_ip) {
+	    if (PVE::LXC::Config->get_entrypoint($conf) ne "/sbin/init") {
+		if ($newip && $newip eq 'dhcp') {
+		    manage_dhclient('start', $vmid, $ipversion, $eth, $rootdir);
+		} elsif ($oldip && $oldip eq 'dhcp') {
+		    manage_dhclient('stop', $vmid, $ipversion, $eth, $rootdir);
+		}
+	    }
+
+	    if ($is_real_ip) {
+		eval { &$ipcmd($family_opt, 'addr', 'add', $newip, 'dev', $eth); };
+		if (my $err = $@) {
+		    warn $err;
+		    return;
+		}
 	    }
 	}
 
@@ -2707,6 +2747,30 @@ sub vm_start {
     }
     PVE::GuestHelpers::exec_hookscript($conf, $vmid, 'post-start');
 
+    my @dhcpv4_interfaces = ();
+    my @dhcpv6_interfaces = ();
+    foreach my $k (sort keys %$conf) {
+	next if $k !~ m/^net(\d+)$/;
+	my $d = PVE::LXC::Config->parse_lxc_network($conf->{$k});
+	push @dhcpv4_interfaces, $d->{name} if $d->{ip} && $d->{ip} eq 'dhcp';
+	push @dhcpv6_interfaces, $d->{name} if $d->{ip6} && $d->{ip6} eq 'dhcp';
+    }
+
+    my $pid = PVE::LXC::find_lxc_pid($vmid);
+    my $rootdir = "/proc/$pid/root";
+
+    if (PVE::LXC::Config->get_entrypoint($conf) ne "/sbin/init") {
+	foreach my $eth (@dhcpv4_interfaces) {
+	    eval { manage_dhclient('start', $vmid, 4, $eth, $rootdir) };
+	    PVE::RESTEnvironment::log_warn("DHCP failed - $@") if $@;
+	}
+
+	foreach my $eth (@dhcpv6_interfaces) {
+	    eval { manage_dhclient('stop', $vmid, 6, $eth, $rootdir) };
+	    PVE::RESTEnvironment::log_warn("DHCP failed - $@") if $@;
+	}
+    }
+
     return;
 }
 
diff --git a/src/PVE/LXC/Config.pm b/src/PVE/LXC/Config.pm
index d7d8b6a..854e711 100644
--- a/src/PVE/LXC/Config.pm
+++ b/src/PVE/LXC/Config.pm
@@ -1490,9 +1490,13 @@ sub vmconfig_hotplug_pending {
 		$cgroup->change_cpu_shares(undef);
 	    } elsif ($opt =~ m/^net(\d)$/) {
 		my $netid = $1;
+		my $net = PVE::LXC::Config->parse_lxc_network($conf->{$opt});
+		if (PVE::LXC::Config->get_entrypoint($conf) ne "/sbin/init") {
+		    PVE::LXC::kill_dhclients($vmid, $net->{name});
+		}
+
 		PVE::Network::veth_delete("veth${vmid}i$netid");
 		if ($have_sdn) {
-		    my $net = PVE::LXC::Config->parse_lxc_network($conf->{$opt});
 		    print "delete ips from $opt\n";
 		    eval { PVE::Network::SDN::Vnets::del_ips_from_mac($net->{bridge}, $net->{hwaddr}, $conf->{hostname}) };
 		    warn $@ if $@;
-- 
2.39.5



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

  parent reply	other threads:[~2025-05-20 12:43 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-20 12:42 [pve-devel] [PATCH container/proxmox{, -perl-rs}/storage 0/9] support OCI images as container templates Filip Schauer
2025-05-20 12:42 ` [pve-devel] [PATCH proxmox 1/9] add proxmox-oci crate Filip Schauer
2025-06-02  9:33   ` Christoph Heiss
2025-05-20 12:42 ` [pve-devel] [PATCH proxmox-perl-rs 2/9] add Perl mapping for OCI container image parser Filip Schauer
2025-06-02  9:34   ` Christoph Heiss
2025-05-20 12:42 ` [pve-devel] [PATCH storage 3/9] allow .tar container templates Filip Schauer
2025-06-02 14:16   ` Michael Köppl
2025-05-20 12:42 ` [pve-devel] [PATCH container 4/9] config: whitelist lxc.init.cwd Filip Schauer
2025-05-20 12:42 ` [pve-devel] [PATCH container 5/9] add support for OCI images as container templates Filip Schauer
2025-05-20 12:42 ` [pve-devel] [PATCH container 6/9] config: add entrypoint parameter Filip Schauer
2025-05-20 12:42 ` [pve-devel] [PATCH container 7/9] configure static IP in LXC config for custom entrypoint Filip Schauer
2025-05-20 12:42 ` [pve-devel] [PATCH container 8/9] setup: debian: create /etc/network path if missing Filip Schauer
2025-06-02  9:37   ` Christoph Heiss
2025-06-02 10:49     ` Filip Schauer
2025-05-20 12:42 ` Filip Schauer [this message]
2025-06-02 16:26 ` [pve-devel] [PATCH container/proxmox{, -perl-rs}/storage 0/9] support OCI images as container templates Michael Köppl
2025-06-11 15:02   ` Filip Schauer
2025-06-06 13:19 ` Christoph Heiss
2025-06-11 15:09   ` Filip Schauer
2025-06-11 14:55 ` [pve-devel] superseded: " Filip Schauer

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=20250520124257.165949-10-f.schauer@proxmox.com \
    --to=f.schauer@proxmox.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal