1 package PVE
::AbstractConfig
;
6 use PVE
::Tools
qw(lock_file lock_file_full);
11 my $nodename = PVE
::INotify
::nodename
();
13 # Printable string, currently either "VM" or "CT"
16 die "abstract method - implement me ";
19 sub __config_max_unused_disks
{
22 die "implement me - abstract method\n";
25 # Path to the flock file for this VM/CT
26 sub config_file_lock
{
27 my ($class, $vmid) = @_;
28 die "abstract method - implement me";
31 # Relative config file path for this VM/CT in CFS
33 my ($class, $vmid, $node) = @_;
34 die "abstract method - implement me";
37 # Absolute config file path for this VM/CT
39 my ($class, $vmid, $node) = @_;
41 my $cfspath = $class->cfs_config_path($vmid, $node);
42 return "/etc/pve/$cfspath";
45 # Read and parse config file for this VM/CT
47 my ($class, $vmid, $node) = @_;
49 $node = $nodename if !$node;
50 my $cfspath = $class->cfs_config_path($vmid, $node);
52 my $conf = PVE
::Cluster
::cfs_read_file
($cfspath);
53 die "Configuration file '$cfspath' does not exist\n"
59 # Generate and write config file for this VM/CT
61 my ($class, $vmid, $conf) = @_;
63 my $cfspath = $class->cfs_config_path($vmid);
65 PVE
::Cluster
::cfs_write_file
($cfspath, $conf);
68 # Lock config file using flock, run $code with @param, unlock config file.
69 # $timeout is the maximum time to aquire the flock
70 sub lock_config_full
{
71 my ($class, $vmid, $timeout, $code, @param) = @_;
73 my $filename = $class->config_file_lock($vmid);
75 my $res = lock_file
($filename, $timeout, $code, @param);
82 # Lock config file using flock, run $code with @param, unlock config file.
83 # $timeout is the maximum time to aquire the flock
84 # $shared eq 1 creates a non-exclusive ("read") flock
85 sub lock_config_mode
{
86 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
88 my $filename = $class->config_file_lock($vmid);
90 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
97 # Lock config file using flock, run $code with @param, unlock config file.
99 my ($class, $vmid, $code, @param) = @_;
101 return $class->lock_config_full($vmid, 10, $code, @param);
104 # Checks whether the config is locked with the lock parameter
106 my ($class, $conf) = @_;
108 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
111 # Returns whether the config is locked with the lock parameter, also checks
112 # whether the lock value is correct if the optional $lock is set.
114 my ($class, $conf, $lock) = @_;
116 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
119 # Sets the lock parameter for this VM/CT's config to $lock.
121 my ($class, $vmid, $lock) = @_;
124 $class->lock_config($vmid, sub {
125 $conf = $class->load_config($vmid);
126 $class->check_lock($conf);
127 $conf->{lock} = $lock;
128 $class->write_config($vmid, $conf);
133 # Removes the lock parameter for this VM/CT's config, also checks whether
134 # the lock value is correct if the optional $lock is set.
136 my ($class, $vmid, $lock) = @_;
138 $class->lock_config($vmid, sub {
139 my $conf = $class->load_config($vmid);
140 if (!$conf->{lock}) {
141 my $lockstring = defined($lock) ?
"'$lock' " : "any";
142 die "no lock found trying to remove $lockstring lock\n";
143 } elsif (defined($lock) && $conf->{lock} ne $lock) {
144 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
146 delete $conf->{lock};
147 $class->write_config($vmid, $conf);
151 # Checks whether protection mode is enabled for this VM/CT.
152 sub check_protection
{
153 my ($class, $conf, $err_msg) = @_;
155 if ($conf->{protection
}) {
156 die "$err_msg - protection mode enabled\n";
160 # Adds an unused volume to $config, if possible.
161 sub add_unused_volume
{
162 my ($class, $config, $volid) = @_;
165 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
166 my $test = "unused$ind";
167 if (my $vid = $config->{$test}) {
168 return if $vid eq $volid; # do not add duplicates
174 die "Too many unused volumes - please delete them first.\n" if !$key;
176 $config->{$key} = $volid;
181 # Returns whether the template parameter is set in $conf.
183 my ($class, $conf) = @_;
185 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
188 # Checks whether $feature is availabe for the referenced volumes in $conf.
189 # Note: depending on the parameters, some volumes may be skipped!
191 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
192 die "implement me - abstract method\n";
195 # get all replicatable volume (hash $res->{$volid} = 1)
196 # $cleanup: for cleanup - simply ignores volumes without replicate feature
197 # $norerr: never raise exceptions - return undef instead
198 sub get_replicatable_volumes
{
199 my ($class, $storecfg, $conf, $cleanup, $noerr) = @_;
201 die "implement me - abstract method\n";
206 # NOTE: Snapshot create/delete involves several non-atomic
207 # actions, and can take a long time.
208 # So we try to avoid locking the file and use the 'lock' variable
209 # inside the config file instead.
211 # Save the vmstate (RAM).
212 sub __snapshot_save_vmstate
{
213 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
214 die "implement me - abstract method\n";
217 # Check whether the VM/CT is running.
218 sub __snapshot_check_running
{
219 my ($class, $vmid) = @_;
220 die "implement me - abstract method\n";
223 # Check whether we need to freeze the VM/CT
224 sub __snapshot_check_freeze_needed
{
225 my ($sself, $vmid, $config, $save_vmstate) = @_;
226 die "implement me - abstract method\n";
229 # Freeze or unfreeze the VM/CT.
230 sub __snapshot_freeze
{
231 my ($class, $vmid, $unfreeze) = @_;
233 die "abstract method - implement me\n";
236 # Code run before and after creating all the volume snapshots
238 sub __snapshot_create_vol_snapshots_hook
{
239 my ($class, $vmid, $snap, $running, $hook) = @_;
244 # Create the volume snapshots for the VM/CT.
245 sub __snapshot_create_vol_snapshot
{
246 my ($class, $vmid, $vs, $volume, $snapname) = @_;
248 die "abstract method - implement me\n";
251 # Remove a drive from the snapshot config.
252 sub __snapshot_delete_remove_drive
{
253 my ($class, $snap, $drive) = @_;
255 die "abstract method - implement me\n";
258 # Delete the vmstate file/drive
259 sub __snapshot_delete_vmstate_file
{
260 my ($class, $snap, $force) = @_;
262 die "abstract method - implement me\n";
265 # Delete a volume snapshot
266 sub __snapshot_delete_vol_snapshot
{
267 my ($class, $vmid, $vs, $volume, $snapname) = @_;
269 die "abstract method - implement me\n";
272 # Checks whether a volume snapshot is possible for this volume.
273 sub __snapshot_rollback_vol_possible
{
274 my ($class, $volume, $snapname) = @_;
276 die "abstract method - implement me\n";
279 # Rolls back this volume.
280 sub __snapshot_rollback_vol_rollback
{
281 my ($class, $volume, $snapname) = @_;
283 die "abstract method - implement me\n";
286 # Stops the VM/CT for a rollback.
287 sub __snapshot_rollback_vm_stop
{
288 my ($class, $vmid) = @_;
290 die "abstract method - implement me\n";
293 # Start the VM/CT after a rollback with restored vmstate.
294 sub __snapshot_rollback_vm_start
{
295 my ($class, $vmid, $vmstate, $forcemachine);
297 die "abstract method - implement me\n";
300 # Get list of volume IDs which are referenced in $conf, but not in $snap.
301 sub __snapshot_rollback_get_unused
{
302 my ($class, $conf, $snap) = @_;
304 die "abstract method - implement me\n";
307 # Iterate over all configured volumes, calling $func for each key/value pair.
308 sub __snapshot_foreach_volume
{
309 my ($class, $conf, $func) = @_;
311 die "abstract method - implement me\n";
314 # Copy the current config $source to the snapshot config $dest
315 sub __snapshot_copy_config
{
316 my ($class, $source, $dest) = @_;
318 foreach my $k (keys %$source) {
319 next if $k eq 'snapshots';
320 next if $k eq 'snapstate';
321 next if $k eq 'snaptime';
322 next if $k eq 'vmstate';
323 next if $k eq 'lock';
324 next if $k eq 'digest';
325 next if $k eq 'description';
326 next if $k =~ m/^unused\d+$/;
328 $dest->{$k} = $source->{$k};
332 # Apply the snapshot config $snap to the config $conf (rollback)
333 sub __snapshot_apply_config
{
334 my ($class, $conf, $snap) = @_;
338 snapshots
=> $conf->{snapshots
},
341 # keep description and list of unused disks
342 foreach my $k (keys %$conf) {
343 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
345 $newconf->{$k} = $conf->{$k};
348 $class->__snapshot_copy_config($snap, $newconf);
353 # Prepares the configuration for snapshotting.
354 sub __snapshot_prepare
{
355 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
361 my $conf = $class->load_config($vmid);
363 die "you can't take a snapshot if it's a template\n"
364 if $class->is_template($conf);
366 $class->check_lock($conf);
368 $conf->{lock} = 'snapshot';
370 die "snapshot name '$snapname' already used\n"
371 if defined($conf->{snapshots
}->{$snapname});
373 my $storecfg = PVE
::Storage
::config
();
374 die "snapshot feature is not available\n"
375 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
377 $snap = $conf->{snapshots
}->{$snapname} = {};
379 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
380 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
383 $class->__snapshot_copy_config($conf, $snap);
385 $snap->{snapstate
} = "prepare";
386 $snap->{snaptime
} = time();
387 $snap->{description
} = $comment if $comment;
389 $class->write_config($vmid, $conf);
392 $class->lock_config($vmid, $updatefn);
397 # Commits the configuration after snapshotting.
398 sub __snapshot_commit
{
399 my ($class, $vmid, $snapname) = @_;
403 my $conf = $class->load_config($vmid);
405 die "missing snapshot lock\n"
406 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
408 my $snap = $conf->{snapshots
}->{$snapname};
409 die "snapshot '$snapname' does not exist\n" if !defined($snap);
411 die "wrong snapshot state\n"
412 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
414 delete $snap->{snapstate
};
415 delete $conf->{lock};
417 $conf->{parent
} = $snapname;
419 $class->write_config($vmid, $conf);
422 $class->lock_config($vmid, $updatefn);
425 # Creates a snapshot for the VM/CT.
426 sub snapshot_create
{
427 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
429 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
431 $save_vmstate = 0 if !$snap->{vmstate
};
433 my $conf = $class->load_config($vmid);
435 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
441 $class->__snapshot_freeze($vmid, 0);
444 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
446 $class->__snapshot_foreach_volume($snap, sub {
447 my ($vs, $volume) = @_;
449 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
450 $drivehash->{$vs} = 1;
456 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
458 $class->__snapshot_freeze($vmid, 1);
460 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
464 warn "snapshot create failed: starting cleanup\n";
465 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
470 $class->__snapshot_commit($vmid, $snapname);
473 # Deletes a snapshot.
474 # Note: $drivehash is only set when called from snapshot_create.
475 sub snapshot_delete
{
476 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
483 $class->set_lock($vmid, 'snapshot-delete')
484 if (!$drivehash); # doesn't already have a 'snapshot' lock
486 my $unlink_parent = sub {
487 my ($confref, $new_parent) = @_;
489 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
491 $confref->{parent
} = $new_parent;
493 delete $confref->{parent
};
498 my $remove_drive = sub {
501 my $conf = $class->load_config($vmid);
502 $snap = $conf->{snapshots
}->{$snapname};
503 die "snapshot '$snapname' does not exist\n" if !defined($snap);
505 $class->__snapshot_delete_remove_drive($snap, $drive);
507 $class->write_config($vmid, $conf);
511 $class->lock_config($vmid, sub {
512 my $conf = $class->load_config($vmid);
514 die "you can't delete a snapshot if vm is a template\n"
515 if $class->is_template($conf);
517 $snap = $conf->{snapshots
}->{$snapname};
518 die "snapshot '$snapname' does not exist\n" if !defined($snap);
520 $snap->{snapstate
} = 'delete';
522 $class->write_config($vmid, $conf);
525 # now remove vmstate file
526 if ($snap->{vmstate
}) {
527 $class->__snapshot_delete_vmstate_file($snap, $force);
529 # save changes (remove vmstate from snapshot)
530 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
533 # now remove all volume snapshots
534 $class->__snapshot_foreach_volume($snap, sub {
535 my ($vs, $volume) = @_;
537 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
538 if (!$drivehash || $drivehash->{$vs}) {
539 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
546 # save changes (remove drive from snapshot)
547 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
551 $class->lock_config($vmid, sub {
552 my $conf = $class->load_config($vmid);
553 $snap = $conf->{snapshots
}->{$snapname};
554 die "snapshot '$snapname' does not exist\n" if !defined($snap);
557 &$unlink_parent($conf, $snap->{parent
});
558 foreach my $sn (keys %{$conf->{snapshots
}}) {
559 next if $sn eq $snapname;
560 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
564 delete $conf->{snapshots
}->{$snapname};
565 delete $conf->{lock};
566 foreach my $volid (@$unused) {
567 $class->add_unused_volume($conf, $volid);
570 $class->write_config($vmid, $conf);
574 # Rolls back to a given snapshot.
575 sub snapshot_rollback
{
576 my ($class, $vmid, $snapname) = @_;
580 my $storecfg = PVE
::Storage
::config
();
582 my $conf = $class->load_config($vmid);
584 my $get_snapshot_config = sub {
586 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
588 my $res = $conf->{snapshots
}->{$snapname};
590 die "snapshot '$snapname' does not exist\n" if !defined($res);
595 my $snap = &$get_snapshot_config();
597 $class->__snapshot_foreach_volume($snap, sub {
598 my ($vs, $volume) = @_;
600 $class->__snapshot_rollback_vol_possible($volume, $snapname);
605 $conf = $class->load_config($vmid);
607 $snap = &$get_snapshot_config();
609 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
610 if $snap->{snapstate
};
613 $class->check_lock($conf);
614 $class->__snapshot_rollback_vm_stop($vmid);
617 die "unable to rollback vm $vmid: vm is running\n"
618 if $class->__snapshot_check_running($vmid);
621 $conf->{lock} = 'rollback';
623 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
624 delete $conf->{lock};
627 # machine only relevant for Qemu
631 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
633 foreach my $volid (@$unused) {
634 $class->add_unused_volume($conf, $volid);
637 my $has_machine_config = defined($conf->{machine
});
639 # copy snapshot config to current config
640 $conf = $class->__snapshot_apply_config($conf, $snap);
641 $conf->{parent
} = $snapname;
643 # Note: old code did not store 'machine', so we try to be smart
644 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
645 $forcemachine = $conf->{machine
} || 'pc-i440fx-1.4';
646 # we remove the 'machine' configuration if not explicitly specified
647 # in the original config.
648 delete $conf->{machine
} if $snap->{vmstate
} && !$has_machine_config;
651 $class->write_config($vmid, $conf);
653 if (!$prepare && $snap->{vmstate
}) {
654 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $forcemachine);
658 $class->lock_config($vmid, $updatefn);
660 $class->__snapshot_foreach_volume($snap, sub {
661 my ($vs, $volume) = @_;
663 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
667 $class->lock_config($vmid, $updatefn);