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 # Lock config file using flock, run $code with @param, unlock config file.
72 # $timeout is the maximum time to aquire the flock
73 sub lock_config_full
{
74 my ($class, $vmid, $timeout, $code, @param) = @_;
76 my $filename = $class->config_file_lock($vmid);
78 my $res = lock_file
($filename, $timeout, $code, @param);
85 sub create_and_lock_config
{
86 my ($class, $vmid, $allow_existing) = @_;
88 $class->lock_config_full($vmid, 5, sub {
89 PVE
::Cluster
::check_vmid_unused
($vmid, $allow_existing);
91 my $conf = eval { $class->load_config($vmid) } || {};
92 $class->check_lock($conf);
93 $conf->{lock} = 'create';
94 $class->write_config($vmid, $conf);
98 # Lock config file using flock, run $code with @param, unlock config file.
99 # $timeout is the maximum time to aquire the flock
100 # $shared eq 1 creates a non-exclusive ("read") flock
101 sub lock_config_mode
{
102 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
104 my $filename = $class->config_file_lock($vmid);
106 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
113 # Lock config file using flock, run $code with @param, unlock config file.
115 my ($class, $vmid, $code, @param) = @_;
117 return $class->lock_config_full($vmid, 10, $code, @param);
120 # Checks whether the config is locked with the lock parameter
122 my ($class, $conf) = @_;
124 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
127 # Returns whether the config is locked with the lock parameter, also checks
128 # whether the lock value is correct if the optional $lock is set.
130 my ($class, $conf, $lock) = @_;
132 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
135 # Sets the lock parameter for this VM/CT's config to $lock.
137 my ($class, $vmid, $lock) = @_;
140 $class->lock_config($vmid, sub {
141 $conf = $class->load_config($vmid);
142 $class->check_lock($conf);
143 $conf->{lock} = $lock;
144 $class->write_config($vmid, $conf);
149 # Removes the lock parameter for this VM/CT's config, also checks whether
150 # the lock value is correct if the optional $lock is set.
152 my ($class, $vmid, $lock) = @_;
154 $class->lock_config($vmid, sub {
155 my $conf = $class->load_config($vmid);
156 if (!$conf->{lock}) {
157 my $lockstring = defined($lock) ?
"'$lock' " : "any";
158 die "no lock found trying to remove $lockstring lock\n";
159 } elsif (defined($lock) && $conf->{lock} ne $lock) {
160 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
162 delete $conf->{lock};
163 $class->write_config($vmid, $conf);
167 # Checks whether protection mode is enabled for this VM/CT.
168 sub check_protection
{
169 my ($class, $conf, $err_msg) = @_;
171 if ($conf->{protection
}) {
172 die "$err_msg - protection mode enabled\n";
176 # Adds an unused volume to $config, if possible.
177 sub add_unused_volume
{
178 my ($class, $config, $volid) = @_;
181 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
182 my $test = "unused$ind";
183 if (my $vid = $config->{$test}) {
184 return if $vid eq $volid; # do not add duplicates
190 die "Too many unused volumes - please delete them first.\n" if !$key;
192 $config->{$key} = $volid;
197 # Returns whether the template parameter is set in $conf.
199 my ($class, $conf) = @_;
201 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
204 # Checks whether $feature is availabe for the referenced volumes in $conf.
205 # Note: depending on the parameters, some volumes may be skipped!
207 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
208 die "implement me - abstract method\n";
211 # get all replicatable volume (hash $res->{$volid} = 1)
212 # $cleanup: for cleanup - simply ignores volumes without replicate feature
213 # $norerr: never raise exceptions - return undef instead
214 sub get_replicatable_volumes
{
215 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
217 die "implement me - abstract method\n";
222 # NOTE: Snapshot create/delete involves several non-atomic
223 # actions, and can take a long time.
224 # So we try to avoid locking the file and use the 'lock' variable
225 # inside the config file instead.
227 # Save the vmstate (RAM).
228 sub __snapshot_save_vmstate
{
229 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
230 die "implement me - abstract method\n";
233 # Check whether the VM/CT is running.
234 sub __snapshot_check_running
{
235 my ($class, $vmid) = @_;
236 die "implement me - abstract method\n";
239 # Check whether we need to freeze the VM/CT
240 sub __snapshot_check_freeze_needed
{
241 my ($sself, $vmid, $config, $save_vmstate) = @_;
242 die "implement me - abstract method\n";
245 # Freeze or unfreeze the VM/CT.
246 sub __snapshot_freeze
{
247 my ($class, $vmid, $unfreeze) = @_;
249 die "abstract method - implement me\n";
252 # Code run before and after creating all the volume snapshots
254 sub __snapshot_create_vol_snapshots_hook
{
255 my ($class, $vmid, $snap, $running, $hook) = @_;
260 # Create the volume snapshots for the VM/CT.
261 sub __snapshot_create_vol_snapshot
{
262 my ($class, $vmid, $vs, $volume, $snapname) = @_;
264 die "abstract method - implement me\n";
267 # Remove a drive from the snapshot config.
268 sub __snapshot_delete_remove_drive
{
269 my ($class, $snap, $drive) = @_;
271 die "abstract method - implement me\n";
274 # Delete the vmstate file/drive
275 sub __snapshot_delete_vmstate_file
{
276 my ($class, $snap, $force) = @_;
278 die "abstract method - implement me\n";
281 # Delete a volume snapshot
282 sub __snapshot_delete_vol_snapshot
{
283 my ($class, $vmid, $vs, $volume, $snapname) = @_;
285 die "abstract method - implement me\n";
288 # Checks whether a volume snapshot is possible for this volume.
289 sub __snapshot_rollback_vol_possible
{
290 my ($class, $volume, $snapname) = @_;
292 die "abstract method - implement me\n";
295 # Rolls back this volume.
296 sub __snapshot_rollback_vol_rollback
{
297 my ($class, $volume, $snapname) = @_;
299 die "abstract method - implement me\n";
302 # Stops the VM/CT for a rollback.
303 sub __snapshot_rollback_vm_stop
{
304 my ($class, $vmid) = @_;
306 die "abstract method - implement me\n";
309 # Start the VM/CT after a rollback with restored vmstate.
310 sub __snapshot_rollback_vm_start
{
311 my ($class, $vmid, $vmstate, $forcemachine);
313 die "abstract method - implement me\n";
316 # Get list of volume IDs which are referenced in $conf, but not in $snap.
317 sub __snapshot_rollback_get_unused
{
318 my ($class, $conf, $snap) = @_;
320 die "abstract method - implement me\n";
323 # Iterate over all configured volumes, calling $func for each key/value pair.
324 sub __snapshot_foreach_volume
{
325 my ($class, $conf, $func) = @_;
327 die "abstract method - implement me\n";
330 # Copy the current config $source to the snapshot config $dest
331 sub __snapshot_copy_config
{
332 my ($class, $source, $dest) = @_;
334 foreach my $k (keys %$source) {
335 next if $k eq 'snapshots';
336 next if $k eq 'snapstate';
337 next if $k eq 'snaptime';
338 next if $k eq 'vmstate';
339 next if $k eq 'lock';
340 next if $k eq 'digest';
341 next if $k eq 'description';
342 next if $k =~ m/^unused\d+$/;
344 $dest->{$k} = $source->{$k};
348 # Apply the snapshot config $snap to the config $conf (rollback)
349 sub __snapshot_apply_config
{
350 my ($class, $conf, $snap) = @_;
354 snapshots
=> $conf->{snapshots
},
357 # keep description and list of unused disks
358 foreach my $k (keys %$conf) {
359 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
361 $newconf->{$k} = $conf->{$k};
364 $class->__snapshot_copy_config($snap, $newconf);
369 # Prepares the configuration for snapshotting.
370 sub __snapshot_prepare
{
371 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
377 my $conf = $class->load_config($vmid);
379 die "you can't take a snapshot if it's a template\n"
380 if $class->is_template($conf);
382 $class->check_lock($conf);
384 $conf->{lock} = 'snapshot';
386 die "snapshot name '$snapname' already used\n"
387 if defined($conf->{snapshots
}->{$snapname});
389 my $storecfg = PVE
::Storage
::config
();
390 die "snapshot feature is not available\n"
391 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
393 $snap = $conf->{snapshots
}->{$snapname} = {};
395 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
396 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
399 $class->__snapshot_copy_config($conf, $snap);
401 $snap->{snapstate
} = "prepare";
402 $snap->{snaptime
} = time();
403 $snap->{description
} = $comment if $comment;
405 $class->write_config($vmid, $conf);
408 $class->lock_config($vmid, $updatefn);
413 # Commits the configuration after snapshotting.
414 sub __snapshot_commit
{
415 my ($class, $vmid, $snapname) = @_;
419 my $conf = $class->load_config($vmid);
421 die "missing snapshot lock\n"
422 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
424 my $snap = $conf->{snapshots
}->{$snapname};
425 die "snapshot '$snapname' does not exist\n" if !defined($snap);
427 die "wrong snapshot state\n"
428 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
430 delete $snap->{snapstate
};
431 delete $conf->{lock};
433 $conf->{parent
} = $snapname;
435 $class->write_config($vmid, $conf);
438 $class->lock_config($vmid, $updatefn);
441 # Creates a snapshot for the VM/CT.
442 sub snapshot_create
{
443 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
445 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
447 $save_vmstate = 0 if !$snap->{vmstate
};
449 my $conf = $class->load_config($vmid);
451 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
457 $class->__snapshot_freeze($vmid, 0);
460 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
462 $class->__snapshot_foreach_volume($snap, sub {
463 my ($vs, $volume) = @_;
465 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
466 $drivehash->{$vs} = 1;
472 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
474 $class->__snapshot_freeze($vmid, 1);
476 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
480 warn "snapshot create failed: starting cleanup\n";
481 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
486 $class->__snapshot_commit($vmid, $snapname);
489 # Deletes a snapshot.
490 # Note: $drivehash is only set when called from snapshot_create.
491 sub snapshot_delete
{
492 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
498 my $conf = $class->load_config($vmid);
499 my $snap = $conf->{snapshots
}->{$snapname};
501 die "snapshot '$snapname' does not exist\n" if !defined($snap);
503 $class->set_lock($vmid, 'snapshot-delete')
504 if (!$drivehash); # doesn't already have a 'snapshot' lock
506 my $unlink_parent = sub {
507 my ($confref, $new_parent) = @_;
509 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
511 $confref->{parent
} = $new_parent;
513 delete $confref->{parent
};
518 my $remove_drive = sub {
521 my $conf = $class->load_config($vmid);
522 $snap = $conf->{snapshots
}->{$snapname};
523 die "snapshot '$snapname' does not exist\n" if !defined($snap);
525 $class->__snapshot_delete_remove_drive($snap, $drive);
527 $class->write_config($vmid, $conf);
531 $class->lock_config($vmid, sub {
532 my $conf = $class->load_config($vmid);
534 die "you can't delete a snapshot if vm is a template\n"
535 if $class->is_template($conf);
537 $snap = $conf->{snapshots
}->{$snapname};
538 die "snapshot '$snapname' does not exist\n" if !defined($snap);
540 $snap->{snapstate
} = 'delete';
542 $class->write_config($vmid, $conf);
545 # now remove vmstate file
546 if ($snap->{vmstate
}) {
547 $class->__snapshot_delete_vmstate_file($snap, $force);
549 # save changes (remove vmstate from snapshot)
550 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
553 # now remove all volume snapshots
554 $class->__snapshot_foreach_volume($snap, sub {
555 my ($vs, $volume) = @_;
557 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
558 if (!$drivehash || $drivehash->{$vs}) {
559 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
566 # save changes (remove drive from snapshot)
567 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
571 $class->lock_config($vmid, sub {
572 my $conf = $class->load_config($vmid);
573 $snap = $conf->{snapshots
}->{$snapname};
574 die "snapshot '$snapname' does not exist\n" if !defined($snap);
577 &$unlink_parent($conf, $snap->{parent
});
578 foreach my $sn (keys %{$conf->{snapshots
}}) {
579 next if $sn eq $snapname;
580 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
584 delete $conf->{snapshots
}->{$snapname};
585 delete $conf->{lock};
586 foreach my $volid (@$unused) {
587 $class->add_unused_volume($conf, $volid);
590 $class->write_config($vmid, $conf);
594 # Rolls back to a given snapshot.
595 sub snapshot_rollback
{
596 my ($class, $vmid, $snapname) = @_;
600 my $storecfg = PVE
::Storage
::config
();
602 my $conf = $class->load_config($vmid);
604 my $get_snapshot_config = sub {
606 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
608 my $res = $conf->{snapshots
}->{$snapname};
610 die "snapshot '$snapname' does not exist\n" if !defined($res);
615 my $repl_conf = PVE
::ReplicationConfig-
>new();
616 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
617 # remove all replication snapshots
618 my $volumes = $class->get_replicatable_volumes($storecfg, $vmid, $conf, 1);
619 my $sorted_volids = [ sort keys %$volumes ];
621 # remove all local replication snapshots (jobid => undef)
622 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
623 PVE
::Replication
::prepare
($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
626 my $snap = &$get_snapshot_config();
628 $class->__snapshot_foreach_volume($snap, sub {
629 my ($vs, $volume) = @_;
631 $class->__snapshot_rollback_vol_possible($volume, $snapname);
636 $conf = $class->load_config($vmid);
638 $snap = &$get_snapshot_config();
640 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
641 if $snap->{snapstate
};
644 $class->check_lock($conf);
645 $class->__snapshot_rollback_vm_stop($vmid);
648 die "unable to rollback vm $vmid: vm is running\n"
649 if $class->__snapshot_check_running($vmid);
652 $conf->{lock} = 'rollback';
654 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
655 delete $conf->{lock};
658 # machine only relevant for Qemu
662 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
664 foreach my $volid (@$unused) {
665 $class->add_unused_volume($conf, $volid);
668 my $has_machine_config = defined($conf->{machine
});
670 # copy snapshot config to current config
671 $conf = $class->__snapshot_apply_config($conf, $snap);
672 $conf->{parent
} = $snapname;
674 # Note: old code did not store 'machine', so we try to be smart
675 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
676 $forcemachine = $conf->{machine
} || 'pc-i440fx-1.4';
677 # we remove the 'machine' configuration if not explicitly specified
678 # in the original config.
679 delete $conf->{machine
} if $snap->{vmstate
} && !$has_machine_config;
682 $class->write_config($vmid, $conf);
684 if (!$prepare && $snap->{vmstate
}) {
685 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $forcemachine);
689 $class->lock_config($vmid, $updatefn);
691 $class->__snapshot_foreach_volume($snap, sub {
692 my ($vs, $volume) = @_;
694 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
698 $class->lock_config($vmid, $updatefn);