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