1 package PVE
::QemuServer
::Cloudinit
;
9 use MIME
::Base64
qw(encode_base64);
11 use PVE
::Tools
qw(run_command file_set_contents);
15 use constant CLOUDINIT_DISK_SIZE
=> 4 * 1024 * 1024; # 4MiB in bytes
17 sub commit_cloudinit_disk
{
18 my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_;
20 my $path = "/run/pve/cloudinit/$vmid/";
22 foreach my $filepath (keys %$files) {
23 if ($filepath !~ m
@^(.*)\
/[^/]+$@) {
24 die "internal error: bad file name in cloud-init image: $filepath\n";
27 mkpath
"$path/$dirname";
29 my $contents = $files->{$filepath};
30 file_set_contents
("$path/$filepath", $contents);
33 my $storecfg = PVE
::Storage
::config
();
34 my $iso_path = PVE
::Storage
::path
($storecfg, $drive->{file
});
35 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
36 my $format = PVE
::QemuServer
::qemu_img_format
($scfg, $volname);
38 my $size = eval { PVE
::Storage
::volume_size_info
($storecfg, $drive->{file
}) };
39 if (!defined($size) || $size <= 0) {
40 $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/;
43 PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid, $format, $name, $size);
44 $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte
46 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
47 $plugin->activate_volume($storeid, $scfg, $volname);
49 print "generating cloud-init ISO\n";
52 ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path],
53 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"]
61 sub get_cloudinit_format
{
63 if (defined(my $format = $conf->{citype
})) {
67 # No format specified, default based on ostype because windows'
68 # cloudbased-init only supports configdrivev2, whereas on linux we need
69 # to use mac addresses because regular cloudinit doesn't map 'ethX' to
70 # the new predicatble network device naming scheme.
71 if (defined(my $ostype = $conf->{ostype
})) {
73 if PVE
::QemuServer
::windows_version
($ostype);
79 sub get_hostname_fqdn
{
80 my ($conf, $vmid) = @_;
81 my $hostname = $conf->{name
} // "VM$vmid";
83 if ($hostname =~ /\./) {
85 $hostname =~ s/\..*$//;
86 } elsif (my $search = $conf->{searchdomain
}) {
87 $fqdn = "$hostname.$search";
89 return ($hostname, $fqdn);
95 # Same logic as in pve-container, but without the testcase special case
96 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
99 split(/\s+/, $conf->{searchdomain
} // $host_resolv_conf->{search
})
102 my $nameserver = $conf->{nameserver
};
103 if (!defined($nameserver)) {
104 $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
106 $nameserver = [split(/\s+/, $nameserver)];
109 return ($searchdomains, $nameserver);
112 sub cloudinit_userdata
{
113 my ($conf, $vmid) = @_;
115 my ($hostname, $fqdn) = get_hostname_fqdn
($conf, $vmid);
117 my $content = "#cloud-config\n";
119 $content .= "hostname: $hostname\n";
120 $content .= "manage_etc_hosts: true\n";
121 $content .= "fqdn: $fqdn\n" if defined($fqdn);
123 my $username = $conf->{ciuser
};
124 my $password = $conf->{cipassword
};
126 $content .= "user: $username\n" if defined($username);
127 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
128 $content .= "password: $password\n" if defined($password);
130 if (defined(my $keys = $conf->{sshkeys
})) {
131 $keys = URI
::Escape
::uri_unescape
($keys);
132 $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)];
133 $keys = [grep { /\S/ } @$keys];
134 $content .= "ssh_authorized_keys:\n";
135 foreach my $k (@$keys) {
136 $content .= " - $k\n";
139 $content .= "chpasswd:\n";
140 $content .= " expire: False\n";
142 if (!defined($username) || $username ne 'root') {
143 $content .= "users:\n";
144 $content .= " - default\n";
147 $content .= "package_upgrade: true\n";
154 my ($addr, $mask) = split('/', $ip);
155 die "not a CIDR: $ip\n" if !defined $mask;
156 return ($addr, $PVE::Network
::ipv4_reverse_mask-
>[$mask]);
159 sub configdrive2_network
{
162 my $content = "auto lo\n";
163 $content .= "iface lo inet loopback\n\n";
165 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
166 if ($nameservers && @$nameservers) {
167 $nameservers = join(' ', @$nameservers);
168 $content .= " dns_nameservers $nameservers\n";
170 if ($searchdomains && @$searchdomains) {
171 $searchdomains = join(' ', @$searchdomains);
172 $content .= " dns_search $searchdomains\n";
175 my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
176 foreach my $iface (sort @ifaces) {
177 (my $id = $iface) =~ s/^net//;
178 next if !$conf->{"ipconfig$id"};
179 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
182 $content .="auto $id\n";
184 if ($net->{ip
} eq 'dhcp') {
185 $content .= "iface $id inet dhcp\n";
187 my ($addr, $mask) = split_ip4
($net->{ip
});
188 $content .= "iface $id inet static\n";
189 $content .= " address $addr\n";
190 $content .= " netmask $mask\n";
191 $content .= " gateway $net->{gw}\n" if $net->{gw
};
195 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
196 $content .= "iface $id inet6 $1\n";
198 my ($addr, $mask) = split('/', $net->{ip6
});
199 $content .= "iface $id inet6 static\n";
200 $content .= " address $addr\n";
201 $content .= " netmask $mask\n";
202 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
210 sub configdrive2_gen_metadata
{
211 my ($user, $network) = @_;
213 my $uuid_str = Digest
::SHA
::sha1_hex
($user.$network);
214 return configdrive2_metadata
($uuid_str);
217 sub configdrive2_metadata
{
222 "network_config": { "content_path": "/content/0000" }
227 sub generate_configdrive2
{
228 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
230 my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files
($conf);
231 $user_data = cloudinit_userdata
($conf, $vmid) if !defined($user_data);
232 $network_data = configdrive2_network
($conf) if !defined($network_data);
233 $vendor_data = '' if !defined($vendor_data);
235 if (!defined($meta_data)) {
236 $meta_data = configdrive2_gen_metadata
($user_data, $network_data);
239 # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO
240 # make sure we always stay below it by keeping the sum of all files below 3 MiB
241 my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data);
242 die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024);
245 '/openstack/latest/user_data' => $user_data,
246 '/openstack/content/0000' => $network_data,
247 '/openstack/latest/meta_data.json' => $meta_data,
248 '/openstack/latest/vendor_data.json' => $vendor_data
250 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
253 sub generate_opennebula
{
254 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
258 my $username = $conf->{ciuser
} || "root";
259 $content .= "USERNAME=$username\n" if defined($username);
261 if (defined(my $password = $conf->{cipassword
})) {
262 $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64
($password) ."\n";
265 if (defined($conf->{sshkeys
})) {
266 my $keys = [ split(/\s*\n\s*/, URI
::Escape
::uri_unescape
($conf->{sshkeys
})) ];
267 $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n";
270 my ($hostname, $fqdn) = get_hostname_fqdn
($conf, $vmid);
271 $content .= "SET_HOSTNAME=$hostname\n";
273 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
274 $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers;
275 $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains;
277 my $networkenabled = undef;
278 my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
279 foreach my $iface (sort @ifaces) {
280 (my $id = $iface) =~ s/^net//;
281 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
282 next if !$conf->{"ipconfig$id"};
283 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
284 my $ethid = "ETH$id";
286 my $mac = lc $net->{hwaddr
};
288 if ($ipconfig->{ip
}) {
291 if ($ipconfig->{ip
} eq 'dhcp') {
292 $content .= "${ethid}_DHCP=YES\n";
294 my ($addr, $mask) = split_ip4
($ipconfig->{ip
});
295 $content .= "${ethid}_IP=$addr\n";
296 $content .= "${ethid}_MASK=$mask\n";
297 $content .= "${ethid}_MAC=$mac\n";
298 $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw
};
300 $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu
};
303 if ($ipconfig->{ip6
}) {
305 if ($ipconfig->{ip6
} eq 'dhcp') {
306 $content .= "${ethid}_DHCP6=YES\n";
307 } elsif ($ipconfig->{ip6
} eq 'auto') {
308 $content .= "${ethid}_AUTO6=YES\n";
310 my ($addr, $mask) = split('/', $ipconfig->{ip6
});
311 $content .= "${ethid}_IP6=$addr\n";
312 $content .= "${ethid}_MASK6=$mask\n";
313 $content .= "${ethid}_MAC6=$mac\n";
314 $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6
};
316 $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu
};
320 $content .= "NETWORK=YES\n" if $networkenabled;
322 my $files = { '/context.sh' => $content };
323 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT');
326 sub nocloud_network_v2
{
331 my $head = "version: 2\n"
336 my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
337 foreach my $iface (sort @ifaces) {
338 (my $id = $iface) =~ s/^net//;
339 next if !$conf->{"ipconfig$id"};
341 # indentation - network interfaces are inside an 'ethernets' hash
344 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
345 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
347 my $mac = $net->{macaddr
}
348 or die "network interface '$iface' has no mac address\n";
350 $content .= "${i}$iface:\n";
352 $content .= "${i}match:\n"
353 . "${i} macaddress: \"$mac\"\n"
354 . "${i
}set-name
: eth
$id\n";
356 if (defined(my $ip = $ipconfig->{ip})) {
358 $content .= "${i
}dhcp4
: true
\n";
360 push @addresses, $ip;
363 if (defined(my $ip = $ipconfig->{ip6})) {
365 $content .= "${i
}dhcp6
: true
\n";
367 push @addresses, $ip;
371 $content .= "${i
}addresses
:\n";
372 $content .= "${i
}- '$_'\n" foreach @addresses;
374 if (defined(my $gw = $ipconfig->{gw})) {
375 $content .= "${i
}gateway4
: '$gw'\n";
377 if (defined(my $gw = $ipconfig->{gw6})) {
378 $content .= "${i
}gateway6
: '$gw'\n";
384 my ($searchdomains, $nameservers) = get_dns_conf($conf);
385 if ($searchdomains || $nameservers) {
386 $content .= "${i
}nameservers
:\n";
387 if (defined($nameservers) && @$nameservers) {
388 $content .= "${i
} addresses
:\n";
389 $content .= "${i
} - '$_'\n" foreach @$nameservers;
391 if (defined($searchdomains) && @$searchdomains) {
392 $content .= "${i
} search
:\n";
393 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
398 return $head.$content;
401 sub nocloud_network {
404 my $content = "version
: 1\n"
407 my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
408 foreach my $iface (sort @ifaces) {
409 (my $id = $iface) =~ s/^net//;
410 next if !$conf->{"ipconfig
$id"};
412 # indentation - network interfaces are inside an 'ethernets' hash
415 my $net = PVE::QemuServer::parse_net($conf->{$iface});
416 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
418 my $mac = lc($net->{macaddr})
419 or die "network interface
'$iface' has no mac address
\n";
421 $content .= "${i
}- type
: physical
\n"
422 . "${i
} name
: eth
$id\n"
423 . "${i
} mac_address
: '$mac'\n"
426 if (defined(my $ip = $ipconfig->{ip})) {
428 $content .= "${i
}- type
: dhcp4
\n";
430 my ($addr, $mask) = split_ip4($ip);
431 $content .= "${i
}- type
: static
\n"
432 . "${i
} address
: '$addr'\n"
433 . "${i
} netmask
: '$mask'\n";
434 if (defined(my $gw = $ipconfig->{gw})) {
435 $content .= "${i
} gateway
: '$gw'\n";
439 if (defined(my $ip = $ipconfig->{ip6})) {
441 $content .= "${i
}- type
: dhcp6
\n";
442 } elsif ($ip eq 'auto') {
443 # SLAAC is only supported by cloud-init since 19.4
444 $content .= "${i
}- type
: ipv6_slaac
\n";
446 $content .= "${i
}- type
: static6
\n"
447 . "${i
} address
: '$ip'\n";
448 if (defined(my $gw = $ipconfig->{gw6})) {
449 $content .= "${i
} gateway
: '$gw'\n";
456 my ($searchdomains, $nameservers) = get_dns_conf($conf);
457 if ($searchdomains || $nameservers) {
458 $content .= "${i
}- type
: nameserver
\n";
459 if (defined($nameservers) && @$nameservers) {
460 $content .= "${i
} address
:\n";
461 $content .= "${i
} - '$_'\n" foreach @$nameservers;
463 if (defined($searchdomains) && @$searchdomains) {
464 $content .= "${i
} search
:\n";
465 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
472 sub nocloud_metadata {
474 return "instance-id
: $uuid\n";
477 sub nocloud_gen_metadata {
478 my ($user, $network) = @_;
480 my $uuid_str = Digest::SHA::sha1_hex($user.$network);
481 return nocloud_metadata($uuid_str);
484 sub generate_nocloud {
485 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
487 my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf);
488 $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
489 $network_data = nocloud_network($conf) if !defined($network_data);
490 $vendor_data = '' if !defined($vendor_data);
492 if (!defined($meta_data)) {
493 $meta_data = nocloud_gen_metadata($user_data, $network_data);
496 # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO
497 # make sure we always stay below it by keeping the sum of all files below 3 MiB
498 my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data);
499 die "Cloud-Init sum of snippets too big
(> 3 MiB
)\n" if $sum > (3 * 1024 * 1024);
502 '/user-data' => $user_data,
503 '/network-config' => $network_data,
504 '/meta-data' => $meta_data,
505 '/vendor-data' => $vendor_data
507 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
510 sub get_custom_cloudinit_files {
513 my $cicustom = $conf->{cicustom};
514 my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
516 my $network_volid = $files->{network};
517 my $user_volid = $files->{user};
518 my $meta_volid = $files->{meta};
519 my $vendor_volid = $files->{vendor};
521 my $storage_conf = PVE::Storage::config();
524 if ($network_volid) {
525 $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
530 $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
535 $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
540 $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid);
543 return ($user_data, $network_data, $meta_data, $vendor_data);
546 sub read_cloudinit_snippets_file {
547 my ($storage_conf, $volid) = @_;
549 my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
550 die "$volid is not in the snippets directory
\n" if $type ne 'snippets';
551 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
554 my $cloudinit_methods = {
555 configdrive2 => \&generate_configdrive2,
556 nocloud => \&generate_nocloud,
557 opennebula => \&generate_opennebula,
560 sub generate_cloudinitconfig {
561 my ($conf, $vmid) = @_;
563 my $format = get_cloudinit_format($conf);
565 PVE::QemuConfig->foreach_volume($conf, sub {
566 my ($ds, $drive) = @_;
568 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
570 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
572 my $generator = $cloudinit_methods->{$format}
573 or die "missing cloudinit methods
for format
'$format'\n";
575 $generator->($conf, $vmid, $drive, $volname, $storeid);
578 my $cloudinitconf = delete $conf->{cloudinit};
581 my @cloudinit_opts = keys %{PVE::QemuServer::cloudinit_config_properties()};
582 push @cloudinit_opts, 'name';
584 for my $opt (@cloudinit_opts) {
586 if ($opt =~ m/^ipconfig(\d+)/) {
588 next if !defined($conf->{$netid});
589 $conf->{cloudinit}->{$netid} = $conf->{$netid};
592 $conf->{cloudinit}->{$opt} = $conf->{$opt} if $conf->{$opt};
595 $conf->{cloudinit}->{name} = "VM
$vmid" if !$conf->{cloudinit}->{name};
597 for my $opt (keys %{$conf}) {
598 if (PVE::QemuServer::is_valid_drivename($opt)) {
599 my $drive = PVE::QemuServer::parse_drive($opt, $conf->{$opt});
600 if (PVE::QemuServer::drive_is_cloudinit($drive)) {
601 $conf->{cloudinit}->{$opt} = $conf->{$opt};
606 PVE::QemuConfig->write_config($vmid, $conf);
610 sub dump_cloudinit_config {
611 my ($conf, $vmid, $type) = @_;
613 my $format = get_cloudinit_format($conf);
615 if ($type eq 'user') {
616 return cloudinit_userdata($conf, $vmid);
617 } elsif ($type eq 'network') {
618 if ($format eq 'nocloud') {
619 return nocloud_network($conf);
621 return configdrive2_network($conf);
623 } else { # metadata config
624 my $user = cloudinit_userdata($conf, $vmid);
625 if ($format eq 'nocloud') {
626 my $network = nocloud_network($conf);
627 return nocloud_gen_metadata($user, $network);
629 my $network = configdrive2_network($conf);
630 return configdrive2_gen_metadata($user, $network);