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