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 my $lockstring = defined($lock) ?
"'$lock' " : "any";
140 die "no lock found trying to remove $lockstring lock\n";
141 } elsif (defined($lock) && $conf->{lock} ne $lock) {
142 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
144 delete $conf->{lock};
145 $class->write_config($vmid, $conf);
149 # Checks whether protection mode is enabled for this VM/CT.
150 sub check_protection
{
151 my ($class, $conf, $err_msg) = @_;
153 if ($conf->{protection
}) {
154 die "$err_msg - protection mode enabled\n";
158 # Adds an unused volume to $config, if possible.
159 sub add_unused_volume
{
160 my ($class, $config, $volid) = @_;
163 for (my $ind = $class->__config_max_unused_disks() - 1; $ind >= 0; $ind--) {
164 my $test = "unused$ind";
165 if (my $vid = $config->{$test}) {
166 return if $vid eq $volid; # do not add duplicates
172 die "Too many unused volumes - please delete them first.\n" if !$key;
174 $config->{$key} = $volid;
179 # Returns whether the template parameter is set in $conf.
181 my ($class, $conf) = @_;
183 return 1 if defined $conf->{template
} && $conf->{template
} == 1;
186 # Checks whether $feature is availabe for the referenced volumes in $conf.
187 # Note: depending on the parameters, some volumes may be skipped!
189 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
190 die "implement me - abstract method\n";
195 # NOTE: Snapshot create/delete involves several non-atomic
196 # actions, and can take a long time.
197 # So we try to avoid locking the file and use the 'lock' variable
198 # inside the config file instead.
200 # Save the vmstate (RAM).
201 sub __snapshot_save_vmstate
{
202 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
203 die "implement me - abstract method\n";
206 # Check whether the VM/CT is running.
207 sub __snapshot_check_running
{
208 my ($class, $vmid) = @_;
209 die "implement me - abstract method\n";
212 # Check whether we need to freeze the VM/CT
213 sub __snapshot_check_freeze_needed
{
214 my ($sself, $vmid, $config, $save_vmstate) = @_;
215 die "implement me - abstract method\n";
218 # Freeze or unfreeze the VM/CT.
219 sub __snapshot_freeze
{
220 my ($class, $vmid, $unfreeze) = @_;
222 die "abstract method - implement me\n";
225 # Code run before and after creating all the volume snapshots
227 sub __snapshot_create_vol_snapshots_hook
{
228 my ($class, $vmid, $snap, $running, $hook) = @_;
233 # Create the volume snapshots for the VM/CT.
234 sub __snapshot_create_vol_snapshot
{
235 my ($class, $vmid, $vs, $volume, $snapname) = @_;
237 die "abstract method - implement me\n";
240 # Remove a drive from the snapshot config.
241 sub __snapshot_delete_remove_drive
{
242 my ($class, $snap, $remove_drive) = @_;
244 die "abstract method - implement me\n";
247 # Delete the vmstate file/drive
248 sub __snapshot_delete_vmstate_file
{
249 my ($class, $snap, $force) = @_;
251 die "abstract method - implement me\n";
254 # Delete a volume snapshot
255 sub __snapshot_delete_vol_snapshot
{
256 my ($class, $vmid, $vs, $volume, $snapname) = @_;
258 die "abstract method - implement me\n";
261 # Checks whether a volume snapshot is possible for this volume.
262 sub __snapshot_rollback_vol_possible
{
263 my ($class, $volume, $snapname) = @_;
265 die "abstract method - implement me\n";
268 # Rolls back this volume.
269 sub __snapshot_rollback_vol_rollback
{
270 my ($class, $volume, $snapname) = @_;
272 die "abstract method - implement me\n";
275 # Stops the VM/CT for a rollback.
276 sub __snapshot_rollback_vm_stop
{
277 my ($class, $vmid) = @_;
279 die "abstract method - implement me\n";
282 # Start the VM/CT after a rollback with restored vmstate.
283 sub __snapshot_rollback_vm_start
{
284 my ($class, $vmid, $vmstate, $forcemachine);
286 die "abstract method - implement me\n";
289 # Iterate over all configured volumes, calling $func for each key/value pair.
290 sub __snapshot_foreach_volume
{
291 my ($class, $conf, $func) = @_;
293 die "abstract method - implement me\n";
296 # Copy the current config $source to the snapshot config $dest
297 sub __snapshot_copy_config
{
298 my ($class, $source, $dest) = @_;
300 foreach my $k (keys %$source) {
301 next if $k eq 'snapshots';
302 next if $k eq 'snapstate';
303 next if $k eq 'snaptime';
304 next if $k eq 'vmstate';
305 next if $k eq 'lock';
306 next if $k eq 'digest';
307 next if $k eq 'description';
308 next if $k =~ m/^unused\d+$/;
310 $dest->{$k} = $source->{$k};
314 # Apply the snapshot config $snap to the config $conf (rollback)
315 sub __snapshot_apply_config
{
316 my ($class, $conf, $snap) = @_;
320 snapshots
=> $conf->{snapshots
},
323 # keep description and list of unused disks
324 foreach my $k (keys %$conf) {
325 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
326 $newconf->{$k} = $conf->{$k};
329 $class->__snapshot_copy_config($snap, $newconf);
334 # Prepares the configuration for snapshotting.
335 sub __snapshot_prepare
{
336 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
342 my $conf = $class->load_config($vmid);
344 die "you can't take a snapshot if it's a template\n"
345 if $class->is_template($conf);
347 $class->check_lock($conf);
349 $conf->{lock} = 'snapshot';
351 die "snapshot name '$snapname' already used\n"
352 if defined($conf->{snapshots
}->{$snapname});
354 my $storecfg = PVE
::Storage
::config
();
355 die "snapshot feature is not available\n"
356 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
358 $snap = $conf->{snapshots
}->{$snapname} = {};
360 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
361 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
364 $class->__snapshot_copy_config($conf, $snap);
366 $snap->{snapstate
} = "prepare";
367 $snap->{snaptime
} = time();
368 $snap->{description
} = $comment if $comment;
370 $class->write_config($vmid, $conf);
373 $class->lock_config($vmid, $updatefn);
378 # Commits the configuration after snapshotting.
379 sub __snapshot_commit
{
380 my ($class, $vmid, $snapname) = @_;
384 my $conf = $class->load_config($vmid);
386 die "missing snapshot lock\n"
387 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
389 my $snap = $conf->{snapshots
}->{$snapname};
390 die "snapshot '$snapname' does not exist\n" if !defined($snap);
392 die "wrong snapshot state\n"
393 if !($snap->{snapstate
} && $snap->{snapstate
} eq "prepare");
395 delete $snap->{snapstate
};
396 delete $conf->{lock};
398 $conf->{parent
} = $snapname;
400 $class->write_config($vmid, $conf);
403 $class->lock_config($vmid, $updatefn);
406 # Creates a snapshot for the VM/CT.
407 sub snapshot_create
{
408 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
410 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
412 $save_vmstate = 0 if !$snap->{vmstate
};
414 my $conf = $class->load_config($vmid);
416 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate
});
422 $class->__snapshot_freeze($vmid, 0);
425 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
427 $class->__snapshot_foreach_volume($snap, sub {
428 my ($vs, $volume) = @_;
430 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
431 $drivehash->{$vs} = 1;
437 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
439 $class->__snapshot_freeze($vmid, 1);
441 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
445 warn "snapshot create failed: starting cleanup\n";
446 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
451 $class->__snapshot_commit($vmid, $snapname);
454 # Deletes a snapshot.
455 # Note: $drivehash is only set when called from snapshot_create.
456 sub snapshot_delete
{
457 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
464 my $unlink_parent = sub {
465 my ($confref, $new_parent) = @_;
467 if ($confref->{parent
} && $confref->{parent
} eq $snapname) {
469 $confref->{parent
} = $new_parent;
471 delete $confref->{parent
};
477 my ($remove_drive) = @_;
479 my $conf = $class->load_config($vmid);
482 $class->check_lock($conf);
483 die "you can't delete a snapshot if vm is a template\n"
484 if $class->is_template($conf);
487 $snap = $conf->{snapshots
}->{$snapname};
489 die "snapshot '$snapname' does not exist\n" if !defined($snap);
493 &$unlink_parent($conf, $snap->{parent
});
494 foreach my $sn (keys %{$conf->{snapshots
}}) {
495 next if $sn eq $snapname;
496 &$unlink_parent($conf->{snapshots
}->{$sn}, $snap->{parent
});
501 $class->__snapshot_delete_remove_drive($snap, $remove_drive);
505 $snap->{snapstate
} = 'delete';
507 delete $conf->{snapshots
}->{$snapname};
508 delete $conf->{lock} if $drivehash;
509 foreach my $volid (@$unused) {
510 $class->add_unused_volume($conf, $volid);
514 $class->write_config($vmid, $conf);
517 $class->lock_config($vmid, $updatefn);
519 # now remove vmstate file
520 if ($snap->{vmstate
}) {
521 $class->__snapshot_delete_vmstate_file($snap, $force);
523 # save changes (remove vmstate from snapshot)
524 $class->lock_config($vmid, $updatefn, 'vmstate') if !$force;
527 # now remove all volume snapshots
528 $class->__snapshot_foreach_volume($snap, sub {
529 my ($vs, $volume) = @_;
531 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup
};
532 if (!$drivehash || $drivehash->{$vs}) {
533 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
540 # save changes (remove mp from snapshot)
541 $class->lock_config($vmid, $updatefn, $vs) if !$force;
546 $class->lock_config($vmid, $updatefn);
549 # Rolls back to a given snapshot.
550 sub snapshot_rollback
{
551 my ($class, $vmid, $snapname) = @_;
555 my $storecfg = PVE
::Storage
::config
();
557 my $conf = $class->load_config($vmid);
559 my $get_snapshot_config = sub {
561 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
563 my $res = $conf->{snapshots
}->{$snapname};
565 die "snapshot '$snapname' does not exist\n" if !defined($res);
570 my $snap = &$get_snapshot_config();
572 $class->__snapshot_foreach_volume($snap, sub {
573 my ($vs, $volume) = @_;
575 $class->__snapshot_rollback_vol_possible($volume, $snapname);
580 $conf = $class->load_config($vmid);
582 $snap = &$get_snapshot_config();
584 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
585 if $snap->{snapstate
};
588 $class->check_lock($conf);
589 $class->__snapshot_rollback_vm_stop($vmid);
592 die "unable to rollback vm $vmid: vm is running\n"
593 if $class->__snapshot_check_running($vmid);
596 $conf->{lock} = 'rollback';
598 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
599 delete $conf->{lock};
602 # machine only relevant for Qemu
606 my $has_machine_config = defined($conf->{machine
});
608 # copy snapshot config to current config
609 $conf = $class->__snapshot_apply_config($conf, $snap);
610 $conf->{parent
} = $snapname;
612 # Note: old code did not store 'machine', so we try to be smart
613 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
614 $forcemachine = $conf->{machine
} || 'pc-i440fx-1.4';
615 # we remove the 'machine' configuration if not explicitly specified
616 # in the original config.
617 delete $conf->{machine
} if $snap->{vmstate
} && !$has_machine_config;
620 $class->write_config($vmid, $conf);
622 if (!$prepare && $snap->{vmstate
}) {
623 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate
}, $forcemachine);
627 $class->lock_config($vmid, $updatefn);
629 $class->__snapshot_foreach_volume($snap, sub {
630 my ($vs, $volume) = @_;
632 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
636 $class->lock_config($vmid, $updatefn);