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