]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuServer/Cloudinit.pm
31f8462089106fb92526f24448c80b0ba3720ae2
[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 ($vmid, $conf, $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 $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
34
35 my $size = PVE::Storage::file_size_info($iso_path);
36
37 eval {
38 run_command([['genisoimage', '-R', '-V', $label, $path],
39 ['qemu-img', 'dd', '-f', 'raw', '-O', $format,
40 'isize=0', "osize=$size", "of=$iso_path"]]);
41 };
42 my $err = $@;
43 rmtree($path);
44 die $err if $err;
45 }
46
47 sub get_cloudinit_format {
48 my ($conf) = @_;
49 if (defined(my $format = $conf->{citype})) {
50 return $format;
51 }
52
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})) {
58 return 'configdrive2'
59 if PVE::QemuServer::windows_version($ostype);
60 }
61
62 return 'nocloud';
63 }
64
65 sub get_hostname_fqdn {
66 my ($conf, $vmid) = @_;
67 my $hostname = $conf->{name} // "VM$vmid";
68 my $fqdn;
69 if ($hostname =~ /\./) {
70 $fqdn = $hostname;
71 $hostname =~ s/\..*$//;
72 } elsif (my $search = $conf->{searchdomain}) {
73 $fqdn = "$hostname.$search";
74 }
75 return ($hostname, $fqdn);
76 }
77
78 sub get_dns_conf {
79 my ($conf) = @_;
80
81 # Same logic as in pve-container, but without the testcase special case
82 my $host_resolv_conf = PVE::INotify::read_file('resolvconf');
83
84 my $searchdomains = [
85 split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search})
86 ];
87
88 my $nameserver = $conf->{nameserver};
89 if (!defined($nameserver)) {
90 $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
91 } else {
92 $nameserver = [split(/\s+/, $nameserver)];
93 }
94
95 return ($searchdomains, $nameserver);
96 }
97
98 sub cloudinit_userdata {
99 my ($conf, $vmid) = @_;
100
101 my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
102
103 my $content = "#cloud-config\n";
104
105 $content .= "hostname: $hostname\n";
106 $content .= "fqdn: $fqdn\n" if defined($fqdn);
107
108 my $username = $conf->{ciuser};
109 my $password = $conf->{cipassword};
110
111 $content .= "user: $username\n" if defined($username);
112 $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
113 $content .= "password: $password\n" if defined($password);
114
115 if (defined(my $keys = $conf->{sshkeys})) {
116 $keys = URI::Escape::uri_unescape($keys);
117 $keys = [map { chomp $_; $_ } split(/\n/, $keys)];
118 $keys = [grep { /\S/ } @$keys];
119 $content .= "ssh_authorized_keys:\n";
120 foreach my $k (@$keys) {
121 $content .= " - $k\n";
122 }
123 }
124 $content .= "chpasswd:\n";
125 $content .= " expire: False\n";
126
127 if (!defined($username) || $username ne 'root') {
128 $content .= "users:\n";
129 $content .= " - default\n";
130 }
131
132 $content .= "package_upgrade: true\n";
133
134 return $content;
135 }
136
137 sub configdrive2_network {
138 my ($conf) = @_;
139
140 my $content = "auto lo\n";
141 $content .= "iface lo inet loopback\n\n";
142
143 my ($searchdomains, $nameservers) = get_dns_conf($conf);
144 if ($nameservers && @$nameservers) {
145 $nameservers = join(' ', @$nameservers);
146 $content .= " dns_nameservers $nameservers\n";
147 }
148 if ($searchdomains && @$searchdomains) {
149 $searchdomains = join(' ', @$searchdomains);
150 $content .= " dns_search $searchdomains\n";
151 }
152
153 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
154 foreach my $iface (@ifaces) {
155 (my $id = $iface) =~ s/^net//;
156 next if !$conf->{"ipconfig$id"};
157 my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
158 $id = "eth$id";
159
160 $content .="auto $id\n";
161 if ($net->{ip}) {
162 if ($net->{ip} eq 'dhcp') {
163 $content .= "iface $id inet dhcp\n";
164 } else {
165 my ($addr, $mask) = split('/', $net->{ip});
166 $content .= "iface $id inet static\n";
167 $content .= " address $addr\n";
168 $content .= " netmask $PVE::Network::ipv4_reverse_mask->[$mask]\n";
169 $content .= " gateway $net->{gw}\n" if $net->{gw};
170 }
171 }
172 if ($net->{ip6}) {
173 if ($net->{ip6} =~ /^(auto|dhcp)$/) {
174 $content .= "iface $id inet6 $1\n";
175 } else {
176 my ($addr, $mask) = split('/', $net->{ip6});
177 $content .= "iface $id inet6 static\n";
178 $content .= " address $addr\n";
179 $content .= " netmask $mask\n";
180 $content .= " gateway $net->{gw6}\n" if $net->{gw6};
181 }
182 }
183 }
184
185 return $content;
186 }
187
188 sub configdrive2_metadata {
189 my ($uuid) = @_;
190 return <<"EOF";
191 {
192 "uuid": "$uuid",
193 "network_config": { "content_path": "/content/0000" }
194 }
195 EOF
196 }
197
198 sub generate_configdrive2 {
199 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
200
201 my $user_data = cloudinit_userdata($conf, $vmid);
202 my $network_data = configdrive2_network($conf);
203
204 my $digest_data = $user_data . $network_data;
205 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
206
207 my $meta_data = configdrive2_metadata($uuid_str);
208
209 my $files = {
210 '/openstack/latest/user_data' => $user_data,
211 '/openstack/content/0000' => $network_data,
212 '/openstack/latest/meta_data.json' => $meta_data
213 };
214 commit_cloudinit_disk($vmid, $conf, $drive, $volname, $storeid, $files, 'config-2');
215 }
216
217 sub nocloud_network_v2 {
218 my ($conf) = @_;
219
220 my $content = '';
221
222 my $head = "version: 2\n"
223 . "ethernets:\n";
224
225 my $dns_done;
226
227 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
228 foreach my $iface (@ifaces) {
229 (my $id = $iface) =~ s/^net//;
230 next if !$conf->{"ipconfig$id"};
231
232 # indentation - network interfaces are inside an 'ethernets' hash
233 my $i = ' ';
234
235 my $net = PVE::QemuServer::parse_net($conf->{$iface});
236 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
237
238 my $mac = $net->{macaddr}
239 or die "network interface '$iface' has no mac address\n";
240
241 $content .= "${i}$iface:\n";
242 $i .= ' ';
243 $content .= "${i}match:\n"
244 . "${i} macaddress: \"$mac\"\n"
245 . "${i}set-name: eth$id\n";
246 my @addresses;
247 if (defined(my $ip = $ipconfig->{ip})) {
248 if ($ip eq 'dhcp') {
249 $content .= "${i}dhcp4: true\n";
250 } else {
251 push @addresses, $ip;
252 }
253 }
254 if (defined(my $ip = $ipconfig->{ip6})) {
255 if ($ip eq 'dhcp') {
256 $content .= "${i}dhcp6: true\n";
257 } else {
258 push @addresses, $ip;
259 }
260 }
261 if (@addresses) {
262 $content .= "${i}addresses:\n";
263 $content .= "${i}- $_\n" foreach @addresses;
264 }
265 if (defined(my $gw = $ipconfig->{gw})) {
266 $content .= "${i}gateway4: $gw\n";
267 }
268 if (defined(my $gw = $ipconfig->{gw6})) {
269 $content .= "${i}gateway6: $gw\n";
270 }
271
272 next if $dns_done;
273 $dns_done = 1;
274
275 my ($searchdomains, $nameservers) = get_dns_conf($conf);
276 if ($searchdomains || $nameservers) {
277 $content .= "${i}nameservers:\n";
278 if (defined($nameservers) && @$nameservers) {
279 $content .= "${i} addresses:\n";
280 $content .= "${i} - $_\n" foreach @$nameservers;
281 }
282 if (defined($searchdomains) && @$searchdomains) {
283 $content .= "${i} search:\n";
284 $content .= "${i} - $_\n" foreach @$searchdomains;
285 }
286 }
287 }
288
289 return $head.$content;
290 }
291
292 sub nocloud_network {
293 my ($conf) = @_;
294
295 my $content = "version: 1\n"
296 . "config:\n";
297
298 my @ifaces = grep(/^net(\d+)$/, keys %$conf);
299 foreach my $iface (@ifaces) {
300 (my $id = $iface) =~ s/^net//;
301 next if !$conf->{"ipconfig$id"};
302
303 # indentation - network interfaces are inside an 'ethernets' hash
304 my $i = ' ';
305
306 my $net = PVE::QemuServer::parse_net($conf->{$iface});
307 my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
308
309 my $mac = $net->{macaddr}
310 or die "network interface '$iface' has no mac address\n";
311
312 $content .= "${i}- type: physical\n"
313 . "${i} name: eth$id\n"
314 . "${i} mac_address: $mac\n"
315 . "${i} subnets:\n";
316 $i .= ' ';
317 if (defined(my $ip = $ipconfig->{ip})) {
318 if ($ip eq 'dhcp') {
319 $content .= "${i}- type: dhcp4\n";
320 } else {
321 $content .= "${i}- type: static\n"
322 . "${i} address: $ip\n";
323 if (defined(my $gw = $ipconfig->{gw})) {
324 $content .= "${i} gateway: $gw\n";
325 }
326 }
327 }
328 if (defined(my $ip = $ipconfig->{ip6})) {
329 if ($ip eq 'dhcp') {
330 $content .= "${i}- type: dhcp6\n";
331 } else {
332 $content .= "${i}- type: static6\n"
333 . "${i} address: $ip\n";
334 if (defined(my $gw = $ipconfig->{gw6})) {
335 $content .= "${i} gateway: $gw\n";
336 }
337 }
338 }
339 }
340
341 my $i = ' ';
342 my ($searchdomains, $nameservers) = get_dns_conf($conf);
343 if ($searchdomains || $nameservers) {
344 $content .= "${i}- type: nameserver\n";
345 if (defined($nameservers) && @$nameservers) {
346 $content .= "${i} address:\n";
347 $content .= "${i} - $_\n" foreach @$nameservers;
348 }
349 if (defined($searchdomains) && @$searchdomains) {
350 $content .= "${i} search:\n";
351 $content .= "${i} - $_\n" foreach @$searchdomains;
352 }
353 }
354
355 return $content;
356 }
357
358 sub nocloud_metadata {
359 my ($uuid) = @_;
360 return "instance-id: $uuid\n";
361 }
362
363 sub generate_nocloud {
364 my ($conf, $vmid, $drive, $volname, $storeid) = @_;
365
366 my $user_data = cloudinit_userdata($conf, $vmid);
367 my $network_data = nocloud_network($conf);
368
369 my $digest_data = $user_data . $network_data;
370 my $uuid_str = Digest::SHA::sha1_hex($digest_data);
371
372 my $meta_data = nocloud_metadata($uuid_str);
373
374 my $files = {
375 '/user-data' => $user_data,
376 '/network-config' => $network_data,
377 '/meta-data' => $meta_data
378 };
379 commit_cloudinit_disk($vmid, $conf, $drive, $volname, $storeid, $files, 'cidata');
380 }
381
382 my $cloudinit_methods = {
383 configdrive2 => \&generate_configdrive2,
384 nocloud => \&generate_nocloud,
385 };
386
387 sub generate_cloudinitconfig {
388 my ($conf, $vmid) = @_;
389
390 my $format = get_cloudinit_format($conf);
391
392 PVE::QemuServer::foreach_drive($conf, sub {
393 my ($ds, $drive) = @_;
394
395 my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
396
397 return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
398
399 my $generator = $cloudinit_methods->{$format}
400 or die "missing cloudinit methods for format '$format'\n";
401
402 $generator->($conf, $vmid, $drive, $volname, $storeid);
403 });
404 }
405
406 1;