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);
49 run_command
([['genisoimage', '-iso-level', '3', '-R', '-V', $label, $path],
50 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
51 'isize=0', "osize=$size", "of=$iso_path"]]);
58 sub get_cloudinit_format
{
60 if (defined(my $format = $conf->{citype
})) {
64 # No format specified, default based on ostype because windows'
65 # cloudbased-init only supports configdrivev2, whereas on linux we need
66 # to use mac addresses because regular cloudinit doesn't map 'ethX' to
67 # the new predicatble network device naming scheme.
68 if (defined(my $ostype = $conf->{ostype
})) {
70 if PVE
::QemuServer
::windows_version
($ostype);
76 sub get_hostname_fqdn
{
77 my ($conf, $vmid) = @_;
78 my $hostname = $conf->{name
} // "VM$vmid";
80 if ($hostname =~ /\./) {
82 $hostname =~ s/\..*$//;
83 } elsif (my $search = $conf->{searchdomain
}) {
84 $fqdn = "$hostname.$search";
86 return ($hostname, $fqdn);
92 # Same logic as in pve-container, but without the testcase special case
93 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
96 split(/\s+/, $conf->{searchdomain
} // $host_resolv_conf->{search
})
99 my $nameserver = $conf->{nameserver
};
100 if (!defined($nameserver)) {
101 $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
103 $nameserver = [split(/\s+/, $nameserver)];
106 return ($searchdomains, $nameserver);
109 sub cloudinit_userdata
{
110 my ($conf, $vmid) = @_;
112 my ($hostname, $fqdn) = get_hostname_fqdn
($conf, $vmid);
114 my $content = "#cloud-config\n";
116 $content .= "hostname: $hostname\n";
117 $content .= "manage_etc_hosts: true\n";
118 $content .= "fqdn: $fqdn\n" if defined($fqdn);
120 my $username = $conf->{ciuser
};
121 my $password = $conf->{cipassword
};
123 $content .= "user: $username\n" if defined($username);
124 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
125 $content .= "password: $password\n" if defined($password);
127 if (defined(my $keys = $conf->{sshkeys
})) {
128 $keys = URI
::Escape
::uri_unescape
($keys);
129 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
130 $keys = [grep { /\S/ } @$keys];
131 $content .= "ssh_authorized_keys:\n";
132 foreach my $k (@$keys) {
133 $content .= " - $k\n";
136 $content .= "chpasswd:\n";
137 $content .= " expire: False\n";
139 if (!defined($username) || $username ne 'root') {
140 $content .= "users:\n";
141 $content .= " - default\n";
144 $content .= "package_upgrade: true\n";
151 my ($addr, $mask) = split('/', $ip);
152 die "not a CIDR: $ip\n" if !defined $mask;
153 return ($addr, $PVE::Network
::ipv4_reverse_mask-
>[$mask]);
156 sub configdrive2_network
{
159 my $content = "auto lo\n";
160 $content .= "iface lo inet loopback\n\n";
162 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
163 if ($nameservers && @$nameservers) {
164 $nameservers = join(' ', @$nameservers);
165 $content .= " dns_nameservers $nameservers\n";
167 if ($searchdomains && @$searchdomains) {
168 $searchdomains = join(' ', @$searchdomains);
169 $content .= " dns_search $searchdomains\n";
172 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
173 foreach my $iface (@ifaces) {
174 (my $id = $iface) =~ s/^net//;
175 next if !$conf->{"ipconfig$id"};
176 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
179 $content .="auto $id\n";
181 if ($net->{ip
} eq 'dhcp') {
182 $content .= "iface $id inet dhcp\n";
184 my ($addr, $mask) = split_ip4
($net->{ip
});
185 $content .= "iface $id inet static\n";
186 $content .= " address $addr\n";
187 $content .= " netmask $mask\n";
188 $content .= " gateway $net->{gw}\n" if $net->{gw
};
192 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
193 $content .= "iface $id inet6 $1\n";
195 my ($addr, $mask) = split('/', $net->{ip6
});
196 $content .= "iface $id inet6 static\n";
197 $content .= " address $addr\n";
198 $content .= " netmask $mask\n";
199 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
207 sub configdrive2_gen_metadata
{
208 my ($user, $network) = @_;
210 my $uuid_str = Digest
::SHA
::sha1_hex
($user.$network);
211 return configdrive2_metadata
($uuid_str);
214 sub configdrive2_metadata
{
219 "network_config": { "content_path": "/content/0000" }
224 sub generate_configdrive2
{
225 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
227 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files
($conf);
228 $user_data = cloudinit_userdata
($conf, $vmid) if !defined($user_data);
229 $network_data = configdrive2_network
($conf) if !defined($network_data);
231 if (!defined($meta_data)) {
232 $meta_data = configdrive2_gen_metadata
($user_data, $network_data);
235 '/openstack/latest/user_data' => $user_data,
236 '/openstack/content/0000' => $network_data,
237 '/openstack/latest/meta_data.json' => $meta_data
239 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
242 sub nocloud_network_v2
{
247 my $head = "version: 2\n"
252 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
253 foreach my $iface (@ifaces) {
254 (my $id = $iface) =~ s/^net//;
255 next if !$conf->{"ipconfig$id"};
257 # indentation - network interfaces are inside an 'ethernets' hash
260 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
261 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
263 my $mac = $net->{macaddr
}
264 or die "network interface '$iface' has no mac address\n";
266 $content .= "${i}$iface:\n";
268 $content .= "${i}match:\n"
269 . "${i} macaddress: \"$mac\"\n"
270 . "${i
}set-name
: eth
$id\n";
272 if (defined(my $ip = $ipconfig->{ip})) {
274 $content .= "${i
}dhcp4
: true
\n";
276 push @addresses, $ip;
279 if (defined(my $ip = $ipconfig->{ip6})) {
281 $content .= "${i
}dhcp6
: true
\n";
283 push @addresses, $ip;
287 $content .= "${i
}addresses
:\n";
288 $content .= "${i
}- '$_'\n" foreach @addresses;
290 if (defined(my $gw = $ipconfig->{gw})) {
291 $content .= "${i
}gateway4
: '$gw'\n";
293 if (defined(my $gw = $ipconfig->{gw6})) {
294 $content .= "${i
}gateway6
: '$gw'\n";
300 my ($searchdomains, $nameservers) = get_dns_conf($conf);
301 if ($searchdomains || $nameservers) {
302 $content .= "${i
}nameservers
:\n";
303 if (defined($nameservers) && @$nameservers) {
304 $content .= "${i
} addresses
:\n";
305 $content .= "${i
} - '$_'\n" foreach @$nameservers;
307 if (defined($searchdomains) && @$searchdomains) {
308 $content .= "${i
} search
:\n";
309 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
314 return $head.$content;
317 sub nocloud_network {
320 my $content = "version
: 1\n"
323 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
324 foreach my $iface (@ifaces) {
325 (my $id = $iface) =~ s/^net//;
326 next if !$conf->{"ipconfig
$id"};
328 # indentation - network interfaces are inside an 'ethernets' hash
331 my $net = PVE::QemuServer::parse_net($conf->{$iface});
332 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
334 my $mac = lc($net->{macaddr})
335 or die "network interface
'$iface' has no mac address
\n";
337 $content .= "${i
}- type
: physical
\n"
338 . "${i
} name
: eth
$id\n"
339 . "${i
} mac_address
: '$mac'\n"
342 if (defined(my $ip = $ipconfig->{ip})) {
344 $content .= "${i
}- type
: dhcp4
\n";
346 my ($addr, $mask) = split_ip4($ip);
347 $content .= "${i
}- type
: static
\n"
348 . "${i
} address
: '$addr'\n"
349 . "${i
} netmask
: '$mask'\n";
350 if (defined(my $gw = $ipconfig->{gw})) {
351 $content .= "${i
} gateway
: '$gw'\n";
355 if (defined(my $ip = $ipconfig->{ip6})) {
357 $content .= "${i
}- type
: dhcp6
\n";
358 } elsif ($ip eq 'auto') {
359 # SLAAC is not supported by cloud-init, this fallback should work with an up-to-date netplan at least
360 $content .= "${i
}- type
: dhcp6
\n";
362 $content .= "${i
}- type
: static
\n"
363 . "${i
} address
: '$ip'\n";
364 if (defined(my $gw = $ipconfig->{gw6})) {
365 $content .= "${i
} gateway
: '$gw'\n";
372 my ($searchdomains, $nameservers) = get_dns_conf($conf);
373 if ($searchdomains || $nameservers) {
374 $content .= "${i
}- type
: nameserver
\n";
375 if (defined($nameservers) && @$nameservers) {
376 $content .= "${i
} address
:\n";
377 $content .= "${i
} - '$_'\n" foreach @$nameservers;
379 if (defined($searchdomains) && @$searchdomains) {
380 $content .= "${i
} search
:\n";
381 $content .= "${i
} - '$_'\n" foreach @$searchdomains;
388 sub nocloud_metadata {
390 return "instance-id
: $uuid\n";
393 sub nocloud_gen_metadata {
394 my ($user, $network) = @_;
396 my $uuid_str = Digest::SHA::sha1_hex($user.$network);
397 return nocloud_metadata($uuid_str);
400 sub generate_nocloud {
401 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
403 my ($user_data, $network_data, $meta_data) = get_custom_cloudinit_files($conf);
404 $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
405 $network_data = nocloud_network($conf) if !defined($network_data);
407 if (!defined($meta_data)) {
408 $meta_data = nocloud_gen_metadata($user_data, $network_data);
412 '/user-data' => $user_data,
413 '/network-config' => $network_data,
414 '/meta-data' => $meta_data
416 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
419 sub get_custom_cloudinit_files {
422 my $cicustom = $conf->{cicustom};
423 my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
425 my $network_volid = $files->{network};
426 my $user_volid = $files->{user};
427 my $meta_volid = $files->{meta};
429 my $storage_conf = PVE::Storage::config();
432 if ($network_volid) {
433 $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
438 $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
443 $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
446 return ($user_data, $network_data, $meta_data);
449 sub read_cloudinit_snippets_file {
450 my ($storage_conf, $volid) = @_;
452 my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
453 die "$volid is not in the snippets directory
\n" if $type ne 'snippets';
454 return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
457 my $cloudinit_methods = {
458 configdrive2 => \&generate_configdrive2,
459 nocloud => \&generate_nocloud,
462 sub generate_cloudinitconfig {
463 my ($conf, $vmid) = @_;
465 my $format = get_cloudinit_format($conf);
467 PVE::QemuServer::foreach_drive($conf, sub {
468 my ($ds, $drive) = @_;
470 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
472 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
474 my $generator = $cloudinit_methods->{$format}
475 or die "missing cloudinit methods
for format
'$format'\n";
477 $generator->($conf, $vmid, $drive, $volname, $storeid);
481 sub dump_cloudinit_config {
482 my ($conf, $vmid, $type) = @_;
484 my $format = get_cloudinit_format($conf);
486 if ($type eq 'user') {
487 return cloudinit_userdata($conf, $vmid);
488 } elsif ($type eq 'network') {
489 if ($format eq 'nocloud') {
490 return nocloud_network($conf);
492 return configdrive2_network($conf);
494 } else { # metadata config
495 my $user = cloudinit_userdata($conf, $vmid);
496 if ($format eq 'nocloud') {
497 my $network = nocloud_network($conf);
498 return nocloud_gen_metadata($user, $network);
500 my $network = configdrive2_network($conf);
501 return configdrive2_gen_metadata($user, $network);