1 package PVE
::QemuServer
::Cloudinit
;
10 use PVE
::Tools
qw(run_command file_set_contents);
14 sub commit_cloudinit_disk
{
15 my ($vmid, $conf, $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', '-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
{
67 my $hostname = $conf->{name
};
69 if ($hostname =~ /\./) {
71 $hostname =~ s/\..*$//;
72 } elsif (my $search = $conf->{searchdomain
}) {
73 $fqdn = "$hostname.$search";
75 return ($hostname, $fqdn);
78 sub cloudinit_userdata
{
81 my ($hostname, $fqdn) = get_hostname_fqdn
($conf);
83 my $content = "#cloud-config\n";
84 $content .= "manage_resolv_conf: true\n";
86 $content .= "hostname: $hostname\n";
87 $content .= "fqdn: $fqdn\n" if defined($fqdn);
89 my $username = $conf->{ciuser
};
90 my $password = $conf->{cipassword
};
92 $content .= "user: $username\n" if defined($username);
93 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
94 $content .= "password: $password\n" if defined($password);
96 if (defined(my $keys = $conf->{sshkeys
})) {
97 $keys = URI
::Escape
::uri_unescape
($keys);
98 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
99 $keys = [grep { /\S/ } @$keys];
100 $content .= "ssh_authorized_keys:\n";
101 foreach my $k (@$keys) {
102 $content .= " - $k\n";
105 $content .= "chpasswd:\n";
106 $content .= " expire: False\n";
108 if (!defined($username) || $username ne 'root') {
109 $content .= "users:\n";
110 $content .= " - default\n";
113 $content .= "package_upgrade: true\n";
118 sub configdrive2_network
{
121 my $content = "auto lo\n";
122 $content .="iface lo inet loopback\n\n";
124 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
125 foreach my $iface (@ifaces) {
126 (my $id = $iface) =~ s/^net//;
127 next if !$conf->{"ipconfig$id"};
128 my $net = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
131 $content .="auto $id\n";
133 if ($net->{ip
} eq 'dhcp') {
134 $content .= "iface $id inet dhcp\n";
136 my ($addr, $mask) = split('/', $net->{ip
});
137 $content .= "iface $id inet static\n";
138 $content .= " address $addr\n";
139 $content .= " netmask $PVE::Network::ipv4_reverse_mask->[$mask]\n";
140 $content .= " gateway $net->{gw}\n" if $net->{gw
};
144 if ($net->{ip6
} =~ /^(auto|dhcp)$/) {
145 $content .= "iface $id inet6 $1\n";
147 my ($addr, $mask) = split('/', $net->{ip6
});
148 $content .= "iface $id inet6 static\n";
149 $content .= " address $addr\n";
150 $content .= " netmask $mask\n";
151 $content .= " gateway $net->{gw6}\n" if $net->{gw6
};
156 $content .=" dns_nameservers $conf->{nameserver}\n" if $conf->{nameserver
};
157 $content .=" dns_search $conf->{searchdomain}\n" if $conf->{searchdomain
};
162 sub configdrive2_metadata
{
167 "network_config": { "content_path": "/content/0000" }
172 sub generate_configdrive2
{
173 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
175 my $user_data = cloudinit_userdata
($conf);
176 my $network_data = configdrive2_network
($conf);
178 my $digest_data = $user_data . $network_data;
179 my $uuid_str = Digest
::SHA
::sha1_hex
($digest_data);
181 my $meta_data = configdrive2_metadata
($uuid_str);
184 '/openstack/latest/user_data' => $user_data,
185 '/openstack/content/0000' => $network_data,
186 '/openstack/latest/meta_data.json' => $meta_data
188 commit_cloudinit_disk
($vmid, $conf, $drive, $volname, $storeid, $files, 'config-2');
191 sub nocloud_network_v2
{
196 my $head = "version: 2\n"
199 my $nameservers_done;
201 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
202 foreach my $iface (@ifaces) {
203 (my $id = $iface) =~ s/^net//;
204 next if !$conf->{"ipconfig$id"};
206 # indentation - network interfaces are inside an 'ethernets' hash
209 my $net = PVE
::QemuServer
::parse_net
($conf->{$iface});
210 my $ipconfig = PVE
::QemuServer
::parse_ipconfig
($conf->{"ipconfig$id"});
212 my $mac = $net->{macaddr
}
213 or die "network interface '$iface' has no mac address\n";
215 my $data = "${i}$iface:\n";
217 $data .= "${i}match:\n"
218 . "${i} macaddress: \"$mac\"\n"
219 . "${i
}set-name
: eth
$id\n";
221 if (defined(my $ip = $ipconfig->{ip})) {
223 $data .= "${i
}dhcp4
: true
\n";
225 push @addresses, $ip;
228 if (defined(my $ip = $ipconfig->{ip6})) {
230 $data .= "${i
}dhcp6
: true
\n";
232 push @addresses, $ip;
236 $data .= "${i
}addresses
:\n";
237 $data .= "${i
}- $_\n" foreach @addresses;
239 if (defined(my $gw = $ipconfig->{gw})) {
240 $data .= "${i
}gateway4
: $gw\n";
242 if (defined(my $gw = $ipconfig->{gw6})) {
243 $data .= "${i
}gateway6
: $gw\n";
246 if (!$nameservers_done) {
247 $nameservers_done = 1;
249 my $nameserver = $conf->{nameserver} // '';
250 my $searchdomain = $conf->{searchdomain} // '';
251 my @nameservers = PVE::Tools::split_list($nameserver);
252 my @searchdomains = PVE::Tools::split_list($searchdomain);
253 if (@nameservers || @searchdomains) {
254 $data .= "${i
}nameservers
:\n";
255 $data .= "${i
} addresses
: [".join(',', @nameservers)."]\n"
257 $data .= "${i
} search
: [".join(',', @searchdomains)."]\n"
266 return $head.$content;
269 sub nocloud_network {
272 my $content = "version
: 1\n"
275 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
276 foreach my $iface (@ifaces) {
277 (my $id = $iface) =~ s/^net//;
278 next if !$conf->{"ipconfig
$id"};
280 # indentation - network interfaces are inside an 'ethernets' hash
283 my $net = PVE::QemuServer::parse_net($conf->{$iface});
284 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig
$id"});
286 my $mac = $net->{macaddr}
287 or die "network interface
'$iface' has no mac address
\n";
289 my $data = "${i
}- type
: physical
\n"
290 . "${i
} name
: eth
$id\n"
291 . "${i
} mac_address
: $mac\n"
294 if (defined(my $ip = $ipconfig->{ip})) {
296 $data .= "${i
}- type
: dhcp4
\n";
298 $data .= "${i
}- type
: static
\n"
299 . "${i
} address
: $ip\n";
300 if (defined(my $gw = $ipconfig->{gw})) {
301 $data .= "${i
} gateway
: $gw\n";
305 if (defined(my $ip = $ipconfig->{ip6})) {
307 $data .= "${i
}- type
: dhcp6
\n";
309 $data .= "${i
}- type
: static6
\n"
310 . "${i
} address
: $ip\n";
311 if (defined(my $gw = $ipconfig->{gw6})) {
312 $data .= "${i
} gateway
: $gw\n";
320 my $nameserver = $conf->{nameserver} // '';
321 my $searchdomain = $conf->{searchdomain} // '';
322 my @nameservers = PVE::Tools::split_list($nameserver);
323 my @searchdomains = PVE::Tools::split_list($searchdomain);
324 if (@nameservers || @searchdomains) {
326 $content .= "${i
}- type
: nameserver
\n";
328 $content .= "${i
} address
:\n";
329 $content .= "${i
} - $_\n" foreach @nameservers;
331 if (@searchdomains) {
332 $content .= "${i
} search
:\n";
333 $content .= "${i
} - $_\n" foreach @searchdomains;
340 sub nocloud_metadata {
342 return "instance-id
: $uuid\n";
345 sub generate_nocloud {
346 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
348 my $user_data = cloudinit_userdata($conf);
349 my $network_data = nocloud_network($conf);
351 my $digest_data = $user_data . $network_data;
352 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
354 my $meta_data = nocloud_metadata($uuid_str);
357 '/user-data' => $user_data,
358 '/network-config' => $network_data,
359 '/meta-data' => $meta_data
361 commit_cloudinit_disk($vmid, $conf, $drive, $volname, $storeid, $files, 'cidata');
364 my $cloudinit_methods = {
365 configdrive2 => \&generate_configdrive2,
366 nocloud => \&generate_nocloud,
369 sub generate_cloudinitconfig {
370 my ($conf, $vmid) = @_;
372 my $format = get_cloudinit_format($conf);
374 PVE::QemuServer::foreach_drive($conf, sub {
375 my ($ds, $drive) = @_;
377 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
379 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
381 my $generator = $cloudinit_methods->{$format}
382 or die "missing cloudinit methods
for format
'$format'\n";
384 $generator->($conf, $vmid, $drive, $volname, $storeid);