1 package PVE
::AbstractConfig
;
6 use PVE
::Tools
qw(lock_file lock_file_full);
9 my $nodename = PVE
::INotify
::nodename
();
11 # Printable string, currently either "VM" or "CT"
14 die "abstract method - implement me ";
17 sub __config_max_unused_disks
{
20 die "implement me - abstract method\n";
23 # Path to the flock file for this VM/CT
24 sub config_file_lock
{
25 my ($class, $vmid) = @_;
26 die "abstract method - implement me";
29 # Relative config file path for this VM/CT in CFS
31 my ($class, $vmid, $node) = @_;
32 die "abstract method - implement me";
35 # Absolute config file path for this VM/CT
37 my ($class, $vmid, $node) = @_;
39 my $cfspath = $class->cfs_config_path($vmid, $node);
40 return "/etc/pve/$cfspath";
43 # Read and parse config file for this VM/CT
45 my ($class, $vmid, $node) = @_;
47 $node = $nodename if !$node;
48 my $cfspath = $class->cfs_config_path($vmid, $node);
50 my $conf = PVE
::Cluster
::cfs_read_file
($cfspath);
51 die "Configuration file '$cfspath' does not exist\n"
57 # Generate and write config file for this VM/CT
59 my ($class, $vmid, $conf) = @_;
61 my $cfspath = $class->cfs_config_path($vmid);
63 PVE
::Cluster
::cfs_write_file
($cfspath, $conf);
66 # Lock config file using flock, run $code with @param, unlock config file.
67 # $timeout is the maximum time to aquire the flock
68 sub lock_config_full
{
69 my ($class, $vmid, $timeout, $code, @param) = @_;
71 my $filename = $class->config_file_lock($vmid);
73 my $res = lock_file
($filename, $timeout, $code, @param);
80 # Lock config file using flock, run $code with @param, unlock config file.
81 # $timeout is the maximum time to aquire the flock
82 # $shared eq 1 creates a non-exclusive ("read") flock
83 sub lock_config_mode
{
84 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
86 my $filename = $class->config_file_lock($vmid);
88 my $res = lock_file_full
($filename, $timeout, $shared, $code, @param);
95 # Lock config file using flock, run $code with @param, unlock config file.
97 my ($class, $vmid, $code, @param) = @_;
99 return $class->lock_config_full($vmid, 10, $code, @param);
102 # Checks whether the config is locked with the lock parameter
104 my ($class, $conf) = @_;
106 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
109 # Returns whether the config is locked with the lock parameter, also checks
110 # whether the lock value is correct if the optional $lock is set.
112 my ($class, $conf, $lock) = @_;
114 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
117 # Sets the lock parameter for this VM/CT's config to $lock.
119 my ($class, $vmid, $lock) = @_;
122 $class->lock_config($vmid, sub {
123 $conf = $class->load_config($vmid);
124 $class->check_lock($conf);
125 $conf->{lock} = $lock;
126 $class->write_config($vmid, $conf);
131 # Removes the lock parameter for this VM/CT's config, also checks whether
132 # the lock value is correct if the optional $lock is set.
134 my ($class, $vmid, $lock) = @_;
136 $class->lock_config($vmid, sub {
137 my $conf = $class->load_config($vmid);
138 if (!$conf->{lock}) {
139 die "no lock found trying to remove lock '$lock'\n";
140 } elsif (defined($lock) && $conf->{lock} ne $lock) {
141 die "found lock '$conf->{lock}' trying to remove lock '$lock'\n";
143 delete $conf->{lock};
144 $class->write_config($vmid, $conf);
148 # Checks whether protection mode is enabled for this VM/CT.
149 sub check_protection
{
150 my ($class, $conf, $err_msg) = @_;
152 if ($conf->{protection
}) {
153 die "$err_msg - protection mode enabled\n";
157 # Adds an unused volume to $config, if possible.
158 sub add_unused_volume
{
159 my ($class, $config, $volid) = @_;
162 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
163 my $test = "unused$ind";
164 if (my $vid = $config->{$test}) {
165 return if $vid eq $volid; # do not add duplicates
171 die "Too many unused volumes - please delete them first.\n" if !$key;
173 $config->{$key} = $volid;
178 # Returns whether the template parameter is set in $conf.
180 my ($class, $conf) = @_;
182 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
185 # Checks whether $feature is availabe for the referenced volumes in $conf.
186 # Note: depending on the parameters, some volumes may be skipped!
188 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
189 die "implement me - abstract method\n";
194 # NOTE: Snapshot create/delete involves several non-atomic
195 # actions, and can take a long time.
196 # So we try to avoid locking the file and use the 'lock' variable
197 # inside the config file instead.
199 # Save the vmstate (RAM).
200 sub __snapshot_save_vmstate
{
201 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
202 die "implement me - abstract method\n";
205 # Check whether the VM/CT is running.
206 sub __snapshot_check_running
{
207 my ($class, $vmid) = @_;
208 die "implement me - abstract method\n";
211 # Check whether we need to freeze the VM/CT
212 sub __snapshot_check_freeze_needed
{
213 my ($sself, $vmid, $config, $save_vmstate) = @_;
214 die "implement me - abstract method\n";
217 # Freeze or unfreeze the VM/CT.
218 sub __snapshot_freeze
{
219 my ($class, $vmid, $unfreeze) = @_;
221 die "abstract method - implement me\n";
224 # Create the volume snapshots for the VM/CT.
225 sub __snapshot_create_vol_snapshot
{
226 my ($class, $vmid, $vs, $volume, $snapname) = @_;
228 die "abstract method - implement me\n";
231 # Remove a drive from the snapshot config.
232 sub __snapshot_delete_remove_drive
{
233 my ($class, $snap, $remove_drive) = @_;
235 die "abstract method - implement me\n";
238 # Delete the vmstate file/drive
239 sub __snapshot_delete_vmstate_file
{
240 my ($class, $snap, $force) = @_;
242 die "abstract method - implement me\n";
245 # Delete a volume snapshot
246 sub __snapshot_delete_vol_snapshot
{
247 my ($class, $vmid, $vs, $volume, $snapname) = @_;
249 die "abstract method - implement me\n";
252 # Checks whether a volume snapshot is possible for this volume.
253 sub __snapshot_rollback_vol_possible
{
254 my ($class, $volume, $snapname) = @_;
256 die "abstract method - implement me\n";
259 # Rolls back this volume.
260 sub __snapshot_rollback_vol_rollback
{
261 my ($class, $volume, $snapname) = @_;
263 die "abstract method - implement me\n";
266 # Stops the VM/CT for a rollback.
267 sub __snapshot_rollback_vm_stop
{
268 my ($class, $vmid) = @_;
270 die "abstract method - implement me\n";
273 # Start the VM/CT after a rollback with restored vmstate.
274 sub __snapshot_rollback_vm_start
{
275 my ($class, $vmid, $vmstate, $forcemachine);
277 die "abstract method - implement me\n";
280 # Iterate over all configured volumes, calling $func for each key/value pair.
281 sub __snapshot_foreach_volume
{
282 my ($class, $conf, $func) = @_;
284 die "abstract method - implement me\n";
287 # Copy the current config $source to the snapshot config $dest
288 sub __snapshot_copy_config
{
289 my ($class, $source, $dest) = @_;
291 foreach my $k (keys %$source) {
292 next if $k eq 'snapshots';
293 next if $k eq 'snapstate';
294 next if $k eq 'snaptime';
295 next if $k eq 'vmstate';
296 next if $k eq 'lock';
297 next if $k eq 'digest';
298 next if $k eq 'description';
299 next if $k =~ m/^unused\d+$/;
301 $dest->{$k} = $source->{$k};
305 # Apply the snapshot config $snap to the config $conf (rollback)
306 sub __snapshot_apply_config
{
307 my ($class, $conf, $snap) = @_;
311 snapshots
=> $conf->{snapshots
},
314 # keep description and list of unused disks
315 foreach my $k (keys %$conf) {
316 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
317 $newconf->{$k} = $conf->{$k};
320 $class->__snapshot_copy_config($snap, $newconf);
325 # Prepares the configuration for snapshotting.
326 sub __snapshot_prepare
{
327 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
333 my $conf = $class->load_config($vmid);
335 die "you can't take a snapshot if it's a template\n"
336 if $class->is_template($conf);
338 $class->check_lock($conf);
340 $conf->{lock} = 'snapshot';
342 die "snapshot name '$snapname' already used\n"
343 if defined($conf->{snapshots
}->{$snapname});
345 my $storecfg = PVE
::Storage
::config
();
346 die "snapshot feature is not available\n"
347 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
349 $snap = $conf->{snapshots
}->{$snapname} = {};
351 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
352 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
355 $class->__snapshot_copy_config($conf, $snap);
357 $snap->{snapstate
} = "prepare";
358 $snap->{snaptime
} = time();
359 $snap->{description
} = $comment if $comment;
361 $class->write_config($vmid, $conf);
364 $class->lock_config($vmid, $updatefn);
369 # Commits the configuration after snapshotting.
370 sub __snapshot_commit
{
371 my ($class, $vmid, $snapname) = @_;
375 my $conf = $class->load_config($vmid);
377 die "missing snapshot lock\n"
378 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
380 my $snap = $conf->{snapshots
}->{$snapname};
381 die "snapshot '$snapname' does not exist\n" if !defined($snap);
383 die "wrong snapshot state\n"
384 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
386 delete $snap->{snapstate
};
387 delete $conf->{lock};
389 $conf->{parent
} = $snapname;
391 $class->write_config($vmid, $conf);
394 $class->lock_config($vmid, $updatefn);
397 # Creates a snapshot for the VM/CT.
398 sub snapshot_create
{
399 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
401 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
403 $save_vmstate = 0 if !$snap->{vmstate
};
405 my $conf = $class->load_config($vmid);
407 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
413 $class->__snapshot_freeze($vmid, 0);
416 $class->__snapshot_foreach_volume($snap, sub {
417 my ($vs, $volume) = @_;
419 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
420 $drivehash->{$vs} = 1;
427 $class->__snapshot_freeze($vmid, 1);
432 warn "snapshot create failed: starting cleanup\n";
433 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
438 $class->__snapshot_commit($vmid, $snapname);
441 # Deletes a snapshot.
442 # Note: $drivehash is only set when called from snapshot_create.
443 sub snapshot_delete
{
444 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
451 my $unlink_parent = sub {
452 my ($confref, $new_parent) = @_;
454 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
456 $confref->{parent
} = $new_parent;
458 delete $confref->{parent
};
464 my ($remove_drive) = @_;
466 my $conf = $class->load_config($vmid);
469 $class->check_lock($conf);
470 die "you can't delete a snapshot if vm is a template\n"
471 if $class->is_template($conf);
474 $snap = $conf->{snapshots
}->{$snapname};
476 die "snapshot '$snapname' does not exist\n" if !defined($snap);
480 &$unlink_parent($conf, $snap->{parent
});
481 foreach my $sn (keys %{$conf->{snapshots
}}) {
482 next if $sn eq $snapname;
483 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
488 $class->__snapshot_delete_remove_drive($snap, $remove_drive);
492 $snap->{snapstate
} = 'delete';
494 delete $conf->{snapshots
}->{$snapname};
495 delete $conf->{lock} if $drivehash;
496 foreach my $volid (@$unused) {
497 $class->add_unused_volume($conf, $volid);
501 $class->write_config($vmid, $conf);
504 $class->lock_config($vmid, $updatefn);
506 # now remove vmstate file
507 if ($snap->{vmstate
}) {
508 $class->__snapshot_delete_vmstate_file($snap, $force);
510 # save changes (remove vmstate from snapshot)
511 $class->lock_config($vmid, $updatefn, 'vmstate') if !$force;
514 # now remove all volume snapshots
515 $class->__snapshot_foreach_volume($snap, sub {
516 my ($vs, $volume) = @_;
518 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
519 if (!$drivehash || $drivehash->{$vs}) {
520 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname); };
527 # save changes (remove mp from snapshot)
528 $class->lock_config($vmid, $updatefn, $vs) if !$force;
529 push @$unused, $volume->{volume
};
534 $class->lock_config($vmid, $updatefn);
537 # Rolls back to a given snapshot.
538 sub snapshot_rollback
{
539 my ($class, $vmid, $snapname) = @_;
543 my $storecfg = PVE
::Storage
::config
();
545 my $conf = $class->load_config($vmid);
547 my $get_snapshot_config = sub {
549 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
551 my $res = $conf->{snapshots
}->{$snapname};
553 die "snapshot '$snapname' does not exist\n" if !defined($res);
558 my $snap = &$get_snapshot_config();
560 $class->__snapshot_foreach_volume($snap, sub {
561 my ($vs, $volume) = @_;
563 $class->__snapshot_rollback_vol_possible($volume, $snapname);
568 $conf = $class->load_config($vmid);
570 $snap = &$get_snapshot_config();
572 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
573 if $snap->{snapstate
};
576 $class->check_lock($conf);
577 $class->__snapshot_rollback_vm_stop($vmid);
580 die "unable to rollback vm $vmid: vm is running\n"
581 if $class->__snapshot_check_running($vmid);
584 $conf->{lock} = 'rollback';
586 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
587 delete $conf->{lock};
590 # machine only relevant for Qemu
594 my $has_machine_config = defined($conf->{machine
});
596 # copy snapshot config to current config
597 $conf = $class->__snapshot_apply_config($conf, $snap);
598 $conf->{parent
} = $snapname;
600 # Note: old code did not store 'machine', so we try to be smart
601 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
602 $forcemachine = $conf->{machine
} || 'pc-i440fx-1.4';
603 # we remove the 'machine' configuration if not explicitly specified
604 # in the original config.
605 delete $conf->{machine
} if $snap->{vmstate
} && !$has_machine_config;
608 $class->write_config($vmid, $conf);
610 if (!$prepare && $snap->{vmstate
}) {
611 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $forcemachine);
615 $class->lock_config($vmid, $updatefn);
617 $class->__snapshot_foreach_volume($snap, sub {
618 my ($vs, $volume) = @_;
620 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
624 $class->lock_config($vmid, $updatefn);