From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <c.heiss@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id E0CDF9BADA
 for <pve-devel@lists.proxmox.com>; Fri, 20 Oct 2023 11:47:29 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id B468A32009
 for <pve-devel@lists.proxmox.com>; Fri, 20 Oct 2023 11:46:59 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS
 for <pve-devel@lists.proxmox.com>; Fri, 20 Oct 2023 11:46:56 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id C82F6435DC
 for <pve-devel@lists.proxmox.com>; Fri, 20 Oct 2023 11:46:55 +0200 (CEST)
From: Christoph Heiss <c.heiss@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Fri, 20 Oct 2023 11:46:45 +0200
Message-ID: <20231020094651.432513-3-c.heiss@proxmox.com>
X-Mailer: git-send-email 2.42.0
In-Reply-To: <20231020094651.432513-1-c.heiss@proxmox.com>
References: <20231020094651.432513-1-c.heiss@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.022 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [runenv.pm, net.pm]
Subject: [pve-devel] [PATCH installer 2/5] run env: retrieve and store
 hostname from DHCP lease if available
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Fri, 20 Oct 2023 09:47:29 -0000

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