1 package PVE
::QemuConfig
;
6 use PVE
::AbstractConfig
;
12 use base
qw(PVE::AbstractConfig);
14 my $nodename = PVE
::INotify
::nodename
();
16 mkdir "/etc/pve/nodes/$nodename";
17 my $confdir = "/etc/pve/nodes/$nodename/qemu-server";
20 my $lock_dir = "/var/lock/qemu-server";
23 my $MAX_UNUSED_DISKS = 256;
25 # BEGIN implemented abstract methods from PVE::AbstractConfig
31 sub __config_max_unused_disks
{
34 return $MAX_UNUSED_DISKS;
37 sub config_file_lock
{
38 my ($class, $vmid) = @_;
40 return "$lock_dir/lock-$vmid.conf";
44 my ($class, $vmid, $node) = @_;
46 $node = $nodename if !$node;
47 return "nodes/$node/qemu-server/$vmid.conf";
51 my ($class, $feature, $conf, $storecfg, $snapname, $running, $backup_only) = @_;
54 PVE
::QemuServer
::foreach_drive
($conf, sub {
55 my ($ds, $drive) = @_;
57 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
58 return if $backup_only && defined($drive->{backup
}) && !$drive->{backup
};
59 my $volid = $drive->{file
};
60 $err = 1 if !PVE
::Storage
::volume_has_feature
($storecfg, $feature, $volid, $snapname, $running);
66 sub get_replicatable_volumes
{
67 my ($class, $storecfg, $vmid, $conf, $cleanup, $noerr) = @_;
71 my $test_volid = sub {
72 my ($volid, $attr) = @_;
74 return if $attr->{cdrom
};
76 return if !$cleanup && !$attr->{replicate
};
78 if ($volid =~ m
|^/|) {
79 return if !$attr->{replicate
};
80 return if $cleanup || $noerr;
81 die "unable to replicate local file/device '$volid'\n";
84 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, $noerr);
87 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
88 return if $scfg->{shared
};
90 my ($path, $owner, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
91 return if !$owner || ($owner != $vmid);
93 if ($vtype ne 'images') {
94 return if $cleanup || $noerr;
95 die "unable to replicate volume '$volid', type '$vtype'\n";
98 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
99 return if $cleanup || $noerr;
100 die "missing replicate feature on volume '$volid'\n";
103 $volhash->{$volid} = 1;
106 PVE
::QemuServer
::foreach_volid
($conf, $test_volid);
108 # add 'unusedX' volumes to volhash
109 foreach my $key (keys %$conf) {
110 if ($key =~ m/^unused/) {
111 $test_volid->($conf->{$key}, { replicate
=> 1 });
118 sub __snapshot_save_vmstate
{
119 my ($class, $vmid, $conf, $snapname, $storecfg, $statestorage, $suspend) = @_;
121 # first, use explicitly configured storage
122 # either directly via API, or via conf
123 my $target = $statestorage // $conf->{vmstatestorage
};
126 my ($shared, $local);
127 PVE
::QemuServer
::foreach_storage_used_by_vm
($conf, sub {
129 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
130 my $dst = $scfg->{shared
} ? \
$shared : \
$local;
131 $$dst = $sid if !$$dst || $scfg->{path
}; # prefer file based storage
134 # second, use shared storage where VM has at least one disk
135 # third, use local storage where VM has at least one disk
136 # fall back to local storage
137 $target = $shared // $local // 'local';
140 my $driver_state_size = 500; # assume 500MB is enough to safe all driver state;
141 my $size = $conf->{memory
}*2 + $driver_state_size;
142 my $scfg = PVE
::Storage
::storage_config
($storecfg, $target);
144 my $name = "vm-$vmid-state-$snapname";
145 $name .= ".raw" if $scfg->{path
}; # add filename extension for file base storage
147 my $statefile = PVE
::Storage
::vdisk_alloc
($storecfg, $target, $vmid, 'raw', $name, $size*1024);
148 my $runningmachine = PVE
::QemuServer
::get_current_qemu_machine
($vmid);
151 $conf->{vmstate
} = $statefile;
152 $conf->{runningmachine
} = $runningmachine;
154 my $snap = $conf->{snapshots
}->{$snapname};
155 $snap->{vmstate
} = $statefile;
156 $snap->{runningmachine
} = $runningmachine;
162 sub __snapshot_check_running
{
163 my ($class, $vmid) = @_;
164 return PVE
::QemuServer
::check_running
($vmid);
167 sub __snapshot_check_freeze_needed
{
168 my ($class, $vmid, $config, $save_vmstate) = @_;
170 my $running = $class->__snapshot_check_running($vmid);
171 if (!$save_vmstate) {
172 return ($running, $running && PVE
::QemuServer
::parse_guest_agent
($config)->{enabled
} && PVE
::QemuServer
::qga_check_running
($vmid));
174 return ($running, 0);
178 sub __snapshot_freeze
{
179 my ($class, $vmid, $unfreeze) = @_;
182 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
183 warn "guest-fsfreeze-thaw problems - $@" if $@;
185 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
186 warn "guest-fsfreeze-freeze problems - $@" if $@;
190 sub __snapshot_create_vol_snapshots_hook
{
191 my ($class, $vmid, $snap, $running, $hook) = @_;
194 my $storecfg = PVE
::Storage
::config
();
196 if ($hook eq "before") {
197 if ($snap->{vmstate
}) {
198 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
199 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
201 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start", statefile
=> $path);
203 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
204 if (!$stat->{status
}) {
205 die "savevm not active\n";
206 } elsif ($stat->{status
} eq 'active') {
209 } elsif ($stat->{status
} eq 'completed') {
212 die "query-savevm returned status '$stat->{status}'\n";
216 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start");
218 } elsif ($hook eq "after") {
220 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-end");
221 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
224 } elsif ($hook eq "after-freeze") {
225 # savevm-end is async, we need to wait
227 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
228 if (!$stat->{bytes
}) {
231 print "savevm not yet finished\n";
240 sub __snapshot_create_vol_snapshot
{
241 my ($class, $vmid, $ds, $drive, $snapname) = @_;
243 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
245 my $volid = $drive->{file
};
246 my $device = "drive-$ds";
247 my $storecfg = PVE
::Storage
::config
();
249 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
252 sub __snapshot_delete_remove_drive
{
253 my ($class, $snap, $remove_drive) = @_;
255 if ($remove_drive eq 'vmstate') {
256 delete $snap->{$remove_drive};
258 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
259 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
261 my $volid = $drive->{file
};
262 delete $snap->{$remove_drive};
263 $class->add_unused_volume($snap, $volid);
267 sub __snapshot_delete_vmstate_file
{
268 my ($class, $snap, $force) = @_;
270 my $storecfg = PVE
::Storage
::config
();
272 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
279 sub __snapshot_delete_vol_snapshot
{
280 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
282 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
283 my $storecfg = PVE
::Storage
::config
();
284 my $volid = $drive->{file
};
285 my $device = "drive-$ds";
287 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
289 push @$unused, $volid;
292 sub __snapshot_rollback_hook
{
293 my ($class, $vmid, $conf, $snap, $prepare, $data) = @_;
296 # we save the machine of the current config
297 $data->{oldmachine
} = $conf->{machine
};
299 # if we have a 'runningmachine' entry in the snapshot we use that
300 # for the forcemachine parameter, else we use the old logic
301 if (defined($conf->{runningmachine
})) {
302 $data->{forcemachine
} = $conf->{runningmachine
};
303 delete $conf->{runningmachine
};
305 # Note: old code did not store 'machine', so we try to be smart
306 # and guess the snapshot was generated with kvm 1.4 (pc-i440fx-1.4).
307 $data->{forcemachine
} = $conf->{machine
} || 'pc-i440fx-1.4';
309 # we remove the 'machine' configuration if not explicitly specified
310 # in the original config.
311 delete $conf->{machine
} if $snap->{vmstate
} && !defined($data->{oldmachine
});
314 if ($conf->{vmgenid
}) {
315 # tell the VM that it's another generation, so it can react
316 # appropriately, e.g. dirty-mark copies of distributed databases or
317 # re-initializing its random number generator
318 $conf->{vmgenid
} = PVE
::QemuServer
::generate_uuid
();
325 sub __snapshot_rollback_vol_possible
{
326 my ($class, $drive, $snapname) = @_;
328 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
330 my $storecfg = PVE
::Storage
::config
();
331 my $volid = $drive->{file
};
333 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
336 sub __snapshot_rollback_vol_rollback
{
337 my ($class, $drive, $snapname) = @_;
339 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
341 my $storecfg = PVE
::Storage
::config
();
342 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
345 sub __snapshot_rollback_vm_stop
{
346 my ($class, $vmid) = @_;
348 my $storecfg = PVE
::Storage
::config
();
349 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
352 sub __snapshot_rollback_vm_start
{
353 my ($class, $vmid, $vmstate, $data) = @_;
355 my $storecfg = PVE
::Storage
::config
();
356 my $statefile = PVE
::Storage
::path
($storecfg, $vmstate);
357 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $statefile, undef, undef, undef, $data->{forcemachine
});
360 sub __snapshot_rollback_get_unused
{
361 my ($class, $conf, $snap) = @_;
365 $class->__snapshot_foreach_volume($conf, sub {
366 my ($vs, $volume) = @_;
368 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
371 my $volid = $volume->{file
};
373 $class->__snapshot_foreach_volume($snap, sub {
374 my ($ds, $drive) = @_;
377 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
380 if ($drive->{file
} && $drive->{file
} eq $volid);
383 push @$unused, $volid if !$found;
389 sub __snapshot_foreach_volume
{
390 my ($class, $conf, $func) = @_;
392 PVE
::QemuServer
::foreach_drive
($conf, $func);
394 # END implemented abstract methods from PVE::AbstractConfig