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) = @_;
95 join ",", map { ( $delete_hash->{$_}->{force
} ?
'!' : '' ) . $_ } keys %$delete_hash;
98 sub add_to_pending_delete
{
99 my ($class, $conf, $key, $force) = @_;
101 delete $conf->{pending
}->{$key};
102 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
103 $pending_delete_hash->{$key}->{force
} = $force;
104 $conf->{pending
}->{delete} = $class->print_pending_delete($pending_delete_hash);
107 sub remove_from_pending_delete
{
108 my ($class, $conf, $key) = @_;
110 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
111 delete $pending_delete_hash->{$key};
113 if (%$pending_delete_hash) {
114 $conf->{pending
}->{delete} = $class->print_pending_delete($pending_delete_hash);
116 delete $conf->{pending
}->{delete};
120 sub cleanup_pending
{
121 my ($class, $conf) = @_;
123 # remove pending changes when nothing changed
125 foreach my $opt (keys %{$conf->{pending
}}) {
126 next if $opt eq 'delete'; # just to be sure
127 if (defined($conf->{$opt}) && ($conf->{pending
}->{$opt} eq $conf->{$opt})) {
129 delete $conf->{pending
}->{$opt};
133 my $current_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
134 my $pending_delete_hash = {};
135 while (my ($opt, $force) = each %$current_delete_hash) {
136 if (defined($conf->{$opt})) {
137 $pending_delete_hash->{$opt} = $force;
143 if (%$pending_delete_hash) {
144 $conf->{pending
}->{delete} = $class->print_pending_delete($pending_delete_hash);
146 delete $conf->{pending
}->{delete};
152 sub load_snapshot_config
{
153 my ($class, $vmid, $snapname) = @_;
155 my $conf = $class->load_config($vmid);
157 my $snapshot = $conf->{snapshots
}->{$snapname};
158 die "snapshot '$snapname' does not exist\n" if !defined($snapshot);
160 $snapshot->{digest
} = $conf->{digest
};
166 sub load_current_config
{
167 my ($class, $vmid, $current) = @_;
169 my $conf = $class->load_config($vmid);
171 # take pending changes in
173 foreach my $opt (keys %{$conf->{pending
}}) {
174 next if $opt eq 'delete';
175 my $value = $conf->{pending
}->{$opt};
176 next if ref($value); # just to be sure
177 $conf->{$opt} = $value;
179 my $pending_delete_hash = $class->parse_pending_delete($conf->{pending
}->{delete});
180 foreach my $opt (keys %$pending_delete_hash) {
181 delete $conf->{$opt} if $conf->{$opt};
185 delete $conf->{snapshots
};
186 delete $conf->{pending
};
192 # Lock config file using flock, run $code with @param, unlock config file.
193 # $timeout is the maximum time to aquire the flock
194 sub lock_config_full
{
195 my ($class, $vmid, $timeout, $code, @param) = @_;
197 my $filename = $class->config_file_lock($vmid);
199 my $res = lock_file
($filename, $timeout, $code, @param);
206 sub create_and_lock_config
{
207 my ($class, $vmid, $allow_existing) = @_;
209 $class->lock_config_full($vmid, 5, sub {
210 PVE
::Cluster
::check_vmid_unused
($vmid, $allow_existing);
212 my $conf = eval { $class->load_config($vmid) } || {};
213 $class->check_lock($conf);
214 $conf->{lock} = 'create';
215 $class->write_config($vmid, $conf);
219 # destroys configuration, only applyable for configs owned by the callers node.
220 # dies if removal fails, e.g., when inquorate.
222 my ($class, $vmid) = @_;
224 my $config_fn = $class->config_file($vmid, $nodename);
225 unlink $config_fn or die "failed to remove config file: $!\n";
228 # Lock config file using flock, run $code with @param, unlock config file.
229 # $timeout is the maximum time to aquire the flock
230 # $shared eq 1 creates a non-exclusive ("read") flock
231 sub lock_config_mode
{
232 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
234 my $filename = $class->config_file_lock($vmid);
236 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
243 # Lock config file using flock, run $code with @param, unlock config file.
245 my ($class, $vmid, $code, @param) = @_;
247 return $class->lock_config_full($vmid, 10, $code, @param);
250 # Checks whether the config is locked with the lock parameter
252 my ($class, $conf) = @_;
254 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
257 # Returns whether the config is locked with the lock parameter, also checks
258 # whether the lock value is correct if the optional $lock is set.
260 my ($class, $conf, $lock) = @_;
262 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
265 # Sets the lock parameter for this VM/CT's config to $lock.
267 my ($class, $vmid, $lock) = @_;
270 $class->lock_config($vmid, sub {
271 $conf = $class->load_config($vmid);
272 $class->check_lock($conf);
273 $conf->{lock} = $lock;
274 $class->write_config($vmid, $conf);
279 # Removes the lock parameter for this VM/CT's config, also checks whether
280 # the lock value is correct if the optional $lock is set.
282 my ($class, $vmid, $lock) = @_;
284 $class->lock_config($vmid, sub {
285 my $conf = $class->load_config($vmid);
286 if (!$conf->{lock}) {
287 my $lockstring = defined($lock) ?
"'$lock' " : "any";
288 die "no lock found trying to remove $lockstring lock\n";
289 } elsif (defined($lock) && $conf->{lock} ne $lock) {
290 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
292 delete $conf->{lock};
293 $class->write_config($vmid, $conf);
297 # Checks whether protection mode is enabled for this VM/CT.
298 sub check_protection
{
299 my ($class, $conf, $err_msg) = @_;
301 if ($conf->{protection
}) {
302 die "$err_msg - protection mode enabled\n";
306 # Adds an unused volume to $config, if possible.
307 sub add_unused_volume
{
308 my ($class, $config, $volid) = @_;
311 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
312 my $test = "unused$ind";
313 if (my $vid = $config->{$test}) {
314 return if $vid eq $volid; # do not add duplicates
320 die "Too many unused volumes - please delete them first.\n" if !$key;
322 $config->{$key} = $volid;
327 # Returns whether the template parameter is set in $conf.
329 my ($class, $conf) = @_;
331 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
334 # Checks whether $feature is availabe for the referenced volumes in $conf.
335 # Note: depending on the parameters, some volumes may be skipped!
337 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
338 die "implement me - abstract method\n";
341 # get all replicatable volume (hash $res->{$volid} = 1)
342 # $cleanup: for cleanup - simply ignores volumes without replicate feature
343 # $norerr: never raise exceptions - return undef instead
344 sub get_replicatable_volumes
{
345 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
347 die "implement me - abstract method\n";
352 # NOTE: Snapshot create/delete involves several non-atomic
353 # actions, and can take a long time.
354 # So we try to avoid locking the file and use the 'lock' variable
355 # inside the config file instead.
357 # Save the vmstate (RAM).
358 sub __snapshot_save_vmstate
{
359 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
360 die "implement me - abstract method\n";
363 # Check whether the VM/CT is running.
364 sub __snapshot_check_running
{
365 my ($class, $vmid) = @_;
366 die "implement me - abstract method\n";
369 # Check whether we need to freeze the VM/CT
370 sub __snapshot_check_freeze_needed
{
371 my ($sself, $vmid, $config, $save_vmstate) = @_;
372 die "implement me - abstract method\n";
375 # Freeze or unfreeze the VM/CT.
376 sub __snapshot_freeze
{
377 my ($class, $vmid, $unfreeze) = @_;
379 die "abstract method - implement me\n";
382 # Code run before and after creating all the volume snapshots
384 sub __snapshot_create_vol_snapshots_hook
{
385 my ($class, $vmid, $snap, $running, $hook) = @_;
390 # Create the volume snapshots for the VM/CT.
391 sub __snapshot_create_vol_snapshot
{
392 my ($class, $vmid, $vs, $volume, $snapname) = @_;
394 die "abstract method - implement me\n";
397 # Remove a drive from the snapshot config.
398 sub __snapshot_delete_remove_drive
{
399 my ($class, $snap, $drive) = @_;
401 die "abstract method - implement me\n";
404 # Delete the vmstate file/drive
405 sub __snapshot_delete_vmstate_file
{
406 my ($class, $snap, $force) = @_;
408 die "abstract method - implement me\n";
411 # Delete a volume snapshot
412 sub __snapshot_delete_vol_snapshot
{
413 my ($class, $vmid, $vs, $volume, $snapname) = @_;
415 die "abstract method - implement me\n";
418 # called during rollback prepare, and between config rollback and starting guest
419 # can change config, e.g. for vmgenid
420 # $data is shared across calls and passed to vm_start
421 sub __snapshot_rollback_hook
{
422 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
427 # Checks whether a volume snapshot is possible for this volume.
428 sub __snapshot_rollback_vol_possible
{
429 my ($class, $volume, $snapname) = @_;
431 die "abstract method - implement me\n";
434 # Rolls back this volume.
435 sub __snapshot_rollback_vol_rollback
{
436 my ($class, $volume, $snapname) = @_;
438 die "abstract method - implement me\n";
441 # Stops the VM/CT for a rollback.
442 sub __snapshot_rollback_vm_stop
{
443 my ($class, $vmid) = @_;
445 die "abstract method - implement me\n";
448 # Start the VM/CT after a rollback with restored vmstate.
449 sub __snapshot_rollback_vm_start
{
450 my ($class, $vmid, $vmstate, $data);
452 die "abstract method - implement me\n";
455 # Get list of volume IDs which are referenced in $conf, but not in $snap.
456 sub __snapshot_rollback_get_unused
{
457 my ($class, $conf, $snap) = @_;
459 die "abstract method - implement me\n";
462 # Iterate over all configured volumes, calling $func for each key/value pair.
463 sub __snapshot_foreach_volume
{
464 my ($class, $conf, $func) = @_;
466 die "abstract method - implement me\n";
469 # Copy the current config $source to the snapshot config $dest
470 sub __snapshot_copy_config
{
471 my ($class, $source, $dest) = @_;
473 foreach my $k (keys %$source) {
474 next if $k eq 'snapshots';
475 next if $k eq 'snapstate';
476 next if $k eq 'snaptime';
477 next if $k eq 'vmstate';
478 next if $k eq 'lock';
479 next if $k eq 'digest';
480 next if $k eq 'description';
481 next if $k =~ m/^unused\d+$/;
483 $dest->{$k} = $source->{$k};
487 # Apply the snapshot config $snap to the config $conf (rollback)
488 sub __snapshot_apply_config
{
489 my ($class, $conf, $snap) = @_;
493 snapshots
=> $conf->{snapshots
},
496 # keep description and list of unused disks
497 foreach my $k (keys %$conf) {
498 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
500 $newconf->{$k} = $conf->{$k};
503 $class->__snapshot_copy_config($snap, $newconf);
508 # Prepares the configuration for snapshotting.
509 sub __snapshot_prepare
{
510 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
516 my $conf = $class->load_config($vmid);
518 die "you can't take a snapshot if it's a template\n"
519 if $class->is_template($conf);
521 $class->check_lock($conf);
523 $conf->{lock} = 'snapshot';
525 die "snapshot name '$snapname' already used\n"
526 if defined($conf->{snapshots
}->{$snapname});
528 my $storecfg = PVE
::Storage
::config
();
529 die "snapshot feature is not available\n"
530 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
532 $snap = $conf->{snapshots
}->{$snapname} = {};
534 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
535 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
538 $class->__snapshot_copy_config($conf, $snap);
540 $snap->{snapstate
} = "prepare";
541 $snap->{snaptime
} = time();
542 $snap->{description
} = $comment if $comment;
544 $class->write_config($vmid, $conf);
547 $class->lock_config($vmid, $updatefn);
552 # Commits the configuration after snapshotting.
553 sub __snapshot_commit
{
554 my ($class, $vmid, $snapname) = @_;
558 my $conf = $class->load_config($vmid);
560 die "missing snapshot lock\n"
561 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
563 my $snap = $conf->{snapshots
}->{$snapname};
564 die "snapshot '$snapname' does not exist\n" if !defined($snap);
566 die "wrong snapshot state\n"
567 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
569 delete $snap->{snapstate
};
570 delete $conf->{lock};
572 $conf->{parent
} = $snapname;
574 $class->write_config($vmid, $conf);
577 $class->lock_config($vmid, $updatefn);
580 # Creates a snapshot for the VM/CT.
581 sub snapshot_create
{
582 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
584 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
586 $save_vmstate = 0 if !$snap->{vmstate
};
588 my $conf = $class->load_config($vmid);
590 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
596 $class->__snapshot_freeze($vmid, 0);
599 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
601 $class->__snapshot_foreach_volume($snap, sub {
602 my ($vs, $volume) = @_;
604 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
605 $drivehash->{$vs} = 1;
611 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
613 $class->__snapshot_freeze($vmid, 1);
615 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
619 warn "snapshot create failed: starting cleanup\n";
620 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
625 $class->__snapshot_commit($vmid, $snapname);
628 # Deletes a snapshot.
629 # Note: $drivehash is only set when called from snapshot_create.
630 sub snapshot_delete
{
631 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
637 my $conf = $class->load_config($vmid);
638 my $snap = $conf->{snapshots
}->{$snapname};
640 die "snapshot '$snapname' does not exist\n" if !defined($snap);
642 $class->set_lock($vmid, 'snapshot-delete')
643 if (!$drivehash); # doesn't already have a 'snapshot' lock
645 my $unlink_parent = sub {
646 my ($confref, $new_parent) = @_;
648 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
650 $confref->{parent
} = $new_parent;
652 delete $confref->{parent
};
657 my $remove_drive = sub {
660 my $conf = $class->load_config($vmid);
661 $snap = $conf->{snapshots
}->{$snapname};
662 die "snapshot '$snapname' does not exist\n" if !defined($snap);
664 $class->__snapshot_delete_remove_drive($snap, $drive);
666 $class->write_config($vmid, $conf);
670 $class->lock_config($vmid, sub {
671 my $conf = $class->load_config($vmid);
673 die "you can't delete a snapshot if vm is a template\n"
674 if $class->is_template($conf);
676 $snap = $conf->{snapshots
}->{$snapname};
677 die "snapshot '$snapname' does not exist\n" if !defined($snap);
679 $snap->{snapstate
} = 'delete';
681 $class->write_config($vmid, $conf);
684 # now remove vmstate file
685 if ($snap->{vmstate
}) {
686 $class->__snapshot_delete_vmstate_file($snap, $force);
688 # save changes (remove vmstate from snapshot)
689 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
692 # now remove all volume snapshots
693 $class->__snapshot_foreach_volume($snap, sub {
694 my ($vs, $volume) = @_;
696 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
697 if (!$drivehash || $drivehash->{$vs}) {
698 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
705 # save changes (remove drive from snapshot)
706 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
710 $class->lock_config($vmid, sub {
711 my $conf = $class->load_config($vmid);
712 $snap = $conf->{snapshots
}->{$snapname};
713 die "snapshot '$snapname' does not exist\n" if !defined($snap);
716 &$unlink_parent($conf, $snap->{parent
});
717 foreach my $sn (keys %{$conf->{snapshots
}}) {
718 next if $sn eq $snapname;
719 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
723 delete $conf->{snapshots
}->{$snapname};
724 delete $conf->{lock};
725 foreach my $volid (@$unused) {
726 $class->add_unused_volume($conf, $volid);
729 $class->write_config($vmid, $conf);
733 # Rolls back to a given snapshot.
734 sub snapshot_rollback
{
735 my ($class, $vmid, $snapname) = @_;
739 my $storecfg = PVE
::Storage
::config
();
741 my $conf = $class->load_config($vmid);
743 my $get_snapshot_config = sub {
745 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
747 my $res = $conf->{snapshots
}->{$snapname};
749 die "snapshot '$snapname' does not exist\n" if !defined($res);
754 my $repl_conf = PVE
::ReplicationConfig-
>new();
755 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
756 # remove all replication snapshots
757 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
758 my $sorted_volids = [ sort keys %$volumes ];
760 # remove all local replication snapshots (jobid => undef)
761 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
762 PVE
::Replication
::prepare
($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
765 my $snap = &$get_snapshot_config();
767 $class->__snapshot_foreach_volume($snap, sub {
768 my ($vs, $volume) = @_;
770 $class->__snapshot_rollback_vol_possible($volume, $snapname);
777 $conf = $class->load_config($vmid);
779 $snap = &$get_snapshot_config();
781 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
782 if $snap->{snapstate
};
785 $class->check_lock($conf);
786 $class->__snapshot_rollback_vm_stop($vmid);
789 die "unable to rollback vm $vmid: vm is running\n"
790 if $class->__snapshot_check_running($vmid);
793 $conf->{lock} = 'rollback';
795 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
796 delete $conf->{lock};
798 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
800 foreach my $volid (@$unused) {
801 $class->add_unused_volume($conf, $volid);
804 # copy snapshot config to current config
805 $conf = $class->__snapshot_apply_config($conf, $snap);
806 $conf->{parent
} = $snapname;
809 $class->__snapshot_rollback_hook($vmid, $conf, $snap, $prepare, $data);
811 $class->write_config($vmid, $conf);
813 if (!$prepare && $snap->{vmstate
}) {
814 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $data);
818 $class->lock_config($vmid, $updatefn);
820 $class->__snapshot_foreach_volume($snap, sub {
821 my ($vs, $volume) = @_;
823 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
827 $class->lock_config($vmid, $updatefn);
830 # bash completion helper
833 my ($class, $vmid) = @_;
835 my $snapshot = eval {
836 my $conf = $class->load_config($vmid);
837 my $snapshots = $conf->{snapshots
} || [];
838 [ sort keys %$snapshots ]