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