1 package PVE
::QemuConfig
;
6 use PVE
::AbstractConfig
;
9 use PVE
::QemuServer
::CPUConfig
;
10 use PVE
::QemuServer
::Drive
;
11 use PVE
::QemuServer
::Helpers
;
12 use PVE
::QemuServer
::Monitor
qw(mon_cmd);
14 use PVE
::QemuServer
::Machine
;
17 use PVE
::Format
qw(render_bytes render_duration);
19 use base
qw(PVE::AbstractConfig);
21 my $nodename = PVE
::INotify
::nodename
();
23 mkdir "/etc/pve/nodes/$nodename";
24 mkdir "/etc/pve/nodes/$nodename/qemu-server";
26 my $lock_dir = "/var/lock/qemu-server";
29 sub assert_config_exists_on_node
{
30 my ($vmid, $node) = @_;
34 my $filename = __PACKAGE__-
>config_file($vmid, $node);
35 my $exists = -f
$filename;
37 my $type = guest_type
();
38 die "unable to find configuration file for $type $vmid on node '$node'\n"
42 # BEGIN implemented abstract methods from PVE::AbstractConfig
48 sub __config_max_unused_disks
{
51 return $PVE::QemuServer
::Drive
::MAX_UNUSED_DISKS
;
54 sub config_file_lock
{
55 my ($class, $vmid) = @_;
57 return "$lock_dir/lock-$vmid.conf";
61 my ($class, $vmid, $node) = @_;
63 $node = $nodename if !$node;
64 return "nodes/$node/qemu-server/$vmid.conf";
68 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
71 $class->foreach_volume($conf, sub {
72 my ($ds, $drive) = @_;
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);
83 sub valid_volume_keys
{
84 my ($class, $reverse) = @_;
86 my @keys = PVE
::QemuServer
::Drive
::valid_drive_names
();
88 return $reverse ?
reverse @keys : @keys;
91 # FIXME: adapt parse_drive to use $noerr for better error messages
93 my ($class, $key, $volume_string, $noerr) = @_;
96 if ($key eq 'vmstate') {
97 eval { PVE
::JSONSchema
::check_format
('pve-volume-id', $volume_string) };
102 $volume = { 'file' => $volume_string };
104 $volume = PVE
::QemuServer
::Drive
::parse_drive
($key, $volume_string);
107 die "unable to parse volume\n" if !defined($volume) && !$noerr;
113 my ($class, $key, $volume) = @_;
115 return PVE
::QemuServer
::Drive
::print_drive
($volume);
124 sub get_replicatable_volumes
{
125 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
129 my $test_volid = sub {
130 my ($volid, $attr) = @_;
132 return if $attr->{cdrom
};
134 return if !$cleanup && !$attr->{replicate
};
136 if ($volid =~ m
|^/|) {
137 return if !$attr->{replicate
};
138 return if $cleanup || $noerr;
139 die "unable to replicate local file/device '$volid'\n";
142 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, $noerr);
145 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
146 return if $scfg->{shared
};
148 my ($path, $owner, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
149 return if !$owner || ($owner != $vmid);
151 if ($vtype ne 'images') {
152 return if $cleanup || $noerr;
153 die "unable to replicate volume '$volid', type '$vtype'\n";
156 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
157 return if $cleanup || $noerr;
158 die "missing replicate feature on volume '$volid'\n";
161 $volhash->{$volid} = 1;
164 PVE
::QemuServer
::foreach_volid
($conf, $test_volid);
169 sub get_backup_volumes
{
170 my ($class, $conf) = @_;
172 my $return_volumes = [];
174 my $test_volume = sub {
175 my ($key, $drive) = @_;
177 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
179 my $included = $drive->{backup
} // 1;
180 my $reason = "backup=";
181 $reason .= defined($drive->{backup
}) ?
'no' : 'yes';
183 if ($key =~ m/^efidisk/ && (!defined($conf->{bios
}) || $conf->{bios
} ne 'ovmf')) {
185 $reason = "efidisk but no OMVF BIOS";
188 push @$return_volumes, {
190 included
=> $included,
192 volume_config
=> $drive,
196 PVE
::QemuConfig-
>foreach_volume($conf, $test_volume);
198 return $return_volumes;
201 sub __snapshot_save_vmstate
{
202 my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
204 # use given storage or search for one from the config
205 my $target = $statestorage;
208 $target = PVE
::QemuServer
::find_vmstate_storage
($conf, $storecfg);
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);
221 my $name = "vm-$vmid-state-$snapname";
222 $name .= ".raw" if $scfg->{path
}; # add filename extension for file base storage
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);
227 # get current QEMU -cpu argument to ensure consistency of custom CPU models
229 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
230 $runningcpu = PVE
::QemuServer
::CPUConfig
::get_cpu_from_running_vm
($pid);
234 $conf = $conf->{snapshots
}->{$snapname};
237 $conf->{vmstate
} = $statefile;
238 $conf->{runningmachine
} = $runningmachine;
239 $conf->{runningcpu
} = $runningcpu;
244 sub __snapshot_activate_storages
{
245 my ($class, $conf, $include_vmstate) = @_;
247 my $storecfg = PVE
::Storage
::config
();
248 my $opts = $include_vmstate ?
{ 'extra_keys' => ['vmstate'] } : {};
249 my $storage_hash = {};
251 $class->foreach_volume_full($conf, $opts, sub {
252 my ($key, $drive) = @_;
254 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
256 my ($storeid) = PVE
::Storage
::parse_volume_id
($drive->{file
});
257 $storage_hash->{$storeid} = 1;
260 PVE
::Storage
::activate_storage_list
($storecfg, [ sort keys $storage_hash->%* ]);
263 sub __snapshot_check_running
{
264 my ($class, $vmid) = @_;
265 return PVE
::QemuServer
::Helpers
::vm_running_locally
($vmid);
268 sub __snapshot_check_freeze_needed
{
269 my ($class, $vmid, $config, $save_vmstate) = @_;
271 my $running = $class->__snapshot_check_running($vmid);
272 if (!$save_vmstate) {
273 return ($running, $running && PVE
::QemuServer
::parse_guest_agent
($config)->{enabled
} && PVE
::QemuServer
::qga_check_running
($vmid));
275 return ($running, 0);
279 sub __snapshot_freeze
{
280 my ($class, $vmid, $unfreeze) = @_;
283 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
284 warn "guest-fsfreeze-thaw problems - $@" if $@;
286 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
287 warn "guest-fsfreeze-freeze problems - $@" if $@;
291 sub __snapshot_create_vol_snapshots_hook
{
292 my ($class, $vmid, $snap, $running, $hook) = @_;
295 my $storecfg = PVE
::Storage
::config
();
297 if ($hook eq "before") {
298 if ($snap->{vmstate
}) {
299 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
300 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
301 my $state_storage_id = PVE
::Storage
::parse_volume_id
($snap->{vmstate
});
303 PVE
::QemuServer
::set_migration_caps
($vmid, 1);
304 mon_cmd
($vmid, "savevm-start", statefile
=> $path);
305 print "saving VM state and RAM using storage '$state_storage_id'\n";
306 my $render_state = sub {
308 my $b = render_bytes
($stat->{bytes
});
309 my $t = render_duration
($stat->{'total-time'} / 1000);
315 my $stat = mon_cmd
($vmid, "query-savevm");
316 if (!$stat->{status
}) {
317 die "savevm not active\n";
318 } elsif ($stat->{status
} eq 'active') {
319 if ($round < 60 || $round % 10 == 0) {
320 my ($b, $t) = $render_state->($stat);
323 print "reducing reporting rate to every 10s\n" if $round == 60;
326 } elsif ($stat->{status
} eq 'completed') {
327 my ($b, $t) = $render_state->($stat);
328 print "completed saving the VM state in $t, saved $b\n";
330 } elsif ($stat->{status
} eq 'failed') {
331 my $err = $stat->{error
} || 'unknown error';
332 die "unable to save VM state and RAM - $err\n";
334 die "query-savevm returned unexpected status '$stat->{status}'\n";
338 mon_cmd
($vmid, "savevm-start");
340 } elsif ($hook eq "after") {
342 mon_cmd
($vmid, "savevm-end");
343 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
346 } elsif ($hook eq "after-freeze") {
347 # savevm-end is async, we need to wait
349 my $stat = mon_cmd
($vmid, "query-savevm");
350 if (!$stat->{bytes
}) {
353 print "savevm not yet finished\n";
362 sub __snapshot_create_vol_snapshot
{
363 my ($class, $vmid, $ds, $drive, $snapname) = @_;
365 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
367 my $volid = $drive->{file
};
368 my $device = "drive-$ds";
369 my $storecfg = PVE
::Storage
::config
();
371 print "snapshotting '$device' ($drive->{file})\n";
373 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
376 sub __snapshot_delete_remove_drive
{
377 my ($class, $snap, $remove_drive) = @_;
379 if ($remove_drive eq 'vmstate') {
380 delete $snap->{$remove_drive};
382 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
383 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
385 my $volid = $drive->{file
};
386 delete $snap->{$remove_drive};
387 $class->add_unused_volume($snap, $volid);
391 sub __snapshot_delete_vmstate_file
{
392 my ($class, $snap, $force) = @_;
394 my $storecfg = PVE
::Storage
::config
();
396 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
403 sub __snapshot_delete_vol_snapshot
{
404 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
406 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
407 my $storecfg = PVE
::Storage
::config
();
408 my $volid = $drive->{file
};
409 my $device = "drive-$ds";
411 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
413 push @$unused, $volid;
416 sub __snapshot_rollback_hook
{
417 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
420 # we save the machine of the current config
421 $data->{oldmachine
} = $conf->{machine
};
423 # if we have a 'runningmachine' entry in the snapshot we use that
424 # for the forcemachine parameter, else we use the old logic
425 if (defined($conf->{runningmachine
})) {
426 $data->{forcemachine
} = $conf->{runningmachine
};
427 delete $conf->{runningmachine
};
429 # runningcpu is newer than runningmachine, so assume it only exists
431 $data->{forcecpu
} = delete $conf->{runningcpu
}
432 if defined($conf->{runningcpu
});
434 # Note: old code did not store 'machine', so we try to be smart
435 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
436 $data->{forcemachine
} = $conf->{machine
} || 'pc-i440fx-1.4';
438 # we remove the 'machine' configuration if not explicitly specified
439 # in the original config.
440 delete $conf->{machine
} if $snap->{vmstate
} && !defined($data->{oldmachine
});
443 if ($conf->{vmgenid
}) {
444 # tell the VM that it's another generation, so it can react
445 # appropriately, e.g. dirty-mark copies of distributed databases or
446 # re-initializing its random number generator
447 $conf->{vmgenid
} = PVE
::QemuServer
::generate_uuid
();
454 sub __snapshot_rollback_vol_possible
{
455 my ($class, $drive, $snapname, $blockers) = @_;
457 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
459 my $storecfg = PVE
::Storage
::config
();
460 my $volid = $drive->{file
};
462 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname, $blockers);
465 sub __snapshot_rollback_vol_rollback
{
466 my ($class, $drive, $snapname) = @_;
468 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
470 my $storecfg = PVE
::Storage
::config
();
471 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
474 sub __snapshot_rollback_vm_stop
{
475 my ($class, $vmid) = @_;
477 my $storecfg = PVE
::Storage
::config
();
478 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
481 sub __snapshot_rollback_vm_start
{
482 my ($class, $vmid, $vmstate, $data) = @_;
484 my $storecfg = PVE
::Storage
::config
();
486 statefile
=> $vmstate,
487 forcemachine
=> $data->{forcemachine
},
488 forcecpu
=> $data->{forcecpu
},
490 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $params);
493 sub __snapshot_rollback_get_unused
{
494 my ($class, $conf, $snap) = @_;
498 $class->foreach_volume($conf, sub {
499 my ($vs, $volume) = @_;
501 return if PVE
::QemuServer
::drive_is_cdrom
($volume, 1);
504 my $volid = $volume->{file
};
506 $class->foreach_volume($snap, sub {
507 my ($ds, $drive) = @_;
510 return if PVE
::QemuServer
::drive_is_cdrom
($drive, 1);
513 if ($drive->{file
} && $drive->{file
} eq $volid);
516 push @$unused, $volid if !$found;
522 sub add_unused_volume
{
523 my ($class, $config, $volid) = @_;
525 if ($volid =~ m/vm-\d+-cloudinit/) {
526 print "found unused cloudinit disk '$volid', removing it\n";
527 my $storecfg = PVE
::Storage
::config
();
528 PVE
::Storage
::vdisk_free
($storecfg, $volid);
531 return $class->SUPER::add_unused_volume
($config, $volid);
535 sub load_current_config
{
536 my ($class, $vmid, $current) = @_;
538 my $conf = $class->SUPER::load_current_config
($vmid, $current);
539 delete $conf->{cloudinit
};
543 # END implemented abstract methods from PVE::AbstractConfig
546 my ($class, $conf, $skip) = @_;
550 $class->foreach_volume($conf, sub {
551 my ($key, $volume) = @_;
553 return if ($skip && $skip eq $key) || $found;
554 $found = $key if PVE
::QemuServer
::Drive
::drive_is_cloudinit
($volume);