1 package PVE
::AbstractConfig
;
6 use PVE
::Tools
qw(lock_file lock_file_full);
11 use PVE
::GuestHelpers
qw(typesafe_ne);
12 use PVE
::ReplicationConfig
;
15 my $nodename = PVE
::INotify
::nodename
();
17 # Printable string, currently either "VM" or "CT"
20 die "abstract method - implement me ";
23 sub __config_max_unused_disks
{
26 die "implement me - abstract method\n";
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";
35 # Relative config file path for this VM/CT in CFS
37 my ($class, $vmid, $node) = @_;
38 die "abstract method - implement me";
41 # Absolute config file path for this VM/CT
43 my ($class, $vmid, $node) = @_;
45 my $cfspath = $class->cfs_config_path($vmid, $node);
46 return "/etc/pve/$cfspath";
49 # Read and parse config file for this VM/CT
51 my ($class, $vmid, $node) = @_;
53 $node = $nodename if !$node;
54 my $cfspath = $class->cfs_config_path($vmid, $node);
56 my $conf = PVE
::Cluster
::cfs_read_file
($cfspath);
57 die "Configuration file '$cfspath' does not exist\n"
63 # Generate and write config file for this VM/CT
65 my ($class, $vmid, $conf) = @_;
67 my $cfspath = $class->cfs_config_path($vmid);
69 PVE
::Cluster
::cfs_write_file
($cfspath, $conf);
72 # Pending changes related
74 sub parse_pending_delete
{
75 my ($class, $data) = @_;
82 my $pending_deletions = {};
83 for my $entry (split(/\s+/, $data)) {
84 my ($force, $key) = $entry =~ /^(!?)(.*)$/;
86 $pending_deletions->{$key} = {
87 force
=> $force ?
1 : 0,
91 return $pending_deletions;
94 sub print_pending_delete
{
95 my ($class, $delete_hash) = @_;
97 my $render_key = sub {
99 $key = "!$key" if $delete_hash->{$key}->{force
};
103 join (',', map { $render_key->($_) } sort keys %$delete_hash);
106 sub add_to_pending_delete
{
107 my ($class, $conf, $key, $force) = @_;
109 $conf->{pending
} //= {};
110 my $pending = $conf->{pending
};
111 my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
113 $pending_delete_hash->{$key} = { force
=> $force };
115 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
120 sub remove_from_pending_delete
{
121 my ($class, $conf, $key) = @_;
123 my $pending = $conf->{pending
};
124 my $pending_delete_hash = $class->parse_pending_delete($pending->{delete});
126 return $conf if ! exists $pending_delete_hash->{$key};
128 delete $pending_delete_hash->{$key};
130 if (%$pending_delete_hash) {
131 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
133 delete $pending->{delete};
139 sub cleanup_pending
{
140 my ($class, $conf) = @_;
142 my $pending = $conf->{pending
};
143 # remove pending changes when nothing changed
145 foreach my $opt (keys %{$conf->{pending
}}) {
146 next if $opt eq 'delete'; # just to be sure
147 if (defined($conf->{$opt}) && ($pending->{$opt} eq $conf->{$opt})) {
149 delete $pending->{$opt};
153 my $current_delete_hash = $class->parse_pending_delete($pending->{delete});
154 my $pending_delete_hash = {};
155 for my $opt (keys %$current_delete_hash) {
156 if (defined($conf->{$opt})) {
157 $pending_delete_hash->{$opt} = $current_delete_hash->{$opt};
163 if (%$pending_delete_hash) {
164 $pending->{delete} = $class->print_pending_delete($pending_delete_hash);
166 delete $pending->{delete};
172 sub get_partial_fast_plug_option
{
175 die "abstract method - implement me ";
178 sub partial_fast_plug
{
179 my ($class, $conf, $opt) = @_;
181 my $partial_fast_plug_option = $class->get_partial_fast_plug_option();
182 return 0 if !$partial_fast_plug_option->{$opt};
184 my $format = $partial_fast_plug_option->{$opt}->{fmt
};
185 my $fast_pluggable = $partial_fast_plug_option->{$opt}->{properties
};
188 if (exists($conf->{$opt})) {
189 $configured = PVE
::JSONSchema
::parse_property_string
($format, $conf->{$opt});
191 my $pending = PVE
::JSONSchema
::parse_property_string
($format, $conf->{pending
}->{$opt});
195 # merge configured and pending opts to iterate
196 my @all_keys = keys %{{ %$pending, %$configured }};
198 foreach my $subopt (@all_keys) {
199 my $type = $format->{$subopt}->{type
};
200 if (typesafe_ne
($configured->{$subopt}, $pending->{$subopt}, $type)) {
201 if ($fast_pluggable->{$subopt}) {
202 $configured->{$subopt} = $pending->{$subopt};
208 # if there're no keys in $configured (after merge) there shouldn't be anything to change
209 if (keys %$configured) {
210 $conf->{$opt} = PVE
::JSONSchema
::print_property_string
($configured, $format);
217 sub load_snapshot_config
{
218 my ($class, $vmid, $snapname) = @_;
220 my $conf = $class->load_config($vmid);
222 my $snapshot = $conf->{snapshots
}->{$snapname};
223 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
225 $snapshot->{digest
} = $conf->{digest
};
231 sub load_current_config
{
232 my ($class, $vmid, $current) = @_;
234 my $conf = $class->load_config($vmid);
236 # take pending changes in
238 foreach my $opt (keys %{$conf->{pending
}}) {
239 next if $opt eq 'delete';
240 my $value = $conf->{pending
}->{$opt};
241 next if ref($value); # just to be sure
242 $conf->{$opt} = $value;
244 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
245 foreach my $opt (keys %$pending_delete_hash) {
246 delete $conf->{$opt} if $conf->{$opt};
250 delete $conf->{snapshots
};
251 delete $conf->{pending
};
257 # Lock config file using flock, run $code with @param, unlock config file.
258 # $timeout is the maximum time to acquire the flock
259 sub lock_config_full
{
260 my ($class, $vmid, $timeout, $code, @param) = @_;
262 my $filename = $class->config_file_lock($vmid);
264 my $res = lock_file
($filename, $timeout, $code, @param);
271 sub create_and_lock_config
{
272 my ($class, $vmid, $allow_existing, $lock) = @_;
274 $class->lock_config_full($vmid, 5, sub {
275 PVE
::Cluster
::check_vmid_unused
($vmid, $allow_existing);
277 my $conf = eval { $class->load_config($vmid) } || {};
278 $class->check_lock($conf);
279 $conf->{lock} = $lock // 'create';
280 $class->write_config($vmid, $conf);
284 # destroys configuration, only applicable for configs owned by the callers node.
285 # dies if removal fails, e.g., when inquorate.
287 my ($class, $vmid) = @_;
289 my $config_fn = $class->config_file($vmid, $nodename);
290 unlink $config_fn or die "failed to remove config file: $!\n";
293 # Lock config file using flock, run $code with @param, unlock config file.
294 # $timeout is the maximum time to acquire the flock
295 # $shared eq 1 creates a non-exclusive ("read") flock
296 sub lock_config_mode
{
297 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
299 my $filename = $class->config_file_lock($vmid);
301 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
308 # Lock config file using flock, run $code with @param, unlock config file.
310 my ($class, $vmid, $code, @param) = @_;
312 return $class->lock_config_full($vmid, 10, $code, @param);
315 # Checks whether the config is locked with the lock parameter
317 my ($class, $conf) = @_;
319 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
322 # Returns whether the config is locked with the lock parameter, also checks
323 # whether the lock value is correct if the optional $lock is set.
325 my ($class, $conf, $lock) = @_;
327 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
330 # Sets the lock parameter for this VM/CT's config to $lock.
332 my ($class, $vmid, $lock) = @_;
335 $class->lock_config($vmid, sub {
336 $conf = $class->load_config($vmid);
337 $class->check_lock($conf);
338 $conf->{lock} = $lock;
339 $class->write_config($vmid, $conf);
344 # Removes the lock parameter for this VM/CT's config, also checks whether
345 # the lock value is correct if the optional $lock is set.
347 my ($class, $vmid, $lock) = @_;
349 $class->lock_config($vmid, sub {
350 my $conf = $class->load_config($vmid);
351 if (!$conf->{lock}) {
352 my $lockstring = defined($lock) ?
"'$lock' " : "any";
353 die "no lock found trying to remove $lockstring lock\n";
354 } elsif (defined($lock) && $conf->{lock} ne $lock) {
355 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
357 delete $conf->{lock};
358 $class->write_config($vmid, $conf);
362 # Checks whether protection mode is enabled for this VM/CT.
363 sub check_protection
{
364 my ($class, $conf, $err_msg) = @_;
366 if ($conf->{protection
}) {
367 die "$err_msg - protection mode enabled\n";
371 # Adds an unused volume to $config, if possible.
372 sub add_unused_volume
{
373 my ($class, $config, $volid) = @_;
376 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
377 my $test = "unused$ind";
378 if (my $vid = $config->{$test}) {
379 return if $vid eq $volid; # do not add duplicates
385 die "Too many unused volumes - please delete them first.\n" if !$key;
387 $config->{$key} = $volid;
392 # Returns whether the template parameter is set in $conf.
394 my ($class, $conf) = @_;
396 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
399 # Checks whether $feature is available for the referenced volumes in $conf.
400 # Note: depending on the parameters, some volumes may be skipped!
402 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
403 die "implement me - abstract method\n";
406 # get all replicatable volume (hash $res->{$volid} = 1)
407 # $cleanup: for cleanup - simply ignores volumes without replicate feature
408 # $norerr: never raise exceptions - return undef instead
409 sub get_replicatable_volumes
{
410 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
412 die "implement me - abstract method\n";
415 # Returns all the guests volumes which would be included in a vzdump job
416 # Return Format (array-ref with hash-refs as elements):
419 # key, key in the config, e.g. mp0, scsi0,...
422 # data volume object as returned from foreach_drive/foreach_mountpoint
425 sub get_backup_volumes
{
426 my ($class, $conf) = @_;
428 die "implement me - abstract method\n";
433 # NOTE: Snapshot create/delete involves several non-atomic
434 # actions, and can take a long time.
435 # So we try to avoid locking the file and use the 'lock' variable
436 # inside the config file instead.
438 # Save the vmstate (RAM).
439 sub __snapshot_save_vmstate
{
440 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
441 die "implement me - abstract method\n";
444 # Check whether the VM/CT is running.
445 sub __snapshot_check_running
{
446 my ($class, $vmid) = @_;
447 die "implement me - abstract method\n";
450 # Check whether we need to freeze the VM/CT
451 sub __snapshot_check_freeze_needed
{
452 my ($sself, $vmid, $config, $save_vmstate) = @_;
453 die "implement me - abstract method\n";
456 # Freeze or unfreeze the VM/CT.
457 sub __snapshot_freeze
{
458 my ($class, $vmid, $unfreeze) = @_;
460 die "abstract method - implement me\n";
463 # Code run before and after creating all the volume snapshots
465 sub __snapshot_create_vol_snapshots_hook
{
466 my ($class, $vmid, $snap, $running, $hook) = @_;
471 # Create the volume snapshots for the VM/CT.
472 sub __snapshot_create_vol_snapshot
{
473 my ($class, $vmid, $vs, $volume, $snapname) = @_;
475 die "abstract method - implement me\n";
478 # Remove a drive from the snapshot config.
479 sub __snapshot_delete_remove_drive
{
480 my ($class, $snap, $drive) = @_;
482 die "abstract method - implement me\n";
485 # Delete the vmstate file/drive
486 sub __snapshot_delete_vmstate_file
{
487 my ($class, $snap, $force) = @_;
489 die "abstract method - implement me\n";
492 # Delete a volume snapshot
493 sub __snapshot_delete_vol_snapshot
{
494 my ($class, $vmid, $vs, $volume, $snapname) = @_;
496 die "abstract method - implement me\n";
499 # called during rollback prepare, and between config rollback and starting guest
500 # can change config, e.g. for vmgenid
501 # $data is shared across calls and passed to vm_start
502 sub __snapshot_rollback_hook
{
503 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
508 # Checks whether a volume snapshot is possible for this volume.
509 sub __snapshot_rollback_vol_possible
{
510 my ($class, $volume, $snapname) = @_;
512 die "abstract method - implement me\n";
515 # Rolls back this volume.
516 sub __snapshot_rollback_vol_rollback
{
517 my ($class, $volume, $snapname) = @_;
519 die "abstract method - implement me\n";
522 # Stops the VM/CT for a rollback.
523 sub __snapshot_rollback_vm_stop
{
524 my ($class, $vmid) = @_;
526 die "abstract method - implement me\n";
529 # Start the VM/CT after a rollback with restored vmstate.
530 sub __snapshot_rollback_vm_start
{
531 my ($class, $vmid, $vmstate, $data);
533 die "abstract method - implement me\n";
536 # Get list of volume IDs which are referenced in $conf, but not in $snap.
537 sub __snapshot_rollback_get_unused
{
538 my ($class, $conf, $snap) = @_;
540 die "abstract method - implement me\n";
543 # Iterate over all configured volumes, calling $func for each key/value pair.
544 sub __snapshot_foreach_volume
{
545 my ($class, $conf, $func) = @_;
547 die "abstract method - implement me\n";
550 # Copy the current config $source to the snapshot config $dest
551 sub __snapshot_copy_config
{
552 my ($class, $source, $dest) = @_;
554 foreach my $k (keys %$source) {
555 next if $k eq 'snapshots';
556 next if $k eq 'snapstate';
557 next if $k eq 'snaptime';
558 next if $k eq 'vmstate';
559 next if $k eq 'lock';
560 next if $k eq 'digest';
561 next if $k eq 'description';
562 next if $k =~ m/^unused\d+$/;
564 $dest->{$k} = $source->{$k};
568 # Apply the snapshot config $snap to the config $conf (rollback)
569 sub __snapshot_apply_config
{
570 my ($class, $conf, $snap) = @_;
574 snapshots
=> $conf->{snapshots
},
577 # keep description and list of unused disks
578 foreach my $k (keys %$conf) {
579 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
581 $newconf->{$k} = $conf->{$k};
584 $class->__snapshot_copy_config($snap, $newconf);
589 # Prepares the configuration for snapshotting.
590 sub __snapshot_prepare
{
591 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
597 my $conf = $class->load_config($vmid);
599 die "you can't take a snapshot if it's a template\n"
600 if $class->is_template($conf);
602 $class->check_lock($conf);
604 $conf->{lock} = 'snapshot';
606 die "snapshot name '$snapname' already used\n"
607 if defined($conf->{snapshots
}->{$snapname});
609 my $storecfg = PVE
::Storage
::config
();
610 die "snapshot feature is not available\n"
611 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
613 $snap = $conf->{snapshots
}->{$snapname} = {};
615 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
616 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
619 $class->__snapshot_copy_config($conf, $snap);
621 $snap->{snapstate
} = "prepare";
622 $snap->{snaptime
} = time();
623 $snap->{description
} = $comment if $comment;
625 $class->write_config($vmid, $conf);
628 $class->lock_config($vmid, $updatefn);
633 # Commits the configuration after snapshotting.
634 sub __snapshot_commit
{
635 my ($class, $vmid, $snapname) = @_;
639 my $conf = $class->load_config($vmid);
641 die "missing snapshot lock\n"
642 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
644 my $snap = $conf->{snapshots
}->{$snapname};
645 die "snapshot '$snapname' does not exist\n" if !defined($snap);
647 die "wrong snapshot state\n"
648 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
650 delete $snap->{snapstate
};
651 delete $conf->{lock};
653 $conf->{parent
} = $snapname;
655 $class->write_config($vmid, $conf);
658 $class->lock_config($vmid, $updatefn);
661 # Creates a snapshot for the VM/CT.
662 sub snapshot_create
{
663 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
665 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
667 $save_vmstate = 0 if !$snap->{vmstate
};
669 my $conf = $class->load_config($vmid);
671 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
677 $class->__snapshot_freeze($vmid, 0);
680 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
682 $class->__snapshot_foreach_volume($snap, sub {
683 my ($vs, $volume) = @_;
685 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
686 $drivehash->{$vs} = 1;
692 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
694 $class->__snapshot_freeze($vmid, 1);
696 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
700 warn "snapshot create failed: starting cleanup\n";
701 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
706 $class->__snapshot_commit($vmid, $snapname);
709 # Deletes a snapshot.
710 # Note: $drivehash is only set when called from snapshot_create.
711 sub snapshot_delete
{
712 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
718 my $conf = $class->load_config($vmid);
719 my $snap = $conf->{snapshots
}->{$snapname};
721 die "snapshot '$snapname' does not exist\n" if !defined($snap);
723 $class->set_lock($vmid, 'snapshot-delete')
724 if (!$drivehash); # doesn't already have a 'snapshot' lock
726 my $unlink_parent = sub {
727 my ($confref, $new_parent) = @_;
729 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
731 $confref->{parent
} = $new_parent;
733 delete $confref->{parent
};
738 my $remove_drive = sub {
741 my $conf = $class->load_config($vmid);
742 $snap = $conf->{snapshots
}->{$snapname};
743 die "snapshot '$snapname' does not exist\n" if !defined($snap);
745 $class->__snapshot_delete_remove_drive($snap, $drive);
747 $class->write_config($vmid, $conf);
751 $class->lock_config($vmid, sub {
752 my $conf = $class->load_config($vmid);
754 die "you can't delete a snapshot if vm is a template\n"
755 if $class->is_template($conf);
757 $snap = $conf->{snapshots
}->{$snapname};
758 die "snapshot '$snapname' does not exist\n" if !defined($snap);
760 $snap->{snapstate
} = 'delete';
762 $class->write_config($vmid, $conf);
765 # now remove vmstate file
766 if ($snap->{vmstate
}) {
767 $class->__snapshot_delete_vmstate_file($snap, $force);
769 # save changes (remove vmstate from snapshot)
770 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
773 # now remove all volume snapshots
774 $class->__snapshot_foreach_volume($snap, sub {
775 my ($vs, $volume) = @_;
777 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
778 if (!$drivehash || $drivehash->{$vs}) {
779 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
786 # save changes (remove drive from snapshot)
787 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
791 $class->lock_config($vmid, sub {
792 my $conf = $class->load_config($vmid);
793 $snap = $conf->{snapshots
}->{$snapname};
794 die "snapshot '$snapname' does not exist\n" if !defined($snap);
797 &$unlink_parent($conf, $snap->{parent
});
798 foreach my $sn (keys %{$conf->{snapshots
}}) {
799 next if $sn eq $snapname;
800 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
804 delete $conf->{snapshots
}->{$snapname};
805 delete $conf->{lock};
806 foreach my $volid (@$unused) {
807 $class->add_unused_volume($conf, $volid);
810 $class->write_config($vmid, $conf);
814 # Rolls back to a given snapshot.
815 sub snapshot_rollback
{
816 my ($class, $vmid, $snapname) = @_;
820 my $storecfg = PVE
::Storage
::config
();
822 my $conf = $class->load_config($vmid);
824 my $get_snapshot_config = sub {
826 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
828 my $res = $conf->{snapshots
}->{$snapname};
830 die "snapshot '$snapname' does not exist\n" if !defined($res);
835 my $repl_conf = PVE
::ReplicationConfig-
>new();
836 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
837 # remove all replication snapshots
838 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
839 my $sorted_volids = [ sort keys %$volumes ];
841 # remove all local replication snapshots (jobid => undef)
842 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
843 PVE
::Replication
::prepare
($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
846 my $snap = &$get_snapshot_config();
848 $class->__snapshot_foreach_volume($snap, sub {
849 my ($vs, $volume) = @_;
851 $class->__snapshot_rollback_vol_possible($volume, $snapname);
858 $conf = $class->load_config($vmid);
860 $snap = &$get_snapshot_config();
862 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
863 if $snap->{snapstate
};
866 $class->check_lock($conf);
867 $class->__snapshot_rollback_vm_stop($vmid);
870 die "unable to rollback vm $vmid: vm is running\n"
871 if $class->__snapshot_check_running($vmid);
874 $conf->{lock} = 'rollback';
876 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
877 delete $conf->{lock};
879 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
881 foreach my $volid (@$unused) {
882 $class->add_unused_volume($conf, $volid);
885 # copy snapshot config to current config
886 $conf = $class->__snapshot_apply_config($conf, $snap);
887 $conf->{parent
} = $snapname;
890 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
892 $class->write_config($vmid, $conf);
894 if (!$prepare && $snap->{vmstate
}) {
895 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $data);
899 $class->lock_config($vmid, $updatefn);
901 $class->__snapshot_foreach_volume($snap, sub {
902 my ($vs, $volume) = @_;
904 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
908 $class->lock_config($vmid, $updatefn);
911 # bash completion helper
914 my ($class, $vmid) = @_;
916 my $snapshot = eval {
917 my $conf = $class->load_config($vmid);
918 my $snapshots = $conf->{snapshots
} || [];
919 [ sort keys %$snapshots ]