]> git.proxmox.com Git - qemu-server.git/blame - PVE/QemuConfig.pm
refactor finding of vmstate storage
[qemu-server.git] / PVE / QemuConfig.pm
CommitLineData
ffda963f
FG
1package PVE::QemuConfig;
2
3use strict;
4use warnings;
5
b2c9558d
FG
6use PVE::AbstractConfig;
7use PVE::INotify;
d036e418 8use PVE::QemuServer::Helpers;
0a13e08e 9use PVE::QemuServer::Monitor qw(mon_cmd);
b2c9558d 10use PVE::QemuServer;
3392d6ca 11use PVE::QemuServer::Machine;
b2c9558d
FG
12use PVE::Storage;
13use PVE::Tools;
14
ffda963f
FG
15use base qw(PVE::AbstractConfig);
16
17my $nodename = PVE::INotify::nodename();
18
19mkdir "/etc/pve/nodes/$nodename";
d036e418 20mkdir "/etc/pve/nodes/$nodename/qemu-server";
ffda963f
FG
21
22my $lock_dir = "/var/lock/qemu-server";
23mkdir $lock_dir;
24
c9db2240 25my $MAX_UNUSED_DISKS = 256;
ffda963f 26
babf613a
SR
27sub assert_config_exists_on_node {
28 my ($vmid, $node) = @_;
29
30 $node //= $nodename;
31
32 my $filename = __PACKAGE__->config_file($vmid, $node);
33 my $exists = -f $filename;
34
35 my $type = guest_type();
36 die "unable to find configuration file for $type $vmid on node '$node'\n"
37 if !$exists;
38}
39
ffda963f
FG
40# BEGIN implemented abstract methods from PVE::AbstractConfig
41
42sub guest_type {
43 return "VM";
44}
45
46sub __config_max_unused_disks {
3aa44d3b 47 my ($class) = @_;
ffda963f
FG
48
49 return $MAX_UNUSED_DISKS;
50}
51
52sub config_file_lock {
53 my ($class, $vmid) = @_;
54
55 return "$lock_dir/lock-$vmid.conf";
56}
57
58sub cfs_config_path {
59 my ($class, $vmid, $node) = @_;
60
61 $node = $nodename if !$node;
62 return "nodes/$node/qemu-server/$vmid.conf";
63}
64
b2c9558d
FG
65sub has_feature {
66 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
67
68 my $err;
69 PVE::QemuServer::foreach_drive($conf, sub {
70 my ($ds, $drive) = @_;
71
72 return if PVE::QemuServer::drive_is_cdrom($drive);
5282865b 73 return if $backup_only && defined($drive->{backup}) && !$drive->{backup};
b2c9558d
FG
74 my $volid = $drive->{file};
75 $err = 1 if !PVE::Storage::volume_has_feature($storecfg, $feature, $volid, $snapname, $running);
76 });
77
78 return $err ? 0 : 1;
79}
80
3aa44d3b 81sub get_replicatable_volumes {
c78f43b9 82 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
3aa44d3b
DM
83
84 my $volhash = {};
85
86 my $test_volid = sub {
f949eb77 87 my ($volid, $attr) = @_;
3aa44d3b 88
a6cb40f7
DM
89 return if $attr->{cdrom};
90
91 return if !$cleanup && !$attr->{replicate};
92
4ab3bcc8
DM
93 if ($volid =~ m|^/|) {
94 return if !$attr->{replicate};
a6cb40f7 95 return if $cleanup || $noerr;
4ab3bcc8
DM
96 die "unable to replicate local file/device '$volid'\n";
97 }
98
a722d4ff
DM
99 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, $noerr);
100 return if !$storeid;
101
6f249d94 102 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
a722d4ff
DM
103 return if $scfg->{shared};
104
f7e7767f
DM
105 my ($path, $owner, $vtype) = PVE::Storage::path($storecfg, $volid);
106 return if !$owner || ($owner != $vmid);
107
a6cb40f7
DM
108 if ($vtype ne 'images') {
109 return if $cleanup || $noerr;
110 die "unable to replicate volume '$volid', type '$vtype'\n";
111 }
3aa44d3b
DM
112
113 if (!PVE::Storage::volume_has_feature($storecfg, 'replicate', $volid)) {
e5857ca8 114 return if $cleanup || $noerr;
3aa44d3b
DM
115 die "missing replicate feature on volume '$volid'\n";
116 }
117
118 $volhash->{$volid} = 1;
119 };
120
f949eb77 121 PVE::QemuServer::foreach_volid($conf, $test_volid);
3aa44d3b 122
8299257e
DM
123 # add 'unusedX' volumes to volhash
124 foreach my $key (keys %$conf) {
125 if ($key =~ m/^unused/) {
126 $test_volid->($conf->{$key}, { replicate => 1 });
127 }
128 }
129
3aa44d3b
DM
130 return $volhash;
131}
132
b2c9558d 133sub __snapshot_save_vmstate {
48b4cdc2 134 my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
b2c9558d 135
66cebc46
DC
136 # use given storage or search for one from the config
137 my $target = $statestorage;
b2c9558d
FG
138
139 if (!$target) {
66cebc46 140 $target = PVE::QemuServer::find_vmstate_storage($conf, $storecfg);
2eeb0c93 141 }
b2c9558d 142
22ea69ca
AA
143 my $defaults = PVE::QemuServer::load_defaults();
144 my $mem_size = $conf->{memory} // $defaults->{memory};
566caaa4 145 my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
b3983fa1
DC
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
22ea69ca 150 my $size = $mem_size*2 + $driver_state_size;
159719e5 151 my $scfg = PVE::Storage::storage_config($storecfg, $target);
b2c9558d
FG
152
153 my $name = "vm-$vmid-state-$snapname";
b2c9558d 154 $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage
159719e5
DC
155
156 my $statefile = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
3392d6ca 157 my $runningmachine = PVE::QemuServer::Machine::get_current_qemu_machine($vmid);
159719e5
DC
158
159 if ($suspend) {
160 $conf->{vmstate} = $statefile;
161 $conf->{runningmachine} = $runningmachine;
162 } else {
163 my $snap = $conf->{snapshots}->{$snapname};
164 $snap->{vmstate} = $statefile;
165 $snap->{runningmachine} = $runningmachine;
166 }
167
168 return $statefile;
b2c9558d
FG
169}
170
171sub __snapshot_check_running {
172 my ($class, $vmid) = @_;
babf613a 173 return PVE::QemuServer::Helpers::vm_running_locally($vmid);
b2c9558d
FG
174}
175
176sub __snapshot_check_freeze_needed {
177 my ($class, $vmid, $config, $save_vmstate) = @_;
178
179 my $running = $class->__snapshot_check_running($vmid);
278e2c9d 180 if (!$save_vmstate) {
9d66b397 181 return ($running, $running && PVE::QemuServer::parse_guest_agent($config)->{enabled} && PVE::QemuServer::qga_check_running($vmid));
b2c9558d
FG
182 } else {
183 return ($running, 0);
184 }
185}
186
187sub __snapshot_freeze {
188 my ($class, $vmid, $unfreeze) = @_;
189
190 if ($unfreeze) {
0a13e08e 191 eval { mon_cmd($vmid, "guest-fsfreeze-thaw"); };
b2c9558d
FG
192 warn "guest-fsfreeze-thaw problems - $@" if $@;
193 } else {
0a13e08e 194 eval { mon_cmd($vmid, "guest-fsfreeze-freeze"); };
b2c9558d
FG
195 warn "guest-fsfreeze-freeze problems - $@" if $@;
196 }
197}
198
199sub __snapshot_create_vol_snapshots_hook {
200 my ($class, $vmid, $snap, $running, $hook) = @_;
201
202 if ($running) {
3a8deb55
AD
203 my $storecfg = PVE::Storage::config();
204
b2c9558d
FG
205 if ($hook eq "before") {
206 if ($snap->{vmstate}) {
b2c9558d 207 my $path = PVE::Storage::path($storecfg, $snap->{vmstate});
3a8deb55
AD
208 PVE::Storage::activate_volumes($storecfg, [$snap->{vmstate}]);
209
0a13e08e 210 mon_cmd($vmid, "savevm-start", statefile => $path);
b2c9558d 211 for(;;) {
0a13e08e 212 my $stat = mon_cmd($vmid, "query-savevm");
b2c9558d
FG
213 if (!$stat->{status}) {
214 die "savevm not active\n";
215 } elsif ($stat->{status} eq 'active') {
216 sleep(1);
217 next;
218 } elsif ($stat->{status} eq 'completed') {
219 last;
220 } else {
221 die "query-savevm returned status '$stat->{status}'\n";
222 }
223 }
224 } else {
0a13e08e 225 mon_cmd($vmid, "savevm-start");
b2c9558d
FG
226 }
227 } elsif ($hook eq "after") {
a022e3fd 228 eval {
0a13e08e 229 mon_cmd($vmid, "savevm-end");
3a8deb55
AD
230 PVE::Storage::deactivate_volumes($storecfg, [$snap->{vmstate}]) if $snap->{vmstate};
231 };
b2c9558d
FG
232 warn $@ if $@;
233 } elsif ($hook eq "after-freeze") {
234 # savevm-end is async, we need to wait
235 for (;;) {
0a13e08e 236 my $stat = mon_cmd($vmid, "query-savevm");
b2c9558d
FG
237 if (!$stat->{bytes}) {
238 last;
239 } else {
240 print "savevm not yet finished\n";
241 sleep(1);
242 next;
243 }
244 }
245 }
246 }
247}
248
249sub __snapshot_create_vol_snapshot {
250 my ($class, $vmid, $ds, $drive, $snapname) = @_;
251
252 return if PVE::QemuServer::drive_is_cdrom($drive);
253
254 my $volid = $drive->{file};
255 my $device = "drive-$ds";
256 my $storecfg = PVE::Storage::config();
257
258 PVE::QemuServer::qemu_volume_snapshot($vmid, $device, $storecfg, $volid, $snapname);
259}
260
261sub __snapshot_delete_remove_drive {
262 my ($class, $snap, $remove_drive) = @_;
263
264 if ($remove_drive eq 'vmstate') {
265 delete $snap->{$remove_drive};
266 } else {
267 my $drive = PVE::QemuServer::parse_drive($remove_drive, $snap->{$remove_drive});
268 return if PVE::QemuServer::drive_is_cdrom($drive);
269
270 my $volid = $drive->{file};
271 delete $snap->{$remove_drive};
272 $class->add_unused_volume($snap, $volid);
273 }
274}
275
276sub __snapshot_delete_vmstate_file {
277 my ($class, $snap, $force) = @_;
278
279 my $storecfg = PVE::Storage::config();
280
281 eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
282 if (my $err = $@) {
283 die $err if !$force;
284 warn $err;
285 }
286}
287
288sub __snapshot_delete_vol_snapshot {
289 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
290
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";
295
296 PVE::QemuServer::qemu_volume_snapshot_delete($vmid, $device, $storecfg, $volid, $snapname);
297
298 push @$unused, $volid;
299}
300
58b1a8d7
DC
301sub __snapshot_rollback_hook {
302 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
303
304 if ($prepare) {
305 # we save the machine of the current config
306 $data->{oldmachine} = $conf->{machine};
307 } else {
e6d35c71
TL
308 # if we have a 'runningmachine' entry in the snapshot we use that
309 # for the forcemachine parameter, else we use the old logic
c6737ef1
DC
310 if (defined($conf->{runningmachine})) {
311 $data->{forcemachine} = $conf->{runningmachine};
312 delete $conf->{runningmachine};
313 } else {
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';
317
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});
321 }
6ee499ff
DC
322
323 if ($conf->{vmgenid}) {
4f4d9772
TL
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
6ee499ff
DC
327 $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
328 }
58b1a8d7
DC
329 }
330
331 return;
332}
333
b2c9558d
FG
334sub __snapshot_rollback_vol_possible {
335 my ($class, $drive, $snapname) = @_;
336
337 return if PVE::QemuServer::drive_is_cdrom($drive);
338
339 my $storecfg = PVE::Storage::config();
340 my $volid = $drive->{file};
341
342 PVE::Storage::volume_rollback_is_possible($storecfg, $volid, $snapname);
343}
344
345sub __snapshot_rollback_vol_rollback {
346 my ($class, $drive, $snapname) = @_;
347
348 return if PVE::QemuServer::drive_is_cdrom($drive);
349
350 my $storecfg = PVE::Storage::config();
351 PVE::Storage::volume_snapshot_rollback($storecfg, $drive->{file}, $snapname);
352}
353
354sub __snapshot_rollback_vm_stop {
355 my ($class, $vmid) = @_;
356
357 my $storecfg = PVE::Storage::config();
358 PVE::QemuServer::vm_stop($storecfg, $vmid, undef, undef, 5, undef, undef);
359}
360
361sub __snapshot_rollback_vm_start {
58b1a8d7 362 my ($class, $vmid, $vmstate, $data) = @_;
b2c9558d
FG
363
364 my $storecfg = PVE::Storage::config();
5c1d42b7 365 PVE::QemuServer::vm_start($storecfg, $vmid, $vmstate, undef, undef, undef, $data->{forcemachine});
b2c9558d
FG
366}
367
c4a54ed5
FG
368sub __snapshot_rollback_get_unused {
369 my ($class, $conf, $snap) = @_;
370
371 my $unused = [];
372
373 $class->__snapshot_foreach_volume($conf, sub {
374 my ($vs, $volume) = @_;
375
376 return if PVE::QemuServer::drive_is_cdrom($volume);
377
378 my $found = 0;
379 my $volid = $volume->{file};
380
381 $class->__snapshot_foreach_volume($snap, sub {
382 my ($ds, $drive) = @_;
383
384 return if $found;
385 return if PVE::QemuServer::drive_is_cdrom($drive);
386
387 $found = 1
388 if ($drive->{file} && $drive->{file} eq $volid);
389 });
390
391 push @$unused, $volid if !$found;
392 });
393
394 return $unused;
395}
396
b2c9558d
FG
397sub __snapshot_foreach_volume {
398 my ($class, $conf, $func) = @_;
399
400 PVE::QemuServer::foreach_drive($conf, $func);
401}
ffda963f
FG
402# END implemented abstract methods from PVE::AbstractConfig
403
4041;