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