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