[pve-devel] [PATCH container 1/1] feat: set hostname to meta

Serge Logvinov serge.logvinov at gmail.com
Fri Sep 10 20:33:43 CEST 2021


Add hostname property in meta-data (nocloud method).
Now only user-data has hostname of VM. If you redifine user-data, you lost the hostname.

Signed-off-by: Serge Logvinov <serge.logvinov at sinextra.dev>
---
 PVE/QemuServer/Cloudinit.pm | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
index a5474d3..b1df93d 100644
--- a/PVE/QemuServer/Cloudinit.pm
+++ b/PVE/QemuServer/Cloudinit.pm
@@ -479,15 +479,15 @@ sub nocloud_network {
 }
 
 sub nocloud_metadata {
-    my ($uuid) = @_;
-    return "instance-id: $uuid\n";
+    my ($uuid, $hostname) = @_;
+    return "instance-id: $uuid\nhostname: $hostname\n";
 }
 
 sub nocloud_gen_metadata {
-    my ($user, $network) = @_;
+    my ($user, $network, $hostname) = @_;
 
     my $uuid_str = Digest::SHA::sha1_hex($user.$network);
-    return nocloud_metadata($uuid_str);
+    return nocloud_metadata($uuid_str, $hostname);
 }
 
 sub generate_nocloud {
@@ -498,7 +498,8 @@ sub generate_nocloud {
     $network_data = nocloud_network($conf) if !defined($network_data);
 
     if (!defined($meta_data)) {
-	$meta_data = nocloud_gen_metadata($user_data, $network_data);
+	my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
+	$meta_data = nocloud_gen_metadata($user_data, $network_data, $hostname);
     }
 
     my $files = {
@@ -589,7 +590,8 @@ sub dump_cloudinit_config {
 	my $user = cloudinit_userdata($conf, $vmid);
 	if ($format eq 'nocloud') {
 	    my $network = nocloud_network($conf);
-	    return nocloud_gen_metadata($user, $network);
+	    my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
+	    return nocloud_gen_metadata($user, $network, $hostname);
 	} else {
 	    my $network = configdrive2_network($conf);
 	    return configdrive2_gen_metadata($user, $network);
-- 
2.24.3 (Apple Git-128)





More information about the pve-devel mailing list