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