]> git.proxmox.com Git - qemu-server.git/blobdiff - PVE/QemuServer/Cloudinit.pm
schema: fix description of migrate_downtime parameter
[qemu-server.git] / PVE / QemuServer / Cloudinit.pm
index f9bcbbcfc368fbc90f1225b96237e82b158ff9cc..abc6b1421b38c67f3de46ea075d5f8ac2fe599ef 100644 (file)
@@ -87,6 +87,8 @@ sub get_hostname_fqdn {
        $hostname =~ s/\..*$//;
     } elsif (my $search = $conf->{searchdomain}) {
        $fqdn = "$hostname.$search";
+    } else {
+       $fqdn = $hostname;
     }
     return ($hostname, $fqdn);
 }
@@ -120,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};
@@ -146,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;
 }
@@ -548,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);
 }
 
@@ -559,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) = @_;
 
@@ -576,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 {
@@ -603,82 +632,4 @@ sub dump_cloudinit_config {
     }
 }
 
-sub get_pending_config {
-    my ($conf, $vmid) = @_;
-
-    my $newconf = dclone($conf);
-
-    my $cloudinit_current = $newconf->{cloudinit};
-    my @cloudinit_opts = keys %{PVE::QemuServer::cloudinit_config_properties()};
-    push @cloudinit_opts, 'name';
-
-    #add cloud-init drive
-    my $drives = {};
-    PVE::QemuConfig->foreach_volume($newconf, sub {
-       my ($ds, $drive) = @_;
-       $drives->{$ds} = 1 if PVE::QemuServer::drive_is_cloudinit($drive);
-    });
-
-    PVE::QemuConfig->foreach_volume($cloudinit_current, sub {
-       my ($ds, $drive) = @_;
-       $drives->{$ds} = 1 if PVE::QemuServer::drive_is_cloudinit($drive);
-    });
-    for my $ds (keys %{$drives}) {
-       push @cloudinit_opts, $ds;
-    }
-
-    $newconf->{name} = "VM$vmid" if !$newconf->{name};
-    $cloudinit_current->{name} = "VM$vmid" if !$cloudinit_current->{name};
-
-    #only mac-address is used in cloud-init config.
-    #We don't want to display other pending net changes.
-    my $print_cloudinit_net = sub {
-       my ($conf, $opt) = @_;
-
-       if (defined($conf->{$opt})) {
-           my $net = PVE::QemuServer::parse_net($conf->{$opt});
-           $conf->{$opt} = "macaddr=".$net->{macaddr} if $net->{macaddr};
-       }
-    };
-
-    my $cloudinit_options = {};
-    for my $opt (@cloudinit_opts) {
-       if ($opt =~ m/^ipconfig(\d+)/) {
-           my $netid = "net$1";
-
-           next if !defined($newconf->{$netid}) && !defined($cloudinit_current->{$netid}) &&
-                   !defined($newconf->{$opt}) && !defined($cloudinit_current->{$opt});
-
-           &$print_cloudinit_net($newconf, $netid);
-           &$print_cloudinit_net($cloudinit_current, $netid);
-           $cloudinit_options->{$netid} = 1;
-       }
-       $cloudinit_options->{$opt} = 1;
-    }
-
-    my $res = [];
-
-    for my $opt (keys %{$cloudinit_options}) {
-
-       my $item = {
-           key => $opt,
-       };
-       if ($cloudinit_current->{$opt}) {
-           $item->{value} = $cloudinit_current->{$opt};
-           if (defined($newconf->{$opt})) {
-               $item->{pending} = $newconf->{$opt} 
-                   if $newconf->{$opt} ne $cloudinit_current->{$opt};
-           } else {
-               $item->{delete} = 1;
-           }
-       } else {
-           $item->{pending} = $newconf->{$opt} if $newconf->{$opt}
-       }
-
-       push @$res, $item;
-   }
-
-   return $res;
-}
-
 1;