]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer/Cloudinit.pm
fix #2429: allow to specify cloud-init vendor snippet via cicustom
[qemu-server.git] / PVE / QemuServer / Cloudinit.pm
index 609e8394d779ec5e455234c8c499a600d04492cc..420566e798ea607517e5974bdfeb3c87f2e960e3 100644 (file)
@@ -6,6 +6,7 @@ use warnings;
 use File::Path;
 use Digest::SHA;
 use URI::Escape;
+use MIME::Base64 qw(encode_base64);
 
 use PVE::Tools qw(run_command file_set_contents);
 use PVE::Storage;
@@ -34,24 +35,23 @@ sub commit_cloudinit_disk {
     my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
     my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
 
-    # required before file_size_info, some existing vols won't show up else
-    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
-    eval { $plugin->activate_volume($storeid, $scfg, $volname) };
-
-    my $size = eval { PVE::Storage::file_size_info($iso_path) };
-    if ($size <= 0) {
+    my $size = eval { PVE::Storage::volume_size_info($storecfg, $drive->{file}) };
+    if (!defined($size) || $size <= 0) {
        $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/;
        my $name = $1;
        $size = 4 * 1024;
        PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size);
        $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte
-       $plugin->activate_volume($storeid, $scfg, $volname);
     }
+    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
+    $plugin->activate_volume($storeid, $scfg, $volname);
 
+    print "generating cloud-init ISO\n";
     eval {
-       run_command([['genisoimage', '-R', '-V', $label, $path],
-                    ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
-                     'isize=0', "osize=$size", "of=$iso_path"]]);
+       run_command([
+           ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path],
+           ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"]
+       ]);
     };
     my $err = $@;
     rmtree($path);
@@ -129,7 +129,7 @@ sub cloudinit_userdata {
 
     if (defined(my $keys = $conf->{sshkeys})) {
        $keys = URI::Escape::uri_unescape($keys);
-       $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
+       $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)];
        $keys = [grep { /\S/ } @$keys];
        $content .= "ssh_authorized_keys:\n";
        foreach my $k (@$keys) {
@@ -172,8 +172,8 @@ sub configdrive2_network {
        $content .= "        dns_search $searchdomains\n";
     }
 
-    my @ifaces = grep(/^net(\d+)$/, keys %$conf);
-    foreach my $iface (@ifaces) {
+    my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
+    foreach my $iface (sort @ifaces) {
        (my $id = $iface) =~ s/^net//;
        next if !$conf->{"ipconfig$id"};
        my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
@@ -227,21 +227,99 @@ EOF
 sub generate_configdrive2 {
     my ($conf, $vmid, $drive, $volname, $storeid) = @_;
 
-    my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
+    my ($user_data, $network_data, $meta_data, $vendor_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);
 
     if (!defined($meta_data)) {
        $meta_data = configdrive2_gen_metadata($user_data, $network_data);
     }
+
+    my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data);
+    die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024);
+
     my $files = {
        '/openstack/latest/user_data' => $user_data,
        '/openstack/content/0000' => $network_data,
-       '/openstack/latest/meta_data.json' => $meta_data
+       '/openstack/latest/meta_data.json' => $meta_data,
+       '/openstack/latest/vendor_data.json' => $vendor_data
     };
     commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
 }
 
+sub generate_opennebula {
+    my ($conf, $vmid, $drive, $volname, $storeid) = @_;
+
+    my $content = "";
+
+    my $username = $conf->{ciuser} || "root";
+    $content .= "USERNAME=$username\n" if defined($username);
+
+    if (defined(my $password = $conf->{cipassword})) {
+       $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64($password) ."\n";
+    }
+
+    if (defined($conf->{sshkeys})) {
+       my $keys = [ split(/\s*\n\s*/, URI::Escape::uri_unescape($conf->{sshkeys})) ];
+       $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n";
+    }
+
+    my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
+    $content .= "SET_HOSTNAME=$hostname\n";
+
+    my ($searchdomains, $nameservers) = get_dns_conf($conf);
+    $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers;
+    $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains;
+
+    my $networkenabled = undef;
+    my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
+    foreach my $iface (sort @ifaces) {
+        (my $id = $iface) =~ s/^net//;
+       my $net = PVE::QemuServer::parse_net($conf->{$iface});
+        next if !$conf->{"ipconfig$id"};
+       my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
+        my $ethid = "ETH$id";
+
+       my $mac = lc $net->{hwaddr};
+
+       if ($ipconfig->{ip}) {
+           $networkenabled = 1;
+
+           if ($ipconfig->{ip} eq 'dhcp') {
+               $content .= "${ethid}_DHCP=YES\n";
+           } else {
+               my ($addr, $mask) = split_ip4($ipconfig->{ip});
+               $content .= "${ethid}_IP=$addr\n";
+               $content .= "${ethid}_MASK=$mask\n";
+               $content .= "${ethid}_MAC=$mac\n";
+               $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw};
+           }
+           $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu};
+       }
+
+       if ($ipconfig->{ip6}) {
+           $networkenabled = 1;
+           if ($ipconfig->{ip6} eq 'dhcp') {
+               $content .= "${ethid}_DHCP6=YES\n";
+           } elsif ($ipconfig->{ip6} eq 'auto') {
+               $content .= "${ethid}_AUTO6=YES\n";
+           } else {
+               my ($addr, $mask) = split('/', $ipconfig->{ip6});
+               $content .= "${ethid}_IP6=$addr\n";
+               $content .= "${ethid}_MASK6=$mask\n";
+               $content .= "${ethid}_MAC6=$mac\n";
+               $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6};
+           }
+           $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu};
+       }
+    }
+
+    $content .= "NETWORK=YES\n" if $networkenabled;
+
+    my $files = { '/context.sh' => $content };
+    commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT');
+}
+
 sub nocloud_network_v2 {
     my ($conf) = @_;
 
@@ -252,8 +330,8 @@ sub nocloud_network_v2 {
 
     my $dns_done;
 
-    my @ifaces = grep(/^net(\d+)$/, keys %$conf);
-    foreach my $iface (@ifaces) {
+    my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
+    foreach my $iface (sort @ifaces) {
        (my $id = $iface) =~ s/^net//;
        next if !$conf->{"ipconfig$id"};
 
@@ -323,8 +401,8 @@ sub nocloud_network {
     my $content = "version: 1\n"
                 . "config:\n";
 
-    my @ifaces = grep(/^net(\d+)$/, keys %$conf);
-    foreach my $iface (@ifaces) {
+    my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
+    foreach my $iface (sort @ifaces) {
        (my $id = $iface) =~ s/^net//;
        next if !$conf->{"ipconfig$id"};
 
@@ -359,10 +437,10 @@ sub nocloud_network {
            if ($ip eq 'dhcp') {
                $content .= "${i}- type: dhcp6\n";
            } elsif ($ip eq 'auto') {
-               # SLAAC is not supported by cloud-init, this fallback should work with an up-to-date netplan at least
-               $content .= "${i}- type: dhcp6\n";
+               # SLAAC is only supported by cloud-init since 19.4
+               $content .= "${i}- type: ipv6_slaac\n";
            } else {
-               $content .= "${i}- type: static\n"
+               $content .= "${i}- type: static6\n"
                       . "${i}  address: '$ip'\n";
                if (defined(my $gw = $ipconfig->{gw6})) {
                    $content .= "${i}  gateway: '$gw'\n";
@@ -403,7 +481,7 @@ sub nocloud_gen_metadata {
 sub generate_nocloud {
     my ($conf, $vmid, $drive, $volname, $storeid) = @_;
 
-    my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
+    my ($user_data, $network_data, $meta_data, $vendor_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);
 
@@ -411,10 +489,14 @@ sub generate_nocloud {
        $meta_data = nocloud_gen_metadata($user_data, $network_data);
     }
 
+    my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data);
+    die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024);
+
     my $files = {
        '/user-data' => $user_data,
        '/network-config' => $network_data,
-       '/meta-data' => $meta_data
+       '/meta-data' => $meta_data,
+       '/vendor-data' => $vendor_data
     };
     commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
 }
@@ -428,6 +510,7 @@ sub get_custom_cloudinit_files {
     my $network_volid = $files->{network};
     my $user_volid = $files->{user};
     my $meta_volid = $files->{meta};
+    my $vendor_volid = $files->{vendor};
 
     my $storage_conf = PVE::Storage::config();
 
@@ -446,7 +529,12 @@ sub get_custom_cloudinit_files {
        $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
     }
 
-    return ($user_data, $network_data, $meta_data);
+    my $vendor_data;
+    if ($vendor_volid) {
+       $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid);
+    }
+
+    return ($user_data, $network_data, $meta_data, $vendor_data);
 }
 
 sub read_cloudinit_snippets_file {
@@ -460,6 +548,7 @@ sub read_cloudinit_snippets_file {
 my $cloudinit_methods = {
     configdrive2 => \&generate_configdrive2,
     nocloud => \&generate_nocloud,
+    opennebula => \&generate_opennebula,
 };
 
 sub generate_cloudinitconfig {
@@ -467,7 +556,7 @@ sub generate_cloudinitconfig {
 
     my $format = get_cloudinit_format($conf);
 
-    PVE::QemuServer::foreach_drive($conf, sub {
+    PVE::QemuConfig->foreach_volume($conf, sub {
         my ($ds, $drive) = @_;
 
        my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);