]> git.proxmox.com Git - qemu-server.git/blame - PVE/QemuServer/Cloudinit.pm
cloudinit: use detected format in volname parsing
[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) {
92fcaab7 37 $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/;
7e8ab2a9 38 my $name = $1;
84821d15
TL
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
7e8ab2a9
ML
42 }
43
9be87f4e
DC
44 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
45 $plugin->activate_volume($storeid, $scfg, $volname);
0c9a7596 46
f62c36cf
WB
47 eval {
48 run_command([['genisoimage', '-R', '-V', $label, $path],
f0a762f7 49 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
f62c36cf
WB
50 'isize=0', "osize=$size", "of=$iso_path"]]);
51 };
52 my $err = $@;
53 rmtree($path);
54 die $err if $err;
0c9a7596
AD
55}
56
41cd94a0
WB
57sub get_cloudinit_format {
58 my ($conf) = @_;
59 if (defined(my $format = $conf->{citype})) {
60 return $format;
61 }
0c9a7596 62
41cd94a0
WB
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 }
0c9a7596 71
41cd94a0 72 return 'nocloud';
0c9a7596
AD
73}
74
e8ac2138 75sub get_hostname_fqdn {
9a6ccb12
DC
76 my ($conf, $vmid) = @_;
77 my $hostname = $conf->{name} // "VM$vmid";
e8ac2138
WB
78 my $fqdn;
79 if ($hostname =~ /\./) {
80 $fqdn = $hostname;
81 $hostname =~ s/\..*$//;
82 } elsif (my $search = $conf->{searchdomain}) {
83 $fqdn = "$hostname.$search";
0c9a7596 84 }
e8ac2138 85 return ($hostname, $fqdn);
41cd94a0
WB
86}
87
67864d19
WB
88sub 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
41cd94a0 108sub cloudinit_userdata {
9a6ccb12 109 my ($conf, $vmid) = @_;
41cd94a0 110
9a6ccb12 111 my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
41cd94a0 112
7b42f951 113 my $content = "#cloud-config\n";
41cd94a0 114
e8ac2138 115 $content .= "hostname: $hostname\n";
8de34458 116 $content .= "manage_etc_hosts: true\n";
e8ac2138
WB
117 $content .= "fqdn: $fqdn\n" if defined($fqdn);
118
7b42f951
WB
119 my $username = $conf->{ciuser};
120 my $password = $conf->{cipassword};
41cd94a0
WB
121
122 $content .= "user: $username\n" if defined($username);
7b42f951
WB
123 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
124 $content .= "password: $password\n" if defined($password);
41cd94a0
WB
125
126 if (defined(my $keys = $conf->{sshkeys})) {
0c9a7596
AD
127 $keys = URI::Escape::uri_unescape($keys);
128 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
129 $keys = [grep { /\S/ } @$keys];
41cd94a0 130 $content .= "ssh_authorized_keys:\n";
0c9a7596 131 foreach my $k (@$keys) {
41cd94a0 132 $content .= " - $k\n";
0c9a7596
AD
133 }
134 }
41cd94a0
WB
135 $content .= "chpasswd:\n";
136 $content .= " expire: False\n";
137
7b42f951
WB
138 if (!defined($username) || $username ne 'root') {
139 $content .= "users:\n";
140 $content .= " - default\n";
141 }
0c9a7596
AD
142
143 $content .= "package_upgrade: true\n";
144
0c9a7596
AD
145 return $content;
146}
147
86280789
WB
148sub 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
41cd94a0
WB
155sub configdrive2_network {
156 my ($conf) = @_;
0c9a7596
AD
157
158 my $content = "auto lo\n";
67864d19
WB
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 }
0c9a7596
AD
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"};
41cd94a0 175 my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
0c9a7596
AD
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 {
86280789 183 my ($addr, $mask) = split_ip4($net->{ip});
0c9a7596 184 $content .= "iface $id inet static\n";
6f3999e0
ML
185 $content .= " address $addr\n";
186 $content .= " netmask $mask\n";
187 $content .= " gateway $net->{gw}\n" if $net->{gw};
0c9a7596
AD
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";
6f3999e0
ML
196 $content .= " address $addr\n";
197 $content .= " netmask $mask\n";
198 $content .= " gateway $net->{gw6}\n" if $net->{gw6};
0c9a7596
AD
199 }
200 }
201 }
202
0c9a7596
AD
203 return $content;
204}
205
41cd94a0
WB
206sub configdrive2_metadata {
207 my ($uuid) = @_;
208 return <<"EOF";
209{
210 "uuid": "$uuid",
211 "network_config": { "content_path": "/content/0000" }
212}
213EOF
214}
215
216sub generate_configdrive2 {
217 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
218
cb702ebe
DL
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);
41cd94a0 222
cb702ebe
DL
223 if (!defined($meta_data)) {
224 my $digest_data = $user_data . $network_data;
225 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
41cd94a0 226
cb702ebe
DL
227 $meta_data = configdrive2_metadata($uuid_str);
228 }
f62c36cf
WB
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 };
4a853915 234 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
41cd94a0
WB
235}
236
237sub nocloud_network_v2 {
238 my ($conf) = @_;
239
240 my $content = '';
241
242 my $head = "version: 2\n"
243 . "ethernets:\n";
244
67864d19 245 my $dns_done;
41cd94a0
WB
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
67864d19 261 $content .= "${i}$iface:\n";
41cd94a0 262 $i .= ' ';
67864d19
WB
263 $content .= "${i}match:\n"
264 . "${i} macaddress: \"$mac\"\n"
265 . "${i}set-name: eth$id\n";
41cd94a0
WB
266 my @addresses;
267 if (defined(my $ip = $ipconfig->{ip})) {
268 if ($ip eq 'dhcp') {
67864d19 269 $content .= "${i}dhcp4: true\n";
41cd94a0
WB
270 } else {
271 push @addresses, $ip;
272 }
273 }
274 if (defined(my $ip = $ipconfig->{ip6})) {
275 if ($ip eq 'dhcp') {
67864d19 276 $content .= "${i}dhcp6: true\n";
41cd94a0
WB
277 } else {
278 push @addresses, $ip;
279 }
280 }
281 if (@addresses) {
67864d19 282 $content .= "${i}addresses:\n";
4efb58a9 283 $content .= "${i}- '$_'\n" foreach @addresses;
41cd94a0
WB
284 }
285 if (defined(my $gw = $ipconfig->{gw})) {
4efb58a9 286 $content .= "${i}gateway4: '$gw'\n";
41cd94a0
WB
287 }
288 if (defined(my $gw = $ipconfig->{gw6})) {
4efb58a9 289 $content .= "${i}gateway6: '$gw'\n";
41cd94a0
WB
290 }
291
67864d19
WB
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";
4efb58a9 300 $content .= "${i} - '$_'\n" foreach @$nameservers;
67864d19
WB
301 }
302 if (defined($searchdomains) && @$searchdomains) {
303 $content .= "${i} search:\n";
4efb58a9 304 $content .= "${i} - '$_'\n" foreach @$searchdomains;
41cd94a0
WB
305 }
306 }
41cd94a0
WB
307 }
308
309 return $head.$content;
310}
311
312sub 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
c3cedb3d 329 my $mac = lc($net->{macaddr})
41cd94a0
WB
330 or die "network interface '$iface' has no mac address\n";
331
67864d19
WB
332 $content .= "${i}- type: physical\n"
333 . "${i} name: eth$id\n"
4efb58a9 334 . "${i} mac_address: '$mac'\n"
67864d19 335 . "${i} subnets:\n";
41cd94a0
WB
336 $i .= ' ';
337 if (defined(my $ip = $ipconfig->{ip})) {
338 if ($ip eq 'dhcp') {
67864d19 339 $content .= "${i}- type: dhcp4\n";
41cd94a0 340 } else {
86280789 341 my ($addr, $mask) = split_ip4($ip);
67864d19 342 $content .= "${i}- type: static\n"
4efb58a9
DL
343 . "${i} address: '$addr'\n"
344 . "${i} netmask: '$mask'\n";
41cd94a0 345 if (defined(my $gw = $ipconfig->{gw})) {
4efb58a9 346 $content .= "${i} gateway: '$gw'\n";
41cd94a0
WB
347 }
348 }
349 }
350 if (defined(my $ip = $ipconfig->{ip6})) {
351 if ($ip eq 'dhcp') {
67864d19 352 $content .= "${i}- type: dhcp6\n";
c701be32
DL
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";
41cd94a0 356 } else {
8d54522b 357 $content .= "${i}- type: static\n"
4efb58a9 358 . "${i} address: '$ip'\n";
41cd94a0 359 if (defined(my $gw = $ipconfig->{gw6})) {
4efb58a9 360 $content .= "${i} gateway: '$gw'\n";
41cd94a0
WB
361 }
362 }
363 }
41cd94a0
WB
364 }
365
67864d19
WB
366 my $i = ' ';
367 my ($searchdomains, $nameservers) = get_dns_conf($conf);
368 if ($searchdomains || $nameservers) {
41cd94a0 369 $content .= "${i}- type: nameserver\n";
67864d19 370 if (defined($nameservers) && @$nameservers) {
41cd94a0 371 $content .= "${i} address:\n";
4efb58a9 372 $content .= "${i} - '$_'\n" foreach @$nameservers;
41cd94a0 373 }
67864d19 374 if (defined($searchdomains) && @$searchdomains) {
41cd94a0 375 $content .= "${i} search:\n";
4efb58a9 376 $content .= "${i} - '$_'\n" foreach @$searchdomains;
41cd94a0
WB
377 }
378 }
379
380 return $content;
381}
382
383sub nocloud_metadata {
e8ac2138
WB
384 my ($uuid) = @_;
385 return "instance-id: $uuid\n";
41cd94a0
WB
386}
387
388sub generate_nocloud {
389 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
390
cb702ebe
DL
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);
41cd94a0 394
cb702ebe
DL
395 if (!defined($meta_data)) {
396 my $digest_data = $user_data . $network_data;
397 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
41cd94a0 398
cb702ebe
DL
399 $meta_data = nocloud_metadata($uuid_str);
400 }
41cd94a0 401
f62c36cf
WB
402 my $files = {
403 '/user-data' => $user_data,
404 '/network-config' => $network_data,
405 '/meta-data' => $meta_data
406 };
4a853915 407 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
41cd94a0
WB
408}
409
cb702ebe
DL
410sub 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
440sub 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';
7e8ab2a9 445 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
cb702ebe
DL
446}
447
41cd94a0
WB
448my $cloudinit_methods = {
449 configdrive2 => \&generate_configdrive2,
450 nocloud => \&generate_nocloud,
451};
452
453sub 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}
0c9a7596
AD
471
4721;