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";
140 sub configdrive2_network
{
143 my $content = "auto lo\n";
144 $content .= "iface lo inet loopback\n\n";
146 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
147 if ($nameservers && @$nameservers) {
148 $nameservers = join(' ', @$nameservers);
149 $content .= " dns_nameservers $nameservers\n";
151 if ($searchdomains && @$searchdomains) {
152 $searchdomains = join(' ', @$searchdomains);
153 $content .= " dns_search $searchdomains\n";
156 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
157 foreach my $iface (@ifaces) {
158 (my $id = $iface) =~ s/^net//;
159 next if !$conf->{"ipconfig$id"};
160 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
163 $content .="auto $id\n";
165 if ($net->{ip
} eq 'dhcp') {
166 $content .= "iface $id inet dhcp\n";
168 my ($addr, $mask) = split('/', $net->{ip
});
169 $content .= "iface $id inet static\n";
170 $content .= " address $addr\n";
171 $content .= " netmask $PVE::Network::ipv4_reverse_mask->[$mask]\n";
172 $content .= " gateway $net->{gw}\n" if $net->{gw
};
176 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
177 $content .= "iface $id inet6 $1\n";
179 my ($addr, $mask) = split('/', $net->{ip6
});
180 $content .= "iface $id inet6 static\n";
181 $content .= " address $addr\n";
182 $content .= " netmask $mask\n";
183 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
191 sub configdrive2_metadata
{
196 "network_config": { "content_path": "/content/0000" }
201 sub generate_configdrive2
{
202 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
204 my $user_data = cloudinit_userdata
($conf, $vmid);
205 my $network_data = configdrive2_network
($conf);
207 my $digest_data = $user_data . $network_data;
208 my $uuid_str = Digest
::SHA
::sha1_hex
($digest_data);
210 my $meta_data = configdrive2_metadata
($uuid_str);
213 '/openstack/latest/user_data' => $user_data,
214 '/openstack/content/0000' => $network_data,
215 '/openstack/latest/meta_data.json' => $meta_data
217 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
220 sub nocloud_network_v2
{
225 my $head = "version: 2\n"
230 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
231 foreach my $iface (@ifaces) {
232 (my $id = $iface) =~ s/^net//;
233 next if !$conf->{"ipconfig$id"};
235 # indentation - network interfaces are inside an 'ethernets' hash
238 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
239 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
241 my $mac = $net->{macaddr
}
242 or die "network interface '$iface' has no mac address\n";
244 $content .= "${i}$iface:\n";
246 $content .= "${i}match:\n"
247 . "${i} macaddress: \"$mac\"\n"
248 . "${i
}set-name
: eth
$id\n";
250 if (defined(my $ip = $ipconfig->{ip})) {
252 $content .= "${i
}dhcp4
: true
\n";
254 push @addresses, $ip;
257 if (defined(my $ip = $ipconfig->{ip6})) {
259 $content .= "${i
}dhcp6
: true
\n";
261 push @addresses, $ip;
265 $content .= "${i
}addresses
:\n";
266 $content .= "${i
}- $_\n" foreach @addresses;
268 if (defined(my $gw = $ipconfig->{gw})) {
269 $content .= "${i
}gateway4
: $gw\n";
271 if (defined(my $gw = $ipconfig->{gw6})) {
272 $content .= "${i
}gateway6
: $gw\n";
278 my ($searchdomains, $nameservers) = get_dns_conf($conf);
279 if ($searchdomains || $nameservers) {
280 $content .= "${i
}nameservers
:\n";
281 if (defined($nameservers) && @$nameservers) {
282 $content .= "${i
} addresses
:\n";
283 $content .= "${i
} - $_\n" foreach @$nameservers;
285 if (defined($searchdomains) && @$searchdomains) {
286 $content .= "${i
} search
:\n";
287 $content .= "${i
} - $_\n" foreach @$searchdomains;
292 return $head.$content;
295 sub nocloud_network {
298 my $content = "version
: 1\n"
301 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
302 foreach my $iface (@ifaces) {
303 (my $id = $iface) =~ s/^net//;
304 next if !$conf->{"ipconfig
$id"};
306 # indentation - network interfaces are inside an 'ethernets' hash
309 my $net = PVE::QemuServer::parse_net($conf->{$iface});
310 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
312 my $mac = $net->{macaddr}
313 or die "network interface
'$iface' has no mac address
\n";
315 $content .= "${i
}- type
: physical
\n"
316 . "${i
} name
: eth
$id\n"
317 . "${i
} mac_address
: $mac\n"
320 if (defined(my $ip = $ipconfig->{ip})) {
322 $content .= "${i
}- type
: dhcp4
\n";
324 $content .= "${i
}- type
: static
\n"
325 . "${i
} address
: $ip\n";
326 if (defined(my $gw = $ipconfig->{gw})) {
327 $content .= "${i
} gateway
: $gw\n";
331 if (defined(my $ip = $ipconfig->{ip6})) {
333 $content .= "${i
}- type
: dhcp6
\n";
335 $content .= "${i
}- type
: static6
\n"
336 . "${i
} address
: $ip\n";
337 if (defined(my $gw = $ipconfig->{gw6})) {
338 $content .= "${i
} gateway
: $gw\n";
345 my ($searchdomains, $nameservers) = get_dns_conf($conf);
346 if ($searchdomains || $nameservers) {
347 $content .= "${i
}- type
: nameserver
\n";
348 if (defined($nameservers) && @$nameservers) {
349 $content .= "${i
} address
:\n";
350 $content .= "${i
} - $_\n" foreach @$nameservers;
352 if (defined($searchdomains) && @$searchdomains) {
353 $content .= "${i
} search
:\n";
354 $content .= "${i
} - $_\n" foreach @$searchdomains;
361 sub nocloud_metadata {
363 return "instance-id
: $uuid\n";
366 sub generate_nocloud {
367 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
369 my $user_data = cloudinit_userdata($conf, $vmid);
370 my $network_data = nocloud_network($conf);
372 my $digest_data = $user_data . $network_data;
373 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
375 my $meta_data = nocloud_metadata($uuid_str);
378 '/user-data' => $user_data,
379 '/network-config' => $network_data,
380 '/meta-data' => $meta_data
382 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
385 my $cloudinit_methods = {
386 configdrive2 => \&generate_configdrive2,
387 nocloud => \&generate_nocloud,
390 sub generate_cloudinitconfig {
391 my ($conf, $vmid) = @_;
393 my $format = get_cloudinit_format($conf);
395 PVE::QemuServer::foreach_drive($conf, sub {
396 my ($ds, $drive) = @_;
398 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
400 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
402 my $generator = $cloudinit_methods->{$format}
403 or die "missing cloudinit methods
for format
'$format'\n";
405 $generator->($conf, $vmid, $drive, $volname, $storeid);