]> git.proxmox.com Git - pve-common.git/blame - src/PVE/AbstractConfig.pm
runtest.pl: use lib '.' - because newer perl does not have that by default
[pve-common.git] / src / PVE / AbstractConfig.pm
CommitLineData
53c25521
FG
1package PVE::AbstractConfig;
2
3use strict;
4use warnings;
5
6use PVE::Tools qw(lock_file lock_file_full);
7use PVE::INotify;
8
9my $nodename = PVE::INotify::nodename();
10
11# Printable string, currently either "VM" or "CT"
12sub guest_type {
13 my ($class) = @_;
14 die "abstract method - implement me ";
15}
16
17sub __config_max_unused_disks {
18 my ($class) = @_;
19
20 die "implement me - abstract method\n";
21}
22
23# Path to the flock file for this VM/CT
24sub config_file_lock {
25 my ($class, $vmid) = @_;
26 die "abstract method - implement me";
27}
28
29# Relative config file path for this VM/CT in CFS
30sub cfs_config_path {
31 my ($class, $vmid, $node) = @_;
32 die "abstract method - implement me";
33}
34
35# Absolute config file path for this VM/CT
36sub config_file {
37 my ($class, $vmid, $node) = @_;
38
39 my $cfspath = $class->cfs_config_path($vmid, $node);
40 return "/etc/pve/$cfspath";
41}
42
43# Read and parse config file for this VM/CT
44sub load_config {
45 my ($class, $vmid, $node) = @_;
46
47 $node = $nodename if !$node;
48 my $cfspath = $class->cfs_config_path($vmid, $node);
49
50 my $conf = PVE::Cluster::cfs_read_file($cfspath);
51 die "Configuration file '$cfspath' does not exist\n"
52 if !defined($conf);
53
54 return $conf;
55}
56
57# Generate and write config file for this VM/CT
58sub write_config {
59 my ($class, $vmid, $conf) = @_;
60
61 my $cfspath = $class->cfs_config_path($vmid);
62
63 PVE::Cluster::cfs_write_file($cfspath, $conf);
64}
65
66# Lock config file using flock, run $code with @param, unlock config file.
67# $timeout is the maximum time to aquire the flock
68sub lock_config_full {
69 my ($class, $vmid, $timeout, $code, @param) = @_;
70
71 my $filename = $class->config_file_lock($vmid);
72
73 my $res = lock_file($filename, $timeout, $code, @param);
74
75 die $@ if $@;
76
77 return $res;
78}
79
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
83sub lock_config_mode {
84 my ($class, $vmid, $timeout, $shared, $code, @param) = @_;
85
86 my $filename = $class->config_file_lock($vmid);
87
88 my $res = lock_file_full($filename, $timeout, $shared, $code, @param);
89
90 die $@ if $@;
91
92 return $res;
93}
94
95# Lock config file using flock, run $code with @param, unlock config file.
96sub lock_config {
97 my ($class, $vmid, $code, @param) = @_;
98
99 return $class->lock_config_full($vmid, 10, $code, @param);
100}
101
102# Checks whether the config is locked with the lock parameter
103sub check_lock {
104 my ($class, $conf) = @_;
105
106 die $class->guest_type()." is locked ($conf->{lock})\n" if $conf->{lock};
107}
108
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.
111sub has_lock {
112 my ($class, $conf, $lock) = @_;
113
114 return $conf->{lock} && (!defined($lock) || $lock eq $conf->{lock});
115}
116
117# Sets the lock parameter for this VM/CT's config to $lock.
118sub set_lock {
119 my ($class, $vmid, $lock) = @_;
120
121 my $conf;
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);
127 });
128 return $conf;
129}
130
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.
133sub remove_lock {
134 my ($class, $vmid, $lock) = @_;
135
136 $class->lock_config($vmid, sub {
137 my $conf = $class->load_config($vmid);
138 if (!$conf->{lock}) {
34f860a2
FG
139 my $lockstring = defined($lock) ? "'$lock' " : "any";
140 die "no lock found trying to remove $lockstring lock\n";
53c25521 141 } elsif (defined($lock) && $conf->{lock} ne $lock) {
34f860a2 142 die "found lock '$conf->{lock}' trying to remove '$lock' lock\n";
53c25521
FG
143 }
144 delete $conf->{lock};
145 $class->write_config($vmid, $conf);
146 });
147}
148
149# Checks whether protection mode is enabled for this VM/CT.
150sub check_protection {
151 my ($class, $conf, $err_msg) = @_;
152
153 if ($conf->{protection}) {
154 die "$err_msg - protection mode enabled\n";
155 }
156}
157
158# Adds an unused volume to $config, if possible.
159sub add_unused_volume {
160 my ($class, $config, $volid) = @_;
161
162 my $key;
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
167 } else {
168 $key = $test;
169 }
170 }
171
172 die "Too many unused volumes - please delete them first.\n" if !$key;
173
174 $config->{$key} = $volid;
175
176 return $key;
177}
178
179# Returns whether the template parameter is set in $conf.
180sub is_template {
181 my ($class, $conf) = @_;
182
183 return 1 if defined $conf->{template} && $conf->{template} == 1;
184}
185
186# Checks whether $feature is availabe for the referenced volumes in $conf.
187# Note: depending on the parameters, some volumes may be skipped!
188sub has_feature {
189 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
190 die "implement me - abstract method\n";
191}
192
193# Internal snapshots
194
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.
199
200# Save the vmstate (RAM).
201sub __snapshot_save_vmstate {
202 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
203 die "implement me - abstract method\n";
204}
205
206# Check whether the VM/CT is running.
207sub __snapshot_check_running {
208 my ($class, $vmid) = @_;
209 die "implement me - abstract method\n";
210}
211
212# Check whether we need to freeze the VM/CT
213sub __snapshot_check_freeze_needed {
214 my ($sself, $vmid, $config, $save_vmstate) = @_;
215 die "implement me - abstract method\n";
216}
217
218# Freeze or unfreeze the VM/CT.
219sub __snapshot_freeze {
220 my ($class, $vmid, $unfreeze) = @_;
221
222 die "abstract method - implement me\n";
223}
224
ba9db5df
FG
225# Code run before and after creating all the volume snapshots
226# base: noop
227sub __snapshot_create_vol_snapshots_hook {
228 my ($class, $vmid, $snap, $running, $hook) = @_;
229
230 return;
231}
232
53c25521
FG
233# Create the volume snapshots for the VM/CT.
234sub __snapshot_create_vol_snapshot {
235 my ($class, $vmid, $vs, $volume, $snapname) = @_;
236
237 die "abstract method - implement me\n";
238}
239
240# Remove a drive from the snapshot config.
241sub __snapshot_delete_remove_drive {
e4ae1170 242 my ($class, $snap, $drive) = @_;
53c25521
FG
243
244 die "abstract method - implement me\n";
245}
246
247# Delete the vmstate file/drive
248sub __snapshot_delete_vmstate_file {
249 my ($class, $snap, $force) = @_;
250
251 die "abstract method - implement me\n";
252}
253
254# Delete a volume snapshot
255sub __snapshot_delete_vol_snapshot {
256 my ($class, $vmid, $vs, $volume, $snapname) = @_;
257
258 die "abstract method - implement me\n";
259}
260
261# Checks whether a volume snapshot is possible for this volume.
262sub __snapshot_rollback_vol_possible {
263 my ($class, $volume, $snapname) = @_;
264
265 die "abstract method - implement me\n";
266}
267
268# Rolls back this volume.
269sub __snapshot_rollback_vol_rollback {
270 my ($class, $volume, $snapname) = @_;
271
272 die "abstract method - implement me\n";
273}
274
275# Stops the VM/CT for a rollback.
276sub __snapshot_rollback_vm_stop {
277 my ($class, $vmid) = @_;
278
279 die "abstract method - implement me\n";
280}
281
282# Start the VM/CT after a rollback with restored vmstate.
283sub __snapshot_rollback_vm_start {
284 my ($class, $vmid, $vmstate, $forcemachine);
285
286 die "abstract method - implement me\n";
287}
288
e561bc74
FG
289# Get list of volume IDs which are referenced in $conf, but not in $snap.
290sub __snapshot_rollback_get_unused {
291 my ($class, $conf, $snap) = @_;
292
293 die "abstract method - implement me\n";
294}
295
53c25521
FG
296# Iterate over all configured volumes, calling $func for each key/value pair.
297sub __snapshot_foreach_volume {
298 my ($class, $conf, $func) = @_;
299
300 die "abstract method - implement me\n";
301}
302
303# Copy the current config $source to the snapshot config $dest
304sub __snapshot_copy_config {
305 my ($class, $source, $dest) = @_;
306
307 foreach my $k (keys %$source) {
308 next if $k eq 'snapshots';
309 next if $k eq 'snapstate';
310 next if $k eq 'snaptime';
311 next if $k eq 'vmstate';
312 next if $k eq 'lock';
313 next if $k eq 'digest';
314 next if $k eq 'description';
315 next if $k =~ m/^unused\d+$/;
316
317 $dest->{$k} = $source->{$k};
318 }
319};
320
321# Apply the snapshot config $snap to the config $conf (rollback)
322sub __snapshot_apply_config {
323 my ($class, $conf, $snap) = @_;
324
325 # copy snapshot list
326 my $newconf = {
327 snapshots => $conf->{snapshots},
328 };
329
330 # keep description and list of unused disks
331 foreach my $k (keys %$conf) {
332 next if !($k =~ m/^unused\d+$/ || $k eq 'description');
333 $newconf->{$k} = $conf->{$k};
334 }
335
336 $class->__snapshot_copy_config($snap, $newconf);
337
338 return $newconf;
339}
340
341# Prepares the configuration for snapshotting.
342sub __snapshot_prepare {
343 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
344
345 my $snap;
346
347 my $updatefn = sub {
348
349 my $conf = $class->load_config($vmid);
350
351 die "you can't take a snapshot if it's a template\n"
352 if $class->is_template($conf);
353
354 $class->check_lock($conf);
355
356 $conf->{lock} = 'snapshot';
357
358 die "snapshot name '$snapname' already used\n"
359 if defined($conf->{snapshots}->{$snapname});
360
361 my $storecfg = PVE::Storage::config();
362 die "snapshot feature is not available\n"
363 if !$class->has_feature('snapshot', $conf, $storecfg, undef, undef, $snapname eq 'vzdump');
364
365 $snap = $conf->{snapshots}->{$snapname} = {};
366
367 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
368 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
369 }
370
371 $class->__snapshot_copy_config($conf, $snap);
372
373 $snap->{snapstate} = "prepare";
374 $snap->{snaptime} = time();
375 $snap->{description} = $comment if $comment;
376
377 $class->write_config($vmid, $conf);
378 };
379
380 $class->lock_config($vmid, $updatefn);
381
382 return $snap;
383}
384
385# Commits the configuration after snapshotting.
386sub __snapshot_commit {
387 my ($class, $vmid, $snapname) = @_;
388
389 my $updatefn = sub {
390
391 my $conf = $class->load_config($vmid);
392
393 die "missing snapshot lock\n"
394 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
395
396 my $snap = $conf->{snapshots}->{$snapname};
397 die "snapshot '$snapname' does not exist\n" if !defined($snap);
398
399 die "wrong snapshot state\n"
400 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
401
402 delete $snap->{snapstate};
403 delete $conf->{lock};
404
405 $conf->{parent} = $snapname;
406
407 $class->write_config($vmid, $conf);
408 };
409
410 $class->lock_config($vmid, $updatefn);
411}
412
413# Creates a snapshot for the VM/CT.
414sub snapshot_create {
415 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
416
417 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
418
419 $save_vmstate = 0 if !$snap->{vmstate};
420
421 my $conf = $class->load_config($vmid);
422
423 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
424
425 my $drivehash = {};
426
427 eval {
428 if ($freezefs) {
429 $class->__snapshot_freeze($vmid, 0);
430 }
431
ba9db5df
FG
432 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
433
53c25521
FG
434 $class->__snapshot_foreach_volume($snap, sub {
435 my ($vs, $volume) = @_;
436
437 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
438 $drivehash->{$vs} = 1;
439 });
440 };
441 my $err = $@;
442
443 if ($running) {
ba9db5df 444 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
53c25521
FG
445 if ($freezefs) {
446 $class->__snapshot_freeze($vmid, 1);
447 }
ba9db5df 448 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
53c25521
FG
449 }
450
451 if ($err) {
452 warn "snapshot create failed: starting cleanup\n";
453 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
454 warn "$@" if $@;
455 die "$err\n";
456 }
457
458 $class->__snapshot_commit($vmid, $snapname);
459}
460
461# Deletes a snapshot.
462# Note: $drivehash is only set when called from snapshot_create.
463sub snapshot_delete {
464 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
465
466 my $prepare = 1;
467
468 my $snap;
469 my $unused = [];
470
0037d634
FG
471 $class->set_lock($vmid, 'snapshot-delete')
472 if (!$drivehash); # doesn't already have a 'snapshot' lock
473
53c25521
FG
474 my $unlink_parent = sub {
475 my ($confref, $new_parent) = @_;
476
477 if ($confref->{parent} && $confref->{parent} eq $snapname) {
478 if ($new_parent) {
479 $confref->{parent} = $new_parent;
480 } else {
481 delete $confref->{parent};
482 }
483 }
484 };
485
e4ae1170
FG
486 my $remove_drive = sub {
487 my ($drive) = @_;
53c25521
FG
488
489 my $conf = $class->load_config($vmid);
e4ae1170
FG
490 $snap = $conf->{snapshots}->{$snapname};
491 die "snapshot '$snapname' does not exist\n" if !defined($snap);
53c25521 492
e4ae1170 493 $class->__snapshot_delete_remove_drive($snap, $drive);
53c25521 494
e4ae1170
FG
495 $class->write_config($vmid, $conf);
496 };
53c25521 497
e4ae1170
FG
498 #prepare
499 $class->lock_config($vmid, sub {
500 my $conf = $class->load_config($vmid);
53c25521 501
e4ae1170
FG
502 die "you can't delete a snapshot if vm is a template\n"
503 if $class->is_template($conf);
53c25521 504
e4ae1170
FG
505 $snap = $conf->{snapshots}->{$snapname};
506 die "snapshot '$snapname' does not exist\n" if !defined($snap);
53c25521 507
e4ae1170 508 $snap->{snapstate} = 'delete';
53c25521
FG
509
510 $class->write_config($vmid, $conf);
e4ae1170 511 });
53c25521
FG
512
513 # now remove vmstate file
514 if ($snap->{vmstate}) {
515 $class->__snapshot_delete_vmstate_file($snap, $force);
516
517 # save changes (remove vmstate from snapshot)
e4ae1170 518 $class->lock_config($vmid, $remove_drive, 'vmstate') if !$force;
53c25521
FG
519 };
520
521 # now remove all volume snapshots
522 $class->__snapshot_foreach_volume($snap, sub {
523 my ($vs, $volume) = @_;
524
525 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
526 if (!$drivehash || $drivehash->{$vs}) {
24623202 527 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
53c25521
FG
528 if (my $err = $@) {
529 die $err if !$force;
530 warn $err;
531 }
532 }
533
e4ae1170
FG
534 # save changes (remove drive from snapshot)
535 $class->lock_config($vmid, $remove_drive, $vs) if !$force;
53c25521
FG
536 });
537
538 # now cleanup config
e4ae1170
FG
539 $class->lock_config($vmid, sub {
540 my $conf = $class->load_config($vmid);
541 $snap = $conf->{snapshots}->{$snapname};
542 die "snapshot '$snapname' does not exist\n" if !defined($snap);
543
544 # remove parent refs
545 &$unlink_parent($conf, $snap->{parent});
546 foreach my $sn (keys %{$conf->{snapshots}}) {
547 next if $sn eq $snapname;
548 &$unlink_parent($conf->{snapshots}->{$sn}, $snap->{parent});
549 }
550
551
552 delete $conf->{snapshots}->{$snapname};
553 delete $conf->{lock};
554 foreach my $volid (@$unused) {
555 $class->add_unused_volume($conf, $volid);
556 }
557
558 $class->write_config($vmid, $conf);
559 });
53c25521
FG
560}
561
562# Rolls back to a given snapshot.
563sub snapshot_rollback {
564 my ($class, $vmid, $snapname) = @_;
565
566 my $prepare = 1;
567
568 my $storecfg = PVE::Storage::config();
569
570 my $conf = $class->load_config($vmid);
571
572 my $get_snapshot_config = sub {
573
574 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
575
576 my $res = $conf->{snapshots}->{$snapname};
577
578 die "snapshot '$snapname' does not exist\n" if !defined($res);
579
580 return $res;
581 };
582
583 my $snap = &$get_snapshot_config();
584
585 $class->__snapshot_foreach_volume($snap, sub {
586 my ($vs, $volume) = @_;
587
588 $class->__snapshot_rollback_vol_possible($volume, $snapname);
589 });
590
591 my $updatefn = sub {
592
593 $conf = $class->load_config($vmid);
594
595 $snap = &$get_snapshot_config();
596
597 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
598 if $snap->{snapstate};
599
600 if ($prepare) {
601 $class->check_lock($conf);
602 $class->__snapshot_rollback_vm_stop($vmid);
603 }
604
605 die "unable to rollback vm $vmid: vm is running\n"
606 if $class->__snapshot_check_running($vmid);
607
608 if ($prepare) {
609 $conf->{lock} = 'rollback';
610 } else {
611 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
612 delete $conf->{lock};
613 }
614
615 # machine only relevant for Qemu
616 my $forcemachine;
617
618 if (!$prepare) {
e561bc74
FG
619 my $unused = $class->__snapshot_rollback_get_unused($conf, $snap);
620
621 foreach my $volid (@$unused) {
622 $class->add_unused_volume($conf, $volid);
623 }
624
53c25521
FG
625 my $has_machine_config = defined($conf->{machine});
626
627 # copy snapshot config to current config
628 $conf = $class->__snapshot_apply_config($conf, $snap);
629 $conf->{parent} = $snapname;
630
631 # Note: old code did not store 'machine', so we try to be smart
632 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
633 $forcemachine = $conf->{machine} || 'pc-i440fx-1.4';
634 # we remove the 'machine' configuration if not explicitly specified
635 # in the original config.
636 delete $conf->{machine} if $snap->{vmstate} && !$has_machine_config;
637 }
638
639 $class->write_config($vmid, $conf);
640
641 if (!$prepare && $snap->{vmstate}) {
642 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $forcemachine);
643 }
644 };
645
646 $class->lock_config($vmid, $updatefn);
647
648 $class->__snapshot_foreach_volume($snap, sub {
649 my ($vs, $volume) = @_;
650
651 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
652 });
653
654 $prepare = 0;
655 $class->lock_config($vmid, $updatefn);
656}
657
6581;