]> git.proxmox.com Git - pve-guest-common.git/blob - PVE/AbstractConfig.pm
refactor print_pending_delete slightly
[pve-guest-common.git] / PVE / AbstractConfig.pm
1 package PVE::AbstractConfig;
2
3 use strict;
4 use warnings;
5
6 use PVE::Tools qw(lock_file lock_file_full);
7 use PVE::INotify;
8 use PVE::Cluster;
9 use PVE::Storage;
10
11 use PVE::ReplicationConfig;
12 use PVE::Replication;
13
14 my $nodename = PVE::INotify::nodename();
15
16 # Printable string, currently either "VM" or "CT"
17 sub guest_type {
18 my ($class) = @_;
19 die "abstract method - implement me ";
20 }
21
22 sub __config_max_unused_disks {
23 my ($class) = @_;
24
25 die "implement me - abstract method\n";
26 }
27
28 # Path to the flock file for this VM/CT
29 sub config_file_lock {
30 my ($class, $vmid) = @_;
31 die "abstract method - implement me";
32 }
33
34 # Relative config file path for this VM/CT in CFS
35 sub cfs_config_path {
36 my ($class, $vmid, $node) = @_;
37 die "abstract method - implement me";
38 }
39
40 # Absolute config file path for this VM/CT
41 sub config_file {
42 my ($class, $vmid, $node) = @_;
43
44 my $cfspath = $class->cfs_config_path($vmid, $node);
45 return "/etc/pve/$cfspath";
46 }
47
48 # Read and parse config file for this VM/CT
49 sub load_config {
50 my ($class, $vmid, $node) = @_;
51
52 $node = $nodename if !$node;
53 my $cfspath = $class->cfs_config_path($vmid, $node);
54
55 my $conf = PVE::Cluster::cfs_read_file($cfspath);
56 die "Configuration file '$cfspath' does not exist\n"
57 if !defined($conf);
58
59 return $conf;
60 }
61
62 # Generate and write config file for this VM/CT
63 sub write_config {
64 my ($class, $vmid, $conf) = @_;
65
66 my $cfspath = $class->cfs_config_path($vmid);
67
68 PVE::Cluster::cfs_write_file($cfspath, $conf);
69 }
70
71 # Pending changes related
72
73 sub parse_pending_delete {
74 my ($class, $data) = @_;
75
76 return {} if !$data;
77
78 $data =~ s/[,;]/ /g;
79 $data =~ s/^\s+//;
80
81 my $pending_deletions = {};
82 for my $entry (split(/\s+/, $data)) {
83 my ($force, $key) = $entry =~ /^(!?)(.*)$/;
84
85 $pending_deletions->{$key} = {
86 force => $force ? 1 : 0,
87 };
88 }
89
90 return $pending_deletions;
91 }
92
93 sub print_pending_delete {
94 my ($class, $delete_hash) = @_;
95
96 my $render_key = sub {
97 my $key = shift;
98 $key = "!$key" if $delete_hash->{$key}->{force};
99 return $key;
100 };
101
102 join (',', map { $render_key->($_) } keys %$delete_hash);
103 }
104
105 sub add_to_pending_delete {
106 my ($class, $conf, $key, $force) = @_;
107
108 delete $conf->{pending}->{$key};
109 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
110 $pending_delete_hash->{$key}->{force} = $force;
111 $conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash);
112 }
113
114 sub remove_from_pending_delete {
115 my ($class, $conf, $key) = @_;
116
117 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
118 delete $pending_delete_hash->{$key};
119
120 if (%$pending_delete_hash) {
121 $conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash);
122 } else {
123 delete $conf->{pending}->{delete};
124 }
125 }
126
127 sub cleanup_pending {
128 my ($class, $conf) = @_;
129
130 # remove pending changes when nothing changed
131 my $changes;
132 foreach my $opt (keys %{$conf->{pending}}) {
133 next if $opt eq 'delete'; # just to be sure
134 if (defined($conf->{$opt}) && ($conf->{pending}->{$opt} eq $conf->{$opt})) {
135 $changes = 1;
136 delete $conf->{pending}->{$opt};
137 }
138 }
139
140 my $current_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
141 my $pending_delete_hash = {};
142 while (my ($opt, $force) = each %$current_delete_hash) {
143 if (defined($conf->{$opt})) {
144 $pending_delete_hash->{$opt} = $force;
145 } else {
146 $changes = 1;
147 }
148 }
149
150 if (%$pending_delete_hash) {
151 $conf->{pending}->{delete} = $class->print_pending_delete($pending_delete_hash);
152 } else {
153 delete $conf->{pending}->{delete};
154 }
155
156 return $changes;
157 }
158
159 sub load_snapshot_config {
160 my ($class, $vmid, $snapname) = @_;
161
162 my $conf = $class->load_config($vmid);
163
164 my $snapshot = $conf->{snapshots}->{$snapname};
165 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
166
167 $snapshot->{digest} = $conf->{digest};
168
169 return $snapshot;
170
171 }
172
173 sub load_current_config {
174 my ($class, $vmid, $current) = @_;
175
176 my $conf = $class->load_config($vmid);
177
178 # take pending changes in
179 if (!$current) {
180 foreach my $opt (keys %{$conf->{pending}}) {
181 next if $opt eq 'delete';
182 my $value = $conf->{pending}->{$opt};
183 next if ref($value); # just to be sure
184 $conf->{$opt} = $value;
185 }
186 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
187 foreach my $opt (keys %$pending_delete_hash) {
188 delete $conf->{$opt} if $conf->{$opt};
189 }
190 }
191
192 delete $conf->{snapshots};
193 delete $conf->{pending};
194
195 return $conf;
196 }
197
198
199 # Lock config file using flock, run $code with @param, unlock config file.
200 # $timeout is the maximum time to aquire the flock
201 sub lock_config_full {
202 my ($class, $vmid, $timeout, $code, @param) = @_;
203
204 my $filename = $class->config_file_lock($vmid);
205
206 my $res = lock_file($filename, $timeout, $code, @param);
207
208 die $@ if $@;
209
210 return $res;
211 }
212
213 sub create_and_lock_config {
214 my ($class, $vmid, $allow_existing) = @_;
215
216 $class->lock_config_full($vmid, 5, sub {
217 PVE::Cluster::check_vmid_unused($vmid, $allow_existing);
218
219 my $conf = eval { $class->load_config($vmid) } || {};
220 $class->check_lock($conf);
221 $conf->{lock} = 'create';
222 $class->write_config($vmid, $conf);
223 });
224 }
225
226 # destroys configuration, only applyable for configs owned by the callers node.
227 # dies if removal fails, e.g., when inquorate.
228 sub destroy_config {
229 my ($class, $vmid) = @_;
230
231 my $config_fn = $class->config_file($vmid, $nodename);
232 unlink $config_fn or die "failed to remove config file: $!\n";
233 }
234
235 # Lock config file using flock, run $code with @param, unlock config file.
236 # $timeout is the maximum time to aquire the flock
237 # $shared eq 1 creates a non-exclusive ("read") flock
238 sub lock_config_mode {
239 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
240
241 my $filename = $class->config_file_lock($vmid);
242
243 my $res = lock_file_full($filename, $timeout, $shared, $code, @param);
244
245 die $@ if $@;
246
247 return $res;
248 }
249
250 # Lock config file using flock, run $code with @param, unlock config file.
251 sub lock_config {
252 my ($class, $vmid, $code, @param) = @_;
253
254 return $class->lock_config_full($vmid, 10, $code, @param);
255 }
256
257 # Checks whether the config is locked with the lock parameter
258 sub check_lock {
259 my ($class, $conf) = @_;
260
261 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
262 }
263
264 # Returns whether the config is locked with the lock parameter, also checks
265 # whether the lock value is correct if the optional $lock is set.
266 sub has_lock {
267 my ($class, $conf, $lock) = @_;
268
269 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
270 }
271
272 # Sets the lock parameter for this VM/CT's config to $lock.
273 sub set_lock {
274 my ($class, $vmid, $lock) = @_;
275
276 my $conf;
277 $class->lock_config($vmid, sub {
278 $conf = $class->load_config($vmid);
279 $class->check_lock($conf);
280 $conf->{lock} = $lock;
281 $class->write_config($vmid, $conf);
282 });
283 return $conf;
284 }
285
286 # Removes the lock parameter for this VM/CT's config, also checks whether
287 # the lock value is correct if the optional $lock is set.
288 sub remove_lock {
289 my ($class, $vmid, $lock) = @_;
290
291 $class->lock_config($vmid, sub {
292 my $conf = $class->load_config($vmid);
293 if (!$conf->{lock}) {
294 my $lockstring = defined($lock) ? "'$lock' " : "any";
295 die "no lock found trying to remove $lockstring lock\n";
296 } elsif (defined($lock) && $conf->{lock} ne $lock) {
297 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
298 }
299 delete $conf->{lock};
300 $class->write_config($vmid, $conf);
301 });
302 }
303
304 # Checks whether protection mode is enabled for this VM/CT.
305 sub check_protection {
306 my ($class, $conf, $err_msg) = @_;
307
308 if ($conf->{protection}) {
309 die "$err_msg - protection mode enabled\n";
310 }
311 }
312
313 # Adds an unused volume to $config, if possible.
314 sub add_unused_volume {
315 my ($class, $config, $volid) = @_;
316
317 my $key;
318 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
319 my $test = "unused$ind";
320 if (my $vid = $config->{$test}) {
321 return if $vid eq $volid; # do not add duplicates
322 } else {
323 $key = $test;
324 }
325 }
326
327 die "Too many unused volumes - please delete them first.\n" if !$key;
328
329 $config->{$key} = $volid;
330
331 return $key;
332 }
333
334 # Returns whether the template parameter is set in $conf.
335 sub is_template {
336 my ($class, $conf) = @_;
337
338 return 1 if defined $conf->{template} && $conf->{template} == 1;
339 }
340
341 # Checks whether $feature is availabe for the referenced volumes in $conf.
342 # Note: depending on the parameters, some volumes may be skipped!
343 sub has_feature {
344 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
345 die "implement me - abstract method\n";
346 }
347
348 # get all replicatable volume (hash $res->{$volid} = 1)
349 # $cleanup: for cleanup - simply ignores volumes without replicate feature
350 # $norerr: never raise exceptions - return undef instead
351 sub get_replicatable_volumes {
352 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
353
354 die "implement me - abstract method\n";
355 }
356
357 # Internal snapshots
358
359 # NOTE: Snapshot create/delete involves several non-atomic
360 # actions, and can take a long time.
361 # So we try to avoid locking the file and use the 'lock' variable
362 # inside the config file instead.
363
364 # Save the vmstate (RAM).
365 sub __snapshot_save_vmstate {
366 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
367 die "implement me - abstract method\n";
368 }
369
370 # Check whether the VM/CT is running.
371 sub __snapshot_check_running {
372 my ($class, $vmid) = @_;
373 die "implement me - abstract method\n";
374 }
375
376 # Check whether we need to freeze the VM/CT
377 sub __snapshot_check_freeze_needed {
378 my ($sself, $vmid, $config, $save_vmstate) = @_;
379 die "implement me - abstract method\n";
380 }
381
382 # Freeze or unfreeze the VM/CT.
383 sub __snapshot_freeze {
384 my ($class, $vmid, $unfreeze) = @_;
385
386 die "abstract method - implement me\n";
387 }
388
389 # Code run before and after creating all the volume snapshots
390 # base: noop
391 sub __snapshot_create_vol_snapshots_hook {
392 my ($class, $vmid, $snap, $running, $hook) = @_;
393
394 return;
395 }
396
397 # Create the volume snapshots for the VM/CT.
398 sub __snapshot_create_vol_snapshot {
399 my ($class, $vmid, $vs, $volume, $snapname) = @_;
400
401 die "abstract method - implement me\n";
402 }
403
404 # Remove a drive from the snapshot config.
405 sub __snapshot_delete_remove_drive {
406 my ($class, $snap, $drive) = @_;
407
408 die "abstract method - implement me\n";
409 }
410
411 # Delete the vmstate file/drive
412 sub __snapshot_delete_vmstate_file {
413 my ($class, $snap, $force) = @_;
414
415 die "abstract method - implement me\n";
416 }
417
418 # Delete a volume snapshot
419 sub __snapshot_delete_vol_snapshot {
420 my ($class, $vmid, $vs, $volume, $snapname) = @_;
421
422 die "abstract method - implement me\n";
423 }
424
425 # called during rollback prepare, and between config rollback and starting guest
426 # can change config, e.g. for vmgenid
427 # $data is shared across calls and passed to vm_start
428 sub __snapshot_rollback_hook {
429 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
430
431 return;
432 }
433
434 # Checks whether a volume snapshot is possible for this volume.
435 sub __snapshot_rollback_vol_possible {
436 my ($class, $volume, $snapname) = @_;
437
438 die "abstract method - implement me\n";
439 }
440
441 # Rolls back this volume.
442 sub __snapshot_rollback_vol_rollback {
443 my ($class, $volume, $snapname) = @_;
444
445 die "abstract method - implement me\n";
446 }
447
448 # Stops the VM/CT for a rollback.
449 sub __snapshot_rollback_vm_stop {
450 my ($class, $vmid) = @_;
451
452 die "abstract method - implement me\n";
453 }
454
455 # Start the VM/CT after a rollback with restored vmstate.
456 sub __snapshot_rollback_vm_start {
457 my ($class, $vmid, $vmstate, $data);
458
459 die "abstract method - implement me\n";
460 }
461
462 # Get list of volume IDs which are referenced in $conf, but not in $snap.
463 sub __snapshot_rollback_get_unused {
464 my ($class, $conf, $snap) = @_;
465
466 die "abstract method - implement me\n";
467 }
468
469 # Iterate over all configured volumes, calling $func for each key/value pair.
470 sub __snapshot_foreach_volume {
471 my ($class, $conf, $func) = @_;
472
473 die "abstract method - implement me\n";
474 }
475
476 # Copy the current config $source to the snapshot config $dest
477 sub __snapshot_copy_config {
478 my ($class, $source, $dest) = @_;
479
480 foreach my $k (keys %$source) {
481 next if $k eq 'snapshots';
482 next if $k eq 'snapstate';
483 next if $k eq 'snaptime';
484 next if $k eq 'vmstate';
485 next if $k eq 'lock';
486 next if $k eq 'digest';
487 next if $k eq 'description';
488 next if $k =~ m/^unused\d+$/;
489
490 $dest->{$k} = $source->{$k};
491 }
492 };
493
494 # Apply the snapshot config $snap to the config $conf (rollback)
495 sub __snapshot_apply_config {
496 my ($class, $conf, $snap) = @_;
497
498 # copy snapshot list
499 my $newconf = {
500 snapshots => $conf->{snapshots},
501 };
502
503 # keep description and list of unused disks
504 foreach my $k (keys %$conf) {
505 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
506
507 $newconf->{$k} = $conf->{$k};
508 }
509
510 $class->__snapshot_copy_config($snap, $newconf);
511
512 return $newconf;
513 }
514
515 # Prepares the configuration for snapshotting.
516 sub __snapshot_prepare {
517 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
518
519 my $snap;
520
521 my $updatefn = sub {
522
523 my $conf = $class->load_config($vmid);
524
525 die "you can't take a snapshot if it's a template\n"
526 if $class->is_template($conf);
527
528 $class->check_lock($conf);
529
530 $conf->{lock} = 'snapshot';
531
532 die "snapshot name '$snapname' already used\n"
533 if defined($conf->{snapshots}->{$snapname});
534
535 my $storecfg = PVE::Storage::config();
536 die "snapshot feature is not available\n"
537 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
538
539 $snap = $conf->{snapshots}->{$snapname} = {};
540
541 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
542 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
543 }
544
545 $class->__snapshot_copy_config($conf, $snap);
546
547 $snap->{snapstate} = "prepare";
548 $snap->{snaptime} = time();
549 $snap->{description} = $comment if $comment;
550
551 $class->write_config($vmid, $conf);
552 };
553
554 $class->lock_config($vmid, $updatefn);
555
556 return $snap;
557 }
558
559 # Commits the configuration after snapshotting.
560 sub __snapshot_commit {
561 my ($class, $vmid, $snapname) = @_;
562
563 my $updatefn = sub {
564
565 my $conf = $class->load_config($vmid);
566
567 die "missing snapshot lock\n"
568 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
569
570 my $snap = $conf->{snapshots}->{$snapname};
571 die "snapshot '$snapname' does not exist\n" if !defined($snap);
572
573 die "wrong snapshot state\n"
574 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
575
576 delete $snap->{snapstate};
577 delete $conf->{lock};
578
579 $conf->{parent} = $snapname;
580
581 $class->write_config($vmid, $conf);
582 };
583
584 $class->lock_config($vmid, $updatefn);
585 }
586
587 # Creates a snapshot for the VM/CT.
588 sub snapshot_create {
589 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
590
591 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
592
593 $save_vmstate = 0 if !$snap->{vmstate};
594
595 my $conf = $class->load_config($vmid);
596
597 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
598
599 my $drivehash = {};
600
601 eval {
602 if ($freezefs) {
603 $class->__snapshot_freeze($vmid, 0);
604 }
605
606 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
607
608 $class->__snapshot_foreach_volume($snap, sub {
609 my ($vs, $volume) = @_;
610
611 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
612 $drivehash->{$vs} = 1;
613 });
614 };
615 my $err = $@;
616
617 if ($running) {
618 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
619 if ($freezefs) {
620 $class->__snapshot_freeze($vmid, 1);
621 }
622 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
623 }
624
625 if ($err) {
626 warn "snapshot create failed: starting cleanup\n";
627 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
628 warn "$@" if $@;
629 die "$err\n";
630 }
631
632 $class->__snapshot_commit($vmid, $snapname);
633 }
634
635 # Deletes a snapshot.
636 # Note: $drivehash is only set when called from snapshot_create.
637 sub snapshot_delete {
638 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
639
640 my $prepare = 1;
641
642 my $unused = [];
643
644 my $conf = $class->load_config($vmid);
645 my $snap = $conf->{snapshots}->{$snapname};
646
647 die "snapshot '$snapname' does not exist\n" if !defined($snap);
648
649 $class->set_lock($vmid, 'snapshot-delete')
650 if (!$drivehash); # doesn't already have a 'snapshot' lock
651
652 my $unlink_parent = sub {
653 my ($confref, $new_parent) = @_;
654
655 if ($confref->{parent} && $confref->{parent} eq $snapname) {
656 if ($new_parent) {
657 $confref->{parent} = $new_parent;
658 } else {
659 delete $confref->{parent};
660 }
661 }
662 };
663
664 my $remove_drive = sub {
665 my ($drive) = @_;
666
667 my $conf = $class->load_config($vmid);
668 $snap = $conf->{snapshots}->{$snapname};
669 die "snapshot '$snapname' does not exist\n" if !defined($snap);
670
671 $class->__snapshot_delete_remove_drive($snap, $drive);
672
673 $class->write_config($vmid, $conf);
674 };
675
676 #prepare
677 $class->lock_config($vmid, sub {
678 my $conf = $class->load_config($vmid);
679
680 die "you can't delete a snapshot if vm is a template\n"
681 if $class->is_template($conf);
682
683 $snap = $conf->{snapshots}->{$snapname};
684 die "snapshot '$snapname' does not exist\n" if !defined($snap);
685
686 $snap->{snapstate} = 'delete';
687
688 $class->write_config($vmid, $conf);
689 });
690
691 # now remove vmstate file
692 if ($snap->{vmstate}) {
693 $class->__snapshot_delete_vmstate_file($snap, $force);
694
695 # save changes (remove vmstate from snapshot)
696 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
697 };
698
699 # now remove all volume snapshots
700 $class->__snapshot_foreach_volume($snap, sub {
701 my ($vs, $volume) = @_;
702
703 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
704 if (!$drivehash || $drivehash->{$vs}) {
705 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
706 if (my $err = $@) {
707 die $err if !$force;
708 warn $err;
709 }
710 }
711
712 # save changes (remove drive from snapshot)
713 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
714 });
715
716 # now cleanup config
717 $class->lock_config($vmid, sub {
718 my $conf = $class->load_config($vmid);
719 $snap = $conf->{snapshots}->{$snapname};
720 die "snapshot '$snapname' does not exist\n" if !defined($snap);
721
722 # remove parent refs
723 &$unlink_parent($conf, $snap->{parent});
724 foreach my $sn (keys %{$conf->{snapshots}}) {
725 next if $sn eq $snapname;
726 &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
727 }
728
729
730 delete $conf->{snapshots}->{$snapname};
731 delete $conf->{lock};
732 foreach my $volid (@$unused) {
733 $class->add_unused_volume($conf, $volid);
734 }
735
736 $class->write_config($vmid, $conf);
737 });
738 }
739
740 # Rolls back to a given snapshot.
741 sub snapshot_rollback {
742 my ($class, $vmid, $snapname) = @_;
743
744 my $prepare = 1;
745
746 my $storecfg = PVE::Storage::config();
747
748 my $conf = $class->load_config($vmid);
749
750 my $get_snapshot_config = sub {
751
752 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
753
754 my $res = $conf->{snapshots}->{$snapname};
755
756 die "snapshot '$snapname' does not exist\n" if !defined($res);
757
758 return $res;
759 };
760
761 my $repl_conf = PVE::ReplicationConfig->new();
762 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
763 # remove all replication snapshots
764 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
765 my $sorted_volids = [ sort keys %$volumes ];
766
767 # remove all local replication snapshots (jobid => undef)
768 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
769 PVE::Replication::prepare($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
770 }
771
772 my $snap = &$get_snapshot_config();
773
774 $class->__snapshot_foreach_volume($snap, sub {
775 my ($vs, $volume) = @_;
776
777 $class->__snapshot_rollback_vol_possible($volume, $snapname);
778 });
779
780 my $data = {};
781
782 my $updatefn = sub {
783
784 $conf = $class->load_config($vmid);
785
786 $snap = &$get_snapshot_config();
787
788 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
789 if $snap->{snapstate};
790
791 if ($prepare) {
792 $class->check_lock($conf);
793 $class->__snapshot_rollback_vm_stop($vmid);
794 }
795
796 die "unable to rollback vm $vmid: vm is running\n"
797 if $class->__snapshot_check_running($vmid);
798
799 if ($prepare) {
800 $conf->{lock} = 'rollback';
801 } else {
802 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
803 delete $conf->{lock};
804
805 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
806
807 foreach my $volid (@$unused) {
808 $class->add_unused_volume($conf, $volid);
809 }
810
811 # copy snapshot config to current config
812 $conf = $class->__snapshot_apply_config($conf, $snap);
813 $conf->{parent} = $snapname;
814 }
815
816 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
817
818 $class->write_config($vmid, $conf);
819
820 if (!$prepare && $snap->{vmstate}) {
821 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data);
822 }
823 };
824
825 $class->lock_config($vmid, $updatefn);
826
827 $class->__snapshot_foreach_volume($snap, sub {
828 my ($vs, $volume) = @_;
829
830 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
831 });
832
833 $prepare = 0;
834 $class->lock_config($vmid, $updatefn);
835 }
836
837 # bash completion helper
838
839 sub snapshot_list {
840 my ($class, $vmid) = @_;
841
842 my $snapshot = eval {
843 my $conf = $class->load_config($vmid);
844 my $snapshots = $conf->{snapshots} || [];
845 [ sort keys %$snapshots ]
846 } || [];
847
848 return $snapshot;
849 }
850
851 1;