]> git.proxmox.com Git - pve-common.git/blame - src/PVE/AbstractConfig.pm
update changelog
[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 {
242 my ($class, $snap, $remove_drive) = @_;
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
289# Iterate over all configured volumes, calling $func for each key/value pair.
290sub __snapshot_foreach_volume {
291 my ($class, $conf, $func) = @_;
292
293 die "abstract method - implement me\n";
294}
295
296# Copy the current config $source to the snapshot config $dest
297sub __snapshot_copy_config {
298 my ($class, $source, $dest) = @_;
299
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+$/;
309
310 $dest->{$k} = $source->{$k};
311 }
312};
313
314# Apply the snapshot config $snap to the config $conf (rollback)
315sub __snapshot_apply_config {
316 my ($class, $conf, $snap) = @_;
317
318 # copy snapshot list
319 my $newconf = {
320 snapshots => $conf->{snapshots},
321 };
322
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};
327 }
328
329 $class->__snapshot_copy_config($snap, $newconf);
330
331 return $newconf;
332}
333
334# Prepares the configuration for snapshotting.
335sub __snapshot_prepare {
336 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
337
338 my $snap;
339
340 my $updatefn = sub {
341
342 my $conf = $class->load_config($vmid);
343
344 die "you can't take a snapshot if it's a template\n"
345 if $class->is_template($conf);
346
347 $class->check_lock($conf);
348
349 $conf->{lock} = 'snapshot';
350
351 die "snapshot name '$snapname' already used\n"
352 if defined($conf->{snapshots}->{$snapname});
353
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');
357
358 $snap = $conf->{snapshots}->{$snapname} = {};
359
360 if ($save_vmstate && $class->__snapshot_check_running($vmid)) {
361 $class->__snapshot_save_vmstate($vmid, $conf, $snapname, $storecfg);
362 }
363
364 $class->__snapshot_copy_config($conf, $snap);
365
366 $snap->{snapstate} = "prepare";
367 $snap->{snaptime} = time();
368 $snap->{description} = $comment if $comment;
369
370 $class->write_config($vmid, $conf);
371 };
372
373 $class->lock_config($vmid, $updatefn);
374
375 return $snap;
376}
377
378# Commits the configuration after snapshotting.
379sub __snapshot_commit {
380 my ($class, $vmid, $snapname) = @_;
381
382 my $updatefn = sub {
383
384 my $conf = $class->load_config($vmid);
385
386 die "missing snapshot lock\n"
387 if !($conf->{lock} && $conf->{lock} eq 'snapshot');
388
389 my $snap = $conf->{snapshots}->{$snapname};
390 die "snapshot '$snapname' does not exist\n" if !defined($snap);
391
392 die "wrong snapshot state\n"
393 if !($snap->{snapstate} && $snap->{snapstate} eq "prepare");
394
395 delete $snap->{snapstate};
396 delete $conf->{lock};
397
398 $conf->{parent} = $snapname;
399
400 $class->write_config($vmid, $conf);
401 };
402
403 $class->lock_config($vmid, $updatefn);
404}
405
406# Creates a snapshot for the VM/CT.
407sub snapshot_create {
408 my ($class, $vmid, $snapname, $save_vmstate, $comment) = @_;
409
410 my $snap = $class->__snapshot_prepare($vmid, $snapname, $save_vmstate, $comment);
411
412 $save_vmstate = 0 if !$snap->{vmstate};
413
414 my $conf = $class->load_config($vmid);
415
416 my ($running, $freezefs) = $class->__snapshot_check_freeze_needed($vmid, $conf, $snap->{vmstate});
417
418 my $drivehash = {};
419
420 eval {
421 if ($freezefs) {
422 $class->__snapshot_freeze($vmid, 0);
423 }
424
ba9db5df
FG
425 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "before");
426
53c25521
FG
427 $class->__snapshot_foreach_volume($snap, sub {
428 my ($vs, $volume) = @_;
429
430 $class->__snapshot_create_vol_snapshot($vmid, $vs, $volume, $snapname);
431 $drivehash->{$vs} = 1;
432 });
433 };
434 my $err = $@;
435
436 if ($running) {
ba9db5df 437 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after");
53c25521
FG
438 if ($freezefs) {
439 $class->__snapshot_freeze($vmid, 1);
440 }
ba9db5df 441 $class->__snapshot_create_vol_snapshots_hook($vmid, $snap, $running, "after-unfreeze");
53c25521
FG
442 }
443
444 if ($err) {
445 warn "snapshot create failed: starting cleanup\n";
446 eval { $class->snapshot_delete($vmid, $snapname, 1, $drivehash); };
447 warn "$@" if $@;
448 die "$err\n";
449 }
450
451 $class->__snapshot_commit($vmid, $snapname);
452}
453
454# Deletes a snapshot.
455# Note: $drivehash is only set when called from snapshot_create.
456sub snapshot_delete {
457 my ($class, $vmid, $snapname, $force, $drivehash) = @_;
458
459 my $prepare = 1;
460
461 my $snap;
462 my $unused = [];
463
464 my $unlink_parent = sub {
465 my ($confref, $new_parent) = @_;
466
467 if ($confref->{parent} && $confref->{parent} eq $snapname) {
468 if ($new_parent) {
469 $confref->{parent} = $new_parent;
470 } else {
471 delete $confref->{parent};
472 }
473 }
474 };
475
476 my $updatefn = sub {
477 my ($remove_drive) = @_;
478
479 my $conf = $class->load_config($vmid);
480
481 if (!$drivehash) {
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);
485 }
486
487 $snap = $conf->{snapshots}->{$snapname};
488
489 die "snapshot '$snapname' does not exist\n" if !defined($snap);
490
491 # remove parent refs
492 if (!$prepare) {
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});
497 }
498 }
499
500 if ($remove_drive) {
501 $class->__snapshot_delete_remove_drive($snap, $remove_drive);
502 }
503
504 if ($prepare) {
505 $snap->{snapstate} = 'delete';
506 } else {
507 delete $conf->{snapshots}->{$snapname};
508 delete $conf->{lock} if $drivehash;
509 foreach my $volid (@$unused) {
510 $class->add_unused_volume($conf, $volid);
511 }
512 }
513
514 $class->write_config($vmid, $conf);
515 };
516
517 $class->lock_config($vmid, $updatefn);
518
519 # now remove vmstate file
520 if ($snap->{vmstate}) {
521 $class->__snapshot_delete_vmstate_file($snap, $force);
522
523 # save changes (remove vmstate from snapshot)
524 $class->lock_config($vmid, $updatefn, 'vmstate') if !$force;
525 };
526
527 # now remove all volume snapshots
528 $class->__snapshot_foreach_volume($snap, sub {
529 my ($vs, $volume) = @_;
530
531 return if $snapname eq 'vzdump' && $vs ne 'rootfs' && !$volume->{backup};
532 if (!$drivehash || $drivehash->{$vs}) {
24623202 533 eval { $class->__snapshot_delete_vol_snapshot($vmid, $vs, $volume, $snapname, $unused); };
53c25521
FG
534 if (my $err = $@) {
535 die $err if !$force;
536 warn $err;
537 }
538 }
539
540 # save changes (remove mp from snapshot)
541 $class->lock_config($vmid, $updatefn, $vs) if !$force;
53c25521
FG
542 });
543
544 # now cleanup config
545 $prepare = 0;
546 $class->lock_config($vmid, $updatefn);
547}
548
549# Rolls back to a given snapshot.
550sub snapshot_rollback {
551 my ($class, $vmid, $snapname) = @_;
552
553 my $prepare = 1;
554
555 my $storecfg = PVE::Storage::config();
556
557 my $conf = $class->load_config($vmid);
558
559 my $get_snapshot_config = sub {
560
561 die "you can't rollback if vm is a template\n" if $class->is_template($conf);
562
563 my $res = $conf->{snapshots}->{$snapname};
564
565 die "snapshot '$snapname' does not exist\n" if !defined($res);
566
567 return $res;
568 };
569
570 my $snap = &$get_snapshot_config();
571
572 $class->__snapshot_foreach_volume($snap, sub {
573 my ($vs, $volume) = @_;
574
575 $class->__snapshot_rollback_vol_possible($volume, $snapname);
576 });
577
578 my $updatefn = sub {
579
580 $conf = $class->load_config($vmid);
581
582 $snap = &$get_snapshot_config();
583
584 die "unable to rollback to incomplete snapshot (snapstate = $snap->{snapstate})\n"
585 if $snap->{snapstate};
586
587 if ($prepare) {
588 $class->check_lock($conf);
589 $class->__snapshot_rollback_vm_stop($vmid);
590 }
591
592 die "unable to rollback vm $vmid: vm is running\n"
593 if $class->__snapshot_check_running($vmid);
594
595 if ($prepare) {
596 $conf->{lock} = 'rollback';
597 } else {
598 die "got wrong lock\n" if !($conf->{lock} && $conf->{lock} eq 'rollback');
599 delete $conf->{lock};
600 }
601
602 # machine only relevant for Qemu
603 my $forcemachine;
604
605 if (!$prepare) {
606 my $has_machine_config = defined($conf->{machine});
607
608 # copy snapshot config to current config
609 $conf = $class->__snapshot_apply_config($conf, $snap);
610 $conf->{parent} = $snapname;
611
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;
618 }
619
620 $class->write_config($vmid, $conf);
621
622 if (!$prepare && $snap->{vmstate}) {
623 $class->__snapshot_rollback_vm_start($vmid, $snap->{vmstate}, $forcemachine);
624 }
625 };
626
627 $class->lock_config($vmid, $updatefn);
628
629 $class->__snapshot_foreach_volume($snap, sub {
630 my ($vs, $volume) = @_;
631
632 $class->__snapshot_rollback_vol_rollback($volume, $snapname);
633 });
634
635 $prepare = 0;
636 $class->lock_config($vmid, $updatefn);
637}
638
6391;