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