[pve-devel] [PATCH qemu-server 1/3] add function to dump cloudinit config

Mira Limbeck m.limbeck at proxmox.com
Tue Jun 4 11:31:52 CEST 2019


This adds a function to dump the generated cloudinit config. Only one
can be dumped at a time, either 'user', 'network' or 'meta'.

The logic to get user, network and metadata is copied from the other
path that also creates the ISO image to keep it simple and not
complicate the other code path further.

The hash generation for the metadata config is unified between nocloud
and configdrive2 formats. We need it a 3rd time with the new dump
functions so it makes sense to combine it and the metadata config
generation in a single function. The <format>_gen_metadata functions are
each used twice.

Signed-off-by: Mira Limbeck <m.limbeck at proxmox.com>
---
The 'get_meta_config' sub is only used once, could also be merged into
'dump_generated_meta_config'.

 PVE/QemuServer/Cloudinit.pm | 78 ++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 70 insertions(+), 8 deletions(-)

diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
index 45176ea..78f907b 100644
--- a/PVE/QemuServer/Cloudinit.pm
+++ b/PVE/QemuServer/Cloudinit.pm
@@ -207,6 +207,13 @@ sub configdrive2_network {
     return $content;
 }
 
+sub configdrive2_gen_metadata {
+    my ($user, $network) = @_;
+
+    my $uuid_str = Digest::SHA::sha1_hex($user.$network);
+    return configdrive2_metadata($uuid_str);
+}
+
 sub configdrive2_metadata {
     my ($uuid) = @_;
     return <<"EOF";
@@ -225,10 +232,7 @@ sub generate_configdrive2 {
     $network_data = configdrive2_network($conf) if !defined($network_data);
 
     if (!defined($meta_data)) {
-	my $digest_data = $user_data . $network_data;
-	my $uuid_str = Digest::SHA::sha1_hex($digest_data);
-
-	$meta_data = configdrive2_metadata($uuid_str);
+	$meta_data = configdrive2_gen_metadata($user_data, $network_data);
     }
     my $files = {
 	'/openstack/latest/user_data' => $user_data,
@@ -389,6 +393,13 @@ sub nocloud_metadata {
     return "instance-id: $uuid\n";
 }
 
+sub nocloud_gen_metadata {
+    my ($user, $network) = @_;
+
+    my $uuid_str = Digest::SHA::sha1_hex($user.$network);
+    return nocloud_metadata($uuid_str);
+}
+
 sub generate_nocloud {
     my ($conf, $vmid, $drive, $volname, $storeid) = @_;
 
@@ -397,10 +408,7 @@ sub generate_nocloud {
     $network_data = nocloud_network($conf) if !defined($network_data);
 
     if (!defined($meta_data)) {
-	my $digest_data = $user_data . $network_data;
-	my $uuid_str = Digest::SHA::sha1_hex($digest_data);
-
-	$meta_data = nocloud_metadata($uuid_str);
+	$meta_data = nocloud_gen_metadata($user_data, $network_data);
     }
 
     my $files = {
@@ -473,4 +481,58 @@ sub generate_cloudinitconfig {
     });
 }
 
+sub dump_generated_user_config {
+    my ($conf, $vmid, $format) = @_;
+
+    return cloudinit_userdata($conf, $vmid);
+}
+
+sub get_network_config {
+    my ($conf, $format) = @_;
+
+    if ($format eq 'nocloud') {
+	return nocloud_network($conf);
+    } else {
+	return configdrive2_network($conf);
+    }
+}
+
+sub dump_generated_network_config {
+    my ($conf, $format) = @_;
+
+    return get_network_config($conf, $format);
+}
+
+sub get_meta_config {
+    my ($format, $user, $network) = @_;
+
+    if ($format eq 'nocloud') {
+	return nocloud_gen_metadata($user, $network);
+    } else {
+	return configdrive2_gen_metadata($user, $network);
+    }
+}
+
+sub dump_generated_meta_config {
+    my ($conf, $vmid, $format) = @_;
+
+    my $user = cloudinit_userdata($conf, $vmid);
+    my $network = get_network_config($conf, $format);
+    return get_meta_config($format, $user, $network);
+}
+
+sub dump_cloudinit_config {
+    my ($conf, $vmid, $type) = @_;
+
+    my $format = get_cloudinit_format($conf);
+
+    if ($type eq 'user') {
+	return dump_generated_user_config($conf, $vmid, $format);
+    } elsif ($type eq 'network') {
+	return dump_generated_network_config($conf, $format);
+    } else { # metadata config
+	return dump_generated_meta_config($conf, $vmid, $format);
+    }
+}
+
 1;
-- 
2.11.0





More information about the pve-devel mailing list