]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer/Cloudinit.pm
3cdf3a6bfeb3ebc06e76d4c9e57995952bf14529
[qemu-server.git] / PVE / QemuServer / Cloudinit.pm
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 configdrive2_network {
141 my ($conf) = @_;
142
143 my $content = "auto lo\n";
144 $content .= "iface lo inet loopback\n\n";
145
146 my ($searchdomains, $nameservers) = get_dns_conf($conf);
147 if ($nameservers && @$nameservers) {
148 $nameservers = join(' ', @$nameservers);
149 $content .= " dns_nameservers $nameservers\n";
150 }
151 if ($searchdomains && @$searchdomains) {
152 $searchdomains = join(' ', @$searchdomains);
153 $content .= " dns_search $searchdomains\n";
154 }
155
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"});
161 $id = "eth$id";
162
163 $content .="auto $id\n";
164 if ($net->{ip}) {
165 if ($net->{ip} eq 'dhcp') {
166 $content .= "iface $id inet dhcp\n";
167 } else {
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};
173 }
174 }
175 if ($net->{ip6}) {
176 if ($net->{ip6} =~ /^(auto|dhcp)$/) {
177 $content .= "iface $id inet6 $1\n";
178 } else {
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};
184 }
185 }
186 }
187
188 return $content;
189 }
190
191 sub configdrive2_metadata {
192 my ($uuid) = @_;
193 return <<"EOF";
194 {
195 "uuid": "$uuid",
196 "network_config": { "content_path": "/content/0000" }
197 }
198 EOF
199 }
200
201 sub generate_configdrive2 {
202 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
203
204 my $user_data = cloudinit_userdata($conf, $vmid);
205 my $network_data = configdrive2_network($conf);
206
207 my $digest_data = $user_data . $network_data;
208 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
209
210 my $meta_data = configdrive2_metadata($uuid_str);
211
212 my $files = {
213 '/openstack/latest/user_data' => $user_data,
214 '/openstack/content/0000' => $network_data,
215 '/openstack/latest/meta_data.json' => $meta_data
216 };
217 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
218 }
219
220 sub nocloud_network_v2 {
221 my ($conf) = @_;
222
223 my $content = '';
224
225 my $head = "version: 2\n"
226 . "ethernets:\n";
227
228 my $dns_done;
229
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"};
234
235 # indentation - network interfaces are inside an 'ethernets' hash
236 my $i = ' ';
237
238 my $net = PVE::QemuServer::parse_net($conf->{$iface});
239 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
240
241 my $mac = $net->{macaddr}
242 or die "network interface '$iface' has no mac address\n";
243
244 $content .= "${i}$iface:\n";
245 $i .= ' ';
246 $content .= "${i}match:\n"
247 . "${i} macaddress: \"$mac\"\n"
248 . "${i}set-name: eth$id\n";
249 my @addresses;
250 if (defined(my $ip = $ipconfig->{ip})) {
251 if ($ip eq 'dhcp') {
252 $content .= "${i}dhcp4: true\n";
253 } else {
254 push @addresses, $ip;
255 }
256 }
257 if (defined(my $ip = $ipconfig->{ip6})) {
258 if ($ip eq 'dhcp') {
259 $content .= "${i}dhcp6: true\n";
260 } else {
261 push @addresses, $ip;
262 }
263 }
264 if (@addresses) {
265 $content .= "${i}addresses:\n";
266 $content .= "${i}- $_\n" foreach @addresses;
267 }
268 if (defined(my $gw = $ipconfig->{gw})) {
269 $content .= "${i}gateway4: $gw\n";
270 }
271 if (defined(my $gw = $ipconfig->{gw6})) {
272 $content .= "${i}gateway6: $gw\n";
273 }
274
275 next if $dns_done;
276 $dns_done = 1;
277
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;
284 }
285 if (defined($searchdomains) && @$searchdomains) {
286 $content .= "${i} search:\n";
287 $content .= "${i} - $_\n" foreach @$searchdomains;
288 }
289 }
290 }
291
292 return $head.$content;
293 }
294
295 sub nocloud_network {
296 my ($conf) = @_;
297
298 my $content = "version: 1\n"
299 . "config:\n";
300
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"};
305
306 # indentation - network interfaces are inside an 'ethernets' hash
307 my $i = ' ';
308
309 my $net = PVE::QemuServer::parse_net($conf->{$iface});
310 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
311
312 my $mac = $net->{macaddr}
313 or die "network interface '$iface' has no mac address\n";
314
315 $content .= "${i}- type: physical\n"
316 . "${i} name: eth$id\n"
317 . "${i} mac_address: $mac\n"
318 . "${i} subnets:\n";
319 $i .= ' ';
320 if (defined(my $ip = $ipconfig->{ip})) {
321 if ($ip eq 'dhcp') {
322 $content .= "${i}- type: dhcp4\n";
323 } else {
324 $content .= "${i}- type: static\n"
325 . "${i} address: $ip\n";
326 if (defined(my $gw = $ipconfig->{gw})) {
327 $content .= "${i} gateway: $gw\n";
328 }
329 }
330 }
331 if (defined(my $ip = $ipconfig->{ip6})) {
332 if ($ip eq 'dhcp') {
333 $content .= "${i}- type: dhcp6\n";
334 } else {
335 $content .= "${i}- type: static6\n"
336 . "${i} address: $ip\n";
337 if (defined(my $gw = $ipconfig->{gw6})) {
338 $content .= "${i} gateway: $gw\n";
339 }
340 }
341 }
342 }
343
344 my $i = ' ';
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;
351 }
352 if (defined($searchdomains) && @$searchdomains) {
353 $content .= "${i} search:\n";
354 $content .= "${i} - $_\n" foreach @$searchdomains;
355 }
356 }
357
358 return $content;
359 }
360
361 sub nocloud_metadata {
362 my ($uuid) = @_;
363 return "instance-id: $uuid\n";
364 }
365
366 sub generate_nocloud {
367 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
368
369 my $user_data = cloudinit_userdata($conf, $vmid);
370 my $network_data = nocloud_network($conf);
371
372 my $digest_data = $user_data . $network_data;
373 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
374
375 my $meta_data = nocloud_metadata($uuid_str);
376
377 my $files = {
378 '/user-data' => $user_data,
379 '/network-config' => $network_data,
380 '/meta-data' => $meta_data
381 };
382 commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
383 }
384
385 my $cloudinit_methods = {
386 configdrive2 => \&generate_configdrive2,
387 nocloud => \&generate_nocloud,
388 };
389
390 sub generate_cloudinitconfig {
391 my ($conf, $vmid) = @_;
392
393 my $format = get_cloudinit_format($conf);
394
395 PVE::QemuServer::foreach_drive($conf, sub {
396 my ($ds, $drive) = @_;
397
398 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
399
400 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
401
402 my $generator = $cloudinit_methods->{$format}
403 or die "missing cloudinit methods for format '$format'\n";
404
405 $generator->($conf, $vmid, $drive, $volname, $storeid);
406 });
407 }
408
409 1;