]> git.proxmox.com Git - qemu-server.git/blame - PVE/QemuServer/Cloudinit.pm
add function to dump cloudinit config
[qemu-server.git] / PVE / QemuServer / Cloudinit.pm
CommitLineData
0c9a7596
AD
1package PVE::QemuServer::Cloudinit;
2
3use strict;
4use warnings;
5
6use File::Path;
7use Digest::SHA;
8use URI::Escape;
9
10use PVE::Tools qw(run_command file_set_contents);
11use PVE::Storage;
12use PVE::QemuServer;
13
7d761a01
ML
14use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes
15
0c9a7596 16sub commit_cloudinit_disk {
4a853915 17 my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_;
f62c36cf
WB
18
19 my $path = "/run/pve/cloudinit/$vmid/";
20 mkpath $path;
21 foreach my $filepath (keys %$files) {
22 if ($filepath !~ m@^(.*)\/[^/]+$@) {
23 die "internal error: bad file name in cloud-init image: $filepath\n";
24 }
25 my $dirname = $1;
26 mkpath "$path/$dirname";
27
28 my $contents = $files->{$filepath};
29 file_set_contents("$path/$filepath", $contents);
30 }
41cd94a0
WB
31
32 my $storecfg = PVE::Storage::config();
33 my $iso_path = PVE::Storage::path($storecfg, $drive->{file});
34 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
7e8ab2a9 35 my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
b56d56cf 36
5cda6c69 37 # required before file_size_info, some existing vols won't show up else
9680b09d
ML
38 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
39 eval { $plugin->activate_volume($storeid, $scfg, $volname) };
40
b56d56cf 41 my $size = eval { PVE::Storage::file_size_info($iso_path) };
6aaf593c 42 if ($size <= 0) {
92fcaab7 43 $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/;
7e8ab2a9 44 my $name = $1;
84821d15
TL
45 $size = 4 * 1024;
46 PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size);
47 $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte
9680b09d 48 $plugin->activate_volume($storeid, $scfg, $volname);
7e8ab2a9
ML
49 }
50
f62c36cf
WB
51 eval {
52 run_command([['genisoimage', '-R', '-V', $label, $path],
f0a762f7 53 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
f62c36cf
WB
54 'isize=0', "osize=$size", "of=$iso_path"]]);
55 };
56 my $err = $@;
57 rmtree($path);
58 die $err if $err;
0c9a7596
AD
59}
60
41cd94a0
WB
61sub get_cloudinit_format {
62 my ($conf) = @_;
63 if (defined(my $format = $conf->{citype})) {
64 return $format;
65 }
0c9a7596 66
41cd94a0
WB
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 }
0c9a7596 75
41cd94a0 76 return 'nocloud';
0c9a7596
AD
77}
78
e8ac2138 79sub get_hostname_fqdn {
9a6ccb12
DC
80 my ($conf, $vmid) = @_;
81 my $hostname = $conf->{name} // "VM$vmid";
e8ac2138
WB
82 my $fqdn;
83 if ($hostname =~ /\./) {
84 $fqdn = $hostname;
85 $hostname =~ s/\..*$//;
86 } elsif (my $search = $conf->{searchdomain}) {
87 $fqdn = "$hostname.$search";
0c9a7596 88 }
e8ac2138 89 return ($hostname, $fqdn);
41cd94a0
WB
90}
91
67864d19
WB
92sub 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
41cd94a0 112sub cloudinit_userdata {
9a6ccb12 113 my ($conf, $vmid) = @_;
41cd94a0 114
9a6ccb12 115 my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
41cd94a0 116
7b42f951 117 my $content = "#cloud-config\n";
41cd94a0 118
e8ac2138 119 $content .= "hostname: $hostname\n";
8de34458 120 $content .= "manage_etc_hosts: true\n";
e8ac2138
WB
121 $content .= "fqdn: $fqdn\n" if defined($fqdn);
122
7b42f951
WB
123 my $username = $conf->{ciuser};
124 my $password = $conf->{cipassword};
41cd94a0
WB
125
126 $content .= "user: $username\n" if defined($username);
7b42f951
WB
127 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
128 $content .= "password: $password\n" if defined($password);
41cd94a0
WB
129
130 if (defined(my $keys = $conf->{sshkeys})) {
0c9a7596
AD
131 $keys = URI::Escape::uri_unescape($keys);
132 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
133 $keys = [grep { /\S/ } @$keys];
41cd94a0 134 $content .= "ssh_authorized_keys:\n";
0c9a7596 135 foreach my $k (@$keys) {
41cd94a0 136 $content .= " - $k\n";
0c9a7596
AD
137 }
138 }
41cd94a0
WB
139 $content .= "chpasswd:\n";
140 $content .= " expire: False\n";
141
7b42f951
WB
142 if (!defined($username) || $username ne 'root') {
143 $content .= "users:\n";
144 $content .= " - default\n";
145 }
0c9a7596
AD
146
147 $content .= "package_upgrade: true\n";
148
0c9a7596
AD
149 return $content;
150}
151
86280789
WB
152sub 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
41cd94a0
WB
159sub configdrive2_network {
160 my ($conf) = @_;
0c9a7596
AD
161
162 my $content = "auto lo\n";
67864d19
WB
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 }
0c9a7596
AD
174
175 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
176 foreach my $iface (@ifaces) {
177 (my $id = $iface) =~ s/^net//;
178 next if !$conf->{"ipconfig$id"};
41cd94a0 179 my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
0c9a7596
AD
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 {
86280789 187 my ($addr, $mask) = split_ip4($net->{ip});
0c9a7596 188 $content .= "iface $id inet static\n";
6f3999e0
ML
189 $content .= " address $addr\n";
190 $content .= " netmask $mask\n";
191 $content .= " gateway $net->{gw}\n" if $net->{gw};
0c9a7596
AD
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";
6f3999e0
ML
200 $content .= " address $addr\n";
201 $content .= " netmask $mask\n";
202 $content .= " gateway $net->{gw6}\n" if $net->{gw6};
0c9a7596
AD
203 }
204 }
205 }
206
0c9a7596
AD
207 return $content;
208}
209
e73ca4d0
ML
210sub 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
41cd94a0
WB
217sub configdrive2_metadata {
218 my ($uuid) = @_;
219 return <<"EOF";
220{
221 "uuid": "$uuid",
222 "network_config": { "content_path": "/content/0000" }
223}
224EOF
225}
226
227sub generate_configdrive2 {
228 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
229
cb702ebe
DL
230 my ($user_data, $network_data, $meta_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);
41cd94a0 233
cb702ebe 234 if (!defined($meta_data)) {
e73ca4d0 235 $meta_data = configdrive2_gen_metadata($user_data, $network_data);
cb702ebe 236 }
f62c36cf
WB
237 my $files = {
238 '/openstack/latest/user_data' => $user_data,
239 '/openstack/content/0000' => $network_data,
240 '/openstack/latest/meta_data.json' => $meta_data
241 };
4a853915 242 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
41cd94a0
WB
243}
244
245sub nocloud_network_v2 {
246 my ($conf) = @_;
247
248 my $content = '';
249
250 my $head = "version: 2\n"
251 . "ethernets:\n";
252
67864d19 253 my $dns_done;
41cd94a0
WB
254
255 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
256 foreach my $iface (@ifaces) {
257 (my $id = $iface) =~ s/^net//;
258 next if !$conf->{"ipconfig$id"};
259
260 # indentation - network interfaces are inside an 'ethernets' hash
261 my $i = ' ';
262
263 my $net = PVE::QemuServer::parse_net($conf->{$iface});
264 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
265
266 my $mac = $net->{macaddr}
267 or die "network interface '$iface' has no mac address\n";
268
67864d19 269 $content .= "${i}$iface:\n";
41cd94a0 270 $i .= ' ';
67864d19
WB
271 $content .= "${i}match:\n"
272 . "${i} macaddress: \"$mac\"\n"
273 . "${i}set-name: eth$id\n";
41cd94a0
WB
274 my @addresses;
275 if (defined(my $ip = $ipconfig->{ip})) {
276 if ($ip eq 'dhcp') {
67864d19 277 $content .= "${i}dhcp4: true\n";
41cd94a0
WB
278 } else {
279 push @addresses, $ip;
280 }
281 }
282 if (defined(my $ip = $ipconfig->{ip6})) {
283 if ($ip eq 'dhcp') {
67864d19 284 $content .= "${i}dhcp6: true\n";
41cd94a0
WB
285 } else {
286 push @addresses, $ip;
287 }
288 }
289 if (@addresses) {
67864d19 290 $content .= "${i}addresses:\n";
4efb58a9 291 $content .= "${i}- '$_'\n" foreach @addresses;
41cd94a0
WB
292 }
293 if (defined(my $gw = $ipconfig->{gw})) {
4efb58a9 294 $content .= "${i}gateway4: '$gw'\n";
41cd94a0
WB
295 }
296 if (defined(my $gw = $ipconfig->{gw6})) {
4efb58a9 297 $content .= "${i}gateway6: '$gw'\n";
41cd94a0
WB
298 }
299
67864d19
WB
300 next if $dns_done;
301 $dns_done = 1;
302
303 my ($searchdomains, $nameservers) = get_dns_conf($conf);
304 if ($searchdomains || $nameservers) {
305 $content .= "${i}nameservers:\n";
306 if (defined($nameservers) && @$nameservers) {
307 $content .= "${i} addresses:\n";
4efb58a9 308 $content .= "${i} - '$_'\n" foreach @$nameservers;
67864d19
WB
309 }
310 if (defined($searchdomains) && @$searchdomains) {
311 $content .= "${i} search:\n";
4efb58a9 312 $content .= "${i} - '$_'\n" foreach @$searchdomains;
41cd94a0
WB
313 }
314 }
41cd94a0
WB
315 }
316
317 return $head.$content;
318}
319
320sub nocloud_network {
321 my ($conf) = @_;
322
323 my $content = "version: 1\n"
324 . "config:\n";
325
326 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
327 foreach my $iface (@ifaces) {
328 (my $id = $iface) =~ s/^net//;
329 next if !$conf->{"ipconfig$id"};
330
331 # indentation - network interfaces are inside an 'ethernets' hash
332 my $i = ' ';
333
334 my $net = PVE::QemuServer::parse_net($conf->{$iface});
335 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
336
c3cedb3d 337 my $mac = lc($net->{macaddr})
41cd94a0
WB
338 or die "network interface '$iface' has no mac address\n";
339
67864d19
WB
340 $content .= "${i}- type: physical\n"
341 . "${i} name: eth$id\n"
4efb58a9 342 . "${i} mac_address: '$mac'\n"
67864d19 343 . "${i} subnets:\n";
41cd94a0
WB
344 $i .= ' ';
345 if (defined(my $ip = $ipconfig->{ip})) {
346 if ($ip eq 'dhcp') {
67864d19 347 $content .= "${i}- type: dhcp4\n";
41cd94a0 348 } else {
86280789 349 my ($addr, $mask) = split_ip4($ip);
67864d19 350 $content .= "${i}- type: static\n"
4efb58a9
DL
351 . "${i} address: '$addr'\n"
352 . "${i} netmask: '$mask'\n";
41cd94a0 353 if (defined(my $gw = $ipconfig->{gw})) {
4efb58a9 354 $content .= "${i} gateway: '$gw'\n";
41cd94a0
WB
355 }
356 }
357 }
358 if (defined(my $ip = $ipconfig->{ip6})) {
359 if ($ip eq 'dhcp') {
67864d19 360 $content .= "${i}- type: dhcp6\n";
c701be32
DL
361 } elsif ($ip eq 'auto') {
362 # SLAAC is not supported by cloud-init, this fallback should work with an up-to-date netplan at least
363 $content .= "${i}- type: dhcp6\n";
41cd94a0 364 } else {
8d54522b 365 $content .= "${i}- type: static\n"
4efb58a9 366 . "${i} address: '$ip'\n";
41cd94a0 367 if (defined(my $gw = $ipconfig->{gw6})) {
4efb58a9 368 $content .= "${i} gateway: '$gw'\n";
41cd94a0
WB
369 }
370 }
371 }
41cd94a0
WB
372 }
373
67864d19
WB
374 my $i = ' ';
375 my ($searchdomains, $nameservers) = get_dns_conf($conf);
376 if ($searchdomains || $nameservers) {
41cd94a0 377 $content .= "${i}- type: nameserver\n";
67864d19 378 if (defined($nameservers) && @$nameservers) {
41cd94a0 379 $content .= "${i} address:\n";
4efb58a9 380 $content .= "${i} - '$_'\n" foreach @$nameservers;
41cd94a0 381 }
67864d19 382 if (defined($searchdomains) && @$searchdomains) {
41cd94a0 383 $content .= "${i} search:\n";
4efb58a9 384 $content .= "${i} - '$_'\n" foreach @$searchdomains;
41cd94a0
WB
385 }
386 }
387
388 return $content;
389}
390
391sub nocloud_metadata {
e8ac2138
WB
392 my ($uuid) = @_;
393 return "instance-id: $uuid\n";
41cd94a0
WB
394}
395
e73ca4d0
ML
396sub nocloud_gen_metadata {
397 my ($user, $network) = @_;
398
399 my $uuid_str = Digest::SHA::sha1_hex($user.$network);
400 return nocloud_metadata($uuid_str);
401}
402
41cd94a0
WB
403sub generate_nocloud {
404 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
405
cb702ebe
DL
406 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
407 $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
408 $network_data = nocloud_network($conf) if !defined($network_data);
41cd94a0 409
cb702ebe 410 if (!defined($meta_data)) {
e73ca4d0 411 $meta_data = nocloud_gen_metadata($user_data, $network_data);
cb702ebe 412 }
41cd94a0 413
f62c36cf
WB
414 my $files = {
415 '/user-data' => $user_data,
416 '/network-config' => $network_data,
417 '/meta-data' => $meta_data
418 };
4a853915 419 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
41cd94a0
WB
420}
421
cb702ebe
DL
422sub get_custom_cloudinit_files {
423 my ($conf) = @_;
424
425 my $cicustom = $conf->{cicustom};
426 my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
427
428 my $network_volid = $files->{network};
429 my $user_volid = $files->{user};
430 my $meta_volid = $files->{meta};
431
432 my $storage_conf = PVE::Storage::config();
433
434 my $network_data;
435 if ($network_volid) {
436 $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
437 }
438
439 my $user_data;
440 if ($user_volid) {
441 $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
442 }
443
444 my $meta_data;
445 if ($meta_volid) {
446 $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
447 }
448
449 return ($user_data, $network_data, $meta_data);
450}
451
452sub read_cloudinit_snippets_file {
453 my ($storage_conf, $volid) = @_;
454
455 my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
456 die "$volid is not in the snippets directory\n" if $type ne 'snippets';
7e8ab2a9 457 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
cb702ebe
DL
458}
459
41cd94a0
WB
460my $cloudinit_methods = {
461 configdrive2 => \&generate_configdrive2,
462 nocloud => \&generate_nocloud,
463};
464
465sub generate_cloudinitconfig {
466 my ($conf, $vmid) = @_;
467
468 my $format = get_cloudinit_format($conf);
469
470 PVE::QemuServer::foreach_drive($conf, sub {
471 my ($ds, $drive) = @_;
472
473 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
474
475 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
476
477 my $generator = $cloudinit_methods->{$format}
478 or die "missing cloudinit methods for format '$format'\n";
479
480 $generator->($conf, $vmid, $drive, $volname, $storeid);
481 });
482}
0c9a7596 483
e73ca4d0
ML
484sub dump_cloudinit_config {
485 my ($conf, $vmid, $type) = @_;
486
487 my $format = get_cloudinit_format($conf);
488
489 if ($type eq 'user') {
490 return cloudinit_userdata($conf, $vmid);
491 } elsif ($type eq 'network') {
492 if ($format eq 'nocloud') {
493 return nocloud_network($conf);
494 } else {
495 return configdrive2_network($conf);
496 }
497 } else { # metadata config
498 my $user = cloudinit_userdata($conf, $vmid);
499 if ($format eq 'nocloud') {
500 my $network = nocloud_network($conf);
501 return nocloud_gen_metadata($user, $network);
502 } else {
503 my $network = configdrive2_network($conf);
504 return configdrive2_gen_metadata($user, $network);
505 }
506 }
507}
508
0c9a7596 5091;