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 # Lock config file using flock, run $code with @param, unlock config file.
86 # $timeout is the maximum time to aquire the flock
87 # $shared eq 1 creates a non-exclusive ("read") flock
88 sub lock_config_mode
{
89 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
91 my $filename = $class->config_file_lock($vmid);
93 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
100 # Lock config file using flock, run $code with @param, unlock config file.
102 my ($class, $vmid, $code, @param) = @_;
104 return $class->lock_config_full($vmid, 10, $code, @param);
107 # Checks whether the config is locked with the lock parameter
109 my ($class, $conf) = @_;
111 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
114 # Returns whether the config is locked with the lock parameter, also checks
115 # whether the lock value is correct if the optional $lock is set.
117 my ($class, $conf, $lock) = @_;
119 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
122 # Sets the lock parameter for this VM/CT's config to $lock.
124 my ($class, $vmid, $lock) = @_;
127 $class->lock_config($vmid, sub {
128 $conf = $class->load_config($vmid);
129 $class->check_lock($conf);
130 $conf->{lock} = $lock;
131 $class->write_config($vmid, $conf);
136 # Removes the lock parameter for this VM/CT's config, also checks whether
137 # the lock value is correct if the optional $lock is set.
139 my ($class, $vmid, $lock) = @_;
141 $class->lock_config($vmid, sub {
142 my $conf = $class->load_config($vmid);
143 if (!$conf->{lock}) {
144 my $lockstring = defined($lock) ?
"'$lock' " : "any";
145 die "no lock found trying to remove $lockstring lock\n";
146 } elsif (defined($lock) && $conf->{lock} ne $lock) {
147 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
149 delete $conf->{lock};
150 $class->write_config($vmid, $conf);
154 # Checks whether protection mode is enabled for this VM/CT.
155 sub check_protection
{
156 my ($class, $conf, $err_msg) = @_;
158 if ($conf->{protection
}) {
159 die "$err_msg - protection mode enabled\n";
163 # Adds an unused volume to $config, if possible.
164 sub add_unused_volume
{
165 my ($class, $config, $volid) = @_;
168 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
169 my $test = "unused$ind";
170 if (my $vid = $config->{$test}) {
171 return if $vid eq $volid; # do not add duplicates
177 die "Too many unused volumes - please delete them first.\n" if !$key;
179 $config->{$key} = $volid;
184 # Returns whether the template parameter is set in $conf.
186 my ($class, $conf) = @_;
188 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
191 # Checks whether $feature is availabe for the referenced volumes in $conf.
192 # Note: depending on the parameters, some volumes may be skipped!
194 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
195 die "implement me - abstract method\n";
198 # get all replicatable volume (hash $res->{$volid} = 1)
199 # $cleanup: for cleanup - simply ignores volumes without replicate feature
200 # $norerr: never raise exceptions - return undef instead
201 sub get_replicatable_volumes
{
202 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
204 die "implement me - abstract method\n";
209 # NOTE: Snapshot create/delete involves several non-atomic
210 # actions, and can take a long time.
211 # So we try to avoid locking the file and use the 'lock' variable
212 # inside the config file instead.
214 # Save the vmstate (RAM).
215 sub __snapshot_save_vmstate
{
216 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
217 die "implement me - abstract method\n";
220 # Check whether the VM/CT is running.
221 sub __snapshot_check_running
{
222 my ($class, $vmid) = @_;
223 die "implement me - abstract method\n";
226 # Check whether we need to freeze the VM/CT
227 sub __snapshot_check_freeze_needed
{
228 my ($sself, $vmid, $config, $save_vmstate) = @_;
229 die "implement me - abstract method\n";
232 # Freeze or unfreeze the VM/CT.
233 sub __snapshot_freeze
{
234 my ($class, $vmid, $unfreeze) = @_;
236 die "abstract method - implement me\n";
239 # Code run before and after creating all the volume snapshots
241 sub __snapshot_create_vol_snapshots_hook
{
242 my ($class, $vmid, $snap, $running, $hook) = @_;
247 # Create the volume snapshots for the VM/CT.
248 sub __snapshot_create_vol_snapshot
{
249 my ($class, $vmid, $vs, $volume, $snapname) = @_;
251 die "abstract method - implement me\n";
254 # Remove a drive from the snapshot config.
255 sub __snapshot_delete_remove_drive
{
256 my ($class, $snap, $drive) = @_;
258 die "abstract method - implement me\n";
261 # Delete the vmstate file/drive
262 sub __snapshot_delete_vmstate_file
{
263 my ($class, $snap, $force) = @_;
265 die "abstract method - implement me\n";
268 # Delete a volume snapshot
269 sub __snapshot_delete_vol_snapshot
{
270 my ($class, $vmid, $vs, $volume, $snapname) = @_;
272 die "abstract method - implement me\n";
275 # Checks whether a volume snapshot is possible for this volume.
276 sub __snapshot_rollback_vol_possible
{
277 my ($class, $volume, $snapname) = @_;
279 die "abstract method - implement me\n";
282 # Rolls back this volume.
283 sub __snapshot_rollback_vol_rollback
{
284 my ($class, $volume, $snapname) = @_;
286 die "abstract method - implement me\n";
289 # Stops the VM/CT for a rollback.
290 sub __snapshot_rollback_vm_stop
{
291 my ($class, $vmid) = @_;
293 die "abstract method - implement me\n";
296 # Start the VM/CT after a rollback with restored vmstate.
297 sub __snapshot_rollback_vm_start
{
298 my ($class, $vmid, $vmstate, $forcemachine);
300 die "abstract method - implement me\n";
303 # Get list of volume IDs which are referenced in $conf, but not in $snap.
304 sub __snapshot_rollback_get_unused
{
305 my ($class, $conf, $snap) = @_;
307 die "abstract method - implement me\n";
310 # Iterate over all configured volumes, calling $func for each key/value pair.
311 sub __snapshot_foreach_volume
{
312 my ($class, $conf, $func) = @_;
314 die "abstract method - implement me\n";
317 # Copy the current config $source to the snapshot config $dest
318 sub __snapshot_copy_config
{
319 my ($class, $source, $dest) = @_;
321 foreach my $k (keys %$source) {
322 next if $k eq 'snapshots';
323 next if $k eq 'snapstate';
324 next if $k eq 'snaptime';
325 next if $k eq 'vmstate';
326 next if $k eq 'lock';
327 next if $k eq 'digest';
328 next if $k eq 'description';
329 next if $k =~ m/^unused\d+$/;
331 $dest->{$k} = $source->{$k};
335 # Apply the snapshot config $snap to the config $conf (rollback)
336 sub __snapshot_apply_config
{
337 my ($class, $conf, $snap) = @_;
341 snapshots
=> $conf->{snapshots
},
344 # keep description and list of unused disks
345 foreach my $k (keys %$conf) {
346 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
348 $newconf->{$k} = $conf->{$k};
351 $class->__snapshot_copy_config($snap, $newconf);
356 # Prepares the configuration for snapshotting.
357 sub __snapshot_prepare
{
358 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
364 my $conf = $class->load_config($vmid);
366 die "you can't take a snapshot if it's a template\n"
367 if $class->is_template($conf);
369 $class->check_lock($conf);
371 $conf->{lock} = 'snapshot';
373 die "snapshot name '$snapname' already used\n"
374 if defined($conf->{snapshots
}->{$snapname});
376 my $storecfg = PVE
::Storage
::config
();
377 die "snapshot feature is not available\n"
378 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
380 $snap = $conf->{snapshots
}->{$snapname} = {};
382 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
383 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
386 $class->__snapshot_copy_config($conf, $snap);
388 $snap->{snapstate
} = "prepare";
389 $snap->{snaptime
} = time();
390 $snap->{description
} = $comment if $comment;
392 $class->write_config($vmid, $conf);
395 $class->lock_config($vmid, $updatefn);
400 # Commits the configuration after snapshotting.
401 sub __snapshot_commit
{
402 my ($class, $vmid, $snapname) = @_;
406 my $conf = $class->load_config($vmid);
408 die "missing snapshot lock\n"
409 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
411 my $snap = $conf->{snapshots
}->{$snapname};
412 die "snapshot '$snapname' does not exist\n" if !defined($snap);
414 die "wrong snapshot state\n"
415 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
417 delete $snap->{snapstate
};
418 delete $conf->{lock};
420 $conf->{parent
} = $snapname;
422 $class->write_config($vmid, $conf);
425 $class->lock_config($vmid, $updatefn);
428 # Creates a snapshot for the VM/CT.
429 sub snapshot_create
{
430 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
432 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
434 $save_vmstate = 0 if !$snap->{vmstate
};
436 my $conf = $class->load_config($vmid);
438 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
444 $class->__snapshot_freeze($vmid, 0);
447 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
449 $class->__snapshot_foreach_volume($snap, sub {
450 my ($vs, $volume) = @_;
452 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
453 $drivehash->{$vs} = 1;
459 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
461 $class->__snapshot_freeze($vmid, 1);
463 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
467 warn "snapshot create failed: starting cleanup\n";
468 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
473 $class->__snapshot_commit($vmid, $snapname);
476 # Deletes a snapshot.
477 # Note: $drivehash is only set when called from snapshot_create.
478 sub snapshot_delete
{
479 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
486 $class->set_lock($vmid, 'snapshot-delete')
487 if (!$drivehash); # doesn't already have a 'snapshot' lock
489 my $unlink_parent = sub {
490 my ($confref, $new_parent) = @_;
492 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
494 $confref->{parent
} = $new_parent;
496 delete $confref->{parent
};
501 my $remove_drive = sub {
504 my $conf = $class->load_config($vmid);
505 $snap = $conf->{snapshots
}->{$snapname};
506 die "snapshot '$snapname' does not exist\n" if !defined($snap);
508 $class->__snapshot_delete_remove_drive($snap, $drive);
510 $class->write_config($vmid, $conf);
514 $class->lock_config($vmid, sub {
515 my $conf = $class->load_config($vmid);
517 die "you can't delete a snapshot if vm is a template\n"
518 if $class->is_template($conf);
520 $snap = $conf->{snapshots
}->{$snapname};
521 die "snapshot '$snapname' does not exist\n" if !defined($snap);
523 $snap->{snapstate
} = 'delete';
525 $class->write_config($vmid, $conf);
528 # now remove vmstate file
529 if ($snap->{vmstate
}) {
530 $class->__snapshot_delete_vmstate_file($snap, $force);
532 # save changes (remove vmstate from snapshot)
533 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
536 # now remove all volume snapshots
537 $class->__snapshot_foreach_volume($snap, sub {
538 my ($vs, $volume) = @_;
540 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
541 if (!$drivehash || $drivehash->{$vs}) {
542 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
549 # save changes (remove drive from snapshot)
550 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
554 $class->lock_config($vmid, sub {
555 my $conf = $class->load_config($vmid);
556 $snap = $conf->{snapshots
}->{$snapname};
557 die "snapshot '$snapname' does not exist\n" if !defined($snap);
560 &$unlink_parent($conf, $snap->{parent
});
561 foreach my $sn (keys %{$conf->{snapshots
}}) {
562 next if $sn eq $snapname;
563 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
567 delete $conf->{snapshots
}->{$snapname};
568 delete $conf->{lock};
569 foreach my $volid (@$unused) {
570 $class->add_unused_volume($conf, $volid);
573 $class->write_config($vmid, $conf);
577 # Rolls back to a given snapshot.
578 sub snapshot_rollback
{
579 my ($class, $vmid, $snapname) = @_;
583 my $storecfg = PVE
::Storage
::config
();
585 my $conf = $class->load_config($vmid);
587 my $get_snapshot_config = sub {
589 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
591 my $res = $conf->{snapshots
}->{$snapname};
593 die "snapshot '$snapname' does not exist\n" if !defined($res);
598 my $repl_conf = PVE
::ReplicationConfig-
>new();
599 if ($repl_conf->check_for_existing_jobs($vmid, 1)) {
600 # remove all replication snapshots
601 my $volumes = $class->get_replicatable_volumes($storecfg, $conf, 1);
602 my $sorted_volids = [ sort keys %$volumes ];
604 # remove all local replication snapshots (jobid => undef)
605 my $logfunc = sub { my $line = shift; chomp $line; print "$line\n"; };
606 PVE
::Replication
::prepare
($storecfg, $sorted_volids, undef, 0, undef, $logfunc);
609 my $snap = &$get_snapshot_config();
611 $class->__snapshot_foreach_volume($snap, sub {
612 my ($vs, $volume) = @_;
614 $class->__snapshot_rollback_vol_possible($volume, $snapname);
619 $conf = $class->load_config($vmid);
621 $snap = &$get_snapshot_config();
623 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
624 if $snap->{snapstate
};
627 $class->check_lock($conf);
628 $class->__snapshot_rollback_vm_stop($vmid);
631 die "unable to rollback vm $vmid: vm is running\n"
632 if $class->__snapshot_check_running($vmid);
635 $conf->{lock} = 'rollback';
637 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
638 delete $conf->{lock};
641 # machine only relevant for Qemu
645 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
647 foreach my $volid (@$unused) {
648 $class->add_unused_volume($conf, $volid);
651 my $has_machine_config = defined($conf->{machine
});
653 # copy snapshot config to current config
654 $conf = $class->__snapshot_apply_config($conf, $snap);
655 $conf->{parent
} = $snapname;
657 # Note: old code did not store 'machine', so we try to be smart
658 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
659 $forcemachine = $conf->{machine
} || 'pc-i440fx-1.4';
660 # we remove the 'machine' configuration if not explicitly specified
661 # in the original config.
662 delete $conf->{machine
} if $snap->{vmstate
} && !$has_machine_config;
665 $class->write_config($vmid, $conf);
667 if (!$prepare && $snap->{vmstate
}) {
668 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $forcemachine);
672 $class->lock_config($vmid, $updatefn);
674 $class->__snapshot_foreach_volume($snap, sub {
675 my ($vs, $volume) = @_;
677 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
681 $class->lock_config($vmid, $updatefn);