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);
165 # add 'unusedX' volumes to volhash
166 foreach my $key (keys %$conf) {
167 if ($key =~ m/^unused/) {
168 $test_volid->($conf->{$key}, { replicate
=> 1 });
175 sub __snapshot_save_vmstate
{
176 my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
178 # use given storage or search for one from the config
179 my $target = $statestorage;
182 $target = PVE
::QemuServer
::find_vmstate_storage
($conf, $storecfg);
185 my $defaults = PVE
::QemuServer
::load_defaults
();
186 my $mem_size = $conf->{memory
} // $defaults->{memory
};
187 my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
188 # our savevm-start does live-save of the memory until the space left in the
189 # volume is just enough for the remaining memory content + internal state
190 # then it stops the vm and copies the rest so we reserve twice the
191 # memory content + state to minimize vm downtime
192 my $size = $mem_size*2 + $driver_state_size;
193 my $scfg = PVE
::Storage
::storage_config
($storecfg, $target);
195 my $name = "vm-$vmid-state-$snapname";
196 $name .= ".raw" if $scfg->{path
}; # add filename extension for file base storage
198 my $statefile = PVE
::Storage
::vdisk_alloc
($storecfg, $target, $vmid, 'raw', $name, $size*1024);
199 my $runningmachine = PVE
::QemuServer
::Machine
::get_current_qemu_machine
($vmid);
201 # get current QEMU -cpu argument to ensure consistency of custom CPU models
203 if (my $pid = PVE
::QemuServer
::check_running
($vmid)) {
204 $runningcpu = PVE
::QemuServer
::CPUConfig
::get_cpu_from_running_vm
($pid);
208 $conf = $conf->{snapshots
}->{$snapname};
211 $conf->{vmstate
} = $statefile;
212 $conf->{runningmachine
} = $runningmachine;
213 $conf->{runningcpu
} = $runningcpu;
218 sub __snapshot_check_running
{
219 my ($class, $vmid) = @_;
220 return PVE
::QemuServer
::Helpers
::vm_running_locally
($vmid);
223 sub __snapshot_check_freeze_needed
{
224 my ($class, $vmid, $config, $save_vmstate) = @_;
226 my $running = $class->__snapshot_check_running($vmid);
227 if (!$save_vmstate) {
228 return ($running, $running && PVE
::QemuServer
::parse_guest_agent
($config)->{enabled
} && PVE
::QemuServer
::qga_check_running
($vmid));
230 return ($running, 0);
234 sub __snapshot_freeze
{
235 my ($class, $vmid, $unfreeze) = @_;
238 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
239 warn "guest-fsfreeze-thaw problems - $@" if $@;
241 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
242 warn "guest-fsfreeze-freeze problems - $@" if $@;
246 sub __snapshot_create_vol_snapshots_hook
{
247 my ($class, $vmid, $snap, $running, $hook) = @_;
250 my $storecfg = PVE
::Storage
::config
();
252 if ($hook eq "before") {
253 if ($snap->{vmstate
}) {
254 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
255 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
257 mon_cmd
($vmid, "savevm-start", statefile
=> $path);
259 my $stat = mon_cmd
($vmid, "query-savevm");
260 if (!$stat->{status
}) {
261 die "savevm not active\n";
262 } elsif ($stat->{status
} eq 'active') {
265 } elsif ($stat->{status
} eq 'completed') {
268 die "query-savevm returned status '$stat->{status}'\n";
272 mon_cmd
($vmid, "savevm-start");
274 } elsif ($hook eq "after") {
276 mon_cmd
($vmid, "savevm-end");
277 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
280 } elsif ($hook eq "after-freeze") {
281 # savevm-end is async, we need to wait
283 my $stat = mon_cmd
($vmid, "query-savevm");
284 if (!$stat->{bytes
}) {
287 print "savevm not yet finished\n";
296 sub __snapshot_create_vol_snapshot
{
297 my ($class, $vmid, $ds, $drive, $snapname) = @_;
299 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
301 my $volid = $drive->{file
};
302 my $device = "drive-$ds";
303 my $storecfg = PVE
::Storage
::config
();
305 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
308 sub __snapshot_delete_remove_drive
{
309 my ($class, $snap, $remove_drive) = @_;
311 if ($remove_drive eq 'vmstate') {
312 delete $snap->{$remove_drive};
314 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
315 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
317 my $volid = $drive->{file
};
318 delete $snap->{$remove_drive};
319 $class->add_unused_volume($snap, $volid);
323 sub __snapshot_delete_vmstate_file
{
324 my ($class, $snap, $force) = @_;
326 my $storecfg = PVE
::Storage
::config
();
328 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
335 sub __snapshot_delete_vol_snapshot
{
336 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
338 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
339 my $storecfg = PVE
::Storage
::config
();
340 my $volid = $drive->{file
};
341 my $device = "drive-$ds";
343 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
345 push @$unused, $volid;
348 sub __snapshot_rollback_hook
{
349 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
352 # we save the machine of the current config
353 $data->{oldmachine
} = $conf->{machine
};
355 # if we have a 'runningmachine' entry in the snapshot we use that
356 # for the forcemachine parameter, else we use the old logic
357 if (defined($conf->{runningmachine
})) {
358 $data->{forcemachine
} = $conf->{runningmachine
};
359 delete $conf->{runningmachine
};
361 # runningcpu is newer than runningmachine, so assume it only exists
363 $data->{forcecpu
} = delete $conf->{runningcpu
}
364 if defined($conf->{runningcpu
});
366 # Note: old code did not store 'machine', so we try to be smart
367 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
368 $data->{forcemachine
} = $conf->{machine
} || 'pc-i440fx-1.4';
370 # we remove the 'machine' configuration if not explicitly specified
371 # in the original config.
372 delete $conf->{machine
} if $snap->{vmstate
} && !defined($data->{oldmachine
});
375 if ($conf->{vmgenid
}) {
376 # tell the VM that it's another generation, so it can react
377 # appropriately, e.g. dirty-mark copies of distributed databases or
378 # re-initializing its random number generator
379 $conf->{vmgenid
} = PVE
::QemuServer
::generate_uuid
();
386 sub __snapshot_rollback_vol_possible
{
387 my ($class, $drive, $snapname) = @_;
389 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
391 my $storecfg = PVE
::Storage
::config
();
392 my $volid = $drive->{file
};
394 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
397 sub __snapshot_rollback_vol_rollback
{
398 my ($class, $drive, $snapname) = @_;
400 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
402 my $storecfg = PVE
::Storage
::config
();
403 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
406 sub __snapshot_rollback_vm_stop
{
407 my ($class, $vmid) = @_;
409 my $storecfg = PVE
::Storage
::config
();
410 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
413 sub __snapshot_rollback_vm_start
{
414 my ($class, $vmid, $vmstate, $data) = @_;
416 my $storecfg = PVE
::Storage
::config
();
418 statefile
=> $vmstate,
419 forcemachine
=> $data->{forcemachine
},
420 forcecpu
=> $data->{forcecpu
},
422 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $params);
425 sub __snapshot_rollback_get_unused
{
426 my ($class, $conf, $snap) = @_;
430 $class->foreach_volume($conf, sub {
431 my ($vs, $volume) = @_;
433 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
436 my $volid = $volume->{file
};
438 $class->foreach_volume($snap, sub {
439 my ($ds, $drive) = @_;
442 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
445 if ($drive->{file
} && $drive->{file
} eq $volid);
448 push @$unused, $volid if !$found;
454 # END implemented abstract methods from PVE::AbstractConfig