[pve-devel] [PATCH v2 qemu-server] cloud-init: allow custom network/user data files via snippets

David Limbeck d.limbeck at proxmox.com
Thu Feb 7 15:12:35 CET 2019


Adds the 'cicustom' option to specify either or both network and user
options as property strings. Their parameters are files in a snippets
storage (e.g. local:snippets/network.yaml). If one or both are specified
they are used instead of their respective generated configuration.
This allows the use of completely custom configurations and is also a
possible solution for bug #2068 by specifying a custom user file that
contains package_upgrade: false.

Tested with Ubuntu 18.10 and cloud-init 18.4.7

Signed-off-by: David Limbeck <d.limbeck at proxmox.com>
---
v2:
 - Added meta parameter as well
 - removed _data from parameters
 - moved the reading of the files into a separate sub
 - renamed {network,user}_data_file to {network,user}_volid
 - changed bug number from 2038 to 2068 in commit message

 PVE/API2/Qemu.pm            |  1 +
 PVE/QemuServer.pm           | 31 +++++++++++++++++++++
 PVE/QemuServer/Cloudinit.pm | 65 +++++++++++++++++++++++++++++++++++++--------
 3 files changed, 86 insertions(+), 11 deletions(-)

diff --git a/PVE/API2/Qemu.pm b/PVE/API2/Qemu.pm
index 22f9f6a..49aaa48 100644
--- a/PVE/API2/Qemu.pm
+++ b/PVE/API2/Qemu.pm
@@ -292,6 +292,7 @@ my $diskoptions = {
 };
 
 my $cloudinitoptions = {
+    cicustom => 1,
     cipassword => 1,
     citype => 1,
     ciuser => 1,
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index 4a903a6..1f2a46b 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -623,6 +623,31 @@ EODESCR
     },
 };
 
+my $cicustom_fmt = {
+    meta => {
+	type => 'string',
+	optional => 1,
+	description => 'Specify a custom file containing all meta data passed to the VM via cloud-init. This is provider specific meaning configdrive2 and nocloud differ.',
+	format => 'pve-volume-id',
+	format_description => 'volume',
+    },
+    network => {
+	type => 'string',
+	optional => 1,
+	description => 'Specify a custom file containing all network data passed to the VM via cloud-init.',
+	format => 'pve-volume-id',
+	format_description => 'volume',
+    },
+    user => {
+	type => 'string',
+	optional => 1,
+	description => 'Specify a custom file containing all user data passed to the VM via cloud-init.',
+	format => 'pve-volume-id',
+	format_description => 'volume',
+    },
+};
+PVE::JSONSchema::register_format('pve-qm-cicustom', $cicustom_fmt);
+
 my $confdesc_cloudinit = {
     citype => {
 	optional => 1,
@@ -640,6 +665,12 @@ my $confdesc_cloudinit = {
 	type => 'string',
 	description => 'cloud-init: Password to assign the user. Using this is generally not recommended. Use ssh keys instead. Also note that older cloud-init versions do not support hashed passwords.',
     },
+    cicustom => {
+	optional => 1,
+	type => 'string',
+	description => 'cloud-init: Specify custom files to replace the automatically generated ones at start.',
+	format => 'pve-qm-cicustom',
+    },
     searchdomain => {
 	optional => 1,
 	type => 'string',
diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
index 5be820c..abb2544 100644
--- a/PVE/QemuServer/Cloudinit.pm
+++ b/PVE/QemuServer/Cloudinit.pm
@@ -208,14 +208,16 @@ EOF
 sub generate_configdrive2 {
     my ($conf, $vmid, $drive, $volname, $storeid) = @_;
 
-    my $user_data = cloudinit_userdata($conf, $vmid);
-    my $network_data = configdrive2_network($conf);
+    my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
+    $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
+    $network_data = configdrive2_network($conf) if !defined($network_data);
 
-    my $digest_data = $user_data . $network_data;
-    my $uuid_str = Digest::SHA::sha1_hex($digest_data);
-
-    my $meta_data = configdrive2_metadata($uuid_str);
+    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);
+    }
     my $files = {
 	'/openstack/latest/user_data' => $user_data,
 	'/openstack/content/0000' => $network_data,
@@ -378,13 +380,16 @@ sub nocloud_metadata {
 sub generate_nocloud {
     my ($conf, $vmid, $drive, $volname, $storeid) = @_;
 
-    my $user_data = cloudinit_userdata($conf, $vmid);
-    my $network_data = nocloud_network($conf);
+    my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
+    $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
+    $network_data = nocloud_network($conf) if !defined($network_data);
 
-    my $digest_data = $user_data . $network_data;
-    my $uuid_str = Digest::SHA::sha1_hex($digest_data);
+    if (!defined($meta_data)) {
+	my $digest_data = $user_data . $network_data;
+	my $uuid_str = Digest::SHA::sha1_hex($digest_data);
 
-    my $meta_data = nocloud_metadata($uuid_str);
+	$meta_data = nocloud_metadata($uuid_str);
+    }
 
     my $files = {
 	'/user-data' => $user_data,
@@ -394,6 +399,44 @@ sub generate_nocloud {
     commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
 }
 
+sub get_custom_cloudinit_files {
+    my ($conf) = @_;
+
+    my $cicustom = $conf->{cicustom};
+    my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
+
+    my $network_volid = $files->{network};
+    my $user_volid = $files->{user};
+    my $meta_volid = $files->{meta};
+
+    my $storage_conf = PVE::Storage::config();
+
+    my $network_data;
+    if ($network_volid) {
+	$network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
+    }
+
+    my $user_data;
+    if ($user_volid) {
+	$user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
+    }
+
+    my $meta_data;
+    if ($meta_volid) {
+	$meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
+    }
+
+    return ($user_data, $network_data, $meta_data);
+}
+
+sub read_cloudinit_snippets_file {
+    my ($storage_conf, $volid) = @_;
+
+    my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
+    die "$volid is not in the snippets directory\n" if $type ne 'snippets';
+    return PVE::Tools::file_get_contents($full_path);
+}
+
 my $cloudinit_methods = {
     configdrive2 => \&generate_configdrive2,
     nocloud => \&generate_nocloud,
-- 
2.11.0





More information about the pve-devel mailing list