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 my $size = eval { PVE
::Storage
::file_size_info
($iso_path) };
37 $volname =~ m/(vm-$vmid-cloudinit(.(qcow2|raw))?)/;
40 PVE
::Storage
::vdisk_alloc
($storecfg, $storeid, $vmid, $format, $name, $size);
41 $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte
44 my $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
45 $plugin->activate_volume($storeid, $scfg, $volname);
48 run_command
([['genisoimage', '-R', '-V', $label, $path],
49 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
50 'isize=0', "osize=$size", "of=$iso_path"]]);
57 sub get_cloudinit_format
{
59 if (defined(my $format = $conf->{citype
})) {
63 # No format specified, default based on ostype because windows'
64 # cloudbased-init only supports configdrivev2, whereas on linux we need
65 # to use mac addresses because regular cloudinit doesn't map 'ethX' to
66 # the new predicatble network device naming scheme.
67 if (defined(my $ostype = $conf->{ostype
})) {
69 if PVE
::QemuServer
::windows_version
($ostype);
75 sub get_hostname_fqdn
{
76 my ($conf, $vmid) = @_;
77 my $hostname = $conf->{name
} // "VM$vmid";
79 if ($hostname =~ /\./) {
81 $hostname =~ s/\..*$//;
82 } elsif (my $search = $conf->{searchdomain
}) {
83 $fqdn = "$hostname.$search";
85 return ($hostname, $fqdn);
91 # Same logic as in pve-container, but without the testcase special case
92 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
95 split(/\s+/, $conf->{searchdomain
} // $host_resolv_conf->{search
})
98 my $nameserver = $conf->{nameserver
};
99 if (!defined($nameserver)) {
100 $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
102 $nameserver = [split(/\s+/, $nameserver)];
105 return ($searchdomains, $nameserver);
108 sub cloudinit_userdata
{
109 my ($conf, $vmid) = @_;
111 my ($hostname, $fqdn) = get_hostname_fqdn
($conf, $vmid);
113 my $content = "#cloud-config\n";
115 $content .= "hostname: $hostname\n";
116 $content .= "manage_etc_hosts: true\n";
117 $content .= "fqdn: $fqdn\n" if defined($fqdn);
119 my $username = $conf->{ciuser
};
120 my $password = $conf->{cipassword
};
122 $content .= "user: $username\n" if defined($username);
123 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
124 $content .= "password: $password\n" if defined($password);
126 if (defined(my $keys = $conf->{sshkeys
})) {
127 $keys = URI
::Escape
::uri_unescape
($keys);
128 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
129 $keys = [grep { /\S/ } @$keys];
130 $content .= "ssh_authorized_keys:\n";
131 foreach my $k (@$keys) {
132 $content .= " - $k\n";
135 $content .= "chpasswd:\n";
136 $content .= " expire: False\n";
138 if (!defined($username) || $username ne 'root') {
139 $content .= "users:\n";
140 $content .= " - default\n";
143 $content .= "package_upgrade: true\n";
150 my ($addr, $mask) = split('/', $ip);
151 die "not a CIDR: $ip\n" if !defined $mask;
152 return ($addr, $PVE::Network
::ipv4_reverse_mask-
>[$mask]);
155 sub configdrive2_network
{
158 my $content = "auto lo\n";
159 $content .= "iface lo inet loopback\n\n";
161 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
162 if ($nameservers && @$nameservers) {
163 $nameservers = join(' ', @$nameservers);
164 $content .= " dns_nameservers $nameservers\n";
166 if ($searchdomains && @$searchdomains) {
167 $searchdomains = join(' ', @$searchdomains);
168 $content .= " dns_search $searchdomains\n";
171 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
172 foreach my $iface (@ifaces) {
173 (my $id = $iface) =~ s/^net//;
174 next if !$conf->{"ipconfig$id"};
175 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
178 $content .="auto $id\n";
180 if ($net->{ip
} eq 'dhcp') {
181 $content .= "iface $id inet dhcp\n";
183 my ($addr, $mask) = split_ip4
($net->{ip
});
184 $content .= "iface $id inet static\n";
185 $content .= " address $addr\n";
186 $content .= " netmask $mask\n";
187 $content .= " gateway $net->{gw}\n" if $net->{gw
};
191 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
192 $content .= "iface $id inet6 $1\n";
194 my ($addr, $mask) = split('/', $net->{ip6
});
195 $content .= "iface $id inet6 static\n";
196 $content .= " address $addr\n";
197 $content .= " netmask $mask\n";
198 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
206 sub configdrive2_metadata
{
211 "network_config": { "content_path": "/content/0000" }
216 sub generate_configdrive2
{
217 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
219 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files
($conf);
220 $user_data = cloudinit_userdata
($conf, $vmid) if !defined($user_data);
221 $network_data = configdrive2_network
($conf) if !defined($network_data);
223 if (!defined($meta_data)) {
224 my $digest_data = $user_data . $network_data;
225 my $uuid_str = Digest
::SHA
::sha1_hex
($digest_data);
227 $meta_data = configdrive2_metadata
($uuid_str);
230 '/openstack/latest/user_data' => $user_data,
231 '/openstack/content/0000' => $network_data,
232 '/openstack/latest/meta_data.json' => $meta_data
234 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
237 sub nocloud_network_v2
{
242 my $head = "version: 2\n"
247 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
248 foreach my $iface (@ifaces) {
249 (my $id = $iface) =~ s/^net//;
250 next if !$conf->{"ipconfig$id"};
252 # indentation - network interfaces are inside an 'ethernets' hash
255 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
256 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
258 my $mac = $net->{macaddr
}
259 or die "network interface '$iface' has no mac address\n";
261 $content .= "${i}$iface:\n";
263 $content .= "${i}match:\n"
264 . "${i} macaddress: \"$mac\"\n"
265 . "${i
}set-name
: eth
$id\n";
267 if (defined(my $ip = $ipconfig->{ip})) {
269 $content .= "${i
}dhcp4
: true
\n";
271 push @addresses, $ip;
274 if (defined(my $ip = $ipconfig->{ip6})) {
276 $content .= "${i
}dhcp6
: true
\n";
278 push @addresses, $ip;
282 $content .= "${i
}addresses
:\n";
283 $content .= "${i
}- '$_'\n" foreach @addresses;
285 if (defined(my $gw = $ipconfig->{gw})) {
286 $content .= "${i
}gateway4
: '$gw'\n";
288 if (defined(my $gw = $ipconfig->{gw6})) {
289 $content .= "${i
}gateway6
: '$gw'\n";
295 my ($searchdomains, $nameservers) = get_dns_conf($conf);
296 if ($searchdomains || $nameservers) {
297 $content .= "${i
}nameservers
:\n";
298 if (defined($nameservers) && @$nameservers) {
299 $content .= "${i
} addresses
:\n";
300 $content .= "${i
} - '$_'\n" foreach @$nameservers;
302 if (defined($searchdomains) && @$searchdomains) {
303 $content .= "${i
} search
:\n";
304 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
309 return $head.$content;
312 sub nocloud_network {
315 my $content = "version
: 1\n"
318 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
319 foreach my $iface (@ifaces) {
320 (my $id = $iface) =~ s/^net//;
321 next if !$conf->{"ipconfig
$id"};
323 # indentation - network interfaces are inside an 'ethernets' hash
326 my $net = PVE::QemuServer::parse_net($conf->{$iface});
327 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
329 my $mac = lc($net->{macaddr})
330 or die "network interface
'$iface' has no mac address
\n";
332 $content .= "${i
}- type
: physical
\n"
333 . "${i
} name
: eth
$id\n"
334 . "${i
} mac_address
: '$mac'\n"
337 if (defined(my $ip = $ipconfig->{ip})) {
339 $content .= "${i
}- type
: dhcp4
\n";
341 my ($addr, $mask) = split_ip4($ip);
342 $content .= "${i
}- type
: static
\n"
343 . "${i
} address
: '$addr'\n"
344 . "${i
} netmask
: '$mask'\n";
345 if (defined(my $gw = $ipconfig->{gw})) {
346 $content .= "${i
} gateway
: '$gw'\n";
350 if (defined(my $ip = $ipconfig->{ip6})) {
352 $content .= "${i
}- type
: dhcp6
\n";
353 } elsif ($ip eq 'auto') {
354 # SLAAC is not supported by cloud-init, this fallback should work with an up-to-date netplan at least
355 $content .= "${i
}- type
: dhcp6
\n";
357 $content .= "${i
}- type
: static
\n"
358 . "${i
} address
: '$ip'\n";
359 if (defined(my $gw = $ipconfig->{gw6})) {
360 $content .= "${i
} gateway
: '$gw'\n";
367 my ($searchdomains, $nameservers) = get_dns_conf($conf);
368 if ($searchdomains || $nameservers) {
369 $content .= "${i
}- type
: nameserver
\n";
370 if (defined($nameservers) && @$nameservers) {
371 $content .= "${i
} address
:\n";
372 $content .= "${i
} - '$_'\n" foreach @$nameservers;
374 if (defined($searchdomains) && @$searchdomains) {
375 $content .= "${i
} search
:\n";
376 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
383 sub nocloud_metadata {
385 return "instance-id
: $uuid\n";
388 sub generate_nocloud {
389 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
391 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
392 $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
393 $network_data = nocloud_network($conf) if !defined($network_data);
395 if (!defined($meta_data)) {
396 my $digest_data = $user_data . $network_data;
397 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
399 $meta_data = nocloud_metadata($uuid_str);
403 '/user-data' => $user_data,
404 '/network-config' => $network_data,
405 '/meta-data' => $meta_data
407 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
410 sub get_custom_cloudinit_files {
413 my $cicustom = $conf->{cicustom};
414 my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
416 my $network_volid = $files->{network};
417 my $user_volid = $files->{user};
418 my $meta_volid = $files->{meta};
420 my $storage_conf = PVE::Storage::config();
423 if ($network_volid) {
424 $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
429 $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
434 $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
437 return ($user_data, $network_data, $meta_data);
440 sub read_cloudinit_snippets_file {
441 my ($storage_conf, $volid) = @_;
443 my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
444 die "$volid is not in the snippets directory
\n" if $type ne 'snippets';
445 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
448 my $cloudinit_methods = {
449 configdrive2 => \&generate_configdrive2,
450 nocloud => \&generate_nocloud,
453 sub generate_cloudinitconfig {
454 my ($conf, $vmid) = @_;
456 my $format = get_cloudinit_format($conf);
458 PVE::QemuServer::foreach_drive($conf, sub {
459 my ($ds, $drive) = @_;
461 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
463 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
465 my $generator = $cloudinit_methods->{$format}
466 or die "missing cloudinit methods
for format
'$format'\n";
468 $generator->($conf, $vmid, $drive, $volname, $storeid);