]> git.proxmox.com Git - qemu-server.git/blob - PVE/QemuConfig.pm
bump version to 8.2.1
[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, $statestorage, $suspend) = @_;
120
121 # first, use explicitly configured storage
122 # either directly via API, or via conf
123 my $target = $statestorage // $conf->{vmstatestorage};
124
125 if (!$target) {
126 my ($shared, $local);
127 PVE::QemuServer::foreach_storage_used_by_vm($conf, sub {
128 my ($sid) = @_;
129 my $scfg = PVE::Storage::storage_config($storecfg, $sid);
130 my $dst = $scfg->{shared} ? \$shared : \$local;
131 $$dst = $sid if !$$dst || $scfg->{path}; # prefer file based storage
132 });
133
134 # second, use shared storage where VM has at least one disk
135 # third, use local storage where VM has at least one disk
136 # fall back to local storage
137 $target = $shared // $local // 'local';
138 }
139
140 my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
141 # our savevm-start does live-save of the memory until the space left in the
142 # volume is just enough for the remaining memory content + internal state
143 # then it stops the vm and copies the rest so we reserve twice the
144 # memory content + state to minimize vm downtime
145 my $size = $conf->{memory}*2 + $driver_state_size;
146 my $scfg = PVE::Storage::storage_config($storecfg, $target);
147
148 my $name = "vm-$vmid-state-$snapname";
149 $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage
150
151 my $statefile = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024);
152 my $runningmachine = PVE::QemuServer::get_current_qemu_machine($vmid);
153
154 if ($suspend) {
155 $conf->{vmstate} = $statefile;
156 $conf->{runningmachine} = $runningmachine;
157 } else {
158 my $snap = $conf->{snapshots}->{$snapname};
159 $snap->{vmstate} = $statefile;
160 $snap->{runningmachine} = $runningmachine;
161 }
162
163 return $statefile;
164 }
165
166 sub __snapshot_check_running {
167 my ($class, $vmid) = @_;
168 return PVE::QemuServer::check_running($vmid);
169 }
170
171 sub __snapshot_check_freeze_needed {
172 my ($class, $vmid, $config, $save_vmstate) = @_;
173
174 my $running = $class->__snapshot_check_running($vmid);
175 if (!$save_vmstate) {
176 return ($running, $running && PVE::QemuServer::parse_guest_agent($config)->{enabled} && PVE::QemuServer::qga_check_running($vmid));
177 } else {
178 return ($running, 0);
179 }
180 }
181
182 sub __snapshot_freeze {
183 my ($class, $vmid, $unfreeze) = @_;
184
185 if ($unfreeze) {
186 eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-thaw"); };
187 warn "guest-fsfreeze-thaw problems - $@" if $@;
188 } else {
189 eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-freeze"); };
190 warn "guest-fsfreeze-freeze problems - $@" if $@;
191 }
192 }
193
194 sub __snapshot_create_vol_snapshots_hook {
195 my ($class, $vmid, $snap, $running, $hook) = @_;
196
197 if ($running) {
198 my $storecfg = PVE::Storage::config();
199
200 if ($hook eq "before") {
201 if ($snap->{vmstate}) {
202 my $path = PVE::Storage::path($storecfg, $snap->{vmstate});
203 PVE::Storage::activate_volumes($storecfg, [$snap->{vmstate}]);
204
205 PVE::QemuServer::vm_mon_cmd($vmid, "savevm-start", statefile => $path);
206 for(;;) {
207 my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm");
208 if (!$stat->{status}) {
209 die "savevm not active\n";
210 } elsif ($stat->{status} eq 'active') {
211 sleep(1);
212 next;
213 } elsif ($stat->{status} eq 'completed') {
214 last;
215 } else {
216 die "query-savevm returned status '$stat->{status}'\n";
217 }
218 }
219 } else {
220 PVE::QemuServer::vm_mon_cmd($vmid, "savevm-start");
221 }
222 } elsif ($hook eq "after") {
223 eval {
224 PVE::QemuServer::vm_mon_cmd($vmid, "savevm-end");
225 PVE::Storage::deactivate_volumes($storecfg, [$snap->{vmstate}]) if $snap->{vmstate};
226 };
227 warn $@ if $@;
228 } elsif ($hook eq "after-freeze") {
229 # savevm-end is async, we need to wait
230 for (;;) {
231 my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm");
232 if (!$stat->{bytes}) {
233 last;
234 } else {
235 print "savevm not yet finished\n";
236 sleep(1);
237 next;
238 }
239 }
240 }
241 }
242 }
243
244 sub __snapshot_create_vol_snapshot {
245 my ($class, $vmid, $ds, $drive, $snapname) = @_;
246
247 return if PVE::QemuServer::drive_is_cdrom($drive);
248
249 my $volid = $drive->{file};
250 my $device = "drive-$ds";
251 my $storecfg = PVE::Storage::config();
252
253 PVE::QemuServer::qemu_volume_snapshot($vmid, $device, $storecfg, $volid, $snapname);
254 }
255
256 sub __snapshot_delete_remove_drive {
257 my ($class, $snap, $remove_drive) = @_;
258
259 if ($remove_drive eq 'vmstate') {
260 delete $snap->{$remove_drive};
261 } else {
262 my $drive = PVE::QemuServer::parse_drive($remove_drive, $snap->{$remove_drive});
263 return if PVE::QemuServer::drive_is_cdrom($drive);
264
265 my $volid = $drive->{file};
266 delete $snap->{$remove_drive};
267 $class->add_unused_volume($snap, $volid);
268 }
269 }
270
271 sub __snapshot_delete_vmstate_file {
272 my ($class, $snap, $force) = @_;
273
274 my $storecfg = PVE::Storage::config();
275
276 eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); };
277 if (my $err = $@) {
278 die $err if !$force;
279 warn $err;
280 }
281 }
282
283 sub __snapshot_delete_vol_snapshot {
284 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
285
286 return if PVE::QemuServer::drive_is_cdrom($drive);
287 my $storecfg = PVE::Storage::config();
288 my $volid = $drive->{file};
289 my $device = "drive-$ds";
290
291 PVE::QemuServer::qemu_volume_snapshot_delete($vmid, $device, $storecfg, $volid, $snapname);
292
293 push @$unused, $volid;
294 }
295
296 sub __snapshot_rollback_hook {
297 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
298
299 if ($prepare) {
300 # we save the machine of the current config
301 $data->{oldmachine} = $conf->{machine};
302 } else {
303 # if we have a 'runningmachine' entry in the snapshot we use that
304 # for the forcemachine parameter, else we use the old logic
305 if (defined($conf->{runningmachine})) {
306 $data->{forcemachine} = $conf->{runningmachine};
307 delete $conf->{runningmachine};
308 } else {
309 # Note: old code did not store 'machine', so we try to be smart
310 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
311 $data->{forcemachine} = $conf->{machine} || 'pc-i440fx-1.4';
312
313 # we remove the 'machine' configuration if not explicitly specified
314 # in the original config.
315 delete $conf->{machine} if $snap->{vmstate} && !defined($data->{oldmachine});
316 }
317
318 if ($conf->{vmgenid}) {
319 # tell the VM that it's another generation, so it can react
320 # appropriately, e.g. dirty-mark copies of distributed databases or
321 # re-initializing its random number generator
322 $conf->{vmgenid} = PVE::QemuServer::generate_uuid();
323 }
324 }
325
326 return;
327 }
328
329 sub __snapshot_rollback_vol_possible {
330 my ($class, $drive, $snapname) = @_;
331
332 return if PVE::QemuServer::drive_is_cdrom($drive);
333
334 my $storecfg = PVE::Storage::config();
335 my $volid = $drive->{file};
336
337 PVE::Storage::volume_rollback_is_possible($storecfg, $volid, $snapname);
338 }
339
340 sub __snapshot_rollback_vol_rollback {
341 my ($class, $drive, $snapname) = @_;
342
343 return if PVE::QemuServer::drive_is_cdrom($drive);
344
345 my $storecfg = PVE::Storage::config();
346 PVE::Storage::volume_snapshot_rollback($storecfg, $drive->{file}, $snapname);
347 }
348
349 sub __snapshot_rollback_vm_stop {
350 my ($class, $vmid) = @_;
351
352 my $storecfg = PVE::Storage::config();
353 PVE::QemuServer::vm_stop($storecfg, $vmid, undef, undef, 5, undef, undef);
354 }
355
356 sub __snapshot_rollback_vm_start {
357 my ($class, $vmid, $vmstate, $data) = @_;
358
359 my $storecfg = PVE::Storage::config();
360 my $statefile = PVE::Storage::path($storecfg, $vmstate);
361 PVE::QemuServer::vm_start($storecfg, $vmid, $statefile, undef, undef, undef, $data->{forcemachine});
362 }
363
364 sub __snapshot_rollback_get_unused {
365 my ($class, $conf, $snap) = @_;
366
367 my $unused = [];
368
369 $class->__snapshot_foreach_volume($conf, sub {
370 my ($vs, $volume) = @_;
371
372 return if PVE::QemuServer::drive_is_cdrom($volume);
373
374 my $found = 0;
375 my $volid = $volume->{file};
376
377 $class->__snapshot_foreach_volume($snap, sub {
378 my ($ds, $drive) = @_;
379
380 return if $found;
381 return if PVE::QemuServer::drive_is_cdrom($drive);
382
383 $found = 1
384 if ($drive->{file} && $drive->{file} eq $volid);
385 });
386
387 push @$unused, $volid if !$found;
388 });
389
390 return $unused;
391 }
392
393 sub __snapshot_foreach_volume {
394 my ($class, $conf, $func) = @_;
395
396 PVE::QemuServer::foreach_drive($conf, $func);
397 }
398 # END implemented abstract methods from PVE::AbstractConfig
399
400 1;