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
;
18 use base
qw(PVE::AbstractConfig);
20 my $nodename = PVE
::INotify
::nodename
();
22 mkdir "/etc/pve/nodes/$nodename";
23 mkdir "/etc/pve/nodes/$nodename/qemu-server";
25 my $lock_dir = "/var/lock/qemu-server";
28 sub assert_config_exists_on_node
{
29 my ($vmid, $node) = @_;
33 my $filename = __PACKAGE__-
>config_file($vmid, $node);
34 my $exists = -f
$filename;
36 my $type = guest_type
();
37 die "unable to find configuration file for $type $vmid on node '$node'\n"
41 # BEGIN implemented abstract methods from PVE::AbstractConfig
47 sub __config_max_unused_disks
{
50 return $PVE::QemuServer
::Drive
::MAX_UNUSED_DISKS
;
53 sub config_file_lock
{
54 my ($class, $vmid) = @_;
56 return "$lock_dir/lock-$vmid.conf";
60 my ($class, $vmid, $node) = @_;
62 $node = $nodename if !$node;
63 return "nodes/$node/qemu-server/$vmid.conf";
67 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
70 $class->foreach_volume($conf, sub {
71 my ($ds, $drive) = @_;
73 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
74 return if $backup_only && defined($drive->{backup
}) && !$drive->{backup
};
75 my $volid = $drive->{file
};
76 $err = 1 if !PVE
::Storage
::volume_has_feature
($storecfg, $feature, $volid, $snapname, $running);
82 sub valid_volume_keys
{
83 my ($class, $reverse) = @_;
85 my @keys = PVE
::QemuServer
::Drive
::valid_drive_names
();
87 return $reverse ?
reverse @keys : @keys;
90 # FIXME: adapt parse_drive to use $noerr for better error messages
92 my ($class, $key, $volume_string, $noerr) = @_;
95 if ($key eq 'vmstate') {
96 eval { PVE
::JSONSchema
::check_format
('pve-volume-id', $volume_string) };
98 return undef if $noerr;
101 $volume = { 'file' => $volume_string };
103 $volume = PVE
::QemuServer
::Drive
::parse_drive
($key, $volume_string);
106 die "unable to parse volume\n" if !defined($volume) && !$noerr;
112 my ($class, $key, $volume) = @_;
114 return PVE
::QemuServer
::Drive
::print_drive
($volume);
123 sub get_replicatable_volumes
{
124 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
128 my $test_volid = sub {
129 my ($volid, $attr) = @_;
131 return if $attr->{cdrom
};
133 return if !$cleanup && !$attr->{replicate
};
135 if ($volid =~ m
|^/|) {
136 return if !$attr->{replicate
};
137 return if $cleanup || $noerr;
138 die "unable to replicate local file/device '$volid'\n";
141 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, $noerr);
144 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
145 return if $scfg->{shared
};
147 my ($path, $owner, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
148 return if !$owner || ($owner != $vmid);
150 if ($vtype ne 'images') {
151 return if $cleanup || $noerr;
152 die "unable to replicate volume '$volid', type '$vtype'\n";
155 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
156 return if $cleanup || $noerr;
157 die "missing replicate feature on volume '$volid'\n";
160 $volhash->{$volid} = 1;
163 PVE
::QemuServer
::foreach_volid
($conf, $test_volid);
168 sub get_backup_volumes
{
169 my ($class, $conf) = @_;
171 my $return_volumes = [];
173 my $test_volume = sub {
174 my ($key, $drive) = @_;
176 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
178 my $included = $drive->{backup
} // 1;
179 my $reason = "backup=";
180 $reason .= defined($drive->{backup
}) ?
'no' : 'yes';
182 if ($key =~ m/^efidisk/ && (!defined($conf->{bios
}) || $conf->{bios
} ne 'ovmf')) {
184 $reason = "efidisk but no OMVF BIOS";
187 push @$return_volumes, {
189 included
=> $included,
191 volume_config
=> $drive,
195 PVE
::QemuConfig-
>foreach_volume($conf, $test_volume);
197 return $return_volumes;
200 sub __snapshot_save_vmstate
{
201 my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
203 # use given storage or search for one from the config
204 my $target = $statestorage;
207 $target = PVE
::QemuServer
::find_vmstate_storage
($conf, $storecfg);
210 my $defaults = PVE
::QemuServer
::load_defaults
();
211 my $mem_size = $conf->{memory
} // $defaults->{memory
};
212 my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
213 # our savevm-start does live-save of the memory until the space left in the
214 # volume is just enough for the remaining memory content + internal state
215 # then it stops the vm and copies the rest so we reserve twice the
216 # memory content + state to minimize vm downtime
217 my $size = $mem_size*2 + $driver_state_size;
218 my $scfg = PVE
::Storage
::storage_config
($storecfg, $target);
220 my $name = "vm-$vmid-state-$snapname";
221 $name .= ".raw" if $scfg->{path
}; # add filename extension for file base storage
223 my $statefile = PVE
::Storage
::vdisk_alloc
($storecfg, $target, $vmid, 'raw', $name, $size*1024);
224 my $runningmachine = PVE
::QemuServer
::Machine
::get_current_qemu_machine
($vmid);
226 # get current QEMU -cpu argument to ensure consistency of custom CPU models
228 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
229 $runningcpu = PVE
::QemuServer
::CPUConfig
::get_cpu_from_running_vm
($pid);
233 $conf = $conf->{snapshots
}->{$snapname};
236 $conf->{vmstate
} = $statefile;
237 $conf->{runningmachine
} = $runningmachine;
238 $conf->{runningcpu
} = $runningcpu;
243 sub __snapshot_check_running
{
244 my ($class, $vmid) = @_;
245 return PVE
::QemuServer
::Helpers
::vm_running_locally
($vmid);
248 sub __snapshot_check_freeze_needed
{
249 my ($class, $vmid, $config, $save_vmstate) = @_;
251 my $running = $class->__snapshot_check_running($vmid);
252 if (!$save_vmstate) {
253 return ($running, $running && PVE
::QemuServer
::parse_guest_agent
($config)->{enabled
} && PVE
::QemuServer
::qga_check_running
($vmid));
255 return ($running, 0);
259 sub __snapshot_freeze
{
260 my ($class, $vmid, $unfreeze) = @_;
263 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
264 warn "guest-fsfreeze-thaw problems - $@" if $@;
266 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
267 warn "guest-fsfreeze-freeze problems - $@" if $@;
271 sub __snapshot_create_vol_snapshots_hook
{
272 my ($class, $vmid, $snap, $running, $hook) = @_;
275 my $storecfg = PVE
::Storage
::config
();
277 if ($hook eq "before") {
278 if ($snap->{vmstate
}) {
279 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
280 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
282 mon_cmd
($vmid, "savevm-start", statefile
=> $path);
284 my $stat = mon_cmd
($vmid, "query-savevm");
285 if (!$stat->{status
}) {
286 die "savevm not active\n";
287 } elsif ($stat->{status
} eq 'active') {
290 } elsif ($stat->{status
} eq 'completed') {
293 die "query-savevm returned status '$stat->{status}'\n";
297 mon_cmd
($vmid, "savevm-start");
299 } elsif ($hook eq "after") {
301 mon_cmd
($vmid, "savevm-end");
302 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
305 } elsif ($hook eq "after-freeze") {
306 # savevm-end is async, we need to wait
308 my $stat = mon_cmd
($vmid, "query-savevm");
309 if (!$stat->{bytes
}) {
312 print "savevm not yet finished\n";
321 sub __snapshot_create_vol_snapshot
{
322 my ($class, $vmid, $ds, $drive, $snapname) = @_;
324 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
326 my $volid = $drive->{file
};
327 my $device = "drive-$ds";
328 my $storecfg = PVE
::Storage
::config
();
330 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
333 sub __snapshot_delete_remove_drive
{
334 my ($class, $snap, $remove_drive) = @_;
336 if ($remove_drive eq 'vmstate') {
337 delete $snap->{$remove_drive};
339 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
340 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
342 my $volid = $drive->{file
};
343 delete $snap->{$remove_drive};
344 $class->add_unused_volume($snap, $volid);
348 sub __snapshot_delete_vmstate_file
{
349 my ($class, $snap, $force) = @_;
351 my $storecfg = PVE
::Storage
::config
();
353 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
360 sub __snapshot_delete_vol_snapshot
{
361 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
363 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
364 my $storecfg = PVE
::Storage
::config
();
365 my $volid = $drive->{file
};
366 my $device = "drive-$ds";
368 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
370 push @$unused, $volid;
373 sub __snapshot_rollback_hook
{
374 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
377 # we save the machine of the current config
378 $data->{oldmachine
} = $conf->{machine
};
380 # if we have a 'runningmachine' entry in the snapshot we use that
381 # for the forcemachine parameter, else we use the old logic
382 if (defined($conf->{runningmachine
})) {
383 $data->{forcemachine
} = $conf->{runningmachine
};
384 delete $conf->{runningmachine
};
386 # runningcpu is newer than runningmachine, so assume it only exists
388 $data->{forcecpu
} = delete $conf->{runningcpu
}
389 if defined($conf->{runningcpu
});
391 # Note: old code did not store 'machine', so we try to be smart
392 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
393 $data->{forcemachine
} = $conf->{machine
} || 'pc-i440fx-1.4';
395 # we remove the 'machine' configuration if not explicitly specified
396 # in the original config.
397 delete $conf->{machine
} if $snap->{vmstate
} && !defined($data->{oldmachine
});
400 if ($conf->{vmgenid
}) {
401 # tell the VM that it's another generation, so it can react
402 # appropriately, e.g. dirty-mark copies of distributed databases or
403 # re-initializing its random number generator
404 $conf->{vmgenid
} = PVE
::QemuServer
::generate_uuid
();
411 sub __snapshot_rollback_vol_possible
{
412 my ($class, $drive, $snapname) = @_;
414 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
416 my $storecfg = PVE
::Storage
::config
();
417 my $volid = $drive->{file
};
419 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
422 sub __snapshot_rollback_vol_rollback
{
423 my ($class, $drive, $snapname) = @_;
425 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
427 my $storecfg = PVE
::Storage
::config
();
428 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
431 sub __snapshot_rollback_vm_stop
{
432 my ($class, $vmid) = @_;
434 my $storecfg = PVE
::Storage
::config
();
435 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
438 sub __snapshot_rollback_vm_start
{
439 my ($class, $vmid, $vmstate, $data) = @_;
441 my $storecfg = PVE
::Storage
::config
();
443 statefile
=> $vmstate,
444 forcemachine
=> $data->{forcemachine
},
445 forcecpu
=> $data->{forcecpu
},
447 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $params);
450 sub __snapshot_rollback_get_unused
{
451 my ($class, $conf, $snap) = @_;
455 $class->foreach_volume($conf, sub {
456 my ($vs, $volume) = @_;
458 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
461 my $volid = $volume->{file
};
463 $class->foreach_volume($snap, sub {
464 my ($ds, $drive) = @_;
467 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
470 if ($drive->{file
} && $drive->{file
} eq $volid);
473 push @$unused, $volid if !$found;
479 # END implemented abstract methods from PVE::AbstractConfig