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
489 # the config including snapshots and unused 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);
517 # Returns whether the template parameter is set in $conf.
519 my ($class, $conf) = @_;
521 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
524 # Checks whether $feature is available for the referenced volumes in $conf.
525 # Note: depending on the parameters, some volumes may be skipped!
527 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
528 die "implement me - abstract method\n";
531 # get all replicatable volume (hash $res->{$volid} = 1)
532 # $cleanup: for cleanup - simply ignores volumes without replicate feature
533 # $norerr: never raise exceptions - return undef instead
534 sub get_replicatable_volumes
{
535 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
537 die "implement me - abstract method\n";
540 # Returns all the guests volumes which would be included in a vzdump job
541 # Return Format (array-ref with hash-refs as elements):
544 # key, key in the config, e.g. mp0, scsi0,...
547 # volume_config volume object as returned from foreach_volume()
550 sub get_backup_volumes
{
551 my ($class, $conf) = @_;
553 die "implement me - abstract method\n";
558 # NOTE: Snapshot create/delete involves several non-atomic
559 # actions, and can take a long time.
560 # So we try to avoid locking the file and use the 'lock' variable
561 # inside the config file instead.
563 # Save the vmstate (RAM).
564 sub __snapshot_save_vmstate
{
565 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
566 die "implement me - abstract method\n";
569 # Check whether the VM/CT is running.
570 sub __snapshot_check_running
{
571 my ($class, $vmid) = @_;
572 die "implement me - abstract method\n";
575 # Check whether we need to freeze the VM/CT
576 sub __snapshot_check_freeze_needed
{
577 my ($sself, $vmid, $config, $save_vmstate) = @_;
578 die "implement me - abstract method\n";
581 # Freeze or unfreeze the VM/CT.
582 sub __snapshot_freeze
{
583 my ($class, $vmid, $unfreeze) = @_;
585 die "abstract method - implement me\n";
588 # Code run before and after creating all the volume snapshots
590 sub __snapshot_create_vol_snapshots_hook
{
591 my ($class, $vmid, $snap, $running, $hook) = @_;
596 # Create the volume snapshots for the VM/CT.
597 sub __snapshot_create_vol_snapshot
{
598 my ($class, $vmid, $vs, $volume, $snapname) = @_;
600 die "abstract method - implement me\n";
603 # Remove a drive from the snapshot config.
604 sub __snapshot_delete_remove_drive
{
605 my ($class, $snap, $drive) = @_;
607 die "abstract method - implement me\n";
610 # Delete the vmstate file/drive
611 sub __snapshot_delete_vmstate_file
{
612 my ($class, $snap, $force) = @_;
614 die "abstract method - implement me\n";
617 # Delete a volume snapshot
618 sub __snapshot_delete_vol_snapshot
{
619 my ($class, $vmid, $vs, $volume, $snapname) = @_;
621 die "abstract method - implement me\n";
624 # called during rollback prepare, and between config rollback and starting guest
625 # can change config, e.g. for vmgenid
626 # $data is shared across calls and passed to vm_start
627 sub __snapshot_rollback_hook
{
628 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
633 # Checks whether a volume snapshot is possible for this volume.
634 sub __snapshot_rollback_vol_possible
{
635 my ($class, $volume, $snapname) = @_;
637 die "abstract method - implement me\n";
640 # Rolls back this volume.
641 sub __snapshot_rollback_vol_rollback
{
642 my ($class, $volume, $snapname) = @_;
644 die "abstract method - implement me\n";
647 # Stops the VM/CT for a rollback.
648 sub __snapshot_rollback_vm_stop
{
649 my ($class, $vmid) = @_;
651 die "abstract method - implement me\n";
654 # Start the VM/CT after a rollback with restored vmstate.
655 sub __snapshot_rollback_vm_start
{
656 my ($class, $vmid, $vmstate, $data);
658 die "abstract method - implement me\n";
661 # Get list of volume IDs which are referenced in $conf, but not in $snap.
662 sub __snapshot_rollback_get_unused
{
663 my ($class, $conf, $snap) = @_;
665 die "abstract method - implement me\n";
668 # Copy the current config $source to the snapshot config $dest
669 sub __snapshot_copy_config
{
670 my ($class, $source, $dest) = @_;
672 foreach my $k (keys %$source) {
673 next if $k eq 'snapshots';
674 next if $k eq 'snapstate';
675 next if $k eq 'snaptime';
676 next if $k eq 'vmstate';
677 next if $k eq 'lock';
678 next if $k eq 'digest';
679 next if $k eq 'description';
680 next if $k =~ m/^unused\d+$/;
682 $dest->{$k} = $source->{$k};
686 # Apply the snapshot config $snap to the config $conf (rollback)
687 sub __snapshot_apply_config
{
688 my ($class, $conf, $snap) = @_;
692 snapshots
=> $conf->{snapshots
},
695 # keep description and list of unused disks
696 foreach my $k (keys %$conf) {
697 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
699 $newconf->{$k} = $conf->{$k};
702 $class->__snapshot_copy_config($snap, $newconf);
707 # Prepares the configuration for snapshotting.
708 sub __snapshot_prepare
{
709 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
715 my $conf = $class->load_config($vmid);
717 die "you can't take a snapshot if it's a template\n"
718 if $class->is_template($conf);
720 $class->check_lock($conf);
722 $conf->{lock} = 'snapshot';
724 die "snapshot name '$snapname' already used\n"
725 if defined($conf->{snapshots
}->{$snapname});
727 my $storecfg = PVE
::Storage
::config
();
728 die "snapshot feature is not available\n"
729 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
731 $snap = $conf->{snapshots
}->{$snapname} = {};
733 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
734 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
737 $class->__snapshot_copy_config($conf, $snap);
739 $snap->{snapstate
} = "prepare";
740 $snap->{snaptime
} = time();
741 $snap->{description
} = $comment if $comment;
743 $class->write_config($vmid, $conf);
746 $class->lock_config($vmid, $updatefn);
751 # Commits the configuration after snapshotting.
752 sub __snapshot_commit
{
753 my ($class, $vmid, $snapname) = @_;
757 my $conf = $class->load_config($vmid);
759 die "missing snapshot lock\n"
760 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
762 my $snap = $conf->{snapshots
}->{$snapname};
763 die "snapshot '$snapname' does not exist\n" if !defined($snap);
765 die "wrong snapshot state\n"
766 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
768 delete $snap->{snapstate
};
769 delete $conf->{lock};
771 $conf->{parent
} = $snapname;
773 $class->write_config($vmid, $conf);
776 $class->lock_config($vmid, $updatefn);
779 # Creates a snapshot for the VM/CT.
780 sub snapshot_create
{
781 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
783 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
785 $save_vmstate = 0 if !$snap->{vmstate
};
787 my $conf = $class->load_config($vmid);
789 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
795 $class->__snapshot_freeze($vmid, 0);
798 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
800 $class->foreach_volume($snap, sub {
801 my ($vs, $volume) = @_;
803 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
804 $drivehash->{$vs} = 1;
810 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
812 $class->__snapshot_freeze($vmid, 1);
814 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
818 warn "snapshot create failed: starting cleanup\n";
819 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
824 $class->__snapshot_commit($vmid, $snapname);
827 # Deletes a snapshot.
828 # Note: $drivehash is only set when called from snapshot_create.
829 sub snapshot_delete
{
830 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
836 my $conf = $class->load_config($vmid);
837 my $snap = $conf->{snapshots
}->{$snapname};
839 die "snapshot '$snapname' does not exist\n" if !defined($snap);
841 $class->set_lock($vmid, 'snapshot-delete')
842 if (!$drivehash); # doesn't already have a 'snapshot' lock
844 my $expected_lock = $drivehash ?
'snapshot' : 'snapshot-delete';
846 my $ensure_correct_lock = sub {
849 die "encountered invalid lock, expected '$expected_lock'\n"
850 if !$class->has_lock($conf, $expected_lock);
853 my $unlink_parent = sub {
854 my ($confref, $new_parent) = @_;
856 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
858 $confref->{parent
} = $new_parent;
860 delete $confref->{parent
};
865 my $remove_drive = sub {
868 my $conf = $class->load_config($vmid);
869 $ensure_correct_lock->($conf);
871 $snap = $conf->{snapshots
}->{$snapname};
872 die "snapshot '$snapname' does not exist\n" if !defined($snap);
874 $class->__snapshot_delete_remove_drive($snap, $drive);
876 $class->write_config($vmid, $conf);
880 $class->lock_config($vmid, sub {
881 my $conf = $class->load_config($vmid);
882 $ensure_correct_lock->($conf);
884 die "you can't delete a snapshot if vm is a template\n"
885 if $class->is_template($conf);
887 $snap = $conf->{snapshots
}->{$snapname};
888 die "snapshot '$snapname' does not exist\n" if !defined($snap);
890 $snap->{snapstate
} = 'delete';
892 $class->write_config($vmid, $conf);
895 # now remove vmstate file
896 if ($snap->{vmstate
}) {
897 $class->__snapshot_delete_vmstate_file($snap, $force);
899 # save changes (remove vmstate from snapshot)
900 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
903 # now remove all volume snapshots
904 $class->foreach_volume($snap, sub {
905 my ($vs, $volume) = @_;
907 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
908 if (!$drivehash || $drivehash->{$vs}) {
909 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
916 # save changes (remove drive from snapshot)
917 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
921 $class->lock_config($vmid, sub {
922 my $conf = $class->load_config($vmid);
923 $ensure_correct_lock->($conf);
925 $snap = $conf->{snapshots
}->{$snapname};
926 die "snapshot '$snapname' does not exist\n" if !defined($snap);
929 &$unlink_parent($conf, $snap->{parent
});
930 foreach my $sn (keys %{$conf->{snapshots
}}) {
931 next if $sn eq $snapname;
932 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
936 delete $conf->{snapshots
}->{$snapname};
937 delete $conf->{lock};
938 foreach my $volid (@$unused) {
939 $class->add_unused_volume($conf, $volid);
942 $class->write_config($vmid, $conf);
946 # Rolls back to a given snapshot.
947 sub snapshot_rollback
{
948 my ($class, $vmid, $snapname) = @_;
952 my $storecfg = PVE
::Storage
::config
();
956 my $get_snapshot_config = sub {
959 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
961 my $res = $conf->{snapshots
}->{$snapname};
963 die "snapshot '$snapname' does not exist\n" if !defined($res);
971 my $conf = $class->load_config($vmid);
972 $snap = $get_snapshot_config->($conf);
975 my $repl_conf = PVE
::ReplicationConfig-
>new();
976 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
977 # remove all replication snapshots
978 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
979 my $sorted_volids = [ sort keys %$volumes ];
981 # remove all local replication snapshots (jobid => undef)
982 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
983 PVE
::Replication
::prepare
($storecfg, $sorted_volids, undef, 1, undef, $logfunc);
986 $class->foreach_volume($snap, sub {
987 my ($vs, $volume) = @_;
989 $class->__snapshot_rollback_vol_possible($volume, $snapname);
993 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
994 if $snap->{snapstate
};
997 $class->check_lock($conf);
998 $class->__snapshot_rollback_vm_stop($vmid);
1001 die "unable to rollback vm $vmid: vm is running\n"
1002 if $class->__snapshot_check_running($vmid);
1005 $conf->{lock} = 'rollback';
1007 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
1008 delete $conf->{lock};
1010 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
1012 foreach my $volid (@$unused) {
1013 $class->add_unused_volume($conf, $volid);
1016 # copy snapshot config to current config
1017 $conf = $class->__snapshot_apply_config($conf, $snap);
1018 $conf->{parent
} = $snapname;
1021 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
1023 $class->write_config($vmid, $conf);
1025 if (!$prepare && $snap->{vmstate
}) {
1026 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $data);
1030 $class->lock_config($vmid, $updatefn);
1032 $class->foreach_volume($snap, sub {
1033 my ($vs, $volume) = @_;
1035 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
1039 $class->lock_config($vmid, $updatefn);
1042 # bash completion helper
1045 my ($class, $vmid) = @_;
1047 my $snapshot = eval {
1048 my $conf = $class->load_config($vmid);
1049 my $snapshots = $conf->{snapshots
} || [];
1050 [ sort keys %$snapshots ]