]> git.proxmox.com Git - qemu-server.git/blame - PVE/QemuServer/Cloudinit.pm
bump version to 5.0-49
[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);
9be87f4e
DC
33 my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
34 $plugin->activate_volume($storeid, $scfg, $volname);
41cd94a0 35 my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
0c9a7596 36
3db6e4ab 37 my $size = PVE::Storage::file_size_info($iso_path);
0c9a7596 38
f62c36cf
WB
39 eval {
40 run_command([['genisoimage', '-R', '-V', $label, $path],
f0a762f7 41 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
f62c36cf
WB
42 'isize=0', "osize=$size", "of=$iso_path"]]);
43 };
44 my $err = $@;
45 rmtree($path);
46 die $err if $err;
0c9a7596
AD
47}
48
41cd94a0
WB
49sub get_cloudinit_format {
50 my ($conf) = @_;
51 if (defined(my $format = $conf->{citype})) {
52 return $format;
53 }
0c9a7596 54
41cd94a0
WB
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 }
0c9a7596 63
41cd94a0 64 return 'nocloud';
0c9a7596
AD
65}
66
e8ac2138 67sub get_hostname_fqdn {
9a6ccb12
DC
68 my ($conf, $vmid) = @_;
69 my $hostname = $conf->{name} // "VM$vmid";
e8ac2138
WB
70 my $fqdn;
71 if ($hostname =~ /\./) {
72 $fqdn = $hostname;
73 $hostname =~ s/\..*$//;
74 } elsif (my $search = $conf->{searchdomain}) {
75 $fqdn = "$hostname.$search";
0c9a7596 76 }
e8ac2138 77 return ($hostname, $fqdn);
41cd94a0
WB
78}
79
67864d19
WB
80sub 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
41cd94a0 100sub cloudinit_userdata {
9a6ccb12 101 my ($conf, $vmid) = @_;
41cd94a0 102
9a6ccb12 103 my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
41cd94a0 104
7b42f951 105 my $content = "#cloud-config\n";
41cd94a0 106
e8ac2138 107 $content .= "hostname: $hostname\n";
8de34458 108 $content .= "manage_etc_hosts: true\n";
e8ac2138
WB
109 $content .= "fqdn: $fqdn\n" if defined($fqdn);
110
7b42f951
WB
111 my $username = $conf->{ciuser};
112 my $password = $conf->{cipassword};
41cd94a0
WB
113
114 $content .= "user: $username\n" if defined($username);
7b42f951
WB
115 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
116 $content .= "password: $password\n" if defined($password);
41cd94a0
WB
117
118 if (defined(my $keys = $conf->{sshkeys})) {
0c9a7596
AD
119 $keys = URI::Escape::uri_unescape($keys);
120 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
121 $keys = [grep { /\S/ } @$keys];
41cd94a0 122 $content .= "ssh_authorized_keys:\n";
0c9a7596 123 foreach my $k (@$keys) {
41cd94a0 124 $content .= " - $k\n";
0c9a7596
AD
125 }
126 }
41cd94a0
WB
127 $content .= "chpasswd:\n";
128 $content .= " expire: False\n";
129
7b42f951
WB
130 if (!defined($username) || $username ne 'root') {
131 $content .= "users:\n";
132 $content .= " - default\n";
133 }
0c9a7596
AD
134
135 $content .= "package_upgrade: true\n";
136
0c9a7596
AD
137 return $content;
138}
139
86280789
WB
140sub 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
41cd94a0
WB
147sub configdrive2_network {
148 my ($conf) = @_;
0c9a7596
AD
149
150 my $content = "auto lo\n";
67864d19
WB
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 }
0c9a7596
AD
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"};
41cd94a0 167 my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
0c9a7596
AD
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 {
86280789 175 my ($addr, $mask) = split_ip4($net->{ip});
0c9a7596 176 $content .= "iface $id inet static\n";
4efb58a9
DL
177 $content .= " address '$addr'\n";
178 $content .= " netmask '$mask'\n";
179 $content .= " gateway '$net->{gw}'\n" if $net->{gw};
0c9a7596
AD
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";
4efb58a9
DL
188 $content .= " address '$addr'\n";
189 $content .= " netmask '$mask'\n";
190 $content .= " gateway '$net->{gw6}'\n" if $net->{gw6};
0c9a7596
AD
191 }
192 }
193 }
194
0c9a7596
AD
195 return $content;
196}
197
41cd94a0
WB
198sub configdrive2_metadata {
199 my ($uuid) = @_;
200 return <<"EOF";
201{
202 "uuid": "$uuid",
203 "network_config": { "content_path": "/content/0000" }
204}
205EOF
206}
207
208sub generate_configdrive2 {
209 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
210
cb702ebe
DL
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);
41cd94a0 214
cb702ebe
DL
215 if (!defined($meta_data)) {
216 my $digest_data = $user_data . $network_data;
217 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
41cd94a0 218
cb702ebe
DL
219 $meta_data = configdrive2_metadata($uuid_str);
220 }
f62c36cf
WB
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 };
4a853915 226 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
41cd94a0
WB
227}
228
229sub nocloud_network_v2 {
230 my ($conf) = @_;
231
232 my $content = '';
233
234 my $head = "version: 2\n"
235 . "ethernets:\n";
236
67864d19 237 my $dns_done;
41cd94a0
WB
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
67864d19 253 $content .= "${i}$iface:\n";
41cd94a0 254 $i .= ' ';
67864d19
WB
255 $content .= "${i}match:\n"
256 . "${i} macaddress: \"$mac\"\n"
257 . "${i}set-name: eth$id\n";
41cd94a0
WB
258 my @addresses;
259 if (defined(my $ip = $ipconfig->{ip})) {
260 if ($ip eq 'dhcp') {
67864d19 261 $content .= "${i}dhcp4: true\n";
41cd94a0
WB
262 } else {
263 push @addresses, $ip;
264 }
265 }
266 if (defined(my $ip = $ipconfig->{ip6})) {
267 if ($ip eq 'dhcp') {
67864d19 268 $content .= "${i}dhcp6: true\n";
41cd94a0
WB
269 } else {
270 push @addresses, $ip;
271 }
272 }
273 if (@addresses) {
67864d19 274 $content .= "${i}addresses:\n";
4efb58a9 275 $content .= "${i}- '$_'\n" foreach @addresses;
41cd94a0
WB
276 }
277 if (defined(my $gw = $ipconfig->{gw})) {
4efb58a9 278 $content .= "${i}gateway4: '$gw'\n";
41cd94a0
WB
279 }
280 if (defined(my $gw = $ipconfig->{gw6})) {
4efb58a9 281 $content .= "${i}gateway6: '$gw'\n";
41cd94a0
WB
282 }
283
67864d19
WB
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";
4efb58a9 292 $content .= "${i} - '$_'\n" foreach @$nameservers;
67864d19
WB
293 }
294 if (defined($searchdomains) && @$searchdomains) {
295 $content .= "${i} search:\n";
4efb58a9 296 $content .= "${i} - '$_'\n" foreach @$searchdomains;
41cd94a0
WB
297 }
298 }
41cd94a0
WB
299 }
300
301 return $head.$content;
302}
303
304sub 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
c3cedb3d 321 my $mac = lc($net->{macaddr})
41cd94a0
WB
322 or die "network interface '$iface' has no mac address\n";
323
67864d19
WB
324 $content .= "${i}- type: physical\n"
325 . "${i} name: eth$id\n"
4efb58a9 326 . "${i} mac_address: '$mac'\n"
67864d19 327 . "${i} subnets:\n";
41cd94a0
WB
328 $i .= ' ';
329 if (defined(my $ip = $ipconfig->{ip})) {
330 if ($ip eq 'dhcp') {
67864d19 331 $content .= "${i}- type: dhcp4\n";
41cd94a0 332 } else {
86280789 333 my ($addr, $mask) = split_ip4($ip);
67864d19 334 $content .= "${i}- type: static\n"
4efb58a9
DL
335 . "${i} address: '$addr'\n"
336 . "${i} netmask: '$mask'\n";
41cd94a0 337 if (defined(my $gw = $ipconfig->{gw})) {
4efb58a9 338 $content .= "${i} gateway: '$gw'\n";
41cd94a0
WB
339 }
340 }
341 }
342 if (defined(my $ip = $ipconfig->{ip6})) {
343 if ($ip eq 'dhcp') {
67864d19 344 $content .= "${i}- type: dhcp6\n";
c701be32
DL
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";
41cd94a0 348 } else {
8d54522b 349 $content .= "${i}- type: static\n"
4efb58a9 350 . "${i} address: '$ip'\n";
41cd94a0 351 if (defined(my $gw = $ipconfig->{gw6})) {
4efb58a9 352 $content .= "${i} gateway: '$gw'\n";
41cd94a0
WB
353 }
354 }
355 }
41cd94a0
WB
356 }
357
67864d19
WB
358 my $i = ' ';
359 my ($searchdomains, $nameservers) = get_dns_conf($conf);
360 if ($searchdomains || $nameservers) {
41cd94a0 361 $content .= "${i}- type: nameserver\n";
67864d19 362 if (defined($nameservers) && @$nameservers) {
41cd94a0 363 $content .= "${i} address:\n";
4efb58a9 364 $content .= "${i} - '$_'\n" foreach @$nameservers;
41cd94a0 365 }
67864d19 366 if (defined($searchdomains) && @$searchdomains) {
41cd94a0 367 $content .= "${i} search:\n";
4efb58a9 368 $content .= "${i} - '$_'\n" foreach @$searchdomains;
41cd94a0
WB
369 }
370 }
371
372 return $content;
373}
374
375sub nocloud_metadata {
e8ac2138
WB
376 my ($uuid) = @_;
377 return "instance-id: $uuid\n";
41cd94a0
WB
378}
379
380sub generate_nocloud {
381 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
382
cb702ebe
DL
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);
41cd94a0 386
cb702ebe
DL
387 if (!defined($meta_data)) {
388 my $digest_data = $user_data . $network_data;
389 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
41cd94a0 390
cb702ebe
DL
391 $meta_data = nocloud_metadata($uuid_str);
392 }
41cd94a0 393
f62c36cf
WB
394 my $files = {
395 '/user-data' => $user_data,
396 '/network-config' => $network_data,
397 '/meta-data' => $meta_data
398 };
4a853915 399 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
41cd94a0
WB
400}
401
cb702ebe
DL
402sub 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
432sub 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
41cd94a0
WB
440my $cloudinit_methods = {
441 configdrive2 => \&generate_configdrive2,
442 nocloud => \&generate_nocloud,
443};
444
445sub 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}
0c9a7596
AD
463
4641;