]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuConfig.pm
implement suspend to disk for running vms
[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::QemuServer;
9 use PVE::Storage;
10 use PVE::Tools;
11
12 use base qw(PVE::AbstractConfig);
13
14 my $nodename = PVE::INotify::nodename();
15
16 mkdir "/etc/pve/nodes/$nodename";
17 my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
18 mkdir $confdir;
19
20 my $lock_dir = "/var/lock/qemu-server";
21 mkdir $lock_dir;
22
23 my $MAX_UNUSED_DISKS = 256;
24
25 # BEGIN implemented abstract methods from PVE::AbstractConfig
26
27 sub guest_type {
28 return "VM";
29 }
30
31 sub __config_max_unused_disks {
32 my ($class) = @_;
33
34 return $MAX_UNUSED_DISKS;
35 }
36
37 sub config_file_lock {
38 my ($class, $vmid) = @_;
39
40 return "$lock_dir/lock-$vmid.conf";
41 }
42
43 sub cfs_config_path {
44 my ($class, $vmid, $node) = @_;
45
46 $node = $nodename if !$node;
47 return "nodes/$node/qemu-server/$vmid.conf";
48 }
49
50 sub has_feature {
51 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
52
53 my $err;
54 PVE::QemuServer::foreach_drive($conf, sub {
55 my ($ds, $drive) = @_;
56
57 return if PVE::QemuServer::drive_is_cdrom($drive);
58 return if $backup_only && defined($drive->{backup}) && !$drive->{backup};
59 my $volid = $drive->{file};
60 $err = 1 if !PVE::Storage::volume_has_feature($storecfg, $feature, $volid, $snapname, $running);
61 });
62
63 return $err ? 0 : 1;
64 }
65
66 sub get_replicatable_volumes {
67 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
68
69 my $volhash = {};
70
71 my $test_volid = sub {
72 my ($volid, $attr) = @_;
73
74 return if $attr->{cdrom};
75
76 return if !$cleanup && !$attr->{replicate};
77
78 if ($volid =~ m|^/|) {
79 return if !$attr->{replicate};
80 return if $cleanup || $noerr;
81 die "unable to replicate local file/device '$volid'\n";
82 }
83
84 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, $noerr);
85 return if !$storeid;
86
87 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
88 return if $scfg->{shared};
89
90 my ($path, $owner, $vtype) = PVE::Storage::path($storecfg, $volid);
91 return if !$owner || ($owner != $vmid);
92
93 if ($vtype ne 'images') {
94 return if $cleanup || $noerr;
95 die "unable to replicate volume '$volid', type '$vtype'\n";
96 }
97
98 if (!PVE::Storage::volume_has_feature($storecfg, 'replicate', $volid)) {
99 return if $cleanup || $noerr;
100 die "missing replicate feature on volume '$volid'\n";
101 }
102
103 $volhash->{$volid} = 1;
104 };
105
106 PVE::QemuServer::foreach_volid($conf, $test_volid);
107
108 # add 'unusedX' volumes to volhash
109 foreach my $key (keys %$conf) {
110 if ($key =~ m/^unused/) {
111 $test_volid->($conf->{$key}, { replicate => 1 });
112 }
113 }
114
115 return $volhash;
116 }
117
118 sub __snapshot_save_vmstate {
119 my ($class, $vmid, $conf, $snapname, $storecfg, $suspend) = @_;
120
121 # first, use explicitly configured storage
122 my $target = $conf->{vmstatestorage};
123
124 if (!$target) {
125 my ($shared, $local);
126 PVE::QemuServer::foreach_storage_used_by_vm($conf, sub {
127 my ($sid) = @_;
128 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
129 my $dst = $scfg->{shared} ? \$shared : \$local;
130 $$dst = $sid if !$$dst || $scfg->{path}; # prefer file based storage
131 });
132
133 # second, use shared storage where VM has at least one disk
134 # third, use local storage where VM has at least one disk
135 # fall back to local storage
136 $target = $shared // $local // 'local';
137 }
138
139 my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
140 my $size = $conf->{memory}*2 + $driver_state_size;
141 my $scfg = PVE::Storage::storage_config($storecfg, $target);
142
143 my $name = "vm-$vmid-state-$snapname";
144 $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage
145
146 my $statefile = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
147 my $runningmachine = PVE::QemuServer::get_current_qemu_machine($vmid);
148
149 if ($suspend) {
150 $conf->{vmstate} = $statefile;
151 $conf->{runningmachine} = $runningmachine;
152 } else {
153 my $snap = $conf->{snapshots}->{$snapname};
154 $snap->{vmstate} = $statefile;
155 $snap->{runningmachine} = $runningmachine;
156 }
157
158 return $statefile;
159 }
160
161 sub __snapshot_check_running {
162 my ($class, $vmid) = @_;
163 return PVE::QemuServer::check_running($vmid);
164 }
165
166 sub __snapshot_check_freeze_needed {
167 my ($class, $vmid, $config, $save_vmstate) = @_;
168
169 my $running = $class->__snapshot_check_running($vmid);
170 if (!$save_vmstate) {
171 return ($running, $running && PVE::QemuServer::parse_guest_agent($config)->{enabled} && PVE::QemuServer::qga_check_running($vmid));
172 } else {
173 return ($running, 0);
174 }
175 }
176
177 sub __snapshot_freeze {
178 my ($class, $vmid, $unfreeze) = @_;
179
180 if ($unfreeze) {
181 eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-thaw"); };
182 warn "guest-fsfreeze-thaw problems - $@" if $@;
183 } else {
184 eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-freeze"); };
185 warn "guest-fsfreeze-freeze problems - $@" if $@;
186 }
187 }
188
189 sub __snapshot_create_vol_snapshots_hook {
190 my ($class, $vmid, $snap, $running, $hook) = @_;
191
192 if ($running) {
193 my $storecfg = PVE::Storage::config();
194
195 if ($hook eq "before") {
196 if ($snap->{vmstate}) {
197 my $path = PVE::Storage::path($storecfg, $snap->{vmstate});
198 PVE::Storage::activate_volumes($storecfg, [$snap->{vmstate}]);
199
200 PVE::QemuServer::vm_mon_cmd($vmid, "savevm-start", statefile => $path);
201 for(;;) {
202 my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm");
203 if (!$stat->{status}) {
204 die "savevm not active\n";
205 } elsif ($stat->{status} eq 'active') {
206 sleep(1);
207 next;
208 } elsif ($stat->{status} eq 'completed') {
209 last;
210 } else {
211 die "query-savevm returned status '$stat->{status}'\n";
212 }
213 }
214 } else {
215 PVE::QemuServer::vm_mon_cmd($vmid, "savevm-start");
216 }
217 } elsif ($hook eq "after") {
218 eval {
219 PVE::QemuServer::vm_mon_cmd($vmid, "savevm-end");
220 PVE::Storage::deactivate_volumes($storecfg, [$snap->{vmstate}]) if $snap->{vmstate};
221 };
222 warn $@ if $@;
223 } elsif ($hook eq "after-freeze") {
224 # savevm-end is async, we need to wait
225 for (;;) {
226 my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm");
227 if (!$stat->{bytes}) {
228 last;
229 } else {
230 print "savevm not yet finished\n";
231 sleep(1);
232 next;
233 }
234 }
235 }
236 }
237 }
238
239 sub __snapshot_create_vol_snapshot {
240 my ($class, $vmid, $ds, $drive, $snapname) = @_;
241
242 return if PVE::QemuServer::drive_is_cdrom($drive);
243
244 my $volid = $drive->{file};
245 my $device = "drive-$ds";
246 my $storecfg = PVE::Storage::config();
247
248 PVE::QemuServer::qemu_volume_snapshot($vmid, $device, $storecfg, $volid, $snapname);
249 }
250
251 sub __snapshot_delete_remove_drive {
252 my ($class, $snap, $remove_drive) = @_;
253
254 if ($remove_drive eq 'vmstate') {
255 delete $snap->{$remove_drive};
256 } else {
257 my $drive = PVE::QemuServer::parse_drive($remove_drive, $snap->{$remove_drive});
258 return if PVE::QemuServer::drive_is_cdrom($drive);
259
260 my $volid = $drive->{file};
261 delete $snap->{$remove_drive};
262 $class->add_unused_volume($snap, $volid);
263 }
264 }
265
266 sub __snapshot_delete_vmstate_file {
267 my ($class, $snap, $force) = @_;
268
269 my $storecfg = PVE::Storage::config();
270
271 eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
272 if (my $err = $@) {
273 die $err if !$force;
274 warn $err;
275 }
276 }
277
278 sub __snapshot_delete_vol_snapshot {
279 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
280
281 return if PVE::QemuServer::drive_is_cdrom($drive);
282 my $storecfg = PVE::Storage::config();
283 my $volid = $drive->{file};
284 my $device = "drive-$ds";
285
286 PVE::QemuServer::qemu_volume_snapshot_delete($vmid, $device, $storecfg, $volid, $snapname);
287
288 push @$unused, $volid;
289 }
290
291 sub __snapshot_rollback_hook {
292 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
293
294 if ($prepare) {
295 # we save the machine of the current config
296 $data->{oldmachine} = $conf->{machine};
297 } else {
298 # if we have a 'runningmachine' entry in the snapshot we use that
299 # for the forcemachine parameter, else we use the old logic
300 if (defined($conf->{runningmachine})) {
301 $data->{forcemachine} = $conf->{runningmachine};
302 delete $conf->{runningmachine};
303 } else {
304 # Note: old code did not store 'machine', so we try to be smart
305 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
306 $data->{forcemachine} = $conf->{machine} || 'pc-i440fx-1.4';
307
308 # we remove the 'machine' configuration if not explicitly specified
309 # in the original config.
310 delete $conf->{machine} if $snap->{vmstate} && !defined($data->{oldmachine});
311 }
312
313 if ($conf->{vmgenid}) {
314 # tell the VM that it's another generation, so it can react
315 # appropriately, e.g. dirty-mark copies of distributed databases or
316 # re-initializing its random number generator
317 $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
318 }
319 }
320
321 return;
322 }
323
324 sub __snapshot_rollback_vol_possible {
325 my ($class, $drive, $snapname) = @_;
326
327 return if PVE::QemuServer::drive_is_cdrom($drive);
328
329 my $storecfg = PVE::Storage::config();
330 my $volid = $drive->{file};
331
332 PVE::Storage::volume_rollback_is_possible($storecfg, $volid, $snapname);
333 }
334
335 sub __snapshot_rollback_vol_rollback {
336 my ($class, $drive, $snapname) = @_;
337
338 return if PVE::QemuServer::drive_is_cdrom($drive);
339
340 my $storecfg = PVE::Storage::config();
341 PVE::Storage::volume_snapshot_rollback($storecfg, $drive->{file}, $snapname);
342 }
343
344 sub __snapshot_rollback_vm_stop {
345 my ($class, $vmid) = @_;
346
347 my $storecfg = PVE::Storage::config();
348 PVE::QemuServer::vm_stop($storecfg, $vmid, undef, undef, 5, undef, undef);
349 }
350
351 sub __snapshot_rollback_vm_start {
352 my ($class, $vmid, $vmstate, $data) = @_;
353
354 my $storecfg = PVE::Storage::config();
355 my $statefile = PVE::Storage::path($storecfg, $vmstate);
356 PVE::QemuServer::vm_start($storecfg, $vmid, $statefile, undef, undef, undef, $data->{forcemachine});
357 }
358
359 sub __snapshot_rollback_get_unused {
360 my ($class, $conf, $snap) = @_;
361
362 my $unused = [];
363
364 $class->__snapshot_foreach_volume($conf, sub {
365 my ($vs, $volume) = @_;
366
367 return if PVE::QemuServer::drive_is_cdrom($volume);
368
369 my $found = 0;
370 my $volid = $volume->{file};
371
372 $class->__snapshot_foreach_volume($snap, sub {
373 my ($ds, $drive) = @_;
374
375 return if $found;
376 return if PVE::QemuServer::drive_is_cdrom($drive);
377
378 $found = 1
379 if ($drive->{file} && $drive->{file} eq $volid);
380 });
381
382 push @$unused, $volid if !$found;
383 });
384
385 return $unused;
386 }
387
388 sub __snapshot_foreach_volume {
389 my ($class, $conf, $func) = @_;
390
391 PVE::QemuServer::foreach_drive($conf, $func);
392 }
393 # END implemented abstract methods from PVE::AbstractConfig
394
395 1;