1 package PVE
::AbstractConfig
;
6 use PVE
::Tools
qw(lock_file lock_file_full);
11 use PVE
::GuestHelpers
qw(typesafe_ne);
12 use PVE
::ReplicationConfig
;
15 my $nodename = PVE
::INotify
::nodename
();
17 # Printable string, currently either "VM" or "CT"
20 die "abstract method - implement me ";
23 sub __config_max_unused_disks
{
26 die "implement me - abstract method\n";
29 # Path to the flock file for this VM/CT
30 sub config_file_lock
{
31 my ($class, $vmid) = @_;
32 die "abstract method - implement me";
35 # Relative config file path for this VM/CT in CFS
37 my ($class, $vmid, $node) = @_;
38 die "abstract method - implement me";
41 # Absolute config file path for this VM/CT
43 my ($class, $vmid, $node) = @_;
45 my $cfspath = $class->cfs_config_path($vmid, $node);
46 return "/etc/pve/$cfspath";
49 # Read and parse config file for this VM/CT
51 my ($class, $vmid, $node) = @_;
53 $node = $nodename if !$node;
54 my $cfspath = $class->cfs_config_path($vmid, $node);
56 my $conf = PVE
::Cluster
::cfs_read_file
($cfspath);
57 die "Configuration file '$cfspath' does not exist\n"
63 # Generate and write config file for this VM/CT
65 my ($class, $vmid, $conf) = @_;
67 my $cfspath = $class->cfs_config_path($vmid);
69 PVE
::Cluster
::cfs_write_file
($cfspath, $conf);
72 # Pending changes related
74 sub parse_pending_delete
{
75 my ($class, $data) = @_;
82 my $pending_deletions = {};
83 for my $entry (split(/\s+/, $data)) {
84 my ($force, $key) = $entry =~ /^(!?)(.*)$/;
85 $pending_deletions->{$key} = {
86 force
=> $force ?
1 : 0,
90 return $pending_deletions;
93 sub print_pending_delete
{
94 my ($class, $delete_hash) = @_;
96 my $render_key = sub {
98 $key = "!$key" if $delete_hash->{$key}->{force
};
102 join (',', map { $render_key->($_) } sort keys %$delete_hash);
105 sub add_to_pending_delete
{
106 my ($class, $conf, $key, $force) = @_;
108 $conf->{pending
} //= {};
109 my $pending = $conf->{pending
};
110 my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
112 $pending_delete_hash->{$key} = { force
=> $force };
114 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
119 sub remove_from_pending_delete
{
120 my ($class, $conf, $key) = @_;
122 my $pending = $conf->{pending
};
123 my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
125 return $conf if ! exists $pending_delete_hash->{$key};
127 delete $pending_delete_hash->{$key};
129 if (%$pending_delete_hash) {
130 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
132 delete $pending->{delete};
138 sub cleanup_pending
{
139 my ($class, $conf) = @_;
141 my $pending = $conf->{pending
};
142 # remove pending changes when nothing changed
144 foreach my $opt (keys %{$conf->{pending
}}) {
145 next if $opt eq 'delete'; # just to be sure
146 if (defined($conf->{$opt}) && ($pending->{$opt} eq $conf->{$opt})) {
148 delete $pending->{$opt};
152 my $current_delete_hash = $class->parse_pending_delete($pending->{delete});
153 my $pending_delete_hash = {};
154 for my $opt (keys %$current_delete_hash) {
155 if (defined($conf->{$opt})) {
156 $pending_delete_hash->{$opt} = $current_delete_hash->{$opt};
162 if (%$pending_delete_hash) {
163 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
165 delete $pending->{delete};
171 sub get_partial_fast_plug_option
{
174 die "abstract method - implement me ";
177 sub partial_fast_plug
{
178 my ($class, $conf, $opt) = @_;
180 my $partial_fast_plug_option = $class->get_partial_fast_plug_option();
181 return 0 if !$partial_fast_plug_option->{$opt};
183 my $format = $partial_fast_plug_option->{$opt}->{fmt
};
184 my $fast_pluggable = $partial_fast_plug_option->{$opt}->{properties
};
187 if (exists($conf->{$opt})) {
188 $configured = PVE
::JSONSchema
::parse_property_string
($format, $conf->{$opt});
190 my $pending = PVE
::JSONSchema
::parse_property_string
($format, $conf->{pending
}->{$opt});
194 # merge configured and pending opts to iterate
195 my @all_keys = keys %{{ %$pending, %$configured }};
197 foreach my $subopt (@all_keys) {
198 my $type = $format->{$subopt}->{type
};
199 if (typesafe_ne
($configured->{$subopt}, $pending->{$subopt}, $type)) {
200 if ($fast_pluggable->{$subopt}) {
201 $configured->{$subopt} = $pending->{$subopt};
207 # if there're no keys in $configured (after merge) there shouldn't be anything to change
208 if (keys %$configured) {
209 $conf->{$opt} = PVE
::JSONSchema
::print_property_string
($configured, $format);
216 sub load_snapshot_config
{
217 my ($class, $vmid, $snapname) = @_;
219 my $conf = $class->load_config($vmid);
221 my $snapshot = $conf->{snapshots
}->{$snapname};
222 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
224 $snapshot->{digest
} = $conf->{digest
};
230 sub load_current_config
{
231 my ($class, $vmid, $current) = @_;
233 my $conf = $class->load_config($vmid);
235 # take pending changes in
237 foreach my $opt (keys %{$conf->{pending
}}) {
238 next if $opt eq 'delete';
239 my $value = $conf->{pending
}->{$opt};
240 next if ref($value); # just to be sure
241 $conf->{$opt} = $value;
243 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
244 foreach my $opt (keys %$pending_delete_hash) {
245 delete $conf->{$opt} if $conf->{$opt};
249 delete $conf->{snapshots
};
250 delete $conf->{pending
};
255 sub create_and_lock_config
{
256 my ($class, $vmid, $allow_existing, $lock) = @_;
258 $class->lock_config_full($vmid, 5, sub {
259 PVE
::Cluster
::check_vmid_unused
($vmid, $allow_existing);
261 my $conf = eval { $class->load_config($vmid) } || {};
262 $class->check_lock($conf);
263 $conf->{lock} = $lock // 'create';
264 $class->write_config($vmid, $conf);
268 # destroys configuration, only applicable for configs owned by the callers node.
269 # dies if removal fails, e.g., when inquorate.
271 my ($class, $vmid) = @_;
273 my $config_fn = $class->config_file($vmid, $nodename);
274 unlink $config_fn or die "failed to remove config file: $!\n";
277 # moves configuration owned by calling node to the target node.
278 # dies if renaming fails.
279 # NOTE: in PVE a node owns the config (hard requirement), so only the owning
280 # node may move the config to another node, which then becomes the new owner.
281 sub move_config_to_node
{
282 my ($class, $vmid, $target_node) = @_;
284 my $config_fn = $class->config_file($vmid);
285 my $new_config_fn = $class->config_file($vmid, $target_node);
287 rename($config_fn, $new_config_fn)
288 or die "failed to move config file to node '$target_node': $!\n";
291 my $lock_file_full_wrapper = sub {
292 my ($class, $vmid, $timeout, $shared, $realcode, @param) = @_;
294 my $filename = $class->config_file_lock($vmid);
296 # make sure configuration file is up-to-date
298 PVE
::Cluster
::cfs_update
();
302 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
309 # Lock config file using non-exclusive ("read") flock, run $code with @param, unlock config file.
310 # $timeout is the maximum time to acquire the flock
311 sub lock_config_shared
{
312 my ($class, $vmid, $timeout, $code, @param) = @_;
314 return $lock_file_full_wrapper->($class, $vmid, $timeout, 1, $code, @param);
317 # Lock config file using flock, run $code with @param, unlock config file.
318 # $timeout is the maximum time to acquire the flock
319 sub lock_config_full
{
320 my ($class, $vmid, $timeout, $code, @param) = @_;
322 return $lock_file_full_wrapper->($class, $vmid, $timeout, 0, $code, @param);
326 # Lock config file using flock, run $code with @param, unlock config file.
328 my ($class, $vmid, $code, @param) = @_;
330 return $class->lock_config_full($vmid, 10, $code, @param);
333 # Checks whether the config is locked with the lock parameter
335 my ($class, $conf) = @_;
337 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
340 # Returns whether the config is locked with the lock parameter, also checks
341 # whether the lock value is correct if the optional $lock is set.
343 my ($class, $conf, $lock) = @_;
345 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
348 # Sets the lock parameter for this VM/CT's config to $lock.
350 my ($class, $vmid, $lock) = @_;
353 $class->lock_config($vmid, sub {
354 $conf = $class->load_config($vmid);
355 $class->check_lock($conf);
356 $conf->{lock} = $lock;
357 $class->write_config($vmid, $conf);
362 # Removes the lock parameter for this VM/CT's config, also checks whether
363 # the lock value is correct if the optional $lock is set.
365 my ($class, $vmid, $lock) = @_;
367 $class->lock_config($vmid, sub {
368 my $conf = $class->load_config($vmid);
369 if (!$conf->{lock}) {
370 my $lockstring = defined($lock) ?
"'$lock' " : "any";
371 die "no lock found trying to remove $lockstring lock\n";
372 } elsif (defined($lock) && $conf->{lock} ne $lock) {
373 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
375 delete $conf->{lock};
376 $class->write_config($vmid, $conf);
380 # Checks whether protection mode is enabled for this VM/CT.
381 sub check_protection
{
382 my ($class, $conf, $err_msg) = @_;
384 if ($conf->{protection
}) {
385 die "$err_msg - protection mode enabled\n";
389 # Returns a list of keys where used volumes can be located
390 sub valid_volume_keys
{
391 my ($class, $reverse) = @_;
393 die "implement me - abstract method\n";
396 # Returns a hash with the information from $volume_string
397 # $key is used to determine the format of the string
399 my ($class, $key, $volume_string, $noerr) = @_;
401 die "implement me - abstract method\n";
404 # Returns a string with the information from $volume
405 # $key is used to determine the format of the string
407 my ($class, $key, $volume) = @_;
409 die "implement me - abstract method\n";
412 # The key under which the volume ID is located in volume hashes
416 die "implement me - abstract method\n";
419 # Adds an unused volume to $config, if possible.
420 sub add_unused_volume
{
421 my ($class, $config, $volid) = @_;
424 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
425 my $test = "unused$ind";
426 if (my $vid = $config->{$test}) {
427 return if $vid eq $volid; # do not add duplicates
433 die "Too many unused volumes - please delete them first.\n" if !$key;
435 $config->{$key} = $volid;
440 # Iterate over all unused volumes, calling $func for each key/value pair
441 # with additional parameters @param.
442 sub foreach_unused_volume
{
443 my ($class, $conf, $func, @param) = @_;
445 foreach my $key (keys %{$conf}) {
446 if ($key =~ m/^unused\d+$/) {
447 my $volume = $class->parse_volume($key, $conf->{$key});
448 $func->($key, $volume, @param);
453 # Iterate over all configured volumes, calling $func for each key/value pair
454 # with additional parameters @param.
455 # By default, unused volumes and specials like vmstate are excluded.
456 # Options: reverse - reverses the order for the iteration
457 # include_unused - also iterate over unused volumes
458 # extra_keys - an array of extra keys to use for the iteration
459 sub foreach_volume_full
{
460 my ($class, $conf, $opts, $func, @param) = @_;
462 die "'reverse' iteration only supported for default keys\n"
463 if $opts->{reverse} && ($opts->{extra_keys
} || $opts->{include_unused
});
465 my @keys = $class->valid_volume_keys($opts->{reverse});
466 push @keys, @{$opts->{extra_keys
}} if $opts->{extra_keys
};
468 foreach my $key (@keys) {
469 my $volume_string = $conf->{$key};
470 next if !defined($volume_string);
472 my $volume = $class->parse_volume($key, $volume_string, 1);
473 next if !defined($volume);
475 $func->($key, $volume, @param);
478 $class->foreach_unused_volume($conf, $func, @param) if $opts->{include_unused
};
482 my ($class, $conf, $func, @param) = @_;
484 return $class->foreach_volume_full($conf, undef, $func, @param);
487 # $volume_map is a hash of 'old_volid' => 'new_volid' pairs.
488 # This method replaces 'old_volid' by 'new_volid' throughout the config including snapshots, pending
489 # changes, unused volumes and vmstate volumes.
490 sub update_volume_ids
{
491 my ($class, $conf, $volume_map) = @_;
493 my $volid_key = $class->volid_key();
495 my $do_replace = sub {
496 my ($key, $volume, $current_section) = @_;
498 my $old_volid = $volume->{$volid_key};
499 if (my $new_volid = $volume_map->{$old_volid}) {
500 $volume->{$volid_key} = $new_volid;
501 $current_section->{$key} = $class->print_volume($key, $volume);
506 'include_unused' => 1,
507 'extra_keys' => ['vmstate'],
510 $class->foreach_volume_full($conf, $opts, $do_replace, $conf);
511 foreach my $snap (keys %{$conf->{snapshots
}}) {
512 my $snap_conf = $conf->{snapshots
}->{$snap};
513 $class->foreach_volume_full($snap_conf, $opts, $do_replace, $snap_conf);
516 $class->foreach_volume_full($conf->{pending
}, $opts, $do_replace, $conf->{pending
});
519 # Returns whether the template parameter is set in $conf.
521 my ($class, $conf) = @_;
523 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
526 # Checks whether $feature is available for the referenced volumes in $conf.
527 # Note: depending on the parameters, some volumes may be skipped!
529 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
530 die "implement me - abstract method\n";
533 # get all replicatable volume (hash $res->{$volid} = 1)
534 # $cleanup: for cleanup - simply ignores volumes without replicate feature
535 # $norerr: never raise exceptions - return undef instead
536 sub get_replicatable_volumes
{
537 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
539 die "implement me - abstract method\n";
542 # Returns all the guests volumes which would be included in a vzdump job
543 # Return Format (array-ref with hash-refs as elements):
546 # key, key in the config, e.g. mp0, scsi0,...
549 # volume_config volume object as returned from foreach_volume()
552 sub get_backup_volumes
{
553 my ($class, $conf) = @_;
555 die "implement me - abstract method\n";
560 # NOTE: Snapshot create/delete involves several non-atomic
561 # actions, and can take a long time.
562 # So we try to avoid locking the file and use the 'lock' variable
563 # inside the config file instead.
565 # Save the vmstate (RAM).
566 sub __snapshot_save_vmstate
{
567 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
568 die "implement me - abstract method\n";
571 # Check whether the VM/CT is running.
572 sub __snapshot_check_running
{
573 my ($class, $vmid) = @_;
574 die "implement me - abstract method\n";
577 # Check whether we need to freeze the VM/CT
578 sub __snapshot_check_freeze_needed
{
579 my ($sself, $vmid, $config, $save_vmstate) = @_;
580 die "implement me - abstract method\n";
583 # Freeze or unfreeze the VM/CT.
584 sub __snapshot_freeze
{
585 my ($class, $vmid, $unfreeze) = @_;
587 die "abstract method - implement me\n";
590 # Code run before and after creating all the volume snapshots
592 sub __snapshot_create_vol_snapshots_hook
{
593 my ($class, $vmid, $snap, $running, $hook) = @_;
598 # Create the volume snapshots for the VM/CT.
599 sub __snapshot_create_vol_snapshot
{
600 my ($class, $vmid, $vs, $volume, $snapname) = @_;
602 die "abstract method - implement me\n";
605 # Remove a drive from the snapshot config.
606 sub __snapshot_delete_remove_drive
{
607 my ($class, $snap, $drive) = @_;
609 die "abstract method - implement me\n";
612 # Delete the vmstate file/drive
613 sub __snapshot_delete_vmstate_file
{
614 my ($class, $snap, $force) = @_;
616 die "abstract method - implement me\n";
619 # Delete a volume snapshot
620 sub __snapshot_delete_vol_snapshot
{
621 my ($class, $vmid, $vs, $volume, $snapname) = @_;
623 die "abstract method - implement me\n";
626 # called during rollback prepare, and between config rollback and starting guest
627 # can change config, e.g. for vmgenid
628 # $data is shared across calls and passed to vm_start
629 sub __snapshot_rollback_hook
{
630 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
635 # Checks whether a volume snapshot is possible for this volume.
636 sub __snapshot_rollback_vol_possible
{
637 my ($class, $volume, $snapname) = @_;
639 die "abstract method - implement me\n";
642 # Rolls back this volume.
643 sub __snapshot_rollback_vol_rollback
{
644 my ($class, $volume, $snapname) = @_;
646 die "abstract method - implement me\n";
649 # Stops the VM/CT for a rollback.
650 sub __snapshot_rollback_vm_stop
{
651 my ($class, $vmid) = @_;
653 die "abstract method - implement me\n";
656 # Start the VM/CT after a rollback with restored vmstate.
657 sub __snapshot_rollback_vm_start
{
658 my ($class, $vmid, $vmstate, $data);
660 die "abstract method - implement me\n";
663 # Get list of volume IDs which are referenced in $conf, but not in $snap.
664 sub __snapshot_rollback_get_unused
{
665 my ($class, $conf, $snap) = @_;
667 die "abstract method - implement me\n";
670 # Copy the current config $source to the snapshot config $dest
671 sub __snapshot_copy_config
{
672 my ($class, $source, $dest) = @_;
674 foreach my $k (keys %$source) {
675 next if $k eq 'snapshots';
676 next if $k eq 'snapstate';
677 next if $k eq 'snaptime';
678 next if $k eq 'vmstate';
679 next if $k eq 'lock';
680 next if $k eq 'digest';
681 next if $k eq 'description';
682 next if $k =~ m/^unused\d+$/;
684 $dest->{$k} = $source->{$k};
688 # Apply the snapshot config $snap to the config $conf (rollback)
689 sub __snapshot_apply_config
{
690 my ($class, $conf, $snap) = @_;
694 snapshots
=> $conf->{snapshots
},
697 # keep description and list of unused disks
698 foreach my $k (keys %$conf) {
699 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
701 $newconf->{$k} = $conf->{$k};
704 $class->__snapshot_copy_config($snap, $newconf);
709 # Prepares the configuration for snapshotting.
710 sub __snapshot_prepare
{
711 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
717 my $conf = $class->load_config($vmid);
719 die "you can't take a snapshot if it's a template\n"
720 if $class->is_template($conf);
722 $class->check_lock($conf);
724 $conf->{lock} = 'snapshot';
726 my $snapshots = $conf->{snapshots
};
728 die "snapshot name '$snapname' already used\n"
729 if defined($snapshots->{$snapname});
731 my $storecfg = PVE
::Storage
::config
();
732 die "snapshot feature is not available\n"
733 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
735 for my $snap (sort keys %$snapshots) {
736 my $parent_name = $snapshots->{$snap}->{parent
} // '';
737 if ($snapname eq $parent_name) {
738 warn "deleting parent '$parent_name' reference from '$snap' to avoid bogus snapshot cycle.\n";
739 delete $snapshots->{$snap}->{parent
};
743 $snap = $snapshots->{$snapname} = {};
745 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
746 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
749 $class->__snapshot_copy_config($conf, $snap);
751 $snap->{snapstate
} = "prepare";
752 $snap->{snaptime
} = time();
753 $snap->{description
} = $comment if $comment;
755 $class->write_config($vmid, $conf);
758 $class->lock_config($vmid, $updatefn);
763 # Commits the configuration after snapshotting.
764 sub __snapshot_commit
{
765 my ($class, $vmid, $snapname) = @_;
769 my $conf = $class->load_config($vmid);
771 die "missing snapshot lock\n"
772 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
774 my $snap = $conf->{snapshots
}->{$snapname};
775 die "snapshot '$snapname' does not exist\n" if !defined($snap);
777 die "wrong snapshot state\n"
778 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
780 delete $snap->{snapstate
};
781 delete $conf->{lock};
783 $conf->{parent
} = $snapname;
785 $class->write_config($vmid, $conf);
788 $class->lock_config($vmid, $updatefn);
791 # Activates the storages affected by the snapshot operations.
792 sub __snapshot_activate_storages
{
793 my ($class, $conf, $include_vmstate) = @_;
795 # FIXME PVE 8.x (or earlier with break/versioned-depens): uncomment and drop return
796 # die "implement me";
800 # Creates a snapshot for the VM/CT.
801 sub snapshot_create
{
802 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
804 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
806 $save_vmstate = 0 if !$snap->{vmstate
};
808 my $conf = $class->load_config($vmid);
810 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
815 $class->__snapshot_activate_storages($conf, 0);
818 $class->__snapshot_freeze($vmid, 0);
821 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
823 $class->foreach_volume($snap, sub {
824 my ($vs, $volume) = @_;
826 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
827 $drivehash->{$vs} = 1;
833 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
835 $class->__snapshot_freeze($vmid, 1);
837 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
841 warn "snapshot create failed: starting cleanup\n";
842 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
847 $class->__snapshot_commit($vmid, $snapname);
850 # Check if the snapshot might still be needed by a replication job.
851 my $snapshot_delete_assert_not_needed_by_replication = sub {
852 my ($class, $vmid, $conf, $snap, $snapname) = @_;
854 my $repl_conf = PVE
::ReplicationConfig-
>new();
855 return if !$repl_conf->check_for_existing_jobs($vmid, 1);
857 my $storecfg = PVE
::Storage
::config
();
859 # Current config's volumes are relevant for replication.
860 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
862 my $replication_jobs = $repl_conf->list_guests_local_replication_jobs($vmid);
864 $class->foreach_volume($snap, sub {
865 my ($vs, $volume) = @_;
867 my $volid_key = $class->volid_key();
868 my $volid = $volume->{$volid_key};
870 return if !$volumes->{$volid};
872 my $snapshots = PVE
::Storage
::volume_snapshot_info
($storecfg, $volid);
874 for my $job ($replication_jobs->@*) {
875 my $jobid = $job->{id
};
877 my @jobs_snapshots = grep {
878 PVE
::Replication
::is_replication_snapshot
($_, $jobid)
879 } keys $snapshots->%*;
881 next if scalar(@jobs_snapshots) > 0;
883 die "snapshot '$snapname' needed by replication job '$jobid' - run replication first\n";
888 # Deletes a snapshot.
889 # Note: $drivehash is only set when called from snapshot_create.
890 sub snapshot_delete
{
891 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
895 my $conf = $class->load_config($vmid);
896 my $snap = $conf->{snapshots
}->{$snapname};
898 die "snapshot '$snapname' does not exist\n" if !defined($snap);
900 $class->__snapshot_activate_storages($snap, 1) if !$drivehash;
902 $snapshot_delete_assert_not_needed_by_replication->($class, $vmid, $conf, $snap, $snapname)
903 if !$drivehash && !$force;
905 $class->set_lock($vmid, 'snapshot-delete')
906 if (!$drivehash); # doesn't already have a 'snapshot' lock
908 my $expected_lock = $drivehash ?
'snapshot' : 'snapshot-delete';
910 my $ensure_correct_lock = sub {
913 die "encountered invalid lock, expected '$expected_lock'\n"
914 if !$class->has_lock($conf, $expected_lock);
917 my $unlink_parent = sub {
918 my ($confref, $new_parent) = @_;
920 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
922 $confref->{parent
} = $new_parent;
924 delete $confref->{parent
};
929 my $remove_drive = sub {
932 my $conf = $class->load_config($vmid);
933 $ensure_correct_lock->($conf);
935 $snap = $conf->{snapshots
}->{$snapname};
936 die "snapshot '$snapname' does not exist\n" if !defined($snap);
938 $class->__snapshot_delete_remove_drive($snap, $drive);
940 $class->write_config($vmid, $conf);
944 $class->lock_config($vmid, sub {
945 my $conf = $class->load_config($vmid);
946 $ensure_correct_lock->($conf);
948 die "you can't delete a snapshot if vm is a template\n"
949 if $class->is_template($conf);
951 $snap = $conf->{snapshots
}->{$snapname};
952 die "snapshot '$snapname' does not exist\n" if !defined($snap);
954 $snap->{snapstate
} = 'delete';
956 $class->write_config($vmid, $conf);
959 # now remove vmstate file
960 if ($snap->{vmstate
}) {
961 $class->__snapshot_delete_vmstate_file($snap, $force);
963 # save changes (remove vmstate from snapshot)
964 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
967 # now remove all volume snapshots
968 $class->foreach_volume($snap, sub {
969 my ($vs, $volume) = @_;
971 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
972 if (!$drivehash || $drivehash->{$vs}) {
973 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
980 # save changes (remove drive from snapshot)
981 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
985 $class->lock_config($vmid, sub {
986 my $conf = $class->load_config($vmid);
987 $ensure_correct_lock->($conf);
989 $snap = $conf->{snapshots
}->{$snapname};
990 die "snapshot '$snapname' does not exist\n" if !defined($snap);
993 &$unlink_parent($conf, $snap->{parent
});
994 foreach my $sn (keys %{$conf->{snapshots
}}) {
995 next if $sn eq $snapname;
996 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
1000 delete $conf->{snapshots
}->{$snapname};
1001 delete $conf->{lock};
1002 foreach my $volid (@$unused) {
1003 $class->add_unused_volume($conf, $volid);
1006 $class->write_config($vmid, $conf);
1010 # Remove replication snapshots to make a rollback possible.
1011 my $rollback_remove_replication_snapshots = sub {
1012 my ($class, $vmid, $snap, $snapname) = @_;
1014 my $storecfg = PVE
::Storage
::config
();
1016 my $repl_conf = PVE
::ReplicationConfig-
>new();
1017 return if !$repl_conf->check_for_existing_jobs($vmid, 1);
1019 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $snap, 1);
1021 # For these, all replication snapshots need to be removed for backwards compatibility.
1024 # For these, we know more and can remove only the required replication snapshots.
1025 my $blocking_snapshots = {};
1027 # filter by what we actually iterate over below (excludes vmstate!)
1028 $class->foreach_volume($snap, sub {
1029 my ($vs, $volume) = @_;
1031 my $volid_key = $class->volid_key();
1032 my $volid = $volume->{$volid_key};
1034 return if !$volumes->{$volid};
1037 eval { $class->__snapshot_rollback_vol_possible($volume, $snapname, $blockers); };
1039 # FIXME die instead, once $blockers is required by the storage plugin API
1040 # and the guest plugins are required to be new enough to support it too.
1041 # Currently, it's not possible to distinguish between blockers being empty
1042 # because the plugin is old versus because there is a different error.
1043 if (scalar($blockers->@*) == 0) {
1044 push @{$volids}, $volid;
1048 for my $blocker ($blockers->@*) {
1049 die $err if !PVE
::Replication
::is_replication_snapshot
($blocker);
1052 $blocking_snapshots->{$volid} = $blockers;
1056 my $removed_repl_snapshot;
1057 for my $volid (sort keys $blocking_snapshots->%*) {
1058 my $blockers = $blocking_snapshots->{$volid};
1060 for my $blocker ($blockers->@*) {
1061 warn "WARN: removing replication snapshot '$volid\@$blocker'\n";
1062 $removed_repl_snapshot = 1;
1063 eval { PVE
::Storage
::volume_snapshot_delete
($storecfg, $volid, $blocker); };
1067 warn "WARN: you shouldn't remove '$snapname' before running the next replication!\n"
1068 if $removed_repl_snapshot;
1070 # Need to keep using a hammer for backwards compatibility...
1071 # remove all local replication snapshots (jobid => undef)
1072 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
1073 PVE
::Replication
::prepare
($storecfg, $volids, undef, 1, undef, $logfunc);
1076 # Rolls back to a given snapshot.
1077 sub snapshot_rollback
{
1078 my ($class, $vmid, $snapname) = @_;
1084 my $get_snapshot_config = sub {
1087 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
1089 my $res = $conf->{snapshots
}->{$snapname};
1091 die "snapshot '$snapname' does not exist\n" if !defined($res);
1098 my $updatefn = sub {
1099 my $conf = $class->load_config($vmid);
1100 $snap = $get_snapshot_config->($conf);
1103 $class->__snapshot_activate_storages($snap, 1);
1105 $rollback_remove_replication_snapshots->($class, $vmid, $snap, $snapname);
1107 $class->foreach_volume($snap, sub {
1108 my ($vs, $volume) = @_;
1110 $class->__snapshot_rollback_vol_possible($volume, $snapname);
1114 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
1115 if $snap->{snapstate
};
1118 $class->check_lock($conf);
1119 $class->__snapshot_rollback_vm_stop($vmid);
1122 die "unable to rollback vm $vmid: vm is running\n"
1123 if $class->__snapshot_check_running($vmid);
1126 $conf->{lock} = 'rollback';
1128 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
1129 delete $conf->{lock};
1131 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
1133 foreach my $volid (@$unused) {
1134 $class->add_unused_volume($conf, $volid);
1137 # copy snapshot config to current config
1138 $conf = $class->__snapshot_apply_config($conf, $snap);
1139 $conf->{parent
} = $snapname;
1142 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
1144 $class->write_config($vmid, $conf);
1146 if (!$prepare && $snap->{vmstate
}) {
1147 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $data);
1151 $class->lock_config($vmid, $updatefn);
1153 $class->foreach_volume($snap, sub {
1154 my ($vs, $volume) = @_;
1156 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
1160 $class->lock_config($vmid, $updatefn);
1163 # bash completion helper
1166 my ($class, $vmid) = @_;
1168 my $snapshot = eval {
1169 my $conf = $class->load_config($vmid);
1170 my $snapshots = $conf->{snapshots
} || [];
1171 [ sort keys %$snapshots ]