]>
Commit | Line | Data |
---|---|---|
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 | if ($nameservers && @$nameservers) { | |
167 | $nameservers = join(' ', @$nameservers); | |
168 | $content .= " dns_nameservers $nameservers\n"; | |
169 | } | |
170 | if ($searchdomains && @$searchdomains) { | |
171 | $searchdomains = join(' ', @$searchdomains); | |
172 | $content .= " dns_search $searchdomains\n"; | |
173 | } | |
174 | ||
175 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; | |
176 | foreach my $iface (sort @ifaces) { | |
177 | (my $id = $iface) =~ s/^net//; | |
178 | next if !$conf->{"ipconfig$id"}; | |
179 | my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); | |
180 | $id = "eth$id"; | |
181 | ||
182 | $content .="auto $id\n"; | |
183 | if ($net->{ip}) { | |
184 | if ($net->{ip} eq 'dhcp') { | |
185 | $content .= "iface $id inet dhcp\n"; | |
186 | } else { | |
187 | my ($addr, $mask) = split_ip4($net->{ip}); | |
188 | $content .= "iface $id inet static\n"; | |
189 | $content .= " address $addr\n"; | |
190 | $content .= " netmask $mask\n"; | |
191 | $content .= " gateway $net->{gw}\n" if $net->{gw}; | |
192 | } | |
193 | } | |
194 | if ($net->{ip6}) { | |
195 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { | |
196 | $content .= "iface $id inet6 $1\n"; | |
197 | } else { | |
198 | my ($addr, $mask) = split('/', $net->{ip6}); | |
199 | $content .= "iface $id inet6 static\n"; | |
200 | $content .= " address $addr\n"; | |
201 | $content .= " netmask $mask\n"; | |
202 | $content .= " gateway $net->{gw6}\n" if $net->{gw6}; | |
203 | } | |
204 | } | |
205 | } | |
206 | ||
207 | return $content; | |
208 | } | |
209 | ||
210 | sub configdrive2_gen_metadata { | |
211 | my ($user, $network) = @_; | |
212 | ||
213 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); | |
214 | return configdrive2_metadata($uuid_str); | |
215 | } | |
216 | ||
217 | sub configdrive2_metadata { | |
218 | my ($uuid) = @_; | |
219 | return <<"EOF"; | |
220 | { | |
221 | "uuid": "$uuid", | |
222 | "network_config": { "content_path": "/content/0000" } | |
223 | } | |
224 | EOF | |
225 | } | |
226 | ||
227 | sub generate_configdrive2 { | |
228 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; | |
229 | ||
230 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); | |
231 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); | |
232 | $network_data = configdrive2_network($conf) if !defined($network_data); | |
233 | ||
234 | if (!defined($meta_data)) { | |
235 | $meta_data = configdrive2_gen_metadata($user_data, $network_data); | |
236 | } | |
237 | ||
238 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); | |
239 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); | |
240 | ||
241 | my $files = { | |
242 | '/openstack/latest/user_data' => $user_data, | |
243 | '/openstack/content/0000' => $network_data, | |
244 | '/openstack/latest/meta_data.json' => $meta_data, | |
245 | '/openstack/latest/vendor_data.json' => $vendor_data | |
246 | }; | |
247 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2'); | |
248 | } | |
249 | ||
250 | sub generate_opennebula { | |
251 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; | |
252 | ||
253 | my $content = ""; | |
254 | ||
255 | my $username = $conf->{ciuser} || "root"; | |
256 | $content .= "USERNAME=$username\n" if defined($username); | |
257 | ||
258 | if (defined(my $password = $conf->{cipassword})) { | |
259 | $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64($password) ."\n"; | |
260 | } | |
261 | ||
262 | if (defined($conf->{sshkeys})) { | |
263 | my $keys = [ split(/\s*\n\s*/, URI::Escape::uri_unescape($conf->{sshkeys})) ]; | |
264 | $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n"; | |
265 | } | |
266 | ||
267 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); | |
268 | $content .= "SET_HOSTNAME=$hostname\n"; | |
269 | ||
270 | my ($searchdomains, $nameservers) = get_dns_conf($conf); | |
271 | $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers; | |
272 | $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains; | |
273 | ||
274 | my $networkenabled = undef; | |
275 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; | |
276 | foreach my $iface (sort @ifaces) { | |
277 | (my $id = $iface) =~ s/^net//; | |
278 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); | |
279 | next if !$conf->{"ipconfig$id"}; | |
280 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); | |
281 | my $ethid = "ETH$id"; | |
282 | ||
283 | my $mac = lc $net->{hwaddr}; | |
284 | ||
285 | if ($ipconfig->{ip}) { | |
286 | $networkenabled = 1; | |
287 | ||
288 | if ($ipconfig->{ip} eq 'dhcp') { | |
289 | $content .= "${ethid}_DHCP=YES\n"; | |
290 | } else { | |
291 | my ($addr, $mask) = split_ip4($ipconfig->{ip}); | |
292 | $content .= "${ethid}_IP=$addr\n"; | |
293 | $content .= "${ethid}_MASK=$mask\n"; | |
294 | $content .= "${ethid}_MAC=$mac\n"; | |
295 | $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw}; | |
296 | } | |
297 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; | |
298 | } | |
299 | ||
300 | if ($ipconfig->{ip6}) { | |
301 | $networkenabled = 1; | |
302 | if ($ipconfig->{ip6} eq 'dhcp') { | |
303 | $content .= "${ethid}_DHCP6=YES\n"; | |
304 | } elsif ($ipconfig->{ip6} eq 'auto') { | |
305 | $content .= "${ethid}_AUTO6=YES\n"; | |
306 | } else { | |
307 | my ($addr, $mask) = split('/', $ipconfig->{ip6}); | |
308 | $content .= "${ethid}_IP6=$addr\n"; | |
309 | $content .= "${ethid}_MASK6=$mask\n"; | |
310 | $content .= "${ethid}_MAC6=$mac\n"; | |
311 | $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6}; | |
312 | } | |
313 | $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu}; | |
314 | } | |
315 | } | |
316 | ||
317 | $content .= "NETWORK=YES\n" if $networkenabled; | |
318 | ||
319 | my $files = { '/context.sh' => $content }; | |
320 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT'); | |
321 | } | |
322 | ||
323 | sub nocloud_network_v2 { | |
324 | my ($conf) = @_; | |
325 | ||
326 | my $content = ''; | |
327 | ||
328 | my $head = "version: 2\n" | |
329 | . "ethernets:\n"; | |
330 | ||
331 | my $dns_done; | |
332 | ||
333 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; | |
334 | foreach my $iface (sort @ifaces) { | |
335 | (my $id = $iface) =~ s/^net//; | |
336 | next if !$conf->{"ipconfig$id"}; | |
337 | ||
338 | # indentation - network interfaces are inside an 'ethernets' hash | |
339 | my $i = ' '; | |
340 | ||
341 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); | |
342 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); | |
343 | ||
344 | my $mac = $net->{macaddr} | |
345 | or die "network interface '$iface' has no mac address\n"; | |
346 | ||
347 | $content .= "${i}$iface:\n"; | |
348 | $i .= ' '; | |
349 | $content .= "${i}match:\n" | |
350 | . "${i} macaddress: \"$mac\"\n" | |
351 | . "${i}set-name: eth$id\n"; | |
352 | my @addresses; | |
353 | if (defined(my $ip = $ipconfig->{ip})) { | |
354 | if ($ip eq 'dhcp') { | |
355 | $content .= "${i}dhcp4: true\n"; | |
356 | } else { | |
357 | push @addresses, $ip; | |
358 | } | |
359 | } | |
360 | if (defined(my $ip = $ipconfig->{ip6})) { | |
361 | if ($ip eq 'dhcp') { | |
362 | $content .= "${i}dhcp6: true\n"; | |
363 | } else { | |
364 | push @addresses, $ip; | |
365 | } | |
366 | } | |
367 | if (@addresses) { | |
368 | $content .= "${i}addresses:\n"; | |
369 | $content .= "${i}- '$_'\n" foreach @addresses; | |
370 | } | |
371 | if (defined(my $gw = $ipconfig->{gw})) { | |
372 | $content .= "${i}gateway4: '$gw'\n"; | |
373 | } | |
374 | if (defined(my $gw = $ipconfig->{gw6})) { | |
375 | $content .= "${i}gateway6: '$gw'\n"; | |
376 | } | |
377 | ||
378 | next if $dns_done; | |
379 | $dns_done = 1; | |
380 | ||
381 | my ($searchdomains, $nameservers) = get_dns_conf($conf); | |
382 | if ($searchdomains || $nameservers) { | |
383 | $content .= "${i}nameservers:\n"; | |
384 | if (defined($nameservers) && @$nameservers) { | |
385 | $content .= "${i} addresses:\n"; | |
386 | $content .= "${i} - '$_'\n" foreach @$nameservers; | |
387 | } | |
388 | if (defined($searchdomains) && @$searchdomains) { | |
389 | $content .= "${i} search:\n"; | |
390 | $content .= "${i} - '$_'\n" foreach @$searchdomains; | |
391 | } | |
392 | } | |
393 | } | |
394 | ||
395 | return $head.$content; | |
396 | } | |
397 | ||
398 | sub nocloud_network { | |
399 | my ($conf) = @_; | |
400 | ||
401 | my $content = "version: 1\n" | |
402 | . "config:\n"; | |
403 | ||
404 | my @ifaces = grep { /^net(\d+)$/ } keys %$conf; | |
405 | foreach my $iface (sort @ifaces) { | |
406 | (my $id = $iface) =~ s/^net//; | |
407 | next if !$conf->{"ipconfig$id"}; | |
408 | ||
409 | # indentation - network interfaces are inside an 'ethernets' hash | |
410 | my $i = ' '; | |
411 | ||
412 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); | |
413 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); | |
414 | ||
415 | my $mac = lc($net->{macaddr}) | |
416 | or die "network interface '$iface' has no mac address\n"; | |
417 | ||
418 | $content .= "${i}- type: physical\n" | |
419 | . "${i} name: eth$id\n" | |
420 | . "${i} mac_address: '$mac'\n" | |
421 | . "${i} subnets:\n"; | |
422 | $i .= ' '; | |
423 | if (defined(my $ip = $ipconfig->{ip})) { | |
424 | if ($ip eq 'dhcp') { | |
425 | $content .= "${i}- type: dhcp4\n"; | |
426 | } else { | |
427 | my ($addr, $mask) = split_ip4($ip); | |
428 | $content .= "${i}- type: static\n" | |
429 | . "${i} address: '$addr'\n" | |
430 | . "${i} netmask: '$mask'\n"; | |
431 | if (defined(my $gw = $ipconfig->{gw})) { | |
432 | $content .= "${i} gateway: '$gw'\n"; | |
433 | } | |
434 | } | |
435 | } | |
436 | if (defined(my $ip = $ipconfig->{ip6})) { | |
437 | if ($ip eq 'dhcp') { | |
438 | $content .= "${i}- type: dhcp6\n"; | |
439 | } elsif ($ip eq 'auto') { | |
440 | # SLAAC is only supported by cloud-init since 19.4 | |
441 | $content .= "${i}- type: ipv6_slaac\n"; | |
442 | } else { | |
443 | $content .= "${i}- type: static6\n" | |
444 | . "${i} address: '$ip'\n"; | |
445 | if (defined(my $gw = $ipconfig->{gw6})) { | |
446 | $content .= "${i} gateway: '$gw'\n"; | |
447 | } | |
448 | } | |
449 | } | |
450 | } | |
451 | ||
452 | my $i = ' '; | |
453 | my ($searchdomains, $nameservers) = get_dns_conf($conf); | |
454 | if ($searchdomains || $nameservers) { | |
455 | $content .= "${i}- type: nameserver\n"; | |
456 | if (defined($nameservers) && @$nameservers) { | |
457 | $content .= "${i} address:\n"; | |
458 | $content .= "${i} - '$_'\n" foreach @$nameservers; | |
459 | } | |
460 | if (defined($searchdomains) && @$searchdomains) { | |
461 | $content .= "${i} search:\n"; | |
462 | $content .= "${i} - '$_'\n" foreach @$searchdomains; | |
463 | } | |
464 | } | |
465 | ||
466 | return $content; | |
467 | } | |
468 | ||
469 | sub nocloud_metadata { | |
470 | my ($uuid) = @_; | |
471 | return "instance-id: $uuid\n"; | |
472 | } | |
473 | ||
474 | sub nocloud_gen_metadata { | |
475 | my ($user, $network) = @_; | |
476 | ||
477 | my $uuid_str = Digest::SHA::sha1_hex($user.$network); | |
478 | return nocloud_metadata($uuid_str); | |
479 | } | |
480 | ||
481 | sub generate_nocloud { | |
482 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; | |
483 | ||
484 | my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf); | |
485 | $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data); | |
486 | $network_data = nocloud_network($conf) if !defined($network_data); | |
487 | ||
488 | if (!defined($meta_data)) { | |
489 | $meta_data = nocloud_gen_metadata($user_data, $network_data); | |
490 | } | |
491 | ||
492 | my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data); | |
493 | die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024); | |
494 | ||
495 | my $files = { | |
496 | '/user-data' => $user_data, | |
497 | '/network-config' => $network_data, | |
498 | '/meta-data' => $meta_data, | |
499 | '/vendor-data' => $vendor_data | |
500 | }; | |
501 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata'); | |
502 | } | |
503 | ||
504 | sub get_custom_cloudinit_files { | |
505 | my ($conf) = @_; | |
506 | ||
507 | my $cicustom = $conf->{cicustom}; | |
508 | my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {}; | |
509 | ||
510 | my $network_volid = $files->{network}; | |
511 | my $user_volid = $files->{user}; | |
512 | my $meta_volid = $files->{meta}; | |
513 | my $vendor_volid = $files->{vendor}; | |
514 | ||
515 | my $storage_conf = PVE::Storage::config(); | |
516 | ||
517 | my $network_data; | |
518 | if ($network_volid) { | |
519 | $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid); | |
520 | } | |
521 | ||
522 | my $user_data; | |
523 | if ($user_volid) { | |
524 | $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid); | |
525 | } | |
526 | ||
527 | my $meta_data; | |
528 | if ($meta_volid) { | |
529 | $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid); | |
530 | } | |
531 | ||
532 | my $vendor_data; | |
533 | if ($vendor_volid) { | |
534 | $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid); | |
535 | } | |
536 | ||
537 | return ($user_data, $network_data, $meta_data, $vendor_data); | |
538 | } | |
539 | ||
540 | sub read_cloudinit_snippets_file { | |
541 | my ($storage_conf, $volid) = @_; | |
542 | ||
543 | my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid); | |
544 | die "$volid is not in the snippets directory\n" if $type ne 'snippets'; | |
545 | return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024); | |
546 | } | |
547 | ||
548 | my $cloudinit_methods = { | |
549 | configdrive2 => \&generate_configdrive2, | |
550 | nocloud => \&generate_nocloud, | |
551 | opennebula => \&generate_opennebula, | |
552 | }; | |
553 | ||
554 | sub generate_cloudinitconfig { | |
555 | my ($conf, $vmid) = @_; | |
556 | ||
557 | my $format = get_cloudinit_format($conf); | |
558 | ||
559 | PVE::QemuConfig->foreach_volume($conf, sub { | |
560 | my ($ds, $drive) = @_; | |
561 | ||
562 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); | |
563 | ||
564 | return if !$volname || $volname !~ m/vm-$vmid-cloudinit/; | |
565 | ||
566 | my $generator = $cloudinit_methods->{$format} | |
567 | or die "missing cloudinit methods for format '$format'\n"; | |
568 | ||
569 | $generator->($conf, $vmid, $drive, $volname, $storeid); | |
570 | }); | |
571 | } | |
572 | ||
573 | sub dump_cloudinit_config { | |
574 | my ($conf, $vmid, $type) = @_; | |
575 | ||
576 | my $format = get_cloudinit_format($conf); | |
577 | ||
578 | if ($type eq 'user') { | |
579 | return cloudinit_userdata($conf, $vmid); | |
580 | } elsif ($type eq 'network') { | |
581 | if ($format eq 'nocloud') { | |
582 | return nocloud_network($conf); | |
583 | } else { | |
584 | return configdrive2_network($conf); | |
585 | } | |
586 | } else { # metadata config | |
587 | my $user = cloudinit_userdata($conf, $vmid); | |
588 | if ($format eq 'nocloud') { | |
589 | my $network = nocloud_network($conf); | |
590 | return nocloud_gen_metadata($user, $network); | |
591 | } else { | |
592 | my $network = configdrive2_network($conf); | |
593 | return configdrive2_gen_metadata($user, $network); | |
594 | } | |
595 | } | |
596 | } | |
597 | ||
598 | 1; |