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 $plugin = PVE
::Storage
::Plugin-
>lookup($scfg->{type
});
34 $plugin->activate_volume($storeid, $scfg, $volname);
35 my $format = PVE
::QemuServer
::qemu_img_format
($scfg, $volname);
37 my $size = PVE
::Storage
::file_size_info
($iso_path);
40 run_command
([['genisoimage', '-R', '-V', $label, $path],
41 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
42 'isize=0', "osize=$size", "of=$iso_path"]]);
49 sub get_cloudinit_format
{
51 if (defined(my $format = $conf->{citype
})) {
55 # No format specified, default based on ostype because windows'
56 # cloudbased-init only supports configdrivev2, whereas on linux we need
57 # to use mac addresses because regular cloudinit doesn't map 'ethX' to
58 # the new predicatble network device naming scheme.
59 if (defined(my $ostype = $conf->{ostype
})) {
61 if PVE
::QemuServer
::windows_version
($ostype);
67 sub get_hostname_fqdn
{
68 my ($conf, $vmid) = @_;
69 my $hostname = $conf->{name
} // "VM$vmid";
71 if ($hostname =~ /\./) {
73 $hostname =~ s/\..*$//;
74 } elsif (my $search = $conf->{searchdomain
}) {
75 $fqdn = "$hostname.$search";
77 return ($hostname, $fqdn);
83 # Same logic as in pve-container, but without the testcase special case
84 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
87 split(/\s+/, $conf->{searchdomain
} // $host_resolv_conf->{search
})
90 my $nameserver = $conf->{nameserver
};
91 if (!defined($nameserver)) {
92 $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
94 $nameserver = [split(/\s+/, $nameserver)];
97 return ($searchdomains, $nameserver);
100 sub cloudinit_userdata
{
101 my ($conf, $vmid) = @_;
103 my ($hostname, $fqdn) = get_hostname_fqdn
($conf, $vmid);
105 my $content = "#cloud-config\n";
107 $content .= "hostname: $hostname\n";
108 $content .= "manage_etc_hosts: true\n";
109 $content .= "fqdn: $fqdn\n" if defined($fqdn);
111 my $username = $conf->{ciuser
};
112 my $password = $conf->{cipassword
};
114 $content .= "user: $username\n" if defined($username);
115 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
116 $content .= "password: $password\n" if defined($password);
118 if (defined(my $keys = $conf->{sshkeys
})) {
119 $keys = URI
::Escape
::uri_unescape
($keys);
120 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
121 $keys = [grep { /\S/ } @$keys];
122 $content .= "ssh_authorized_keys:\n";
123 foreach my $k (@$keys) {
124 $content .= " - $k\n";
127 $content .= "chpasswd:\n";
128 $content .= " expire: False\n";
130 if (!defined($username) || $username ne 'root') {
131 $content .= "users:\n";
132 $content .= " - default\n";
135 $content .= "package_upgrade: true\n";
142 my ($addr, $mask) = split('/', $ip);
143 die "not a CIDR: $ip\n" if !defined $mask;
144 return ($addr, $PVE::Network
::ipv4_reverse_mask-
>[$mask]);
147 sub configdrive2_network
{
150 my $content = "auto lo\n";
151 $content .= "iface lo inet loopback\n\n";
153 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
154 if ($nameservers && @$nameservers) {
155 $nameservers = join(' ', @$nameservers);
156 $content .= " dns_nameservers $nameservers\n";
158 if ($searchdomains && @$searchdomains) {
159 $searchdomains = join(' ', @$searchdomains);
160 $content .= " dns_search $searchdomains\n";
163 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
164 foreach my $iface (@ifaces) {
165 (my $id = $iface) =~ s/^net//;
166 next if !$conf->{"ipconfig$id"};
167 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
170 $content .="auto $id\n";
172 if ($net->{ip
} eq 'dhcp') {
173 $content .= "iface $id inet dhcp\n";
175 my ($addr, $mask) = split_ip4
($net->{ip
});
176 $content .= "iface $id inet static\n";
177 $content .= " address $addr\n";
178 $content .= " netmask $mask\n";
179 $content .= " gateway $net->{gw}\n" if $net->{gw
};
183 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
184 $content .= "iface $id inet6 $1\n";
186 my ($addr, $mask) = split('/', $net->{ip6
});
187 $content .= "iface $id inet6 static\n";
188 $content .= " address $addr\n";
189 $content .= " netmask $mask\n";
190 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
198 sub configdrive2_metadata
{
203 "network_config": { "content_path": "/content/0000" }
208 sub generate_configdrive2
{
209 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
211 my $user_data = cloudinit_userdata
($conf, $vmid);
212 my $network_data = configdrive2_network
($conf);
214 my $digest_data = $user_data . $network_data;
215 my $uuid_str = Digest
::SHA
::sha1_hex
($digest_data);
217 my $meta_data = configdrive2_metadata
($uuid_str);
220 '/openstack/latest/user_data' => $user_data,
221 '/openstack/content/0000' => $network_data,
222 '/openstack/latest/meta_data.json' => $meta_data
224 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
227 sub nocloud_network_v2
{
232 my $head = "version: 2\n"
237 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
238 foreach my $iface (@ifaces) {
239 (my $id = $iface) =~ s/^net//;
240 next if !$conf->{"ipconfig$id"};
242 # indentation - network interfaces are inside an 'ethernets' hash
245 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
246 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
248 my $mac = $net->{macaddr
}
249 or die "network interface '$iface' has no mac address\n";
251 $content .= "${i}$iface:\n";
253 $content .= "${i}match:\n"
254 . "${i} macaddress: \"$mac\"\n"
255 . "${i
}set-name
: eth
$id\n";
257 if (defined(my $ip = $ipconfig->{ip})) {
259 $content .= "${i
}dhcp4
: true
\n";
261 push @addresses, $ip;
264 if (defined(my $ip = $ipconfig->{ip6})) {
266 $content .= "${i
}dhcp6
: true
\n";
268 push @addresses, $ip;
272 $content .= "${i
}addresses
:\n";
273 $content .= "${i
}- $_\n" foreach @addresses;
275 if (defined(my $gw = $ipconfig->{gw})) {
276 $content .= "${i
}gateway4
: $gw\n";
278 if (defined(my $gw = $ipconfig->{gw6})) {
279 $content .= "${i
}gateway6
: $gw\n";
285 my ($searchdomains, $nameservers) = get_dns_conf($conf);
286 if ($searchdomains || $nameservers) {
287 $content .= "${i
}nameservers
:\n";
288 if (defined($nameservers) && @$nameservers) {
289 $content .= "${i
} addresses
:\n";
290 $content .= "${i
} - $_\n" foreach @$nameservers;
292 if (defined($searchdomains) && @$searchdomains) {
293 $content .= "${i
} search
:\n";
294 $content .= "${i
} - $_\n" foreach @$searchdomains;
299 return $head.$content;
302 sub nocloud_network {
305 my $content = "version
: 1\n"
308 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
309 foreach my $iface (@ifaces) {
310 (my $id = $iface) =~ s/^net//;
311 next if !$conf->{"ipconfig
$id"};
313 # indentation - network interfaces are inside an 'ethernets' hash
316 my $net = PVE::QemuServer::parse_net($conf->{$iface});
317 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
319 my $mac = lc($net->{macaddr})
320 or die "network interface
'$iface' has no mac address
\n";
322 $content .= "${i
}- type
: physical
\n"
323 . "${i
} name
: eth
$id\n"
324 . "${i
} mac_address
: $mac\n"
327 if (defined(my $ip = $ipconfig->{ip})) {
329 $content .= "${i
}- type
: dhcp4
\n";
331 my ($addr, $mask) = split_ip4($ip);
332 $content .= "${i
}- type
: static
\n"
333 . "${i
} address
: $addr\n"
334 . "${i
} netmask
: $mask\n";
335 if (defined(my $gw = $ipconfig->{gw})) {
336 $content .= "${i
} gateway
: $gw\n";
340 if (defined(my $ip = $ipconfig->{ip6})) {
342 $content .= "${i
}- type
: dhcp6
\n";
343 } elsif ($ip eq 'auto') {
344 # SLAAC is not supported by cloud-init, this fallback should work with an up-to-date netplan at least
345 $content .= "${i
}- type
: dhcp6
\n";
347 $content .= "${i
}- type
: static
\n"
348 . "${i
} address
: $ip\n";
349 if (defined(my $gw = $ipconfig->{gw6})) {
350 $content .= "${i
} gateway
: $gw\n";
357 my ($searchdomains, $nameservers) = get_dns_conf($conf);
358 if ($searchdomains || $nameservers) {
359 $content .= "${i
}- type
: nameserver
\n";
360 if (defined($nameservers) && @$nameservers) {
361 $content .= "${i
} address
:\n";
362 $content .= "${i
} - $_\n" foreach @$nameservers;
364 if (defined($searchdomains) && @$searchdomains) {
365 $content .= "${i
} search
:\n";
366 $content .= "${i
} - $_\n" foreach @$searchdomains;
373 sub nocloud_metadata {
375 return "instance-id
: $uuid\n";
378 sub generate_nocloud {
379 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
381 my $user_data = cloudinit_userdata($conf, $vmid);
382 my $network_data = nocloud_network($conf);
384 my $digest_data = $user_data . $network_data;
385 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
387 my $meta_data = nocloud_metadata($uuid_str);
390 '/user-data' => $user_data,
391 '/network-config' => $network_data,
392 '/meta-data' => $meta_data
394 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
397 my $cloudinit_methods = {
398 configdrive2 => \&generate_configdrive2,
399 nocloud => \&generate_nocloud,
402 sub generate_cloudinitconfig {
403 my ($conf, $vmid) = @_;
405 my $format = get_cloudinit_format($conf);
407 PVE::QemuServer::foreach_drive($conf, sub {
408 my ($ds, $drive) = @_;
410 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
412 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
414 my $generator = $cloudinit_methods->{$format}
415 or die "missing cloudinit methods
for format
'$format'\n";
417 $generator->($conf, $vmid, $drive, $volname, $storeid);