]>
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 | 66 | sub 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 | |
a722d4ff DM |
74 | my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, $noerr); |
75 | return if !$storeid; | |
76 | ||
77 | my $scfg = storage_config($storecfg, $storeid); | |
78 | return if $scfg->{shared}; | |
79 | ||
f7e7767f DM |
80 | my ($path, $owner, $vtype) = PVE::Storage::path($storecfg, $volid); |
81 | return if !$owner || ($owner != $vmid); | |
82 | ||
f949eb77 | 83 | return if $attr->{cdrom}; |
3aa44d3b | 84 | |
f7e7767f DM |
85 | die "unable to replicate volume '$volid', type '$vtype'\n" if $vtype ne 'images'; |
86 | ||
f949eb77 | 87 | return if !$cleanup && !$attr->{replicate}; |
3aa44d3b DM |
88 | |
89 | if (!PVE::Storage::volume_has_feature($storecfg, 'replicate', $volid)) { | |
e5857ca8 | 90 | return if $cleanup || $noerr; |
3aa44d3b DM |
91 | die "missing replicate feature on volume '$volid'\n"; |
92 | } | |
93 | ||
94 | $volhash->{$volid} = 1; | |
95 | }; | |
96 | ||
f949eb77 | 97 | PVE::QemuServer::foreach_volid($conf, $test_volid); |
3aa44d3b | 98 | |
8299257e DM |
99 | # add 'unusedX' volumes to volhash |
100 | foreach my $key (keys %$conf) { | |
101 | if ($key =~ m/^unused/) { | |
102 | $test_volid->($conf->{$key}, { replicate => 1 }); | |
103 | } | |
104 | } | |
105 | ||
3aa44d3b DM |
106 | return $volhash; |
107 | } | |
108 | ||
b2c9558d FG |
109 | sub __snapshot_save_vmstate { |
110 | my ($class, $vmid, $conf, $snapname, $storecfg) = @_; | |
111 | ||
112 | my $snap = $conf->{snapshots}->{$snapname}; | |
113 | ||
114 | my $target; | |
115 | ||
116 | # search shared storage first | |
117 | PVE::QemuServer::foreach_writable_storage($conf, sub { | |
118 | my ($sid) = @_; | |
119 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
120 | return if !$scfg->{shared}; | |
121 | ||
122 | $target = $sid if !$target || $scfg->{path}; # prefer file based storage | |
123 | }); | |
124 | ||
125 | if (!$target) { | |
126 | # now search local storage | |
127 | PVE::QemuServer::foreach_writable_storage($conf, sub { | |
128 | my ($sid) = @_; | |
129 | my $scfg = PVE::Storage::storage_config($storecfg, $sid); | |
130 | return if $scfg->{shared}; | |
131 | ||
132 | $target = $sid if !$target || $scfg->{path}; # prefer file based storage; | |
133 | }); | |
134 | } | |
135 | ||
136 | $target = 'local' if !$target; | |
137 | ||
138 | my $driver_state_size = 500; # assume 32MB is enough to safe all driver state; | |
139 | # we abort live save after $conf->{memory}, so we need at max twice that space | |
140 | my $size = $conf->{memory}*2 + $driver_state_size; | |
141 | ||
142 | my $name = "vm-$vmid-state-$snapname"; | |
143 | my $scfg = PVE::Storage::storage_config($storecfg, $target); | |
144 | $name .= ".raw" if $scfg->{path}; # add filename extension for file base storage | |
145 | $snap->{vmstate} = PVE::Storage::vdisk_alloc($storecfg, $target, $vmid, 'raw', $name, $size*1024); | |
146 | # always overwrite machine if we save vmstate. This makes sure we | |
147 | # can restore it later using correct machine type | |
148 | $snap->{machine} = PVE::QemuServer::get_current_qemu_machine($vmid); | |
149 | } | |
150 | ||
151 | sub __snapshot_check_running { | |
152 | my ($class, $vmid) = @_; | |
153 | return PVE::QemuServer::check_running($vmid); | |
154 | } | |
155 | ||
156 | sub __snapshot_check_freeze_needed { | |
157 | my ($class, $vmid, $config, $save_vmstate) = @_; | |
158 | ||
159 | my $running = $class->__snapshot_check_running($vmid); | |
160 | if ($save_vmstate) { | |
161 | return ($running, $running && $config->{agent} && PVE::QemuServer::qga_check_running($vmid)); | |
162 | } else { | |
163 | return ($running, 0); | |
164 | } | |
165 | } | |
166 | ||
167 | sub __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 | ||
179 | sub __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 | ||
229 | sub __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 | ||
241 | sub __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 | ||
256 | sub __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 | ||
268 | sub __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 | ||
281 | sub __snapshot_rollback_vol_possible { | |
282 | my ($class, $drive, $snapname) = @_; | |
283 | ||
284 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
285 | ||
286 | my $storecfg = PVE::Storage::config(); | |
287 | my $volid = $drive->{file}; | |
288 | ||
289 | PVE::Storage::volume_rollback_is_possible($storecfg, $volid, $snapname); | |
290 | } | |
291 | ||
292 | sub __snapshot_rollback_vol_rollback { | |
293 | my ($class, $drive, $snapname) = @_; | |
294 | ||
295 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
296 | ||
297 | my $storecfg = PVE::Storage::config(); | |
298 | PVE::Storage::volume_snapshot_rollback($storecfg, $drive->{file}, $snapname); | |
299 | } | |
300 | ||
301 | sub __snapshot_rollback_vm_stop { | |
302 | my ($class, $vmid) = @_; | |
303 | ||
304 | my $storecfg = PVE::Storage::config(); | |
305 | PVE::QemuServer::vm_stop($storecfg, $vmid, undef, undef, 5, undef, undef); | |
306 | } | |
307 | ||
308 | sub __snapshot_rollback_vm_start { | |
309 | my ($class, $vmid, $vmstate, $forcemachine) = @_; | |
310 | ||
311 | my $storecfg = PVE::Storage::config(); | |
312 | my $statefile = PVE::Storage::path($storecfg, $vmstate); | |
313 | PVE::QemuServer::vm_start($storecfg, $vmid, $statefile, undef, undef, undef, $forcemachine); | |
314 | } | |
315 | ||
c4a54ed5 FG |
316 | sub __snapshot_rollback_get_unused { |
317 | my ($class, $conf, $snap) = @_; | |
318 | ||
319 | my $unused = []; | |
320 | ||
321 | $class->__snapshot_foreach_volume($conf, sub { | |
322 | my ($vs, $volume) = @_; | |
323 | ||
324 | return if PVE::QemuServer::drive_is_cdrom($volume); | |
325 | ||
326 | my $found = 0; | |
327 | my $volid = $volume->{file}; | |
328 | ||
329 | $class->__snapshot_foreach_volume($snap, sub { | |
330 | my ($ds, $drive) = @_; | |
331 | ||
332 | return if $found; | |
333 | return if PVE::QemuServer::drive_is_cdrom($drive); | |
334 | ||
335 | $found = 1 | |
336 | if ($drive->{file} && $drive->{file} eq $volid); | |
337 | }); | |
338 | ||
339 | push @$unused, $volid if !$found; | |
340 | }); | |
341 | ||
342 | return $unused; | |
343 | } | |
344 | ||
b2c9558d FG |
345 | sub __snapshot_foreach_volume { |
346 | my ($class, $conf, $func) = @_; | |
347 | ||
348 | PVE::QemuServer::foreach_drive($conf, $func); | |
349 | } | |
ffda963f FG |
350 | # END implemented abstract methods from PVE::AbstractConfig |
351 | ||
352 | 1; |