]> git.proxmox.com Git - pve-guest-common.git/blob - src/PVE/AbstractConfig.pm
fix #4572: config: also update volume IDs in pending section
[pve-guest-common.git] / src / 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 $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 return 0 if !$partial_fast_plug_option->{$opt};
182
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};
199 if (typesafe_ne($configured->{$subopt}, $pending->{$subopt}, $type)) {
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
216 sub 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
230 sub 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
255 sub create_and_lock_config {
256 my ($class, $vmid, $allow_existing, $lock) = @_;
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);
263 $conf->{lock} = $lock // 'create';
264 $class->write_config($vmid, $conf);
265 });
266 }
267
268 # destroys configuration, only applicable for configs owned by the callers node.
269 # dies if removal fails, e.g., when inquorate.
270 sub 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
277 # moves configuration owned by calling node to the target node.
278 # dies if renaming fails.
279 # NOTE: in PVE a node owns the config (hard requirement), so only the owning
280 # node may move the config to another node, which then becomes the new owner.
281 sub move_config_to_node {
282 my ($class, $vmid, $target_node) = @_;
283
284 my $config_fn = $class->config_file($vmid);
285 my $new_config_fn = $class->config_file($vmid, $target_node);
286
287 rename($config_fn, $new_config_fn)
288 or die "failed to move config file to node '$target_node': $!\n";
289 }
290
291 my $lock_file_full_wrapper = sub {
292 my ($class, $vmid, $timeout, $shared, $realcode, @param) = @_;
293
294 my $filename = $class->config_file_lock($vmid);
295
296 # make sure configuration file is up-to-date
297 my $code = sub {
298 PVE::Cluster::cfs_update();
299 $realcode->(@_);
300 };
301
302 my $res = lock_file_full($filename, $timeout, $shared, $code, @param);
303
304 die $@ if $@;
305
306 return $res;
307 };
308
309 # Lock config file using non-exclusive ("read") flock, run $code with @param, unlock config file.
310 # $timeout is the maximum time to acquire the flock
311 sub lock_config_shared {
312 my ($class, $vmid, $timeout, $code, @param) = @_;
313
314 return $lock_file_full_wrapper->($class, $vmid, $timeout, 1, $code, @param);
315 }
316
317 # Lock config file using flock, run $code with @param, unlock config file.
318 # $timeout is the maximum time to acquire the flock
319 sub lock_config_full {
320 my ($class, $vmid, $timeout, $code, @param) = @_;
321
322 return $lock_file_full_wrapper->($class, $vmid, $timeout, 0, $code, @param);
323 }
324
325
326 # Lock config file using flock, run $code with @param, unlock config file.
327 sub lock_config {
328 my ($class, $vmid, $code, @param) = @_;
329
330 return $class->lock_config_full($vmid, 10, $code, @param);
331 }
332
333 # Checks whether the config is locked with the lock parameter
334 sub check_lock {
335 my ($class, $conf) = @_;
336
337 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
338 }
339
340 # Returns whether the config is locked with the lock parameter, also checks
341 # whether the lock value is correct if the optional $lock is set.
342 sub has_lock {
343 my ($class, $conf, $lock) = @_;
344
345 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
346 }
347
348 # Sets the lock parameter for this VM/CT's config to $lock.
349 sub set_lock {
350 my ($class, $vmid, $lock) = @_;
351
352 my $conf;
353 $class->lock_config($vmid, sub {
354 $conf = $class->load_config($vmid);
355 $class->check_lock($conf);
356 $conf->{lock} = $lock;
357 $class->write_config($vmid, $conf);
358 });
359 return $conf;
360 }
361
362 # Removes the lock parameter for this VM/CT's config, also checks whether
363 # the lock value is correct if the optional $lock is set.
364 sub remove_lock {
365 my ($class, $vmid, $lock) = @_;
366
367 $class->lock_config($vmid, sub {
368 my $conf = $class->load_config($vmid);
369 if (!$conf->{lock}) {
370 my $lockstring = defined($lock) ? "'$lock' " : "any";
371 die "no lock found trying to remove $lockstring lock\n";
372 } elsif (defined($lock) && $conf->{lock} ne $lock) {
373 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
374 }
375 delete $conf->{lock};
376 $class->write_config($vmid, $conf);
377 });
378 }
379
380 # Checks whether protection mode is enabled for this VM/CT.
381 sub check_protection {
382 my ($class, $conf, $err_msg) = @_;
383
384 if ($conf->{protection}) {
385 die "$err_msg - protection mode enabled\n";
386 }
387 }
388
389 # Returns a list of keys where used volumes can be located
390 sub valid_volume_keys {
391 my ($class, $reverse) = @_;
392
393 die "implement me - abstract method\n";
394 }
395
396 # Returns a hash with the information from $volume_string
397 # $key is used to determine the format of the string
398 sub parse_volume {
399 my ($class, $key, $volume_string, $noerr) = @_;
400
401 die "implement me - abstract method\n";
402 }
403
404 # Returns a string with the information from $volume
405 # $key is used to determine the format of the string
406 sub print_volume {
407 my ($class, $key, $volume) = @_;
408
409 die "implement me - abstract method\n";
410 }
411
412 # The key under which the volume ID is located in volume hashes
413 sub volid_key {
414 my ($class) = @_;
415
416 die "implement me - abstract method\n";
417 }
418
419 # Adds an unused volume to $config, if possible.
420 sub add_unused_volume {
421 my ($class, $config, $volid) = @_;
422
423 my $key;
424 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
425 my $test = "unused$ind";
426 if (my $vid = $config->{$test}) {
427 return if $vid eq $volid; # do not add duplicates
428 } else {
429 $key = $test;
430 }
431 }
432
433 die "Too many unused volumes - please delete them first.\n" if !$key;
434
435 $config->{$key} = $volid;
436
437 return $key;
438 }
439
440 # Iterate over all unused volumes, calling $func for each key/value pair
441 # with additional parameters @param.
442 sub foreach_unused_volume {
443 my ($class, $conf, $func, @param) = @_;
444
445 foreach my $key (keys %{$conf}) {
446 if ($key =~ m/^unused\d+$/) {
447 my $volume = $class->parse_volume($key, $conf->{$key});
448 $func->($key, $volume, @param);
449 }
450 }
451 }
452
453 # Iterate over all configured volumes, calling $func for each key/value pair
454 # with additional parameters @param.
455 # By default, unused volumes and specials like vmstate are excluded.
456 # Options: reverse - reverses the order for the iteration
457 # include_unused - also iterate over unused volumes
458 # extra_keys - an array of extra keys to use for the iteration
459 sub foreach_volume_full {
460 my ($class, $conf, $opts, $func, @param) = @_;
461
462 die "'reverse' iteration only supported for default keys\n"
463 if $opts->{reverse} && ($opts->{extra_keys} || $opts->{include_unused});
464
465 my @keys = $class->valid_volume_keys($opts->{reverse});
466 push @keys, @{$opts->{extra_keys}} if $opts->{extra_keys};
467
468 foreach my $key (@keys) {
469 my $volume_string = $conf->{$key};
470 next if !defined($volume_string);
471
472 my $volume = $class->parse_volume($key, $volume_string, 1);
473 next if !defined($volume);
474
475 $func->($key, $volume, @param);
476 }
477
478 $class->foreach_unused_volume($conf, $func, @param) if $opts->{include_unused};
479 }
480
481 sub foreach_volume {
482 my ($class, $conf, $func, @param) = @_;
483
484 return $class->foreach_volume_full($conf, undef, $func, @param);
485 }
486
487 # $volume_map is a hash of 'old_volid' => 'new_volid' pairs.
488 # This method replaces 'old_volid' by 'new_volid' throughout the config including snapshots, pending
489 # changes, unused volumes and vmstate volumes.
490 sub update_volume_ids {
491 my ($class, $conf, $volume_map) = @_;
492
493 my $volid_key = $class->volid_key();
494
495 my $do_replace = sub {
496 my ($key, $volume, $current_section) = @_;
497
498 my $old_volid = $volume->{$volid_key};
499 if (my $new_volid = $volume_map->{$old_volid}) {
500 $volume->{$volid_key} = $new_volid;
501 $current_section->{$key} = $class->print_volume($key, $volume);
502 }
503 };
504
505 my $opts = {
506 'include_unused' => 1,
507 'extra_keys' => ['vmstate'],
508 };
509
510 $class->foreach_volume_full($conf, $opts, $do_replace, $conf);
511 foreach my $snap (keys %{$conf->{snapshots}}) {
512 my $snap_conf = $conf->{snapshots}->{$snap};
513 $class->foreach_volume_full($snap_conf, $opts, $do_replace, $snap_conf);
514 }
515
516 $class->foreach_volume_full($conf->{pending}, $opts, $do_replace, $conf->{pending});
517 }
518
519 # Returns whether the template parameter is set in $conf.
520 sub is_template {
521 my ($class, $conf) = @_;
522
523 return 1 if defined $conf->{template} && $conf->{template} == 1;
524 }
525
526 # Checks whether $feature is available for the referenced volumes in $conf.
527 # Note: depending on the parameters, some volumes may be skipped!
528 sub has_feature {
529 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
530 die "implement me - abstract method\n";
531 }
532
533 # get all replicatable volume (hash $res->{$volid} = 1)
534 # $cleanup: for cleanup - simply ignores volumes without replicate feature
535 # $norerr: never raise exceptions - return undef instead
536 sub get_replicatable_volumes {
537 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
538
539 die "implement me - abstract method\n";
540 }
541
542 # Returns all the guests volumes which would be included in a vzdump job
543 # Return Format (array-ref with hash-refs as elements):
544 # [
545 # {
546 # key, key in the config, e.g. mp0, scsi0,...
547 # included, boolean
548 # reason, string
549 # volume_config volume object as returned from foreach_volume()
550 # },
551 # ]
552 sub get_backup_volumes {
553 my ($class, $conf) = @_;
554
555 die "implement me - abstract method\n";
556 }
557
558 # Internal snapshots
559
560 # NOTE: Snapshot create/delete involves several non-atomic
561 # actions, and can take a long time.
562 # So we try to avoid locking the file and use the 'lock' variable
563 # inside the config file instead.
564
565 # Save the vmstate (RAM).
566 sub __snapshot_save_vmstate {
567 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
568 die "implement me - abstract method\n";
569 }
570
571 # Check whether the VM/CT is running.
572 sub __snapshot_check_running {
573 my ($class, $vmid) = @_;
574 die "implement me - abstract method\n";
575 }
576
577 # Check whether we need to freeze the VM/CT
578 sub __snapshot_check_freeze_needed {
579 my ($sself, $vmid, $config, $save_vmstate) = @_;
580 die "implement me - abstract method\n";
581 }
582
583 # Freeze or unfreeze the VM/CT.
584 sub __snapshot_freeze {
585 my ($class, $vmid, $unfreeze) = @_;
586
587 die "abstract method - implement me\n";
588 }
589
590 # Code run before and after creating all the volume snapshots
591 # base: noop
592 sub __snapshot_create_vol_snapshots_hook {
593 my ($class, $vmid, $snap, $running, $hook) = @_;
594
595 return;
596 }
597
598 # Create the volume snapshots for the VM/CT.
599 sub __snapshot_create_vol_snapshot {
600 my ($class, $vmid, $vs, $volume, $snapname) = @_;
601
602 die "abstract method - implement me\n";
603 }
604
605 # Remove a drive from the snapshot config.
606 sub __snapshot_delete_remove_drive {
607 my ($class, $snap, $drive) = @_;
608
609 die "abstract method - implement me\n";
610 }
611
612 # Delete the vmstate file/drive
613 sub __snapshot_delete_vmstate_file {
614 my ($class, $snap, $force) = @_;
615
616 die "abstract method - implement me\n";
617 }
618
619 # Delete a volume snapshot
620 sub __snapshot_delete_vol_snapshot {
621 my ($class, $vmid, $vs, $volume, $snapname) = @_;
622
623 die "abstract method - implement me\n";
624 }
625
626 # called during rollback prepare, and between config rollback and starting guest
627 # can change config, e.g. for vmgenid
628 # $data is shared across calls and passed to vm_start
629 sub __snapshot_rollback_hook {
630 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
631
632 return;
633 }
634
635 # Checks whether a volume snapshot is possible for this volume.
636 sub __snapshot_rollback_vol_possible {
637 my ($class, $volume, $snapname) = @_;
638
639 die "abstract method - implement me\n";
640 }
641
642 # Rolls back this volume.
643 sub __snapshot_rollback_vol_rollback {
644 my ($class, $volume, $snapname) = @_;
645
646 die "abstract method - implement me\n";
647 }
648
649 # Stops the VM/CT for a rollback.
650 sub __snapshot_rollback_vm_stop {
651 my ($class, $vmid) = @_;
652
653 die "abstract method - implement me\n";
654 }
655
656 # Start the VM/CT after a rollback with restored vmstate.
657 sub __snapshot_rollback_vm_start {
658 my ($class, $vmid, $vmstate, $data);
659
660 die "abstract method - implement me\n";
661 }
662
663 # Get list of volume IDs which are referenced in $conf, but not in $snap.
664 sub __snapshot_rollback_get_unused {
665 my ($class, $conf, $snap) = @_;
666
667 die "abstract method - implement me\n";
668 }
669
670 # Copy the current config $source to the snapshot config $dest
671 sub __snapshot_copy_config {
672 my ($class, $source, $dest) = @_;
673
674 foreach my $k (keys %$source) {
675 next if $k eq 'snapshots';
676 next if $k eq 'snapstate';
677 next if $k eq 'snaptime';
678 next if $k eq 'vmstate';
679 next if $k eq 'lock';
680 next if $k eq 'digest';
681 next if $k eq 'description';
682 next if $k =~ m/^unused\d+$/;
683
684 $dest->{$k} = $source->{$k};
685 }
686 };
687
688 # Apply the snapshot config $snap to the config $conf (rollback)
689 sub __snapshot_apply_config {
690 my ($class, $conf, $snap) = @_;
691
692 # copy snapshot list
693 my $newconf = {
694 snapshots => $conf->{snapshots},
695 };
696
697 # keep description and list of unused disks
698 foreach my $k (keys %$conf) {
699 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
700
701 $newconf->{$k} = $conf->{$k};
702 }
703
704 $class->__snapshot_copy_config($snap, $newconf);
705
706 return $newconf;
707 }
708
709 # Prepares the configuration for snapshotting.
710 sub __snapshot_prepare {
711 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
712
713 my $snap;
714
715 my $updatefn = sub {
716
717 my $conf = $class->load_config($vmid);
718
719 die "you can't take a snapshot if it's a template\n"
720 if $class->is_template($conf);
721
722 $class->check_lock($conf);
723
724 $conf->{lock} = 'snapshot';
725
726 my $snapshots = $conf->{snapshots};
727
728 die "snapshot name '$snapname' already used\n"
729 if defined($snapshots->{$snapname});
730
731 my $storecfg = PVE::Storage::config();
732 die "snapshot feature is not available\n"
733 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
734
735 for my $snap (sort keys %$snapshots) {
736 my $parent_name = $snapshots->{$snap}->{parent} // '';
737 if ($snapname eq $parent_name) {
738 warn "deleting parent '$parent_name' reference from '$snap' to avoid bogus snapshot cycle.\n";
739 delete $snapshots->{$snap}->{parent};
740 }
741 }
742
743 $snap = $snapshots->{$snapname} = {};
744
745 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
746 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
747 }
748
749 $class->__snapshot_copy_config($conf, $snap);
750
751 $snap->{snapstate} = "prepare";
752 $snap->{snaptime} = time();
753 $snap->{description} = $comment if $comment;
754
755 $class->write_config($vmid, $conf);
756 };
757
758 $class->lock_config($vmid, $updatefn);
759
760 return $snap;
761 }
762
763 # Commits the configuration after snapshotting.
764 sub __snapshot_commit {
765 my ($class, $vmid, $snapname) = @_;
766
767 my $updatefn = sub {
768
769 my $conf = $class->load_config($vmid);
770
771 die "missing snapshot lock\n"
772 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
773
774 my $snap = $conf->{snapshots}->{$snapname};
775 die "snapshot '$snapname' does not exist\n" if !defined($snap);
776
777 die "wrong snapshot state\n"
778 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
779
780 delete $snap->{snapstate};
781 delete $conf->{lock};
782
783 $conf->{parent} = $snapname;
784
785 $class->write_config($vmid, $conf);
786 };
787
788 $class->lock_config($vmid, $updatefn);
789 }
790
791 # Activates the storages affected by the snapshot operations.
792 sub __snapshot_activate_storages {
793 my ($class, $conf, $include_vmstate) = @_;
794
795 # FIXME PVE 8.x (or earlier with break/versioned-depens): uncomment and drop return
796 # die "implement me";
797 return;
798 }
799
800 # Creates a snapshot for the VM/CT.
801 sub snapshot_create {
802 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
803
804 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
805
806 $save_vmstate = 0 if !$snap->{vmstate};
807
808 my $conf = $class->load_config($vmid);
809
810 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
811
812 my $drivehash = {};
813
814 eval {
815 $class->__snapshot_activate_storages($conf, 0);
816
817 if ($freezefs) {
818 $class->__snapshot_freeze($vmid, 0);
819 }
820
821 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
822
823 $class->foreach_volume($snap, sub {
824 my ($vs, $volume) = @_;
825
826 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
827 $drivehash->{$vs} = 1;
828 });
829 };
830 my $err = $@;
831
832 if ($running) {
833 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
834 if ($freezefs) {
835 $class->__snapshot_freeze($vmid, 1);
836 }
837 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
838 }
839
840 if ($err) {
841 warn "snapshot create failed: starting cleanup\n";
842 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
843 warn "$@" if $@;
844 die "$err\n";
845 }
846
847 $class->__snapshot_commit($vmid, $snapname);
848 }
849
850 # Check if the snapshot might still be needed by a replication job.
851 my $snapshot_delete_assert_not_needed_by_replication = sub {
852 my ($class, $vmid, $conf, $snap, $snapname) = @_;
853
854 my $repl_conf = PVE::ReplicationConfig->new();
855 return if !$repl_conf->check_for_existing_jobs($vmid, 1);
856
857 my $storecfg = PVE::Storage::config();
858
859 # Current config's volumes are relevant for replication.
860 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
861
862 my $replication_jobs = $repl_conf->list_guests_local_replication_jobs($vmid);
863
864 $class->foreach_volume($snap, sub {
865 my ($vs, $volume) = @_;
866
867 my $volid_key = $class->volid_key();
868 my $volid = $volume->{$volid_key};
869
870 return if !$volumes->{$volid};
871
872 my $snapshots = PVE::Storage::volume_snapshot_info($storecfg, $volid);
873
874 for my $job ($replication_jobs->@*) {
875 my $jobid = $job->{id};
876
877 my @jobs_snapshots = grep {
878 PVE::Replication::is_replication_snapshot($_, $jobid)
879 } keys $snapshots->%*;
880
881 next if scalar(@jobs_snapshots) > 0;
882
883 die "snapshot '$snapname' needed by replication job '$jobid' - run replication first\n";
884 }
885 });
886 };
887
888 # Deletes a snapshot.
889 # Note: $drivehash is only set when called from snapshot_create.
890 sub snapshot_delete {
891 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
892
893 my $unused = [];
894
895 my $conf = $class->load_config($vmid);
896 my $snap = $conf->{snapshots}->{$snapname};
897
898 die "snapshot '$snapname' does not exist\n" if !defined($snap);
899
900 $class->__snapshot_activate_storages($snap, 1) if !$drivehash;
901
902 $snapshot_delete_assert_not_needed_by_replication->($class, $vmid, $conf, $snap, $snapname)
903 if !$drivehash && !$force;
904
905 $class->set_lock($vmid, 'snapshot-delete')
906 if (!$drivehash); # doesn't already have a 'snapshot' lock
907
908 my $expected_lock = $drivehash ? 'snapshot' : 'snapshot-delete';
909
910 my $ensure_correct_lock = sub {
911 my ($conf) = @_;
912
913 die "encountered invalid lock, expected '$expected_lock'\n"
914 if !$class->has_lock($conf, $expected_lock);
915 };
916
917 my $unlink_parent = sub {
918 my ($confref, $new_parent) = @_;
919
920 if ($confref->{parent} && $confref->{parent} eq $snapname) {
921 if ($new_parent) {
922 $confref->{parent} = $new_parent;
923 } else {
924 delete $confref->{parent};
925 }
926 }
927 };
928
929 my $remove_drive = sub {
930 my ($drive) = @_;
931
932 my $conf = $class->load_config($vmid);
933 $ensure_correct_lock->($conf);
934
935 $snap = $conf->{snapshots}->{$snapname};
936 die "snapshot '$snapname' does not exist\n" if !defined($snap);
937
938 $class->__snapshot_delete_remove_drive($snap, $drive);
939
940 $class->write_config($vmid, $conf);
941 };
942
943 #prepare
944 $class->lock_config($vmid, sub {
945 my $conf = $class->load_config($vmid);
946 $ensure_correct_lock->($conf);
947
948 die "you can't delete a snapshot if vm is a template\n"
949 if $class->is_template($conf);
950
951 $snap = $conf->{snapshots}->{$snapname};
952 die "snapshot '$snapname' does not exist\n" if !defined($snap);
953
954 $snap->{snapstate} = 'delete';
955
956 $class->write_config($vmid, $conf);
957 });
958
959 # now remove vmstate file
960 if ($snap->{vmstate}) {
961 $class->__snapshot_delete_vmstate_file($snap, $force);
962
963 # save changes (remove vmstate from snapshot)
964 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
965 };
966
967 # now remove all volume snapshots
968 $class->foreach_volume($snap, sub {
969 my ($vs, $volume) = @_;
970
971 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
972 if (!$drivehash || $drivehash->{$vs}) {
973 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
974 if (my $err = $@) {
975 die $err if !$force;
976 warn $err;
977 }
978 }
979
980 # save changes (remove drive from snapshot)
981 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
982 });
983
984 # now cleanup config
985 $class->lock_config($vmid, sub {
986 my $conf = $class->load_config($vmid);
987 $ensure_correct_lock->($conf);
988
989 $snap = $conf->{snapshots}->{$snapname};
990 die "snapshot '$snapname' does not exist\n" if !defined($snap);
991
992 # remove parent refs
993 &$unlink_parent($conf, $snap->{parent});
994 foreach my $sn (keys %{$conf->{snapshots}}) {
995 next if $sn eq $snapname;
996 &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
997 }
998
999
1000 delete $conf->{snapshots}->{$snapname};
1001 delete $conf->{lock};
1002 foreach my $volid (@$unused) {
1003 $class->add_unused_volume($conf, $volid);
1004 }
1005
1006 $class->write_config($vmid, $conf);
1007 });
1008 }
1009
1010 # Remove replication snapshots to make a rollback possible.
1011 my $rollback_remove_replication_snapshots = sub {
1012 my ($class, $vmid, $snap, $snapname) = @_;
1013
1014 my $storecfg = PVE::Storage::config();
1015
1016 my $repl_conf = PVE::ReplicationConfig->new();
1017 return if !$repl_conf->check_for_existing_jobs($vmid, 1);
1018
1019 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $snap, 1);
1020
1021 # For these, all replication snapshots need to be removed for backwards compatibility.
1022 my $volids = [];
1023
1024 # For these, we know more and can remove only the required replication snapshots.
1025 my $blocking_snapshots = {};
1026
1027 # filter by what we actually iterate over below (excludes vmstate!)
1028 $class->foreach_volume($snap, sub {
1029 my ($vs, $volume) = @_;
1030
1031 my $volid_key = $class->volid_key();
1032 my $volid = $volume->{$volid_key};
1033
1034 return if !$volumes->{$volid};
1035
1036 my $blockers = [];
1037 eval { $class->__snapshot_rollback_vol_possible($volume, $snapname, $blockers); };
1038 if (my $err = $@) {
1039 # FIXME die instead, once $blockers is required by the storage plugin API
1040 # and the guest plugins are required to be new enough to support it too.
1041 # Currently, it's not possible to distinguish between blockers being empty
1042 # because the plugin is old versus because there is a different error.
1043 if (scalar($blockers->@*) == 0) {
1044 push @{$volids}, $volid;
1045 return;
1046 }
1047
1048 for my $blocker ($blockers->@*) {
1049 die $err if !PVE::Replication::is_replication_snapshot($blocker);
1050 }
1051
1052 $blocking_snapshots->{$volid} = $blockers;
1053 }
1054 });
1055
1056 my $removed_repl_snapshot;
1057 for my $volid (sort keys $blocking_snapshots->%*) {
1058 my $blockers = $blocking_snapshots->{$volid};
1059
1060 for my $blocker ($blockers->@*) {
1061 warn "WARN: removing replication snapshot '$volid\@$blocker'\n";
1062 $removed_repl_snapshot = 1;
1063 eval { PVE::Storage::volume_snapshot_delete($storecfg, $volid, $blocker); };
1064 die $@ if $@;
1065 }
1066 }
1067 warn "WARN: you shouldn't remove '$snapname' before running the next replication!\n"
1068 if $removed_repl_snapshot;
1069
1070 # Need to keep using a hammer for backwards compatibility...
1071 # remove all local replication snapshots (jobid => undef)
1072 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
1073 PVE::Replication::prepare($storecfg, $volids, undef, 1, undef, $logfunc);
1074 };
1075
1076 # Rolls back to a given snapshot.
1077 sub snapshot_rollback {
1078 my ($class, $vmid, $snapname) = @_;
1079
1080 my $prepare = 1;
1081
1082 my $data = {};
1083
1084 my $get_snapshot_config = sub {
1085 my ($conf) = @_;
1086
1087 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
1088
1089 my $res = $conf->{snapshots}->{$snapname};
1090
1091 die "snapshot '$snapname' does not exist\n" if !defined($res);
1092
1093 return $res;
1094 };
1095
1096 my $snap;
1097
1098 my $updatefn = sub {
1099 my $conf = $class->load_config($vmid);
1100 $snap = $get_snapshot_config->($conf);
1101
1102 if ($prepare) {
1103 $class->__snapshot_activate_storages($snap, 1);
1104
1105 $rollback_remove_replication_snapshots->($class, $vmid, $snap, $snapname);
1106
1107 $class->foreach_volume($snap, sub {
1108 my ($vs, $volume) = @_;
1109
1110 $class->__snapshot_rollback_vol_possible($volume, $snapname);
1111 });
1112 }
1113
1114 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
1115 if $snap->{snapstate};
1116
1117 if ($prepare) {
1118 $class->check_lock($conf);
1119 $class->__snapshot_rollback_vm_stop($vmid);
1120 }
1121
1122 die "unable to rollback vm $vmid: vm is running\n"
1123 if $class->__snapshot_check_running($vmid);
1124
1125 if ($prepare) {
1126 $conf->{lock} = 'rollback';
1127 } else {
1128 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
1129 delete $conf->{lock};
1130
1131 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
1132
1133 foreach my $volid (@$unused) {
1134 $class->add_unused_volume($conf, $volid);
1135 }
1136
1137 # copy snapshot config to current config
1138 $conf = $class->__snapshot_apply_config($conf, $snap);
1139 $conf->{parent} = $snapname;
1140 }
1141
1142 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
1143
1144 $class->write_config($vmid, $conf);
1145
1146 if (!$prepare && $snap->{vmstate}) {
1147 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data);
1148 }
1149 };
1150
1151 $class->lock_config($vmid, $updatefn);
1152
1153 $class->foreach_volume($snap, sub {
1154 my ($vs, $volume) = @_;
1155
1156 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
1157 });
1158
1159 $prepare = 0;
1160 $class->lock_config($vmid, $updatefn);
1161 }
1162
1163 # bash completion helper
1164
1165 sub snapshot_list {
1166 my ($class, $vmid) = @_;
1167
1168 my $snapshot = eval {
1169 my $conf = $class->load_config($vmid);
1170 my $snapshots = $conf->{snapshots} || [];
1171 [ sort keys %$snapshots ]
1172 } || [];
1173
1174 return $snapshot;
1175 }
1176
1177 1;