[pve-devel] [PATCH v6 qemu-server 7/9] api2: add cloudinit config api

Fabian Ebner f.ebner at proxmox.com
Tue Jun 21 13:45:06 CEST 2022


Am 20.06.22 um 12:45 schrieb Alexandre Derumier:
> diff --git a/PVE/QemuServer/Cloudinit.pm b/PVE/QemuServer/Cloudinit.pm
> index cdaf4e5..2355953 100644
> --- a/PVE/QemuServer/Cloudinit.pm
> +++ b/PVE/QemuServer/Cloudinit.pm
> @@ -632,4 +633,81 @@ 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);
> +    });
> +    foreach my $ds (keys %{$drives}) {

Style nit: please use for

> +	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. 

Style nit: trailing whitespace

> +    #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";
> + 

Style nit: trailing whitespace

> +	    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};

Style nit: line too long

> +	    } else {
> +		$item->{delete} = 1;
> +	    }
> +	} else {
> +	    $item->{pending} = $newconf->{$opt} if $newconf->{$opt}
> +	}
> +
> +	push @$res, $item;
> +   }
> +
> +   return $res;
> +}
> +
>  1;





More information about the pve-devel mailing list