]> git.proxmox.com Git - pve-guest-common.git/blame - PVE/AbstractConfig.pm
get_backup_volumes: clarifcation and indentation improvements to comment
[pve-guest-common.git] / PVE / AbstractConfig.pm
CommitLineData
58a3c91c
FG
1package PVE::AbstractConfig;
2
3use strict;
4use warnings;
5
6use PVE::Tools qw(lock_file lock_file_full);
7use PVE::INotify;
8use PVE::Cluster;
9use PVE::Storage;
10
67b3581e 11use PVE::GuestHelpers qw(typesafe_ne);
14f17b49
DM
12use PVE::ReplicationConfig;
13use PVE::Replication;
14
58a3c91c
FG
15my $nodename = PVE::INotify::nodename();
16
17# Printable string, currently either "VM" or "CT"
18sub guest_type {
19 my ($class) = @_;
20 die "abstract method - implement me ";
21}
22
23sub __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
30sub 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
36sub 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
42sub 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
50sub 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
64sub 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
810ee088
OB
72# Pending changes related
73
74sub parse_pending_delete {
75 my ($class, $data) = @_;
7b9d1ade
TL
76
77 return {} if !$data;
78
810ee088
OB
79 $data =~ s/[,;]/ /g;
80 $data =~ s/^\s+//;
7b9d1ade
TL
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;
810ee088
OB
92}
93
94sub print_pending_delete {
95 my ($class, $delete_hash) = @_;
51088c75
TL
96
97 my $render_key = sub {
98 my $key = shift;
99 $key = "!$key" if $delete_hash->{$key}->{force};
100 return $key;
101 };
102
6a6ad855 103 join (',', map { $render_key->($_) } sort keys %$delete_hash);
810ee088
OB
104}
105
106sub add_to_pending_delete {
107 my ($class, $conf, $key, $force) = @_;
108
51e827b8
TL
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;
810ee088
OB
118}
119
120sub remove_from_pending_delete {
121 my ($class, $conf, $key) = @_;
122
464a42c4
TL
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
810ee088
OB
128 delete $pending_delete_hash->{$key};
129
130 if (%$pending_delete_hash) {
464a42c4 131 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
810ee088 132 } else {
464a42c4 133 delete $pending->{delete};
810ee088 134 }
464a42c4
TL
135
136 return $conf;
810ee088
OB
137}
138
139sub cleanup_pending {
140 my ($class, $conf) = @_;
141
c24919a9 142 my $pending = $conf->{pending};
810ee088
OB
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
9420142c 147 if (defined($conf->{$opt}) && ($pending->{$opt} eq $conf->{$opt})) {
810ee088 148 $changes = 1;
c24919a9 149 delete $pending->{$opt};
810ee088
OB
150 }
151 }
152
c24919a9 153 my $current_delete_hash = $class->parse_pending_delete($pending->{delete});
810ee088 154 my $pending_delete_hash = {};
c24919a9 155 for my $opt (keys %$current_delete_hash) {
810ee088 156 if (defined($conf->{$opt})) {
c24919a9 157 $pending_delete_hash->{$opt} = $current_delete_hash->{$opt};
810ee088
OB
158 } else {
159 $changes = 1;
160 }
161 }
162
163 if (%$pending_delete_hash) {
c24919a9 164 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
810ee088 165 } else {
c24919a9 166 delete $pending->{delete};
810ee088
OB
167 }
168
169 return $changes;
170}
171
866f5834
OB
172sub get_partial_fast_plug_option {
173 my ($class) = @_;
174
175 die "abstract method - implement me ";
176}
177
178sub partial_fast_plug {
179 my ($class, $conf, $opt) = @_;
180
181 my $partial_fast_plug_option = $class->get_partial_fast_plug_option();
47d7954c
TL
182 return 0 if !$partial_fast_plug_option->{$opt};
183
866f5834
OB
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};
67b3581e 200 if (typesafe_ne($configured->{$subopt}, $pending->{$subopt}, $type)) {
866f5834
OB
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
61264e82
OB
217sub 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
231sub 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
58a3c91c 257# Lock config file using flock, run $code with @param, unlock config file.
ab44df53 258# $timeout is the maximum time to acquire the flock
58a3c91c
FG
259sub 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
cbbb06a5 271sub create_and_lock_config {
fde98d5e 272 my ($class, $vmid, $allow_existing, $lock) = @_;
cbbb06a5
TL
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);
fde98d5e 279 $conf->{lock} = $lock // 'create';
cbbb06a5
TL
280 $class->write_config($vmid, $conf);
281 });
282}
283
ab44df53 284# destroys configuration, only applicable for configs owned by the callers node.
1aa76f2f
TL
285# dies if removal fails, e.g., when inquorate.
286sub 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
58a3c91c 293# Lock config file using flock, run $code with @param, unlock config file.
ab44df53 294# $timeout is the maximum time to acquire the flock
58a3c91c
FG
295# $shared eq 1 creates a non-exclusive ("read") flock
296sub 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.
309sub 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
316sub 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.
324sub 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.
331sub 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.
346sub 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.
363sub 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.
372sub 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.
393sub is_template {
394 my ($class, $conf) = @_;
395
396 return 1 if defined $conf->{template} && $conf->{template} == 1;
397}
398
ab44df53 399# Checks whether $feature is available for the referenced volumes in $conf.
58a3c91c
FG
400# Note: depending on the parameters, some volumes may be skipped!
401sub has_feature {
402 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
403 die "implement me - abstract method\n";
404}
405
3f85b14d
DM
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
409sub get_replicatable_volumes {
54b79ff5 410 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
3f85b14d
DM
411
412 die "implement me - abstract method\n";
413}
414
1245ad85
TL
415# Returns all the guests volumes which would be included in a vzdump job
416# Return Format (array-ref with hash-refs as elements):
b2a2affa
AL
417# [
418# {
1245ad85
TL
419# key, key in the config, e.g. mp0, scsi0,...
420# included, boolean
421# reason, string
422# data volume object as returned from foreach_drive/foreach_mountpoint
423# },
b2a2affa
AL
424# ]
425sub get_backup_volumes {
426 my ($class, $conf) = @_;
427
428 die "implement me - abstract method\n";
429}
430
58a3c91c
FG
431# Internal snapshots
432
433# NOTE: Snapshot create/delete involves several non-atomic
434# actions, and can take a long time.
435# So we try to avoid locking the file and use the 'lock' variable
436# inside the config file instead.
437
438# Save the vmstate (RAM).
439sub __snapshot_save_vmstate {
440 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
441 die "implement me - abstract method\n";
442}
443
444# Check whether the VM/CT is running.
445sub __snapshot_check_running {
446 my ($class, $vmid) = @_;
447 die "implement me - abstract method\n";
448}
449
450# Check whether we need to freeze the VM/CT
451sub __snapshot_check_freeze_needed {
452 my ($sself, $vmid, $config, $save_vmstate) = @_;
453 die "implement me - abstract method\n";
454}
455
456# Freeze or unfreeze the VM/CT.
457sub __snapshot_freeze {
458 my ($class, $vmid, $unfreeze) = @_;
459
460 die "abstract method - implement me\n";
461}
462
463# Code run before and after creating all the volume snapshots
464# base: noop
465sub __snapshot_create_vol_snapshots_hook {
466 my ($class, $vmid, $snap, $running, $hook) = @_;
467
468 return;
469}
470
471# Create the volume snapshots for the VM/CT.
472sub __snapshot_create_vol_snapshot {
473 my ($class, $vmid, $vs, $volume, $snapname) = @_;
474
475 die "abstract method - implement me\n";
476}
477
478# Remove a drive from the snapshot config.
479sub __snapshot_delete_remove_drive {
480 my ($class, $snap, $drive) = @_;
481
482 die "abstract method - implement me\n";
483}
484
485# Delete the vmstate file/drive
486sub __snapshot_delete_vmstate_file {
487 my ($class, $snap, $force) = @_;
488
489 die "abstract method - implement me\n";
490}
491
492# Delete a volume snapshot
493sub __snapshot_delete_vol_snapshot {
494 my ($class, $vmid, $vs, $volume, $snapname) = @_;
495
496 die "abstract method - implement me\n";
497}
498
3fa4ce45
DC
499# called during rollback prepare, and between config rollback and starting guest
500# can change config, e.g. for vmgenid
501# $data is shared across calls and passed to vm_start
502sub __snapshot_rollback_hook {
503 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
504
505 return;
506}
507
58a3c91c
FG
508# Checks whether a volume snapshot is possible for this volume.
509sub __snapshot_rollback_vol_possible {
510 my ($class, $volume, $snapname) = @_;
511
512 die "abstract method - implement me\n";
513}
514
515# Rolls back this volume.
516sub __snapshot_rollback_vol_rollback {
517 my ($class, $volume, $snapname) = @_;
518
519 die "abstract method - implement me\n";
520}
521
522# Stops the VM/CT for a rollback.
523sub __snapshot_rollback_vm_stop {
524 my ($class, $vmid) = @_;
525
526 die "abstract method - implement me\n";
527}
528
529# Start the VM/CT after a rollback with restored vmstate.
530sub __snapshot_rollback_vm_start {
3fa4ce45 531 my ($class, $vmid, $vmstate, $data);
58a3c91c
FG
532
533 die "abstract method - implement me\n";
534}
535
536# Get list of volume IDs which are referenced in $conf, but not in $snap.
537sub __snapshot_rollback_get_unused {
538 my ($class, $conf, $snap) = @_;
539
540 die "abstract method - implement me\n";
541}
542
543# Iterate over all configured volumes, calling $func for each key/value pair.
544sub __snapshot_foreach_volume {
545 my ($class, $conf, $func) = @_;
546
547 die "abstract method - implement me\n";
548}
549
550# Copy the current config $source to the snapshot config $dest
551sub __snapshot_copy_config {
552 my ($class, $source, $dest) = @_;
553
554 foreach my $k (keys %$source) {
555 next if $k eq 'snapshots';
556 next if $k eq 'snapstate';
557 next if $k eq 'snaptime';
558 next if $k eq 'vmstate';
559 next if $k eq 'lock';
560 next if $k eq 'digest';
561 next if $k eq 'description';
562 next if $k =~ m/^unused\d+$/;
563
564 $dest->{$k} = $source->{$k};
565 }
566};
567
568# Apply the snapshot config $snap to the config $conf (rollback)
569sub __snapshot_apply_config {
570 my ($class, $conf, $snap) = @_;
571
572 # copy snapshot list
573 my $newconf = {
574 snapshots => $conf->{snapshots},
575 };
576
eb74d483 577 # keep description and list of unused disks
58a3c91c 578 foreach my $k (keys %$conf) {
eb74d483 579 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
38532f70 580
58a3c91c
FG
581 $newconf->{$k} = $conf->{$k};
582 }
583
584 $class->__snapshot_copy_config($snap, $newconf);
585
586 return $newconf;
587}
588
589# Prepares the configuration for snapshotting.
590sub __snapshot_prepare {
591 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
592
593 my $snap;
594
595 my $updatefn = sub {
596
597 my $conf = $class->load_config($vmid);
598
599 die "you can't take a snapshot if it's a template\n"
600 if $class->is_template($conf);
601
602 $class->check_lock($conf);
603
604 $conf->{lock} = 'snapshot';
605
606 die "snapshot name '$snapname' already used\n"
607 if defined($conf->{snapshots}->{$snapname});
608
609 my $storecfg = PVE::Storage::config();
610 die "snapshot feature is not available\n"
611 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
612
613 $snap = $conf->{snapshots}->{$snapname} = {};
614
615 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
616 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
617 }
618
619 $class->__snapshot_copy_config($conf, $snap);
620
621 $snap->{snapstate} = "prepare";
622 $snap->{snaptime} = time();
623 $snap->{description} = $comment if $comment;
624
625 $class->write_config($vmid, $conf);
626 };
627
628 $class->lock_config($vmid, $updatefn);
629
630 return $snap;
631}
632
633# Commits the configuration after snapshotting.
634sub __snapshot_commit {
635 my ($class, $vmid, $snapname) = @_;
636
637 my $updatefn = sub {
638
639 my $conf = $class->load_config($vmid);
640
641 die "missing snapshot lock\n"
642 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
643
644 my $snap = $conf->{snapshots}->{$snapname};
645 die "snapshot '$snapname' does not exist\n" if !defined($snap);
646
647 die "wrong snapshot state\n"
648 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
649
650 delete $snap->{snapstate};
651 delete $conf->{lock};
652
653 $conf->{parent} = $snapname;
654
655 $class->write_config($vmid, $conf);
656 };
657
658 $class->lock_config($vmid, $updatefn);
659}
660
661# Creates a snapshot for the VM/CT.
662sub snapshot_create {
663 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
664
665 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
666
667 $save_vmstate = 0 if !$snap->{vmstate};
668
669 my $conf = $class->load_config($vmid);
670
671 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
672
673 my $drivehash = {};
674
675 eval {
676 if ($freezefs) {
677 $class->__snapshot_freeze($vmid, 0);
678 }
679
680 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
681
682 $class->__snapshot_foreach_volume($snap, sub {
683 my ($vs, $volume) = @_;
684
685 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
686 $drivehash->{$vs} = 1;
687 });
688 };
689 my $err = $@;
690
691 if ($running) {
692 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
693 if ($freezefs) {
694 $class->__snapshot_freeze($vmid, 1);
695 }
696 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
697 }
698
699 if ($err) {
700 warn "snapshot create failed: starting cleanup\n";
701 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
702 warn "$@" if $@;
703 die "$err\n";
704 }
705
706 $class->__snapshot_commit($vmid, $snapname);
707}
708
709# Deletes a snapshot.
710# Note: $drivehash is only set when called from snapshot_create.
711sub snapshot_delete {
712 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
713
714 my $prepare = 1;
715
58a3c91c
FG
716 my $unused = [];
717
4c3144ea
AA
718 my $conf = $class->load_config($vmid);
719 my $snap = $conf->{snapshots}->{$snapname};
720
721 die "snapshot '$snapname' does not exist\n" if !defined($snap);
722
58a3c91c
FG
723 $class->set_lock($vmid, 'snapshot-delete')
724 if (!$drivehash); # doesn't already have a 'snapshot' lock
725
726 my $unlink_parent = sub {
727 my ($confref, $new_parent) = @_;
728
729 if ($confref->{parent} && $confref->{parent} eq $snapname) {
730 if ($new_parent) {
731 $confref->{parent} = $new_parent;
732 } else {
733 delete $confref->{parent};
734 }
735 }
736 };
737
738 my $remove_drive = sub {
739 my ($drive) = @_;
740
741 my $conf = $class->load_config($vmid);
742 $snap = $conf->{snapshots}->{$snapname};
743 die "snapshot '$snapname' does not exist\n" if !defined($snap);
744
745 $class->__snapshot_delete_remove_drive($snap, $drive);
746
747 $class->write_config($vmid, $conf);
748 };
749
750 #prepare
751 $class->lock_config($vmid, sub {
752 my $conf = $class->load_config($vmid);
753
754 die "you can't delete a snapshot if vm is a template\n"
755 if $class->is_template($conf);
756
757 $snap = $conf->{snapshots}->{$snapname};
758 die "snapshot '$snapname' does not exist\n" if !defined($snap);
759
760 $snap->{snapstate} = 'delete';
761
762 $class->write_config($vmid, $conf);
763 });
764
765 # now remove vmstate file
766 if ($snap->{vmstate}) {
767 $class->__snapshot_delete_vmstate_file($snap, $force);
768
769 # save changes (remove vmstate from snapshot)
770 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
771 };
772
773 # now remove all volume snapshots
774 $class->__snapshot_foreach_volume($snap, sub {
775 my ($vs, $volume) = @_;
776
777 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
778 if (!$drivehash || $drivehash->{$vs}) {
779 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
780 if (my $err = $@) {
781 die $err if !$force;
782 warn $err;
783 }
784 }
785
786 # save changes (remove drive from snapshot)
787 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
788 });
789
790 # now cleanup config
791 $class->lock_config($vmid, sub {
792 my $conf = $class->load_config($vmid);
793 $snap = $conf->{snapshots}->{$snapname};
794 die "snapshot '$snapname' does not exist\n" if !defined($snap);
795
796 # remove parent refs
797 &$unlink_parent($conf, $snap->{parent});
798 foreach my $sn (keys %{$conf->{snapshots}}) {
799 next if $sn eq $snapname;
800 &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
801 }
802
803
804 delete $conf->{snapshots}->{$snapname};
805 delete $conf->{lock};
806 foreach my $volid (@$unused) {
807 $class->add_unused_volume($conf, $volid);
808 }
809
810 $class->write_config($vmid, $conf);
811 });
812}
813
814# Rolls back to a given snapshot.
815sub snapshot_rollback {
816 my ($class, $vmid, $snapname) = @_;
817
818 my $prepare = 1;
819
820 my $storecfg = PVE::Storage::config();
821
822 my $conf = $class->load_config($vmid);
823
824 my $get_snapshot_config = sub {
825
826 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
827
828 my $res = $conf->{snapshots}->{$snapname};
829
830 die "snapshot '$snapname' does not exist\n" if !defined($res);
831
832 return $res;
833 };
834
683a30e0
WL
835 my $repl_conf = PVE::ReplicationConfig->new();
836 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
837 # remove all replication snapshots
c324e907 838 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
683a30e0
WL
839 my $sorted_volids = [ sort keys %$volumes ];
840
841 # remove all local replication snapshots (jobid => undef)
842 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
843 PVE::Replication::prepare($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
844 }
845
58a3c91c
FG
846 my $snap = &$get_snapshot_config();
847
848 $class->__snapshot_foreach_volume($snap, sub {
849 my ($vs, $volume) = @_;
850
851 $class->__snapshot_rollback_vol_possible($volume, $snapname);
852 });
853
3fa4ce45
DC
854 my $data = {};
855
58a3c91c
FG
856 my $updatefn = sub {
857
858 $conf = $class->load_config($vmid);
859
860 $snap = &$get_snapshot_config();
861
862 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
863 if $snap->{snapstate};
864
865 if ($prepare) {
866 $class->check_lock($conf);
867 $class->__snapshot_rollback_vm_stop($vmid);
868 }
869
870 die "unable to rollback vm $vmid: vm is running\n"
871 if $class->__snapshot_check_running($vmid);
872
873 if ($prepare) {
874 $conf->{lock} = 'rollback';
875 } else {
876 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
877 delete $conf->{lock};
58a3c91c 878
58a3c91c
FG
879 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
880
881 foreach my $volid (@$unused) {
882 $class->add_unused_volume($conf, $volid);
883 }
884
58a3c91c
FG
885 # copy snapshot config to current config
886 $conf = $class->__snapshot_apply_config($conf, $snap);
887 $conf->{parent} = $snapname;
58a3c91c
FG
888 }
889
3fa4ce45
DC
890 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
891
58a3c91c
FG
892 $class->write_config($vmid, $conf);
893
894 if (!$prepare && $snap->{vmstate}) {
3fa4ce45 895 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data);
58a3c91c
FG
896 }
897 };
898
899 $class->lock_config($vmid, $updatefn);
900
901 $class->__snapshot_foreach_volume($snap, sub {
902 my ($vs, $volume) = @_;
903
904 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
905 });
906
907 $prepare = 0;
908 $class->lock_config($vmid, $updatefn);
909}
910
eb50bb61
RV
911# bash completion helper
912
90fba9cd
WB
913sub snapshot_list {
914 my ($class, $vmid) = @_;
eb50bb61 915
90fba9cd
WB
916 my $snapshot = eval {
917 my $conf = $class->load_config($vmid);
918 my $snapshots = $conf->{snapshots} || [];
919 [ sort keys %$snapshots ]
920 } || [];
eb50bb61
RV
921
922 return $snapshot;
923}
924
58a3c91c 9251;