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