1 package PVE
::QemuServer
::Cloudinit
;
10 use PVE
::Tools
qw(run_command file_set_contents);
14 sub commit_cloudinit_disk
{
15 my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_;
17 my $path = "/run/pve/cloudinit/$vmid/";
19 foreach my $filepath (keys %$files) {
20 if ($filepath !~ m
@^(.*)\
/[^/]+$@) {
21 die "internal error: bad file name in cloud-init image: $filepath\n";
24 mkpath
"$path/$dirname";
26 my $contents = $files->{$filepath};
27 file_set_contents
("$path/$filepath", $contents);
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);
33 my $format = PVE
::QemuServer
::qemu_img_format
($scfg, $volname);
35 # required before file_size_info for ceph + krbd as it gets mapped too late otherwise
36 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
37 eval { $plugin->activate_volume($storeid, $scfg, $volname) };
39 my $size = eval { PVE
::Storage
::file_size_info
($iso_path) };
41 $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/;
44 PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid, $format, $name, $size);
45 $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte
46 $plugin->activate_volume($storeid, $scfg, $volname);
51 run_command
([['genisoimage', '-R', '-V', $label, $path],
52 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
53 'isize=0', "osize=$size", "of=$iso_path"]]);
60 sub get_cloudinit_format
{
62 if (defined(my $format = $conf->{citype
})) {
66 # No format specified, default based on ostype because windows'
67 # cloudbased-init only supports configdrivev2, whereas on linux we need
68 # to use mac addresses because regular cloudinit doesn't map 'ethX' to
69 # the new predicatble network device naming scheme.
70 if (defined(my $ostype = $conf->{ostype
})) {
72 if PVE
::QemuServer
::windows_version
($ostype);
78 sub get_hostname_fqdn
{
79 my ($conf, $vmid) = @_;
80 my $hostname = $conf->{name
} // "VM$vmid";
82 if ($hostname =~ /\./) {
84 $hostname =~ s/\..*$//;
85 } elsif (my $search = $conf->{searchdomain
}) {
86 $fqdn = "$hostname.$search";
88 return ($hostname, $fqdn);
94 # Same logic as in pve-container, but without the testcase special case
95 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
98 split(/\s+/, $conf->{searchdomain
} // $host_resolv_conf->{search
})
101 my $nameserver = $conf->{nameserver
};
102 if (!defined($nameserver)) {
103 $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
105 $nameserver = [split(/\s+/, $nameserver)];
108 return ($searchdomains, $nameserver);
111 sub cloudinit_userdata
{
112 my ($conf, $vmid) = @_;
114 my ($hostname, $fqdn) = get_hostname_fqdn
($conf, $vmid);
116 my $content = "#cloud-config\n";
118 $content .= "hostname: $hostname\n";
119 $content .= "manage_etc_hosts: true\n";
120 $content .= "fqdn: $fqdn\n" if defined($fqdn);
122 my $username = $conf->{ciuser
};
123 my $password = $conf->{cipassword
};
125 $content .= "user: $username\n" if defined($username);
126 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
127 $content .= "password: $password\n" if defined($password);
129 if (defined(my $keys = $conf->{sshkeys
})) {
130 $keys = URI
::Escape
::uri_unescape
($keys);
131 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
132 $keys = [grep { /\S/ } @$keys];
133 $content .= "ssh_authorized_keys:\n";
134 foreach my $k (@$keys) {
135 $content .= " - $k\n";
138 $content .= "chpasswd:\n";
139 $content .= " expire: False\n";
141 if (!defined($username) || $username ne 'root') {
142 $content .= "users:\n";
143 $content .= " - default\n";
146 $content .= "package_upgrade: true\n";
153 my ($addr, $mask) = split('/', $ip);
154 die "not a CIDR: $ip\n" if !defined $mask;
155 return ($addr, $PVE::Network
::ipv4_reverse_mask-
>[$mask]);
158 sub configdrive2_network
{
161 my $content = "auto lo\n";
162 $content .= "iface lo inet loopback\n\n";
164 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
165 if ($nameservers && @$nameservers) {
166 $nameservers = join(' ', @$nameservers);
167 $content .= " dns_nameservers $nameservers\n";
169 if ($searchdomains && @$searchdomains) {
170 $searchdomains = join(' ', @$searchdomains);
171 $content .= " dns_search $searchdomains\n";
174 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
175 foreach my $iface (@ifaces) {
176 (my $id = $iface) =~ s/^net//;
177 next if !$conf->{"ipconfig$id"};
178 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
181 $content .="auto $id\n";
183 if ($net->{ip
} eq 'dhcp') {
184 $content .= "iface $id inet dhcp\n";
186 my ($addr, $mask) = split_ip4
($net->{ip
});
187 $content .= "iface $id inet static\n";
188 $content .= " address $addr\n";
189 $content .= " netmask $mask\n";
190 $content .= " gateway $net->{gw}\n" if $net->{gw
};
194 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
195 $content .= "iface $id inet6 $1\n";
197 my ($addr, $mask) = split('/', $net->{ip6
});
198 $content .= "iface $id inet6 static\n";
199 $content .= " address $addr\n";
200 $content .= " netmask $mask\n";
201 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
209 sub configdrive2_metadata
{
214 "network_config": { "content_path": "/content/0000" }
219 sub generate_configdrive2
{
220 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
222 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files
($conf);
223 $user_data = cloudinit_userdata
($conf, $vmid) if !defined($user_data);
224 $network_data = configdrive2_network
($conf) if !defined($network_data);
226 if (!defined($meta_data)) {
227 my $digest_data = $user_data . $network_data;
228 my $uuid_str = Digest
::SHA
::sha1_hex
($digest_data);
230 $meta_data = configdrive2_metadata
($uuid_str);
233 '/openstack/latest/user_data' => $user_data,
234 '/openstack/content/0000' => $network_data,
235 '/openstack/latest/meta_data.json' => $meta_data
237 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
240 sub nocloud_network_v2
{
245 my $head = "version: 2\n"
250 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
251 foreach my $iface (@ifaces) {
252 (my $id = $iface) =~ s/^net//;
253 next if !$conf->{"ipconfig$id"};
255 # indentation - network interfaces are inside an 'ethernets' hash
258 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
259 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
261 my $mac = $net->{macaddr
}
262 or die "network interface '$iface' has no mac address\n";
264 $content .= "${i}$iface:\n";
266 $content .= "${i}match:\n"
267 . "${i} macaddress: \"$mac\"\n"
268 . "${i
}set-name
: eth
$id\n";
270 if (defined(my $ip = $ipconfig->{ip})) {
272 $content .= "${i
}dhcp4
: true
\n";
274 push @addresses, $ip;
277 if (defined(my $ip = $ipconfig->{ip6})) {
279 $content .= "${i
}dhcp6
: true
\n";
281 push @addresses, $ip;
285 $content .= "${i
}addresses
:\n";
286 $content .= "${i
}- '$_'\n" foreach @addresses;
288 if (defined(my $gw = $ipconfig->{gw})) {
289 $content .= "${i
}gateway4
: '$gw'\n";
291 if (defined(my $gw = $ipconfig->{gw6})) {
292 $content .= "${i
}gateway6
: '$gw'\n";
298 my ($searchdomains, $nameservers) = get_dns_conf($conf);
299 if ($searchdomains || $nameservers) {
300 $content .= "${i
}nameservers
:\n";
301 if (defined($nameservers) && @$nameservers) {
302 $content .= "${i
} addresses
:\n";
303 $content .= "${i
} - '$_'\n" foreach @$nameservers;
305 if (defined($searchdomains) && @$searchdomains) {
306 $content .= "${i
} search
:\n";
307 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
312 return $head.$content;
315 sub nocloud_network {
318 my $content = "version
: 1\n"
321 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
322 foreach my $iface (@ifaces) {
323 (my $id = $iface) =~ s/^net//;
324 next if !$conf->{"ipconfig
$id"};
326 # indentation - network interfaces are inside an 'ethernets' hash
329 my $net = PVE::QemuServer::parse_net($conf->{$iface});
330 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
332 my $mac = lc($net->{macaddr})
333 or die "network interface
'$iface' has no mac address
\n";
335 $content .= "${i
}- type
: physical
\n"
336 . "${i
} name
: eth
$id\n"
337 . "${i
} mac_address
: '$mac'\n"
340 if (defined(my $ip = $ipconfig->{ip})) {
342 $content .= "${i
}- type
: dhcp4
\n";
344 my ($addr, $mask) = split_ip4($ip);
345 $content .= "${i
}- type
: static
\n"
346 . "${i
} address
: '$addr'\n"
347 . "${i
} netmask
: '$mask'\n";
348 if (defined(my $gw = $ipconfig->{gw})) {
349 $content .= "${i
} gateway
: '$gw'\n";
353 if (defined(my $ip = $ipconfig->{ip6})) {
355 $content .= "${i
}- type
: dhcp6
\n";
356 } elsif ($ip eq 'auto') {
357 # SLAAC is not supported by cloud-init, this fallback should work with an up-to-date netplan at least
358 $content .= "${i
}- type
: dhcp6
\n";
360 $content .= "${i
}- type
: static
\n"
361 . "${i
} address
: '$ip'\n";
362 if (defined(my $gw = $ipconfig->{gw6})) {
363 $content .= "${i
} gateway
: '$gw'\n";
370 my ($searchdomains, $nameservers) = get_dns_conf($conf);
371 if ($searchdomains || $nameservers) {
372 $content .= "${i
}- type
: nameserver
\n";
373 if (defined($nameservers) && @$nameservers) {
374 $content .= "${i
} address
:\n";
375 $content .= "${i
} - '$_'\n" foreach @$nameservers;
377 if (defined($searchdomains) && @$searchdomains) {
378 $content .= "${i
} search
:\n";
379 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
386 sub nocloud_metadata {
388 return "instance-id
: $uuid\n";
391 sub generate_nocloud {
392 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
394 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
395 $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
396 $network_data = nocloud_network($conf) if !defined($network_data);
398 if (!defined($meta_data)) {
399 my $digest_data = $user_data . $network_data;
400 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
402 $meta_data = nocloud_metadata($uuid_str);
406 '/user-data' => $user_data,
407 '/network-config' => $network_data,
408 '/meta-data' => $meta_data
410 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
413 sub get_custom_cloudinit_files {
416 my $cicustom = $conf->{cicustom};
417 my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
419 my $network_volid = $files->{network};
420 my $user_volid = $files->{user};
421 my $meta_volid = $files->{meta};
423 my $storage_conf = PVE::Storage::config();
426 if ($network_volid) {
427 $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
432 $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
437 $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
440 return ($user_data, $network_data, $meta_data);
443 sub read_cloudinit_snippets_file {
444 my ($storage_conf, $volid) = @_;
446 my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
447 die "$volid is not in the snippets directory
\n" if $type ne 'snippets';
448 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
451 my $cloudinit_methods = {
452 configdrive2 => \&generate_configdrive2,
453 nocloud => \&generate_nocloud,
456 sub generate_cloudinitconfig {
457 my ($conf, $vmid) = @_;
459 my $format = get_cloudinit_format($conf);
461 PVE::QemuServer::foreach_drive($conf, sub {
462 my ($ds, $drive) = @_;
464 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
466 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
468 my $generator = $cloudinit_methods->{$format}
469 or die "missing cloudinit methods
for format
'$format'\n";
471 $generator->($conf, $vmid, $drive, $volname, $storeid);