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