]> git.proxmox.com Git - pve-guest-common.git/blob - PVE/AbstractConfig.pm
mention prune behavior for the remove parameter
[pve-guest-common.git] / PVE / AbstractConfig.pm
1 package PVE::AbstractConfig;
2
3 use strict;
4 use warnings;
5
6 use PVE::Tools qw(lock_file lock_file_full);
7 use PVE::INotify;
8 use PVE::Cluster;
9 use PVE::Storage;
10
11 use PVE::GuestHelpers qw(typesafe_ne);
12 use PVE::ReplicationConfig;
13 use PVE::Replication;
14
15 my $nodename = PVE::INotify::nodename();
16
17 # Printable string, currently either "VM" or "CT"
18 sub guest_type {
19 my ($class) = @_;
20 die "abstract method - implement me ";
21 }
22
23 sub __config_max_unused_disks {
24 my ($class) = @_;
25
26 die "implement me - abstract method\n";
27 }
28
29 # Path to the flock file for this VM/CT
30 sub config_file_lock {
31 my ($class, $vmid) = @_;
32 die "abstract method - implement me";
33 }
34
35 # Relative config file path for this VM/CT in CFS
36 sub cfs_config_path {
37 my ($class, $vmid, $node) = @_;
38 die "abstract method - implement me";
39 }
40
41 # Absolute config file path for this VM/CT
42 sub config_file {
43 my ($class, $vmid, $node) = @_;
44
45 my $cfspath = $class->cfs_config_path($vmid, $node);
46 return "/etc/pve/$cfspath";
47 }
48
49 # Read and parse config file for this VM/CT
50 sub load_config {
51 my ($class, $vmid, $node) = @_;
52
53 $node = $nodename if !$node;
54 my $cfspath = $class->cfs_config_path($vmid, $node);
55
56 my $conf = PVE::Cluster::cfs_read_file($cfspath);
57 die "Configuration file '$cfspath' does not exist\n"
58 if !defined($conf);
59
60 return $conf;
61 }
62
63 # Generate and write config file for this VM/CT
64 sub write_config {
65 my ($class, $vmid, $conf) = @_;
66
67 my $cfspath = $class->cfs_config_path($vmid);
68
69 PVE::Cluster::cfs_write_file($cfspath, $conf);
70 }
71
72 # Pending changes related
73
74 sub parse_pending_delete {
75 my ($class, $data) = @_;
76
77 return {} if !$data;
78
79 $data =~ s/[,;]/ /g;
80 $data =~ s/^\s+//;
81
82 my $pending_deletions = {};
83 for my $entry (split(/\s+/, $data)) {
84 my ($force, $key) = $entry =~ /^(!?)(.*)$/;
85 $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
489 # the config including snapshots and unused 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
517 # Returns whether the template parameter is set in $conf.
518 sub is_template {
519 my ($class, $conf) = @_;
520
521 return 1 if defined $conf->{template} && $conf->{template} == 1;
522 }
523
524 # Checks whether $feature is available for the referenced volumes in $conf.
525 # Note: depending on the parameters, some volumes may be skipped!
526 sub has_feature {
527 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
528 die "implement me - abstract method\n";
529 }
530
531 # get all replicatable volume (hash $res->{$volid} = 1)
532 # $cleanup: for cleanup - simply ignores volumes without replicate feature
533 # $norerr: never raise exceptions - return undef instead
534 sub get_replicatable_volumes {
535 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
536
537 die "implement me - abstract method\n";
538 }
539
540 # Returns all the guests volumes which would be included in a vzdump job
541 # Return Format (array-ref with hash-refs as elements):
542 # [
543 # {
544 # key, key in the config, e.g. mp0, scsi0,...
545 # included, boolean
546 # reason, string
547 # volume_config volume object as returned from foreach_volume()
548 # },
549 # ]
550 sub get_backup_volumes {
551 my ($class, $conf) = @_;
552
553 die "implement me - abstract method\n";
554 }
555
556 # Internal snapshots
557
558 # NOTE: Snapshot create/delete involves several non-atomic
559 # actions, and can take a long time.
560 # So we try to avoid locking the file and use the 'lock' variable
561 # inside the config file instead.
562
563 # Save the vmstate (RAM).
564 sub __snapshot_save_vmstate {
565 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
566 die "implement me - abstract method\n";
567 }
568
569 # Check whether the VM/CT is running.
570 sub __snapshot_check_running {
571 my ($class, $vmid) = @_;
572 die "implement me - abstract method\n";
573 }
574
575 # Check whether we need to freeze the VM/CT
576 sub __snapshot_check_freeze_needed {
577 my ($sself, $vmid, $config, $save_vmstate) = @_;
578 die "implement me - abstract method\n";
579 }
580
581 # Freeze or unfreeze the VM/CT.
582 sub __snapshot_freeze {
583 my ($class, $vmid, $unfreeze) = @_;
584
585 die "abstract method - implement me\n";
586 }
587
588 # Code run before and after creating all the volume snapshots
589 # base: noop
590 sub __snapshot_create_vol_snapshots_hook {
591 my ($class, $vmid, $snap, $running, $hook) = @_;
592
593 return;
594 }
595
596 # Create the volume snapshots for the VM/CT.
597 sub __snapshot_create_vol_snapshot {
598 my ($class, $vmid, $vs, $volume, $snapname) = @_;
599
600 die "abstract method - implement me\n";
601 }
602
603 # Remove a drive from the snapshot config.
604 sub __snapshot_delete_remove_drive {
605 my ($class, $snap, $drive) = @_;
606
607 die "abstract method - implement me\n";
608 }
609
610 # Delete the vmstate file/drive
611 sub __snapshot_delete_vmstate_file {
612 my ($class, $snap, $force) = @_;
613
614 die "abstract method - implement me\n";
615 }
616
617 # Delete a volume snapshot
618 sub __snapshot_delete_vol_snapshot {
619 my ($class, $vmid, $vs, $volume, $snapname) = @_;
620
621 die "abstract method - implement me\n";
622 }
623
624 # called during rollback prepare, and between config rollback and starting guest
625 # can change config, e.g. for vmgenid
626 # $data is shared across calls and passed to vm_start
627 sub __snapshot_rollback_hook {
628 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
629
630 return;
631 }
632
633 # Checks whether a volume snapshot is possible for this volume.
634 sub __snapshot_rollback_vol_possible {
635 my ($class, $volume, $snapname) = @_;
636
637 die "abstract method - implement me\n";
638 }
639
640 # Rolls back this volume.
641 sub __snapshot_rollback_vol_rollback {
642 my ($class, $volume, $snapname) = @_;
643
644 die "abstract method - implement me\n";
645 }
646
647 # Stops the VM/CT for a rollback.
648 sub __snapshot_rollback_vm_stop {
649 my ($class, $vmid) = @_;
650
651 die "abstract method - implement me\n";
652 }
653
654 # Start the VM/CT after a rollback with restored vmstate.
655 sub __snapshot_rollback_vm_start {
656 my ($class, $vmid, $vmstate, $data);
657
658 die "abstract method - implement me\n";
659 }
660
661 # Get list of volume IDs which are referenced in $conf, but not in $snap.
662 sub __snapshot_rollback_get_unused {
663 my ($class, $conf, $snap) = @_;
664
665 die "abstract method - implement me\n";
666 }
667
668 # Copy the current config $source to the snapshot config $dest
669 sub __snapshot_copy_config {
670 my ($class, $source, $dest) = @_;
671
672 foreach my $k (keys %$source) {
673 next if $k eq 'snapshots';
674 next if $k eq 'snapstate';
675 next if $k eq 'snaptime';
676 next if $k eq 'vmstate';
677 next if $k eq 'lock';
678 next if $k eq 'digest';
679 next if $k eq 'description';
680 next if $k =~ m/^unused\d+$/;
681
682 $dest->{$k} = $source->{$k};
683 }
684 };
685
686 # Apply the snapshot config $snap to the config $conf (rollback)
687 sub __snapshot_apply_config {
688 my ($class, $conf, $snap) = @_;
689
690 # copy snapshot list
691 my $newconf = {
692 snapshots => $conf->{snapshots},
693 };
694
695 # keep description and list of unused disks
696 foreach my $k (keys %$conf) {
697 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
698
699 $newconf->{$k} = $conf->{$k};
700 }
701
702 $class->__snapshot_copy_config($snap, $newconf);
703
704 return $newconf;
705 }
706
707 # Prepares the configuration for snapshotting.
708 sub __snapshot_prepare {
709 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
710
711 my $snap;
712
713 my $updatefn = sub {
714
715 my $conf = $class->load_config($vmid);
716
717 die "you can't take a snapshot if it's a template\n"
718 if $class->is_template($conf);
719
720 $class->check_lock($conf);
721
722 $conf->{lock} = 'snapshot';
723
724 die "snapshot name '$snapname' already used\n"
725 if defined($conf->{snapshots}->{$snapname});
726
727 my $storecfg = PVE::Storage::config();
728 die "snapshot feature is not available\n"
729 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
730
731 $snap = $conf->{snapshots}->{$snapname} = {};
732
733 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
734 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
735 }
736
737 $class->__snapshot_copy_config($conf, $snap);
738
739 $snap->{snapstate} = "prepare";
740 $snap->{snaptime} = time();
741 $snap->{description} = $comment if $comment;
742
743 $class->write_config($vmid, $conf);
744 };
745
746 $class->lock_config($vmid, $updatefn);
747
748 return $snap;
749 }
750
751 # Commits the configuration after snapshotting.
752 sub __snapshot_commit {
753 my ($class, $vmid, $snapname) = @_;
754
755 my $updatefn = sub {
756
757 my $conf = $class->load_config($vmid);
758
759 die "missing snapshot lock\n"
760 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
761
762 my $snap = $conf->{snapshots}->{$snapname};
763 die "snapshot '$snapname' does not exist\n" if !defined($snap);
764
765 die "wrong snapshot state\n"
766 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
767
768 delete $snap->{snapstate};
769 delete $conf->{lock};
770
771 $conf->{parent} = $snapname;
772
773 $class->write_config($vmid, $conf);
774 };
775
776 $class->lock_config($vmid, $updatefn);
777 }
778
779 # Creates a snapshot for the VM/CT.
780 sub snapshot_create {
781 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
782
783 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
784
785 $save_vmstate = 0 if !$snap->{vmstate};
786
787 my $conf = $class->load_config($vmid);
788
789 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
790
791 my $drivehash = {};
792
793 eval {
794 if ($freezefs) {
795 $class->__snapshot_freeze($vmid, 0);
796 }
797
798 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
799
800 $class->foreach_volume($snap, sub {
801 my ($vs, $volume) = @_;
802
803 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
804 $drivehash->{$vs} = 1;
805 });
806 };
807 my $err = $@;
808
809 if ($running) {
810 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
811 if ($freezefs) {
812 $class->__snapshot_freeze($vmid, 1);
813 }
814 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
815 }
816
817 if ($err) {
818 warn "snapshot create failed: starting cleanup\n";
819 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
820 warn "$@" if $@;
821 die "$err\n";
822 }
823
824 $class->__snapshot_commit($vmid, $snapname);
825 }
826
827 # Deletes a snapshot.
828 # Note: $drivehash is only set when called from snapshot_create.
829 sub snapshot_delete {
830 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
831
832 my $prepare = 1;
833
834 my $unused = [];
835
836 my $conf = $class->load_config($vmid);
837 my $snap = $conf->{snapshots}->{$snapname};
838
839 die "snapshot '$snapname' does not exist\n" if !defined($snap);
840
841 $class->set_lock($vmid, 'snapshot-delete')
842 if (!$drivehash); # doesn't already have a 'snapshot' lock
843
844 my $expected_lock = $drivehash ? 'snapshot' : 'snapshot-delete';
845
846 my $ensure_correct_lock = sub {
847 my ($conf) = @_;
848
849 die "encountered invalid lock, expected '$expected_lock'\n"
850 if !$class->has_lock($conf, $expected_lock);
851 };
852
853 my $unlink_parent = sub {
854 my ($confref, $new_parent) = @_;
855
856 if ($confref->{parent} && $confref->{parent} eq $snapname) {
857 if ($new_parent) {
858 $confref->{parent} = $new_parent;
859 } else {
860 delete $confref->{parent};
861 }
862 }
863 };
864
865 my $remove_drive = sub {
866 my ($drive) = @_;
867
868 my $conf = $class->load_config($vmid);
869 $ensure_correct_lock->($conf);
870
871 $snap = $conf->{snapshots}->{$snapname};
872 die "snapshot '$snapname' does not exist\n" if !defined($snap);
873
874 $class->__snapshot_delete_remove_drive($snap, $drive);
875
876 $class->write_config($vmid, $conf);
877 };
878
879 #prepare
880 $class->lock_config($vmid, sub {
881 my $conf = $class->load_config($vmid);
882 $ensure_correct_lock->($conf);
883
884 die "you can't delete a snapshot if vm is a template\n"
885 if $class->is_template($conf);
886
887 $snap = $conf->{snapshots}->{$snapname};
888 die "snapshot '$snapname' does not exist\n" if !defined($snap);
889
890 $snap->{snapstate} = 'delete';
891
892 $class->write_config($vmid, $conf);
893 });
894
895 # now remove vmstate file
896 if ($snap->{vmstate}) {
897 $class->__snapshot_delete_vmstate_file($snap, $force);
898
899 # save changes (remove vmstate from snapshot)
900 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
901 };
902
903 # now remove all volume snapshots
904 $class->foreach_volume($snap, sub {
905 my ($vs, $volume) = @_;
906
907 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
908 if (!$drivehash || $drivehash->{$vs}) {
909 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
910 if (my $err = $@) {
911 die $err if !$force;
912 warn $err;
913 }
914 }
915
916 # save changes (remove drive from snapshot)
917 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
918 });
919
920 # now cleanup config
921 $class->lock_config($vmid, sub {
922 my $conf = $class->load_config($vmid);
923 $ensure_correct_lock->($conf);
924
925 $snap = $conf->{snapshots}->{$snapname};
926 die "snapshot '$snapname' does not exist\n" if !defined($snap);
927
928 # remove parent refs
929 &$unlink_parent($conf, $snap->{parent});
930 foreach my $sn (keys %{$conf->{snapshots}}) {
931 next if $sn eq $snapname;
932 &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
933 }
934
935
936 delete $conf->{snapshots}->{$snapname};
937 delete $conf->{lock};
938 foreach my $volid (@$unused) {
939 $class->add_unused_volume($conf, $volid);
940 }
941
942 $class->write_config($vmid, $conf);
943 });
944 }
945
946 # Rolls back to a given snapshot.
947 sub snapshot_rollback {
948 my ($class, $vmid, $snapname) = @_;
949
950 my $prepare = 1;
951
952 my $storecfg = PVE::Storage::config();
953
954 my $data = {};
955
956 my $get_snapshot_config = sub {
957 my ($conf) = @_;
958
959 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
960
961 my $res = $conf->{snapshots}->{$snapname};
962
963 die "snapshot '$snapname' does not exist\n" if !defined($res);
964
965 return $res;
966 };
967
968 my $snap;
969
970 my $updatefn = sub {
971 my $conf = $class->load_config($vmid);
972 $snap = $get_snapshot_config->($conf);
973
974 if ($prepare) {
975 my $repl_conf = PVE::ReplicationConfig->new();
976 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
977 # remove all replication snapshots
978 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
979 my $sorted_volids = [ sort keys %$volumes ];
980
981 # remove all local replication snapshots (jobid => undef)
982 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
983 PVE::Replication::prepare($storecfg, $sorted_volids, undef, 1, undef, $logfunc);
984 }
985
986 $class->foreach_volume($snap, sub {
987 my ($vs, $volume) = @_;
988
989 $class->__snapshot_rollback_vol_possible($volume, $snapname);
990 });
991 }
992
993 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
994 if $snap->{snapstate};
995
996 if ($prepare) {
997 $class->check_lock($conf);
998 $class->__snapshot_rollback_vm_stop($vmid);
999 }
1000
1001 die "unable to rollback vm $vmid: vm is running\n"
1002 if $class->__snapshot_check_running($vmid);
1003
1004 if ($prepare) {
1005 $conf->{lock} = 'rollback';
1006 } else {
1007 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
1008 delete $conf->{lock};
1009
1010 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
1011
1012 foreach my $volid (@$unused) {
1013 $class->add_unused_volume($conf, $volid);
1014 }
1015
1016 # copy snapshot config to current config
1017 $conf = $class->__snapshot_apply_config($conf, $snap);
1018 $conf->{parent} = $snapname;
1019 }
1020
1021 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
1022
1023 $class->write_config($vmid, $conf);
1024
1025 if (!$prepare && $snap->{vmstate}) {
1026 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $data);
1027 }
1028 };
1029
1030 $class->lock_config($vmid, $updatefn);
1031
1032 $class->foreach_volume($snap, sub {
1033 my ($vs, $volume) = @_;
1034
1035 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
1036 });
1037
1038 $prepare = 0;
1039 $class->lock_config($vmid, $updatefn);
1040 }
1041
1042 # bash completion helper
1043
1044 sub snapshot_list {
1045 my ($class, $vmid) = @_;
1046
1047 my $snapshot = eval {
1048 my $conf = $class->load_config($vmid);
1049 my $snapshots = $conf->{snapshots} || [];
1050 [ sort keys %$snapshots ]
1051 } || [];
1052
1053 return $snapshot;
1054 }
1055
1056 1;