]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer/Cloudinit.pm
0e4bfd5ff8a8571f9f8136897e97acca99a714f3
[qemu-server.git] / PVE / QemuServer / 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
10 use PVE::Tools qw(run_command file_set_contents);
11 use PVE::Storage;
12 use PVE::QemuServer;
13
14 sub commit_cloudinit_disk {
15 my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_;
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 }
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);
33 my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
34
35 # required before file_size_info, some existing vols won't show up else
36 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
37 eval { $plugin->activate_volume($storeid, $scfg, $volname) };
38
39 my $size = eval { PVE::Storage::file_size_info($iso_path) };
40 if ($size <= 0) {
41 $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/;
42 my $name = $1;
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
46 $plugin->activate_volume($storeid, $scfg, $volname);
47 }
48
49 eval {
50 run_command([['genisoimage', '-R', '-V', $label, $path],
51 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
52 'isize=0', "osize=$size", "of=$iso_path"]]);
53 };
54 my $err = $@;
55 rmtree($path);
56 die $err if $err;
57 }
58
59 sub get_cloudinit_format {
60 my ($conf) = @_;
61 if (defined(my $format = $conf->{citype})) {
62 return $format;
63 }
64
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 }
73
74 return 'nocloud';
75 }
76
77 sub get_hostname_fqdn {
78 my ($conf, $vmid) = @_;
79 my $hostname = $conf->{name} // "VM$vmid";
80 my $fqdn;
81 if ($hostname =~ /\./) {
82 $fqdn = $hostname;
83 $hostname =~ s/\..*$//;
84 } elsif (my $search = $conf->{searchdomain}) {
85 $fqdn = "$hostname.$search";
86 }
87 return ($hostname, $fqdn);
88 }
89
90 sub 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
110 sub cloudinit_userdata {
111 my ($conf, $vmid) = @_;
112
113 my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
114
115 my $content = "#cloud-config\n";
116
117 $content .= "hostname: $hostname\n";
118 $content .= "manage_etc_hosts: true\n";
119 $content .= "fqdn: $fqdn\n" if defined($fqdn);
120
121 my $username = $conf->{ciuser};
122 my $password = $conf->{cipassword};
123
124 $content .= "user: $username\n" if defined($username);
125 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
126 $content .= "password: $password\n" if defined($password);
127
128 if (defined(my $keys = $conf->{sshkeys})) {
129 $keys = URI::Escape::uri_unescape($keys);
130 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
131 $keys = [grep { /\S/ } @$keys];
132 $content .= "ssh_authorized_keys:\n";
133 foreach my $k (@$keys) {
134 $content .= " - $k\n";
135 }
136 }
137 $content .= "chpasswd:\n";
138 $content .= " expire: False\n";
139
140 if (!defined($username) || $username ne 'root') {
141 $content .= "users:\n";
142 $content .= " - default\n";
143 }
144
145 $content .= "package_upgrade: true\n";
146
147 return $content;
148 }
149
150 sub 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
157 sub configdrive2_network {
158 my ($conf) = @_;
159
160 my $content = "auto lo\n";
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 }
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"};
177 my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
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 {
185 my ($addr, $mask) = split_ip4($net->{ip});
186 $content .= "iface $id inet static\n";
187 $content .= " address $addr\n";
188 $content .= " netmask $mask\n";
189 $content .= " gateway $net->{gw}\n" if $net->{gw};
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";
198 $content .= " address $addr\n";
199 $content .= " netmask $mask\n";
200 $content .= " gateway $net->{gw6}\n" if $net->{gw6};
201 }
202 }
203 }
204
205 return $content;
206 }
207
208 sub configdrive2_metadata {
209 my ($uuid) = @_;
210 return <<"EOF";
211 {
212 "uuid": "$uuid",
213 "network_config": { "content_path": "/content/0000" }
214 }
215 EOF
216 }
217
218 sub generate_configdrive2 {
219 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
220
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);
224
225 if (!defined($meta_data)) {
226 my $digest_data = $user_data . $network_data;
227 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
228
229 $meta_data = configdrive2_metadata($uuid_str);
230 }
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 };
236 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
237 }
238
239 sub nocloud_network_v2 {
240 my ($conf) = @_;
241
242 my $content = '';
243
244 my $head = "version: 2\n"
245 . "ethernets:\n";
246
247 my $dns_done;
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
263 $content .= "${i}$iface:\n";
264 $i .= ' ';
265 $content .= "${i}match:\n"
266 . "${i} macaddress: \"$mac\"\n"
267 . "${i}set-name: eth$id\n";
268 my @addresses;
269 if (defined(my $ip = $ipconfig->{ip})) {
270 if ($ip eq 'dhcp') {
271 $content .= "${i}dhcp4: true\n";
272 } else {
273 push @addresses, $ip;
274 }
275 }
276 if (defined(my $ip = $ipconfig->{ip6})) {
277 if ($ip eq 'dhcp') {
278 $content .= "${i}dhcp6: true\n";
279 } else {
280 push @addresses, $ip;
281 }
282 }
283 if (@addresses) {
284 $content .= "${i}addresses:\n";
285 $content .= "${i}- '$_'\n" foreach @addresses;
286 }
287 if (defined(my $gw = $ipconfig->{gw})) {
288 $content .= "${i}gateway4: '$gw'\n";
289 }
290 if (defined(my $gw = $ipconfig->{gw6})) {
291 $content .= "${i}gateway6: '$gw'\n";
292 }
293
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";
302 $content .= "${i} - '$_'\n" foreach @$nameservers;
303 }
304 if (defined($searchdomains) && @$searchdomains) {
305 $content .= "${i} search:\n";
306 $content .= "${i} - '$_'\n" foreach @$searchdomains;
307 }
308 }
309 }
310
311 return $head.$content;
312 }
313
314 sub 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
331 my $mac = lc($net->{macaddr})
332 or die "network interface '$iface' has no mac address\n";
333
334 $content .= "${i}- type: physical\n"
335 . "${i} name: eth$id\n"
336 . "${i} mac_address: '$mac'\n"
337 . "${i} subnets:\n";
338 $i .= ' ';
339 if (defined(my $ip = $ipconfig->{ip})) {
340 if ($ip eq 'dhcp') {
341 $content .= "${i}- type: dhcp4\n";
342 } else {
343 my ($addr, $mask) = split_ip4($ip);
344 $content .= "${i}- type: static\n"
345 . "${i} address: '$addr'\n"
346 . "${i} netmask: '$mask'\n";
347 if (defined(my $gw = $ipconfig->{gw})) {
348 $content .= "${i} gateway: '$gw'\n";
349 }
350 }
351 }
352 if (defined(my $ip = $ipconfig->{ip6})) {
353 if ($ip eq 'dhcp') {
354 $content .= "${i}- type: dhcp6\n";
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";
358 } else {
359 $content .= "${i}- type: static\n"
360 . "${i} address: '$ip'\n";
361 if (defined(my $gw = $ipconfig->{gw6})) {
362 $content .= "${i} gateway: '$gw'\n";
363 }
364 }
365 }
366 }
367
368 my $i = ' ';
369 my ($searchdomains, $nameservers) = get_dns_conf($conf);
370 if ($searchdomains || $nameservers) {
371 $content .= "${i}- type: nameserver\n";
372 if (defined($nameservers) && @$nameservers) {
373 $content .= "${i} address:\n";
374 $content .= "${i} - '$_'\n" foreach @$nameservers;
375 }
376 if (defined($searchdomains) && @$searchdomains) {
377 $content .= "${i} search:\n";
378 $content .= "${i} - '$_'\n" foreach @$searchdomains;
379 }
380 }
381
382 return $content;
383 }
384
385 sub nocloud_metadata {
386 my ($uuid) = @_;
387 return "instance-id: $uuid\n";
388 }
389
390 sub generate_nocloud {
391 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
392
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);
396
397 if (!defined($meta_data)) {
398 my $digest_data = $user_data . $network_data;
399 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
400
401 $meta_data = nocloud_metadata($uuid_str);
402 }
403
404 my $files = {
405 '/user-data' => $user_data,
406 '/network-config' => $network_data,
407 '/meta-data' => $meta_data
408 };
409 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
410 }
411
412 sub 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
442 sub 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';
447 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
448 }
449
450 my $cloudinit_methods = {
451 configdrive2 => \&generate_configdrive2,
452 nocloud => \&generate_nocloud,
453 };
454
455 sub 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 }
473
474 1;