]>
Commit | Line | Data |
---|---|---|
1 | package PVE::QemuServer::Cloudinit; | |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
6 | use File::Path; | |
7 | use Digest::SHA; | |
8 | use URI::Escape; | |
9 | ||
10 | use PVE::Tools qw(run_command file_set_contents); | |
11 | use PVE::Storage; | |
12 | use PVE::QemuServer; | |
13 | ||
14 | sub commit_cloudinit_disk { | |
15 | my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_; | |
16 | ||
17 | my $path = "/run/pve/cloudinit/$vmid/"; | |
18 | mkpath $path; | |
19 | foreach my $filepath (keys %$files) { | |
20 | if ($filepath !~ m@^(.*)\/[^/]+$@) { | |
21 | die "internal error: bad file name in cloud-init image: $filepath\n"; | |
22 | } | |
23 | my $dirname = $1; | |
24 | mkpath "$path/$dirname"; | |
25 | ||
26 | my $contents = $files->{$filepath}; | |
27 | file_set_contents("$path/$filepath", $contents); | |
28 | } | |
29 | ||
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); | |
36 | ||
37 | my $size = PVE::Storage::file_size_info($iso_path); | |
38 | ||
39 | eval { | |
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"]]); | |
43 | }; | |
44 | my $err = $@; | |
45 | rmtree($path); | |
46 | die $err if $err; | |
47 | } | |
48 | ||
49 | sub get_cloudinit_format { | |
50 | my ($conf) = @_; | |
51 | if (defined(my $format = $conf->{citype})) { | |
52 | return $format; | |
53 | } | |
54 | ||
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})) { | |
60 | return 'configdrive2' | |
61 | if PVE::QemuServer::windows_version($ostype); | |
62 | } | |
63 | ||
64 | return 'nocloud'; | |
65 | } | |
66 | ||
67 | sub get_hostname_fqdn { | |
68 | my ($conf, $vmid) = @_; | |
69 | my $hostname = $conf->{name} // "VM$vmid"; | |
70 | my $fqdn; | |
71 | if ($hostname =~ /\./) { | |
72 | $fqdn = $hostname; | |
73 | $hostname =~ s/\..*$//; | |
74 | } elsif (my $search = $conf->{searchdomain}) { | |
75 | $fqdn = "$hostname.$search"; | |
76 | } | |
77 | return ($hostname, $fqdn); | |
78 | } | |
79 | ||
80 | sub get_dns_conf { | |
81 | my ($conf) = @_; | |
82 | ||
83 | # Same logic as in pve-container, but without the testcase special case | |
84 | my $host_resolv_conf = PVE::INotify::read_file('resolvconf'); | |
85 | ||
86 | my $searchdomains = [ | |
87 | split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search}) | |
88 | ]; | |
89 | ||
90 | my $nameserver = $conf->{nameserver}; | |
91 | if (!defined($nameserver)) { | |
92 | $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}]; | |
93 | } else { | |
94 | $nameserver = [split(/\s+/, $nameserver)]; | |
95 | } | |
96 | ||
97 | return ($searchdomains, $nameserver); | |
98 | } | |
99 | ||
100 | sub cloudinit_userdata { | |
101 | my ($conf, $vmid) = @_; | |
102 | ||
103 | my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid); | |
104 | ||
105 | my $content = "#cloud-config\n"; | |
106 | ||
107 | $content .= "hostname: $hostname\n"; | |
108 | $content .= "manage_etc_hosts: true\n"; | |
109 | $content .= "fqdn: $fqdn\n" if defined($fqdn); | |
110 | ||
111 | my $username = $conf->{ciuser}; | |
112 | my $password = $conf->{cipassword}; | |
113 | ||
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); | |
117 | ||
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"; | |
125 | } | |
126 | } | |
127 | $content .= "chpasswd:\n"; | |
128 | $content .= " expire: False\n"; | |
129 | ||
130 | if (!defined($username) || $username ne 'root') { | |
131 | $content .= "users:\n"; | |
132 | $content .= " - default\n"; | |
133 | } | |
134 | ||
135 | $content .= "package_upgrade: true\n"; | |
136 | ||
137 | return $content; | |
138 | } | |
139 | ||
140 | sub split_ip4 { | |
141 | my ($ip) = @_; | |
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]); | |
145 | } | |
146 | ||
147 | sub configdrive2_network { | |
148 | my ($conf) = @_; | |
149 | ||
150 | my $content = "auto lo\n"; | |
151 | $content .= "iface lo inet loopback\n\n"; | |
152 | ||
153 | my ($searchdomains, $nameservers) = get_dns_conf($conf); | |
154 | if ($nameservers && @$nameservers) { | |
155 | $nameservers = join(' ', @$nameservers); | |
156 | $content .= " dns_nameservers $nameservers\n"; | |
157 | } | |
158 | if ($searchdomains && @$searchdomains) { | |
159 | $searchdomains = join(' ', @$searchdomains); | |
160 | $content .= " dns_search $searchdomains\n"; | |
161 | } | |
162 | ||
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"}); | |
168 | $id = "eth$id"; | |
169 | ||
170 | $content .="auto $id\n"; | |
171 | if ($net->{ip}) { | |
172 | if ($net->{ip} eq 'dhcp') { | |
173 | $content .= "iface $id inet dhcp\n"; | |
174 | } else { | |
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}; | |
180 | } | |
181 | } | |
182 | if ($net->{ip6}) { | |
183 | if ($net->{ip6} =~ /^(auto|dhcp)$/) { | |
184 | $content .= "iface $id inet6 $1\n"; | |
185 | } else { | |
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}; | |
191 | } | |
192 | } | |
193 | } | |
194 | ||
195 | return $content; | |
196 | } | |
197 | ||
198 | sub configdrive2_metadata { | |
199 | my ($uuid) = @_; | |
200 | return <<"EOF"; | |
201 | { | |
202 | "uuid": "$uuid", | |
203 | "network_config": { "content_path": "/content/0000" } | |
204 | } | |
205 | EOF | |
206 | } | |
207 | ||
208 | sub generate_configdrive2 { | |
209 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; | |
210 | ||
211 | my $user_data = cloudinit_userdata($conf, $vmid); | |
212 | my $network_data = configdrive2_network($conf); | |
213 | ||
214 | my $digest_data = $user_data . $network_data; | |
215 | my $uuid_str = Digest::SHA::sha1_hex($digest_data); | |
216 | ||
217 | my $meta_data = configdrive2_metadata($uuid_str); | |
218 | ||
219 | my $files = { | |
220 | '/openstack/latest/user_data' => $user_data, | |
221 | '/openstack/content/0000' => $network_data, | |
222 | '/openstack/latest/meta_data.json' => $meta_data | |
223 | }; | |
224 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2'); | |
225 | } | |
226 | ||
227 | sub nocloud_network_v2 { | |
228 | my ($conf) = @_; | |
229 | ||
230 | my $content = ''; | |
231 | ||
232 | my $head = "version: 2\n" | |
233 | . "ethernets:\n"; | |
234 | ||
235 | my $dns_done; | |
236 | ||
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"}; | |
241 | ||
242 | # indentation - network interfaces are inside an 'ethernets' hash | |
243 | my $i = ' '; | |
244 | ||
245 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); | |
246 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); | |
247 | ||
248 | my $mac = $net->{macaddr} | |
249 | or die "network interface '$iface' has no mac address\n"; | |
250 | ||
251 | $content .= "${i}$iface:\n"; | |
252 | $i .= ' '; | |
253 | $content .= "${i}match:\n" | |
254 | . "${i} macaddress: \"$mac\"\n" | |
255 | . "${i}set-name: eth$id\n"; | |
256 | my @addresses; | |
257 | if (defined(my $ip = $ipconfig->{ip})) { | |
258 | if ($ip eq 'dhcp') { | |
259 | $content .= "${i}dhcp4: true\n"; | |
260 | } else { | |
261 | push @addresses, $ip; | |
262 | } | |
263 | } | |
264 | if (defined(my $ip = $ipconfig->{ip6})) { | |
265 | if ($ip eq 'dhcp') { | |
266 | $content .= "${i}dhcp6: true\n"; | |
267 | } else { | |
268 | push @addresses, $ip; | |
269 | } | |
270 | } | |
271 | if (@addresses) { | |
272 | $content .= "${i}addresses:\n"; | |
273 | $content .= "${i}- $_\n" foreach @addresses; | |
274 | } | |
275 | if (defined(my $gw = $ipconfig->{gw})) { | |
276 | $content .= "${i}gateway4: $gw\n"; | |
277 | } | |
278 | if (defined(my $gw = $ipconfig->{gw6})) { | |
279 | $content .= "${i}gateway6: $gw\n"; | |
280 | } | |
281 | ||
282 | next if $dns_done; | |
283 | $dns_done = 1; | |
284 | ||
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; | |
291 | } | |
292 | if (defined($searchdomains) && @$searchdomains) { | |
293 | $content .= "${i} search:\n"; | |
294 | $content .= "${i} - $_\n" foreach @$searchdomains; | |
295 | } | |
296 | } | |
297 | } | |
298 | ||
299 | return $head.$content; | |
300 | } | |
301 | ||
302 | sub nocloud_network { | |
303 | my ($conf) = @_; | |
304 | ||
305 | my $content = "version: 1\n" | |
306 | . "config:\n"; | |
307 | ||
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"}; | |
312 | ||
313 | # indentation - network interfaces are inside an 'ethernets' hash | |
314 | my $i = ' '; | |
315 | ||
316 | my $net = PVE::QemuServer::parse_net($conf->{$iface}); | |
317 | my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"}); | |
318 | ||
319 | my $mac = lc($net->{macaddr}) | |
320 | or die "network interface '$iface' has no mac address\n"; | |
321 | ||
322 | $content .= "${i}- type: physical\n" | |
323 | . "${i} name: eth$id\n" | |
324 | . "${i} mac_address: $mac\n" | |
325 | . "${i} subnets:\n"; | |
326 | $i .= ' '; | |
327 | if (defined(my $ip = $ipconfig->{ip})) { | |
328 | if ($ip eq 'dhcp') { | |
329 | $content .= "${i}- type: dhcp4\n"; | |
330 | } else { | |
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"; | |
337 | } | |
338 | } | |
339 | } | |
340 | if (defined(my $ip = $ipconfig->{ip6})) { | |
341 | if ($ip eq 'dhcp') { | |
342 | $content .= "${i}- type: dhcp6\n"; | |
343 | } else { | |
344 | $content .= "${i}- type: static\n" | |
345 | . "${i} address: $ip\n"; | |
346 | if (defined(my $gw = $ipconfig->{gw6})) { | |
347 | $content .= "${i} gateway: $gw\n"; | |
348 | } | |
349 | } | |
350 | } | |
351 | } | |
352 | ||
353 | my $i = ' '; | |
354 | my ($searchdomains, $nameservers) = get_dns_conf($conf); | |
355 | if ($searchdomains || $nameservers) { | |
356 | $content .= "${i}- type: nameserver\n"; | |
357 | if (defined($nameservers) && @$nameservers) { | |
358 | $content .= "${i} address:\n"; | |
359 | $content .= "${i} - $_\n" foreach @$nameservers; | |
360 | } | |
361 | if (defined($searchdomains) && @$searchdomains) { | |
362 | $content .= "${i} search:\n"; | |
363 | $content .= "${i} - $_\n" foreach @$searchdomains; | |
364 | } | |
365 | } | |
366 | ||
367 | return $content; | |
368 | } | |
369 | ||
370 | sub nocloud_metadata { | |
371 | my ($uuid) = @_; | |
372 | return "instance-id: $uuid\n"; | |
373 | } | |
374 | ||
375 | sub generate_nocloud { | |
376 | my ($conf, $vmid, $drive, $volname, $storeid) = @_; | |
377 | ||
378 | my $user_data = cloudinit_userdata($conf, $vmid); | |
379 | my $network_data = nocloud_network($conf); | |
380 | ||
381 | my $digest_data = $user_data . $network_data; | |
382 | my $uuid_str = Digest::SHA::sha1_hex($digest_data); | |
383 | ||
384 | my $meta_data = nocloud_metadata($uuid_str); | |
385 | ||
386 | my $files = { | |
387 | '/user-data' => $user_data, | |
388 | '/network-config' => $network_data, | |
389 | '/meta-data' => $meta_data | |
390 | }; | |
391 | commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata'); | |
392 | } | |
393 | ||
394 | my $cloudinit_methods = { | |
395 | configdrive2 => \&generate_configdrive2, | |
396 | nocloud => \&generate_nocloud, | |
397 | }; | |
398 | ||
399 | sub generate_cloudinitconfig { | |
400 | my ($conf, $vmid) = @_; | |
401 | ||
402 | my $format = get_cloudinit_format($conf); | |
403 | ||
404 | PVE::QemuServer::foreach_drive($conf, sub { | |
405 | my ($ds, $drive) = @_; | |
406 | ||
407 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1); | |
408 | ||
409 | return if !$volname || $volname !~ m/vm-$vmid-cloudinit/; | |
410 | ||
411 | my $generator = $cloudinit_methods->{$format} | |
412 | or die "missing cloudinit methods for format '$format'\n"; | |
413 | ||
414 | $generator->($conf, $vmid, $drive, $volname, $storeid); | |
415 | }); | |
416 | } | |
417 | ||
418 | 1; |