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