]> git.proxmox.com Git - pve-guest-common.git/blame - PVE/AbstractConfig.pm
Adapt description of get_backup_volumes
[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 =~ /^(!?)(.*)$/;
7b9d1ade
TL
85 $pending_deletions->{$key} = {
86 force => $force ? 1 : 0,
87 };
88 }
89
90 return $pending_deletions;
810ee088
OB
91}
92
93sub print_pending_delete {
94 my ($class, $delete_hash) = @_;
51088c75
TL
95
96 my $render_key = sub {
97 my $key = shift;
98 $key = "!$key" if $delete_hash->{$key}->{force};
99 return $key;
100 };
101
6a6ad855 102 join (',', map { $render_key->($_) } sort keys %$delete_hash);
810ee088
OB
103}
104
105sub add_to_pending_delete {
106 my ($class, $conf, $key, $force) = @_;
107
51e827b8
TL
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;
810ee088
OB
117}
118
119sub remove_from_pending_delete {
120 my ($class, $conf, $key) = @_;
121
464a42c4
TL
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
810ee088
OB
127 delete $pending_delete_hash->{$key};
128
129 if (%$pending_delete_hash) {
464a42c4 130 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
810ee088 131 } else {
464a42c4 132 delete $pending->{delete};
810ee088 133 }
464a42c4
TL
134
135 return $conf;
810ee088
OB
136}
137
138sub cleanup_pending {
139 my ($class, $conf) = @_;
140
c24919a9 141 my $pending = $conf->{pending};
810ee088
OB
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
9420142c 146 if (defined($conf->{$opt}) && ($pending->{$opt} eq $conf->{$opt})) {
810ee088 147 $changes = 1;
c24919a9 148 delete $pending->{$opt};
810ee088
OB
149 }
150 }
151
c24919a9 152 my $current_delete_hash = $class->parse_pending_delete($pending->{delete});
810ee088 153 my $pending_delete_hash = {};
c24919a9 154 for my $opt (keys %$current_delete_hash) {
810ee088 155 if (defined($conf->{$opt})) {
c24919a9 156 $pending_delete_hash->{$opt} = $current_delete_hash->{$opt};
810ee088
OB
157 } else {
158 $changes = 1;
159 }
160 }
161
162 if (%$pending_delete_hash) {
c24919a9 163 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
810ee088 164 } else {
c24919a9 165 delete $pending->{delete};
810ee088
OB
166 }
167
168 return $changes;
169}
170
866f5834
OB
171sub get_partial_fast_plug_option {
172 my ($class) = @_;
173
174 die "abstract method - implement me ";
175}
176
177sub partial_fast_plug {
178 my ($class, $conf, $opt) = @_;
179
180 my $partial_fast_plug_option = $class->get_partial_fast_plug_option();
47d7954c
TL
181 return 0 if !$partial_fast_plug_option->{$opt};
182
866f5834
OB
183 my $format = $partial_fast_plug_option->{$opt}->{fmt};
184 my $fast_pluggable = $partial_fast_plug_option->{$opt}->{properties};
185
186 my $configured = {};
187 if (exists($conf->{$opt})) {
188 $configured = PVE::JSONSchema::parse_property_string($format, $conf->{$opt});
189 }
190 my $pending = PVE::JSONSchema::parse_property_string($format, $conf->{pending}->{$opt});
191
192 my $changes = 0;
193
194 # merge configured and pending opts to iterate
195 my @all_keys = keys %{{ %$pending, %$configured }};
196
197 foreach my $subopt (@all_keys) {
198 my $type = $format->{$subopt}->{type};
67b3581e 199 if (typesafe_ne($configured->{$subopt}, $pending->{$subopt}, $type)) {
866f5834
OB
200 if ($fast_pluggable->{$subopt}) {
201 $configured->{$subopt} = $pending->{$subopt};
202 $changes = 1
203 }
204 }
205 }
206
207 # if there're no keys in $configured (after merge) there shouldn't be anything to change
208 if (keys %$configured) {
209 $conf->{$opt} = PVE::JSONSchema::print_property_string($configured, $format);
210 }
211
212 return $changes;
213}
214
215
61264e82
OB
216sub load_snapshot_config {
217 my ($class, $vmid, $snapname) = @_;
218
219 my $conf = $class->load_config($vmid);
220
221 my $snapshot = $conf->{snapshots}->{$snapname};
222 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
223
224 $snapshot->{digest} = $conf->{digest};
225
226 return $snapshot;
227
228}
229
230sub load_current_config {
231 my ($class, $vmid, $current) = @_;
232
233 my $conf = $class->load_config($vmid);
234
235 # take pending changes in
236 if (!$current) {
237 foreach my $opt (keys %{$conf->{pending}}) {
238 next if $opt eq 'delete';
239 my $value = $conf->{pending}->{$opt};
240 next if ref($value); # just to be sure
241 $conf->{$opt} = $value;
242 }
243 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending}->{delete});
244 foreach my $opt (keys %$pending_delete_hash) {
245 delete $conf->{$opt} if $conf->{$opt};
246 }
247 }
248
249 delete $conf->{snapshots};
250 delete $conf->{pending};
251
252 return $conf;
253}
254
cbbb06a5 255sub create_and_lock_config {
fde98d5e 256 my ($class, $vmid, $allow_existing, $lock) = @_;
cbbb06a5
TL
257
258 $class->lock_config_full($vmid, 5, sub {
259 PVE::Cluster::check_vmid_unused($vmid, $allow_existing);
260
261 my $conf = eval { $class->load_config($vmid) } || {};
262 $class->check_lock($conf);
fde98d5e 263 $conf->{lock} = $lock // 'create';
cbbb06a5
TL
264 $class->write_config($vmid, $conf);
265 });
266}
267
ab44df53 268# destroys configuration, only applicable for configs owned by the callers node.
1aa76f2f
TL
269# dies if removal fails, e.g., when inquorate.
270sub destroy_config {
271 my ($class, $vmid) = @_;
272
273 my $config_fn = $class->config_file($vmid, $nodename);
274 unlink $config_fn or die "failed to remove config file: $!\n";
275}
276
9cdd4400
FG
277my $lock_file_full_wrapper = sub {
278 my ($class, $vmid, $timeout, $shared, $realcode, @param) = @_;
58a3c91c
FG
279
280 my $filename = $class->config_file_lock($vmid);
281
97b9ccec 282 # make sure configuration file is up-to-date
9cdd4400 283 my $code = sub {
97b9ccec 284 PVE::Cluster::cfs_update();
9cdd4400 285 $realcode->(@_);
97b9ccec
FE
286 };
287
9cdd4400 288 my $res = lock_file_full($filename, $timeout, $shared, $code, @param);
58a3c91c
FG
289
290 die $@ if $@;
291
292 return $res;
9cdd4400
FG
293};
294
295# Lock config file using non-exclusive ("read") flock, run $code with @param, unlock config file.
296# $timeout is the maximum time to acquire the flock
297sub lock_config_shared {
298 my ($class, $vmid, $timeout, $code, @param) = @_;
299
300 return $lock_file_full_wrapper->($class, $vmid, $timeout, 1, $code, @param);
58a3c91c
FG
301}
302
9cdd4400
FG
303# Lock config file using flock, run $code with @param, unlock config file.
304# $timeout is the maximum time to acquire the flock
305sub lock_config_full {
306 my ($class, $vmid, $timeout, $code, @param) = @_;
307
308 return $lock_file_full_wrapper->($class, $vmid, $timeout, 0, $code, @param);
309}
310
311
58a3c91c
FG
312# Lock config file using flock, run $code with @param, unlock config file.
313sub lock_config {
314 my ($class, $vmid, $code, @param) = @_;
315
316 return $class->lock_config_full($vmid, 10, $code, @param);
317}
318
319# Checks whether the config is locked with the lock parameter
320sub check_lock {
321 my ($class, $conf) = @_;
322
323 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
324}
325
326# Returns whether the config is locked with the lock parameter, also checks
327# whether the lock value is correct if the optional $lock is set.
328sub has_lock {
329 my ($class, $conf, $lock) = @_;
330
331 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
332}
333
334# Sets the lock parameter for this VM/CT's config to $lock.
335sub set_lock {
336 my ($class, $vmid, $lock) = @_;
337
338 my $conf;
339 $class->lock_config($vmid, sub {
340 $conf = $class->load_config($vmid);
341 $class->check_lock($conf);
342 $conf->{lock} = $lock;
343 $class->write_config($vmid, $conf);
344 });
345 return $conf;
346}
347
348# Removes the lock parameter for this VM/CT's config, also checks whether
349# the lock value is correct if the optional $lock is set.
350sub remove_lock {
351 my ($class, $vmid, $lock) = @_;
352
353 $class->lock_config($vmid, sub {
354 my $conf = $class->load_config($vmid);
355 if (!$conf->{lock}) {
356 my $lockstring = defined($lock) ? "'$lock' " : "any";
357 die "no lock found trying to remove $lockstring lock\n";
358 } elsif (defined($lock) && $conf->{lock} ne $lock) {
359 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
360 }
361 delete $conf->{lock};
362 $class->write_config($vmid, $conf);
363 });
364}
365
366# Checks whether protection mode is enabled for this VM/CT.
367sub check_protection {
368 my ($class, $conf, $err_msg) = @_;
369
370 if ($conf->{protection}) {
371 die "$err_msg - protection mode enabled\n";
372 }
373}
374
4e6382ab
FE
375# Returns a list of keys where used volumes can be located
376sub valid_volume_keys {
377 my ($class, $reverse) = @_;
378
379 die "implement me - abstract method\n";
380}
381
382# Returns a hash with the information from $volume_string
383# $key is used to determine the format of the string
384sub parse_volume {
385 my ($class, $key, $volume_string, $noerr) = @_;
386
387 die "implement me - abstract method\n";
388}
389
390# Returns a string with the information from $volume
391# $key is used to determine the format of the string
392sub print_volume {
393 my ($class, $key, $volume) = @_;
394
395 die "implement me - abstract method\n";
396}
397
398# The key under which the volume ID is located in volume hashes
399sub volid_key {
400 my ($class) = @_;
401
402 die "implement me - abstract method\n";
403}
404
58a3c91c
FG
405# Adds an unused volume to $config, if possible.
406sub add_unused_volume {
407 my ($class, $config, $volid) = @_;
408
409 my $key;
410 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
411 my $test = "unused$ind";
412 if (my $vid = $config->{$test}) {
413 return if $vid eq $volid; # do not add duplicates
414 } else {
415 $key = $test;
416 }
417 }
418
419 die "Too many unused volumes - please delete them first.\n" if !$key;
420
421 $config->{$key} = $volid;
422
423 return $key;
424}
425
8db01440
FE
426# Iterate over all unused volumes, calling $func for each key/value pair
427# with additional parameters @param.
428sub foreach_unused_volume {
429 my ($class, $conf, $func, @param) = @_;
430
431 foreach my $key (keys %{$conf}) {
432 if ($key =~ m/^unused\d+$/) {
433 my $volume = $class->parse_volume($key, $conf->{$key});
434 $func->($key, $volume, @param);
435 }
436 }
437}
438
261d47a4
FE
439# Iterate over all configured volumes, calling $func for each key/value pair
440# with additional parameters @param.
441# By default, unused volumes and specials like vmstate are excluded.
442# Options: reverse - reverses the order for the iteration
443# include_unused - also iterate over unused volumes
444# extra_keys - an array of extra keys to use for the iteration
445sub foreach_volume_full {
446 my ($class, $conf, $opts, $func, @param) = @_;
447
448 die "'reverse' iteration only supported for default keys\n"
449 if $opts->{reverse} && ($opts->{extra_keys} || $opts->{include_unused});
450
451 my @keys = $class->valid_volume_keys($opts->{reverse});
452 push @keys, @{$opts->{extra_keys}} if $opts->{extra_keys};
453
454 foreach my $key (@keys) {
455 my $volume_string = $conf->{$key};
456 next if !defined($volume_string);
457
458 my $volume = $class->parse_volume($key, $volume_string, 1);
459 next if !defined($volume);
460
461 $func->($key, $volume, @param);
462 }
463
464 $class->foreach_unused_volume($conf, $func, @param) if $opts->{include_unused};
465}
466
467sub foreach_volume {
468 my ($class, $conf, $func, @param) = @_;
469
470 return $class->foreach_volume_full($conf, undef, $func, @param);
471}
472
95a9508e
FE
473# $volume_map is a hash of 'old_volid' => 'new_volid' pairs.
474# This method replaces 'old_volid' by 'new_volid' throughout
475# the config including snapshots and unused and vmstate volumes
476sub update_volume_ids {
477 my ($class, $conf, $volume_map) = @_;
478
479 my $volid_key = $class->volid_key();
480
481 my $do_replace = sub {
482 my ($key, $volume, $current_section) = @_;
483
484 my $old_volid = $volume->{$volid_key};
485 if (my $new_volid = $volume_map->{$old_volid}) {
486 $volume->{$volid_key} = $new_volid;
487 $current_section->{$key} = $class->print_volume($key, $volume);
488 }
489 };
490
491 my $opts = {
492 'include_unused' => 1,
493 'extra_keys' => ['vmstate'],
494 };
495
496 $class->foreach_volume_full($conf, $opts, $do_replace, $conf);
497 foreach my $snap (keys %{$conf->{snapshots}}) {
498 my $snap_conf = $conf->{snapshots}->{$snap};
499 $class->foreach_volume_full($snap_conf, $opts, $do_replace, $snap_conf);
500 }
501}
502
58a3c91c
FG
503# Returns whether the template parameter is set in $conf.
504sub is_template {
505 my ($class, $conf) = @_;
506
507 return 1 if defined $conf->{template} && $conf->{template} == 1;
508}
509
ab44df53 510# Checks whether $feature is available for the referenced volumes in $conf.
58a3c91c
FG
511# Note: depending on the parameters, some volumes may be skipped!
512sub has_feature {
513 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
514 die "implement me - abstract method\n";
515}
516
3f85b14d
DM
517# get all replicatable volume (hash $res->{$volid} = 1)
518# $cleanup: for cleanup - simply ignores volumes without replicate feature
519# $norerr: never raise exceptions - return undef instead
520sub get_replicatable_volumes {
54b79ff5 521 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
3f85b14d
DM
522
523 die "implement me - abstract method\n";
524}
525
1245ad85
TL
526# Returns all the guests volumes which would be included in a vzdump job
527# Return Format (array-ref with hash-refs as elements):
b2a2affa
AL
528# [
529# {
a8878e5e
AL
530# key, key in the config, e.g. mp0, scsi0,...
531# included, boolean
532# reason, string
533# volume_config volume object as returned from foreach_volume()
1245ad85 534# },
b2a2affa
AL
535# ]
536sub get_backup_volumes {
537 my ($class, $conf) = @_;
538
539 die "implement me - abstract method\n";
540}
541
58a3c91c
FG
542# Internal snapshots
543
544# NOTE: Snapshot create/delete involves several non-atomic
545# actions, and can take a long time.
546# So we try to avoid locking the file and use the 'lock' variable
547# inside the config file instead.
548
549# Save the vmstate (RAM).
550sub __snapshot_save_vmstate {
551 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
552 die "implement me - abstract method\n";
553}
554
555# Check whether the VM/CT is running.
556sub __snapshot_check_running {
557 my ($class, $vmid) = @_;
558 die "implement me - abstract method\n";
559}
560
561# Check whether we need to freeze the VM/CT
562sub __snapshot_check_freeze_needed {
563 my ($sself, $vmid, $config, $save_vmstate) = @_;
564 die "implement me - abstract method\n";
565}
566
567# Freeze or unfreeze the VM/CT.
568sub __snapshot_freeze {
569 my ($class, $vmid, $unfreeze) = @_;
570
571 die "abstract method - implement me\n";
572}
573
574# Code run before and after creating all the volume snapshots
575# base: noop
576sub __snapshot_create_vol_snapshots_hook {
577 my ($class, $vmid, $snap, $running, $hook) = @_;
578
579 return;
580}
581
582# Create the volume snapshots for the VM/CT.
583sub __snapshot_create_vol_snapshot {
584 my ($class, $vmid, $vs, $volume, $snapname) = @_;
585
586 die "abstract method - implement me\n";
587}
588
589# Remove a drive from the snapshot config.
590sub __snapshot_delete_remove_drive {
591 my ($class, $snap, $drive) = @_;
592
593 die "abstract method - implement me\n";
594}
595
596# Delete the vmstate file/drive
597sub __snapshot_delete_vmstate_file {
598 my ($class, $snap, $force) = @_;
599
600 die "abstract method - implement me\n";
601}
602
603# Delete a volume snapshot
604sub __snapshot_delete_vol_snapshot {
605 my ($class, $vmid, $vs, $volume, $snapname) = @_;
606
607 die "abstract method - implement me\n";
608}
609
3fa4ce45
DC
610# called during rollback prepare, and between config rollback and starting guest
611# can change config, e.g. for vmgenid
612# $data is shared across calls and passed to vm_start
613sub __snapshot_rollback_hook {
614 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
615
616 return;
617}
618
58a3c91c
FG
619# Checks whether a volume snapshot is possible for this volume.
620sub __snapshot_rollback_vol_possible {
621 my ($class, $volume, $snapname) = @_;
622
623 die "abstract method - implement me\n";
624}
625
626# Rolls back this volume.
627sub __snapshot_rollback_vol_rollback {
628 my ($class, $volume, $snapname) = @_;
629
630 die "abstract method - implement me\n";
631}
632
633# Stops the VM/CT for a rollback.
634sub __snapshot_rollback_vm_stop {
635 my ($class, $vmid) = @_;
636
637 die "abstract method - implement me\n";
638}
639
640# Start the VM/CT after a rollback with restored vmstate.
641sub __snapshot_rollback_vm_start {
3fa4ce45 642 my ($class, $vmid, $vmstate, $data);
58a3c91c
FG
643
644 die "abstract method - implement me\n";
645}
646
647# Get list of volume IDs which are referenced in $conf, but not in $snap.
648sub __snapshot_rollback_get_unused {
649 my ($class, $conf, $snap) = @_;
650
651 die "abstract method - implement me\n";
652}
653
58a3c91c
FG
654# Copy the current config $source to the snapshot config $dest
655sub __snapshot_copy_config {
656 my ($class, $source, $dest) = @_;
657
658 foreach my $k (keys %$source) {
659 next if $k eq 'snapshots';
660 next if $k eq 'snapstate';
661 next if $k eq 'snaptime';
662 next if $k eq 'vmstate';
663 next if $k eq 'lock';
664 next if $k eq 'digest';
665 next if $k eq 'description';
666 next if $k =~ m/^unused\d+$/;
667
668 $dest->{$k} = $source->{$k};
669 }
670};
671
672# Apply the snapshot config $snap to the config $conf (rollback)
673sub __snapshot_apply_config {
674 my ($class, $conf, $snap) = @_;
675
676 # copy snapshot list
677 my $newconf = {
678 snapshots => $conf->{snapshots},
679 };
680
eb74d483 681 # keep description and list of unused disks
58a3c91c 682 foreach my $k (keys %$conf) {
eb74d483 683 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
38532f70 684
58a3c91c
FG
685 $newconf->{$k} = $conf->{$k};
686 }
687
688 $class->__snapshot_copy_config($snap, $newconf);
689
690 return $newconf;
691}
692
693# Prepares the configuration for snapshotting.
694sub __snapshot_prepare {
695 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
696
697 my $snap;
698
699 my $updatefn = sub {
700
701 my $conf = $class->load_config($vmid);
702
703 die "you can't take a snapshot if it's a template\n"
704 if $class->is_template($conf);
705
706 $class->check_lock($conf);
707
708 $conf->{lock} = 'snapshot';
709
710 die "snapshot name '$snapname' already used\n"
711 if defined($conf->{snapshots}->{$snapname});
712
713 my $storecfg = PVE::Storage::config();
714 die "snapshot feature is not available\n"
715 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
716
717 $snap = $conf->{snapshots}->{$snapname} = {};
718
719 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
720 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
721 }
722
723 $class->__snapshot_copy_config($conf, $snap);
724
725 $snap->{snapstate} = "prepare";
726 $snap->{snaptime} = time();
727 $snap->{description} = $comment if $comment;
728
729 $class->write_config($vmid, $conf);
730 };
731
732 $class->lock_config($vmid, $updatefn);
733
734 return $snap;
735}
736
737# Commits the configuration after snapshotting.
738sub __snapshot_commit {
739 my ($class, $vmid, $snapname) = @_;
740
741 my $updatefn = sub {
742
743 my $conf = $class->load_config($vmid);
744
745 die "missing snapshot lock\n"
746 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
747
748 my $snap = $conf->{snapshots}->{$snapname};
749 die "snapshot '$snapname' does not exist\n" if !defined($snap);
750
751 die "wrong snapshot state\n"
752 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
753
754 delete $snap->{snapstate};
755 delete $conf->{lock};
756
757 $conf->{parent} = $snapname;
758
759 $class->write_config($vmid, $conf);
760 };
761
762 $class->lock_config($vmid, $updatefn);
763}
764
765# Creates a snapshot for the VM/CT.
766sub snapshot_create {
767 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
768
769 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
770
771 $save_vmstate = 0 if !$snap->{vmstate};
772
773 my $conf = $class->load_config($vmid);
774
775 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
776
777 my $drivehash = {};
778
779 eval {
780 if ($freezefs) {
781 $class->__snapshot_freeze($vmid, 0);
782 }
783
784 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
785
261d47a4 786 $class->foreach_volume($snap, sub {
58a3c91c
FG
787 my ($vs, $volume) = @_;
788
789 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
790 $drivehash->{$vs} = 1;
791 });
792 };
793 my $err = $@;
794
795 if ($running) {
796 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
797 if ($freezefs) {
798 $class->__snapshot_freeze($vmid, 1);
799 }
800 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
801 }
802
803 if ($err) {
804 warn "snapshot create failed: starting cleanup\n";
805 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
806 warn "$@" if $@;
807 die "$err\n";
808 }
809
810 $class->__snapshot_commit($vmid, $snapname);
811}
812
813# Deletes a snapshot.
814# Note: $drivehash is only set when called from snapshot_create.
815sub snapshot_delete {
816 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
817
818 my $prepare = 1;
819
58a3c91c
FG
820 my $unused = [];
821
4c3144ea
AA
822 my $conf = $class->load_config($vmid);
823 my $snap = $conf->{snapshots}->{$snapname};
824
825 die "snapshot '$snapname' does not exist\n" if !defined($snap);
826
58a3c91c
FG
827 $class->set_lock($vmid, 'snapshot-delete')
828 if (!$drivehash); # doesn't already have a 'snapshot' lock
829
990d9675
FG
830 my $expected_lock = $drivehash ? 'snapshot' : 'snapshot-delete';
831
832 my $ensure_correct_lock = sub {
833 my ($conf) = @_;
834
835 die "encountered invalid lock, expected '$expected_lock'\n"
836 if !$class->has_lock($conf, $expected_lock);
837 };
838
58a3c91c
FG
839 my $unlink_parent = sub {
840 my ($confref, $new_parent) = @_;
841
842 if ($confref->{parent} && $confref->{parent} eq $snapname) {
843 if ($new_parent) {
844 $confref->{parent} = $new_parent;
845 } else {
846 delete $confref->{parent};
847 }
848 }
849 };
850
851 my $remove_drive = sub {
852 my ($drive) = @_;
853
854 my $conf = $class->load_config($vmid);
990d9675
FG
855 $ensure_correct_lock->($conf);
856
58a3c91c
FG
857 $snap = $conf->{snapshots}->{$snapname};
858 die "snapshot '$snapname' does not exist\n" if !defined($snap);
859
860 $class->__snapshot_delete_remove_drive($snap, $drive);
861
862 $class->write_config($vmid, $conf);
863 };
864
865 #prepare
866 $class->lock_config($vmid, sub {
867 my $conf = $class->load_config($vmid);
990d9675 868 $ensure_correct_lock->($conf);
58a3c91c
FG
869
870 die "you can't delete a snapshot if vm is a template\n"
871 if $class->is_template($conf);
872
873 $snap = $conf->{snapshots}->{$snapname};
874 die "snapshot '$snapname' does not exist\n" if !defined($snap);
875
876 $snap->{snapstate} = 'delete';
877
878 $class->write_config($vmid, $conf);
879 });
880
881 # now remove vmstate file
882 if ($snap->{vmstate}) {
883 $class->__snapshot_delete_vmstate_file($snap, $force);
884
885 # save changes (remove vmstate from snapshot)
886 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
887 };
888
889 # now remove all volume snapshots
261d47a4 890 $class->foreach_volume($snap, sub {
58a3c91c
FG
891 my ($vs, $volume) = @_;
892
893 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
894 if (!$drivehash || $drivehash->{$vs}) {
895 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
896 if (my $err = $@) {
897 die $err if !$force;
898 warn $err;
899 }
900 }
901
902 # save changes (remove drive from snapshot)
903 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
904 });
905
906 # now cleanup config
907 $class->lock_config($vmid, sub {
908 my $conf = $class->load_config($vmid);
990d9675
FG
909 $ensure_correct_lock->($conf);
910
58a3c91c
FG
911 $snap = $conf->{snapshots}->{$snapname};
912 die "snapshot '$snapname' does not exist\n" if !defined($snap);
913
914 # remove parent refs
915 &$unlink_parent($conf, $snap->{parent});
916 foreach my $sn (keys %{$conf->{snapshots}}) {
917 next if $sn eq $snapname;
918 &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
919 }
920
921
922 delete $conf->{snapshots}->{$snapname};
923 delete $conf->{lock};
924 foreach my $volid (@$unused) {
925 $class->add_unused_volume($conf, $volid);
926 }
927
928 $class->write_config($vmid, $conf);
929 });
930}
931
932# Rolls back to a given snapshot.
933sub snapshot_rollback {
934 my ($class, $vmid, $snapname) = @_;
935
936 my $prepare = 1;
937
938 my $storecfg = PVE::Storage::config();
939
82a04857 940 my $data = {};
58a3c91c
FG
941
942 my $get_snapshot_config = sub {
82a04857 943 my ($conf) = @_;
58a3c91c
FG
944
945 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
946
947 my $res = $conf->{snapshots}->{$snapname};
948
949 die "snapshot '$snapname' does not exist\n" if !defined($res);
950
951 return $res;
952 };
953
82a04857 954 my $snap;
3fa4ce45 955
58a3c91c 956 my $updatefn = sub {
82a04857
FG
957 my $conf = $class->load_config($vmid);
958 $snap = $get_snapshot_config->($conf);
58a3c91c 959
82a04857
FG
960 if ($prepare) {
961 my $repl_conf = PVE::ReplicationConfig->new();
962 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
963 # remove all replication snapshots
964 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
965 my $sorted_volids = [ sort keys %$volumes ];
966
967 # remove all local replication snapshots (jobid => undef)
968 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
c261973c 969 PVE::Replication::prepare($storecfg, $sorted_volids, undef, 1, undef, $logfunc);
82a04857
FG
970 }
971
972 $class->foreach_volume($snap, sub {
973 my ($vs, $volume) = @_;
58a3c91c 974
82a04857
FG
975 $class->__snapshot_rollback_vol_possible($volume, $snapname);
976 });
977 }
58a3c91c
FG
978
979 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
980 if $snap->{snapstate};
981
982 if ($prepare) {
983 $class->check_lock($conf);
984 $class->__snapshot_rollback_vm_stop($vmid);
985 }
986
987 die "unable to rollback vm $vmid: vm is running\n"
988 if $class->__snapshot_check_running($vmid);
989
990 if ($prepare) {
991 $conf->{lock} = 'rollback';
992 } else {
993 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
994 delete $conf->{lock};
58a3c91c 995
58a3c91c
FG
996 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
997
998 foreach my $volid (@$unused) {
999 $class->add_unused_volume($conf, $volid);
1000 }
1001
58a3c91c
FG
1002 # copy snapshot config to current config
1003 $conf = $class->__snapshot_apply_config($conf, $snap);
1004 $conf->{parent} = $snapname;
58a3c91c
FG
1005 }
1006
3fa4ce45
DC
1007 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
1008
58a3c91c
FG
1009 $class->write_config($vmid, $conf);
1010
1011 if (!$prepare && $snap->{vmstate}) {
3fa4ce45 1012 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data);
58a3c91c
FG
1013 }
1014 };
1015
1016 $class->lock_config($vmid, $updatefn);
1017
261d47a4 1018 $class->foreach_volume($snap, sub {
58a3c91c
FG
1019 my ($vs, $volume) = @_;
1020
1021 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
1022 });
1023
1024 $prepare = 0;
1025 $class->lock_config($vmid, $updatefn);
1026}
1027
eb50bb61
RV
1028# bash completion helper
1029
90fba9cd
WB
1030sub snapshot_list {
1031 my ($class, $vmid) = @_;
eb50bb61 1032
90fba9cd
WB
1033 my $snapshot = eval {
1034 my $conf = $class->load_config($vmid);
1035 my $snapshots = $conf->{snapshots} || [];
1036 [ sort keys %$snapshots ]
1037 } || [];
eb50bb61
RV
1038
1039 return $snapshot;
1040}
1041
58a3c91c 10421;