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 = PVE
::Storage
::file_size_info
($iso_path);
38 run_command
([['genisoimage', '-R', '-V', $label, $path],
39 ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format,
40 'isize=0', "osize=$size", "of=$iso_path"]]);
47 sub get_cloudinit_format
{
49 if (defined(my $format = $conf->{citype
})) {
53 # No format specified, default based on ostype because windows'
54 # cloudbased-init only supports configdrivev2, whereas on linux we need
55 # to use mac addresses because regular cloudinit doesn't map 'ethX' to
56 # the new predicatble network device naming scheme.
57 if (defined(my $ostype = $conf->{ostype
})) {
59 if PVE
::QemuServer
::windows_version
($ostype);
65 sub get_hostname_fqdn
{
66 my ($conf, $vmid) = @_;
67 my $hostname = $conf->{name
} // "VM$vmid";
69 if ($hostname =~ /\./) {
71 $hostname =~ s/\..*$//;
72 } elsif (my $search = $conf->{searchdomain
}) {
73 $fqdn = "$hostname.$search";
75 return ($hostname, $fqdn);
81 # Same logic as in pve-container, but without the testcase special case
82 my $host_resolv_conf = PVE
::INotify
::read_file
('resolvconf');
85 split(/\s+/, $conf->{searchdomain
} // $host_resolv_conf->{search
})
88 my $nameserver = $conf->{nameserver
};
89 if (!defined($nameserver)) {
90 $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
92 $nameserver = [split(/\s+/, $nameserver)];
95 return ($searchdomains, $nameserver);
98 sub cloudinit_userdata
{
99 my ($conf, $vmid) = @_;
101 my ($hostname, $fqdn) = get_hostname_fqdn
($conf, $vmid);
103 my $content = "#cloud-config\n";
105 $content .= "hostname: $hostname\n";
106 $content .= "manage_etc_hosts: true\n";
107 $content .= "fqdn: $fqdn\n" if defined($fqdn);
109 my $username = $conf->{ciuser
};
110 my $password = $conf->{cipassword
};
112 $content .= "user: $username\n" if defined($username);
113 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
114 $content .= "password: $password\n" if defined($password);
116 if (defined(my $keys = $conf->{sshkeys
})) {
117 $keys = URI
::Escape
::uri_unescape
($keys);
118 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
119 $keys = [grep { /\S/ } @$keys];
120 $content .= "ssh_authorized_keys:\n";
121 foreach my $k (@$keys) {
122 $content .= " - $k\n";
125 $content .= "chpasswd:\n";
126 $content .= " expire: False\n";
128 if (!defined($username) || $username ne 'root') {
129 $content .= "users:\n";
130 $content .= " - default\n";
133 $content .= "package_upgrade: true\n";
138 sub configdrive2_network
{
141 my $content = "auto lo\n";
142 $content .= "iface lo inet loopback\n\n";
144 my ($searchdomains, $nameservers) = get_dns_conf
($conf);
145 if ($nameservers && @$nameservers) {
146 $nameservers = join(' ', @$nameservers);
147 $content .= " dns_nameservers $nameservers\n";
149 if ($searchdomains && @$searchdomains) {
150 $searchdomains = join(' ', @$searchdomains);
151 $content .= " dns_search $searchdomains\n";
154 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
155 foreach my $iface (@ifaces) {
156 (my $id = $iface) =~ s/^net//;
157 next if !$conf->{"ipconfig$id"};
158 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
161 $content .="auto $id\n";
163 if ($net->{ip
} eq 'dhcp') {
164 $content .= "iface $id inet dhcp\n";
166 my ($addr, $mask) = split('/', $net->{ip
});
167 $content .= "iface $id inet static\n";
168 $content .= " address $addr\n";
169 $content .= " netmask $PVE::Network::ipv4_reverse_mask->[$mask]\n";
170 $content .= " gateway $net->{gw}\n" if $net->{gw
};
174 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
175 $content .= "iface $id inet6 $1\n";
177 my ($addr, $mask) = split('/', $net->{ip6
});
178 $content .= "iface $id inet6 static\n";
179 $content .= " address $addr\n";
180 $content .= " netmask $mask\n";
181 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
189 sub configdrive2_metadata
{
194 "network_config": { "content_path": "/content/0000" }
199 sub generate_configdrive2
{
200 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
202 my $user_data = cloudinit_userdata
($conf, $vmid);
203 my $network_data = configdrive2_network
($conf);
205 my $digest_data = $user_data . $network_data;
206 my $uuid_str = Digest
::SHA
::sha1_hex
($digest_data);
208 my $meta_data = configdrive2_metadata
($uuid_str);
211 '/openstack/latest/user_data' => $user_data,
212 '/openstack/content/0000' => $network_data,
213 '/openstack/latest/meta_data.json' => $meta_data
215 commit_cloudinit_disk
($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
218 sub nocloud_network_v2
{
223 my $head = "version: 2\n"
228 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
229 foreach my $iface (@ifaces) {
230 (my $id = $iface) =~ s/^net//;
231 next if !$conf->{"ipconfig$id"};
233 # indentation - network interfaces are inside an 'ethernets' hash
236 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
237 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
239 my $mac = $net->{macaddr
}
240 or die "network interface '$iface' has no mac address\n";
242 $content .= "${i}$iface:\n";
244 $content .= "${i}match:\n"
245 . "${i} macaddress: \"$mac\"\n"
246 . "${i
}set-name
: eth
$id\n";
248 if (defined(my $ip = $ipconfig->{ip})) {
250 $content .= "${i
}dhcp4
: true
\n";
252 push @addresses, $ip;
255 if (defined(my $ip = $ipconfig->{ip6})) {
257 $content .= "${i
}dhcp6
: true
\n";
259 push @addresses, $ip;
263 $content .= "${i
}addresses
:\n";
264 $content .= "${i
}- $_\n" foreach @addresses;
266 if (defined(my $gw = $ipconfig->{gw})) {
267 $content .= "${i
}gateway4
: $gw\n";
269 if (defined(my $gw = $ipconfig->{gw6})) {
270 $content .= "${i
}gateway6
: $gw\n";
276 my ($searchdomains, $nameservers) = get_dns_conf($conf);
277 if ($searchdomains || $nameservers) {
278 $content .= "${i
}nameservers
:\n";
279 if (defined($nameservers) && @$nameservers) {
280 $content .= "${i
} addresses
:\n";
281 $content .= "${i
} - $_\n" foreach @$nameservers;
283 if (defined($searchdomains) && @$searchdomains) {
284 $content .= "${i
} search
:\n";
285 $content .= "${i
} - $_\n" foreach @$searchdomains;
290 return $head.$content;
293 sub nocloud_network {
296 my $content = "version
: 1\n"
299 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
300 foreach my $iface (@ifaces) {
301 (my $id = $iface) =~ s/^net//;
302 next if !$conf->{"ipconfig
$id"};
304 # indentation - network interfaces are inside an 'ethernets' hash
307 my $net = PVE::QemuServer::parse_net($conf->{$iface});
308 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
310 my $mac = $net->{macaddr}
311 or die "network interface
'$iface' has no mac address
\n";
313 $content .= "${i
}- type
: physical
\n"
314 . "${i
} name
: eth
$id\n"
315 . "${i
} mac_address
: $mac\n"
318 if (defined(my $ip = $ipconfig->{ip})) {
320 $content .= "${i
}- type
: dhcp4
\n";
322 $content .= "${i
}- type
: static
\n"
323 . "${i
} address
: $ip\n";
324 if (defined(my $gw = $ipconfig->{gw})) {
325 $content .= "${i
} gateway
: $gw\n";
329 if (defined(my $ip = $ipconfig->{ip6})) {
331 $content .= "${i
}- type
: dhcp6
\n";
333 $content .= "${i
}- type
: static6
\n"
334 . "${i
} address
: $ip\n";
335 if (defined(my $gw = $ipconfig->{gw6})) {
336 $content .= "${i
} gateway
: $gw\n";
343 my ($searchdomains, $nameservers) = get_dns_conf($conf);
344 if ($searchdomains || $nameservers) {
345 $content .= "${i
}- type
: nameserver
\n";
346 if (defined($nameservers) && @$nameservers) {
347 $content .= "${i
} address
:\n";
348 $content .= "${i
} - $_\n" foreach @$nameservers;
350 if (defined($searchdomains) && @$searchdomains) {
351 $content .= "${i
} search
:\n";
352 $content .= "${i
} - $_\n" foreach @$searchdomains;
359 sub nocloud_metadata {
361 return "instance-id
: $uuid\n";
364 sub generate_nocloud {
365 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
367 my $user_data = cloudinit_userdata($conf, $vmid);
368 my $network_data = nocloud_network($conf);
370 my $digest_data = $user_data . $network_data;
371 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
373 my $meta_data = nocloud_metadata($uuid_str);
376 '/user-data' => $user_data,
377 '/network-config' => $network_data,
378 '/meta-data' => $meta_data
380 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
383 my $cloudinit_methods = {
384 configdrive2 => \&generate_configdrive2,
385 nocloud => \&generate_nocloud,
388 sub generate_cloudinitconfig {
389 my ($conf, $vmid) = @_;
391 my $format = get_cloudinit_format($conf);
393 PVE::QemuServer::foreach_drive($conf, sub {
394 my ($ds, $drive) = @_;
396 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
398 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
400 my $generator = $cloudinit_methods->{$format}
401 or die "missing cloudinit methods
for format
'$format'\n";
403 $generator->($conf, $vmid, $drive, $volname, $storeid);