Guest User

Untitled

a guest
Feb 22nd, 2022
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 20.87 KB | None | 0 0
  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. use MIME::Base64 qw(encode_base64);
  10.  
  11. use PVE::Tools qw(run_command file_set_contents);
  12. use PVE::Storage;
  13. use PVE::QemuServer;
  14.  
  15. use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes
  16.  
  17. sub commit_cloudinit_disk {
  18. my ($conf, $vmid, $drive, $volname, $storeid, $files, $label) = @_;
  19.  
  20. my $path = "/run/pve/cloudinit/$vmid/";
  21. mkpath $path;
  22. foreach my $filepath (keys %$files) {
  23. if ($filepath !~ m@^(.*)\/[^/]+$@) {
  24. die "internal error: bad file name in cloud-init image: $filepath\n";
  25. }
  26. my $dirname = $1;
  27. mkpath "$path/$dirname";
  28.  
  29. my $contents = $files->{$filepath};
  30. file_set_contents("$path/$filepath", $contents);
  31. }
  32.  
  33. my $storecfg = PVE::Storage::config();
  34. my $iso_path = PVE::Storage::path($storecfg, $drive->{file});
  35. my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
  36. my $format = PVE::QemuServer::qemu_img_format($scfg, $volname);
  37.  
  38. my $size = eval { PVE::Storage::volume_size_info($storecfg, $drive->{file}) };
  39. if (!defined($size) || $size <= 0) {
  40. $volname =~ m/(vm-$vmid-cloudinit(.\Q$format\E)?)/;
  41. my $name = $1;
  42. $size = 4 * 1024;
  43. PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, $format, $name, $size);
  44. $size *= 1024; # vdisk alloc takes KB, qemu-img dd's osize takes byte
  45. }
  46. my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
  47. $plugin->activate_volume($storeid, $scfg, $volname);
  48.  
  49. print "generating cloud-init ISO\n";
  50. eval {
  51. run_command([
  52. ['genisoimage', '-quiet', '-iso-level', '3', '-R', '-V', $label, $path],
  53. ['qemu-img', 'dd', '-n', '-f', 'raw', '-O', $format, 'isize=0', "osize=$size", "of=$iso_path"]
  54. ]);
  55. };
  56. my $err = $@;
  57. rmtree($path);
  58. die $err if $err;
  59. }
  60.  
  61. sub get_cloudinit_format {
  62. my ($conf) = @_;
  63. if (defined(my $format = $conf->{citype})) {
  64. return $format;
  65. }
  66.  
  67. # No format specified, default based on ostype because windows'
  68. # cloudbased-init only supports configdrivev2, whereas on linux we need
  69. # to use mac addresses because regular cloudinit doesn't map 'ethX' to
  70. # the new predicatble network device naming scheme.
  71. if (defined(my $ostype = $conf->{ostype})) {
  72. return 'configdrive2'
  73. if PVE::QemuServer::windows_version($ostype);
  74. }
  75.  
  76. return 'nocloud';
  77. }
  78.  
  79. sub get_hostname_fqdn {
  80. my ($conf, $vmid) = @_;
  81. my $hostname = $conf->{name} // "VM$vmid";
  82. my $fqdn;
  83. if ($hostname =~ /\./) {
  84. $fqdn = $hostname;
  85. $hostname =~ s/\..*$//;
  86. } elsif (my $search = $conf->{searchdomain}) {
  87. $fqdn = "$hostname.$search";
  88. }
  89. return ($hostname, $fqdn);
  90. }
  91.  
  92. sub get_dns_conf {
  93. my ($conf) = @_;
  94.  
  95. # Same logic as in pve-container, but without the testcase special case
  96. my $host_resolv_conf = PVE::INotify::read_file('resolvconf');
  97.  
  98. my $searchdomains = [
  99. split(/\s+/, $conf->{searchdomain} // $host_resolv_conf->{search})
  100. ];
  101.  
  102. my $nameserver = $conf->{nameserver};
  103. if (!defined($nameserver)) {
  104. $nameserver = [grep { $_ } $host_resolv_conf->@{qw(dns1 dns2 dns3)}];
  105. } else {
  106. $nameserver = [split(/\s+/, $nameserver)];
  107. }
  108.  
  109. return ($searchdomains, $nameserver);
  110. }
  111.  
  112. sub cloudinit_userdata {
  113. my ($conf, $vmid) = @_;
  114.  
  115. my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
  116.  
  117. my $content = "#cloud-config\n";
  118.  
  119. $content .= "hostname: $hostname\n";
  120. $content .= "manage_etc_hosts: true\n";
  121. $content .= "fqdn: $fqdn\n" if defined($fqdn);
  122.  
  123. my $username = $conf->{ciuser};
  124. my $password = $conf->{cipassword};
  125.  
  126. $content .= "user: $username\n" if defined($username);
  127. $content .= "disable_root: False\n" if defined($username) && $username eq 'root';
  128. $content .= "password: $password\n" if defined($password);
  129.  
  130. if (defined(my $keys = $conf->{sshkeys})) {
  131. $keys = URI::Escape::uri_unescape($keys);
  132. $keys = [map { my $key = $_; chomp $key; $key } split(/\n/, $keys)];
  133. $keys = [grep { /\S/ } @$keys];
  134. $content .= "ssh_authorized_keys:\n";
  135. foreach my $k (@$keys) {
  136. $content .= " - $k\n";
  137. }
  138. }
  139. $content .= "chpasswd:\n";
  140. $content .= " expire: False\n";
  141.  
  142. if (!defined($username) || $username ne 'root') {
  143. $content .= "users:\n";
  144. $content .= " - default\n";
  145. }
  146.  
  147. $content .= "package_upgrade: true\n";
  148.  
  149. return $content;
  150. }
  151.  
  152. sub split_ip4 {
  153. my ($ip) = @_;
  154. my ($addr, $mask) = split('/', $ip);
  155. die "not a CIDR: $ip\n" if !defined $mask;
  156. return ($addr, $PVE::Network::ipv4_reverse_mask->[$mask]);
  157. }
  158.  
  159. sub configdrive2_network {
  160. my ($conf) = @_;
  161.  
  162. my $content = "auto lo\n";
  163. $content .= "iface lo inet loopback\n\n";
  164.  
  165. my ($searchdomains, $nameservers) = get_dns_conf($conf);
  166.  
  167. ## support windows
  168. my $ostype = $conf->{"ostype"};
  169. my $default_dns = '';
  170. my $default_search = '';
  171. ##
  172. my $dnsinserted = 0; # insert dns just once for the machine
  173.  
  174. if ($nameservers && @$nameservers) {
  175. $nameservers = join(' ', @$nameservers);
  176. $content .= " dns_nameservers $nameservers\n";
  177. $default_dns = $nameservers; # Support windows
  178. }
  179. if ($searchdomains && @$searchdomains) {
  180. $searchdomains = join(' ', @$searchdomains);
  181. $content .= " dns_search $searchdomains\n";
  182. $default_search = $searchdomains; # Support Windows
  183. }
  184.  
  185. my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
  186. foreach my $iface (sort @ifaces) {
  187. (my $id = $iface) =~ s/^net//;
  188. next if !$conf->{"ipconfig$id"};
  189. my $net = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
  190. $id = "eth$id";
  191.  
  192. $content .="auto $id\n";
  193. if ($net->{ip}) {
  194. if ($net->{ip} eq 'dhcp') {
  195. $content .= "iface $id inet dhcp\n";
  196. } else {
  197. my ($addr, $mask) = split_ip4($net->{ip});
  198. $content .= "iface $id inet static\n";
  199. $content .= " address $addr\n";
  200. $content .= " netmask $mask\n";
  201. $content .= " gateway $net->{gw}\n" if $net->{gw};
  202. ## Support Windows
  203. if(PVE::QemuServer::windows_version($ostype) && not($dnsinserted)) {
  204. $content .= " dns-nameservers $default_dns\n";
  205. $content .= " dns-search $default_search\n";
  206. $dnsinserted++;
  207. }
  208. ##
  209. }
  210. }
  211. if ($net->{ip6}) {
  212. if ($net->{ip6} =~ /^(auto|dhcp)$/) {
  213. $content .= "iface $id inet6 $1\n";
  214. } else {
  215. my ($addr, $mask) = split('/', $net->{ip6});
  216. $content .= "iface $id inet6 static\n";
  217. $content .= " address $addr\n";
  218. $content .= " netmask $mask\n";
  219. $content .= " gateway $net->{gw6}\n" if $net->{gw6};
  220. }
  221. }
  222. }
  223.  
  224. return $content;
  225. }
  226.  
  227. # Get mac addresses of dhcp nics from conf file
  228. sub get_mac_addresses {
  229. my ($conf) = @_;
  230.  
  231. my $dhcpstring = undef;
  232. my @dhcpmacs = ();
  233. my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
  234.  
  235. foreach my $iface (sort @ifaces) {
  236. (my $id = $iface) =~ s/^net//;
  237. my $net = PVE::QemuServer::parse_net($conf->{$iface});
  238. next if !$conf->{"ipconfig$id"};
  239. my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
  240.  
  241. my $mac = lc $net->{macaddr};
  242.  
  243. if (($ipconfig->{ip}) and ($ipconfig->{ip} eq 'dhcp')){
  244. push @dhcpmacs, $mac;
  245. }
  246. }
  247.  
  248. if (@dhcpmacs){
  249. $dhcpstring = ",\n \"dhcp\":[";
  250. foreach my $mac (@dhcpmacs){
  251. if ($mac != $dhcpmacs[-1]){
  252. $dhcpstring .= "\"$mac\",";
  253. }
  254. else{
  255. $dhcpstring .= "\"$mac\"]";
  256. }
  257. }
  258. }
  259. return ($dhcpstring);
  260. }
  261.  
  262. sub configdrive2_gen_metadata {
  263. my ($conf, $vmid, $user, $network) = @_;
  264.  
  265. # Get mac addresses of dhcp nics from conf file
  266. my $dhcpmacs = undef;
  267. $dhcpmacs = get_mac_addresses($conf);
  268.  
  269. # Get UUID
  270. my $uuid_str = Digest::SHA::sha1_hex($user.$network);
  271.  
  272. # Get hostname
  273. my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
  274.  
  275. # Get username, default to Administrator if none
  276. my $username = undef;
  277. if (defined($conf->{ciuser})){
  278. my $name = $conf->{ciuser};
  279. $username = ",\n \"admin_username\": \"$name\""
  280. }
  281.  
  282. # Get user password
  283. my $password = $conf->{cipassword};
  284.  
  285. # Get ssh keys and make a list out of it in json format
  286. my $keystring = undef;
  287. my $pubkeys = $conf->{sshkeys};
  288. $pubkeys = URI::Escape::uri_unescape($pubkeys);
  289. my @pubkeysarray = split "\n", $pubkeys;
  290. if (@pubkeysarray) {
  291. my $arraylength = @pubkeysarray;
  292. my $incrementer = 1;
  293. $keystring =",\n \"public_keys\": {\n";
  294. for my $key (@pubkeysarray){
  295. $keystring .= " \"SSH${incrementer}\" : \"${key}\"";
  296. if ($arraylength != $incrementer){
  297. $keystring .= ",\n";
  298. }else{
  299. $keystring .= "\n }";
  300. }
  301. $incrementer++;
  302. }
  303. }
  304.  
  305. return configdrive2_metadata($password, $uuid_str, $hostname, $username, $keystring, $network, $dhcpmacs);
  306. }
  307.  
  308. sub configdrive2_metadata {
  309. my ($password, $uuid, $hostname, $username, $pubkeys, $network, $dhcpmacs) = @_;
  310. return <<"EOF";
  311. {
  312. "meta":{
  313. "admin_pass": "$password"$username
  314. },
  315. "uuid":"$uuid",
  316. "hostname":"$hostname",
  317. "network_config":{"content_path":"/content/0000"}$pubkeys$dhcpmacs
  318. }
  319. EOF
  320. }
  321.  
  322.  
  323. sub generate_configdrive2 {
  324. my ($conf, $vmid, $drive, $volname, $storeid) = @_;
  325.  
  326. my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf);
  327. $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
  328. $network_data = configdrive2_network($conf) if !defined($network_data);
  329.  
  330. if (!defined($meta_data)) {
  331. $meta_data = configdrive2_gen_metadata($conf, $vmid, $user_data, $network_data);
  332. }
  333.  
  334. # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO
  335. # make sure we always stay below it by keeping the sum of all files below 3 MiB
  336. my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data);
  337. die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024);
  338.  
  339. my $files = {
  340. '/openstack/latest/user_data' => $user_data,
  341. '/openstack/content/0000' => $network_data,
  342. '/openstack/latest/meta_data.json' => $meta_data,
  343. '/openstack/latest/vendor_data.json' => $vendor_data
  344. };
  345. commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'config-2');
  346. }
  347.  
  348. sub generate_opennebula {
  349. my ($conf, $vmid, $drive, $volname, $storeid) = @_;
  350.  
  351. my $content = "";
  352.  
  353. my $username = $conf->{ciuser} || "root";
  354. $content .= "USERNAME=$username\n" if defined($username);
  355.  
  356. if (defined(my $password = $conf->{cipassword})) {
  357. $content .= "CRYPTED_PASSWORD_BASE64=". encode_base64($password) ."\n";
  358. }
  359.  
  360. if (defined($conf->{sshkeys})) {
  361. my $keys = [ split(/\s*\n\s*/, URI::Escape::uri_unescape($conf->{sshkeys})) ];
  362. $content .= "SSH_PUBLIC_KEY=\"". join("\n", $keys->@*) ."\"\n";
  363. }
  364.  
  365. my ($hostname, $fqdn) = get_hostname_fqdn($conf, $vmid);
  366. $content .= "SET_HOSTNAME=$hostname\n";
  367.  
  368. my ($searchdomains, $nameservers) = get_dns_conf($conf);
  369. $content .= 'DNS="' . join(' ', @$nameservers) ."\"\n" if $nameservers && @$nameservers;
  370. $content .= 'SEARCH_DOMAIN="'. join(' ', @$searchdomains) ."\"\n" if $searchdomains && @$searchdomains;
  371.  
  372. my $networkenabled = undef;
  373. my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
  374. foreach my $iface (sort @ifaces) {
  375. (my $id = $iface) =~ s/^net//;
  376. my $net = PVE::QemuServer::parse_net($conf->{$iface});
  377. next if !$conf->{"ipconfig$id"};
  378. my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
  379. my $ethid = "ETH$id";
  380.  
  381. my $mac = lc $net->{hwaddr};
  382.  
  383. if ($ipconfig->{ip}) {
  384. $networkenabled = 1;
  385.  
  386. if ($ipconfig->{ip} eq 'dhcp') {
  387. $content .= "${ethid}_DHCP=YES\n";
  388. } else {
  389. my ($addr, $mask) = split_ip4($ipconfig->{ip});
  390. $content .= "${ethid}_IP=$addr\n";
  391. $content .= "${ethid}_MASK=$mask\n";
  392. $content .= "${ethid}_MAC=$mac\n";
  393. $content .= "${ethid}_GATEWAY=$ipconfig->{gw}\n" if $ipconfig->{gw};
  394. }
  395. $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu};
  396. }
  397.  
  398. if ($ipconfig->{ip6}) {
  399. $networkenabled = 1;
  400. if ($ipconfig->{ip6} eq 'dhcp') {
  401. $content .= "${ethid}_DHCP6=YES\n";
  402. } elsif ($ipconfig->{ip6} eq 'auto') {
  403. $content .= "${ethid}_AUTO6=YES\n";
  404. } else {
  405. my ($addr, $mask) = split('/', $ipconfig->{ip6});
  406. $content .= "${ethid}_IP6=$addr\n";
  407. $content .= "${ethid}_MASK6=$mask\n";
  408. $content .= "${ethid}_MAC6=$mac\n";
  409. $content .= "${ethid}_GATEWAY6=$ipconfig->{gw6}\n" if $ipconfig->{gw6};
  410. }
  411. $content .= "${ethid}_MTU=$net->{mtu}\n" if $net->{mtu};
  412. }
  413. }
  414.  
  415. $content .= "NETWORK=YES\n" if $networkenabled;
  416.  
  417. my $files = { '/context.sh' => $content };
  418. commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'CONTEXT');
  419. }
  420.  
  421. sub nocloud_network_v2 {
  422. my ($conf) = @_;
  423.  
  424. my $content = '';
  425.  
  426. my $head = "version: 2\n"
  427. . "ethernets:\n";
  428.  
  429. my $dns_done;
  430.  
  431. my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
  432. foreach my $iface (sort @ifaces) {
  433. (my $id = $iface) =~ s/^net//;
  434. next if !$conf->{"ipconfig$id"};
  435.  
  436. # indentation - network interfaces are inside an 'ethernets' hash
  437. my $i = ' ';
  438.  
  439. my $net = PVE::QemuServer::parse_net($conf->{$iface});
  440. my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
  441.  
  442. my $mac = $net->{macaddr}
  443. or die "network interface '$iface' has no mac address\n";
  444.  
  445. $content .= "${i}$iface:\n";
  446. $i .= ' ';
  447. $content .= "${i}match:\n"
  448. . "${i} macaddress: \"$mac\"\n"
  449. . "${i}set-name: eth$id\n";
  450. my @addresses;
  451. if (defined(my $ip = $ipconfig->{ip})) {
  452. if ($ip eq 'dhcp') {
  453. $content .= "${i}dhcp4: true\n";
  454. } else {
  455. push @addresses, $ip;
  456. }
  457. }
  458. if (defined(my $ip = $ipconfig->{ip6})) {
  459. if ($ip eq 'dhcp') {
  460. $content .= "${i}dhcp6: true\n";
  461. } else {
  462. push @addresses, $ip;
  463. }
  464. }
  465. if (@addresses) {
  466. $content .= "${i}addresses:\n";
  467. $content .= "${i}- '$_'\n" foreach @addresses;
  468. }
  469. if (defined(my $gw = $ipconfig->{gw})) {
  470. $content .= "${i}gateway4: '$gw'\n";
  471. }
  472. if (defined(my $gw = $ipconfig->{gw6})) {
  473. $content .= "${i}gateway6: '$gw'\n";
  474. }
  475.  
  476. next if $dns_done;
  477. $dns_done = 1;
  478.  
  479. my ($searchdomains, $nameservers) = get_dns_conf($conf);
  480. if ($searchdomains || $nameservers) {
  481. $content .= "${i}nameservers:\n";
  482. if (defined($nameservers) && @$nameservers) {
  483. $content .= "${i} addresses:\n";
  484. $content .= "${i} - '$_'\n" foreach @$nameservers;
  485. }
  486. if (defined($searchdomains) && @$searchdomains) {
  487. $content .= "${i} search:\n";
  488. $content .= "${i} - '$_'\n" foreach @$searchdomains;
  489. }
  490. }
  491. }
  492.  
  493. return $head.$content;
  494. }
  495.  
  496. sub nocloud_network {
  497. my ($conf) = @_;
  498.  
  499. my $content = "version: 1\n"
  500. . "config:\n";
  501.  
  502. my @ifaces = grep { /^net(\d+)$/ } keys %$conf;
  503. foreach my $iface (sort @ifaces) {
  504. (my $id = $iface) =~ s/^net//;
  505. next if !$conf->{"ipconfig$id"};
  506.  
  507. # indentation - network interfaces are inside an 'ethernets' hash
  508. my $i = ' ';
  509.  
  510. my $net = PVE::QemuServer::parse_net($conf->{$iface});
  511. my $ipconfig = PVE::QemuServer::parse_ipconfig($conf->{"ipconfig$id"});
  512.  
  513. my $mac = lc($net->{macaddr})
  514. or die "network interface '$iface' has no mac address\n";
  515.  
  516. $content .= "${i}- type: physical\n"
  517. . "${i} name: eth$id\n"
  518. . "${i} mac_address: '$mac'\n"
  519. . "${i} subnets:\n";
  520. $i .= ' ';
  521. if (defined(my $ip = $ipconfig->{ip})) {
  522. if ($ip eq 'dhcp') {
  523. $content .= "${i}- type: dhcp4\n";
  524. } else {
  525. my ($addr, $mask) = split_ip4($ip);
  526. $content .= "${i}- type: static\n"
  527. . "${i} address: '$addr'\n"
  528. . "${i} netmask: '$mask'\n";
  529. if (defined(my $gw = $ipconfig->{gw})) {
  530. $content .= "${i} gateway: '$gw'\n";
  531. }
  532. }
  533. }
  534. if (defined(my $ip = $ipconfig->{ip6})) {
  535. if ($ip eq 'dhcp') {
  536. $content .= "${i}- type: dhcp6\n";
  537. } elsif ($ip eq 'auto') {
  538. # SLAAC is only supported by cloud-init since 19.4
  539. $content .= "${i}- type: ipv6_slaac\n";
  540. } else {
  541. $content .= "${i}- type: static6\n"
  542. . "${i} address: '$ip'\n";
  543. if (defined(my $gw = $ipconfig->{gw6})) {
  544. $content .= "${i} gateway: '$gw'\n";
  545. }
  546. }
  547. }
  548. }
  549.  
  550. my $i = ' ';
  551. my ($searchdomains, $nameservers) = get_dns_conf($conf);
  552. if ($searchdomains || $nameservers) {
  553. $content .= "${i}- type: nameserver\n";
  554. if (defined($nameservers) && @$nameservers) {
  555. $content .= "${i} address:\n";
  556. $content .= "${i} - '$_'\n" foreach @$nameservers;
  557. }
  558. if (defined($searchdomains) && @$searchdomains) {
  559. $content .= "${i} search:\n";
  560. $content .= "${i} - '$_'\n" foreach @$searchdomains;
  561. }
  562. }
  563.  
  564. return $content;
  565. }
  566.  
  567. sub nocloud_metadata {
  568. my ($uuid) = @_;
  569. return "instance-id: $uuid\n";
  570. }
  571.  
  572. sub nocloud_gen_metadata {
  573. my ($user, $network) = @_;
  574.  
  575. my $uuid_str = Digest::SHA::sha1_hex($user.$network);
  576. return nocloud_metadata($uuid_str);
  577. }
  578.  
  579. sub generate_nocloud {
  580. my ($conf, $vmid, $drive, $volname, $storeid) = @_;
  581.  
  582. my ($user_data, $network_data, $meta_data, $vendor_data) = get_custom_cloudinit_files($conf);
  583. $user_data = cloudinit_userdata($conf, $vmid) if !defined($user_data);
  584. $network_data = nocloud_network($conf) if !defined($network_data);
  585.  
  586. if (!defined($meta_data)) {
  587. $meta_data = nocloud_gen_metadata($user_data, $network_data);
  588. }
  589.  
  590. # we always allocate a 4MiB disk for cloudinit and with the overhead of the ISO
  591. # make sure we always stay below it by keeping the sum of all files below 3 MiB
  592. my $sum = length($user_data) + length($network_data) + length($meta_data) + length($vendor_data);
  593. die "Cloud-Init sum of snippets too big (> 3 MiB)\n" if $sum > (3 * 1024 * 1024);
  594.  
  595. my $files = {
  596. '/user-data' => $user_data,
  597. '/network-config' => $network_data,
  598. '/meta-data' => $meta_data,
  599. '/vendor-data' => $vendor_data
  600. };
  601. commit_cloudinit_disk($conf, $vmid, $drive, $volname, $storeid, $files, 'cidata');
  602. }
  603.  
  604. sub get_custom_cloudinit_files {
  605. my ($conf) = @_;
  606.  
  607. my $cicustom = $conf->{cicustom};
  608. my $files = $cicustom ? PVE::JSONSchema::parse_property_string('pve-qm-cicustom', $cicustom) : {};
  609.  
  610. my $network_volid = $files->{network};
  611. my $user_volid = $files->{user};
  612. my $meta_volid = $files->{meta};
  613. my $vendor_volid = $files->{vendor};
  614.  
  615. my $storage_conf = PVE::Storage::config();
  616.  
  617. my $network_data;
  618. if ($network_volid) {
  619. $network_data = read_cloudinit_snippets_file($storage_conf, $network_volid);
  620. }
  621.  
  622. my $user_data;
  623. if ($user_volid) {
  624. $user_data = read_cloudinit_snippets_file($storage_conf, $user_volid);
  625. }
  626.  
  627. my $meta_data;
  628. if ($meta_volid) {
  629. $meta_data = read_cloudinit_snippets_file($storage_conf, $meta_volid);
  630. }
  631.  
  632. my $vendor_data;
  633. if ($vendor_volid) {
  634. $vendor_data = read_cloudinit_snippets_file($storage_conf, $vendor_volid);
  635. }
  636.  
  637. return ($user_data, $network_data, $meta_data, $vendor_data);
  638. }
  639.  
  640. sub read_cloudinit_snippets_file {
  641. my ($storage_conf, $volid) = @_;
  642.  
  643. my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid);
  644. die "$volid is not in the snippets directory\n" if $type ne 'snippets';
  645. return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024);
  646. }
  647.  
  648. my $cloudinit_methods = {
  649. configdrive2 => \&generate_configdrive2,
  650. nocloud => \&generate_nocloud,
  651. opennebula => \&generate_opennebula,
  652. };
  653.  
  654. sub generate_cloudinitconfig {
  655. my ($conf, $vmid) = @_;
  656.  
  657. my $format = get_cloudinit_format($conf);
  658.  
  659. PVE::QemuConfig->foreach_volume($conf, sub {
  660. my ($ds, $drive) = @_;
  661.  
  662. my ($storeid, $volname) = PVE::Storage::parse_volume_id($drive->{file}, 1);
  663.  
  664. return if !$volname || $volname !~ m/vm-$vmid-cloudinit/;
  665.  
  666. my $generator = $cloudinit_methods->{$format}
  667. or die "missing cloudinit methods for format '$format'\n";
  668.  
  669. $generator->($conf, $vmid, $drive, $volname, $storeid);
  670. });
  671. }
  672.  
  673. sub dump_cloudinit_config {
  674. my ($conf, $vmid, $type) = @_;
  675.  
  676. my $format = get_cloudinit_format($conf);
  677.  
  678. if ($type eq 'user') {
  679. return cloudinit_userdata($conf, $vmid);
  680. } elsif ($type eq 'network') {
  681. if ($format eq 'nocloud') {
  682. return nocloud_network($conf);
  683. } else {
  684. return configdrive2_network($conf);
  685. }
  686. } else { # metadata config
  687. my $user = cloudinit_userdata($conf, $vmid);
  688. if ($format eq 'nocloud') {
  689. my $network = nocloud_network($conf);
  690. return nocloud_gen_metadata($user, $network);
  691. } else {
  692. my $network = configdrive2_network($conf);
  693. return configdrive2_gen_metadata($user, $network);
  694. }
  695. }
  696. }
  697.  
  698. 1;
Add Comment
Please, Sign In to add comment