]>
Commit | Line | Data |
---|---|---|
ffda963f FG |
1 | package PVE::QemuConfig; |
2 | ||
3 | use strict; | |
4 | use warnings; | |
5 | ||
b2c9558d FG |
6 | use PVE::AbstractConfig; |
7 | use PVE::INotify; | |
8 | use PVE::QemuServer; | |
9 | use PVE::Storage; | |
10 | use PVE::Tools; | |
11 | ||
ffda963f FG |
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 = 8; | |
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 { | |
3aa44d3b | 32 | my ($class) = @_; |
ffda963f FG |
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 | ||
b2c9558d FG |
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 && !$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 | ||
3aa44d3b DM |
66 | sub get_replicatable_volumes { |
67 | my ($class, $storecfg, $conf, $noerr) = @_; | |
68 | ||
69 | my $volhash = {}; | |
70 | ||
71 | my $test_volid = sub { | |
72 | my ($volid, $drive) = @_; | |
73 | ||
74 | return if !$volid; | |
75 | ||
76 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
77 | ||
78 | return if defined($drive->{replicate}) && !$drive->{replicate}; | |
79 | ||
80 | if (!PVE::Storage::volume_has_feature($storecfg, 'replicate', $volid)) { | |
81 | return if $noerr; | |
82 | die "missing replicate feature on volume '$volid'\n"; | |
83 | } | |
84 | ||
85 | $volhash->{$volid} = 1; | |
86 | }; | |
87 | ||
88 | PVE::QemuServer::foreach_drive($conf, sub { | |
89 | my ($ds, $drive) = @_; | |
90 | $test_volid->($drive->{file}, $drive); | |
91 | }); | |
92 | ||
93 | foreach my $snapname (keys %{$conf->{snapshots}}) { | |
94 | my $snap = $conf->{snapshots}->{$snapname}; | |
95 | # fixme: what about $snap->{vmstate} | |
96 | PVE::QemuServer::foreach_drive($snap, sub { | |
97 | my ($ds, $drive) = @_; | |
98 | $test_volid->($drive->{file}, $drive); | |
99 | }); | |
100 | } | |
101 | ||
102 | return $volhash; | |
103 | } | |
104 | ||
b2c9558d FG |
105 | sub __snapshot_save_vmstate { |
106 | my ($class, $vmid, $conf, $snapname, $storecfg) = @_; | |
107 | ||
108 | my $snap = $conf->{snapshots}->{$snapname}; | |
109 | ||
110 | my $target; | |
111 | ||
112 | # search shared storage first | |
113 | PVE::QemuServer::foreach_writable_storage($conf, sub { | |
114 | my ($sid) = @_; | |
115 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
116 | return if !$scfg->{shared}; | |
117 | ||
118 | $target = $sid if !$target || $scfg->{path}; # prefer file based storage | |
119 | }); | |
120 | ||
121 | if (!$target) { | |
122 | # now search local storage | |
123 | PVE::QemuServer::foreach_writable_storage($conf, sub { | |
124 | my ($sid) = @_; | |
125 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
126 | return if $scfg->{shared}; | |
127 | ||
128 | $target = $sid if !$target || $scfg->{path}; # prefer file based storage; | |
129 | }); | |
130 | } | |
131 | ||
132 | $target = 'local' if !$target; | |
133 | ||
134 | my $driver_state_size = 500; # assume 32MB is enough to safe all driver state; | |
135 | # we abort live save after $conf->{memory}, so we need at max twice that space | |
136 | my $size = $conf->{memory}*2 + $driver_state_size; | |
137 | ||
138 | my $name = "vm-$vmid-state-$snapname"; | |
139 | my $scfg = PVE::Storage::storage_config($storecfg, $target); | |
140 | $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage | |
141 | $snap->{vmstate} = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024); | |
142 | # always overwrite machine if we save vmstate. This makes sure we | |
143 | # can restore it later using correct machine type | |
144 | $snap->{machine} = PVE::QemuServer::get_current_qemu_machine($vmid); | |
145 | } | |
146 | ||
147 | sub __snapshot_check_running { | |
148 | my ($class, $vmid) = @_; | |
149 | return PVE::QemuServer::check_running($vmid); | |
150 | } | |
151 | ||
152 | sub __snapshot_check_freeze_needed { | |
153 | my ($class, $vmid, $config, $save_vmstate) = @_; | |
154 | ||
155 | my $running = $class->__snapshot_check_running($vmid); | |
156 | if ($save_vmstate) { | |
157 | return ($running, $running && $config->{agent} && PVE::QemuServer::qga_check_running($vmid)); | |
158 | } else { | |
159 | return ($running, 0); | |
160 | } | |
161 | } | |
162 | ||
163 | sub __snapshot_freeze { | |
164 | my ($class, $vmid, $unfreeze) = @_; | |
165 | ||
166 | if ($unfreeze) { | |
167 | eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-thaw"); }; | |
168 | warn "guest-fsfreeze-thaw problems - $@" if $@; | |
169 | } else { | |
170 | eval { PVE::QemuServer::vm_mon_cmd($vmid, "guest-fsfreeze-freeze"); }; | |
171 | warn "guest-fsfreeze-freeze problems - $@" if $@; | |
172 | } | |
173 | } | |
174 | ||
175 | sub __snapshot_create_vol_snapshots_hook { | |
176 | my ($class, $vmid, $snap, $running, $hook) = @_; | |
177 | ||
178 | if ($running) { | |
3a8deb55 AD |
179 | my $storecfg = PVE::Storage::config(); |
180 | ||
b2c9558d FG |
181 | if ($hook eq "before") { |
182 | if ($snap->{vmstate}) { | |
b2c9558d | 183 | my $path = PVE::Storage::path($storecfg, $snap->{vmstate}); |
3a8deb55 AD |
184 | PVE::Storage::activate_volumes($storecfg, [$snap->{vmstate}]); |
185 | ||
b2c9558d FG |
186 | PVE::QemuServer::vm_mon_cmd($vmid, "savevm-start", statefile => $path); |
187 | for(;;) { | |
188 | my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm"); | |
189 | if (!$stat->{status}) { | |
190 | die "savevm not active\n"; | |
191 | } elsif ($stat->{status} eq 'active') { | |
192 | sleep(1); | |
193 | next; | |
194 | } elsif ($stat->{status} eq 'completed') { | |
195 | last; | |
196 | } else { | |
197 | die "query-savevm returned status '$stat->{status}'\n"; | |
198 | } | |
199 | } | |
200 | } else { | |
201 | PVE::QemuServer::vm_mon_cmd($vmid, "savevm-start"); | |
202 | } | |
203 | } elsif ($hook eq "after") { | |
3a8deb55 AD |
204 | eval { |
205 | PVE::QemuServer::vm_mon_cmd($vmid, "savevm-end"); | |
206 | PVE::Storage::deactivate_volumes($storecfg, [$snap->{vmstate}]) if $snap->{vmstate}; | |
207 | }; | |
b2c9558d FG |
208 | warn $@ if $@; |
209 | } elsif ($hook eq "after-freeze") { | |
210 | # savevm-end is async, we need to wait | |
211 | for (;;) { | |
212 | my $stat = PVE::QemuServer::vm_mon_cmd_nocheck($vmid, "query-savevm"); | |
213 | if (!$stat->{bytes}) { | |
214 | last; | |
215 | } else { | |
216 | print "savevm not yet finished\n"; | |
217 | sleep(1); | |
218 | next; | |
219 | } | |
220 | } | |
221 | } | |
222 | } | |
223 | } | |
224 | ||
225 | sub __snapshot_create_vol_snapshot { | |
226 | my ($class, $vmid, $ds, $drive, $snapname) = @_; | |
227 | ||
228 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
229 | ||
230 | my $volid = $drive->{file}; | |
231 | my $device = "drive-$ds"; | |
232 | my $storecfg = PVE::Storage::config(); | |
233 | ||
234 | PVE::QemuServer::qemu_volume_snapshot($vmid, $device, $storecfg, $volid, $snapname); | |
235 | } | |
236 | ||
237 | sub __snapshot_delete_remove_drive { | |
238 | my ($class, $snap, $remove_drive) = @_; | |
239 | ||
240 | if ($remove_drive eq 'vmstate') { | |
241 | delete $snap->{$remove_drive}; | |
242 | } else { | |
243 | my $drive = PVE::QemuServer::parse_drive($remove_drive, $snap->{$remove_drive}); | |
244 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
245 | ||
246 | my $volid = $drive->{file}; | |
247 | delete $snap->{$remove_drive}; | |
248 | $class->add_unused_volume($snap, $volid); | |
249 | } | |
250 | } | |
251 | ||
252 | sub __snapshot_delete_vmstate_file { | |
253 | my ($class, $snap, $force) = @_; | |
254 | ||
255 | my $storecfg = PVE::Storage::config(); | |
256 | ||
257 | eval { PVE::Storage::vdisk_free($storecfg, $snap->{vmstate}); }; | |
258 | if (my $err = $@) { | |
259 | die $err if !$force; | |
260 | warn $err; | |
261 | } | |
262 | } | |
263 | ||
264 | sub __snapshot_delete_vol_snapshot { | |
265 | my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_; | |
266 | ||
267 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
268 | my $storecfg = PVE::Storage::config(); | |
269 | my $volid = $drive->{file}; | |
270 | my $device = "drive-$ds"; | |
271 | ||
272 | PVE::QemuServer::qemu_volume_snapshot_delete($vmid, $device, $storecfg, $volid, $snapname); | |
273 | ||
274 | push @$unused, $volid; | |
275 | } | |
276 | ||
277 | sub __snapshot_rollback_vol_possible { | |
278 | my ($class, $drive, $snapname) = @_; | |
279 | ||
280 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
281 | ||
282 | my $storecfg = PVE::Storage::config(); | |
283 | my $volid = $drive->{file}; | |
284 | ||
285 | PVE::Storage::volume_rollback_is_possible($storecfg, $volid, $snapname); | |
286 | } | |
287 | ||
288 | sub __snapshot_rollback_vol_rollback { | |
289 | my ($class, $drive, $snapname) = @_; | |
290 | ||
291 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
292 | ||
293 | my $storecfg = PVE::Storage::config(); | |
294 | PVE::Storage::volume_snapshot_rollback($storecfg, $drive->{file}, $snapname); | |
295 | } | |
296 | ||
297 | sub __snapshot_rollback_vm_stop { | |
298 | my ($class, $vmid) = @_; | |
299 | ||
300 | my $storecfg = PVE::Storage::config(); | |
301 | PVE::QemuServer::vm_stop($storecfg, $vmid, undef, undef, 5, undef, undef); | |
302 | } | |
303 | ||
304 | sub __snapshot_rollback_vm_start { | |
305 | my ($class, $vmid, $vmstate, $forcemachine) = @_; | |
306 | ||
307 | my $storecfg = PVE::Storage::config(); | |
308 | my $statefile = PVE::Storage::path($storecfg, $vmstate); | |
309 | PVE::QemuServer::vm_start($storecfg, $vmid, $statefile, undef, undef, undef, $forcemachine); | |
310 | } | |
311 | ||
c4a54ed5 FG |
312 | sub __snapshot_rollback_get_unused { |
313 | my ($class, $conf, $snap) = @_; | |
314 | ||
315 | my $unused = []; | |
316 | ||
317 | $class->__snapshot_foreach_volume($conf, sub { | |
318 | my ($vs, $volume) = @_; | |
319 | ||
320 | return if PVE::QemuServer::drive_is_cdrom($volume); | |
321 | ||
322 | my $found = 0; | |
323 | my $volid = $volume->{file}; | |
324 | ||
325 | $class->__snapshot_foreach_volume($snap, sub { | |
326 | my ($ds, $drive) = @_; | |
327 | ||
328 | return if $found; | |
329 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
330 | ||
331 | $found = 1 | |
332 | if ($drive->{file} && $drive->{file} eq $volid); | |
333 | }); | |
334 | ||
335 | push @$unused, $volid if !$found; | |
336 | }); | |
337 | ||
338 | return $unused; | |
339 | } | |
340 | ||
b2c9558d FG |
341 | sub __snapshot_foreach_volume { |
342 | my ($class, $conf, $func) = @_; | |
343 | ||
344 | PVE::QemuServer::foreach_drive($conf, $func); | |
345 | } | |
ffda963f FG |
346 | # END implemented abstract methods from PVE::AbstractConfig |
347 | ||
348 | 1; |