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