all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [pve-devel] [PATCH installer 2/5] run env: retrieve and store hostname from DHCP lease if available
Date: Fri, 20 Oct 2023 11:46:45 +0200	[thread overview]
Message-ID: <20231020094651.432513-3-c.heiss@proxmox.com> (raw)
In-Reply-To: <20231020094651.432513-1-c.heiss@proxmox.com>

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
---
 Proxmox/Install/RunEnv.pm |  4 ++++
 Proxmox/Sys/Net.pm        | 25 +++++++++++++++++++++++++
 2 files changed, 29 insertions(+)

diff --git a/Proxmox/Install/RunEnv.pm b/Proxmox/Install/RunEnv.pm
index 19b5387..7589679 100644
--- a/Proxmox/Install/RunEnv.pm
+++ b/Proxmox/Install/RunEnv.pm
@@ -263,6 +263,10 @@ sub query_installation_environment : prototype() {
 	routes => $routes,
 	dns => query_dns(),
     };
+
+    # Cannot be put directly in the above hash as it might return undef ..
+    $output->{network}->{hostname} = Proxmox::Sys::Net::get_dhcp_hostname();
+
     # FIXME: move whatever makes sense over to Proxmox::Sys::Net:: and keep that as single source,
     # it can then use some different structure just fine (after adapting the GTK GUI to that) but
     # **never** to (slightly different!) things for the same stuff...
diff --git a/Proxmox/Sys/Net.pm b/Proxmox/Sys/Net.pm
index f5a9885..35d2abd 100644
--- a/Proxmox/Sys/Net.pm
+++ b/Proxmox/Sys/Net.pm
@@ -189,4 +189,29 @@ sub get_ip_config {
     }
 }
 
+# Tries to detect the hostname for this system via DHCP, if available.
+# DHCP server can set option 12 to inform the client about it's hostname [0].
+# dhclient dumps all options set by the DHCP server it in lease file, so just
+# read it from there.
+# [0] RFC 2132, section 3.14
+sub get_dhcp_hostname : prototype() {
+    my $leasefile = '/var/lib/dhcp/dhclient.leases';
+    return if ! -f $leasefile;
+
+    open (my $fh, '<', $leasefile) or return;
+
+    my $name = undef;
+    while (my $line = <$fh>) {
+	# "The name may or may not be qualified with the local domain name"
+	# Thus, only match the first part.
+	if ($line =~ m/^\s+option host-name \"(${FQDN_RE})\";$/) {
+	    $name = $1;
+	    last;
+	}
+    }
+
+    close($fh);
+    return $1 if defined($name) && $name =~ m/^([^\.]+)(?:\.(?:\S+))?$/;
+}
+
 1;
-- 
2.42.0





  parent reply	other threads:[~2023-10-20  9:47 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-20  9:46 [pve-devel] [PATCH installer 0/5] use " Christoph Heiss
2023-10-20  9:46 ` [pve-devel] [PATCH installer 1/5] net: move hostname/fqdn regexes into common code Christoph Heiss
2023-10-20  9:46 ` Christoph Heiss [this message]
2023-10-20  9:46 ` [pve-devel] [PATCH installer 3/5] proxinstall: use hostname from run env if available Christoph Heiss
2023-10-20  9:46 ` [pve-devel] [PATCH installer 4/5] tui: " Christoph Heiss
2023-10-20  9:46 ` [pve-devel] [PATCH installer 5/5] tui: add some tests for `NetworkInfo` -> `NetworkOptions` conversion Christoph Heiss
2023-10-20 15:21 ` [pve-devel] applied-series: [PATCH installer 0/5] use hostname from DHCP lease if available Thomas Lamprecht
2023-10-24  7:45   ` Christoph Heiss

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=20231020094651.432513-3-c.heiss@proxmox.com \
    --to=c.heiss@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 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.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal