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 = 8;
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 && !$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 if ($volid =~ m
|^/|) {
75 return if !$attr->{replicate
};
76 die "unable to replicate local file/device '$volid'\n";
79 return if $attr->{cdrom
};
81 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, $noerr);
84 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
85 return if $scfg->{shared
};
87 my ($path, $owner, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
88 return if !$owner || ($owner != $vmid);
90 die "unable to replicate volume '$volid', type '$vtype'\n" if $vtype ne 'images';
92 return if !$cleanup && !$attr->{replicate
};
94 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
95 return if $cleanup || $noerr;
96 die "missing replicate feature on volume '$volid'\n";
99 $volhash->{$volid} = 1;
102 PVE
::QemuServer
::foreach_volid
($conf, $test_volid);
104 # add 'unusedX' volumes to volhash
105 foreach my $key (keys %$conf) {
106 if ($key =~ m/^unused/) {
107 $test_volid->($conf->{$key}, { replicate
=> 1 });
114 sub __snapshot_save_vmstate
{
115 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
117 my $snap = $conf->{snapshots
}->{$snapname};
121 # search shared storage first
122 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
124 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
125 return if !$scfg->{shared
};
127 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage
131 # now search local storage
132 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
134 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
135 return if $scfg->{shared
};
137 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage;
141 $target = 'local' if !$target;
143 my $driver_state_size = 500; # assume 32MB is enough to safe all driver state;
144 # we abort live save after $conf->{memory}, so we need at max twice that space
145 my $size = $conf->{memory
}*2 + $driver_state_size;
147 my $name = "vm-$vmid-state-$snapname";
148 my $scfg = PVE
::Storage
::storage_config
($storecfg, $target);
149 $name .= ".raw" if $scfg->{path
}; # add filename extension for file base storage
150 $snap->{vmstate
} = PVE
::Storage
::vdisk_alloc
($storecfg, $target, $vmid, 'raw', $name, $size*1024);
151 # always overwrite machine if we save vmstate. This makes sure we
152 # can restore it later using correct machine type
153 $snap->{machine
} = PVE
::QemuServer
::get_current_qemu_machine
($vmid);
156 sub __snapshot_check_running
{
157 my ($class, $vmid) = @_;
158 return PVE
::QemuServer
::check_running
($vmid);
161 sub __snapshot_check_freeze_needed
{
162 my ($class, $vmid, $config, $save_vmstate) = @_;
164 my $running = $class->__snapshot_check_running($vmid);
166 return ($running, $running && $config->{agent
} && PVE
::QemuServer
::qga_check_running
($vmid));
168 return ($running, 0);
172 sub __snapshot_freeze
{
173 my ($class, $vmid, $unfreeze) = @_;
176 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
177 warn "guest-fsfreeze-thaw problems - $@" if $@;
179 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
180 warn "guest-fsfreeze-freeze problems - $@" if $@;
184 sub __snapshot_create_vol_snapshots_hook
{
185 my ($class, $vmid, $snap, $running, $hook) = @_;
188 my $storecfg = PVE
::Storage
::config
();
190 if ($hook eq "before") {
191 if ($snap->{vmstate
}) {
192 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
193 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
195 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start", statefile
=> $path);
197 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
198 if (!$stat->{status
}) {
199 die "savevm not active\n";
200 } elsif ($stat->{status
} eq 'active') {
203 } elsif ($stat->{status
} eq 'completed') {
206 die "query-savevm returned status '$stat->{status}'\n";
210 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start");
212 } elsif ($hook eq "after") {
214 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-end");
215 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
218 } elsif ($hook eq "after-freeze") {
219 # savevm-end is async, we need to wait
221 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
222 if (!$stat->{bytes
}) {
225 print "savevm not yet finished\n";
234 sub __snapshot_create_vol_snapshot
{
235 my ($class, $vmid, $ds, $drive, $snapname) = @_;
237 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
239 my $volid = $drive->{file
};
240 my $device = "drive-$ds";
241 my $storecfg = PVE
::Storage
::config
();
243 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
246 sub __snapshot_delete_remove_drive
{
247 my ($class, $snap, $remove_drive) = @_;
249 if ($remove_drive eq 'vmstate') {
250 delete $snap->{$remove_drive};
252 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
253 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
255 my $volid = $drive->{file
};
256 delete $snap->{$remove_drive};
257 $class->add_unused_volume($snap, $volid);
261 sub __snapshot_delete_vmstate_file
{
262 my ($class, $snap, $force) = @_;
264 my $storecfg = PVE
::Storage
::config
();
266 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
273 sub __snapshot_delete_vol_snapshot
{
274 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
276 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
277 my $storecfg = PVE
::Storage
::config
();
278 my $volid = $drive->{file
};
279 my $device = "drive-$ds";
281 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
283 push @$unused, $volid;
286 sub __snapshot_rollback_vol_possible
{
287 my ($class, $drive, $snapname) = @_;
289 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
291 my $storecfg = PVE
::Storage
::config
();
292 my $volid = $drive->{file
};
294 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
297 sub __snapshot_rollback_vol_rollback
{
298 my ($class, $drive, $snapname) = @_;
300 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
302 my $storecfg = PVE
::Storage
::config
();
303 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
306 sub __snapshot_rollback_vm_stop
{
307 my ($class, $vmid) = @_;
309 my $storecfg = PVE
::Storage
::config
();
310 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
313 sub __snapshot_rollback_vm_start
{
314 my ($class, $vmid, $vmstate, $forcemachine) = @_;
316 my $storecfg = PVE
::Storage
::config
();
317 my $statefile = PVE
::Storage
::path
($storecfg, $vmstate);
318 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $statefile, undef, undef, undef, $forcemachine);
321 sub __snapshot_rollback_get_unused
{
322 my ($class, $conf, $snap) = @_;
326 $class->__snapshot_foreach_volume($conf, sub {
327 my ($vs, $volume) = @_;
329 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
332 my $volid = $volume->{file
};
334 $class->__snapshot_foreach_volume($snap, sub {
335 my ($ds, $drive) = @_;
338 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
341 if ($drive->{file
} && $drive->{file
} eq $volid);
344 push @$unused, $volid if !$found;
350 sub __snapshot_foreach_volume
{
351 my ($class, $conf, $func) = @_;
353 PVE
::QemuServer
::foreach_drive
($conf, $func);
355 # END implemented abstract methods from PVE::AbstractConfig