1 package PVE
::QemuConfig
;
6 use PVE
::AbstractConfig
;
8 use PVE
::QemuServer
::Helpers
;
9 use PVE
::QemuServer
::Monitor
qw(mon_cmd);
11 use PVE
::QemuServer
::Machine
;
15 use base
qw(PVE::AbstractConfig);
17 my $nodename = PVE
::INotify
::nodename
();
19 mkdir "/etc/pve/nodes/$nodename";
20 mkdir "/etc/pve/nodes/$nodename/qemu-server";
22 my $lock_dir = "/var/lock/qemu-server";
25 my $MAX_UNUSED_DISKS = 256;
27 sub assert_config_exists_on_node
{
28 my ($vmid, $node) = @_;
32 my $filename = __PACKAGE__-
>config_file($vmid, $node);
33 my $exists = -f
$filename;
35 my $type = guest_type
();
36 die "unable to find configuration file for $type $vmid on node '$node'\n"
40 # BEGIN implemented abstract methods from PVE::AbstractConfig
46 sub __config_max_unused_disks
{
49 return $MAX_UNUSED_DISKS;
52 sub config_file_lock
{
53 my ($class, $vmid) = @_;
55 return "$lock_dir/lock-$vmid.conf";
59 my ($class, $vmid, $node) = @_;
61 $node = $nodename if !$node;
62 return "nodes/$node/qemu-server/$vmid.conf";
66 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
69 PVE
::QemuServer
::foreach_drive
($conf, sub {
70 my ($ds, $drive) = @_;
72 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
73 return if $backup_only && defined($drive->{backup
}) && !$drive->{backup
};
74 my $volid = $drive->{file
};
75 $err = 1 if !PVE
::Storage
::volume_has_feature
($storecfg, $feature, $volid, $snapname, $running);
81 sub get_replicatable_volumes
{
82 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
86 my $test_volid = sub {
87 my ($volid, $attr) = @_;
89 return if $attr->{cdrom
};
91 return if !$cleanup && !$attr->{replicate
};
93 if ($volid =~ m
|^/|) {
94 return if !$attr->{replicate
};
95 return if $cleanup || $noerr;
96 die "unable to replicate local file/device '$volid'\n";
99 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, $noerr);
102 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
103 return if $scfg->{shared
};
105 my ($path, $owner, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
106 return if !$owner || ($owner != $vmid);
108 if ($vtype ne 'images') {
109 return if $cleanup || $noerr;
110 die "unable to replicate volume '$volid', type '$vtype'\n";
113 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
114 return if $cleanup || $noerr;
115 die "missing replicate feature on volume '$volid'\n";
118 $volhash->{$volid} = 1;
121 PVE
::QemuServer
::foreach_volid
($conf, $test_volid);
123 # add 'unusedX' volumes to volhash
124 foreach my $key (keys %$conf) {
125 if ($key =~ m/^unused/) {
126 $test_volid->($conf->{$key}, { replicate
=> 1 });
133 sub __snapshot_save_vmstate
{
134 my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
136 # use given storage or search for one from the config
137 my $target = $statestorage;
140 $target = PVE
::QemuServer
::find_vmstate_storage
($conf, $storecfg);
143 my $defaults = PVE
::QemuServer
::load_defaults
();
144 my $mem_size = $conf->{memory
} // $defaults->{memory
};
145 my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
146 # our savevm-start does live-save of the memory until the space left in the
147 # volume is just enough for the remaining memory content + internal state
148 # then it stops the vm and copies the rest so we reserve twice the
149 # memory content + state to minimize vm downtime
150 my $size = $mem_size*2 + $driver_state_size;
151 my $scfg = PVE
::Storage
::storage_config
($storecfg, $target);
153 my $name = "vm-$vmid-state-$snapname";
154 $name .= ".raw" if $scfg->{path
}; # add filename extension for file base storage
156 my $statefile = PVE
::Storage
::vdisk_alloc
($storecfg, $target, $vmid, 'raw', $name, $size*1024);
157 my $runningmachine = PVE
::QemuServer
::Machine
::get_current_qemu_machine
($vmid);
160 $conf->{vmstate
} = $statefile;
161 $conf->{runningmachine
} = $runningmachine;
163 my $snap = $conf->{snapshots
}->{$snapname};
164 $snap->{vmstate
} = $statefile;
165 $snap->{runningmachine
} = $runningmachine;
171 sub __snapshot_check_running
{
172 my ($class, $vmid) = @_;
173 return PVE
::QemuServer
::Helpers
::vm_running_locally
($vmid);
176 sub __snapshot_check_freeze_needed
{
177 my ($class, $vmid, $config, $save_vmstate) = @_;
179 my $running = $class->__snapshot_check_running($vmid);
180 if (!$save_vmstate) {
181 return ($running, $running && PVE
::QemuServer
::parse_guest_agent
($config)->{enabled
} && PVE
::QemuServer
::qga_check_running
($vmid));
183 return ($running, 0);
187 sub __snapshot_freeze
{
188 my ($class, $vmid, $unfreeze) = @_;
191 eval { mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
192 warn "guest-fsfreeze-thaw problems - $@" if $@;
194 eval { mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
195 warn "guest-fsfreeze-freeze problems - $@" if $@;
199 sub __snapshot_create_vol_snapshots_hook
{
200 my ($class, $vmid, $snap, $running, $hook) = @_;
203 my $storecfg = PVE
::Storage
::config
();
205 if ($hook eq "before") {
206 if ($snap->{vmstate
}) {
207 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
208 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
210 mon_cmd
($vmid, "savevm-start", statefile
=> $path);
212 my $stat = mon_cmd
($vmid, "query-savevm");
213 if (!$stat->{status
}) {
214 die "savevm not active\n";
215 } elsif ($stat->{status
} eq 'active') {
218 } elsif ($stat->{status
} eq 'completed') {
221 die "query-savevm returned status '$stat->{status}'\n";
225 mon_cmd
($vmid, "savevm-start");
227 } elsif ($hook eq "after") {
229 mon_cmd
($vmid, "savevm-end");
230 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
233 } elsif ($hook eq "after-freeze") {
234 # savevm-end is async, we need to wait
236 my $stat = mon_cmd
($vmid, "query-savevm");
237 if (!$stat->{bytes
}) {
240 print "savevm not yet finished\n";
249 sub __snapshot_create_vol_snapshot
{
250 my ($class, $vmid, $ds, $drive, $snapname) = @_;
252 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
254 my $volid = $drive->{file
};
255 my $device = "drive-$ds";
256 my $storecfg = PVE
::Storage
::config
();
258 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
261 sub __snapshot_delete_remove_drive
{
262 my ($class, $snap, $remove_drive) = @_;
264 if ($remove_drive eq 'vmstate') {
265 delete $snap->{$remove_drive};
267 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
268 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
270 my $volid = $drive->{file
};
271 delete $snap->{$remove_drive};
272 $class->add_unused_volume($snap, $volid);
276 sub __snapshot_delete_vmstate_file
{
277 my ($class, $snap, $force) = @_;
279 my $storecfg = PVE
::Storage
::config
();
281 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
288 sub __snapshot_delete_vol_snapshot
{
289 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
291 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
292 my $storecfg = PVE
::Storage
::config
();
293 my $volid = $drive->{file
};
294 my $device = "drive-$ds";
296 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
298 push @$unused, $volid;
301 sub __snapshot_rollback_hook
{
302 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
305 # we save the machine of the current config
306 $data->{oldmachine
} = $conf->{machine
};
308 # if we have a 'runningmachine' entry in the snapshot we use that
309 # for the forcemachine parameter, else we use the old logic
310 if (defined($conf->{runningmachine
})) {
311 $data->{forcemachine
} = $conf->{runningmachine
};
312 delete $conf->{runningmachine
};
314 # Note: old code did not store 'machine', so we try to be smart
315 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
316 $data->{forcemachine
} = $conf->{machine
} || 'pc-i440fx-1.4';
318 # we remove the 'machine' configuration if not explicitly specified
319 # in the original config.
320 delete $conf->{machine
} if $snap->{vmstate
} && !defined($data->{oldmachine
});
323 if ($conf->{vmgenid
}) {
324 # tell the VM that it's another generation, so it can react
325 # appropriately, e.g. dirty-mark copies of distributed databases or
326 # re-initializing its random number generator
327 $conf->{vmgenid
} = PVE
::QemuServer
::generate_uuid
();
334 sub __snapshot_rollback_vol_possible
{
335 my ($class, $drive, $snapname) = @_;
337 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
339 my $storecfg = PVE
::Storage
::config
();
340 my $volid = $drive->{file
};
342 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
345 sub __snapshot_rollback_vol_rollback
{
346 my ($class, $drive, $snapname) = @_;
348 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
350 my $storecfg = PVE
::Storage
::config
();
351 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
354 sub __snapshot_rollback_vm_stop
{
355 my ($class, $vmid) = @_;
357 my $storecfg = PVE
::Storage
::config
();
358 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
361 sub __snapshot_rollback_vm_start
{
362 my ($class, $vmid, $vmstate, $data) = @_;
364 my $storecfg = PVE
::Storage
::config
();
365 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $vmstate, undef, undef, undef, $data->{forcemachine
});
368 sub __snapshot_rollback_get_unused
{
369 my ($class, $conf, $snap) = @_;
373 $class->__snapshot_foreach_volume($conf, sub {
374 my ($vs, $volume) = @_;
376 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
379 my $volid = $volume->{file
};
381 $class->__snapshot_foreach_volume($snap, sub {
382 my ($ds, $drive) = @_;
385 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
388 if ($drive->{file
} && $drive->{file
} eq $volid);
391 push @$unused, $volid if !$found;
397 sub __snapshot_foreach_volume
{
398 my ($class, $conf, $func) = @_;
400 PVE
::QemuServer
::foreach_drive
($conf, $func);
402 # END implemented abstract methods from PVE::AbstractConfig