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 =~ /^(!?)(.*)$/;
86 $pending_deletions->{$key} = {
87 force
=> $force ?
1 : 0,
91 return $pending_deletions;
94 sub print_pending_delete
{
95 my ($class, $delete_hash) = @_;
97 my $render_key = sub {
99 $key = "!$key" if $delete_hash->{$key}->{force
};
103 join (',', map { $render_key->($_) } sort keys %$delete_hash);
106 sub add_to_pending_delete
{
107 my ($class, $conf, $key, $force) = @_;
109 $conf->{pending
} //= {};
110 my $pending = $conf->{pending
};
111 my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
113 $pending_delete_hash->{$key} = { force
=> $force };
115 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
120 sub remove_from_pending_delete
{
121 my ($class, $conf, $key) = @_;
123 my $pending = $conf->{pending
};
124 my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
126 return $conf if ! exists $pending_delete_hash->{$key};
128 delete $pending_delete_hash->{$key};
130 if (%$pending_delete_hash) {
131 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
133 delete $pending->{delete};
139 sub cleanup_pending
{
140 my ($class, $conf) = @_;
142 my $pending = $conf->{pending
};
143 # remove pending changes when nothing changed
145 foreach my $opt (keys %{$conf->{pending
}}) {
146 next if $opt eq 'delete'; # just to be sure
147 if (defined($conf->{$opt}) && ($pending->{$opt} eq $conf->{$opt})) {
149 delete $pending->{$opt};
153 my $current_delete_hash = $class->parse_pending_delete($pending->{delete});
154 my $pending_delete_hash = {};
155 for my $opt (keys %$current_delete_hash) {
156 if (defined($conf->{$opt})) {
157 $pending_delete_hash->{$opt} = $current_delete_hash->{$opt};
163 if (%$pending_delete_hash) {
164 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
166 delete $pending->{delete};
172 sub get_partial_fast_plug_option
{
175 die "abstract method - implement me ";
178 sub partial_fast_plug
{
179 my ($class, $conf, $opt) = @_;
181 my $partial_fast_plug_option = $class->get_partial_fast_plug_option();
182 return 0 if !$partial_fast_plug_option->{$opt};
184 my $format = $partial_fast_plug_option->{$opt}->{fmt
};
185 my $fast_pluggable = $partial_fast_plug_option->{$opt}->{properties
};
188 if (exists($conf->{$opt})) {
189 $configured = PVE
::JSONSchema
::parse_property_string
($format, $conf->{$opt});
191 my $pending = PVE
::JSONSchema
::parse_property_string
($format, $conf->{pending
}->{$opt});
195 # merge configured and pending opts to iterate
196 my @all_keys = keys %{{ %$pending, %$configured }};
198 foreach my $subopt (@all_keys) {
199 my $type = $format->{$subopt}->{type
};
200 if (typesafe_ne
($configured->{$subopt}, $pending->{$subopt}, $type)) {
201 if ($fast_pluggable->{$subopt}) {
202 $configured->{$subopt} = $pending->{$subopt};
208 # if there're no keys in $configured (after merge) there shouldn't be anything to change
209 if (keys %$configured) {
210 $conf->{$opt} = PVE
::JSONSchema
::print_property_string
($configured, $format);
217 sub load_snapshot_config
{
218 my ($class, $vmid, $snapname) = @_;
220 my $conf = $class->load_config($vmid);
222 my $snapshot = $conf->{snapshots
}->{$snapname};
223 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
225 $snapshot->{digest
} = $conf->{digest
};
231 sub load_current_config
{
232 my ($class, $vmid, $current) = @_;
234 my $conf = $class->load_config($vmid);
236 # take pending changes in
238 foreach my $opt (keys %{$conf->{pending
}}) {
239 next if $opt eq 'delete';
240 my $value = $conf->{pending
}->{$opt};
241 next if ref($value); # just to be sure
242 $conf->{$opt} = $value;
244 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
245 foreach my $opt (keys %$pending_delete_hash) {
246 delete $conf->{$opt} if $conf->{$opt};
250 delete $conf->{snapshots
};
251 delete $conf->{pending
};
257 # Lock config file using flock, run $code with @param, unlock config file.
258 # $timeout is the maximum time to acquire the flock
259 sub lock_config_full
{
260 my ($class, $vmid, $timeout, $code, @param) = @_;
262 my $filename = $class->config_file_lock($vmid);
264 my $res = lock_file
($filename, $timeout, $code, @param);
271 sub create_and_lock_config
{
272 my ($class, $vmid, $allow_existing, $lock) = @_;
274 $class->lock_config_full($vmid, 5, sub {
275 PVE
::Cluster
::check_vmid_unused
($vmid, $allow_existing);
277 my $conf = eval { $class->load_config($vmid) } || {};
278 $class->check_lock($conf);
279 $conf->{lock} = $lock // 'create';
280 $class->write_config($vmid, $conf);
284 # destroys configuration, only applicable for configs owned by the callers node.
285 # dies if removal fails, e.g., when inquorate.
287 my ($class, $vmid) = @_;
289 my $config_fn = $class->config_file($vmid, $nodename);
290 unlink $config_fn or die "failed to remove config file: $!\n";
293 # Lock config file using flock, run $code with @param, unlock config file.
294 # $timeout is the maximum time to acquire the flock
295 # $shared eq 1 creates a non-exclusive ("read") flock
296 sub lock_config_mode
{
297 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
299 my $filename = $class->config_file_lock($vmid);
301 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
308 # Lock config file using flock, run $code with @param, unlock config file.
310 my ($class, $vmid, $code, @param) = @_;
312 return $class->lock_config_full($vmid, 10, $code, @param);
315 # Checks whether the config is locked with the lock parameter
317 my ($class, $conf) = @_;
319 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
322 # Returns whether the config is locked with the lock parameter, also checks
323 # whether the lock value is correct if the optional $lock is set.
325 my ($class, $conf, $lock) = @_;
327 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
330 # Sets the lock parameter for this VM/CT's config to $lock.
332 my ($class, $vmid, $lock) = @_;
335 $class->lock_config($vmid, sub {
336 $conf = $class->load_config($vmid);
337 $class->check_lock($conf);
338 $conf->{lock} = $lock;
339 $class->write_config($vmid, $conf);
344 # Removes the lock parameter for this VM/CT's config, also checks whether
345 # the lock value is correct if the optional $lock is set.
347 my ($class, $vmid, $lock) = @_;
349 $class->lock_config($vmid, sub {
350 my $conf = $class->load_config($vmid);
351 if (!$conf->{lock}) {
352 my $lockstring = defined($lock) ?
"'$lock' " : "any";
353 die "no lock found trying to remove $lockstring lock\n";
354 } elsif (defined($lock) && $conf->{lock} ne $lock) {
355 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
357 delete $conf->{lock};
358 $class->write_config($vmid, $conf);
362 # Checks whether protection mode is enabled for this VM/CT.
363 sub check_protection
{
364 my ($class, $conf, $err_msg) = @_;
366 if ($conf->{protection
}) {
367 die "$err_msg - protection mode enabled\n";
371 # Returns a list of keys where used volumes can be located
372 sub valid_volume_keys
{
373 my ($class, $reverse) = @_;
375 die "implement me - abstract method\n";
378 # Returns a hash with the information from $volume_string
379 # $key is used to determine the format of the string
381 my ($class, $key, $volume_string, $noerr) = @_;
383 die "implement me - abstract method\n";
386 # Returns a string with the information from $volume
387 # $key is used to determine the format of the string
389 my ($class, $key, $volume) = @_;
391 die "implement me - abstract method\n";
394 # The key under which the volume ID is located in volume hashes
398 die "implement me - abstract method\n";
401 # Adds an unused volume to $config, if possible.
402 sub add_unused_volume
{
403 my ($class, $config, $volid) = @_;
406 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
407 my $test = "unused$ind";
408 if (my $vid = $config->{$test}) {
409 return if $vid eq $volid; # do not add duplicates
415 die "Too many unused volumes - please delete them first.\n" if !$key;
417 $config->{$key} = $volid;
422 # Iterate over all unused volumes, calling $func for each key/value pair
423 # with additional parameters @param.
424 sub foreach_unused_volume
{
425 my ($class, $conf, $func, @param) = @_;
427 foreach my $key (keys %{$conf}) {
428 if ($key =~ m/^unused\d+$/) {
429 my $volume = $class->parse_volume($key, $conf->{$key});
430 $func->($key, $volume, @param);
435 # Iterate over all configured volumes, calling $func for each key/value pair
436 # with additional parameters @param.
437 # By default, unused volumes and specials like vmstate are excluded.
438 # Options: reverse - reverses the order for the iteration
439 # include_unused - also iterate over unused volumes
440 # extra_keys - an array of extra keys to use for the iteration
441 sub foreach_volume_full
{
442 my ($class, $conf, $opts, $func, @param) = @_;
444 die "'reverse' iteration only supported for default keys\n"
445 if $opts->{reverse} && ($opts->{extra_keys
} || $opts->{include_unused
});
447 my @keys = $class->valid_volume_keys($opts->{reverse});
448 push @keys, @{$opts->{extra_keys
}} if $opts->{extra_keys
};
450 foreach my $key (@keys) {
451 my $volume_string = $conf->{$key};
452 next if !defined($volume_string);
454 my $volume = $class->parse_volume($key, $volume_string, 1);
455 next if !defined($volume);
457 $func->($key, $volume, @param);
460 $class->foreach_unused_volume($conf, $func, @param) if $opts->{include_unused
};
464 my ($class, $conf, $func, @param) = @_;
466 return $class->foreach_volume_full($conf, undef, $func, @param);
469 # Returns whether the template parameter is set in $conf.
471 my ($class, $conf) = @_;
473 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
476 # Checks whether $feature is available for the referenced volumes in $conf.
477 # Note: depending on the parameters, some volumes may be skipped!
479 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
480 die "implement me - abstract method\n";
483 # get all replicatable volume (hash $res->{$volid} = 1)
484 # $cleanup: for cleanup - simply ignores volumes without replicate feature
485 # $norerr: never raise exceptions - return undef instead
486 sub get_replicatable_volumes
{
487 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
489 die "implement me - abstract method\n";
492 # Returns all the guests volumes which would be included in a vzdump job
493 # Return Format (array-ref with hash-refs as elements):
496 # key, key in the config, e.g. mp0, scsi0,...
499 # data volume object as returned from foreach_drive/foreach_mountpoint
502 sub get_backup_volumes
{
503 my ($class, $conf) = @_;
505 die "implement me - abstract method\n";
510 # NOTE: Snapshot create/delete involves several non-atomic
511 # actions, and can take a long time.
512 # So we try to avoid locking the file and use the 'lock' variable
513 # inside the config file instead.
515 # Save the vmstate (RAM).
516 sub __snapshot_save_vmstate
{
517 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
518 die "implement me - abstract method\n";
521 # Check whether the VM/CT is running.
522 sub __snapshot_check_running
{
523 my ($class, $vmid) = @_;
524 die "implement me - abstract method\n";
527 # Check whether we need to freeze the VM/CT
528 sub __snapshot_check_freeze_needed
{
529 my ($sself, $vmid, $config, $save_vmstate) = @_;
530 die "implement me - abstract method\n";
533 # Freeze or unfreeze the VM/CT.
534 sub __snapshot_freeze
{
535 my ($class, $vmid, $unfreeze) = @_;
537 die "abstract method - implement me\n";
540 # Code run before and after creating all the volume snapshots
542 sub __snapshot_create_vol_snapshots_hook
{
543 my ($class, $vmid, $snap, $running, $hook) = @_;
548 # Create the volume snapshots for the VM/CT.
549 sub __snapshot_create_vol_snapshot
{
550 my ($class, $vmid, $vs, $volume, $snapname) = @_;
552 die "abstract method - implement me\n";
555 # Remove a drive from the snapshot config.
556 sub __snapshot_delete_remove_drive
{
557 my ($class, $snap, $drive) = @_;
559 die "abstract method - implement me\n";
562 # Delete the vmstate file/drive
563 sub __snapshot_delete_vmstate_file
{
564 my ($class, $snap, $force) = @_;
566 die "abstract method - implement me\n";
569 # Delete a volume snapshot
570 sub __snapshot_delete_vol_snapshot
{
571 my ($class, $vmid, $vs, $volume, $snapname) = @_;
573 die "abstract method - implement me\n";
576 # called during rollback prepare, and between config rollback and starting guest
577 # can change config, e.g. for vmgenid
578 # $data is shared across calls and passed to vm_start
579 sub __snapshot_rollback_hook
{
580 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
585 # Checks whether a volume snapshot is possible for this volume.
586 sub __snapshot_rollback_vol_possible
{
587 my ($class, $volume, $snapname) = @_;
589 die "abstract method - implement me\n";
592 # Rolls back this volume.
593 sub __snapshot_rollback_vol_rollback
{
594 my ($class, $volume, $snapname) = @_;
596 die "abstract method - implement me\n";
599 # Stops the VM/CT for a rollback.
600 sub __snapshot_rollback_vm_stop
{
601 my ($class, $vmid) = @_;
603 die "abstract method - implement me\n";
606 # Start the VM/CT after a rollback with restored vmstate.
607 sub __snapshot_rollback_vm_start
{
608 my ($class, $vmid, $vmstate, $data);
610 die "abstract method - implement me\n";
613 # Get list of volume IDs which are referenced in $conf, but not in $snap.
614 sub __snapshot_rollback_get_unused
{
615 my ($class, $conf, $snap) = @_;
617 die "abstract method - implement me\n";
620 # Copy the current config $source to the snapshot config $dest
621 sub __snapshot_copy_config
{
622 my ($class, $source, $dest) = @_;
624 foreach my $k (keys %$source) {
625 next if $k eq 'snapshots';
626 next if $k eq 'snapstate';
627 next if $k eq 'snaptime';
628 next if $k eq 'vmstate';
629 next if $k eq 'lock';
630 next if $k eq 'digest';
631 next if $k eq 'description';
632 next if $k =~ m/^unused\d+$/;
634 $dest->{$k} = $source->{$k};
638 # Apply the snapshot config $snap to the config $conf (rollback)
639 sub __snapshot_apply_config
{
640 my ($class, $conf, $snap) = @_;
644 snapshots
=> $conf->{snapshots
},
647 # keep description and list of unused disks
648 foreach my $k (keys %$conf) {
649 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
651 $newconf->{$k} = $conf->{$k};
654 $class->__snapshot_copy_config($snap, $newconf);
659 # Prepares the configuration for snapshotting.
660 sub __snapshot_prepare
{
661 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
667 my $conf = $class->load_config($vmid);
669 die "you can't take a snapshot if it's a template\n"
670 if $class->is_template($conf);
672 $class->check_lock($conf);
674 $conf->{lock} = 'snapshot';
676 die "snapshot name '$snapname' already used\n"
677 if defined($conf->{snapshots
}->{$snapname});
679 my $storecfg = PVE
::Storage
::config
();
680 die "snapshot feature is not available\n"
681 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
683 $snap = $conf->{snapshots
}->{$snapname} = {};
685 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
686 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
689 $class->__snapshot_copy_config($conf, $snap);
691 $snap->{snapstate
} = "prepare";
692 $snap->{snaptime
} = time();
693 $snap->{description
} = $comment if $comment;
695 $class->write_config($vmid, $conf);
698 $class->lock_config($vmid, $updatefn);
703 # Commits the configuration after snapshotting.
704 sub __snapshot_commit
{
705 my ($class, $vmid, $snapname) = @_;
709 my $conf = $class->load_config($vmid);
711 die "missing snapshot lock\n"
712 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
714 my $snap = $conf->{snapshots
}->{$snapname};
715 die "snapshot '$snapname' does not exist\n" if !defined($snap);
717 die "wrong snapshot state\n"
718 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
720 delete $snap->{snapstate
};
721 delete $conf->{lock};
723 $conf->{parent
} = $snapname;
725 $class->write_config($vmid, $conf);
728 $class->lock_config($vmid, $updatefn);
731 # Creates a snapshot for the VM/CT.
732 sub snapshot_create
{
733 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
735 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
737 $save_vmstate = 0 if !$snap->{vmstate
};
739 my $conf = $class->load_config($vmid);
741 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
747 $class->__snapshot_freeze($vmid, 0);
750 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
752 $class->foreach_volume($snap, sub {
753 my ($vs, $volume) = @_;
755 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
756 $drivehash->{$vs} = 1;
762 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
764 $class->__snapshot_freeze($vmid, 1);
766 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
770 warn "snapshot create failed: starting cleanup\n";
771 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
776 $class->__snapshot_commit($vmid, $snapname);
779 # Deletes a snapshot.
780 # Note: $drivehash is only set when called from snapshot_create.
781 sub snapshot_delete
{
782 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
788 my $conf = $class->load_config($vmid);
789 my $snap = $conf->{snapshots
}->{$snapname};
791 die "snapshot '$snapname' does not exist\n" if !defined($snap);
793 $class->set_lock($vmid, 'snapshot-delete')
794 if (!$drivehash); # doesn't already have a 'snapshot' lock
796 my $unlink_parent = sub {
797 my ($confref, $new_parent) = @_;
799 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
801 $confref->{parent
} = $new_parent;
803 delete $confref->{parent
};
808 my $remove_drive = sub {
811 my $conf = $class->load_config($vmid);
812 $snap = $conf->{snapshots
}->{$snapname};
813 die "snapshot '$snapname' does not exist\n" if !defined($snap);
815 $class->__snapshot_delete_remove_drive($snap, $drive);
817 $class->write_config($vmid, $conf);
821 $class->lock_config($vmid, sub {
822 my $conf = $class->load_config($vmid);
824 die "you can't delete a snapshot if vm is a template\n"
825 if $class->is_template($conf);
827 $snap = $conf->{snapshots
}->{$snapname};
828 die "snapshot '$snapname' does not exist\n" if !defined($snap);
830 $snap->{snapstate
} = 'delete';
832 $class->write_config($vmid, $conf);
835 # now remove vmstate file
836 if ($snap->{vmstate
}) {
837 $class->__snapshot_delete_vmstate_file($snap, $force);
839 # save changes (remove vmstate from snapshot)
840 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
843 # now remove all volume snapshots
844 $class->foreach_volume($snap, sub {
845 my ($vs, $volume) = @_;
847 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
848 if (!$drivehash || $drivehash->{$vs}) {
849 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
856 # save changes (remove drive from snapshot)
857 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
861 $class->lock_config($vmid, sub {
862 my $conf = $class->load_config($vmid);
863 $snap = $conf->{snapshots
}->{$snapname};
864 die "snapshot '$snapname' does not exist\n" if !defined($snap);
867 &$unlink_parent($conf, $snap->{parent
});
868 foreach my $sn (keys %{$conf->{snapshots
}}) {
869 next if $sn eq $snapname;
870 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
874 delete $conf->{snapshots
}->{$snapname};
875 delete $conf->{lock};
876 foreach my $volid (@$unused) {
877 $class->add_unused_volume($conf, $volid);
880 $class->write_config($vmid, $conf);
884 # Rolls back to a given snapshot.
885 sub snapshot_rollback
{
886 my ($class, $vmid, $snapname) = @_;
890 my $storecfg = PVE
::Storage
::config
();
892 my $conf = $class->load_config($vmid);
894 my $get_snapshot_config = sub {
896 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
898 my $res = $conf->{snapshots
}->{$snapname};
900 die "snapshot '$snapname' does not exist\n" if !defined($res);
905 my $repl_conf = PVE
::ReplicationConfig-
>new();
906 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
907 # remove all replication snapshots
908 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
909 my $sorted_volids = [ sort keys %$volumes ];
911 # remove all local replication snapshots (jobid => undef)
912 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
913 PVE
::Replication
::prepare
($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
916 my $snap = &$get_snapshot_config();
918 $class->foreach_volume($snap, sub {
919 my ($vs, $volume) = @_;
921 $class->__snapshot_rollback_vol_possible($volume, $snapname);
928 $conf = $class->load_config($vmid);
930 $snap = &$get_snapshot_config();
932 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
933 if $snap->{snapstate
};
936 $class->check_lock($conf);
937 $class->__snapshot_rollback_vm_stop($vmid);
940 die "unable to rollback vm $vmid: vm is running\n"
941 if $class->__snapshot_check_running($vmid);
944 $conf->{lock} = 'rollback';
946 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
947 delete $conf->{lock};
949 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
951 foreach my $volid (@$unused) {
952 $class->add_unused_volume($conf, $volid);
955 # copy snapshot config to current config
956 $conf = $class->__snapshot_apply_config($conf, $snap);
957 $conf->{parent
} = $snapname;
960 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
962 $class->write_config($vmid, $conf);
964 if (!$prepare && $snap->{vmstate
}) {
965 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $data);
969 $class->lock_config($vmid, $updatefn);
971 $class->foreach_volume($snap, sub {
972 my ($vs, $volume) = @_;
974 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
978 $class->lock_config($vmid, $updatefn);
981 # bash completion helper
984 my ($class, $vmid) = @_;
986 my $snapshot = eval {
987 my $conf = $class->load_config($vmid);
988 my $snapshots = $conf->{snapshots
} || [];
989 [ sort keys %$snapshots ]