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