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, some existing vols won't show up else
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);
50 run_command
([['genisoimage', '-R', '-V', $label, $path],
51 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
52 'isize=0', "osize=$size", "of=$iso_path"]]);
59 sub get_cloudinit_format
{
61 if (defined(my $format = $conf->{citype
})) {
65 # No format specified, default based on ostype because windows'
66 # cloudbased-init only supports configdrivev2, whereas on linux we need
67 # to use mac addresses because regular cloudinit doesn't map 'ethX' to
68 # the new predicatble network device naming scheme.
69 if (defined(my $ostype = $conf->{ostype
})) {
71 if PVE
::QemuServer
::windows_version
($ostype);
77 sub get_hostname_fqdn
{
78 my ($conf, $vmid) = @_;
79 my $hostname = $conf->{name
} // "VM$vmid";
81 if ($hostname =~ /\./) {
83 $hostname =~ s/\..*$//;
84 } elsif (my $search = $conf->{searchdomain
}) {
85 $fqdn = "$hostname.$search";
87 return ($hostname, $fqdn);
93 # Same logic as in pve-container, but without the testcase special case
94 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
97 split(/\s+/, $conf->{searchdomain
} // $host_resolv_conf->{search
})
100 my $nameserver = $conf->{nameserver
};
101 if (!defined($nameserver)) {
102 $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
104 $nameserver = [split(/\s+/, $nameserver)];
107 return ($searchdomains, $nameserver);
110 sub cloudinit_userdata
{
111 my ($conf, $vmid) = @_;
113 my ($hostname, $fqdn) = get_hostname_fqdn
($conf, $vmid);
115 my $content = "#cloud-config\n";
117 $content .= "hostname: $hostname\n";
118 $content .= "manage_etc_hosts: true\n";
119 $content .= "fqdn: $fqdn\n" if defined($fqdn);
121 my $username = $conf->{ciuser
};
122 my $password = $conf->{cipassword
};
124 $content .= "user: $username\n" if defined($username);
125 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
126 $content .= "password: $password\n" if defined($password);
128 if (defined(my $keys = $conf->{sshkeys
})) {
129 $keys = URI
::Escape
::uri_unescape
($keys);
130 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
131 $keys = [grep { /\S/ } @$keys];
132 $content .= "ssh_authorized_keys:\n";
133 foreach my $k (@$keys) {
134 $content .= " - $k\n";
137 $content .= "chpasswd:\n";
138 $content .= " expire: False\n";
140 if (!defined($username) || $username ne 'root') {
141 $content .= "users:\n";
142 $content .= " - default\n";
145 $content .= "package_upgrade: true\n";
152 my ($addr, $mask) = split('/', $ip);
153 die "not a CIDR: $ip\n" if !defined $mask;
154 return ($addr, $PVE::Network
::ipv4_reverse_mask-
>[$mask]);
157 sub configdrive2_network
{
160 my $content = "auto lo\n";
161 $content .= "iface lo inet loopback\n\n";
163 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
164 if ($nameservers && @$nameservers) {
165 $nameservers = join(' ', @$nameservers);
166 $content .= " dns_nameservers $nameservers\n";
168 if ($searchdomains && @$searchdomains) {
169 $searchdomains = join(' ', @$searchdomains);
170 $content .= " dns_search $searchdomains\n";
173 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
174 foreach my $iface (@ifaces) {
175 (my $id = $iface) =~ s/^net//;
176 next if !$conf->{"ipconfig$id"};
177 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
180 $content .="auto $id\n";
182 if ($net->{ip
} eq 'dhcp') {
183 $content .= "iface $id inet dhcp\n";
185 my ($addr, $mask) = split_ip4
($net->{ip
});
186 $content .= "iface $id inet static\n";
187 $content .= " address $addr\n";
188 $content .= " netmask $mask\n";
189 $content .= " gateway $net->{gw}\n" if $net->{gw
};
193 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
194 $content .= "iface $id inet6 $1\n";
196 my ($addr, $mask) = split('/', $net->{ip6
});
197 $content .= "iface $id inet6 static\n";
198 $content .= " address $addr\n";
199 $content .= " netmask $mask\n";
200 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
208 sub configdrive2_metadata
{
213 "network_config": { "content_path": "/content/0000" }
218 sub generate_configdrive2
{
219 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
221 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files
($conf);
222 $user_data = cloudinit_userdata
($conf, $vmid) if !defined($user_data);
223 $network_data = configdrive2_network
($conf) if !defined($network_data);
225 if (!defined($meta_data)) {
226 my $digest_data = $user_data . $network_data;
227 my $uuid_str = Digest
::SHA
::sha1_hex
($digest_data);
229 $meta_data = configdrive2_metadata
($uuid_str);
232 '/openstack/latest/user_data' => $user_data,
233 '/openstack/content/0000' => $network_data,
234 '/openstack/latest/meta_data.json' => $meta_data
236 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
239 sub nocloud_network_v2
{
244 my $head = "version: 2\n"
249 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
250 foreach my $iface (@ifaces) {
251 (my $id = $iface) =~ s/^net//;
252 next if !$conf->{"ipconfig$id"};
254 # indentation - network interfaces are inside an 'ethernets' hash
257 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
258 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
260 my $mac = $net->{macaddr
}
261 or die "network interface '$iface' has no mac address\n";
263 $content .= "${i}$iface:\n";
265 $content .= "${i}match:\n"
266 . "${i} macaddress: \"$mac\"\n"
267 . "${i
}set-name
: eth
$id\n";
269 if (defined(my $ip = $ipconfig->{ip})) {
271 $content .= "${i
}dhcp4
: true
\n";
273 push @addresses, $ip;
276 if (defined(my $ip = $ipconfig->{ip6})) {
278 $content .= "${i
}dhcp6
: true
\n";
280 push @addresses, $ip;
284 $content .= "${i
}addresses
:\n";
285 $content .= "${i
}- '$_'\n" foreach @addresses;
287 if (defined(my $gw = $ipconfig->{gw})) {
288 $content .= "${i
}gateway4
: '$gw'\n";
290 if (defined(my $gw = $ipconfig->{gw6})) {
291 $content .= "${i
}gateway6
: '$gw'\n";
297 my ($searchdomains, $nameservers) = get_dns_conf($conf);
298 if ($searchdomains || $nameservers) {
299 $content .= "${i
}nameservers
:\n";
300 if (defined($nameservers) && @$nameservers) {
301 $content .= "${i
} addresses
:\n";
302 $content .= "${i
} - '$_'\n" foreach @$nameservers;
304 if (defined($searchdomains) && @$searchdomains) {
305 $content .= "${i
} search
:\n";
306 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
311 return $head.$content;
314 sub nocloud_network {
317 my $content = "version
: 1\n"
320 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
321 foreach my $iface (@ifaces) {
322 (my $id = $iface) =~ s/^net//;
323 next if !$conf->{"ipconfig
$id"};
325 # indentation - network interfaces are inside an 'ethernets' hash
328 my $net = PVE::QemuServer::parse_net($conf->{$iface});
329 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
331 my $mac = lc($net->{macaddr})
332 or die "network interface
'$iface' has no mac address
\n";
334 $content .= "${i
}- type
: physical
\n"
335 . "${i
} name
: eth
$id\n"
336 . "${i
} mac_address
: '$mac'\n"
339 if (defined(my $ip = $ipconfig->{ip})) {
341 $content .= "${i
}- type
: dhcp4
\n";
343 my ($addr, $mask) = split_ip4($ip);
344 $content .= "${i
}- type
: static
\n"
345 . "${i
} address
: '$addr'\n"
346 . "${i
} netmask
: '$mask'\n";
347 if (defined(my $gw = $ipconfig->{gw})) {
348 $content .= "${i
} gateway
: '$gw'\n";
352 if (defined(my $ip = $ipconfig->{ip6})) {
354 $content .= "${i
}- type
: dhcp6
\n";
355 } elsif ($ip eq 'auto') {
356 # SLAAC is not supported by cloud-init, this fallback should work with an up-to-date netplan at least
357 $content .= "${i
}- type
: dhcp6
\n";
359 $content .= "${i
}- type
: static
\n"
360 . "${i
} address
: '$ip'\n";
361 if (defined(my $gw = $ipconfig->{gw6})) {
362 $content .= "${i
} gateway
: '$gw'\n";
369 my ($searchdomains, $nameservers) = get_dns_conf($conf);
370 if ($searchdomains || $nameservers) {
371 $content .= "${i
}- type
: nameserver
\n";
372 if (defined($nameservers) && @$nameservers) {
373 $content .= "${i
} address
:\n";
374 $content .= "${i
} - '$_'\n" foreach @$nameservers;
376 if (defined($searchdomains) && @$searchdomains) {
377 $content .= "${i
} search
:\n";
378 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
385 sub nocloud_metadata {
387 return "instance-id
: $uuid\n";
390 sub generate_nocloud {
391 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
393 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
394 $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
395 $network_data = nocloud_network($conf) if !defined($network_data);
397 if (!defined($meta_data)) {
398 my $digest_data = $user_data . $network_data;
399 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
401 $meta_data = nocloud_metadata($uuid_str);
405 '/user-data' => $user_data,
406 '/network-config' => $network_data,
407 '/meta-data' => $meta_data
409 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
412 sub get_custom_cloudinit_files {
415 my $cicustom = $conf->{cicustom};
416 my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
418 my $network_volid = $files->{network};
419 my $user_volid = $files->{user};
420 my $meta_volid = $files->{meta};
422 my $storage_conf = PVE::Storage::config();
425 if ($network_volid) {
426 $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
431 $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
436 $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
439 return ($user_data, $network_data, $meta_data);
442 sub read_cloudinit_snippets_file {
443 my ($storage_conf, $volid) = @_;
445 my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
446 die "$volid is not in the snippets directory
\n" if $type ne 'snippets';
447 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
450 my $cloudinit_methods = {
451 configdrive2 => \&generate_configdrive2,
452 nocloud => \&generate_nocloud,
455 sub generate_cloudinitconfig {
456 my ($conf, $vmid) = @_;
458 my $format = get_cloudinit_format($conf);
460 PVE::QemuServer::foreach_drive($conf, sub {
461 my ($ds, $drive) = @_;
463 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
465 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
467 my $generator = $cloudinit_methods->{$format}
468 or die "missing cloudinit methods
for format
'$format'\n";
470 $generator->($conf, $vmid, $drive, $volname, $storeid);