[pve-devel] [PATCH qemu-server v2 1/3] add function to dump cloudinit config
Mira Limbeck
m.limbeck at proxmox.com
Wed Jun 5 11:09:41 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 now.
Signed-off-by: Mira Limbeck <m.limbeck at proxmox.com>
---
v2:
- removed all dump functions except for one and moved the logic there
as suggested by @Thomas
PVE/QemuServer/Cloudinit.pm | 49 +++++++++++++++++++++++++++++++++++++--------
1 file changed, 41 insertions(+), 8 deletions(-)
diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
index 45176ea..609e839 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,29 @@ sub generate_cloudinitconfig {
});
}
+sub dump_cloudinit_config {
+ my ($conf, $vmid, $type) = @_;
+
+ my $format = get_cloudinit_format($conf);
+
+ if ($type eq 'user') {
+ return cloudinit_userdata($conf, $vmid);
+ } elsif ($type eq 'network') {
+ if ($format eq 'nocloud') {
+ return nocloud_network($conf);
+ } else {
+ return configdrive2_network($conf);
+ }
+ } else { # metadata config
+ my $user = cloudinit_userdata($conf, $vmid);
+ if ($format eq 'nocloud') {
+ my $network = nocloud_network($conf);
+ return nocloud_gen_metadata($user, $network);
+ } else {
+ my $network = configdrive2_network($conf);
+ return configdrive2_gen_metadata($user, $network);
+ }
+ }
+}
+
1;
--
2.11.0
More information about the pve-devel
mailing list