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