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