X-Git-Url: https://git.proxmox.com/?p=qemu-server.git;a=blobdiff_plain;f=PVE%2FQemuServer%2FCloudinit.pm;h=a5474d3ecc5c8e48675dac4df71c7f0d4e2bcdd8;hp=b7daa2a5e466ab795f3ded876aa1e348504accd3;hb=refs%2Fheads%2Fmaster;hpb=ea18b60455b168e8949df04259a98b0b06c4e48c diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm index b7daa2a..abc6b14 100644 --- a/PVE/QemuServer/Cloudinit.pm +++ b/PVE/QemuServer/Cloudinit.pm @@ -7,10 +7,12 @@ use File::Path; use Digest::SHA; use URI::Escape; use MIME::Base64 qw(encode_base64); +use Storable qw(dclone); use PVE::Tools qw(run_command file_set_contents); use PVE::Storage; use PVE::QemuServer; +use PVE::QemuServer::Helpers; use constant CLOUDINIT_DISK_SIZE => 4 * 1024 * 1024; # 4MiB in bytes @@ -70,7 +72,7 @@ sub get_cloudinit_format { # the new predicatble network device naming scheme. if (defined(my $ostype = $conf->{ostype})) { return 'configdrive2' - if PVE::QemuServer::windows_version($ostype); + if PVE::QemuServer::Helpers::windows_version($ostype); } return 'nocloud'; @@ -85,6 +87,8 @@ sub get_hostname_fqdn { $hostname =~ s/\..*$//; } elsif (my $search = $conf->{searchdomain}) { $fqdn = "$hostname.$search"; + } else { + $fqdn = $hostname; } return ($hostname, $fqdn); } @@ -118,7 +122,7 @@ sub cloudinit_userdata { $content .= "hostname: $hostname\n"; $content .= "manage_etc_hosts: true\n"; - $content .= "fqdn: $fqdn\n" if defined($fqdn); + $content .= "fqdn: $fqdn\n"; my $username = $conf->{ciuser}; my $password = $conf->{cipassword}; @@ -144,7 +148,7 @@ sub cloudinit_userdata { $content .= " - default\n"; } - $content .= "package_upgrade: true\n"; + $content .= "package_upgrade: true\n" if !defined($conf->{ciupgrade}) || $conf->{ciupgrade}; return $content; } @@ -546,8 +550,11 @@ sub get_custom_cloudinit_files { sub read_cloudinit_snippets_file { my ($storage_conf, $volid) = @_; - my ($full_path, undef, $type) = PVE::Storage::path($storage_conf, $volid); - die "$volid is not in the snippets directory\n" if $type ne 'snippets'; + my ($vtype, undef) = PVE::Storage::parse_volname($storage_conf, $volid); + + die "$volid is not in the snippets directory\n" if $vtype ne 'snippets'; + + my $full_path = PVE::Storage::abs_filesystem_path($storage_conf, $volid, 1); return PVE::Tools::file_get_contents($full_path, 1 * 1024 * 1024); } @@ -557,11 +564,19 @@ my $cloudinit_methods = { opennebula => \&generate_opennebula, }; -sub generate_cloudinitconfig { +sub has_changes { + my ($conf) = @_; + + return !!$conf->{cloudinit}->%*; +} + +sub generate_cloudinit_config { my ($conf, $vmid) = @_; my $format = get_cloudinit_format($conf); + my $has_changes = has_changes($conf); + PVE::QemuConfig->foreach_volume($conf, sub { my ($ds, $drive) = @_; @@ -574,6 +589,22 @@ sub generate_cloudinitconfig { $generator->($conf, $vmid, $drive, $volname, $storeid); }); + + return $has_changes; +} + +sub apply_cloudinit_config { + my ($conf, $vmid) = @_; + + my $has_changes = generate_cloudinit_config($conf, $vmid); + + if ($has_changes) { + delete $conf->{cloudinit}; + PVE::QemuConfig->write_config($vmid, $conf); + return 1; + } + + return $has_changes; } sub dump_cloudinit_config {