]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuConfig.pm
savevm: show information about drives during snapshot
[qemu-server.git] / PVE / QemuConfig.pm
1 package PVE::QemuConfig;
2
3 use strict;
4 use warnings;
5
6 use PVE::AbstractConfig;
7 use PVE::INotify;
8 use PVE::JSONSchema;
9 use PVE::QemuServer::CPUConfig;
10 use PVE::QemuServer::Drive;
11 use PVE::QemuServer::Helpers;
12 use PVE::QemuServer::Monitor qw(mon_cmd);
13 use PVE::QemuServer;
14 use PVE::QemuServer::Machine;
15 use PVE::Storage;
16 use PVE::Tools;
17 use PVE::Format qw(render_bytes render_duration);
18
19 use base qw(PVE::AbstractConfig);
20
21 my $nodename = PVE::INotify::nodename();
22
23 mkdir "/etc/pve/nodes/$nodename";
24 mkdir "/etc/pve/nodes/$nodename/qemu-server";
25
26 my $lock_dir = "/var/lock/qemu-server";
27 mkdir $lock_dir;
28
29 sub assert_config_exists_on_node {
30 my ($vmid, $node) = @_;
31
32 $node //= $nodename;
33
34 my $filename = __PACKAGE__->config_file($vmid, $node);
35 my $exists = -f $filename;
36
37 my $type = guest_type();
38 die "unable to find configuration file for $type $vmid on node '$node'\n"
39 if !$exists;
40 }
41
42 # BEGIN implemented abstract methods from PVE::AbstractConfig
43
44 sub guest_type {
45 return "VM";
46 }
47
48 sub __config_max_unused_disks {
49 my ($class) = @_;
50
51 return $PVE::QemuServer::Drive::MAX_UNUSED_DISKS;
52 }
53
54 sub config_file_lock {
55 my ($class, $vmid) = @_;
56
57 return "$lock_dir/lock-$vmid.conf";
58 }
59
60 sub cfs_config_path {
61 my ($class, $vmid, $node) = @_;
62
63 $node = $nodename if !$node;
64 return "nodes/$node/qemu-server/$vmid.conf";
65 }
66
67 sub has_feature {
68 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
69
70 my $err;
71 $class->foreach_volume($conf, sub {
72 my ($ds, $drive) = @_;
73
74 return if PVE::QemuServer::drive_is_cdrom($drive);
75 return if $backup_only && defined($drive->{backup}) && !$drive->{backup};
76 my $volid = $drive->{file};
77 $err = 1 if !PVE::Storage::volume_has_feature($storecfg, $feature, $volid, $snapname, $running);
78 });
79
80 return $err ? 0 : 1;
81 }
82
83 sub valid_volume_keys {
84 my ($class, $reverse) = @_;
85
86 my @keys = PVE::QemuServer::Drive::valid_drive_names();
87
88 return $reverse ? reverse @keys : @keys;
89 }
90
91 # FIXME: adapt parse_drive to use $noerr for better error messages
92 sub parse_volume {
93 my ($class, $key, $volume_string, $noerr) = @_;
94
95 my $volume;
96 if ($key eq 'vmstate') {
97 eval { PVE::JSONSchema::check_format('pve-volume-id', $volume_string) };
98 if (my $err = $@) {
99 return if $noerr;
100 die $err;
101 }
102 $volume = { 'file' => $volume_string };
103 } else {
104 $volume = PVE::QemuServer::Drive::parse_drive($key, $volume_string);
105 }
106
107 die "unable to parse volume\n" if !defined($volume) && !$noerr;
108
109 return $volume;
110 }
111
112 sub print_volume {
113 my ($class, $key, $volume) = @_;
114
115 return PVE::QemuServer::Drive::print_drive($volume);
116 }
117
118 sub volid_key {
119 my ($class) = @_;
120
121 return 'file';
122 }
123
124 sub get_replicatable_volumes {
125 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
126
127 my $volhash = {};
128
129 my $test_volid = sub {
130 my ($volid, $attr) = @_;
131
132 return if $attr->{cdrom};
133
134 return if !$cleanup && !$attr->{replicate};
135
136 if ($volid =~ m|^/|) {
137 return if !$attr->{replicate};
138 return if $cleanup || $noerr;
139 die "unable to replicate local file/device '$volid'\n";
140 }
141
142 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, $noerr);
143 return if !$storeid;
144
145 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
146 return if $scfg->{shared};
147
148 my ($path, $owner, $vtype) = PVE::Storage::path($storecfg, $volid);
149 return if !$owner || ($owner != $vmid);
150
151 if ($vtype ne 'images') {
152 return if $cleanup || $noerr;
153 die "unable to replicate volume '$volid', type '$vtype'\n";
154 }
155
156 if (!PVE::Storage::volume_has_feature($storecfg, 'replicate', $volid)) {
157 return if $cleanup || $noerr;
158 die "missing replicate feature on volume '$volid'\n";
159 }
160
161 $volhash->{$volid} = 1;
162 };
163
164 PVE::QemuServer::foreach_volid($conf, $test_volid);
165
166 return $volhash;
167 }
168
169 sub get_backup_volumes {
170 my ($class, $conf) = @_;
171
172 my $return_volumes = [];
173
174 my $test_volume = sub {
175 my ($key, $drive) = @_;
176
177 return if PVE::QemuServer::drive_is_cdrom($drive);
178
179 my $included = $drive->{backup} // 1;
180 my $reason = "backup=";
181 $reason .= defined($drive->{backup}) ? 'no' : 'yes';
182
183 if ($key =~ m/^efidisk/ && (!defined($conf->{bios}) || $conf->{bios} ne 'ovmf')) {
184 $included = 0;
185 $reason = "efidisk but no OMVF BIOS";
186 }
187
188 push @$return_volumes, {
189 key => $key,
190 included => $included,
191 reason => $reason,
192 volume_config => $drive,
193 };
194 };
195
196 PVE::QemuConfig->foreach_volume($conf, $test_volume);
197
198 return $return_volumes;
199 }
200
201 sub __snapshot_save_vmstate {
202 my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
203
204 # use given storage or search for one from the config
205 my $target = $statestorage;
206
207 if (!$target) {
208 $target = PVE::QemuServer::find_vmstate_storage($conf, $storecfg);
209 }
210
211 my $defaults = PVE::QemuServer::load_defaults();
212 my $mem_size = $conf->{memory} // $defaults->{memory};
213 my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
214 # our savevm-start does live-save of the memory until the space left in the
215 # volume is just enough for the remaining memory content + internal state
216 # then it stops the vm and copies the rest so we reserve twice the
217 # memory content + state to minimize vm downtime
218 my $size = $mem_size*2 + $driver_state_size;
219 my $scfg = PVE::Storage::storage_config($storecfg, $target);
220
221 my $name = "vm-$vmid-state-$snapname";
222 $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage
223
224 my $statefile = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
225 my $runningmachine = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
226
227 # get current QEMU -cpu argument to ensure consistency of custom CPU models
228 my $runningcpu;
229 if (my $pid = PVE::QemuServer::check_running($vmid)) {
230 $runningcpu = PVE::QemuServer::CPUConfig::get_cpu_from_running_vm($pid);
231 }
232
233 if (!$suspend) {
234 $conf = $conf->{snapshots}->{$snapname};
235 }
236
237 $conf->{vmstate} = $statefile;
238 $conf->{runningmachine} = $runningmachine;
239 $conf->{runningcpu} = $runningcpu;
240
241 return $statefile;
242 }
243
244 sub __snapshot_check_running {
245 my ($class, $vmid) = @_;
246 return PVE::QemuServer::Helpers::vm_running_locally($vmid);
247 }
248
249 sub __snapshot_check_freeze_needed {
250 my ($class, $vmid, $config, $save_vmstate) = @_;
251
252 my $running = $class->__snapshot_check_running($vmid);
253 if (!$save_vmstate) {
254 return ($running, $running && PVE::QemuServer::parse_guest_agent($config)->{enabled} && PVE::QemuServer::qga_check_running($vmid));
255 } else {
256 return ($running, 0);
257 }
258 }
259
260 sub __snapshot_freeze {
261 my ($class, $vmid, $unfreeze) = @_;
262
263 if ($unfreeze) {
264 eval { mon_cmd($vmid, "guest-fsfreeze-thaw"); };
265 warn "guest-fsfreeze-thaw problems - $@" if $@;
266 } else {
267 eval { mon_cmd($vmid, "guest-fsfreeze-freeze"); };
268 warn "guest-fsfreeze-freeze problems - $@" if $@;
269 }
270 }
271
272 sub __snapshot_create_vol_snapshots_hook {
273 my ($class, $vmid, $snap, $running, $hook) = @_;
274
275 if ($running) {
276 my $storecfg = PVE::Storage::config();
277
278 if ($hook eq "before") {
279 if ($snap->{vmstate}) {
280 my $path = PVE::Storage::path($storecfg, $snap->{vmstate});
281 PVE::Storage::activate_volumes($storecfg, [$snap->{vmstate}]);
282
283 mon_cmd($vmid, "savevm-start", statefile => $path);
284 print "saving VM state and RAM\n";
285 my $render_state = sub {
286 my ($stat) = @_;
287 my $b = render_bytes($stat->{bytes});
288 my $t = render_duration($stat->{'total-time'} / 1000);
289 return ($b, $t);
290 };
291 for(;;) {
292 my $stat = mon_cmd($vmid, "query-savevm");
293 if (!$stat->{status}) {
294 die "savevm not active\n";
295 } elsif ($stat->{status} eq 'active') {
296 sleep(1);
297 my ($b, $t) = $render_state->($stat);
298 print "$b in $t\n";
299 next;
300 } elsif ($stat->{status} eq 'completed') {
301 my ($b, $t) = $render_state->($stat);
302 print "completed savevm in $t, saved $b\n";
303 last;
304 } else {
305 die "query-savevm returned status '$stat->{status}'\n";
306 }
307 }
308 } else {
309 mon_cmd($vmid, "savevm-start");
310 }
311 } elsif ($hook eq "after") {
312 eval {
313 mon_cmd($vmid, "savevm-end");
314 PVE::Storage::deactivate_volumes($storecfg, [$snap->{vmstate}]) if $snap->{vmstate};
315 };
316 warn $@ if $@;
317 } elsif ($hook eq "after-freeze") {
318 # savevm-end is async, we need to wait
319 for (;;) {
320 my $stat = mon_cmd($vmid, "query-savevm");
321 if (!$stat->{bytes}) {
322 last;
323 } else {
324 print "savevm not yet finished\n";
325 sleep(1);
326 next;
327 }
328 }
329 }
330 }
331 }
332
333 sub __snapshot_create_vol_snapshot {
334 my ($class, $vmid, $ds, $drive, $snapname) = @_;
335
336 return if PVE::QemuServer::drive_is_cdrom($drive);
337
338 my $volid = $drive->{file};
339 my $device = "drive-$ds";
340 my $storecfg = PVE::Storage::config();
341
342 print "snapshotting '$device' ($drive->{file})\n";
343
344 PVE::QemuServer::qemu_volume_snapshot($vmid, $device, $storecfg, $volid, $snapname);
345 }
346
347 sub __snapshot_delete_remove_drive {
348 my ($class, $snap, $remove_drive) = @_;
349
350 if ($remove_drive eq 'vmstate') {
351 delete $snap->{$remove_drive};
352 } else {
353 my $drive = PVE::QemuServer::parse_drive($remove_drive, $snap->{$remove_drive});
354 return if PVE::QemuServer::drive_is_cdrom($drive);
355
356 my $volid = $drive->{file};
357 delete $snap->{$remove_drive};
358 $class->add_unused_volume($snap, $volid);
359 }
360 }
361
362 sub __snapshot_delete_vmstate_file {
363 my ($class, $snap, $force) = @_;
364
365 my $storecfg = PVE::Storage::config();
366
367 eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
368 if (my $err = $@) {
369 die $err if !$force;
370 warn $err;
371 }
372 }
373
374 sub __snapshot_delete_vol_snapshot {
375 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
376
377 return if PVE::QemuServer::drive_is_cdrom($drive);
378 my $storecfg = PVE::Storage::config();
379 my $volid = $drive->{file};
380 my $device = "drive-$ds";
381
382 PVE::QemuServer::qemu_volume_snapshot_delete($vmid, $device, $storecfg, $volid, $snapname);
383
384 push @$unused, $volid;
385 }
386
387 sub __snapshot_rollback_hook {
388 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
389
390 if ($prepare) {
391 # we save the machine of the current config
392 $data->{oldmachine} = $conf->{machine};
393 } else {
394 # if we have a 'runningmachine' entry in the snapshot we use that
395 # for the forcemachine parameter, else we use the old logic
396 if (defined($conf->{runningmachine})) {
397 $data->{forcemachine} = $conf->{runningmachine};
398 delete $conf->{runningmachine};
399
400 # runningcpu is newer than runningmachine, so assume it only exists
401 # here, if at all
402 $data->{forcecpu} = delete $conf->{runningcpu}
403 if defined($conf->{runningcpu});
404 } else {
405 # Note: old code did not store 'machine', so we try to be smart
406 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
407 $data->{forcemachine} = $conf->{machine} || 'pc-i440fx-1.4';
408
409 # we remove the 'machine' configuration if not explicitly specified
410 # in the original config.
411 delete $conf->{machine} if $snap->{vmstate} && !defined($data->{oldmachine});
412 }
413
414 if ($conf->{vmgenid}) {
415 # tell the VM that it's another generation, so it can react
416 # appropriately, e.g. dirty-mark copies of distributed databases or
417 # re-initializing its random number generator
418 $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
419 }
420 }
421
422 return;
423 }
424
425 sub __snapshot_rollback_vol_possible {
426 my ($class, $drive, $snapname) = @_;
427
428 return if PVE::QemuServer::drive_is_cdrom($drive);
429
430 my $storecfg = PVE::Storage::config();
431 my $volid = $drive->{file};
432
433 PVE::Storage::volume_rollback_is_possible($storecfg, $volid, $snapname);
434 }
435
436 sub __snapshot_rollback_vol_rollback {
437 my ($class, $drive, $snapname) = @_;
438
439 return if PVE::QemuServer::drive_is_cdrom($drive);
440
441 my $storecfg = PVE::Storage::config();
442 PVE::Storage::volume_snapshot_rollback($storecfg, $drive->{file}, $snapname);
443 }
444
445 sub __snapshot_rollback_vm_stop {
446 my ($class, $vmid) = @_;
447
448 my $storecfg = PVE::Storage::config();
449 PVE::QemuServer::vm_stop($storecfg, $vmid, undef, undef, 5, undef, undef);
450 }
451
452 sub __snapshot_rollback_vm_start {
453 my ($class, $vmid, $vmstate, $data) = @_;
454
455 my $storecfg = PVE::Storage::config();
456 my $params = {
457 statefile => $vmstate,
458 forcemachine => $data->{forcemachine},
459 forcecpu => $data->{forcecpu},
460 };
461 PVE::QemuServer::vm_start($storecfg, $vmid, $params);
462 }
463
464 sub __snapshot_rollback_get_unused {
465 my ($class, $conf, $snap) = @_;
466
467 my $unused = [];
468
469 $class->foreach_volume($conf, sub {
470 my ($vs, $volume) = @_;
471
472 return if PVE::QemuServer::drive_is_cdrom($volume);
473
474 my $found = 0;
475 my $volid = $volume->{file};
476
477 $class->foreach_volume($snap, sub {
478 my ($ds, $drive) = @_;
479
480 return if $found;
481 return if PVE::QemuServer::drive_is_cdrom($drive);
482
483 $found = 1
484 if ($drive->{file} && $drive->{file} eq $volid);
485 });
486
487 push @$unused, $volid if !$found;
488 });
489
490 return $unused;
491 }
492
493 # END implemented abstract methods from PVE::AbstractConfig
494
495 1;