]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuConfig.pm
grammar fix: s/does not exists/does not exist/g
[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::Helpers;
9 use PVE::QemuServer::Monitor qw(mon_cmd);
10 use PVE::QemuServer;
11 use PVE::QemuServer::Machine;
12 use PVE::Storage;
13 use PVE::Tools;
14
15 use base qw(PVE::AbstractConfig);
16
17 my $nodename = PVE::INotify::nodename();
18
19 mkdir "/etc/pve/nodes/$nodename";
20 mkdir "/etc/pve/nodes/$nodename/qemu-server";
21
22 my $lock_dir = "/var/lock/qemu-server";
23 mkdir $lock_dir;
24
25 my $MAX_UNUSED_DISKS = 256;
26
27 sub 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
40 # BEGIN implemented abstract methods from PVE::AbstractConfig
41
42 sub guest_type {
43 return "VM";
44 }
45
46 sub __config_max_unused_disks {
47 my ($class) = @_;
48
49 return $MAX_UNUSED_DISKS;
50 }
51
52 sub config_file_lock {
53 my ($class, $vmid) = @_;
54
55 return "$lock_dir/lock-$vmid.conf";
56 }
57
58 sub cfs_config_path {
59 my ($class, $vmid, $node) = @_;
60
61 $node = $nodename if !$node;
62 return "nodes/$node/qemu-server/$vmid.conf";
63 }
64
65 sub 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);
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);
76 });
77
78 return $err ? 0 : 1;
79 }
80
81 sub get_replicatable_volumes {
82 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
83
84 my $volhash = {};
85
86 my $test_volid = sub {
87 my ($volid, $attr) = @_;
88
89 return if $attr->{cdrom};
90
91 return if !$cleanup && !$attr->{replicate};
92
93 if ($volid =~ m|^/|) {
94 return if !$attr->{replicate};
95 return if $cleanup || $noerr;
96 die "unable to replicate local file/device '$volid'\n";
97 }
98
99 my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, $noerr);
100 return if !$storeid;
101
102 my $scfg = PVE::Storage::storage_config($storecfg, $storeid);
103 return if $scfg->{shared};
104
105 my ($path, $owner, $vtype) = PVE::Storage::path($storecfg, $volid);
106 return if !$owner || ($owner != $vmid);
107
108 if ($vtype ne 'images') {
109 return if $cleanup || $noerr;
110 die "unable to replicate volume '$volid', type '$vtype'\n";
111 }
112
113 if (!PVE::Storage::volume_has_feature($storecfg, 'replicate', $volid)) {
114 return if $cleanup || $noerr;
115 die "missing replicate feature on volume '$volid'\n";
116 }
117
118 $volhash->{$volid} = 1;
119 };
120
121 PVE::QemuServer::foreach_volid($conf, $test_volid);
122
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
130 return $volhash;
131 }
132
133 sub __snapshot_save_vmstate {
134 my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
135
136 # use given storage or search for one from the config
137 my $target = $statestorage;
138
139 if (!$target) {
140 $target = PVE::QemuServer::find_vmstate_storage($conf, $storecfg);
141 }
142
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);
152
153 my $name = "vm-$vmid-state-$snapname";
154 $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage
155
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);
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;
169 }
170
171 sub __snapshot_check_running {
172 my ($class, $vmid) = @_;
173 return PVE::QemuServer::Helpers::vm_running_locally($vmid);
174 }
175
176 sub __snapshot_check_freeze_needed {
177 my ($class, $vmid, $config, $save_vmstate) = @_;
178
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));
182 } else {
183 return ($running, 0);
184 }
185 }
186
187 sub __snapshot_freeze {
188 my ($class, $vmid, $unfreeze) = @_;
189
190 if ($unfreeze) {
191 eval { mon_cmd($vmid, "guest-fsfreeze-thaw"); };
192 warn "guest-fsfreeze-thaw problems - $@" if $@;
193 } else {
194 eval { mon_cmd($vmid, "guest-fsfreeze-freeze"); };
195 warn "guest-fsfreeze-freeze problems - $@" if $@;
196 }
197 }
198
199 sub __snapshot_create_vol_snapshots_hook {
200 my ($class, $vmid, $snap, $running, $hook) = @_;
201
202 if ($running) {
203 my $storecfg = PVE::Storage::config();
204
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}]);
209
210 mon_cmd($vmid, "savevm-start", statefile => $path);
211 for(;;) {
212 my $stat = mon_cmd($vmid, "query-savevm");
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 {
225 mon_cmd($vmid, "savevm-start");
226 }
227 } elsif ($hook eq "after") {
228 eval {
229 mon_cmd($vmid, "savevm-end");
230 PVE::Storage::deactivate_volumes($storecfg, [$snap->{vmstate}]) if $snap->{vmstate};
231 };
232 warn $@ if $@;
233 } elsif ($hook eq "after-freeze") {
234 # savevm-end is async, we need to wait
235 for (;;) {
236 my $stat = mon_cmd($vmid, "query-savevm");
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
249 sub __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
261 sub __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
276 sub __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
288 sub __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
301 sub __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 {
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};
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 }
322
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();
328 }
329 }
330
331 return;
332 }
333
334 sub __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
345 sub __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
354 sub __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
361 sub __snapshot_rollback_vm_start {
362 my ($class, $vmid, $vmstate, $data) = @_;
363
364 my $storecfg = PVE::Storage::config();
365 PVE::QemuServer::vm_start($storecfg, $vmid, $vmstate, undef, undef, undef, $data->{forcemachine});
366 }
367
368 sub __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
397 sub __snapshot_foreach_volume {
398 my ($class, $conf, $func) = @_;
399
400 PVE::QemuServer::foreach_drive($conf, $func);
401 }
402 # END implemented abstract methods from PVE::AbstractConfig
403
404 1;