├── 7.3.3 - extra manualpatch.txt ├── 8.0.7 ├── patch │ ├── Cloudinit.pm.patch │ └── Qemu.pm.patch ├── source │ ├── Cloudinit.pm │ └── Qemu.pm └── target │ ├── Cloudinit.pm │ └── Qemu.pm ├── LICENSE ├── MANUALPATCH.md ├── README.md ├── conf ├── Unattend.xml ├── cloudbase-init-unattend.conf └── cloudbase-init.conf ├── localscripts ├── 01ActivateAdministrator.py └── 99EjectDrive.py ├── powershell └── FixUserService.ps1 ├── proxmox 7.3.3 ├── Cloudinit.pm └── Qemu.pm ├── pve7.4.3-patch ├── Cloudinit.pm └── Qemu.pm ├── qemu-server-6.4-2 ├── Cloudinit.pm.patch ├── Qemu.pm.patch └── sourcefiles │ ├── Cloudinit.pm │ └── Qemu.pm └── qemu-server-7.1-4 ├── Cloudinit.pm.patch ├── Qemu.pm.patch └── sourcefiles ├── Cloudinit.pm └── Qemu.pm /7.3.3 - extra manualpatch.txt: -------------------------------------------------------------------------------- 1 | # For sub generate_configdrive2 2 | if (!defined($meta_data)) { 3 | - $meta_data = configdrive2_gen_metadata($user_data, $network_data); 4 | + $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 5 | } 6 | 7 | 8 | # For sub dump_cloudinit_config 9 | } else { 10 | my $network = configdrive2_network($conf); 11 | - return configdrive2_gen_metadata($user, $network); 12 | + return configdrive2_gen_metadata($conf, $vmid, $user, $network); 13 | } 14 | -------------------------------------------------------------------------------- /8.0.7/patch/Cloudinit.pm.patch: -------------------------------------------------------------------------------- 1 | --- /usr/share/perl5/PVE/QemuServer/Cloudinit.pm.original 2023-08-21 11:30:45.000000000 +0200 2 | +++ /usr/share/perl5/PVE/QemuServer/Cloudinit.pm 2023-09-15 09:24:34.736589981 +0200 3 | @@ -122,7 +122,7 @@ 4 | 5 | $content .= "hostname: $hostname\n"; 6 | $content .= "manage_etc_hosts: true\n"; 7 | - $content .= "fqdn: $fqdn\n"; 8 | + $content .= "fqdn: $fqdn\n" if defined($fqdn); 9 | 10 | my $username = $conf->{ciuser}; 11 | my $password = $conf->{cipassword}; 12 | @@ -167,63 +167,151 @@ 13 | $content .= "iface lo inet loopback\n\n"; 14 | 15 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 16 | + 17 | + my $ostype = $conf->{"ostype"}; 18 | + my $default_dns = ''; 19 | + my $default_search = ''; 20 | + 21 | if ($nameservers && @$nameservers) { 22 | - $nameservers = join(' ', @$nameservers); 23 | - $content .= " dns_nameservers $nameservers\n"; 24 | + $nameservers = join(' ', @$nameservers); 25 | + $content .= " dns_nameservers $nameservers\n"; 26 | + $default_dns = $nameservers; 27 | } 28 | if ($searchdomains && @$searchdomains) { 29 | - $searchdomains = join(' ', @$searchdomains); 30 | - $content .= " dns_search $searchdomains\n"; 31 | + $searchdomains = join(' ', @$searchdomains); 32 | + $content .= " dns_search $searchdomains\n"; 33 | + $default_search = $searchdomains; 34 | } 35 | 36 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 37 | foreach my $iface (sort @ifaces) { 38 | - (my $id = $iface) =~ s/^net//; 39 | - next if !$conf->{"ipconfig$id"}; 40 | - my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 41 | - $id = "eth$id"; 42 | - 43 | - $content .="auto $id\n"; 44 | - if ($net->{ip}) { 45 | - if ($net->{ip} eq 'dhcp') { 46 | - $content .= "iface $id inet dhcp\n"; 47 | - } else { 48 | - my ($addr, $mask) = split_ip4($net->{ip}); 49 | - $content .= "iface $id inet static\n"; 50 | - $content .= " address $addr\n"; 51 | - $content .= " netmask $mask\n"; 52 | - $content .= " gateway $net->{gw}\n" if $net->{gw}; 53 | - } 54 | - } 55 | - if ($net->{ip6}) { 56 | - if ($net->{ip6} =~ /^(auto|dhcp)$/) { 57 | - $content .= "iface $id inet6 $1\n"; 58 | - } else { 59 | - my ($addr, $mask) = split('/', $net->{ip6}); 60 | - $content .= "iface $id inet6 static\n"; 61 | - $content .= " address $addr\n"; 62 | - $content .= " netmask $mask\n"; 63 | - $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 64 | - } 65 | - } 66 | + (my $id = $iface) =~ s/^net//; 67 | + next if !$conf->{"ipconfig$id"}; 68 | + my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 69 | + $id = "eth$id"; 70 | + 71 | + $content .="auto $id\n"; 72 | + if ($net->{ip}) { 73 | + if ($net->{ip} eq 'dhcp') { 74 | + $content .= "iface $id inet dhcp\n"; 75 | + } else { 76 | + my ($addr, $mask) = split_ip4($net->{ip}); 77 | + $content .= "iface $id inet static\n"; 78 | + $content .= " address $addr\n"; 79 | + $content .= " netmask $mask\n"; 80 | + $content .= " gateway $net->{gw}\n" if $net->{gw}; 81 | + if(PVE::QemuServer::windows_version($ostype) && ($id eq "eth0")) { 82 | + $content .= " dns-nameservers $default_dns\n"; 83 | + $content .= " dns-search $default_search\n"; 84 | + } 85 | + } 86 | + } 87 | + if ($net->{ip6}) { 88 | + if ($net->{ip6} =~ /^(auto|dhcp)$/) { 89 | + $content .= "iface $id inet6 $1\n"; 90 | + } else { 91 | + my ($addr, $mask) = split('/', $net->{ip6}); 92 | + $content .= "iface $id inet6 static\n"; 93 | + $content .= " address $addr\n"; 94 | + $content .= " netmask $mask\n"; 95 | + $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 96 | + } 97 | + } 98 | } 99 | 100 | return $content; 101 | } 102 | 103 | +sub get_mac_addresses { 104 | + my ($conf) = @_; 105 | + 106 | + my $dhcpstring = undef; 107 | + my @dhcpmacs = (); 108 | + my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 109 | + 110 | + foreach my $iface (sort @ifaces) { 111 | + (my $id = $iface) =~ s/^net//; 112 | + my $net = PVE::QemuServer::parse_net($conf->{$iface}); 113 | + next if !$conf->{"ipconfig$id"}; 114 | + my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 115 | + 116 | + my $mac = lc $net->{macaddr}; 117 | + 118 | + if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 119 | + push @dhcpmacs, $mac; 120 | + } 121 | + } 122 | + 123 | + if (@dhcpmacs){ 124 | + $dhcpstring = ",\n \"dhcp\":["; 125 | + foreach my $mac (@dhcpmacs){ 126 | + if ($mac != @dhcpmacs[-1]){ 127 | + $dhcpstring .= "\"$mac\","; 128 | + } 129 | + else{ 130 | + $dhcpstring .= "\"$mac\"]"; 131 | + } 132 | + } 133 | + } 134 | + return ($dhcpstring); 135 | +} 136 | + 137 | sub configdrive2_gen_metadata { 138 | - my ($user, $network) = @_; 139 | + my ($conf, $vmid, $user, $network) = @_; 140 | + 141 | + # Get mac addresses of dhcp nics from conf file 142 | + my $dhcpmacs = undef; 143 | + $dhcpmacs = get_mac_addresses($conf); 144 | 145 | + # Get UUID 146 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 147 | - return configdrive2_metadata($uuid_str); 148 | + 149 | + # Get hostname 150 | + my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 151 | + 152 | + # Get username, default to Administrator if none 153 | + my $username = undef; 154 | + if (defined($conf->{ciuser})){ 155 | + my $name = $conf->{ciuser}; 156 | + $username = ",\n \"admin_username\": \"$name\"" 157 | + } 158 | + 159 | + # Get user password 160 | + my $password = $conf->{cipassword}; 161 | + 162 | + # Get ssh keys and make a list out of it in json format 163 | + my $keystring = undef; 164 | + my $pubkeys = $conf->{sshkeys}; 165 | + $pubkeys = URI::Escape::uri_unescape($pubkeys); 166 | + my @pubkeysarray = split "\n", $pubkeys; 167 | + if (@pubkeysarray) { 168 | + my $arraylength = @pubkeysarray; 169 | + my $incrementer = 1; 170 | + $keystring =",\n \"public_keys\": {\n"; 171 | + for my $key (@pubkeysarray){ 172 | + $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 173 | + if ($arraylength != $incrementer){ 174 | + $keystring .= ",\n"; 175 | + }else{ 176 | + $keystring .= "\n }"; 177 | + } 178 | + $incrementer++; 179 | + } 180 | + } 181 | + 182 | + return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 183 | } 184 | 185 | sub configdrive2_metadata { 186 | - my ($uuid) = @_; 187 | + my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 188 | return <<"EOF"; 189 | { 190 | - "uuid": "$uuid", 191 | - "network_config": { "content_path": "/content/0000" } 192 | + "meta":{ 193 | + "admin_pass": "$password"$username 194 | + }, 195 | + "uuid":"$uuid", 196 | + "hostname":"$hostname", 197 | + "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs 198 | } 199 | EOF 200 | } 201 | @@ -237,7 +325,7 @@ 202 | $vendor_data = '' if !defined($vendor_data); 203 | 204 | if (!defined($meta_data)) { 205 | - $meta_data = configdrive2_gen_metadata($user_data, $network_data); 206 | + $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 207 | } 208 | 209 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 210 | @@ -624,7 +712,7 @@ 211 | return nocloud_gen_metadata($user, $network); 212 | } else { 213 | my $network = configdrive2_network($conf); 214 | - return configdrive2_gen_metadata($user, $network); 215 | + return configdrive2_gen_metadata($conf, $vmid, $user, $network); 216 | } 217 | } 218 | } 219 | -------------------------------------------------------------------------------- /8.0.7/patch/Qemu.pm.patch: -------------------------------------------------------------------------------- 1 | --- /usr/share/perl5/PVE/API2/Qemu.pm.original 2023-08-21 11:30:45.000000000 +0200 2 | +++ /usr/share/perl5/PVE/API2/Qemu.pm 2023-09-15 09:24:49.529312648 +0200 3 | @@ -1539,10 +1539,16 @@ 4 | 5 | my $skip_cloud_init = extract_param($param, 'skip_cloud_init'); 6 | 7 | + my $conf = PVE::QemuConfig->load_config($vmid); 8 | + 9 | + my $ostype = $conf->{ostype}; 10 | + 11 | if (defined(my $cipassword = $param->{cipassword})) { 12 | # Same logic as in cloud-init (but with the regex fixed...) 13 | - $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) 14 | - if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/; 15 | + if (!(PVE::QemuServer::windows_version($ostype))) { 16 | + $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) 17 | + if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/; 18 | + } 19 | } 20 | 21 | my @paramarr = (); # used for log message 22 | @@ -4805,7 +4811,7 @@ 23 | }, 24 | }, 25 | }, 26 | - returns => { 27 | + returns => { 28 | type => 'string', 29 | description => "the task ID.", 30 | }, 31 | @@ -4847,6 +4853,9 @@ 32 | my (undef, undef, undef, undef, undef, undef, $format) = 33 | PVE::Storage::parse_volname($storecfg, $drive->{file}); 34 | 35 | + die "can't resize volume: $disk if snapshot exists\n" 36 | + if %{$conf->{snapshots}} && $format eq 'qcow2'; 37 | + 38 | my $volid = $drive->{file}; 39 | 40 | die "disk '$disk' has no associated volume\n" if !$volid; 41 | @@ -4893,8 +4902,8 @@ 42 | }; 43 | 44 | my $worker = sub { 45 | - PVE::QemuConfig->lock_config($vmid, $updatefn); 46 | - }; 47 | + PVE::QemuConfig->lock_config($vmid, $updatefn); 48 | + }; 49 | 50 | return $rpcenv->fork_worker('resize', $vmid, $authuser, $worker); 51 | }}); 52 | -------------------------------------------------------------------------------- /8.0.7/source/Cloudinit.pm: -------------------------------------------------------------------------------- 1 | package PVE::QemuServer::Cloudinit; 2 | 3 | use strict; 4 | use warnings; 5 | 6 | use File::Path; 7 | use Digest::SHA; 8 | use URI::Escape; 9 | use MIME::Base64 qw(encode_base64); 10 | use Storable qw(dclone); 11 | 12 | use PVE::Tools qw(run_command file_set_contents); 13 | use PVE::Storage; 14 | use PVE::QemuServer; 15 | use PVE::QemuServer::Helpers; 16 | 17 | use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes 18 | 19 | sub commit_cloudinit_disk { 20 | my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_; 21 | 22 | my $path = "/run/pve/cloudinit/$vmid/"; 23 | mkpath $path; 24 | foreach my $filepath (keys %$files) { 25 | if ($filepath !~ m@^(.*)\/[^/]+$@) { 26 | die "internal error: bad file name in cloud-init image: $filepath\n"; 27 | } 28 | my $dirname = $1; 29 | mkpath "$path/$dirname"; 30 | 31 | my $contents = $files->{$filepath}; 32 | file_set_contents("$path/$filepath", $contents); 33 | } 34 | 35 | my $storecfg = PVE::Storage::config(); 36 | my $iso_path = PVE::Storage::path($storecfg, $drive->{file}); 37 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); 38 | my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); 39 | 40 | my $size = eval { PVE::Storage::volume_size_info($storecfg, $drive->{file}) }; 41 | if (!defined($size) || $size <= 0) { 42 | $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/; 43 | my $name = $1; 44 | $size = 4 * 1024; 45 | PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size); 46 | $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte 47 | } 48 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); 49 | $plugin->activate_volume($storeid, $scfg, $volname); 50 | 51 | print "generating cloud-init ISO\n"; 52 | eval { 53 | run_command([ 54 | ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path], 55 | ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"] 56 | ]); 57 | }; 58 | my $err = $@; 59 | rmtree($path); 60 | die $err if $err; 61 | } 62 | 63 | sub get_cloudinit_format { 64 | my ($conf) = @_; 65 | if (defined(my $format = $conf->{citype})) { 66 | return $format; 67 | } 68 | 69 | # No format specified, default based on ostype because windows' 70 | # cloudbased-init only supports configdrivev2, whereas on linux we need 71 | # to use mac addresses because regular cloudinit doesn't map 'ethX' to 72 | # the new predicatble network device naming scheme. 73 | if (defined(my $ostype = $conf->{ostype})) { 74 | return 'configdrive2' 75 | if PVE::QemuServer::Helpers::windows_version($ostype); 76 | } 77 | 78 | return 'nocloud'; 79 | } 80 | 81 | sub get_hostname_fqdn { 82 | my ($conf, $vmid) = @_; 83 | my $hostname = $conf->{name} // "VM$vmid"; 84 | my $fqdn; 85 | if ($hostname =~ /\./) { 86 | $fqdn = $hostname; 87 | $hostname =~ s/\..*$//; 88 | } elsif (my $search = $conf->{searchdomain}) { 89 | $fqdn = "$hostname.$search"; 90 | } else { 91 | $fqdn = $hostname; 92 | } 93 | return ($hostname, $fqdn); 94 | } 95 | 96 | sub get_dns_conf { 97 | my ($conf) = @_; 98 | 99 | # Same logic as in pve-container, but without the testcase special case 100 | my $host_resolv_conf = PVE::INotify::read_file('resolvconf'); 101 | 102 | my $searchdomains = [ 103 | split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search}) 104 | ]; 105 | 106 | my $nameserver = $conf->{nameserver}; 107 | if (!defined($nameserver)) { 108 | $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}]; 109 | } else { 110 | $nameserver = [split(/\s+/, $nameserver)]; 111 | } 112 | 113 | return ($searchdomains, $nameserver); 114 | } 115 | 116 | sub cloudinit_userdata { 117 | my ($conf, $vmid) = @_; 118 | 119 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 120 | 121 | my $content = "#cloud-config\n"; 122 | 123 | $content .= "hostname: $hostname\n"; 124 | $content .= "manage_etc_hosts: true\n"; 125 | $content .= "fqdn: $fqdn\n" if defined($fqdn); 126 | 127 | my $username = $conf->{ciuser}; 128 | my $password = $conf->{cipassword}; 129 | 130 | $content .= "user: $username\n" if defined($username); 131 | $content .= "disable_root: False\n" if defined($username) && $username eq 'root'; 132 | $content .= "password: $password\n" if defined($password); 133 | 134 | if (defined(my $keys = $conf->{sshkeys})) { 135 | $keys = URI::Escape::uri_unescape($keys); 136 | $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)]; 137 | $keys = [grep { /\S/ } @$keys]; 138 | $content .= "ssh_authorized_keys:\n"; 139 | foreach my $k (@$keys) { 140 | $content .= " - $k\n"; 141 | } 142 | } 143 | $content .= "chpasswd:\n"; 144 | $content .= " expire: False\n"; 145 | 146 | if (!defined($username) || $username ne 'root') { 147 | $content .= "users:\n"; 148 | $content .= " - default\n"; 149 | } 150 | 151 | $content .= "package_upgrade: true\n" if !defined($conf->{ciupgrade}) || $conf->{ciupgrade}; 152 | 153 | return $content; 154 | } 155 | 156 | sub split_ip4 { 157 | my ($ip) = @_; 158 | my ($addr, $mask) = split('/', $ip); 159 | die "not a CIDR: $ip\n" if !defined $mask; 160 | return ($addr, $PVE::Network::ipv4_reverse_mask->[$mask]); 161 | } 162 | 163 | sub configdrive2_network { 164 | my ($conf) = @_; 165 | 166 | my $content = "auto lo\n"; 167 | $content .= "iface lo inet loopback\n\n"; 168 | 169 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 170 | if ($nameservers && @$nameservers) { 171 | $nameservers = join(' ', @$nameservers); 172 | $content .= " dns_nameservers $nameservers\n"; 173 | } 174 | if ($searchdomains && @$searchdomains) { 175 | $searchdomains = join(' ', @$searchdomains); 176 | $content .= " dns_search $searchdomains\n"; 177 | } 178 | 179 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 180 | foreach my $iface (sort @ifaces) { 181 | (my $id = $iface) =~ s/^net//; 182 | next if !$conf->{"ipconfig$id"}; 183 | my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 184 | $id = "eth$id"; 185 | 186 | $content .="auto $id\n"; 187 | if ($net->{ip}) { 188 | if ($net->{ip} eq 'dhcp') { 189 | $content .= "iface $id inet dhcp\n"; 190 | } else { 191 | my ($addr, $mask) = split_ip4($net->{ip}); 192 | $content .= "iface $id inet static\n"; 193 | $content .= " address $addr\n"; 194 | $content .= " netmask $mask\n"; 195 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 196 | } 197 | } 198 | if ($net->{ip6}) { 199 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { 200 | $content .= "iface $id inet6 $1\n"; 201 | } else { 202 | my ($addr, $mask) = split('/', $net->{ip6}); 203 | $content .= "iface $id inet6 static\n"; 204 | $content .= " address $addr\n"; 205 | $content .= " netmask $mask\n"; 206 | $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 207 | } 208 | } 209 | } 210 | 211 | return $content; 212 | } 213 | 214 | sub configdrive2_gen_metadata { 215 | my ($user, $network) = @_; 216 | 217 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 218 | return configdrive2_metadata($uuid_str); 219 | } 220 | 221 | sub configdrive2_metadata { 222 | my ($uuid) = @_; 223 | return <<"EOF"; 224 | { 225 | "uuid": "$uuid", 226 | "network_config": { "content_path": "/content/0000" } 227 | } 228 | EOF 229 | } 230 | 231 | sub generate_configdrive2 { 232 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 233 | 234 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 235 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 236 | $network_data = configdrive2_network($conf) if !defined($network_data); 237 | $vendor_data = '' if !defined($vendor_data); 238 | 239 | if (!defined($meta_data)) { 240 | $meta_data = configdrive2_gen_metadata($user_data, $network_data); 241 | } 242 | 243 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 244 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 245 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 246 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 247 | 248 | my $files = { 249 | '/openstack/latest/user_data' => $user_data, 250 | '/openstack/content/0000' => $network_data, 251 | '/openstack/latest/meta_data.json' => $meta_data, 252 | '/openstack/latest/vendor_data.json' => $vendor_data 253 | }; 254 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2'); 255 | } 256 | 257 | sub generate_opennebula { 258 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 259 | 260 | my $content = ""; 261 | 262 | my $username = $conf->{ciuser} || "root"; 263 | $content .= "USERNAME=$username\n" if defined($username); 264 | 265 | if (defined(my $password = $conf->{cipassword})) { 266 | $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64($password) ."\n"; 267 | } 268 | 269 | if (defined($conf->{sshkeys})) { 270 | my $keys = [ split(/\s*\n\s*/, URI::Escape::uri_unescape($conf->{sshkeys})) ]; 271 | $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n"; 272 | } 273 | 274 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 275 | $content .= "SET_HOSTNAME=$hostname\n"; 276 | 277 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 278 | $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers; 279 | $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains; 280 | 281 | my $networkenabled = undef; 282 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 283 | foreach my $iface (sort @ifaces) { 284 | (my $id = $iface) =~ s/^net//; 285 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 286 | next if !$conf->{"ipconfig$id"}; 287 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 288 | my $ethid = "ETH$id"; 289 | 290 | my $mac = lc $net->{hwaddr}; 291 | 292 | if ($ipconfig->{ip}) { 293 | $networkenabled = 1; 294 | 295 | if ($ipconfig->{ip} eq 'dhcp') { 296 | $content .= "${ethid}_DHCP=YES\n"; 297 | } else { 298 | my ($addr, $mask) = split_ip4($ipconfig->{ip}); 299 | $content .= "${ethid}_IP=$addr\n"; 300 | $content .= "${ethid}_MASK=$mask\n"; 301 | $content .= "${ethid}_MAC=$mac\n"; 302 | $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw}; 303 | } 304 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 305 | } 306 | 307 | if ($ipconfig->{ip6}) { 308 | $networkenabled = 1; 309 | if ($ipconfig->{ip6} eq 'dhcp') { 310 | $content .= "${ethid}_DHCP6=YES\n"; 311 | } elsif ($ipconfig->{ip6} eq 'auto') { 312 | $content .= "${ethid}_AUTO6=YES\n"; 313 | } else { 314 | my ($addr, $mask) = split('/', $ipconfig->{ip6}); 315 | $content .= "${ethid}_IP6=$addr\n"; 316 | $content .= "${ethid}_MASK6=$mask\n"; 317 | $content .= "${ethid}_MAC6=$mac\n"; 318 | $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6}; 319 | } 320 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 321 | } 322 | } 323 | 324 | $content .= "NETWORK=YES\n" if $networkenabled; 325 | 326 | my $files = { '/context.sh' => $content }; 327 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT'); 328 | } 329 | 330 | sub nocloud_network_v2 { 331 | my ($conf) = @_; 332 | 333 | my $content = ''; 334 | 335 | my $head = "version: 2\n" 336 | . "ethernets:\n"; 337 | 338 | my $dns_done; 339 | 340 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 341 | foreach my $iface (sort @ifaces) { 342 | (my $id = $iface) =~ s/^net//; 343 | next if !$conf->{"ipconfig$id"}; 344 | 345 | # indentation - network interfaces are inside an 'ethernets' hash 346 | my $i = ' '; 347 | 348 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 349 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 350 | 351 | my $mac = $net->{macaddr} 352 | or die "network interface '$iface' has no mac address\n"; 353 | 354 | $content .= "${i}$iface:\n"; 355 | $i .= ' '; 356 | $content .= "${i}match:\n" 357 | . "${i} macaddress: \"$mac\"\n" 358 | . "${i}set-name: eth$id\n"; 359 | my @addresses; 360 | if (defined(my $ip = $ipconfig->{ip})) { 361 | if ($ip eq 'dhcp') { 362 | $content .= "${i}dhcp4: true\n"; 363 | } else { 364 | push @addresses, $ip; 365 | } 366 | } 367 | if (defined(my $ip = $ipconfig->{ip6})) { 368 | if ($ip eq 'dhcp') { 369 | $content .= "${i}dhcp6: true\n"; 370 | } else { 371 | push @addresses, $ip; 372 | } 373 | } 374 | if (@addresses) { 375 | $content .= "${i}addresses:\n"; 376 | $content .= "${i}- '$_'\n" foreach @addresses; 377 | } 378 | if (defined(my $gw = $ipconfig->{gw})) { 379 | $content .= "${i}gateway4: '$gw'\n"; 380 | } 381 | if (defined(my $gw = $ipconfig->{gw6})) { 382 | $content .= "${i}gateway6: '$gw'\n"; 383 | } 384 | 385 | next if $dns_done; 386 | $dns_done = 1; 387 | 388 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 389 | if ($searchdomains || $nameservers) { 390 | $content .= "${i}nameservers:\n"; 391 | if (defined($nameservers) && @$nameservers) { 392 | $content .= "${i} addresses:\n"; 393 | $content .= "${i} - '$_'\n" foreach @$nameservers; 394 | } 395 | if (defined($searchdomains) && @$searchdomains) { 396 | $content .= "${i} search:\n"; 397 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 398 | } 399 | } 400 | } 401 | 402 | return $head.$content; 403 | } 404 | 405 | sub nocloud_network { 406 | my ($conf) = @_; 407 | 408 | my $content = "version: 1\n" 409 | . "config:\n"; 410 | 411 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 412 | foreach my $iface (sort @ifaces) { 413 | (my $id = $iface) =~ s/^net//; 414 | next if !$conf->{"ipconfig$id"}; 415 | 416 | # indentation - network interfaces are inside an 'ethernets' hash 417 | my $i = ' '; 418 | 419 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 420 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 421 | 422 | my $mac = lc($net->{macaddr}) 423 | or die "network interface '$iface' has no mac address\n"; 424 | 425 | $content .= "${i}- type: physical\n" 426 | . "${i} name: eth$id\n" 427 | . "${i} mac_address: '$mac'\n" 428 | . "${i} subnets:\n"; 429 | $i .= ' '; 430 | if (defined(my $ip = $ipconfig->{ip})) { 431 | if ($ip eq 'dhcp') { 432 | $content .= "${i}- type: dhcp4\n"; 433 | } else { 434 | my ($addr, $mask) = split_ip4($ip); 435 | $content .= "${i}- type: static\n" 436 | . "${i} address: '$addr'\n" 437 | . "${i} netmask: '$mask'\n"; 438 | if (defined(my $gw = $ipconfig->{gw})) { 439 | $content .= "${i} gateway: '$gw'\n"; 440 | } 441 | } 442 | } 443 | if (defined(my $ip = $ipconfig->{ip6})) { 444 | if ($ip eq 'dhcp') { 445 | $content .= "${i}- type: dhcp6\n"; 446 | } elsif ($ip eq 'auto') { 447 | # SLAAC is only supported by cloud-init since 19.4 448 | $content .= "${i}- type: ipv6_slaac\n"; 449 | } else { 450 | $content .= "${i}- type: static6\n" 451 | . "${i} address: '$ip'\n"; 452 | if (defined(my $gw = $ipconfig->{gw6})) { 453 | $content .= "${i} gateway: '$gw'\n"; 454 | } 455 | } 456 | } 457 | } 458 | 459 | my $i = ' '; 460 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 461 | if ($searchdomains || $nameservers) { 462 | $content .= "${i}- type: nameserver\n"; 463 | if (defined($nameservers) && @$nameservers) { 464 | $content .= "${i} address:\n"; 465 | $content .= "${i} - '$_'\n" foreach @$nameservers; 466 | } 467 | if (defined($searchdomains) && @$searchdomains) { 468 | $content .= "${i} search:\n"; 469 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 470 | } 471 | } 472 | 473 | return $content; 474 | } 475 | 476 | sub nocloud_metadata { 477 | my ($uuid) = @_; 478 | return "instance-id: $uuid\n"; 479 | } 480 | 481 | sub nocloud_gen_metadata { 482 | my ($user, $network) = @_; 483 | 484 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 485 | return nocloud_metadata($uuid_str); 486 | } 487 | 488 | sub generate_nocloud { 489 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 490 | 491 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 492 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 493 | $network_data = nocloud_network($conf) if !defined($network_data); 494 | $vendor_data = '' if !defined($vendor_data); 495 | 496 | if (!defined($meta_data)) { 497 | $meta_data = nocloud_gen_metadata($user_data, $network_data); 498 | } 499 | 500 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 501 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 502 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 503 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 504 | 505 | my $files = { 506 | '/user-data' => $user_data, 507 | '/network-config' => $network_data, 508 | '/meta-data' => $meta_data, 509 | '/vendor-data' => $vendor_data 510 | }; 511 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata'); 512 | } 513 | 514 | sub get_custom_cloudinit_files { 515 | my ($conf) = @_; 516 | 517 | my $cicustom = $conf->{cicustom}; 518 | my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {}; 519 | 520 | my $network_volid = $files->{network}; 521 | my $user_volid = $files->{user}; 522 | my $meta_volid = $files->{meta}; 523 | my $vendor_volid = $files->{vendor}; 524 | 525 | my $storage_conf = PVE::Storage::config(); 526 | 527 | my $network_data; 528 | if ($network_volid) { 529 | $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid); 530 | } 531 | 532 | my $user_data; 533 | if ($user_volid) { 534 | $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid); 535 | } 536 | 537 | my $meta_data; 538 | if ($meta_volid) { 539 | $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid); 540 | } 541 | 542 | my $vendor_data; 543 | if ($vendor_volid) { 544 | $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid); 545 | } 546 | 547 | return ($user_data, $network_data, $meta_data, $vendor_data); 548 | } 549 | 550 | sub read_cloudinit_snippets_file { 551 | my ($storage_conf, $volid) = @_; 552 | 553 | my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid); 554 | die "$volid is not in the snippets directory\n" if $type ne 'snippets'; 555 | return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024); 556 | } 557 | 558 | my $cloudinit_methods = { 559 | configdrive2 => \&generate_configdrive2, 560 | nocloud => \&generate_nocloud, 561 | opennebula => \&generate_opennebula, 562 | }; 563 | 564 | sub has_changes { 565 | my ($conf) = @_; 566 | 567 | return !!$conf->{cloudinit}->%*; 568 | } 569 | 570 | sub generate_cloudinit_config { 571 | my ($conf, $vmid) = @_; 572 | 573 | my $format = get_cloudinit_format($conf); 574 | 575 | my $has_changes = has_changes($conf); 576 | 577 | PVE::QemuConfig->foreach_volume($conf, sub { 578 | my ($ds, $drive) = @_; 579 | 580 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); 581 | 582 | return if !$volname || $volname !~ m/vm-$vmid-cloudinit/; 583 | 584 | my $generator = $cloudinit_methods->{$format} 585 | or die "missing cloudinit methods for format '$format'\n"; 586 | 587 | $generator->($conf, $vmid, $drive, $volname, $storeid); 588 | }); 589 | 590 | return $has_changes; 591 | } 592 | 593 | sub apply_cloudinit_config { 594 | my ($conf, $vmid) = @_; 595 | 596 | my $has_changes = generate_cloudinit_config($conf, $vmid); 597 | 598 | if ($has_changes) { 599 | delete $conf->{cloudinit}; 600 | PVE::QemuConfig->write_config($vmid, $conf); 601 | return 1; 602 | } 603 | 604 | return $has_changes; 605 | } 606 | 607 | sub dump_cloudinit_config { 608 | my ($conf, $vmid, $type) = @_; 609 | 610 | my $format = get_cloudinit_format($conf); 611 | 612 | if ($type eq 'user') { 613 | return cloudinit_userdata($conf, $vmid); 614 | } elsif ($type eq 'network') { 615 | if ($format eq 'nocloud') { 616 | return nocloud_network($conf); 617 | } else { 618 | return configdrive2_network($conf); 619 | } 620 | } else { # metadata config 621 | my $user = cloudinit_userdata($conf, $vmid); 622 | if ($format eq 'nocloud') { 623 | my $network = nocloud_network($conf); 624 | return nocloud_gen_metadata($user, $network); 625 | } else { 626 | my $network = configdrive2_network($conf); 627 | return configdrive2_gen_metadata($user, $network); 628 | } 629 | } 630 | } 631 | 632 | 1; 633 | -------------------------------------------------------------------------------- /8.0.7/target/Cloudinit.pm: -------------------------------------------------------------------------------- 1 | package PVE::QemuServer::Cloudinit; 2 | 3 | use strict; 4 | use warnings; 5 | 6 | use File::Path; 7 | use Digest::SHA; 8 | use URI::Escape; 9 | use MIME::Base64 qw(encode_base64); 10 | use Storable qw(dclone); 11 | 12 | use PVE::Tools qw(run_command file_set_contents); 13 | use PVE::Storage; 14 | use PVE::QemuServer; 15 | use PVE::QemuServer::Helpers; 16 | 17 | use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes 18 | 19 | sub commit_cloudinit_disk { 20 | my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_; 21 | 22 | my $path = "/run/pve/cloudinit/$vmid/"; 23 | mkpath $path; 24 | foreach my $filepath (keys %$files) { 25 | if ($filepath !~ m@^(.*)\/[^/]+$@) { 26 | die "internal error: bad file name in cloud-init image: $filepath\n"; 27 | } 28 | my $dirname = $1; 29 | mkpath "$path/$dirname"; 30 | 31 | my $contents = $files->{$filepath}; 32 | file_set_contents("$path/$filepath", $contents); 33 | } 34 | 35 | my $storecfg = PVE::Storage::config(); 36 | my $iso_path = PVE::Storage::path($storecfg, $drive->{file}); 37 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); 38 | my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); 39 | 40 | my $size = eval { PVE::Storage::volume_size_info($storecfg, $drive->{file}) }; 41 | if (!defined($size) || $size <= 0) { 42 | $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/; 43 | my $name = $1; 44 | $size = 4 * 1024; 45 | PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size); 46 | $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte 47 | } 48 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); 49 | $plugin->activate_volume($storeid, $scfg, $volname); 50 | 51 | print "generating cloud-init ISO\n"; 52 | eval { 53 | run_command([ 54 | ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path], 55 | ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"] 56 | ]); 57 | }; 58 | my $err = $@; 59 | rmtree($path); 60 | die $err if $err; 61 | } 62 | 63 | sub get_cloudinit_format { 64 | my ($conf) = @_; 65 | if (defined(my $format = $conf->{citype})) { 66 | return $format; 67 | } 68 | 69 | # No format specified, default based on ostype because windows' 70 | # cloudbased-init only supports configdrivev2, whereas on linux we need 71 | # to use mac addresses because regular cloudinit doesn't map 'ethX' to 72 | # the new predicatble network device naming scheme. 73 | if (defined(my $ostype = $conf->{ostype})) { 74 | return 'configdrive2' 75 | if PVE::QemuServer::Helpers::windows_version($ostype); 76 | } 77 | 78 | return 'nocloud'; 79 | } 80 | 81 | sub get_hostname_fqdn { 82 | my ($conf, $vmid) = @_; 83 | my $hostname = $conf->{name} // "VM$vmid"; 84 | my $fqdn; 85 | if ($hostname =~ /\./) { 86 | $fqdn = $hostname; 87 | $hostname =~ s/\..*$//; 88 | } elsif (my $search = $conf->{searchdomain}) { 89 | $fqdn = "$hostname.$search"; 90 | } else { 91 | $fqdn = $hostname; 92 | } 93 | return ($hostname, $fqdn); 94 | } 95 | 96 | sub get_dns_conf { 97 | my ($conf) = @_; 98 | 99 | # Same logic as in pve-container, but without the testcase special case 100 | my $host_resolv_conf = PVE::INotify::read_file('resolvconf'); 101 | 102 | my $searchdomains = [ 103 | split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search}) 104 | ]; 105 | 106 | my $nameserver = $conf->{nameserver}; 107 | if (!defined($nameserver)) { 108 | $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}]; 109 | } else { 110 | $nameserver = [split(/\s+/, $nameserver)]; 111 | } 112 | 113 | return ($searchdomains, $nameserver); 114 | } 115 | 116 | sub cloudinit_userdata { 117 | my ($conf, $vmid) = @_; 118 | 119 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 120 | 121 | my $content = "#cloud-config\n"; 122 | 123 | $content .= "hostname: $hostname\n"; 124 | $content .= "manage_etc_hosts: true\n"; 125 | $content .= "fqdn: $fqdn\n" if defined($fqdn); 126 | 127 | my $username = $conf->{ciuser}; 128 | my $password = $conf->{cipassword}; 129 | 130 | $content .= "user: $username\n" if defined($username); 131 | $content .= "disable_root: False\n" if defined($username) && $username eq 'root'; 132 | $content .= "password: $password\n" if defined($password); 133 | 134 | if (defined(my $keys = $conf->{sshkeys})) { 135 | $keys = URI::Escape::uri_unescape($keys); 136 | $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)]; 137 | $keys = [grep { /\S/ } @$keys]; 138 | $content .= "ssh_authorized_keys:\n"; 139 | foreach my $k (@$keys) { 140 | $content .= " - $k\n"; 141 | } 142 | } 143 | $content .= "chpasswd:\n"; 144 | $content .= " expire: False\n"; 145 | 146 | if (!defined($username) || $username ne 'root') { 147 | $content .= "users:\n"; 148 | $content .= " - default\n"; 149 | } 150 | 151 | $content .= "package_upgrade: true\n" if !defined($conf->{ciupgrade}) || $conf->{ciupgrade}; 152 | 153 | return $content; 154 | } 155 | 156 | sub split_ip4 { 157 | my ($ip) = @_; 158 | my ($addr, $mask) = split('/', $ip); 159 | die "not a CIDR: $ip\n" if !defined $mask; 160 | return ($addr, $PVE::Network::ipv4_reverse_mask->[$mask]); 161 | } 162 | 163 | sub configdrive2_network { 164 | my ($conf) = @_; 165 | 166 | my $content = "auto lo\n"; 167 | $content .= "iface lo inet loopback\n\n"; 168 | 169 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 170 | 171 | my $ostype = $conf->{"ostype"}; 172 | my $default_dns = ''; 173 | my $default_search = ''; 174 | 175 | if ($nameservers && @$nameservers) { 176 | $nameservers = join(' ', @$nameservers); 177 | $content .= " dns_nameservers $nameservers\n"; 178 | $default_dns = $nameservers; 179 | } 180 | if ($searchdomains && @$searchdomains) { 181 | $searchdomains = join(' ', @$searchdomains); 182 | $content .= " dns_search $searchdomains\n"; 183 | $default_search = $searchdomains; 184 | } 185 | 186 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 187 | foreach my $iface (sort @ifaces) { 188 | (my $id = $iface) =~ s/^net//; 189 | next if !$conf->{"ipconfig$id"}; 190 | my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 191 | $id = "eth$id"; 192 | 193 | $content .="auto $id\n"; 194 | if ($net->{ip}) { 195 | if ($net->{ip} eq 'dhcp') { 196 | $content .= "iface $id inet dhcp\n"; 197 | } else { 198 | my ($addr, $mask) = split_ip4($net->{ip}); 199 | $content .= "iface $id inet static\n"; 200 | $content .= " address $addr\n"; 201 | $content .= " netmask $mask\n"; 202 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 203 | if(PVE::QemuServer::windows_version($ostype) && ($id eq "eth0")) { 204 | $content .= " dns-nameservers $default_dns\n"; 205 | $content .= " dns-search $default_search\n"; 206 | } 207 | } 208 | } 209 | if ($net->{ip6}) { 210 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { 211 | $content .= "iface $id inet6 $1\n"; 212 | } else { 213 | my ($addr, $mask) = split('/', $net->{ip6}); 214 | $content .= "iface $id inet6 static\n"; 215 | $content .= " address $addr\n"; 216 | $content .= " netmask $mask\n"; 217 | $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 218 | } 219 | } 220 | } 221 | 222 | return $content; 223 | } 224 | 225 | sub get_mac_addresses { 226 | my ($conf) = @_; 227 | 228 | my $dhcpstring = undef; 229 | my @dhcpmacs = (); 230 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 231 | 232 | foreach my $iface (sort @ifaces) { 233 | (my $id = $iface) =~ s/^net//; 234 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 235 | next if !$conf->{"ipconfig$id"}; 236 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 237 | 238 | my $mac = lc $net->{macaddr}; 239 | 240 | if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 241 | push @dhcpmacs, $mac; 242 | } 243 | } 244 | 245 | if (@dhcpmacs){ 246 | $dhcpstring = ",\n \"dhcp\":["; 247 | foreach my $mac (@dhcpmacs){ 248 | if ($mac != @dhcpmacs[-1]){ 249 | $dhcpstring .= "\"$mac\","; 250 | } 251 | else{ 252 | $dhcpstring .= "\"$mac\"]"; 253 | } 254 | } 255 | } 256 | return ($dhcpstring); 257 | } 258 | 259 | sub configdrive2_gen_metadata { 260 | my ($conf, $vmid, $user, $network) = @_; 261 | 262 | # Get mac addresses of dhcp nics from conf file 263 | my $dhcpmacs = undef; 264 | $dhcpmacs = get_mac_addresses($conf); 265 | 266 | # Get UUID 267 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 268 | 269 | # Get hostname 270 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 271 | 272 | # Get username, default to Administrator if none 273 | my $username = undef; 274 | if (defined($conf->{ciuser})){ 275 | my $name = $conf->{ciuser}; 276 | $username = ",\n \"admin_username\": \"$name\"" 277 | } 278 | 279 | # Get user password 280 | my $password = $conf->{cipassword}; 281 | 282 | # Get ssh keys and make a list out of it in json format 283 | my $keystring = undef; 284 | my $pubkeys = $conf->{sshkeys}; 285 | $pubkeys = URI::Escape::uri_unescape($pubkeys); 286 | my @pubkeysarray = split "\n", $pubkeys; 287 | if (@pubkeysarray) { 288 | my $arraylength = @pubkeysarray; 289 | my $incrementer = 1; 290 | $keystring =",\n \"public_keys\": {\n"; 291 | for my $key (@pubkeysarray){ 292 | $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 293 | if ($arraylength != $incrementer){ 294 | $keystring .= ",\n"; 295 | }else{ 296 | $keystring .= "\n }"; 297 | } 298 | $incrementer++; 299 | } 300 | } 301 | 302 | return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 303 | } 304 | 305 | sub configdrive2_metadata { 306 | my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 307 | return <<"EOF"; 308 | { 309 | "meta":{ 310 | "admin_pass": "$password"$username 311 | }, 312 | "uuid":"$uuid", 313 | "hostname":"$hostname", 314 | "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs 315 | } 316 | EOF 317 | } 318 | 319 | sub generate_configdrive2 { 320 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 321 | 322 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 323 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 324 | $network_data = configdrive2_network($conf) if !defined($network_data); 325 | $vendor_data = '' if !defined($vendor_data); 326 | 327 | if (!defined($meta_data)) { 328 | $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 329 | } 330 | 331 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 332 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 333 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 334 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 335 | 336 | my $files = { 337 | '/openstack/latest/user_data' => $user_data, 338 | '/openstack/content/0000' => $network_data, 339 | '/openstack/latest/meta_data.json' => $meta_data, 340 | '/openstack/latest/vendor_data.json' => $vendor_data 341 | }; 342 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2'); 343 | } 344 | 345 | sub generate_opennebula { 346 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 347 | 348 | my $content = ""; 349 | 350 | my $username = $conf->{ciuser} || "root"; 351 | $content .= "USERNAME=$username\n" if defined($username); 352 | 353 | if (defined(my $password = $conf->{cipassword})) { 354 | $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64($password) ."\n"; 355 | } 356 | 357 | if (defined($conf->{sshkeys})) { 358 | my $keys = [ split(/\s*\n\s*/, URI::Escape::uri_unescape($conf->{sshkeys})) ]; 359 | $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n"; 360 | } 361 | 362 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 363 | $content .= "SET_HOSTNAME=$hostname\n"; 364 | 365 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 366 | $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers; 367 | $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains; 368 | 369 | my $networkenabled = undef; 370 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 371 | foreach my $iface (sort @ifaces) { 372 | (my $id = $iface) =~ s/^net//; 373 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 374 | next if !$conf->{"ipconfig$id"}; 375 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 376 | my $ethid = "ETH$id"; 377 | 378 | my $mac = lc $net->{hwaddr}; 379 | 380 | if ($ipconfig->{ip}) { 381 | $networkenabled = 1; 382 | 383 | if ($ipconfig->{ip} eq 'dhcp') { 384 | $content .= "${ethid}_DHCP=YES\n"; 385 | } else { 386 | my ($addr, $mask) = split_ip4($ipconfig->{ip}); 387 | $content .= "${ethid}_IP=$addr\n"; 388 | $content .= "${ethid}_MASK=$mask\n"; 389 | $content .= "${ethid}_MAC=$mac\n"; 390 | $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw}; 391 | } 392 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 393 | } 394 | 395 | if ($ipconfig->{ip6}) { 396 | $networkenabled = 1; 397 | if ($ipconfig->{ip6} eq 'dhcp') { 398 | $content .= "${ethid}_DHCP6=YES\n"; 399 | } elsif ($ipconfig->{ip6} eq 'auto') { 400 | $content .= "${ethid}_AUTO6=YES\n"; 401 | } else { 402 | my ($addr, $mask) = split('/', $ipconfig->{ip6}); 403 | $content .= "${ethid}_IP6=$addr\n"; 404 | $content .= "${ethid}_MASK6=$mask\n"; 405 | $content .= "${ethid}_MAC6=$mac\n"; 406 | $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6}; 407 | } 408 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 409 | } 410 | } 411 | 412 | $content .= "NETWORK=YES\n" if $networkenabled; 413 | 414 | my $files = { '/context.sh' => $content }; 415 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT'); 416 | } 417 | 418 | sub nocloud_network_v2 { 419 | my ($conf) = @_; 420 | 421 | my $content = ''; 422 | 423 | my $head = "version: 2\n" 424 | . "ethernets:\n"; 425 | 426 | my $dns_done; 427 | 428 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 429 | foreach my $iface (sort @ifaces) { 430 | (my $id = $iface) =~ s/^net//; 431 | next if !$conf->{"ipconfig$id"}; 432 | 433 | # indentation - network interfaces are inside an 'ethernets' hash 434 | my $i = ' '; 435 | 436 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 437 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 438 | 439 | my $mac = $net->{macaddr} 440 | or die "network interface '$iface' has no mac address\n"; 441 | 442 | $content .= "${i}$iface:\n"; 443 | $i .= ' '; 444 | $content .= "${i}match:\n" 445 | . "${i} macaddress: \"$mac\"\n" 446 | . "${i}set-name: eth$id\n"; 447 | my @addresses; 448 | if (defined(my $ip = $ipconfig->{ip})) { 449 | if ($ip eq 'dhcp') { 450 | $content .= "${i}dhcp4: true\n"; 451 | } else { 452 | push @addresses, $ip; 453 | } 454 | } 455 | if (defined(my $ip = $ipconfig->{ip6})) { 456 | if ($ip eq 'dhcp') { 457 | $content .= "${i}dhcp6: true\n"; 458 | } else { 459 | push @addresses, $ip; 460 | } 461 | } 462 | if (@addresses) { 463 | $content .= "${i}addresses:\n"; 464 | $content .= "${i}- '$_'\n" foreach @addresses; 465 | } 466 | if (defined(my $gw = $ipconfig->{gw})) { 467 | $content .= "${i}gateway4: '$gw'\n"; 468 | } 469 | if (defined(my $gw = $ipconfig->{gw6})) { 470 | $content .= "${i}gateway6: '$gw'\n"; 471 | } 472 | 473 | next if $dns_done; 474 | $dns_done = 1; 475 | 476 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 477 | if ($searchdomains || $nameservers) { 478 | $content .= "${i}nameservers:\n"; 479 | if (defined($nameservers) && @$nameservers) { 480 | $content .= "${i} addresses:\n"; 481 | $content .= "${i} - '$_'\n" foreach @$nameservers; 482 | } 483 | if (defined($searchdomains) && @$searchdomains) { 484 | $content .= "${i} search:\n"; 485 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 486 | } 487 | } 488 | } 489 | 490 | return $head.$content; 491 | } 492 | 493 | sub nocloud_network { 494 | my ($conf) = @_; 495 | 496 | my $content = "version: 1\n" 497 | . "config:\n"; 498 | 499 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 500 | foreach my $iface (sort @ifaces) { 501 | (my $id = $iface) =~ s/^net//; 502 | next if !$conf->{"ipconfig$id"}; 503 | 504 | # indentation - network interfaces are inside an 'ethernets' hash 505 | my $i = ' '; 506 | 507 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 508 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 509 | 510 | my $mac = lc($net->{macaddr}) 511 | or die "network interface '$iface' has no mac address\n"; 512 | 513 | $content .= "${i}- type: physical\n" 514 | . "${i} name: eth$id\n" 515 | . "${i} mac_address: '$mac'\n" 516 | . "${i} subnets:\n"; 517 | $i .= ' '; 518 | if (defined(my $ip = $ipconfig->{ip})) { 519 | if ($ip eq 'dhcp') { 520 | $content .= "${i}- type: dhcp4\n"; 521 | } else { 522 | my ($addr, $mask) = split_ip4($ip); 523 | $content .= "${i}- type: static\n" 524 | . "${i} address: '$addr'\n" 525 | . "${i} netmask: '$mask'\n"; 526 | if (defined(my $gw = $ipconfig->{gw})) { 527 | $content .= "${i} gateway: '$gw'\n"; 528 | } 529 | } 530 | } 531 | if (defined(my $ip = $ipconfig->{ip6})) { 532 | if ($ip eq 'dhcp') { 533 | $content .= "${i}- type: dhcp6\n"; 534 | } elsif ($ip eq 'auto') { 535 | # SLAAC is only supported by cloud-init since 19.4 536 | $content .= "${i}- type: ipv6_slaac\n"; 537 | } else { 538 | $content .= "${i}- type: static6\n" 539 | . "${i} address: '$ip'\n"; 540 | if (defined(my $gw = $ipconfig->{gw6})) { 541 | $content .= "${i} gateway: '$gw'\n"; 542 | } 543 | } 544 | } 545 | } 546 | 547 | my $i = ' '; 548 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 549 | if ($searchdomains || $nameservers) { 550 | $content .= "${i}- type: nameserver\n"; 551 | if (defined($nameservers) && @$nameservers) { 552 | $content .= "${i} address:\n"; 553 | $content .= "${i} - '$_'\n" foreach @$nameservers; 554 | } 555 | if (defined($searchdomains) && @$searchdomains) { 556 | $content .= "${i} search:\n"; 557 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 558 | } 559 | } 560 | 561 | return $content; 562 | } 563 | 564 | sub nocloud_metadata { 565 | my ($uuid) = @_; 566 | return "instance-id: $uuid\n"; 567 | } 568 | 569 | sub nocloud_gen_metadata { 570 | my ($user, $network) = @_; 571 | 572 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 573 | return nocloud_metadata($uuid_str); 574 | } 575 | 576 | sub generate_nocloud { 577 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 578 | 579 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 580 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 581 | $network_data = nocloud_network($conf) if !defined($network_data); 582 | $vendor_data = '' if !defined($vendor_data); 583 | 584 | if (!defined($meta_data)) { 585 | $meta_data = nocloud_gen_metadata($user_data, $network_data); 586 | } 587 | 588 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 589 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 590 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 591 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 592 | 593 | my $files = { 594 | '/user-data' => $user_data, 595 | '/network-config' => $network_data, 596 | '/meta-data' => $meta_data, 597 | '/vendor-data' => $vendor_data 598 | }; 599 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata'); 600 | } 601 | 602 | sub get_custom_cloudinit_files { 603 | my ($conf) = @_; 604 | 605 | my $cicustom = $conf->{cicustom}; 606 | my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {}; 607 | 608 | my $network_volid = $files->{network}; 609 | my $user_volid = $files->{user}; 610 | my $meta_volid = $files->{meta}; 611 | my $vendor_volid = $files->{vendor}; 612 | 613 | my $storage_conf = PVE::Storage::config(); 614 | 615 | my $network_data; 616 | if ($network_volid) { 617 | $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid); 618 | } 619 | 620 | my $user_data; 621 | if ($user_volid) { 622 | $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid); 623 | } 624 | 625 | my $meta_data; 626 | if ($meta_volid) { 627 | $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid); 628 | } 629 | 630 | my $vendor_data; 631 | if ($vendor_volid) { 632 | $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid); 633 | } 634 | 635 | return ($user_data, $network_data, $meta_data, $vendor_data); 636 | } 637 | 638 | sub read_cloudinit_snippets_file { 639 | my ($storage_conf, $volid) = @_; 640 | 641 | my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid); 642 | die "$volid is not in the snippets directory\n" if $type ne 'snippets'; 643 | return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024); 644 | } 645 | 646 | my $cloudinit_methods = { 647 | configdrive2 => \&generate_configdrive2, 648 | nocloud => \&generate_nocloud, 649 | opennebula => \&generate_opennebula, 650 | }; 651 | 652 | sub has_changes { 653 | my ($conf) = @_; 654 | 655 | return !!$conf->{cloudinit}->%*; 656 | } 657 | 658 | sub generate_cloudinit_config { 659 | my ($conf, $vmid) = @_; 660 | 661 | my $format = get_cloudinit_format($conf); 662 | 663 | my $has_changes = has_changes($conf); 664 | 665 | PVE::QemuConfig->foreach_volume($conf, sub { 666 | my ($ds, $drive) = @_; 667 | 668 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); 669 | 670 | return if !$volname || $volname !~ m/vm-$vmid-cloudinit/; 671 | 672 | my $generator = $cloudinit_methods->{$format} 673 | or die "missing cloudinit methods for format '$format'\n"; 674 | 675 | $generator->($conf, $vmid, $drive, $volname, $storeid); 676 | }); 677 | 678 | return $has_changes; 679 | } 680 | 681 | sub apply_cloudinit_config { 682 | my ($conf, $vmid) = @_; 683 | 684 | my $has_changes = generate_cloudinit_config($conf, $vmid); 685 | 686 | if ($has_changes) { 687 | delete $conf->{cloudinit}; 688 | PVE::QemuConfig->write_config($vmid, $conf); 689 | return 1; 690 | } 691 | 692 | return $has_changes; 693 | } 694 | 695 | sub dump_cloudinit_config { 696 | my ($conf, $vmid, $type) = @_; 697 | 698 | my $format = get_cloudinit_format($conf); 699 | 700 | if ($type eq 'user') { 701 | return cloudinit_userdata($conf, $vmid); 702 | } elsif ($type eq 'network') { 703 | if ($format eq 'nocloud') { 704 | return nocloud_network($conf); 705 | } else { 706 | return configdrive2_network($conf); 707 | } 708 | } else { # metadata config 709 | my $user = cloudinit_userdata($conf, $vmid); 710 | if ($format eq 'nocloud') { 711 | my $network = nocloud_network($conf); 712 | return nocloud_gen_metadata($user, $network); 713 | } else { 714 | my $network = configdrive2_network($conf); 715 | return configdrive2_gen_metadata($conf, $vmid, $user, $network); 716 | } 717 | } 718 | } 719 | 720 | 1; 721 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /MANUALPATCH.md: -------------------------------------------------------------------------------- 1 | Do these steps to apply patches manually; 2 | 3 | There is two files that we need to modify; 4 | 5 | ## Qemu.pm 6 | At : /usr/share/perl5/PVE/API2/Qemu.pm 7 | 8 | We need to change user password to cleartext because it is hashed as default and Cloudbase-Init can not use it as it is. 9 | We can get the os type from the options of the VM so we will use to prevent Proxmox from hashing it if it is a Windows VM. 10 | 11 | The code to edit in my $update_vm_api fonction is belove; 12 | 13 | 14 | ``` 15 | my $conf = PVE::QemuConfig->load_config($vmid); 16 | my $ostype = $conf->{ostype}; 17 | 18 | if (defined(my $cipassword = $param->{cipassword})) { 19 | # Same logic as in cloud-init (but with the regex fixed...) 20 | if (!(PVE::QemuServer::windows_version($ostype))) { # new if block for support windowsand insert old code inside it 21 | $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) 22 | if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/; 23 | } 24 | } 25 | ``` 26 | 27 | ## Cloudinit.pm 28 | At : /usr/share/perl5/PVE/QemuServer/Cloudinit.pm 29 | 30 | We have a few changes to make to generate a meta_data.json that is compatible with Cloudbase-Init. 31 | 32 | * sub configdrive2_network 33 | 34 | We add a few lines to add DNS config 35 | 36 | ``` 37 | sub configdrive2_network { 38 | my ($conf) = @_; 39 | 40 | my $content = "auto lo\n"; 41 | $content .= "iface lo inet loopback\n\n"; 42 | 43 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 44 | 45 | ## support windows 46 | my $ostype = $conf->{"ostype"}; 47 | my $default_dns = ''; 48 | my $default_search = ''; 49 | ## 50 | 51 | if ($nameservers && @$nameservers) { 52 | $nameservers = join(' ', @$nameservers); 53 | $content .= " dns_nameservers $nameservers\n"; 54 | $default_dns = $nameservers; # Support windows 55 | } 56 | if ($searchdomains && @$searchdomains) { 57 | $searchdomains = join(' ', @$searchdomains); 58 | $content .= " dns_search $searchdomains\n"; 59 | $default_search = $searchdomains; # Support windows 60 | } 61 | 62 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 63 | foreach my $iface (sort @ifaces) { 64 | (my $id = $iface) =~ s/^net//; 65 | next if !$conf->{"ipconfig$id"}; 66 | my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 67 | $id = "eth$id"; 68 | 69 | $content .="auto $id\n"; 70 | if ($net->{ip}) { 71 | if ($net->{ip} eq 'dhcp') { 72 | $content .= "iface $id inet dhcp\n"; 73 | } else { 74 | my ($addr, $mask) = split_ip4($net->{ip}); 75 | $content .= "iface $id inet static\n"; 76 | $content .= " address $addr\n"; 77 | $content .= " netmask $mask\n"; 78 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 79 | ## Support Windows 80 | if(PVE::QemuServer::windows_version($ostype) && ($id eq "eth0")) { 81 | $content .= " dns-nameservers $default_dns\n"; 82 | $content .= " dns-search $default_search\n"; 83 | } 84 | ## 85 | } 86 | } 87 | if ($net->{ip6}) { 88 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { 89 | $content .= "iface $id inet6 $1\n"; 90 | } else { 91 | my ($addr, $mask) = split('/', $net->{ip6}); 92 | $content .= "iface $id inet6 static\n"; 93 | $content .= " address $addr\n"; 94 | $content .= " netmask $mask\n"; 95 | $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 96 | } 97 | } 98 | } 99 | 100 | return $content; 101 | } 102 | 103 | ``` 104 | 105 | * New fonction before sub configdrive2_gen_metadata 106 | 107 | Cloudbase-Init doesnt turn static network adapters back to DHCP configuration. This fonction will provide us the mac adresses of adapters to turn on dhcp from our config file and we will use it later with our script to enable dhcp on those network adapters. 108 | 109 | ``` 110 | sub get_mac_addresses { 111 | my ($conf) = @_; 112 | 113 | my $dhcpstring = undef; 114 | my @dhcpmacs = (); 115 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 116 | 117 | foreach my $iface (sort @ifaces) { 118 | (my $id = $iface) =~ s/^net//; 119 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 120 | next if !$conf->{"ipconfig$id"}; 121 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 122 | 123 | my $mac = lc $net->{macaddr}; 124 | 125 | if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 126 | push @dhcpmacs, $mac; 127 | } 128 | } 129 | 130 | if (@dhcpmacs){ 131 | $dhcpstring = ",\n \"dhcp\":["; 132 | foreach my $mac (@dhcpmacs){ 133 | if ($mac != @dhcpmacs[-1]){ 134 | $dhcpstring .= "\"$mac\","; 135 | } 136 | else{ 137 | $dhcpstring .= "\"$mac\"]"; 138 | } 139 | } 140 | } 141 | return ($dhcpstring); 142 | } 143 | 144 | ``` 145 | 146 | * configdrive2_gen_metadata 147 | We will generate our meta data variables from this fonction and call the give to another fonction which will format them. 148 | We get DHCP macs from our previous fonction, UUID, Hostname, Username, Password and we generate a json list from ssh keys. 149 | 150 | ``` 151 | sub configdrive2_gen_metadata { 152 | my ($conf, $vmid, $user, $network) = @_; 153 | 154 | # Get mac addresses of dhcp nics from conf file 155 | my $dhcpmacs = undef; 156 | $dhcpmacs = get_mac_addresses($conf); 157 | 158 | # Get UUID 159 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 160 | 161 | # Get hostname 162 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 163 | 164 | # Get username, default to Administrator if none 165 | my $username = undef; 166 | if (defined($conf->{ciuser})){ 167 | my $name = $conf->{ciuser}; 168 | $username = ",\n \"admin_username\": \"$name\"" 169 | } 170 | 171 | # Get user password 172 | my $password = $conf->{cipassword}; 173 | 174 | # Get ssh keys and make a list out of it in json format 175 | my $keystring = undef; 176 | my $pubkeys = $conf->{sshkeys}; 177 | $pubkeys = URI::Escape::uri_unescape($pubkeys); 178 | my @pubkeysarray = split "\n", $pubkeys; 179 | if (@pubkeysarray) { 180 | my $arraylength = @pubkeysarray; 181 | my $incrementer = 1; 182 | $keystring =",\n \"public_keys\": {\n"; 183 | for my $key (@pubkeysarray){ 184 | $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 185 | if ($arraylength != $incrementer){ 186 | $keystring .= ",\n"; 187 | }else{ 188 | $keystring .= "\n }"; 189 | } 190 | $incrementer++; 191 | } 192 | } 193 | 194 | return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 195 | } 196 | ``` 197 | 198 | * configdrive2_metadata 199 | 200 | This will format a json file, with our previously generated values, in a very stringy way since this is how proxmox originally generates this data. 201 | 202 | ``` 203 | sub configdrive2_metadata { 204 | my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 205 | return <<"EOF"; 206 | { 207 | "meta":{ 208 | "admin_pass": "$password"$username 209 | }, 210 | "uuid":"$uuid", 211 | "hostname":"$hostname", 212 | "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs 213 | } 214 | EOF 215 | } 216 | ``` 217 | 218 | 219 | 220 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Complete explanation 2 | 3 | https://proxmox 4 | 5 | # Geco-cloudbase-init 6 | 7 | This is an implementation of Cloudbase-Init to Windows virtual machines running in a Proxmox Node in order to use cloud-init with those vms. 8 | 9 | What can you do with this implementation? 10 | Use Cloudbase-Init with Windows VMs to: 11 | * Create a new user with username or enable administrator. 12 | * Set a password on the new user or administrator. 13 | * Set static ip or dhcp on network adapters. 14 | * Set machine hostname. 15 | * Insert public ssh keys to "user/.ssh/authorized_keys" file of created/enabled user. 16 | * Expand partition volumes automatically when there's a resized disk. 17 | 18 | You can do all below on system startup with the data provided by the cloud-init section of the proxmox gui. 19 | 20 | 21 | There is two files that we need to modify Qemu.pm and Cloudinit.pm. 22 | * Qemu.pm to get password as cleartext in meta_data drive when it is a Windows VM. 23 | * Cloudinit.pm to generate a metadata json file with variables that are compatible with Cloudbase-Init. 24 | 25 | ## Install Proxmox patch 26 | 27 | We have provided patch file for two different versions, if you are on those versions you can simply download it and apply the patch by doing; 28 | ``` 29 | # Check the version of your qemu-server 30 | apt show qemu-server |grep "Version" 31 | 32 | # If your version is 6.4-2 or 7.1-4 download the corresponding patch from this repo and continue, if not continue manual patching below. 33 | 34 | # Launch below as a test to see if you can apply the patch file, change path to where you downloaded the files and run this for two .patch files. 35 | patch --force --forward --backup -p0 --directory / --input "/absolute/path/to/patchfile.pm.patch" --dry-run && echo "You can apply patch" || { echo "Can't apply patch!";} 36 | 37 | # If the result is "Can't apply patch!", you can type "apt reinstall qemu-server" to reinstall the qemu-server files(If you have made changes to qemu-server source files they will be lost!) 38 | 39 | # Apply the patch if the result is "You can apply patch" 40 | patch --force --forward --backup -p0 --directory / --input "/absolute/path/to/patchfile.pm.patch" 41 | 42 | ``` 43 | 44 | If you want to revert the patch: 45 | `patch --force --reverse --backup -p0 --directory / --input "/absolute/path/to/patchfile.pm.patch"` 46 | 47 | 48 | If you want to apply the patch manually you can follow these steps: [Manual Patching](https://git.geco-it.net/c.soylu/Geco-cloudbase-init/src/branch/master/MANUALPATCH.md) 49 | 50 | ## Windows VM Configuration 51 | * Create a Windows VM in proxmox 52 | * Go to Hardware section of your VM, add Cloud-Init Drive and Serial Port 0 53 | 54 | Then configure Windows to your needs and proceed to Cloudbase-Init installation. 55 | 56 | ### Install Cloudbase-Init 57 | Install Cloudbase-Init Continous Build from the [official website](https://cloudbase.it/cloudbase-init/#download). 58 | 59 | Why Continous Build? Because the stable build dates from 2020 and doesn't include functionalities we use. 60 | 61 | ### Cloudbase-Init LocalScripts 62 | We have [two scripts](https://git.geco-it.net/c.soylu/Geco-cloudbase-init/src/branch/master/localscripts) that do some fonctionality that Cloudbase-Init doesnt have; 63 | * Enabling administrator user when it's name is given to the Cloudbase-Init. 64 | * Enabling DHCP on the network adapters. 65 | 66 | Move those scripts into Cloudbase Solutions\Cloudbase-Init\LocalScripts\ in your program files of your Windows VM. 67 | 68 | ### Configure Cloudbase-Init 69 | Deploy [these two conf files](https://git.geco-it.net/c.soylu/Geco-Cloudbase-Init/src/branch/master/conf) to `C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf`. 70 | 71 | Inside those files you will find the default Administrator name and the user group that will be used while user creation. You can launch our [ModifyConf.ps1](https://git.geco-it.net/GECO-IT-PUBLIC/Geco-Cloudbase-Init/src/branch/master/powershell) script to modify that file to get the correct username and group of your Windows language. 72 | 73 | ### Run PowerShell Script 74 | [This](https://git.geco-it.net/c.soylu/Geco-cloudbase-init/src/branch/master/powershell) powershell script has a few uses. 75 | * Deletes the "cloudbase-init" user, delegates "cloudbase-init" service to local Systeme user and modifies execution path of the script also to use local system user. 76 | * Installs OpenSSH-Server from optional features of Windows. 77 | * Removes a store language package that causes an error when generelazing for sysprep. 78 | 79 | Run this script after installing and configuring Cloudbase-Init Continous Build. 80 | 81 | ### Run SysPrep 82 | When everything is installed simply run below in powershell to launch sysprep: 83 | 84 | ``` 85 | cd ‘C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf’ 86 | C:\Windows\System32\sysprep\sysprep.exe /generalize /oobe /unattend:Unattend.xml 87 | 88 | ``` 89 | 90 | -------------------------------------------------------------------------------- /conf/Unattend.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | true 6 | 7 | 8 | 9 | 10 | 11 | true 12 | Work 13 | 1 14 | true 15 | true 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 1 24 | sc.exe config cloudbase-init start= auto 25 | Re-enable auto start of cloudbase-init 26 | Never 27 | 28 | 29 | 2 30 | sc.exe config cloudbase-init obj= .\LocalSystem 31 | Give permissions to cloudbase 32 | Never 33 | 34 | 35 | 3 36 | cmd.exe /c ""C:\Program Files\Cloudbase Solutions\Cloudbase-Init\Python\Scripts\cloudbase-init.exe" --config-file "C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf\cloudbase-init-unattend.conf" && exit 1 || exit 2" 37 | Run Cloudbase-Init to set the hostname 38 | OnRequest 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /conf/cloudbase-init-unattend.conf: -------------------------------------------------------------------------------- 1 | [DEFAULT] 2 | username=Administrator 3 | groups=Administrators 4 | inject_user_password=true 5 | config_drive_raw_hhd=true 6 | config_drive_cdrom=true 7 | config_drive_vfat=true 8 | bsdtar_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\bsdtar.exe 9 | mtools_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\ 10 | verbose=true 11 | debug=true 12 | log-dir=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log\ 13 | log-file=cloudbase-init-unattend.log 14 | default_log_levels=comtypes=INFO,suds=INFO,iso8601=WARN,requests=WARN 15 | logging_serial_port_settings=COM1,115200,N,8 16 | mtu_use_dhcp_config=true 17 | ntp_use_dhcp_config=true 18 | local_scripts_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\LocalScripts\ 19 | check_latest_version=false 20 | metadata_services=cloudbaseinit.metadata.services.configdrive.ConfigDriveService,cloudbaseinit.metadata.services.httpservice.HttpService,cloudbaseinit.metadata.services.ec2service.EC2Service,cloudbaseinit.metadata.services.maasservice.MaaSHttpService 21 | plugins=cloudbaseinit.plugins.common.mtu.MTUPlugin,cloudbaseinit.plugins.common.sethostname.SetHostNamePlugin,cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin 22 | allow_reboot=true 23 | stop_service_on_exit=false 24 | -------------------------------------------------------------------------------- /conf/cloudbase-init.conf: -------------------------------------------------------------------------------- 1 | [DEFAULT] 2 | username=Administrator 3 | groups=Administrators 4 | netbios_host_name_compatibility=true 5 | inject_user_password=true 6 | first_logon_behaviour=no 7 | config_drive_raw_hhd=true 8 | config_drive_cdrom=true 9 | config_drive_vfat=true 10 | locations=cdrom 11 | bsdtar_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\bsdtar.exe 12 | mtools_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\bin\ 13 | metadata_services=cloudbaseinit.metadata.services.configdrive.ConfigDriveService 14 | verbose=true 15 | debug=true 16 | ntp_use_dhcp_config=true 17 | real_time_clock_utc=true 18 | ntp_enable_service=true 19 | rdp_set_keepalive=true 20 | enable_automatic_updates=true 21 | log-dir=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\log\ 22 | log-file=cloudbase-init.log 23 | default_log_levels=comtypes=INFO,suds=INFO,iso8601=WARN,requests=WARN 24 | logging_serial_port_settings=COM1,115200,N,8 25 | local_scripts_path=C:\Program Files\Cloudbase Solutions\Cloudbase-Init\LocalScripts\ 26 | plugins=cloudbaseinit.plugins.common.mtu.MTUPlugin, cloudbaseinit.plugins.windows.ntpclient.NTPClientPlugin, cloudbaseinit.plugins.common.sethostname.SetHostNamePlugin, cloudbaseinit.plugins.windows.createuser.CreateUserPlugin, cloudbaseinit.plugins.common.networkconfig.NetworkConfigPlugin, cloudbaseinit.plugins.common.sshpublickeys.SetUserSSHPublicKeysPlugin, cloudbaseinit.plugins.windows.extendvolumes.ExtendVolumesPlugin, cloudbaseinit.plugins.common.setuserpassword.SetUserPasswordPlugin, cloudbaseinit.plugins.common.localscripts.LocalScriptsPlugin 27 | allow_reboot=true 28 | stop_service_on_exit=false 29 | check_latest_version=false 30 | -------------------------------------------------------------------------------- /localscripts/01ActivateAdministrator.py: -------------------------------------------------------------------------------- 1 | import os,json,sys,subprocess,configparser,platform 2 | 3 | def find_drive(file_path): 4 | for number in range(65,91): 5 | drive_letter = chr(number) 6 | if os.path.exists(drive_letter+file_path): 7 | return drive_letter+file_path 8 | print("\n Searched file could not be found on any drive with path:" + file_path) 9 | return False 10 | 11 | def load_json_file(file_path,variable): 12 | file = open(file_path) 13 | data = json.load(file) 14 | file.close() 15 | return data.get(variable) 16 | 17 | def get_administrator_status(): 18 | command = "(Get-LocalUser | Where-Object{$_.SID -like \"S-1-5-*-500\"}).Enabled" 19 | run = subprocess.run(["powershell", "-Command", command], stdout=subprocess.PIPE, universal_newlines=True) 20 | print("Is admin account enabled already: " + run.stdout) 21 | return run.stdout 22 | 23 | def get_administrator_name(): 24 | command = "(Get-LocalUser | Where-Object{$_.SID -like \"S-1-5-*-500\"}).Name" 25 | run = subprocess.run(["powershell", "-Command", command], stdout=subprocess.PIPE, universal_newlines=True) 26 | print("Administrator username: " + run.stdout) 27 | return run.stdout 28 | 29 | def enable_administrator_account(): 30 | command = "(Get-LocalUser | Where-Object{$_.SID -like \"S-1-5-*-500\"}).Name | Enable-LocalUser" 31 | run = subprocess.run(["powershell", "-Command", command], stdout=subprocess.PIPE, universal_newlines=True) 32 | print("\n Administrator account is activated by localscript") 33 | return run.stdout 34 | 35 | def is_os_64bit(): 36 | return platform.machine().endswith('64') 37 | 38 | def get_data(variable,path): 39 | configParser = configparser.RawConfigParser() 40 | configParser.read(path) 41 | data = configParser.get('DEFAULT',variable) 42 | return data 43 | 44 | 45 | # variables 46 | meta_data_path = find_drive(":\OPENSTACK\LATEST\META_DATA.json") 47 | admin_name = get_administrator_name() 48 | 49 | # execute 50 | if (meta_data_path) and ("admin_username" in load_json_file(meta_data_path,"meta")): 51 | meta_data = load_json_file(meta_data_path,"meta") 52 | meta_username = meta_data["admin_username"] 53 | print("Meta_Data admin_username is :" + meta_username) 54 | else: 55 | if is_os_64bit(): 56 | conf_path = r'C:\Program Files\Cloudbase Solutions\Cloudbase-Init\conf\cloudbase-init.conf' 57 | print("System architecture is 64 bit.") 58 | 59 | else: 60 | conf_path = r'C:\Program Files (x86)\Cloudbase Solutions\Cloudbase-Init\conf\cloudbase-init.conf' 61 | print("System architecture is 32 bit.") 62 | 63 | meta_username = get_data('username', conf_path) 64 | print("Conf username:"+meta_username) 65 | 66 | if meta_username in admin_name and "False" in get_administrator_status(): 67 | run = enable_administrator_account() 68 | sys.exit(1001) 69 | else: 70 | print("Cloud-init user is not Administrateur/Administrator or Admin account is already enabled, script aborted.") 71 | sys.exit(0) 72 | -------------------------------------------------------------------------------- /localscripts/99EjectDrive.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import ctypes 4 | import sys 5 | 6 | def remove_drive(): 7 | drive_letter = os.popen('wmic logicaldisk where VolumeName="config-2" get Caption | findstr /I ":"').read() 8 | if drive_letter: 9 | print('powershell "(new-object -COM Shell.Application).NameSpace(17).ParseName(\'' + drive_letter + '\').InvokeVerb(\'Eject\')"') 10 | 11 | remove_drive() 12 | sys.exit(0) -------------------------------------------------------------------------------- /powershell/FixUserService.ps1: -------------------------------------------------------------------------------- 1 | # Delete cloudbase-init User 2 | 3 | net user cloudbase-init /delete 4 | 5 | # Attribute service to local system 6 | 7 | sc.exe config cloudbase-init obj= .\LocalSystem 8 | 9 | # Modify executon path of Service 10 | 11 | $newtext = Get-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\cloudbase-init' -Name 'ImagePath' | Select-Object -ExpandProperty ImagePath | %{$_.replace(" cloudbase-init ", " NT-AUTHORITY\SYSTEM ")} 12 | Set-ItemProperty -Path 'HKLM:\SYSTEM\CurrentControlSet\Services\cloudbase-init' -Name 'ImagePath' -Value $newtext 13 | 14 | # Remove a microsoft store language package that causes generelazing issues 15 | 16 | Get-AppxPackage | Where-Object {$_.name -Like "*Language*"} | Remove-AppxPackage 17 | -------------------------------------------------------------------------------- /proxmox 7.3.3/Cloudinit.pm: -------------------------------------------------------------------------------- 1 | package PVE::QemuServer::Cloudinit; 2 | 3 | use strict; 4 | use warnings; 5 | 6 | use File::Path; 7 | use Digest::SHA; 8 | use URI::Escape; 9 | use MIME::Base64 qw(encode_base64); 10 | use Storable qw(dclone); 11 | 12 | use PVE::Tools qw(run_command file_set_contents); 13 | use PVE::Storage; 14 | use PVE::QemuServer; 15 | use PVE::QemuServer::Helpers; 16 | 17 | use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes 18 | 19 | sub commit_cloudinit_disk { 20 | my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_; 21 | 22 | my $path = "/run/pve/cloudinit/$vmid/"; 23 | mkpath $path; 24 | foreach my $filepath (keys %$files) { 25 | if ($filepath !~ m@^(.*)\/[^/]+$@) { 26 | die "internal error: bad file name in cloud-init image: $filepath\n"; 27 | } 28 | my $dirname = $1; 29 | mkpath "$path/$dirname"; 30 | 31 | my $contents = $files->{$filepath}; 32 | file_set_contents("$path/$filepath", $contents); 33 | } 34 | 35 | my $storecfg = PVE::Storage::config(); 36 | my $iso_path = PVE::Storage::path($storecfg, $drive->{file}); 37 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); 38 | my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); 39 | 40 | my $size = eval { PVE::Storage::volume_size_info($storecfg, $drive->{file}) }; 41 | if (!defined($size) || $size <= 0) { 42 | $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/; 43 | my $name = $1; 44 | $size = 4 * 1024; 45 | PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size); 46 | $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte 47 | } 48 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); 49 | $plugin->activate_volume($storeid, $scfg, $volname); 50 | 51 | print "generating cloud-init ISO\n"; 52 | eval { 53 | run_command([ 54 | ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path], 55 | ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"] 56 | ]); 57 | }; 58 | my $err = $@; 59 | rmtree($path); 60 | die $err if $err; 61 | } 62 | 63 | sub get_cloudinit_format { 64 | my ($conf) = @_; 65 | if (defined(my $format = $conf->{citype})) { 66 | return $format; 67 | } 68 | 69 | # No format specified, default based on ostype because windows' 70 | # cloudbased-init only supports configdrivev2, whereas on linux we need 71 | # to use mac addresses because regular cloudinit doesn't map 'ethX' to 72 | # the new predicatble network device naming scheme. 73 | if (defined(my $ostype = $conf->{ostype})) { 74 | return 'configdrive2' 75 | if PVE::QemuServer::Helpers::windows_version($ostype); 76 | } 77 | 78 | return 'nocloud'; 79 | } 80 | 81 | sub get_hostname_fqdn { 82 | my ($conf, $vmid) = @_; 83 | my $hostname = $conf->{name} // "VM$vmid"; 84 | my $fqdn; 85 | if ($hostname =~ /\./) { 86 | $fqdn = $hostname; 87 | $hostname =~ s/\..*$//; 88 | } elsif (my $search = $conf->{searchdomain}) { 89 | $fqdn = "$hostname.$search"; 90 | } 91 | return ($hostname, $fqdn); 92 | } 93 | 94 | sub get_dns_conf { 95 | my ($conf) = @_; 96 | 97 | # Same logic as in pve-container, but without the testcase special case 98 | my $host_resolv_conf = PVE::INotify::read_file('resolvconf'); 99 | 100 | my $searchdomains = [ 101 | split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search}) 102 | ]; 103 | 104 | my $nameserver = $conf->{nameserver}; 105 | if (!defined($nameserver)) { 106 | $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}]; 107 | } else { 108 | $nameserver = [split(/\s+/, $nameserver)]; 109 | } 110 | 111 | return ($searchdomains, $nameserver); 112 | } 113 | 114 | sub cloudinit_userdata { 115 | my ($conf, $vmid) = @_; 116 | 117 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 118 | 119 | my $content = "#cloud-config\n"; 120 | 121 | $content .= "hostname: $hostname\n"; 122 | $content .= "manage_etc_hosts: true\n"; 123 | $content .= "fqdn: $fqdn\n" if defined($fqdn); 124 | 125 | my $username = $conf->{ciuser}; 126 | my $password = $conf->{cipassword}; 127 | 128 | $content .= "user: $username\n" if defined($username); 129 | $content .= "disable_root: False\n" if defined($username) && $username eq 'root'; 130 | $content .= "password: $password\n" if defined($password); 131 | 132 | if (defined(my $keys = $conf->{sshkeys})) { 133 | $keys = URI::Escape::uri_unescape($keys); 134 | $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)]; 135 | $keys = [grep { /\S/ } @$keys]; 136 | $content .= "ssh_authorized_keys:\n"; 137 | foreach my $k (@$keys) { 138 | $content .= " - $k\n"; 139 | } 140 | } 141 | $content .= "chpasswd:\n"; 142 | $content .= " expire: False\n"; 143 | 144 | if (!defined($username) || $username ne 'root') { 145 | $content .= "users:\n"; 146 | $content .= " - default\n"; 147 | } 148 | 149 | $content .= "package_upgrade: true\n"; 150 | 151 | return $content; 152 | } 153 | 154 | sub split_ip4 { 155 | my ($ip) = @_; 156 | my ($addr, $mask) = split('/', $ip); 157 | die "not a CIDR: $ip\n" if !defined $mask; 158 | return ($addr, $PVE::Network::ipv4_reverse_mask->[$mask]); 159 | } 160 | 161 | sub configdrive2_network { 162 | my ($conf) = @_; 163 | 164 | my $content = "auto lo\n"; 165 | $content .= "iface lo inet loopback\n\n"; 166 | 167 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 168 | 169 | ## support windows 170 | my $ostype = $conf->{"ostype"}; 171 | my $default_dns = ''; 172 | my $default_search = ''; 173 | ## 174 | 175 | if ($nameservers && @$nameservers) { 176 | $nameservers = join(' ', @$nameservers); 177 | $content .= " dns_nameservers $nameservers\n"; 178 | $default_dns = $nameservers; # Support windows 179 | } 180 | if ($searchdomains && @$searchdomains) { 181 | $searchdomains = join(' ', @$searchdomains); 182 | $content .= " dns_search $searchdomains\n"; 183 | $default_search = $searchdomains; # Support windows 184 | } 185 | 186 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 187 | foreach my $iface (sort @ifaces) { 188 | (my $id = $iface) =~ s/^net//; 189 | next if !$conf->{"ipconfig$id"}; 190 | my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 191 | $id = "eth$id"; 192 | 193 | $content .="auto $id\n"; 194 | if ($net->{ip}) { 195 | if ($net->{ip} eq 'dhcp') { 196 | $content .= "iface $id inet dhcp\n"; 197 | } else { 198 | my ($addr, $mask) = split_ip4($net->{ip}); 199 | $content .= "iface $id inet static\n"; 200 | $content .= " address $addr\n"; 201 | $content .= " netmask $mask\n"; 202 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 203 | ## Support Windows 204 | if(PVE::QemuServer::windows_version($ostype) && ($id eq "eth0")) { 205 | $content .= " dns-nameservers $default_dns\n"; 206 | $content .= " dns-search $default_search\n"; 207 | } 208 | ## 209 | } 210 | } 211 | if ($net->{ip6}) { 212 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { 213 | $content .= "iface $id inet6 $1\n"; 214 | } else { 215 | my ($addr, $mask) = split('/', $net->{ip6}); 216 | $content .= "iface $id inet6 static\n"; 217 | $content .= " address $addr\n"; 218 | $content .= " netmask $mask\n"; 219 | $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 220 | } 221 | } 222 | } 223 | 224 | return $content; 225 | } 226 | 227 | sub get_mac_addresses { 228 | my ($conf) = @_; 229 | 230 | my $dhcpstring = undef; 231 | my @dhcpmacs = (); 232 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 233 | 234 | foreach my $iface (sort @ifaces) { 235 | (my $id = $iface) =~ s/^net//; 236 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 237 | next if !$conf->{"ipconfig$id"}; 238 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 239 | 240 | my $mac = lc $net->{macaddr}; 241 | 242 | if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 243 | push @dhcpmacs, $mac; 244 | } 245 | } 246 | 247 | if (@dhcpmacs){ 248 | $dhcpstring = ",\n \"dhcp\":["; 249 | foreach my $mac (@dhcpmacs){ 250 | if ($mac != @dhcpmacs[-1]){ 251 | $dhcpstring .= "\"$mac\","; 252 | } 253 | else{ 254 | $dhcpstring .= "\"$mac\"]"; 255 | } 256 | } 257 | } 258 | return ($dhcpstring); 259 | } 260 | 261 | sub configdrive2_gen_metadata { 262 | my ($conf, $vmid, $user, $network) = @_; 263 | 264 | # Get mac addresses of dhcp nics from conf file 265 | my $dhcpmacs = undef; 266 | $dhcpmacs = get_mac_addresses($conf); 267 | 268 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 269 | 270 | # Get hostname 271 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 272 | 273 | # Get username, default to Administrator if none 274 | my $username = undef; 275 | if (defined($conf->{ciuser})){ 276 | my $name = $conf->{ciuser}; 277 | $username = ",\n \"admin_username\": \"$name\"" 278 | } 279 | 280 | # Get user password 281 | my $password = $conf->{cipassword}; 282 | 283 | # Get ssh keys and make a list out of it in json format 284 | my $keystring = undef; 285 | my $pubkeys = $conf->{sshkeys}; 286 | $pubkeys = URI::Escape::uri_unescape($pubkeys); 287 | my @pubkeysarray = split "\n", $pubkeys; 288 | if (@pubkeysarray) { 289 | my $arraylength = @pubkeysarray; 290 | my $incrementer = 1; 291 | $keystring =",\n \"public_keys\": {\n"; 292 | for my $key (@pubkeysarray){ 293 | $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 294 | if ($arraylength != $incrementer){ 295 | $keystring .= ",\n"; 296 | }else{ 297 | $keystring .= "\n }"; 298 | } 299 | $incrementer++; 300 | } 301 | } 302 | 303 | return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 304 | } 305 | 306 | sub configdrive2_metadata { 307 | my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 308 | return <<"EOF"; 309 | { 310 | "meta":{ 311 | "admin_pass": "$password"$username 312 | }, 313 | "uuid": "$uuid", 314 | "hostname":"$hostname", 315 | "network_config": { "content_path": "/content/0000"}$pubkeys$dhcpmacs 316 | } 317 | EOF 318 | } 319 | 320 | sub generate_configdrive2 { 321 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 322 | 323 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 324 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 325 | $network_data = configdrive2_network($conf) if !defined($network_data); 326 | $vendor_data = '' if !defined($vendor_data); 327 | 328 | if (!defined($meta_data)) { 329 | $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 330 | } 331 | 332 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 333 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 334 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 335 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 336 | 337 | my $files = { 338 | '/openstack/latest/user_data' => $user_data, 339 | '/openstack/content/0000' => $network_data, 340 | '/openstack/latest/meta_data.json' => $meta_data, 341 | '/openstack/latest/vendor_data.json' => $vendor_data 342 | }; 343 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2'); 344 | } 345 | 346 | sub generate_opennebula { 347 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 348 | 349 | my $content = ""; 350 | 351 | my $username = $conf->{ciuser} || "root"; 352 | $content .= "USERNAME=$username\n" if defined($username); 353 | 354 | if (defined(my $password = $conf->{cipassword})) { 355 | $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64($password) ."\n"; 356 | } 357 | 358 | if (defined($conf->{sshkeys})) { 359 | my $keys = [ split(/\s*\n\s*/, URI::Escape::uri_unescape($conf->{sshkeys})) ]; 360 | $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n"; 361 | } 362 | 363 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 364 | $content .= "SET_HOSTNAME=$hostname\n"; 365 | 366 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 367 | $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers; 368 | $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains; 369 | 370 | my $networkenabled = undef; 371 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 372 | foreach my $iface (sort @ifaces) { 373 | (my $id = $iface) =~ s/^net//; 374 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 375 | next if !$conf->{"ipconfig$id"}; 376 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 377 | my $ethid = "ETH$id"; 378 | 379 | my $mac = lc $net->{hwaddr}; 380 | 381 | if ($ipconfig->{ip}) { 382 | $networkenabled = 1; 383 | 384 | if ($ipconfig->{ip} eq 'dhcp') { 385 | $content .= "${ethid}_DHCP=YES\n"; 386 | } else { 387 | my ($addr, $mask) = split_ip4($ipconfig->{ip}); 388 | $content .= "${ethid}_IP=$addr\n"; 389 | $content .= "${ethid}_MASK=$mask\n"; 390 | $content .= "${ethid}_MAC=$mac\n"; 391 | $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw}; 392 | } 393 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 394 | } 395 | 396 | if ($ipconfig->{ip6}) { 397 | $networkenabled = 1; 398 | if ($ipconfig->{ip6} eq 'dhcp') { 399 | $content .= "${ethid}_DHCP6=YES\n"; 400 | } elsif ($ipconfig->{ip6} eq 'auto') { 401 | $content .= "${ethid}_AUTO6=YES\n"; 402 | } else { 403 | my ($addr, $mask) = split('/', $ipconfig->{ip6}); 404 | $content .= "${ethid}_IP6=$addr\n"; 405 | $content .= "${ethid}_MASK6=$mask\n"; 406 | $content .= "${ethid}_MAC6=$mac\n"; 407 | $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6}; 408 | } 409 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 410 | } 411 | } 412 | 413 | $content .= "NETWORK=YES\n" if $networkenabled; 414 | 415 | my $files = { '/context.sh' => $content }; 416 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT'); 417 | } 418 | 419 | sub nocloud_network_v2 { 420 | my ($conf) = @_; 421 | 422 | my $content = ''; 423 | 424 | my $head = "version: 2\n" 425 | . "ethernets:\n"; 426 | 427 | my $dns_done; 428 | 429 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 430 | foreach my $iface (sort @ifaces) { 431 | (my $id = $iface) =~ s/^net//; 432 | next if !$conf->{"ipconfig$id"}; 433 | 434 | # indentation - network interfaces are inside an 'ethernets' hash 435 | my $i = ' '; 436 | 437 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 438 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 439 | 440 | my $mac = $net->{macaddr} 441 | or die "network interface '$iface' has no mac address\n"; 442 | 443 | $content .= "${i}$iface:\n"; 444 | $i .= ' '; 445 | $content .= "${i}match:\n" 446 | . "${i} macaddress: \"$mac\"\n" 447 | . "${i}set-name: eth$id\n"; 448 | my @addresses; 449 | if (defined(my $ip = $ipconfig->{ip})) { 450 | if ($ip eq 'dhcp') { 451 | $content .= "${i}dhcp4: true\n"; 452 | } else { 453 | push @addresses, $ip; 454 | } 455 | } 456 | if (defined(my $ip = $ipconfig->{ip6})) { 457 | if ($ip eq 'dhcp') { 458 | $content .= "${i}dhcp6: true\n"; 459 | } else { 460 | push @addresses, $ip; 461 | } 462 | } 463 | if (@addresses) { 464 | $content .= "${i}addresses:\n"; 465 | $content .= "${i}- '$_'\n" foreach @addresses; 466 | } 467 | if (defined(my $gw = $ipconfig->{gw})) { 468 | $content .= "${i}gateway4: '$gw'\n"; 469 | } 470 | if (defined(my $gw = $ipconfig->{gw6})) { 471 | $content .= "${i}gateway6: '$gw'\n"; 472 | } 473 | 474 | next if $dns_done; 475 | $dns_done = 1; 476 | 477 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 478 | if ($searchdomains || $nameservers) { 479 | $content .= "${i}nameservers:\n"; 480 | if (defined($nameservers) && @$nameservers) { 481 | $content .= "${i} addresses:\n"; 482 | $content .= "${i} - '$_'\n" foreach @$nameservers; 483 | } 484 | if (defined($searchdomains) && @$searchdomains) { 485 | $content .= "${i} search:\n"; 486 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 487 | } 488 | } 489 | } 490 | 491 | return $head.$content; 492 | } 493 | 494 | sub nocloud_network { 495 | my ($conf) = @_; 496 | 497 | my $content = "version: 1\n" 498 | . "config:\n"; 499 | 500 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 501 | foreach my $iface (sort @ifaces) { 502 | (my $id = $iface) =~ s/^net//; 503 | next if !$conf->{"ipconfig$id"}; 504 | 505 | # indentation - network interfaces are inside an 'ethernets' hash 506 | my $i = ' '; 507 | 508 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 509 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 510 | 511 | my $mac = lc($net->{macaddr}) 512 | or die "network interface '$iface' has no mac address\n"; 513 | 514 | $content .= "${i}- type: physical\n" 515 | . "${i} name: eth$id\n" 516 | . "${i} mac_address: '$mac'\n" 517 | . "${i} subnets:\n"; 518 | $i .= ' '; 519 | if (defined(my $ip = $ipconfig->{ip})) { 520 | if ($ip eq 'dhcp') { 521 | $content .= "${i}- type: dhcp4\n"; 522 | } else { 523 | my ($addr, $mask) = split_ip4($ip); 524 | $content .= "${i}- type: static\n" 525 | . "${i} address: '$addr'\n" 526 | . "${i} netmask: '$mask'\n"; 527 | if (defined(my $gw = $ipconfig->{gw})) { 528 | $content .= "${i} gateway: '$gw'\n"; 529 | } 530 | } 531 | } 532 | if (defined(my $ip = $ipconfig->{ip6})) { 533 | if ($ip eq 'dhcp') { 534 | $content .= "${i}- type: dhcp6\n"; 535 | } elsif ($ip eq 'auto') { 536 | # SLAAC is only supported by cloud-init since 19.4 537 | $content .= "${i}- type: ipv6_slaac\n"; 538 | } else { 539 | $content .= "${i}- type: static6\n" 540 | . "${i} address: '$ip'\n"; 541 | if (defined(my $gw = $ipconfig->{gw6})) { 542 | $content .= "${i} gateway: '$gw'\n"; 543 | } 544 | } 545 | } 546 | } 547 | 548 | my $i = ' '; 549 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 550 | if ($searchdomains || $nameservers) { 551 | $content .= "${i}- type: nameserver\n"; 552 | if (defined($nameservers) && @$nameservers) { 553 | $content .= "${i} address:\n"; 554 | $content .= "${i} - '$_'\n" foreach @$nameservers; 555 | } 556 | if (defined($searchdomains) && @$searchdomains) { 557 | $content .= "${i} search:\n"; 558 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 559 | } 560 | } 561 | 562 | return $content; 563 | } 564 | 565 | sub nocloud_metadata { 566 | my ($uuid) = @_; 567 | return "instance-id: $uuid\n"; 568 | } 569 | 570 | sub nocloud_gen_metadata { 571 | my ($user, $network) = @_; 572 | 573 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 574 | return nocloud_metadata($uuid_str); 575 | } 576 | 577 | sub generate_nocloud { 578 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 579 | 580 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 581 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 582 | $network_data = nocloud_network($conf) if !defined($network_data); 583 | $vendor_data = '' if !defined($vendor_data); 584 | 585 | if (!defined($meta_data)) { 586 | $meta_data = nocloud_gen_metadata($user_data, $network_data); 587 | } 588 | 589 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 590 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 591 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 592 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 593 | 594 | my $files = { 595 | '/user-data' => $user_data, 596 | '/network-config' => $network_data, 597 | '/meta-data' => $meta_data, 598 | '/vendor-data' => $vendor_data 599 | }; 600 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata'); 601 | } 602 | 603 | sub get_custom_cloudinit_files { 604 | my ($conf) = @_; 605 | 606 | my $cicustom = $conf->{cicustom}; 607 | my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {}; 608 | 609 | my $network_volid = $files->{network}; 610 | my $user_volid = $files->{user}; 611 | my $meta_volid = $files->{meta}; 612 | my $vendor_volid = $files->{vendor}; 613 | 614 | my $storage_conf = PVE::Storage::config(); 615 | 616 | my $network_data; 617 | if ($network_volid) { 618 | $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid); 619 | } 620 | 621 | my $user_data; 622 | if ($user_volid) { 623 | $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid); 624 | } 625 | 626 | my $meta_data; 627 | if ($meta_volid) { 628 | $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid); 629 | } 630 | 631 | my $vendor_data; 632 | if ($vendor_volid) { 633 | $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid); 634 | } 635 | 636 | return ($user_data, $network_data, $meta_data, $vendor_data); 637 | } 638 | 639 | sub read_cloudinit_snippets_file { 640 | my ($storage_conf, $volid) = @_; 641 | 642 | my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid); 643 | die "$volid is not in the snippets directory\n" if $type ne 'snippets'; 644 | return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024); 645 | } 646 | 647 | my $cloudinit_methods = { 648 | configdrive2 => \&generate_configdrive2, 649 | nocloud => \&generate_nocloud, 650 | opennebula => \&generate_opennebula, 651 | }; 652 | 653 | sub has_changes { 654 | my ($conf) = @_; 655 | 656 | return !!$conf->{cloudinit}->%*; 657 | } 658 | 659 | sub generate_cloudinit_config { 660 | my ($conf, $vmid) = @_; 661 | 662 | my $format = get_cloudinit_format($conf); 663 | 664 | my $has_changes = has_changes($conf); 665 | 666 | PVE::QemuConfig->foreach_volume($conf, sub { 667 | my ($ds, $drive) = @_; 668 | 669 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); 670 | 671 | return if !$volname || $volname !~ m/vm-$vmid-cloudinit/; 672 | 673 | my $generator = $cloudinit_methods->{$format} 674 | or die "missing cloudinit methods for format '$format'\n"; 675 | 676 | $generator->($conf, $vmid, $drive, $volname, $storeid); 677 | }); 678 | 679 | return $has_changes; 680 | } 681 | 682 | sub apply_cloudinit_config { 683 | my ($conf, $vmid) = @_; 684 | 685 | my $has_changes = generate_cloudinit_config($conf, $vmid); 686 | 687 | if ($has_changes) { 688 | delete $conf->{cloudinit}; 689 | PVE::QemuConfig->write_config($vmid, $conf); 690 | return 1; 691 | } 692 | 693 | return $has_changes; 694 | } 695 | 696 | sub dump_cloudinit_config { 697 | my ($conf, $vmid, $type) = @_; 698 | 699 | my $format = get_cloudinit_format($conf); 700 | 701 | if ($type eq 'user') { 702 | return cloudinit_userdata($conf, $vmid); 703 | } elsif ($type eq 'network') { 704 | if ($format eq 'nocloud') { 705 | return nocloud_network($conf); 706 | } else { 707 | return configdrive2_network($conf); 708 | } 709 | } else { # metadata config 710 | my $user = cloudinit_userdata($conf, $vmid); 711 | if ($format eq 'nocloud') { 712 | my $network = nocloud_network($conf); 713 | return nocloud_gen_metadata($user, $network); 714 | } else { 715 | my $network = configdrive2_network($conf); 716 | return configdrive2_gen_metadata($conf, $vmid, $user, $network); 717 | } 718 | } 719 | } 720 | 721 | 1; 722 | -------------------------------------------------------------------------------- /pve7.4.3-patch/Cloudinit.pm: -------------------------------------------------------------------------------- 1 | package PVE::QemuServer::Cloudinit; 2 | 3 | use strict; 4 | use warnings; 5 | 6 | use File::Path; 7 | use Digest::SHA; 8 | use URI::Escape; 9 | use MIME::Base64 qw(encode_base64); 10 | use Storable qw(dclone); 11 | 12 | use PVE::Tools qw(run_command file_set_contents); 13 | use PVE::Storage; 14 | use PVE::QemuServer; 15 | use PVE::QemuServer::Helpers; 16 | 17 | use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes 18 | 19 | sub commit_cloudinit_disk { 20 | my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_; 21 | 22 | my $path = "/run/pve/cloudinit/$vmid/"; 23 | mkpath $path; 24 | foreach my $filepath (keys %$files) { 25 | if ($filepath !~ m@^(.*)\/[^/]+$@) { 26 | die "internal error: bad file name in cloud-init image: $filepath\n"; 27 | } 28 | my $dirname = $1; 29 | mkpath "$path/$dirname"; 30 | 31 | my $contents = $files->{$filepath}; 32 | file_set_contents("$path/$filepath", $contents); 33 | } 34 | 35 | my $storecfg = PVE::Storage::config(); 36 | my $iso_path = PVE::Storage::path($storecfg, $drive->{file}); 37 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); 38 | my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); 39 | 40 | my $size = eval { PVE::Storage::volume_size_info($storecfg, $drive->{file}) }; 41 | if (!defined($size) || $size <= 0) { 42 | $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/; 43 | my $name = $1; 44 | $size = 4 * 1024; 45 | PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size); 46 | $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte 47 | } 48 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); 49 | $plugin->activate_volume($storeid, $scfg, $volname); 50 | 51 | print "generating cloud-init ISO\n"; 52 | eval { 53 | run_command([ 54 | ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path], 55 | ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"] 56 | ]); 57 | }; 58 | my $err = $@; 59 | rmtree($path); 60 | die $err if $err; 61 | } 62 | 63 | sub get_cloudinit_format { 64 | my ($conf) = @_; 65 | if (defined(my $format = $conf->{citype})) { 66 | return $format; 67 | } 68 | 69 | # No format specified, default based on ostype because windows' 70 | # cloudbased-init only supports configdrivev2, whereas on linux we need 71 | # to use mac addresses because regular cloudinit doesn't map 'ethX' to 72 | # the new predicatble network device naming scheme. 73 | if (defined(my $ostype = $conf->{ostype})) { 74 | return 'configdrive2' 75 | if PVE::QemuServer::Helpers::windows_version($ostype); 76 | } 77 | 78 | return 'nocloud'; 79 | } 80 | 81 | sub get_hostname_fqdn { 82 | my ($conf, $vmid) = @_; 83 | my $hostname = $conf->{name} // "VM$vmid"; 84 | my $fqdn; 85 | if ($hostname =~ /\./) { 86 | $fqdn = $hostname; 87 | $hostname =~ s/\..*$//; 88 | } elsif (my $search = $conf->{searchdomain}) { 89 | $fqdn = "$hostname.$search"; 90 | } 91 | return ($hostname, $fqdn); 92 | } 93 | 94 | sub get_dns_conf { 95 | my ($conf) = @_; 96 | 97 | # Same logic as in pve-container, but without the testcase special case 98 | my $host_resolv_conf = PVE::INotify::read_file('resolvconf'); 99 | 100 | my $searchdomains = [ 101 | split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search}) 102 | ]; 103 | 104 | my $nameserver = $conf->{nameserver}; 105 | if (!defined($nameserver)) { 106 | $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}]; 107 | } else { 108 | $nameserver = [split(/\s+/, $nameserver)]; 109 | } 110 | 111 | return ($searchdomains, $nameserver); 112 | } 113 | 114 | sub cloudinit_userdata { 115 | my ($conf, $vmid) = @_; 116 | 117 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 118 | 119 | my $content = "#cloud-config\n"; 120 | 121 | $content .= "hostname: $hostname\n"; 122 | $content .= "manage_etc_hosts: true\n"; 123 | $content .= "fqdn: $fqdn\n" if defined($fqdn); 124 | 125 | my $username = $conf->{ciuser}; 126 | my $password = $conf->{cipassword}; 127 | 128 | $content .= "user: $username\n" if defined($username); 129 | $content .= "disable_root: False\n" if defined($username) && $username eq 'root'; 130 | $content .= "password: $password\n" if defined($password); 131 | 132 | if (defined(my $keys = $conf->{sshkeys})) { 133 | $keys = URI::Escape::uri_unescape($keys); 134 | $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)]; 135 | $keys = [grep { /\S/ } @$keys]; 136 | $content .= "ssh_authorized_keys:\n"; 137 | foreach my $k (@$keys) { 138 | $content .= " - $k\n"; 139 | } 140 | } 141 | $content .= "chpasswd:\n"; 142 | $content .= " expire: False\n"; 143 | 144 | if (!defined($username) || $username ne 'root') { 145 | $content .= "users:\n"; 146 | $content .= " - default\n"; 147 | } 148 | 149 | $content .= "package_upgrade: true\n"; 150 | 151 | return $content; 152 | } 153 | 154 | sub split_ip4 { 155 | my ($ip) = @_; 156 | my ($addr, $mask) = split('/', $ip); 157 | die "not a CIDR: $ip\n" if !defined $mask; 158 | return ($addr, $PVE::Network::ipv4_reverse_mask->[$mask]); 159 | } 160 | 161 | sub configdrive2_network { 162 | my ($conf) = @_; 163 | 164 | my $content = "auto lo\n"; 165 | $content .= "iface lo inet loopback\n\n"; 166 | 167 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 168 | 169 | my $ostype = $conf->{"ostype"}; 170 | my $default_dns = ''; 171 | my $default_search = ''; 172 | 173 | if ($nameservers && @$nameservers) { 174 | $nameservers = join(' ', @$nameservers); 175 | $content .= " dns_nameservers $nameservers\n"; 176 | $default_dns = $nameservers; 177 | } 178 | if ($searchdomains && @$searchdomains) { 179 | $searchdomains = join(' ', @$searchdomains); 180 | $content .= " dns_search $searchdomains\n"; 181 | $default_search = $searchdomains; 182 | } 183 | 184 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 185 | foreach my $iface (sort @ifaces) { 186 | (my $id = $iface) =~ s/^net//; 187 | next if !$conf->{"ipconfig$id"}; 188 | my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 189 | $id = "eth$id"; 190 | 191 | $content .="auto $id\n"; 192 | if ($net->{ip}) { 193 | if ($net->{ip} eq 'dhcp') { 194 | $content .= "iface $id inet dhcp\n"; 195 | } else { 196 | my ($addr, $mask) = split_ip4($net->{ip}); 197 | $content .= "iface $id inet static\n"; 198 | $content .= " address $addr\n"; 199 | $content .= " netmask $mask\n"; 200 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 201 | if(PVE::QemuServer::windows_version($ostype) && ($id eq "eth0")) { 202 | $content .= " dns-nameservers $default_dns\n"; 203 | $content .= " dns-search $default_search\n"; 204 | } 205 | } 206 | } 207 | if ($net->{ip6}) { 208 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { 209 | $content .= "iface $id inet6 $1\n"; 210 | } else { 211 | my ($addr, $mask) = split('/', $net->{ip6}); 212 | $content .= "iface $id inet6 static\n"; 213 | $content .= " address $addr\n"; 214 | $content .= " netmask $mask\n"; 215 | $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 216 | } 217 | } 218 | } 219 | 220 | return $content; 221 | } 222 | 223 | sub get_mac_addresses { 224 | my ($conf) = @_; 225 | 226 | my $dhcpstring = undef; 227 | my @dhcpmacs = (); 228 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 229 | 230 | foreach my $iface (sort @ifaces) { 231 | (my $id = $iface) =~ s/^net//; 232 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 233 | next if !$conf->{"ipconfig$id"}; 234 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 235 | 236 | my $mac = lc $net->{macaddr}; 237 | 238 | if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 239 | push @dhcpmacs, $mac; 240 | } 241 | } 242 | 243 | if (@dhcpmacs){ 244 | $dhcpstring = ",\n \"dhcp\":["; 245 | foreach my $mac (@dhcpmacs){ 246 | if ($mac != @dhcpmacs[-1]){ 247 | $dhcpstring .= "\"$mac\","; 248 | } 249 | else{ 250 | $dhcpstring .= "\"$mac\"]"; 251 | } 252 | } 253 | } 254 | return ($dhcpstring); 255 | } 256 | 257 | sub configdrive2_gen_metadata { 258 | my ($conf, $vmid, $user, $network) = @_; 259 | 260 | # Get mac addresses of dhcp nics from conf file 261 | my $dhcpmacs = undef; 262 | $dhcpmacs = get_mac_addresses($conf); 263 | 264 | # Get UUID 265 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 266 | 267 | # Get hostname 268 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 269 | 270 | # Get username, default to Administrator if none 271 | my $username = undef; 272 | if (defined($conf->{ciuser})){ 273 | my $name = $conf->{ciuser}; 274 | $username = ",\n \"admin_username\": \"$name\"" 275 | } 276 | 277 | # Get user password 278 | my $password = $conf->{cipassword}; 279 | 280 | # Get ssh keys and make a list out of it in json format 281 | my $keystring = undef; 282 | my $pubkeys = $conf->{sshkeys}; 283 | $pubkeys = URI::Escape::uri_unescape($pubkeys); 284 | my @pubkeysarray = split "\n", $pubkeys; 285 | if (@pubkeysarray) { 286 | my $arraylength = @pubkeysarray; 287 | my $incrementer = 1; 288 | $keystring =",\n \"public_keys\": {\n"; 289 | for my $key (@pubkeysarray){ 290 | $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 291 | if ($arraylength != $incrementer){ 292 | $keystring .= ",\n"; 293 | }else{ 294 | $keystring .= "\n }"; 295 | } 296 | $incrementer++; 297 | } 298 | } 299 | 300 | return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 301 | } 302 | 303 | sub configdrive2_metadata { 304 | my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 305 | return <<"EOF"; 306 | { 307 | "meta":{ 308 | "admin_pass": "$password"$username 309 | }, 310 | "uuid":"$uuid", 311 | "hostname":"$hostname", 312 | "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs 313 | } 314 | EOF 315 | } 316 | 317 | sub generate_configdrive2 { 318 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 319 | 320 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 321 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 322 | $network_data = configdrive2_network($conf) if !defined($network_data); 323 | $vendor_data = '' if !defined($vendor_data); 324 | 325 | if (!defined($meta_data)) { 326 | $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 327 | } 328 | 329 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 330 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 331 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 332 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 333 | 334 | my $files = { 335 | '/openstack/latest/user_data' => $user_data, 336 | '/openstack/content/0000' => $network_data, 337 | '/openstack/latest/meta_data.json' => $meta_data, 338 | '/openstack/latest/vendor_data.json' => $vendor_data 339 | }; 340 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2'); 341 | } 342 | 343 | sub generate_opennebula { 344 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 345 | 346 | my $content = ""; 347 | 348 | my $username = $conf->{ciuser} || "root"; 349 | $content .= "USERNAME=$username\n" if defined($username); 350 | 351 | if (defined(my $password = $conf->{cipassword})) { 352 | $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64($password) ."\n"; 353 | } 354 | 355 | if (defined($conf->{sshkeys})) { 356 | my $keys = [ split(/\s*\n\s*/, URI::Escape::uri_unescape($conf->{sshkeys})) ]; 357 | $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n"; 358 | } 359 | 360 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 361 | $content .= "SET_HOSTNAME=$hostname\n"; 362 | 363 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 364 | $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers; 365 | $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains; 366 | 367 | my $networkenabled = undef; 368 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 369 | foreach my $iface (sort @ifaces) { 370 | (my $id = $iface) =~ s/^net//; 371 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 372 | next if !$conf->{"ipconfig$id"}; 373 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 374 | my $ethid = "ETH$id"; 375 | 376 | my $mac = lc $net->{hwaddr}; 377 | 378 | if ($ipconfig->{ip}) { 379 | $networkenabled = 1; 380 | 381 | if ($ipconfig->{ip} eq 'dhcp') { 382 | $content .= "${ethid}_DHCP=YES\n"; 383 | } else { 384 | my ($addr, $mask) = split_ip4($ipconfig->{ip}); 385 | $content .= "${ethid}_IP=$addr\n"; 386 | $content .= "${ethid}_MASK=$mask\n"; 387 | $content .= "${ethid}_MAC=$mac\n"; 388 | $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw}; 389 | } 390 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 391 | } 392 | 393 | if ($ipconfig->{ip6}) { 394 | $networkenabled = 1; 395 | if ($ipconfig->{ip6} eq 'dhcp') { 396 | $content .= "${ethid}_DHCP6=YES\n"; 397 | } elsif ($ipconfig->{ip6} eq 'auto') { 398 | $content .= "${ethid}_AUTO6=YES\n"; 399 | } else { 400 | my ($addr, $mask) = split('/', $ipconfig->{ip6}); 401 | $content .= "${ethid}_IP6=$addr\n"; 402 | $content .= "${ethid}_MASK6=$mask\n"; 403 | $content .= "${ethid}_MAC6=$mac\n"; 404 | $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6}; 405 | } 406 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 407 | } 408 | } 409 | 410 | $content .= "NETWORK=YES\n" if $networkenabled; 411 | 412 | my $files = { '/context.sh' => $content }; 413 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT'); 414 | } 415 | 416 | sub nocloud_network_v2 { 417 | my ($conf) = @_; 418 | 419 | my $content = ''; 420 | 421 | my $head = "version: 2\n" 422 | . "ethernets:\n"; 423 | 424 | my $dns_done; 425 | 426 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 427 | foreach my $iface (sort @ifaces) { 428 | (my $id = $iface) =~ s/^net//; 429 | next if !$conf->{"ipconfig$id"}; 430 | 431 | # indentation - network interfaces are inside an 'ethernets' hash 432 | my $i = ' '; 433 | 434 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 435 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 436 | 437 | my $mac = $net->{macaddr} 438 | or die "network interface '$iface' has no mac address\n"; 439 | 440 | $content .= "${i}$iface:\n"; 441 | $i .= ' '; 442 | $content .= "${i}match:\n" 443 | . "${i} macaddress: \"$mac\"\n" 444 | . "${i}set-name: eth$id\n"; 445 | my @addresses; 446 | if (defined(my $ip = $ipconfig->{ip})) { 447 | if ($ip eq 'dhcp') { 448 | $content .= "${i}dhcp4: true\n"; 449 | } else { 450 | push @addresses, $ip; 451 | } 452 | } 453 | if (defined(my $ip = $ipconfig->{ip6})) { 454 | if ($ip eq 'dhcp') { 455 | $content .= "${i}dhcp6: true\n"; 456 | } else { 457 | push @addresses, $ip; 458 | } 459 | } 460 | if (@addresses) { 461 | $content .= "${i}addresses:\n"; 462 | $content .= "${i}- '$_'\n" foreach @addresses; 463 | } 464 | if (defined(my $gw = $ipconfig->{gw})) { 465 | $content .= "${i}gateway4: '$gw'\n"; 466 | } 467 | if (defined(my $gw = $ipconfig->{gw6})) { 468 | $content .= "${i}gateway6: '$gw'\n"; 469 | } 470 | 471 | next if $dns_done; 472 | $dns_done = 1; 473 | 474 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 475 | if ($searchdomains || $nameservers) { 476 | $content .= "${i}nameservers:\n"; 477 | if (defined($nameservers) && @$nameservers) { 478 | $content .= "${i} addresses:\n"; 479 | $content .= "${i} - '$_'\n" foreach @$nameservers; 480 | } 481 | if (defined($searchdomains) && @$searchdomains) { 482 | $content .= "${i} search:\n"; 483 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 484 | } 485 | } 486 | } 487 | 488 | return $head.$content; 489 | } 490 | 491 | sub nocloud_network { 492 | my ($conf) = @_; 493 | 494 | my $content = "version: 1\n" 495 | . "config:\n"; 496 | 497 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 498 | foreach my $iface (sort @ifaces) { 499 | (my $id = $iface) =~ s/^net//; 500 | next if !$conf->{"ipconfig$id"}; 501 | 502 | # indentation - network interfaces are inside an 'ethernets' hash 503 | my $i = ' '; 504 | 505 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 506 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 507 | 508 | my $mac = lc($net->{macaddr}) 509 | or die "network interface '$iface' has no mac address\n"; 510 | 511 | $content .= "${i}- type: physical\n" 512 | . "${i} name: eth$id\n" 513 | . "${i} mac_address: '$mac'\n" 514 | . "${i} subnets:\n"; 515 | $i .= ' '; 516 | if (defined(my $ip = $ipconfig->{ip})) { 517 | if ($ip eq 'dhcp') { 518 | $content .= "${i}- type: dhcp4\n"; 519 | } else { 520 | my ($addr, $mask) = split_ip4($ip); 521 | $content .= "${i}- type: static\n" 522 | . "${i} address: '$addr'\n" 523 | . "${i} netmask: '$mask'\n"; 524 | if (defined(my $gw = $ipconfig->{gw})) { 525 | $content .= "${i} gateway: '$gw'\n"; 526 | } 527 | } 528 | } 529 | if (defined(my $ip = $ipconfig->{ip6})) { 530 | if ($ip eq 'dhcp') { 531 | $content .= "${i}- type: dhcp6\n"; 532 | } elsif ($ip eq 'auto') { 533 | # SLAAC is only supported by cloud-init since 19.4 534 | $content .= "${i}- type: ipv6_slaac\n"; 535 | } else { 536 | $content .= "${i}- type: static6\n" 537 | . "${i} address: '$ip'\n"; 538 | if (defined(my $gw = $ipconfig->{gw6})) { 539 | $content .= "${i} gateway: '$gw'\n"; 540 | } 541 | } 542 | } 543 | } 544 | 545 | my $i = ' '; 546 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 547 | if ($searchdomains || $nameservers) { 548 | $content .= "${i}- type: nameserver\n"; 549 | if (defined($nameservers) && @$nameservers) { 550 | $content .= "${i} address:\n"; 551 | $content .= "${i} - '$_'\n" foreach @$nameservers; 552 | } 553 | if (defined($searchdomains) && @$searchdomains) { 554 | $content .= "${i} search:\n"; 555 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 556 | } 557 | } 558 | 559 | return $content; 560 | } 561 | 562 | sub nocloud_metadata { 563 | my ($uuid) = @_; 564 | return "instance-id: $uuid\n"; 565 | } 566 | 567 | sub nocloud_gen_metadata { 568 | my ($user, $network) = @_; 569 | 570 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 571 | return nocloud_metadata($uuid_str); 572 | } 573 | 574 | sub generate_nocloud { 575 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 576 | 577 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 578 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 579 | $network_data = nocloud_network($conf) if !defined($network_data); 580 | $vendor_data = '' if !defined($vendor_data); 581 | 582 | if (!defined($meta_data)) { 583 | $meta_data = nocloud_gen_metadata($user_data, $network_data); 584 | } 585 | 586 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 587 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 588 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 589 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 590 | 591 | my $files = { 592 | '/user-data' => $user_data, 593 | '/network-config' => $network_data, 594 | '/meta-data' => $meta_data, 595 | '/vendor-data' => $vendor_data 596 | }; 597 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata'); 598 | } 599 | 600 | sub get_custom_cloudinit_files { 601 | my ($conf) = @_; 602 | 603 | my $cicustom = $conf->{cicustom}; 604 | my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {}; 605 | 606 | my $network_volid = $files->{network}; 607 | my $user_volid = $files->{user}; 608 | my $meta_volid = $files->{meta}; 609 | my $vendor_volid = $files->{vendor}; 610 | 611 | my $storage_conf = PVE::Storage::config(); 612 | 613 | my $network_data; 614 | if ($network_volid) { 615 | $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid); 616 | } 617 | 618 | my $user_data; 619 | if ($user_volid) { 620 | $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid); 621 | } 622 | 623 | my $meta_data; 624 | if ($meta_volid) { 625 | $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid); 626 | } 627 | 628 | my $vendor_data; 629 | if ($vendor_volid) { 630 | $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid); 631 | } 632 | 633 | return ($user_data, $network_data, $meta_data, $vendor_data); 634 | } 635 | 636 | sub read_cloudinit_snippets_file { 637 | my ($storage_conf, $volid) = @_; 638 | 639 | my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid); 640 | die "$volid is not in the snippets directory\n" if $type ne 'snippets'; 641 | return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024); 642 | } 643 | 644 | my $cloudinit_methods = { 645 | configdrive2 => \&generate_configdrive2, 646 | nocloud => \&generate_nocloud, 647 | opennebula => \&generate_opennebula, 648 | }; 649 | 650 | sub has_changes { 651 | my ($conf) = @_; 652 | 653 | return !!$conf->{cloudinit}->%*; 654 | } 655 | 656 | sub generate_cloudinit_config { 657 | my ($conf, $vmid) = @_; 658 | 659 | my $format = get_cloudinit_format($conf); 660 | 661 | my $has_changes = has_changes($conf); 662 | 663 | PVE::QemuConfig->foreach_volume($conf, sub { 664 | my ($ds, $drive) = @_; 665 | 666 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); 667 | 668 | return if !$volname || $volname !~ m/vm-$vmid-cloudinit/; 669 | 670 | my $generator = $cloudinit_methods->{$format} 671 | or die "missing cloudinit methods for format '$format'\n"; 672 | 673 | $generator->($conf, $vmid, $drive, $volname, $storeid); 674 | }); 675 | 676 | return $has_changes; 677 | } 678 | 679 | sub apply_cloudinit_config { 680 | my ($conf, $vmid) = @_; 681 | 682 | my $has_changes = generate_cloudinit_config($conf, $vmid); 683 | 684 | if ($has_changes) { 685 | delete $conf->{cloudinit}; 686 | PVE::QemuConfig->write_config($vmid, $conf); 687 | return 1; 688 | } 689 | 690 | return $has_changes; 691 | } 692 | 693 | sub dump_cloudinit_config { 694 | my ($conf, $vmid, $type) = @_; 695 | 696 | my $format = get_cloudinit_format($conf); 697 | 698 | if ($type eq 'user') { 699 | return cloudinit_userdata($conf, $vmid); 700 | } elsif ($type eq 'network') { 701 | if ($format eq 'nocloud') { 702 | return nocloud_network($conf); 703 | } else { 704 | return configdrive2_network($conf); 705 | } 706 | } else { # metadata config 707 | my $user = cloudinit_userdata($conf, $vmid); 708 | if ($format eq 'nocloud') { 709 | my $network = nocloud_network($conf); 710 | return nocloud_gen_metadata($user, $network); 711 | } else { 712 | my $network = configdrive2_network($conf); 713 | return configdrive2_gen_metadata($conf, $vmid, $user, $network); 714 | } 715 | } 716 | } 717 | 718 | 1; 719 | -------------------------------------------------------------------------------- /qemu-server-6.4-2/Cloudinit.pm.patch: -------------------------------------------------------------------------------- 1 | --- /usr/share/perl5/PVE/QemuServer/Cloudinit.pm.orig 2022-01-11 18:58:17.840849655 +0100 2 | +++ /usr/share/perl5/PVE/QemuServer/Cloudinit.pm 2022-01-18 10:53:41.035133131 +0100 3 | @@ -159,6 +159,12 @@ 4 | sub configdrive2_network { 5 | my ($conf) = @_; 6 | 7 | + ## Support Windows 8 | + my $ostype = $conf->{"ostype"}; 9 | + my $default_dns = ''; 10 | + my $default_search = ''; 11 | + ## 12 | + 13 | my $content = "auto lo\n"; 14 | $content .= "iface lo inet loopback\n\n"; 15 | 16 | @@ -166,12 +172,15 @@ 17 | if ($nameservers && @$nameservers) { 18 | $nameservers = join(' ', @$nameservers); 19 | $content .= " dns_nameservers $nameservers\n"; 20 | + $default_dns = $nameservers; # Support windows 21 | } 22 | if ($searchdomains && @$searchdomains) { 23 | $searchdomains = join(' ', @$searchdomains); 24 | $content .= " dns_search $searchdomains\n"; 25 | + $default_search = $searchdomains; # Support Windows 26 | } 27 | - 28 | + 29 | + my $dnsinserted = 0; # Insert dns once for Windows machines 30 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 31 | foreach my $iface (sort @ifaces) { 32 | (my $id = $iface) =~ s/^net//; 33 | @@ -189,6 +198,14 @@ 34 | $content .= " address $addr\n"; 35 | $content .= " netmask $mask\n"; 36 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 37 | + 38 | + ## Support windows 39 | + if(PVE::QemuServer::windows_version($ostype) && not($dnsinserted)) { 40 | + $content .= " dns-nameservers $default_dns\n"; 41 | + $content .= " dns-search $default_search\n"; 42 | + $dnsinserted++; 43 | + } 44 | + ## 45 | } 46 | } 47 | if ($net->{ip6}) { 48 | @@ -207,19 +224,97 @@ 49 | return $content; 50 | } 51 | 52 | +# Get mac addresses from Conf file 53 | +sub get_mac_addresses { 54 | + my ($conf) = @_; 55 | + 56 | + my $dhcpstring = undef; 57 | + my @dhcpmacs = (); 58 | + my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 59 | + 60 | + foreach my $iface (sort @ifaces) { 61 | + (my $id = $iface) =~ s/^net//; 62 | + my $net = PVE::QemuServer::parse_net($conf->{$iface}); 63 | + next if !$conf->{"ipconfig$id"}; 64 | + my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 65 | + 66 | + my $mac = lc $net->{macaddr}; 67 | + 68 | + if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 69 | + push @dhcpmacs, $mac; 70 | + } 71 | + } 72 | + 73 | + if (@dhcpmacs){ 74 | + $dhcpstring = ",\n \"dhcp\":["; 75 | + foreach my $mac (@dhcpmacs){ 76 | + if ($mac != @dhcpmacs[-1]){ 77 | + $dhcpstring .= "\"$mac\","; 78 | + } 79 | + else{ 80 | + $dhcpstring .= "\"$mac\"]"; 81 | + } 82 | + } 83 | + } 84 | + return ($dhcpstring); 85 | +} 86 | + 87 | sub configdrive2_gen_metadata { 88 | - my ($user, $network) = @_; 89 | + my ($conf, $vmid, $user, $network) = @_; 90 | + 91 | + # Get mac addresses of dhcp nics from conf file 92 | + my $dhcpmacs = undef; 93 | + $dhcpmacs = get_mac_addresses($conf); 94 | 95 | + # Get UUID 96 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 97 | - return configdrive2_metadata($uuid_str); 98 | + 99 | + # Get hostname 100 | + my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 101 | + 102 | + # Get username, default to Administrator if none 103 | + my $username = undef; 104 | + if (defined($conf->{ciuser})){ 105 | + my $name = $conf->{ciuser}; 106 | + $username = ",\n \"admin_username\": \"$name\"" 107 | + } 108 | + 109 | + # Get user password 110 | + my $password = $conf->{cipassword}; 111 | + 112 | + # Get ssh keys and make a list out of it in json format 113 | + my $keystring = undef; 114 | + my $pubkeys = $conf->{sshkeys}; 115 | + $pubkeys = URI::Escape::uri_unescape($pubkeys); 116 | + my @pubkeysarray = split "\n", $pubkeys; 117 | + if (@pubkeysarray) { 118 | + my $arraylength = @pubkeysarray; 119 | + my $incrementer = 1; 120 | + $keystring =",\n \"public_keys\": {\n"; 121 | + for my $key (@pubkeysarray){ 122 | + $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 123 | + if ($arraylength != $incrementer){ 124 | + $keystring .= ",\n"; 125 | + }else{ 126 | + $keystring .= "\n }"; 127 | + } 128 | + $incrementer++; 129 | + } 130 | + } 131 | + 132 | + return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 133 | } 134 | 135 | sub configdrive2_metadata { 136 | - my ($uuid) = @_; 137 | + my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 138 | return <<"EOF"; 139 | { 140 | - "uuid": "$uuid", 141 | - "network_config": { "content_path": "/content/0000" } 142 | + "meta":{ 143 | + "admin_pass": "$password"$username 144 | + }, 145 | + "uuid":"$uuid", 146 | + "hostname":"$hostname", 147 | + "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs 148 | } 149 | EOF 150 | } 151 | @@ -232,7 +327,7 @@ 152 | $network_data = configdrive2_network($conf) if !defined($network_data); 153 | 154 | if (!defined($meta_data)) { 155 | - $meta_data = configdrive2_gen_metadata($user_data, $network_data); 156 | + $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 157 | } 158 | my $files = { 159 | '/openstack/latest/user_data' => $user_data, 160 | -------------------------------------------------------------------------------- /qemu-server-6.4-2/Qemu.pm.patch: -------------------------------------------------------------------------------- 1 | --- /usr/share/perl5/PVE/API2/Qemu.pm.orig 2021-04-29 16:16:04.000000000 +0200 2 | +++ /usr/share/perl5/PVE/API2/Qemu.pm 2022-01-11 16:24:26.638619060 +0100 3 | @@ -1084,10 +1084,17 @@ 4 | 5 | my $background_delay = extract_param($param, 'background_delay'); 6 | 7 | + my $conf = PVE::QemuConfig->load_config($vmid); 8 | + 9 | + my $ostype = $conf->{ostype}; 10 | + 11 | + 12 | if (defined(my $cipassword = $param->{cipassword})) { 13 | # Same logic as in cloud-init (but with the regex fixed...) 14 | - $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) 15 | - if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/; 16 | + if (!(PVE::QemuServer::windows_version($ostype))) { 17 | + $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) 18 | + if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/; 19 | + } 20 | } 21 | 22 | my @paramarr = (); # used for log message 23 | -------------------------------------------------------------------------------- /qemu-server-6.4-2/sourcefiles/Cloudinit.pm: -------------------------------------------------------------------------------- 1 | package PVE::QemuServer::Cloudinit; 2 | 3 | use strict; 4 | use warnings; 5 | 6 | use File::Path; 7 | use Digest::SHA; 8 | use URI::Escape; 9 | use MIME::Base64 qw(encode_base64); 10 | 11 | use PVE::Tools qw(run_command file_set_contents); 12 | use PVE::Storage; 13 | use PVE::QemuServer; 14 | 15 | use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes 16 | 17 | sub commit_cloudinit_disk { 18 | my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_; 19 | 20 | my $path = "/run/pve/cloudinit/$vmid/"; 21 | mkpath $path; 22 | foreach my $filepath (keys %$files) { 23 | if ($filepath !~ m@^(.*)\/[^/]+$@) { 24 | die "internal error: bad file name in cloud-init image: $filepath\n"; 25 | } 26 | my $dirname = $1; 27 | mkpath "$path/$dirname"; 28 | 29 | my $contents = $files->{$filepath}; 30 | file_set_contents("$path/$filepath", $contents); 31 | } 32 | 33 | my $storecfg = PVE::Storage::config(); 34 | my $iso_path = PVE::Storage::path($storecfg, $drive->{file}); 35 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); 36 | my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); 37 | 38 | my $size = eval { PVE::Storage::volume_size_info($storecfg, $drive->{file}) }; 39 | if (!defined($size) || $size <= 0) { 40 | $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/; 41 | my $name = $1; 42 | $size = 4 * 1024; 43 | PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size); 44 | $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte 45 | } 46 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); 47 | $plugin->activate_volume($storeid, $scfg, $volname); 48 | 49 | print "generating cloud-init ISO\n"; 50 | eval { 51 | run_command([ 52 | ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path], 53 | ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"] 54 | ]); 55 | }; 56 | my $err = $@; 57 | rmtree($path); 58 | die $err if $err; 59 | } 60 | 61 | sub get_cloudinit_format { 62 | my ($conf) = @_; 63 | if (defined(my $format = $conf->{citype})) { 64 | return $format; 65 | } 66 | 67 | # No format specified, default based on ostype because windows' 68 | # cloudbased-init only supports configdrivev2, whereas on linux we need 69 | # to use mac addresses because regular cloudinit doesn't map 'ethX' to 70 | # the new predicatble network device naming scheme. 71 | if (defined(my $ostype = $conf->{ostype})) { 72 | return 'configdrive2' 73 | if PVE::QemuServer::windows_version($ostype); 74 | } 75 | 76 | return 'nocloud'; 77 | } 78 | 79 | sub get_hostname_fqdn { 80 | my ($conf, $vmid) = @_; 81 | my $hostname = $conf->{name} // "VM$vmid"; 82 | my $fqdn; 83 | if ($hostname =~ /\./) { 84 | $fqdn = $hostname; 85 | $hostname =~ s/\..*$//; 86 | } elsif (my $search = $conf->{searchdomain}) { 87 | $fqdn = "$hostname.$search"; 88 | } 89 | return ($hostname, $fqdn); 90 | } 91 | 92 | sub get_dns_conf { 93 | my ($conf) = @_; 94 | 95 | # Same logic as in pve-container, but without the testcase special case 96 | my $host_resolv_conf = PVE::INotify::read_file('resolvconf'); 97 | 98 | my $searchdomains = [ 99 | split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search}) 100 | ]; 101 | 102 | my $nameserver = $conf->{nameserver}; 103 | if (!defined($nameserver)) { 104 | $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}]; 105 | } else { 106 | $nameserver = [split(/\s+/, $nameserver)]; 107 | } 108 | 109 | return ($searchdomains, $nameserver); 110 | } 111 | 112 | sub cloudinit_userdata { 113 | my ($conf, $vmid) = @_; 114 | 115 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 116 | 117 | my $content = "#cloud-config\n"; 118 | 119 | $content .= "hostname: $hostname\n"; 120 | $content .= "manage_etc_hosts: true\n"; 121 | $content .= "fqdn: $fqdn\n" if defined($fqdn); 122 | 123 | my $username = $conf->{ciuser}; 124 | my $password = $conf->{cipassword}; 125 | 126 | $content .= "user: $username\n" if defined($username); 127 | $content .= "disable_root: False\n" if defined($username) && $username eq 'root'; 128 | $content .= "password: $password\n" if defined($password); 129 | 130 | if (defined(my $keys = $conf->{sshkeys})) { 131 | $keys = URI::Escape::uri_unescape($keys); 132 | $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)]; 133 | $keys = [grep { /\S/ } @$keys]; 134 | $content .= "ssh_authorized_keys:\n"; 135 | foreach my $k (@$keys) { 136 | $content .= " - $k\n"; 137 | } 138 | } 139 | $content .= "chpasswd:\n"; 140 | $content .= " expire: False\n"; 141 | 142 | if (!defined($username) || $username ne 'root') { 143 | $content .= "users:\n"; 144 | $content .= " - default\n"; 145 | } 146 | 147 | $content .= "package_upgrade: true\n"; 148 | 149 | return $content; 150 | } 151 | 152 | sub split_ip4 { 153 | my ($ip) = @_; 154 | my ($addr, $mask) = split('/', $ip); 155 | die "not a CIDR: $ip\n" if !defined $mask; 156 | return ($addr, $PVE::Network::ipv4_reverse_mask->[$mask]); 157 | } 158 | 159 | sub configdrive2_network { 160 | my ($conf) = @_; 161 | 162 | ## Support Windows 163 | my $ostype = $conf->{"ostype"}; 164 | my $default_dns = ''; 165 | my $default_search = ''; 166 | ## 167 | 168 | my $content = "auto lo\n"; 169 | $content .= "iface lo inet loopback\n\n"; 170 | 171 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 172 | if ($nameservers && @$nameservers) { 173 | $nameservers = join(' ', @$nameservers); 174 | $content .= " dns_nameservers $nameservers\n"; 175 | $default_dns = $nameservers; # Support windows 176 | } 177 | if ($searchdomains && @$searchdomains) { 178 | $searchdomains = join(' ', @$searchdomains); 179 | $content .= " dns_search $searchdomains\n"; 180 | $default_search = $searchdomains; # Support Windows 181 | } 182 | 183 | my $dnsinserted = 0; # Insert dns once for Windows machines 184 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 185 | foreach my $iface (sort @ifaces) { 186 | (my $id = $iface) =~ s/^net//; 187 | next if !$conf->{"ipconfig$id"}; 188 | my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 189 | $id = "eth$id"; 190 | 191 | $content .="auto $id\n"; 192 | if ($net->{ip}) { 193 | if ($net->{ip} eq 'dhcp') { 194 | $content .= "iface $id inet dhcp\n"; 195 | } else { 196 | my ($addr, $mask) = split_ip4($net->{ip}); 197 | $content .= "iface $id inet static\n"; 198 | $content .= " address $addr\n"; 199 | $content .= " netmask $mask\n"; 200 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 201 | 202 | ## Support windows 203 | if(PVE::QemuServer::windows_version($ostype) && not($dnsinserted)) { 204 | $content .= " dns-nameservers $default_dns\n"; 205 | $content .= " dns-search $default_search\n"; 206 | $dnsinserted++; 207 | } 208 | ## 209 | } 210 | } 211 | if ($net->{ip6}) { 212 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { 213 | $content .= "iface $id inet6 $1\n"; 214 | } else { 215 | my ($addr, $mask) = split('/', $net->{ip6}); 216 | $content .= "iface $id inet6 static\n"; 217 | $content .= " address $addr\n"; 218 | $content .= " netmask $mask\n"; 219 | $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 220 | } 221 | } 222 | } 223 | 224 | return $content; 225 | } 226 | 227 | # Get mac addresses from Conf file 228 | sub get_mac_addresses { 229 | my ($conf) = @_; 230 | 231 | my $dhcpstring = undef; 232 | my @dhcpmacs = (); 233 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 234 | 235 | foreach my $iface (sort @ifaces) { 236 | (my $id = $iface) =~ s/^net//; 237 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 238 | next if !$conf->{"ipconfig$id"}; 239 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 240 | 241 | my $mac = lc $net->{macaddr}; 242 | 243 | if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 244 | push @dhcpmacs, $mac; 245 | } 246 | } 247 | 248 | if (@dhcpmacs){ 249 | $dhcpstring = ",\n \"dhcp\":["; 250 | foreach my $mac (@dhcpmacs){ 251 | if ($mac != @dhcpmacs[-1]){ 252 | $dhcpstring .= "\"$mac\","; 253 | } 254 | else{ 255 | $dhcpstring .= "\"$mac\"]"; 256 | } 257 | } 258 | } 259 | return ($dhcpstring); 260 | } 261 | 262 | sub configdrive2_gen_metadata { 263 | my ($conf, $vmid, $user, $network) = @_; 264 | 265 | # Get mac addresses of dhcp nics from conf file 266 | my $dhcpmacs = undef; 267 | $dhcpmacs = get_mac_addresses($conf); 268 | 269 | # Get UUID 270 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 271 | 272 | # Get hostname 273 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 274 | 275 | # Get username, default to Administrator if none 276 | my $username = undef; 277 | if (defined($conf->{ciuser})){ 278 | my $name = $conf->{ciuser}; 279 | $username = ",\n \"admin_username\": \"$name\"" 280 | } 281 | 282 | # Get user password 283 | my $password = $conf->{cipassword}; 284 | 285 | # Get ssh keys and make a list out of it in json format 286 | my $keystring = undef; 287 | my $pubkeys = $conf->{sshkeys}; 288 | $pubkeys = URI::Escape::uri_unescape($pubkeys); 289 | my @pubkeysarray = split "\n", $pubkeys; 290 | if (@pubkeysarray) { 291 | my $arraylength = @pubkeysarray; 292 | my $incrementer = 1; 293 | $keystring =",\n \"public_keys\": {\n"; 294 | for my $key (@pubkeysarray){ 295 | $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 296 | if ($arraylength != $incrementer){ 297 | $keystring .= ",\n"; 298 | }else{ 299 | $keystring .= "\n }"; 300 | } 301 | $incrementer++; 302 | } 303 | } 304 | 305 | return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 306 | } 307 | 308 | sub configdrive2_metadata { 309 | my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 310 | return <<"EOF"; 311 | { 312 | "meta":{ 313 | "admin_pass": "$password"$username 314 | }, 315 | "uuid":"$uuid", 316 | "hostname":"$hostname", 317 | "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs 318 | } 319 | EOF 320 | } 321 | 322 | sub generate_configdrive2 { 323 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 324 | 325 | my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf); 326 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 327 | $network_data = configdrive2_network($conf) if !defined($network_data); 328 | 329 | if (!defined($meta_data)) { 330 | $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 331 | } 332 | my $files = { 333 | '/openstack/latest/user_data' => $user_data, 334 | '/openstack/content/0000' => $network_data, 335 | '/openstack/latest/meta_data.json' => $meta_data 336 | }; 337 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2'); 338 | } 339 | 340 | sub generate_opennebula { 341 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 342 | 343 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 344 | 345 | my $content = ""; 346 | 347 | my $username = $conf->{ciuser} || "root"; 348 | my $password = encode_base64($conf->{cipassword}) if defined($conf->{cipassword}); 349 | 350 | $content .= "USERNAME=$username\n" if defined($username); 351 | $content .= "CRYPTED_PASSWORD_BASE64=$password\n" if defined($password); 352 | 353 | if (defined(my $keys = $conf->{sshkeys})) { 354 | $keys = URI::Escape::uri_unescape($keys); 355 | $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)]; 356 | $keys = [grep { /\S/ } @$keys]; 357 | $content .= "SSH_PUBLIC_KEY=\""; 358 | 359 | foreach my $k (@$keys) { 360 | $content .= "$k\n"; 361 | } 362 | $content .= "\"\n"; 363 | 364 | } 365 | 366 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 367 | if ($nameservers && @$nameservers) { 368 | $nameservers = join(' ', @$nameservers); 369 | $content .= "DNS=\"$nameservers\"\n"; 370 | } 371 | 372 | $content .= "SET_HOSTNAME=$hostname\n"; 373 | 374 | if ($searchdomains && @$searchdomains) { 375 | $searchdomains = join(' ', @$searchdomains); 376 | $content .= "SEARCH_DOMAIN=\"$searchdomains\"\n"; 377 | } 378 | 379 | my $networkenabled = undef; 380 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 381 | foreach my $iface (sort @ifaces) { 382 | (my $id = $iface) =~ s/^net//; 383 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 384 | next if !$conf->{"ipconfig$id"}; 385 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 386 | my $ethid = "ETH$id"; 387 | 388 | my $mac = lc $net->{hwaddr}; 389 | 390 | if ($ipconfig->{ip}) { 391 | $networkenabled = 1; 392 | 393 | if ($ipconfig->{ip} eq 'dhcp') { 394 | $content .= $ethid."_DHCP=YES\n"; 395 | } else { 396 | my ($addr, $mask) = split_ip4($ipconfig->{ip}); 397 | $content .= $ethid."_IP=$addr\n"; 398 | $content .= $ethid."_MASK=$mask\n"; 399 | $content .= $ethid."_MAC=$mac\n"; 400 | $content .= $ethid."_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw}; 401 | } 402 | $content .= $ethid."_MTU=$net->{mtu}\n" if $net->{mtu}; 403 | } 404 | 405 | if ($ipconfig->{ip6}) { 406 | $networkenabled = 1; 407 | if ($ipconfig->{ip6} eq 'dhcp') { 408 | $content .= $ethid."_DHCP6=YES\n"; 409 | } elsif ($ipconfig->{ip6} eq 'auto') { 410 | $content .= $ethid."_AUTO6=YES\n"; 411 | } else { 412 | my ($addr, $mask) = split('/', $ipconfig->{ip6}); 413 | $content .= $ethid."_IP6=$addr\n"; 414 | $content .= $ethid."_MASK6=$mask\n"; 415 | $content .= $ethid."_MAC6=$mac\n"; 416 | $content .= $ethid."_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6}; 417 | } 418 | $content .= $ethid."_MTU=$net->{mtu}\n" if $net->{mtu}; 419 | } 420 | } 421 | 422 | $content .= "NETWORK=YES\n" if $networkenabled; 423 | 424 | my $files = { 425 | '/context.sh' => $content, 426 | }; 427 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT'); 428 | } 429 | 430 | sub nocloud_network_v2 { 431 | my ($conf) = @_; 432 | 433 | my $content = ''; 434 | 435 | my $head = "version: 2\n" 436 | . "ethernets:\n"; 437 | 438 | my $dns_done; 439 | 440 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 441 | foreach my $iface (sort @ifaces) { 442 | (my $id = $iface) =~ s/^net//; 443 | next if !$conf->{"ipconfig$id"}; 444 | 445 | # indentation - network interfaces are inside an 'ethernets' hash 446 | my $i = ' '; 447 | 448 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 449 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 450 | 451 | my $mac = $net->{macaddr} 452 | or die "network interface '$iface' has no mac address\n"; 453 | 454 | $content .= "${i}$iface:\n"; 455 | $i .= ' '; 456 | $content .= "${i}match:\n" 457 | . "${i} macaddress: \"$mac\"\n" 458 | . "${i}set-name: eth$id\n"; 459 | my @addresses; 460 | if (defined(my $ip = $ipconfig->{ip})) { 461 | if ($ip eq 'dhcp') { 462 | $content .= "${i}dhcp4: true\n"; 463 | } else { 464 | push @addresses, $ip; 465 | } 466 | } 467 | if (defined(my $ip = $ipconfig->{ip6})) { 468 | if ($ip eq 'dhcp') { 469 | $content .= "${i}dhcp6: true\n"; 470 | } else { 471 | push @addresses, $ip; 472 | } 473 | } 474 | if (@addresses) { 475 | $content .= "${i}addresses:\n"; 476 | $content .= "${i}- '$_'\n" foreach @addresses; 477 | } 478 | if (defined(my $gw = $ipconfig->{gw})) { 479 | $content .= "${i}gateway4: '$gw'\n"; 480 | } 481 | if (defined(my $gw = $ipconfig->{gw6})) { 482 | $content .= "${i}gateway6: '$gw'\n"; 483 | } 484 | 485 | next if $dns_done; 486 | $dns_done = 1; 487 | 488 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 489 | if ($searchdomains || $nameservers) { 490 | $content .= "${i}nameservers:\n"; 491 | if (defined($nameservers) && @$nameservers) { 492 | $content .= "${i} addresses:\n"; 493 | $content .= "${i} - '$_'\n" foreach @$nameservers; 494 | } 495 | if (defined($searchdomains) && @$searchdomains) { 496 | $content .= "${i} search:\n"; 497 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 498 | } 499 | } 500 | } 501 | 502 | return $head.$content; 503 | } 504 | 505 | sub nocloud_network { 506 | my ($conf) = @_; 507 | 508 | my $content = "version: 1\n" 509 | . "config:\n"; 510 | 511 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 512 | foreach my $iface (sort @ifaces) { 513 | (my $id = $iface) =~ s/^net//; 514 | next if !$conf->{"ipconfig$id"}; 515 | 516 | # indentation - network interfaces are inside an 'ethernets' hash 517 | my $i = ' '; 518 | 519 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 520 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 521 | 522 | my $mac = lc($net->{macaddr}) 523 | or die "network interface '$iface' has no mac address\n"; 524 | 525 | $content .= "${i}- type: physical\n" 526 | . "${i} name: eth$id\n" 527 | . "${i} mac_address: '$mac'\n" 528 | . "${i} subnets:\n"; 529 | $i .= ' '; 530 | if (defined(my $ip = $ipconfig->{ip})) { 531 | if ($ip eq 'dhcp') { 532 | $content .= "${i}- type: dhcp4\n"; 533 | } else { 534 | my ($addr, $mask) = split_ip4($ip); 535 | $content .= "${i}- type: static\n" 536 | . "${i} address: '$addr'\n" 537 | . "${i} netmask: '$mask'\n"; 538 | if (defined(my $gw = $ipconfig->{gw})) { 539 | $content .= "${i} gateway: '$gw'\n"; 540 | } 541 | } 542 | } 543 | if (defined(my $ip = $ipconfig->{ip6})) { 544 | if ($ip eq 'dhcp') { 545 | $content .= "${i}- type: dhcp6\n"; 546 | } elsif ($ip eq 'auto') { 547 | # SLAAC is only supported by cloud-init since 19.4 548 | $content .= "${i}- type: ipv6_slaac\n"; 549 | } else { 550 | $content .= "${i}- type: static6\n" 551 | . "${i} address: '$ip'\n"; 552 | if (defined(my $gw = $ipconfig->{gw6})) { 553 | $content .= "${i} gateway: '$gw'\n"; 554 | } 555 | } 556 | } 557 | } 558 | 559 | my $i = ' '; 560 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 561 | if ($searchdomains || $nameservers) { 562 | $content .= "${i}- type: nameserver\n"; 563 | if (defined($nameservers) && @$nameservers) { 564 | $content .= "${i} address:\n"; 565 | $content .= "${i} - '$_'\n" foreach @$nameservers; 566 | } 567 | if (defined($searchdomains) && @$searchdomains) { 568 | $content .= "${i} search:\n"; 569 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 570 | } 571 | } 572 | 573 | return $content; 574 | } 575 | 576 | sub nocloud_metadata { 577 | my ($uuid) = @_; 578 | return "instance-id: $uuid\n"; 579 | } 580 | 581 | sub nocloud_gen_metadata { 582 | my ($user, $network) = @_; 583 | 584 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 585 | return nocloud_metadata($uuid_str); 586 | } 587 | 588 | sub generate_nocloud { 589 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 590 | 591 | my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf); 592 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 593 | $network_data = nocloud_network($conf) if !defined($network_data); 594 | 595 | if (!defined($meta_data)) { 596 | $meta_data = nocloud_gen_metadata($user_data, $network_data); 597 | } 598 | 599 | my $files = { 600 | '/user-data' => $user_data, 601 | '/network-config' => $network_data, 602 | '/meta-data' => $meta_data 603 | }; 604 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata'); 605 | } 606 | 607 | sub get_custom_cloudinit_files { 608 | my ($conf) = @_; 609 | 610 | my $cicustom = $conf->{cicustom}; 611 | my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {}; 612 | 613 | my $network_volid = $files->{network}; 614 | my $user_volid = $files->{user}; 615 | my $meta_volid = $files->{meta}; 616 | 617 | my $storage_conf = PVE::Storage::config(); 618 | 619 | my $network_data; 620 | if ($network_volid) { 621 | $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid); 622 | } 623 | 624 | my $user_data; 625 | if ($user_volid) { 626 | $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid); 627 | } 628 | 629 | my $meta_data; 630 | if ($meta_volid) { 631 | $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid); 632 | } 633 | 634 | return ($user_data, $network_data, $meta_data); 635 | } 636 | 637 | sub read_cloudinit_snippets_file { 638 | my ($storage_conf, $volid) = @_; 639 | 640 | my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid); 641 | die "$volid is not in the snippets directory\n" if $type ne 'snippets'; 642 | return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024); 643 | } 644 | 645 | my $cloudinit_methods = { 646 | configdrive2 => \&generate_configdrive2, 647 | nocloud => \&generate_nocloud, 648 | opennebula => \&generate_opennebula, 649 | }; 650 | 651 | sub generate_cloudinitconfig { 652 | my ($conf, $vmid) = @_; 653 | 654 | my $format = get_cloudinit_format($conf); 655 | 656 | PVE::QemuConfig->foreach_volume($conf, sub { 657 | my ($ds, $drive) = @_; 658 | 659 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); 660 | 661 | return if !$volname || $volname !~ m/vm-$vmid-cloudinit/; 662 | 663 | my $generator = $cloudinit_methods->{$format} 664 | or die "missing cloudinit methods for format '$format'\n"; 665 | 666 | $generator->($conf, $vmid, $drive, $volname, $storeid); 667 | }); 668 | } 669 | 670 | sub dump_cloudinit_config { 671 | my ($conf, $vmid, $type) = @_; 672 | 673 | my $format = get_cloudinit_format($conf); 674 | 675 | if ($type eq 'user') { 676 | return cloudinit_userdata($conf, $vmid); 677 | } elsif ($type eq 'network') { 678 | if ($format eq 'nocloud') { 679 | return nocloud_network($conf); 680 | } else { 681 | return configdrive2_network($conf); 682 | } 683 | } else { # metadata config 684 | my $user = cloudinit_userdata($conf, $vmid); 685 | if ($format eq 'nocloud') { 686 | my $network = nocloud_network($conf); 687 | return nocloud_gen_metadata($user, $network); 688 | } else { 689 | my $network = configdrive2_network($conf); 690 | return configdrive2_gen_metadata($user, $network); 691 | } 692 | } 693 | } 694 | 695 | 1; 696 | -------------------------------------------------------------------------------- /qemu-server-7.1-4/Cloudinit.pm.patch: -------------------------------------------------------------------------------- 1 | --- /usr/share/perl5/PVE/QemuServer/Cloudinit.pm.orig 2021-11-22 17:07:13.000000000 +0100 2 | +++ /usr/share/perl5/PVE/QemuServer/Cloudinit.pm 2022-01-18 11:18:40.902886004 +0100 3 | @@ -163,13 +163,23 @@ 4 | $content .= "iface lo inet loopback\n\n"; 5 | 6 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 7 | + 8 | + ## support windows 9 | + my $ostype = $conf->{"ostype"}; 10 | + my $default_dns = ''; 11 | + my $default_search = ''; 12 | + ## 13 | + my $dnsinserted = 0; # insert dns just once for the machine 14 | + 15 | if ($nameservers && @$nameservers) { 16 | $nameservers = join(' ', @$nameservers); 17 | $content .= " dns_nameservers $nameservers\n"; 18 | + $default_dns = $nameservers; # Support windows 19 | } 20 | if ($searchdomains && @$searchdomains) { 21 | $searchdomains = join(' ', @$searchdomains); 22 | $content .= " dns_search $searchdomains\n"; 23 | + $default_search = $searchdomains; # Support Windows 24 | } 25 | 26 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 27 | @@ -189,6 +199,13 @@ 28 | $content .= " address $addr\n"; 29 | $content .= " netmask $mask\n"; 30 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 31 | + ## Support Windows 32 | + if(PVE::QemuServer::windows_version($ostype) && not($dnsinserted)) { 33 | + $content .= " dns-nameservers $default_dns\n"; 34 | + $content .= " dns-search $default_search\n"; 35 | + $dnsinserted++; 36 | + } 37 | + ## 38 | } 39 | } 40 | if ($net->{ip6}) { 41 | @@ -207,23 +224,102 @@ 42 | return $content; 43 | } 44 | 45 | +# Get mac addresses of dhcp nics from conf file 46 | +sub get_mac_addresses { 47 | + my ($conf) = @_; 48 | + 49 | + my $dhcpstring = undef; 50 | + my @dhcpmacs = (); 51 | + my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 52 | + 53 | + foreach my $iface (sort @ifaces) { 54 | + (my $id = $iface) =~ s/^net//; 55 | + my $net = PVE::QemuServer::parse_net($conf->{$iface}); 56 | + next if !$conf->{"ipconfig$id"}; 57 | + my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 58 | + 59 | + my $mac = lc $net->{macaddr}; 60 | + 61 | + if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 62 | + push @dhcpmacs, $mac; 63 | + } 64 | + } 65 | + 66 | + if (@dhcpmacs){ 67 | + $dhcpstring = ",\n \"dhcp\":["; 68 | + foreach my $mac (@dhcpmacs){ 69 | + if ($mac != $dhcpmacs[-1]){ 70 | + $dhcpstring .= "\"$mac\","; 71 | + } 72 | + else{ 73 | + $dhcpstring .= "\"$mac\"]"; 74 | + } 75 | + } 76 | + } 77 | + return ($dhcpstring); 78 | +} 79 | + 80 | sub configdrive2_gen_metadata { 81 | - my ($user, $network) = @_; 82 | + my ($conf, $vmid, $user, $network) = @_; 83 | + 84 | + # Get mac addresses of dhcp nics from conf file 85 | + my $dhcpmacs = undef; 86 | + $dhcpmacs = get_mac_addresses($conf); 87 | 88 | + # Get UUID 89 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 90 | - return configdrive2_metadata($uuid_str); 91 | + 92 | + # Get hostname 93 | + my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 94 | + 95 | + # Get username, default to Administrator if none 96 | + my $username = undef; 97 | + if (defined($conf->{ciuser})){ 98 | + my $name = $conf->{ciuser}; 99 | + $username = ",\n \"admin_username\": \"$name\"" 100 | + } 101 | + 102 | + # Get user password 103 | + my $password = $conf->{cipassword}; 104 | + 105 | + # Get ssh keys and make a list out of it in json format 106 | + my $keystring = undef; 107 | + my $pubkeys = $conf->{sshkeys}; 108 | + $pubkeys = URI::Escape::uri_unescape($pubkeys); 109 | + my @pubkeysarray = split "\n", $pubkeys; 110 | + if (@pubkeysarray) { 111 | + my $arraylength = @pubkeysarray; 112 | + my $incrementer = 1; 113 | + $keystring =",\n \"public_keys\": {\n"; 114 | + for my $key (@pubkeysarray){ 115 | + $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 116 | + if ($arraylength != $incrementer){ 117 | + $keystring .= ",\n"; 118 | + }else{ 119 | + $keystring .= "\n }"; 120 | + } 121 | + $incrementer++; 122 | + } 123 | + } 124 | + 125 | + return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 126 | } 127 | 128 | sub configdrive2_metadata { 129 | - my ($uuid) = @_; 130 | + my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 131 | return <<"EOF"; 132 | { 133 | - "uuid": "$uuid", 134 | - "network_config": { "content_path": "/content/0000" } 135 | + "meta":{ 136 | + "admin_pass": "$password"$username 137 | + }, 138 | + "uuid":"$uuid", 139 | + "hostname":"$hostname", 140 | + "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs 141 | } 142 | EOF 143 | } 144 | 145 | + 146 | sub generate_configdrive2 { 147 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 148 | 149 | @@ -232,7 +328,7 @@ 150 | $network_data = configdrive2_network($conf) if !defined($network_data); 151 | 152 | if (!defined($meta_data)) { 153 | - $meta_data = configdrive2_gen_metadata($user_data, $network_data); 154 | + $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 155 | } 156 | 157 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 158 | -------------------------------------------------------------------------------- /qemu-server-7.1-4/Qemu.pm.patch: -------------------------------------------------------------------------------- 1 | --- /usr/share/perl5/PVE/API2/Qemu.pm.orig 2021-11-22 17:07:13.000000000 +0100 2 | +++ /usr/share/perl5/PVE/API2/Qemu.pm 2022-01-11 16:24:06.339326806 +0100 3 | @@ -1105,10 +1105,16 @@ 4 | 5 | my $background_delay = extract_param($param, 'background_delay'); 6 | 7 | + my $conf = PVE::QemuConfig->load_config($vmid); 8 | + 9 | + my $ostype = $conf->{ostype}; 10 | + 11 | if (defined(my $cipassword = $param->{cipassword})) { 12 | # Same logic as in cloud-init (but with the regex fixed...) 13 | - $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) 14 | - if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/; 15 | + if (!(PVE::QemuServer::windows_version($ostype))) { 16 | + $param->{cipassword} = PVE::Tools::encrypt_pw($cipassword) 17 | + if $cipassword !~ /^\$(?:[156]|2[ay])(\$.+){2}/; 18 | + } 19 | } 20 | 21 | my @paramarr = (); # used for log message 22 | -------------------------------------------------------------------------------- /qemu-server-7.1-4/sourcefiles/Cloudinit.pm: -------------------------------------------------------------------------------- 1 | package PVE::QemuServer::Cloudinit; 2 | 3 | use strict; 4 | use warnings; 5 | 6 | use File::Path; 7 | use Digest::SHA; 8 | use URI::Escape; 9 | use MIME::Base64 qw(encode_base64); 10 | 11 | use PVE::Tools qw(run_command file_set_contents); 12 | use PVE::Storage; 13 | use PVE::QemuServer; 14 | 15 | use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes 16 | 17 | sub commit_cloudinit_disk { 18 | my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_; 19 | 20 | my $path = "/run/pve/cloudinit/$vmid/"; 21 | mkpath $path; 22 | foreach my $filepath (keys %$files) { 23 | if ($filepath !~ m@^(.*)\/[^/]+$@) { 24 | die "internal error: bad file name in cloud-init image: $filepath\n"; 25 | } 26 | my $dirname = $1; 27 | mkpath "$path/$dirname"; 28 | 29 | my $contents = $files->{$filepath}; 30 | file_set_contents("$path/$filepath", $contents); 31 | } 32 | 33 | my $storecfg = PVE::Storage::config(); 34 | my $iso_path = PVE::Storage::path($storecfg, $drive->{file}); 35 | my $scfg = PVE::Storage::storage_config($storecfg, $storeid); 36 | my $format = PVE::QemuServer::qemu_img_format($scfg, $volname); 37 | 38 | my $size = eval { PVE::Storage::volume_size_info($storecfg, $drive->{file}) }; 39 | if (!defined($size) || $size <= 0) { 40 | $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/; 41 | my $name = $1; 42 | $size = 4 * 1024; 43 | PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size); 44 | $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte 45 | } 46 | my $plugin = PVE::Storage::Plugin->lookup($scfg->{type}); 47 | $plugin->activate_volume($storeid, $scfg, $volname); 48 | 49 | print "generating cloud-init ISO\n"; 50 | eval { 51 | run_command([ 52 | ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path], 53 | ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"] 54 | ]); 55 | }; 56 | my $err = $@; 57 | rmtree($path); 58 | die $err if $err; 59 | } 60 | 61 | sub get_cloudinit_format { 62 | my ($conf) = @_; 63 | if (defined(my $format = $conf->{citype})) { 64 | return $format; 65 | } 66 | 67 | # No format specified, default based on ostype because windows' 68 | # cloudbased-init only supports configdrivev2, whereas on linux we need 69 | # to use mac addresses because regular cloudinit doesn't map 'ethX' to 70 | # the new predicatble network device naming scheme. 71 | if (defined(my $ostype = $conf->{ostype})) { 72 | return 'configdrive2' 73 | if PVE::QemuServer::windows_version($ostype); 74 | } 75 | 76 | return 'nocloud'; 77 | } 78 | 79 | sub get_hostname_fqdn { 80 | my ($conf, $vmid) = @_; 81 | my $hostname = $conf->{name} // "VM$vmid"; 82 | my $fqdn; 83 | if ($hostname =~ /\./) { 84 | $fqdn = $hostname; 85 | $hostname =~ s/\..*$//; 86 | } elsif (my $search = $conf->{searchdomain}) { 87 | $fqdn = "$hostname.$search"; 88 | } 89 | return ($hostname, $fqdn); 90 | } 91 | 92 | sub get_dns_conf { 93 | my ($conf) = @_; 94 | 95 | # Same logic as in pve-container, but without the testcase special case 96 | my $host_resolv_conf = PVE::INotify::read_file('resolvconf'); 97 | 98 | my $searchdomains = [ 99 | split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search}) 100 | ]; 101 | 102 | my $nameserver = $conf->{nameserver}; 103 | if (!defined($nameserver)) { 104 | $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}]; 105 | } else { 106 | $nameserver = [split(/\s+/, $nameserver)]; 107 | } 108 | 109 | return ($searchdomains, $nameserver); 110 | } 111 | 112 | sub cloudinit_userdata { 113 | my ($conf, $vmid) = @_; 114 | 115 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 116 | 117 | my $content = "#cloud-config\n"; 118 | 119 | $content .= "hostname: $hostname\n"; 120 | $content .= "manage_etc_hosts: true\n"; 121 | $content .= "fqdn: $fqdn\n" if defined($fqdn); 122 | 123 | my $username = $conf->{ciuser}; 124 | my $password = $conf->{cipassword}; 125 | 126 | $content .= "user: $username\n" if defined($username); 127 | $content .= "disable_root: False\n" if defined($username) && $username eq 'root'; 128 | $content .= "password: $password\n" if defined($password); 129 | 130 | if (defined(my $keys = $conf->{sshkeys})) { 131 | $keys = URI::Escape::uri_unescape($keys); 132 | $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)]; 133 | $keys = [grep { /\S/ } @$keys]; 134 | $content .= "ssh_authorized_keys:\n"; 135 | foreach my $k (@$keys) { 136 | $content .= " - $k\n"; 137 | } 138 | } 139 | $content .= "chpasswd:\n"; 140 | $content .= " expire: False\n"; 141 | 142 | if (!defined($username) || $username ne 'root') { 143 | $content .= "users:\n"; 144 | $content .= " - default\n"; 145 | } 146 | 147 | $content .= "package_upgrade: true\n"; 148 | 149 | return $content; 150 | } 151 | 152 | sub split_ip4 { 153 | my ($ip) = @_; 154 | my ($addr, $mask) = split('/', $ip); 155 | die "not a CIDR: $ip\n" if !defined $mask; 156 | return ($addr, $PVE::Network::ipv4_reverse_mask->[$mask]); 157 | } 158 | 159 | sub configdrive2_network { 160 | my ($conf) = @_; 161 | 162 | my $content = "auto lo\n"; 163 | $content .= "iface lo inet loopback\n\n"; 164 | 165 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 166 | 167 | ## support windows 168 | my $ostype = $conf->{"ostype"}; 169 | my $default_dns = ''; 170 | my $default_search = ''; 171 | ## 172 | my $dnsinserted = 0; # insert dns just once for the machine 173 | 174 | if ($nameservers && @$nameservers) { 175 | $nameservers = join(' ', @$nameservers); 176 | $content .= " dns_nameservers $nameservers\n"; 177 | $default_dns = $nameservers; # Support windows 178 | } 179 | if ($searchdomains && @$searchdomains) { 180 | $searchdomains = join(' ', @$searchdomains); 181 | $content .= " dns_search $searchdomains\n"; 182 | $default_search = $searchdomains; # Support Windows 183 | } 184 | 185 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 186 | foreach my $iface (sort @ifaces) { 187 | (my $id = $iface) =~ s/^net//; 188 | next if !$conf->{"ipconfig$id"}; 189 | my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 190 | $id = "eth$id"; 191 | 192 | $content .="auto $id\n"; 193 | if ($net->{ip}) { 194 | if ($net->{ip} eq 'dhcp') { 195 | $content .= "iface $id inet dhcp\n"; 196 | } else { 197 | my ($addr, $mask) = split_ip4($net->{ip}); 198 | $content .= "iface $id inet static\n"; 199 | $content .= " address $addr\n"; 200 | $content .= " netmask $mask\n"; 201 | $content .= " gateway $net->{gw}\n" if $net->{gw}; 202 | ## Support Windows 203 | if(PVE::QemuServer::windows_version($ostype) && not($dnsinserted)) { 204 | $content .= " dns-nameservers $default_dns\n"; 205 | $content .= " dns-search $default_search\n"; 206 | $dnsinserted++; 207 | } 208 | ## 209 | } 210 | } 211 | if ($net->{ip6}) { 212 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { 213 | $content .= "iface $id inet6 $1\n"; 214 | } else { 215 | my ($addr, $mask) = split('/', $net->{ip6}); 216 | $content .= "iface $id inet6 static\n"; 217 | $content .= " address $addr\n"; 218 | $content .= " netmask $mask\n"; 219 | $content .= " gateway $net->{gw6}\n" if $net->{gw6}; 220 | } 221 | } 222 | } 223 | 224 | return $content; 225 | } 226 | 227 | # Get mac addresses of dhcp nics from conf file 228 | sub get_mac_addresses { 229 | my ($conf) = @_; 230 | 231 | my $dhcpstring = undef; 232 | my @dhcpmacs = (); 233 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 234 | 235 | foreach my $iface (sort @ifaces) { 236 | (my $id = $iface) =~ s/^net//; 237 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 238 | next if !$conf->{"ipconfig$id"}; 239 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 240 | 241 | my $mac = lc $net->{macaddr}; 242 | 243 | if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){ 244 | push @dhcpmacs, $mac; 245 | } 246 | } 247 | 248 | if (@dhcpmacs){ 249 | $dhcpstring = ",\n \"dhcp\":["; 250 | foreach my $mac (@dhcpmacs){ 251 | if ($mac != $dhcpmacs[-1]){ 252 | $dhcpstring .= "\"$mac\","; 253 | } 254 | else{ 255 | $dhcpstring .= "\"$mac\"]"; 256 | } 257 | } 258 | } 259 | return ($dhcpstring); 260 | } 261 | 262 | sub configdrive2_gen_metadata { 263 | my ($conf, $vmid, $user, $network) = @_; 264 | 265 | # Get mac addresses of dhcp nics from conf file 266 | my $dhcpmacs = undef; 267 | $dhcpmacs = get_mac_addresses($conf); 268 | 269 | # Get UUID 270 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 271 | 272 | # Get hostname 273 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 274 | 275 | # Get username, default to Administrator if none 276 | my $username = undef; 277 | if (defined($conf->{ciuser})){ 278 | my $name = $conf->{ciuser}; 279 | $username = ",\n \"admin_username\": \"$name\"" 280 | } 281 | 282 | # Get user password 283 | my $password = $conf->{cipassword}; 284 | 285 | # Get ssh keys and make a list out of it in json format 286 | my $keystring = undef; 287 | my $pubkeys = $conf->{sshkeys}; 288 | $pubkeys = URI::Escape::uri_unescape($pubkeys); 289 | my @pubkeysarray = split "\n", $pubkeys; 290 | if (@pubkeysarray) { 291 | my $arraylength = @pubkeysarray; 292 | my $incrementer = 1; 293 | $keystring =",\n \"public_keys\": {\n"; 294 | for my $key (@pubkeysarray){ 295 | $keystring .= " \"SSH${incrementer}\" : \"${key}\""; 296 | if ($arraylength != $incrementer){ 297 | $keystring .= ",\n"; 298 | }else{ 299 | $keystring .= "\n }"; 300 | } 301 | $incrementer++; 302 | } 303 | } 304 | 305 | return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs); 306 | } 307 | 308 | sub configdrive2_metadata { 309 | my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_; 310 | return <<"EOF"; 311 | { 312 | "meta":{ 313 | "admin_pass": "$password"$username 314 | }, 315 | "uuid":"$uuid", 316 | "hostname":"$hostname", 317 | "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs 318 | } 319 | EOF 320 | } 321 | 322 | 323 | sub generate_configdrive2 { 324 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 325 | 326 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 327 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 328 | $network_data = configdrive2_network($conf) if !defined($network_data); 329 | 330 | if (!defined($meta_data)) { 331 | $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data); 332 | } 333 | 334 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 335 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 336 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 337 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 338 | 339 | my $files = { 340 | '/openstack/latest/user_data' => $user_data, 341 | '/openstack/content/0000' => $network_data, 342 | '/openstack/latest/meta_data.json' => $meta_data, 343 | '/openstack/latest/vendor_data.json' => $vendor_data 344 | }; 345 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2'); 346 | } 347 | 348 | sub generate_opennebula { 349 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 350 | 351 | my $content = ""; 352 | 353 | my $username = $conf->{ciuser} || "root"; 354 | $content .= "USERNAME=$username\n" if defined($username); 355 | 356 | if (defined(my $password = $conf->{cipassword})) { 357 | $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64($password) ."\n"; 358 | } 359 | 360 | if (defined($conf->{sshkeys})) { 361 | my $keys = [ split(/\s*\n\s*/, URI::Escape::uri_unescape($conf->{sshkeys})) ]; 362 | $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n"; 363 | } 364 | 365 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); 366 | $content .= "SET_HOSTNAME=$hostname\n"; 367 | 368 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 369 | $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers; 370 | $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains; 371 | 372 | my $networkenabled = undef; 373 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 374 | foreach my $iface (sort @ifaces) { 375 | (my $id = $iface) =~ s/^net//; 376 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 377 | next if !$conf->{"ipconfig$id"}; 378 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 379 | my $ethid = "ETH$id"; 380 | 381 | my $mac = lc $net->{hwaddr}; 382 | 383 | if ($ipconfig->{ip}) { 384 | $networkenabled = 1; 385 | 386 | if ($ipconfig->{ip} eq 'dhcp') { 387 | $content .= "${ethid}_DHCP=YES\n"; 388 | } else { 389 | my ($addr, $mask) = split_ip4($ipconfig->{ip}); 390 | $content .= "${ethid}_IP=$addr\n"; 391 | $content .= "${ethid}_MASK=$mask\n"; 392 | $content .= "${ethid}_MAC=$mac\n"; 393 | $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw}; 394 | } 395 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 396 | } 397 | 398 | if ($ipconfig->{ip6}) { 399 | $networkenabled = 1; 400 | if ($ipconfig->{ip6} eq 'dhcp') { 401 | $content .= "${ethid}_DHCP6=YES\n"; 402 | } elsif ($ipconfig->{ip6} eq 'auto') { 403 | $content .= "${ethid}_AUTO6=YES\n"; 404 | } else { 405 | my ($addr, $mask) = split('/', $ipconfig->{ip6}); 406 | $content .= "${ethid}_IP6=$addr\n"; 407 | $content .= "${ethid}_MASK6=$mask\n"; 408 | $content .= "${ethid}_MAC6=$mac\n"; 409 | $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6}; 410 | } 411 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; 412 | } 413 | } 414 | 415 | $content .= "NETWORK=YES\n" if $networkenabled; 416 | 417 | my $files = { '/context.sh' => $content }; 418 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT'); 419 | } 420 | 421 | sub nocloud_network_v2 { 422 | my ($conf) = @_; 423 | 424 | my $content = ''; 425 | 426 | my $head = "version: 2\n" 427 | . "ethernets:\n"; 428 | 429 | my $dns_done; 430 | 431 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 432 | foreach my $iface (sort @ifaces) { 433 | (my $id = $iface) =~ s/^net//; 434 | next if !$conf->{"ipconfig$id"}; 435 | 436 | # indentation - network interfaces are inside an 'ethernets' hash 437 | my $i = ' '; 438 | 439 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 440 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 441 | 442 | my $mac = $net->{macaddr} 443 | or die "network interface '$iface' has no mac address\n"; 444 | 445 | $content .= "${i}$iface:\n"; 446 | $i .= ' '; 447 | $content .= "${i}match:\n" 448 | . "${i} macaddress: \"$mac\"\n" 449 | . "${i}set-name: eth$id\n"; 450 | my @addresses; 451 | if (defined(my $ip = $ipconfig->{ip})) { 452 | if ($ip eq 'dhcp') { 453 | $content .= "${i}dhcp4: true\n"; 454 | } else { 455 | push @addresses, $ip; 456 | } 457 | } 458 | if (defined(my $ip = $ipconfig->{ip6})) { 459 | if ($ip eq 'dhcp') { 460 | $content .= "${i}dhcp6: true\n"; 461 | } else { 462 | push @addresses, $ip; 463 | } 464 | } 465 | if (@addresses) { 466 | $content .= "${i}addresses:\n"; 467 | $content .= "${i}- '$_'\n" foreach @addresses; 468 | } 469 | if (defined(my $gw = $ipconfig->{gw})) { 470 | $content .= "${i}gateway4: '$gw'\n"; 471 | } 472 | if (defined(my $gw = $ipconfig->{gw6})) { 473 | $content .= "${i}gateway6: '$gw'\n"; 474 | } 475 | 476 | next if $dns_done; 477 | $dns_done = 1; 478 | 479 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 480 | if ($searchdomains || $nameservers) { 481 | $content .= "${i}nameservers:\n"; 482 | if (defined($nameservers) && @$nameservers) { 483 | $content .= "${i} addresses:\n"; 484 | $content .= "${i} - '$_'\n" foreach @$nameservers; 485 | } 486 | if (defined($searchdomains) && @$searchdomains) { 487 | $content .= "${i} search:\n"; 488 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 489 | } 490 | } 491 | } 492 | 493 | return $head.$content; 494 | } 495 | 496 | sub nocloud_network { 497 | my ($conf) = @_; 498 | 499 | my $content = "version: 1\n" 500 | . "config:\n"; 501 | 502 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; 503 | foreach my $iface (sort @ifaces) { 504 | (my $id = $iface) =~ s/^net//; 505 | next if !$conf->{"ipconfig$id"}; 506 | 507 | # indentation - network interfaces are inside an 'ethernets' hash 508 | my $i = ' '; 509 | 510 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); 511 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); 512 | 513 | my $mac = lc($net->{macaddr}) 514 | or die "network interface '$iface' has no mac address\n"; 515 | 516 | $content .= "${i}- type: physical\n" 517 | . "${i} name: eth$id\n" 518 | . "${i} mac_address: '$mac'\n" 519 | . "${i} subnets:\n"; 520 | $i .= ' '; 521 | if (defined(my $ip = $ipconfig->{ip})) { 522 | if ($ip eq 'dhcp') { 523 | $content .= "${i}- type: dhcp4\n"; 524 | } else { 525 | my ($addr, $mask) = split_ip4($ip); 526 | $content .= "${i}- type: static\n" 527 | . "${i} address: '$addr'\n" 528 | . "${i} netmask: '$mask'\n"; 529 | if (defined(my $gw = $ipconfig->{gw})) { 530 | $content .= "${i} gateway: '$gw'\n"; 531 | } 532 | } 533 | } 534 | if (defined(my $ip = $ipconfig->{ip6})) { 535 | if ($ip eq 'dhcp') { 536 | $content .= "${i}- type: dhcp6\n"; 537 | } elsif ($ip eq 'auto') { 538 | # SLAAC is only supported by cloud-init since 19.4 539 | $content .= "${i}- type: ipv6_slaac\n"; 540 | } else { 541 | $content .= "${i}- type: static6\n" 542 | . "${i} address: '$ip'\n"; 543 | if (defined(my $gw = $ipconfig->{gw6})) { 544 | $content .= "${i} gateway: '$gw'\n"; 545 | } 546 | } 547 | } 548 | } 549 | 550 | my $i = ' '; 551 | my ($searchdomains, $nameservers) = get_dns_conf($conf); 552 | if ($searchdomains || $nameservers) { 553 | $content .= "${i}- type: nameserver\n"; 554 | if (defined($nameservers) && @$nameservers) { 555 | $content .= "${i} address:\n"; 556 | $content .= "${i} - '$_'\n" foreach @$nameservers; 557 | } 558 | if (defined($searchdomains) && @$searchdomains) { 559 | $content .= "${i} search:\n"; 560 | $content .= "${i} - '$_'\n" foreach @$searchdomains; 561 | } 562 | } 563 | 564 | return $content; 565 | } 566 | 567 | sub nocloud_metadata { 568 | my ($uuid) = @_; 569 | return "instance-id: $uuid\n"; 570 | } 571 | 572 | sub nocloud_gen_metadata { 573 | my ($user, $network) = @_; 574 | 575 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); 576 | return nocloud_metadata($uuid_str); 577 | } 578 | 579 | sub generate_nocloud { 580 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; 581 | 582 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); 583 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); 584 | $network_data = nocloud_network($conf) if !defined($network_data); 585 | 586 | if (!defined($meta_data)) { 587 | $meta_data = nocloud_gen_metadata($user_data, $network_data); 588 | } 589 | 590 | # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO 591 | # make sure we always stay below it by keeping the sum of all files below 3 MiB 592 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); 593 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); 594 | 595 | my $files = { 596 | '/user-data' => $user_data, 597 | '/network-config' => $network_data, 598 | '/meta-data' => $meta_data, 599 | '/vendor-data' => $vendor_data 600 | }; 601 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata'); 602 | } 603 | 604 | sub get_custom_cloudinit_files { 605 | my ($conf) = @_; 606 | 607 | my $cicustom = $conf->{cicustom}; 608 | my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {}; 609 | 610 | my $network_volid = $files->{network}; 611 | my $user_volid = $files->{user}; 612 | my $meta_volid = $files->{meta}; 613 | my $vendor_volid = $files->{vendor}; 614 | 615 | my $storage_conf = PVE::Storage::config(); 616 | 617 | my $network_data; 618 | if ($network_volid) { 619 | $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid); 620 | } 621 | 622 | my $user_data; 623 | if ($user_volid) { 624 | $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid); 625 | } 626 | 627 | my $meta_data; 628 | if ($meta_volid) { 629 | $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid); 630 | } 631 | 632 | my $vendor_data; 633 | if ($vendor_volid) { 634 | $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid); 635 | } 636 | 637 | return ($user_data, $network_data, $meta_data, $vendor_data); 638 | } 639 | 640 | sub read_cloudinit_snippets_file { 641 | my ($storage_conf, $volid) = @_; 642 | 643 | my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid); 644 | die "$volid is not in the snippets directory\n" if $type ne 'snippets'; 645 | return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024); 646 | } 647 | 648 | my $cloudinit_methods = { 649 | configdrive2 => \&generate_configdrive2, 650 | nocloud => \&generate_nocloud, 651 | opennebula => \&generate_opennebula, 652 | }; 653 | 654 | sub generate_cloudinitconfig { 655 | my ($conf, $vmid) = @_; 656 | 657 | my $format = get_cloudinit_format($conf); 658 | 659 | PVE::QemuConfig->foreach_volume($conf, sub { 660 | my ($ds, $drive) = @_; 661 | 662 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); 663 | 664 | return if !$volname || $volname !~ m/vm-$vmid-cloudinit/; 665 | 666 | my $generator = $cloudinit_methods->{$format} 667 | or die "missing cloudinit methods for format '$format'\n"; 668 | 669 | $generator->($conf, $vmid, $drive, $volname, $storeid); 670 | }); 671 | } 672 | 673 | sub dump_cloudinit_config { 674 | my ($conf, $vmid, $type) = @_; 675 | 676 | my $format = get_cloudinit_format($conf); 677 | 678 | if ($type eq 'user') { 679 | return cloudinit_userdata($conf, $vmid); 680 | } elsif ($type eq 'network') { 681 | if ($format eq 'nocloud') { 682 | return nocloud_network($conf); 683 | } else { 684 | return configdrive2_network($conf); 685 | } 686 | } else { # metadata config 687 | my $user = cloudinit_userdata($conf, $vmid); 688 | if ($format eq 'nocloud') { 689 | my $network = nocloud_network($conf); 690 | return nocloud_gen_metadata($user, $network); 691 | } else { 692 | my $network = configdrive2_network($conf); 693 | return configdrive2_gen_metadata($user, $network); 694 | } 695 | } 696 | } 697 | 698 | 1; 699 | --------------------------------------------------------------------------------