1 package PVE
::AbstractConfig
;
6 use PVE
::Tools
qw(lock_file lock_file_full);
11 use PVE
::ReplicationConfig
;
14 my $nodename = PVE
::INotify
::nodename
();
16 # Printable string, currently either "VM" or "CT"
19 die "abstract method - implement me ";
22 sub __config_max_unused_disks
{
25 die "implement me - abstract method\n";
28 # Path to the flock file for this VM/CT
29 sub config_file_lock
{
30 my ($class, $vmid) = @_;
31 die "abstract method - implement me";
34 # Relative config file path for this VM/CT in CFS
36 my ($class, $vmid, $node) = @_;
37 die "abstract method - implement me";
40 # Absolute config file path for this VM/CT
42 my ($class, $vmid, $node) = @_;
44 my $cfspath = $class->cfs_config_path($vmid, $node);
45 return "/etc/pve/$cfspath";
48 # Read and parse config file for this VM/CT
50 my ($class, $vmid, $node) = @_;
52 $node = $nodename if !$node;
53 my $cfspath = $class->cfs_config_path($vmid, $node);
55 my $conf = PVE
::Cluster
::cfs_read_file
($cfspath);
56 die "Configuration file '$cfspath' does not exist\n"
62 # Generate and write config file for this VM/CT
64 my ($class, $vmid, $conf) = @_;
66 my $cfspath = $class->cfs_config_path($vmid);
68 PVE
::Cluster
::cfs_write_file
($cfspath, $conf);
71 # Pending changes related
73 sub parse_pending_delete
{
74 my ($class, $data) = @_;
81 my $pending_deletions = {};
82 for my $entry (split(/\s+/, $data)) {
83 my ($force, $key) = $entry =~ /^(!?)(.*)$/;
85 $pending_deletions->{$key} = {
86 force
=> $force ?
1 : 0,
90 return $pending_deletions;
93 sub print_pending_delete
{
94 my ($class, $delete_hash) = @_;
96 my $render_key = sub {
98 $key = "!$key" if $delete_hash->{$key}->{force
};
102 join (',', map { $render_key->($_) } sort keys %$delete_hash);
105 sub add_to_pending_delete
{
106 my ($class, $conf, $key, $force) = @_;
108 $conf->{pending
} //= {};
109 my $pending = $conf->{pending
};
110 my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
112 $pending_delete_hash->{$key} = { force
=> $force };
114 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
119 sub remove_from_pending_delete
{
120 my ($class, $conf, $key) = @_;
122 my $pending = $conf->{pending
};
123 my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
125 return $conf if ! exists $pending_delete_hash->{$key};
127 delete $pending_delete_hash->{$key};
129 if (%$pending_delete_hash) {
130 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
132 delete $pending->{delete};
138 sub cleanup_pending
{
139 my ($class, $conf) = @_;
141 my $pending = $conf->{pending
};
142 # remove pending changes when nothing changed
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})) {
148 delete $pending->{$opt};
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};
162 if (%$pending_delete_hash) {
163 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
165 delete $pending->{delete};
171 sub get_partial_fast_plug_option
{
174 die "abstract method - implement me ";
177 sub partial_fast_plug
{
178 my ($class, $conf, $opt) = @_;
180 my $partial_fast_plug_option = $class->get_partial_fast_plug_option();
181 my $format = $partial_fast_plug_option->{$opt}->{fmt
};
182 my $fast_pluggable = $partial_fast_plug_option->{$opt}->{properties
};
185 if (exists($conf->{$opt})) {
186 $configured = PVE
::JSONSchema
::parse_property_string
($format, $conf->{$opt});
188 my $pending = PVE
::JSONSchema
::parse_property_string
($format, $conf->{pending
}->{$opt});
192 # merge configured and pending opts to iterate
193 my @all_keys = keys %{{ %$pending, %$configured }};
195 foreach my $subopt (@all_keys) {
196 my $type = $format->{$subopt}->{type
};
197 if (PVE
::GuestHelpers
::typesafe_ne
($configured->{$subopt}, $pending->{$subopt}, $type)) {
198 if ($fast_pluggable->{$subopt}) {
199 $configured->{$subopt} = $pending->{$subopt};
205 # if there're no keys in $configured (after merge) there shouldn't be anything to change
206 if (keys %$configured) {
207 $conf->{$opt} = PVE
::JSONSchema
::print_property_string
($configured, $format);
214 sub load_snapshot_config
{
215 my ($class, $vmid, $snapname) = @_;
217 my $conf = $class->load_config($vmid);
219 my $snapshot = $conf->{snapshots
}->{$snapname};
220 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
222 $snapshot->{digest
} = $conf->{digest
};
228 sub load_current_config
{
229 my ($class, $vmid, $current) = @_;
231 my $conf = $class->load_config($vmid);
233 # take pending changes in
235 foreach my $opt (keys %{$conf->{pending
}}) {
236 next if $opt eq 'delete';
237 my $value = $conf->{pending
}->{$opt};
238 next if ref($value); # just to be sure
239 $conf->{$opt} = $value;
241 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
242 foreach my $opt (keys %$pending_delete_hash) {
243 delete $conf->{$opt} if $conf->{$opt};
247 delete $conf->{snapshots
};
248 delete $conf->{pending
};
254 # Lock config file using flock, run $code with @param, unlock config file.
255 # $timeout is the maximum time to acquire the flock
256 sub lock_config_full
{
257 my ($class, $vmid, $timeout, $code, @param) = @_;
259 my $filename = $class->config_file_lock($vmid);
261 my $res = lock_file
($filename, $timeout, $code, @param);
268 sub create_and_lock_config
{
269 my ($class, $vmid, $allow_existing, $lock) = @_;
271 $class->lock_config_full($vmid, 5, sub {
272 PVE
::Cluster
::check_vmid_unused
($vmid, $allow_existing);
274 my $conf = eval { $class->load_config($vmid) } || {};
275 $class->check_lock($conf);
276 $conf->{lock} = $lock // 'create';
277 $class->write_config($vmid, $conf);
281 # destroys configuration, only applicable for configs owned by the callers node.
282 # dies if removal fails, e.g., when inquorate.
284 my ($class, $vmid) = @_;
286 my $config_fn = $class->config_file($vmid, $nodename);
287 unlink $config_fn or die "failed to remove config file: $!\n";
290 # Lock config file using flock, run $code with @param, unlock config file.
291 # $timeout is the maximum time to acquire the flock
292 # $shared eq 1 creates a non-exclusive ("read") flock
293 sub lock_config_mode
{
294 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
296 my $filename = $class->config_file_lock($vmid);
298 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
305 # Lock config file using flock, run $code with @param, unlock config file.
307 my ($class, $vmid, $code, @param) = @_;
309 return $class->lock_config_full($vmid, 10, $code, @param);
312 # Checks whether the config is locked with the lock parameter
314 my ($class, $conf) = @_;
316 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
319 # Returns whether the config is locked with the lock parameter, also checks
320 # whether the lock value is correct if the optional $lock is set.
322 my ($class, $conf, $lock) = @_;
324 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
327 # Sets the lock parameter for this VM/CT's config to $lock.
329 my ($class, $vmid, $lock) = @_;
332 $class->lock_config($vmid, sub {
333 $conf = $class->load_config($vmid);
334 $class->check_lock($conf);
335 $conf->{lock} = $lock;
336 $class->write_config($vmid, $conf);
341 # Removes the lock parameter for this VM/CT's config, also checks whether
342 # the lock value is correct if the optional $lock is set.
344 my ($class, $vmid, $lock) = @_;
346 $class->lock_config($vmid, sub {
347 my $conf = $class->load_config($vmid);
348 if (!$conf->{lock}) {
349 my $lockstring = defined($lock) ?
"'$lock' " : "any";
350 die "no lock found trying to remove $lockstring lock\n";
351 } elsif (defined($lock) && $conf->{lock} ne $lock) {
352 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
354 delete $conf->{lock};
355 $class->write_config($vmid, $conf);
359 # Checks whether protection mode is enabled for this VM/CT.
360 sub check_protection
{
361 my ($class, $conf, $err_msg) = @_;
363 if ($conf->{protection
}) {
364 die "$err_msg - protection mode enabled\n";
368 # Adds an unused volume to $config, if possible.
369 sub add_unused_volume
{
370 my ($class, $config, $volid) = @_;
373 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
374 my $test = "unused$ind";
375 if (my $vid = $config->{$test}) {
376 return if $vid eq $volid; # do not add duplicates
382 die "Too many unused volumes - please delete them first.\n" if !$key;
384 $config->{$key} = $volid;
389 # Returns whether the template parameter is set in $conf.
391 my ($class, $conf) = @_;
393 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
396 # Checks whether $feature is available for the referenced volumes in $conf.
397 # Note: depending on the parameters, some volumes may be skipped!
399 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
400 die "implement me - abstract method\n";
403 # get all replicatable volume (hash $res->{$volid} = 1)
404 # $cleanup: for cleanup - simply ignores volumes without replicate feature
405 # $norerr: never raise exceptions - return undef instead
406 sub get_replicatable_volumes
{
407 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
409 die "implement me - abstract method\n";
414 # NOTE: Snapshot create/delete involves several non-atomic
415 # actions, and can take a long time.
416 # So we try to avoid locking the file and use the 'lock' variable
417 # inside the config file instead.
419 # Save the vmstate (RAM).
420 sub __snapshot_save_vmstate
{
421 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
422 die "implement me - abstract method\n";
425 # Check whether the VM/CT is running.
426 sub __snapshot_check_running
{
427 my ($class, $vmid) = @_;
428 die "implement me - abstract method\n";
431 # Check whether we need to freeze the VM/CT
432 sub __snapshot_check_freeze_needed
{
433 my ($sself, $vmid, $config, $save_vmstate) = @_;
434 die "implement me - abstract method\n";
437 # Freeze or unfreeze the VM/CT.
438 sub __snapshot_freeze
{
439 my ($class, $vmid, $unfreeze) = @_;
441 die "abstract method - implement me\n";
444 # Code run before and after creating all the volume snapshots
446 sub __snapshot_create_vol_snapshots_hook
{
447 my ($class, $vmid, $snap, $running, $hook) = @_;
452 # Create the volume snapshots for the VM/CT.
453 sub __snapshot_create_vol_snapshot
{
454 my ($class, $vmid, $vs, $volume, $snapname) = @_;
456 die "abstract method - implement me\n";
459 # Remove a drive from the snapshot config.
460 sub __snapshot_delete_remove_drive
{
461 my ($class, $snap, $drive) = @_;
463 die "abstract method - implement me\n";
466 # Delete the vmstate file/drive
467 sub __snapshot_delete_vmstate_file
{
468 my ($class, $snap, $force) = @_;
470 die "abstract method - implement me\n";
473 # Delete a volume snapshot
474 sub __snapshot_delete_vol_snapshot
{
475 my ($class, $vmid, $vs, $volume, $snapname) = @_;
477 die "abstract method - implement me\n";
480 # called during rollback prepare, and between config rollback and starting guest
481 # can change config, e.g. for vmgenid
482 # $data is shared across calls and passed to vm_start
483 sub __snapshot_rollback_hook
{
484 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
489 # Checks whether a volume snapshot is possible for this volume.
490 sub __snapshot_rollback_vol_possible
{
491 my ($class, $volume, $snapname) = @_;
493 die "abstract method - implement me\n";
496 # Rolls back this volume.
497 sub __snapshot_rollback_vol_rollback
{
498 my ($class, $volume, $snapname) = @_;
500 die "abstract method - implement me\n";
503 # Stops the VM/CT for a rollback.
504 sub __snapshot_rollback_vm_stop
{
505 my ($class, $vmid) = @_;
507 die "abstract method - implement me\n";
510 # Start the VM/CT after a rollback with restored vmstate.
511 sub __snapshot_rollback_vm_start
{
512 my ($class, $vmid, $vmstate, $data);
514 die "abstract method - implement me\n";
517 # Get list of volume IDs which are referenced in $conf, but not in $snap.
518 sub __snapshot_rollback_get_unused
{
519 my ($class, $conf, $snap) = @_;
521 die "abstract method - implement me\n";
524 # Iterate over all configured volumes, calling $func for each key/value pair.
525 sub __snapshot_foreach_volume
{
526 my ($class, $conf, $func) = @_;
528 die "abstract method - implement me\n";
531 # Copy the current config $source to the snapshot config $dest
532 sub __snapshot_copy_config
{
533 my ($class, $source, $dest) = @_;
535 foreach my $k (keys %$source) {
536 next if $k eq 'snapshots';
537 next if $k eq 'snapstate';
538 next if $k eq 'snaptime';
539 next if $k eq 'vmstate';
540 next if $k eq 'lock';
541 next if $k eq 'digest';
542 next if $k eq 'description';
543 next if $k =~ m/^unused\d+$/;
545 $dest->{$k} = $source->{$k};
549 # Apply the snapshot config $snap to the config $conf (rollback)
550 sub __snapshot_apply_config
{
551 my ($class, $conf, $snap) = @_;
555 snapshots
=> $conf->{snapshots
},
558 # keep description and list of unused disks
559 foreach my $k (keys %$conf) {
560 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
562 $newconf->{$k} = $conf->{$k};
565 $class->__snapshot_copy_config($snap, $newconf);
570 # Prepares the configuration for snapshotting.
571 sub __snapshot_prepare
{
572 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
578 my $conf = $class->load_config($vmid);
580 die "you can't take a snapshot if it's a template\n"
581 if $class->is_template($conf);
583 $class->check_lock($conf);
585 $conf->{lock} = 'snapshot';
587 die "snapshot name '$snapname' already used\n"
588 if defined($conf->{snapshots
}->{$snapname});
590 my $storecfg = PVE
::Storage
::config
();
591 die "snapshot feature is not available\n"
592 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
594 $snap = $conf->{snapshots
}->{$snapname} = {};
596 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
597 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
600 $class->__snapshot_copy_config($conf, $snap);
602 $snap->{snapstate
} = "prepare";
603 $snap->{snaptime
} = time();
604 $snap->{description
} = $comment if $comment;
606 $class->write_config($vmid, $conf);
609 $class->lock_config($vmid, $updatefn);
614 # Commits the configuration after snapshotting.
615 sub __snapshot_commit
{
616 my ($class, $vmid, $snapname) = @_;
620 my $conf = $class->load_config($vmid);
622 die "missing snapshot lock\n"
623 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
625 my $snap = $conf->{snapshots
}->{$snapname};
626 die "snapshot '$snapname' does not exist\n" if !defined($snap);
628 die "wrong snapshot state\n"
629 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
631 delete $snap->{snapstate
};
632 delete $conf->{lock};
634 $conf->{parent
} = $snapname;
636 $class->write_config($vmid, $conf);
639 $class->lock_config($vmid, $updatefn);
642 # Creates a snapshot for the VM/CT.
643 sub snapshot_create
{
644 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
646 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
648 $save_vmstate = 0 if !$snap->{vmstate
};
650 my $conf = $class->load_config($vmid);
652 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
658 $class->__snapshot_freeze($vmid, 0);
661 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
663 $class->__snapshot_foreach_volume($snap, sub {
664 my ($vs, $volume) = @_;
666 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
667 $drivehash->{$vs} = 1;
673 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
675 $class->__snapshot_freeze($vmid, 1);
677 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
681 warn "snapshot create failed: starting cleanup\n";
682 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
687 $class->__snapshot_commit($vmid, $snapname);
690 # Deletes a snapshot.
691 # Note: $drivehash is only set when called from snapshot_create.
692 sub snapshot_delete
{
693 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
699 my $conf = $class->load_config($vmid);
700 my $snap = $conf->{snapshots
}->{$snapname};
702 die "snapshot '$snapname' does not exist\n" if !defined($snap);
704 $class->set_lock($vmid, 'snapshot-delete')
705 if (!$drivehash); # doesn't already have a 'snapshot' lock
707 my $unlink_parent = sub {
708 my ($confref, $new_parent) = @_;
710 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
712 $confref->{parent
} = $new_parent;
714 delete $confref->{parent
};
719 my $remove_drive = sub {
722 my $conf = $class->load_config($vmid);
723 $snap = $conf->{snapshots
}->{$snapname};
724 die "snapshot '$snapname' does not exist\n" if !defined($snap);
726 $class->__snapshot_delete_remove_drive($snap, $drive);
728 $class->write_config($vmid, $conf);
732 $class->lock_config($vmid, sub {
733 my $conf = $class->load_config($vmid);
735 die "you can't delete a snapshot if vm is a template\n"
736 if $class->is_template($conf);
738 $snap = $conf->{snapshots
}->{$snapname};
739 die "snapshot '$snapname' does not exist\n" if !defined($snap);
741 $snap->{snapstate
} = 'delete';
743 $class->write_config($vmid, $conf);
746 # now remove vmstate file
747 if ($snap->{vmstate
}) {
748 $class->__snapshot_delete_vmstate_file($snap, $force);
750 # save changes (remove vmstate from snapshot)
751 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
754 # now remove all volume snapshots
755 $class->__snapshot_foreach_volume($snap, sub {
756 my ($vs, $volume) = @_;
758 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
759 if (!$drivehash || $drivehash->{$vs}) {
760 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
767 # save changes (remove drive from snapshot)
768 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
772 $class->lock_config($vmid, sub {
773 my $conf = $class->load_config($vmid);
774 $snap = $conf->{snapshots
}->{$snapname};
775 die "snapshot '$snapname' does not exist\n" if !defined($snap);
778 &$unlink_parent($conf, $snap->{parent
});
779 foreach my $sn (keys %{$conf->{snapshots
}}) {
780 next if $sn eq $snapname;
781 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
785 delete $conf->{snapshots
}->{$snapname};
786 delete $conf->{lock};
787 foreach my $volid (@$unused) {
788 $class->add_unused_volume($conf, $volid);
791 $class->write_config($vmid, $conf);
795 # Rolls back to a given snapshot.
796 sub snapshot_rollback
{
797 my ($class, $vmid, $snapname) = @_;
801 my $storecfg = PVE
::Storage
::config
();
803 my $conf = $class->load_config($vmid);
805 my $get_snapshot_config = sub {
807 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
809 my $res = $conf->{snapshots
}->{$snapname};
811 die "snapshot '$snapname' does not exist\n" if !defined($res);
816 my $repl_conf = PVE
::ReplicationConfig-
>new();
817 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
818 # remove all replication snapshots
819 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
820 my $sorted_volids = [ sort keys %$volumes ];
822 # remove all local replication snapshots (jobid => undef)
823 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
824 PVE
::Replication
::prepare
($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
827 my $snap = &$get_snapshot_config();
829 $class->__snapshot_foreach_volume($snap, sub {
830 my ($vs, $volume) = @_;
832 $class->__snapshot_rollback_vol_possible($volume, $snapname);
839 $conf = $class->load_config($vmid);
841 $snap = &$get_snapshot_config();
843 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
844 if $snap->{snapstate
};
847 $class->check_lock($conf);
848 $class->__snapshot_rollback_vm_stop($vmid);
851 die "unable to rollback vm $vmid: vm is running\n"
852 if $class->__snapshot_check_running($vmid);
855 $conf->{lock} = 'rollback';
857 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
858 delete $conf->{lock};
860 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
862 foreach my $volid (@$unused) {
863 $class->add_unused_volume($conf, $volid);
866 # copy snapshot config to current config
867 $conf = $class->__snapshot_apply_config($conf, $snap);
868 $conf->{parent
} = $snapname;
871 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
873 $class->write_config($vmid, $conf);
875 if (!$prepare && $snap->{vmstate
}) {
876 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $data);
880 $class->lock_config($vmid, $updatefn);
882 $class->__snapshot_foreach_volume($snap, sub {
883 my ($vs, $volume) = @_;
885 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
889 $class->lock_config($vmid, $updatefn);
892 # bash completion helper
895 my ($class, $vmid) = @_;
897 my $snapshot = eval {
898 my $conf = $class->load_config($vmid);
899 my $snapshots = $conf->{snapshots
} || [];
900 [ sort keys %$snapshots ]