1 package PVE
::QemuServer
::Cloudinit
;
10 use PVE
::Tools
qw(run_command file_set_contents);
14 use constant CLOUDINIT_DISK_SIZE
=> 4 * 1024 * 1024; # 4MiB in bytes
16 sub commit_cloudinit_disk
{
17 my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_;
19 my $path = "/run/pve/cloudinit/$vmid/";
21 foreach my $filepath (keys %$files) {
22 if ($filepath !~ m
@^(.*)\
/[^/]+$@) {
23 die "internal error: bad file name in cloud-init image: $filepath\n";
26 mkpath
"$path/$dirname";
28 my $contents = $files->{$filepath};
29 file_set_contents
("$path/$filepath", $contents);
32 my $storecfg = PVE
::Storage
::config
();
33 my $iso_path = PVE
::Storage
::path
($storecfg, $drive->{file
});
34 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
35 my $format = PVE
::QemuServer
::qemu_img_format
($scfg, $volname);
37 my $size = eval { PVE
::Storage
::volume_size_info
($storecfg, $drive->{file
}) };
38 if (!defined($size) || $size <= 0) {
39 $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/;
42 PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid, $format, $name, $size);
43 $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte
45 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
46 $plugin->activate_volume($storeid, $scfg, $volname);
48 print "generating cloud-init ISO\n";
51 ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path],
52 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, '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 { my $key = $_; chomp $key; $key } 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 (sort @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_gen_metadata
{
210 my ($user, $network) = @_;
212 my $uuid_str = Digest
::SHA
::sha1_hex
($user.$network);
213 return configdrive2_metadata
($uuid_str);
216 sub configdrive2_metadata
{
221 "network_config": { "content_path": "/content/0000" }
226 sub generate_configdrive2
{
227 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
229 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files
($conf);
230 $user_data = cloudinit_userdata
($conf, $vmid) if !defined($user_data);
231 $network_data = configdrive2_network
($conf) if !defined($network_data);
233 if (!defined($meta_data)) {
234 $meta_data = configdrive2_gen_metadata
($user_data, $network_data);
237 '/openstack/latest/user_data' => $user_data,
238 '/openstack/content/0000' => $network_data,
239 '/openstack/latest/meta_data.json' => $meta_data
241 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
244 sub nocloud_network_v2
{
249 my $head = "version: 2\n"
254 my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
255 foreach my $iface (sort @ifaces) {
256 (my $id = $iface) =~ s/^net//;
257 next if !$conf->{"ipconfig$id"};
259 # indentation - network interfaces are inside an 'ethernets' hash
262 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
263 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
265 my $mac = $net->{macaddr
}
266 or die "network interface '$iface' has no mac address\n";
268 $content .= "${i}$iface:\n";
270 $content .= "${i}match:\n"
271 . "${i} macaddress: \"$mac\"\n"
272 . "${i
}set-name
: eth
$id\n";
274 if (defined(my $ip = $ipconfig->{ip})) {
276 $content .= "${i
}dhcp4
: true
\n";
278 push @addresses, $ip;
281 if (defined(my $ip = $ipconfig->{ip6})) {
283 $content .= "${i
}dhcp6
: true
\n";
285 push @addresses, $ip;
289 $content .= "${i
}addresses
:\n";
290 $content .= "${i
}- '$_'\n" foreach @addresses;
292 if (defined(my $gw = $ipconfig->{gw})) {
293 $content .= "${i
}gateway4
: '$gw'\n";
295 if (defined(my $gw = $ipconfig->{gw6})) {
296 $content .= "${i
}gateway6
: '$gw'\n";
302 my ($searchdomains, $nameservers) = get_dns_conf($conf);
303 if ($searchdomains || $nameservers) {
304 $content .= "${i
}nameservers
:\n";
305 if (defined($nameservers) && @$nameservers) {
306 $content .= "${i
} addresses
:\n";
307 $content .= "${i
} - '$_'\n" foreach @$nameservers;
309 if (defined($searchdomains) && @$searchdomains) {
310 $content .= "${i
} search
:\n";
311 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
316 return $head.$content;
319 sub nocloud_network {
322 my $content = "version
: 1\n"
325 my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
326 foreach my $iface (sort @ifaces) {
327 (my $id = $iface) =~ s/^net//;
328 next if !$conf->{"ipconfig
$id"};
330 # indentation - network interfaces are inside an 'ethernets' hash
333 my $net = PVE::QemuServer::parse_net($conf->{$iface});
334 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
336 my $mac = lc($net->{macaddr})
337 or die "network interface
'$iface' has no mac address
\n";
339 $content .= "${i
}- type
: physical
\n"
340 . "${i
} name
: eth
$id\n"
341 . "${i
} mac_address
: '$mac'\n"
344 if (defined(my $ip = $ipconfig->{ip})) {
346 $content .= "${i
}- type
: dhcp4
\n";
348 my ($addr, $mask) = split_ip4($ip);
349 $content .= "${i
}- type
: static
\n"
350 . "${i
} address
: '$addr'\n"
351 . "${i
} netmask
: '$mask'\n";
352 if (defined(my $gw = $ipconfig->{gw})) {
353 $content .= "${i
} gateway
: '$gw'\n";
357 if (defined(my $ip = $ipconfig->{ip6})) {
359 $content .= "${i
}- type
: dhcp6
\n";
360 } elsif ($ip eq 'auto') {
361 # SLAAC is not supported by cloud-init, this fallback should work with an up-to-date netplan at least
362 $content .= "${i
}- type
: dhcp6
\n";
364 $content .= "${i
}- type
: static
\n"
365 . "${i
} address
: '$ip'\n";
366 if (defined(my $gw = $ipconfig->{gw6})) {
367 $content .= "${i
} gateway
: '$gw'\n";
374 my ($searchdomains, $nameservers) = get_dns_conf($conf);
375 if ($searchdomains || $nameservers) {
376 $content .= "${i
}- type
: nameserver
\n";
377 if (defined($nameservers) && @$nameservers) {
378 $content .= "${i
} address
:\n";
379 $content .= "${i
} - '$_'\n" foreach @$nameservers;
381 if (defined($searchdomains) && @$searchdomains) {
382 $content .= "${i
} search
:\n";
383 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
390 sub nocloud_metadata {
392 return "instance-id
: $uuid\n";
395 sub nocloud_gen_metadata {
396 my ($user, $network) = @_;
398 my $uuid_str = Digest::SHA::sha1_hex($user.$network);
399 return nocloud_metadata($uuid_str);
402 sub generate_nocloud {
403 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
405 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
406 $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
407 $network_data = nocloud_network($conf) if !defined($network_data);
409 if (!defined($meta_data)) {
410 $meta_data = nocloud_gen_metadata($user_data, $network_data);
414 '/user-data' => $user_data,
415 '/network-config' => $network_data,
416 '/meta-data' => $meta_data
418 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
421 sub get_custom_cloudinit_files {
424 my $cicustom = $conf->{cicustom};
425 my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
427 my $network_volid = $files->{network};
428 my $user_volid = $files->{user};
429 my $meta_volid = $files->{meta};
431 my $storage_conf = PVE::Storage::config();
434 if ($network_volid) {
435 $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
440 $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
445 $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
448 return ($user_data, $network_data, $meta_data);
451 sub read_cloudinit_snippets_file {
452 my ($storage_conf, $volid) = @_;
454 my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
455 die "$volid is not in the snippets directory
\n" if $type ne 'snippets';
456 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
459 my $cloudinit_methods = {
460 configdrive2 => \&generate_configdrive2,
461 nocloud => \&generate_nocloud,
464 sub generate_cloudinitconfig {
465 my ($conf, $vmid) = @_;
467 my $format = get_cloudinit_format($conf);
469 PVE::QemuConfig->foreach_volume($conf, sub {
470 my ($ds, $drive) = @_;
472 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
474 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
476 my $generator = $cloudinit_methods->{$format}
477 or die "missing cloudinit methods
for format
'$format'\n";
479 $generator->($conf, $vmid, $drive, $volname, $storeid);
483 sub dump_cloudinit_config {
484 my ($conf, $vmid, $type) = @_;
486 my $format = get_cloudinit_format($conf);
488 if ($type eq 'user') {
489 return cloudinit_userdata($conf, $vmid);
490 } elsif ($type eq 'network') {
491 if ($format eq 'nocloud') {
492 return nocloud_network($conf);
494 return configdrive2_network($conf);
496 } else { # metadata config
497 my $user = cloudinit_userdata($conf, $vmid);
498 if ($format eq 'nocloud') {
499 my $network = nocloud_network($conf);
500 return nocloud_gen_metadata($user, $network);
502 my $network = configdrive2_network($conf);
503 return configdrive2_gen_metadata($user, $network);