use PVE::Cluster;
use PVE::Storage;
+use PVE::GuestHelpers qw(typesafe_ne);
use PVE::ReplicationConfig;
use PVE::Replication;
PVE::Cluster::cfs_write_file($cfspath, $conf);
}
+# Pending changes related
+
+sub parse_pending_delete {
+ my ($class, $data) = @_;
+
+ return {} if !$data;
+
+ $data =~ s/[,;]/ /g;
+ $data =~ s/^\s+//;
+
+ my $pending_deletions = {};
+ for my $entry (split(/\s+/, $data)) {
+ my ($force, $key) = $entry =~ /^(!?)(.*)$/;
+
+ $pending_deletions->{$key} = {
+ force => $force ? 1 : 0,
+ };
+ }
+
+ return $pending_deletions;
+}
+
+sub print_pending_delete {
+ my ($class, $delete_hash) = @_;
+
+ my $render_key = sub {
+ my $key = shift;
+ $key = "!$key" if $delete_hash->{$key}->{force};
+ return $key;
+ };
+
+ join (',', map { $render_key->($_) } sort keys %$delete_hash);
+}
+
+sub add_to_pending_delete {
+ my ($class, $conf, $key, $force) = @_;
+
+ $conf->{pending} //= {};
+ my $pending = $conf->{pending};
+ my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
+
+ $pending_delete_hash->{$key} = { force => $force };
+
+ $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
+
+ return $conf;
+}
+
+sub remove_from_pending_delete {
+ my ($class, $conf, $key) = @_;
+
+ my $pending = $conf->{pending};
+ my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
+
+ return $conf if ! exists $pending_delete_hash->{$key};
+
+ delete $pending_delete_hash->{$key};
+
+ if (%$pending_delete_hash) {
+ $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
+ } else {
+ delete $pending->{delete};
+ }
+
+ return $conf;
+}
+
+sub cleanup_pending {
+ my ($class, $conf) = @_;
+
+ my $pending = $conf->{pending};
+ # remove pending changes when nothing changed
+ my $changes;
+ foreach my $opt (keys %{$conf->{pending}}) {
+ next if $opt eq 'delete'; # just to be sure
+ if (defined($conf->{$opt}) && ($pending->{$opt} eq $conf->{$opt})) {
+ $changes = 1;
+ delete $pending->{$opt};
+ }
+ }
+
+ my $current_delete_hash = $class->parse_pending_delete($pending->{delete});
+ my $pending_delete_hash = {};
+ for my $opt (keys %$current_delete_hash) {
+ if (defined($conf->{$opt})) {
+ $pending_delete_hash->{$opt} = $current_delete_hash->{$opt};
+ } else {
+ $changes = 1;
+ }
+ }
+
+ if (%$pending_delete_hash) {
+ $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
+ } else {
+ delete $pending->{delete};
+ }
+
+ return $changes;
+}
+
+sub get_partial_fast_plug_option {
+ my ($class) = @_;
+
+ die "abstract method - implement me ";
+}
+
+sub partial_fast_plug {
+ my ($class, $conf, $opt) = @_;
+
+ my $partial_fast_plug_option = $class->get_partial_fast_plug_option();
+ return 0 if !$partial_fast_plug_option->{$opt};
+
+ my $format = $partial_fast_plug_option->{$opt}->{fmt};
+ my $fast_pluggable = $partial_fast_plug_option->{$opt}->{properties};
+
+ my $configured = {};
+ if (exists($conf->{$opt})) {
+ $configured = PVE::JSONSchema::parse_property_string($format, $conf->{$opt});
+ }
+ my $pending = PVE::JSONSchema::parse_property_string($format, $conf->{pending}->{$opt});
+
+ my $changes = 0;
+
+ # merge configured and pending opts to iterate
+ my @all_keys = keys %{{ %$pending, %$configured }};
+
+ foreach my $subopt (@all_keys) {
+ my $type = $format->{$subopt}->{type};
+ if (typesafe_ne($configured->{$subopt}, $pending->{$subopt}, $type)) {
+ if ($fast_pluggable->{$subopt}) {
+ $configured->{$subopt} = $pending->{$subopt};
+ $changes = 1
+ }
+ }
+ }
+
+ # if there're no keys in $configured (after merge) there shouldn't be anything to change
+ if (keys %$configured) {
+ $conf->{$opt} = PVE::JSONSchema::print_property_string($configured, $format);
+ }
+
+ return $changes;
+}
+
+
+sub load_snapshot_config {
+ my ($class, $vmid, $snapname) = @_;
+
+ my $conf = $class->load_config($vmid);
+
+ my $snapshot = $conf->{snapshots}->{$snapname};
+ die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
+
+ $snapshot->{digest} = $conf->{digest};
+
+ return $snapshot;
+
+}
+
+sub load_current_config {
+ my ($class, $vmid, $current) = @_;
+
+ my $conf = $class->load_config($vmid);
+
+ # take pending changes in
+ if (!$current) {
+ foreach my $opt (keys %{$conf->{pending}}) {
+ next if $opt eq 'delete';
+ my $value = $conf->{pending}->{$opt};
+ next if ref($value); # just to be sure
+ $conf->{$opt} = $value;
+ }
+ my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
+ foreach my $opt (keys %$pending_delete_hash) {
+ delete $conf->{$opt} if $conf->{$opt};
+ }
+ }
+
+ delete $conf->{snapshots};
+ delete $conf->{pending};
+
+ return $conf;
+}
+
+
# Lock config file using flock, run $code with @param, unlock config file.
-# $timeout is the maximum time to aquire the flock
+# $timeout is the maximum time to acquire the flock
sub lock_config_full {
my ($class, $vmid, $timeout, $code, @param) = @_;
}
sub create_and_lock_config {
- my ($class, $vmid, $allow_existing) = @_;
+ my ($class, $vmid, $allow_existing, $lock) = @_;
$class->lock_config_full($vmid, 5, sub {
PVE::Cluster::check_vmid_unused($vmid, $allow_existing);
my $conf = eval { $class->load_config($vmid) } || {};
$class->check_lock($conf);
- $conf->{lock} = 'create';
+ $conf->{lock} = $lock // 'create';
$class->write_config($vmid, $conf);
});
}
+# destroys configuration, only applicable for configs owned by the callers node.
+# dies if removal fails, e.g., when inquorate.
+sub destroy_config {
+ my ($class, $vmid) = @_;
+
+ my $config_fn = $class->config_file($vmid, $nodename);
+ unlink $config_fn or die "failed to remove config file: $!\n";
+}
+
# Lock config file using flock, run $code with @param, unlock config file.
-# $timeout is the maximum time to aquire the flock
+# $timeout is the maximum time to acquire the flock
# $shared eq 1 creates a non-exclusive ("read") flock
sub lock_config_mode {
my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
return 1 if defined $conf->{template} && $conf->{template} == 1;
}
-# Checks whether $feature is availabe for the referenced volumes in $conf.
+# Checks whether $feature is available for the referenced volumes in $conf.
# Note: depending on the parameters, some volumes may be skipped!
sub has_feature {
my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
$class->lock_config($vmid, $updatefn);
}
+# bash completion helper
+
+sub snapshot_list {
+ my ($class, $vmid) = @_;
+
+ my $snapshot = eval {
+ my $conf = $class->load_config($vmid);
+ my $snapshots = $conf->{snapshots} || [];
+ [ sort keys %$snapshots ]
+ } || [];
+
+ return $snapshot;
+}
+
1;