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 my ($storeid, $volname) = PVE
::Storage
::parse_volume_id
($volid, $noerr);
77 my $scfg = PVE
::Storage
::storage_config
($storecfg, $storeid);
78 return if $scfg->{shared
};
80 my ($path, $owner, $vtype) = PVE
::Storage
::path
($storecfg, $volid);
81 return if !$owner || ($owner != $vmid);
83 return if $attr->{cdrom
};
85 die "unable to replicate volume '$volid', type '$vtype'\n" if $vtype ne 'images';
87 return if !$cleanup && !$attr->{replicate
};
89 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
90 return if $cleanup || $noerr;
91 die "missing replicate feature on volume '$volid'\n";
94 $volhash->{$volid} = 1;
97 PVE
::QemuServer
::foreach_volid
($conf, $test_volid);
99 # add 'unusedX' volumes to volhash
100 foreach my $key (keys %$conf) {
101 if ($key =~ m/^unused/) {
102 $test_volid->($conf->{$key}, { replicate
=> 1 });
109 sub __snapshot_save_vmstate
{
110 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
112 my $snap = $conf->{snapshots
}->{$snapname};
116 # search shared storage first
117 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
119 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
120 return if !$scfg->{shared
};
122 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage
126 # now search local storage
127 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
129 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
130 return if $scfg->{shared
};
132 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage;
136 $target = 'local' if !$target;
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;
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);
151 sub __snapshot_check_running
{
152 my ($class, $vmid) = @_;
153 return PVE
::QemuServer
::check_running
($vmid);
156 sub __snapshot_check_freeze_needed
{
157 my ($class, $vmid, $config, $save_vmstate) = @_;
159 my $running = $class->__snapshot_check_running($vmid);
161 return ($running, $running && $config->{agent
} && PVE
::QemuServer
::qga_check_running
($vmid));
163 return ($running, 0);
167 sub __snapshot_freeze
{
168 my ($class, $vmid, $unfreeze) = @_;
171 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
172 warn "guest-fsfreeze-thaw problems - $@" if $@;
174 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
175 warn "guest-fsfreeze-freeze problems - $@" if $@;
179 sub __snapshot_create_vol_snapshots_hook
{
180 my ($class, $vmid, $snap, $running, $hook) = @_;
183 my $storecfg = PVE
::Storage
::config
();
185 if ($hook eq "before") {
186 if ($snap->{vmstate
}) {
187 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
188 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
190 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start", statefile
=> $path);
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') {
198 } elsif ($stat->{status
} eq 'completed') {
201 die "query-savevm returned status '$stat->{status}'\n";
205 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start");
207 } elsif ($hook eq "after") {
209 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-end");
210 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
213 } elsif ($hook eq "after-freeze") {
214 # savevm-end is async, we need to wait
216 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
217 if (!$stat->{bytes
}) {
220 print "savevm not yet finished\n";
229 sub __snapshot_create_vol_snapshot
{
230 my ($class, $vmid, $ds, $drive, $snapname) = @_;
232 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
234 my $volid = $drive->{file
};
235 my $device = "drive-$ds";
236 my $storecfg = PVE
::Storage
::config
();
238 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
241 sub __snapshot_delete_remove_drive
{
242 my ($class, $snap, $remove_drive) = @_;
244 if ($remove_drive eq 'vmstate') {
245 delete $snap->{$remove_drive};
247 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
248 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
250 my $volid = $drive->{file
};
251 delete $snap->{$remove_drive};
252 $class->add_unused_volume($snap, $volid);
256 sub __snapshot_delete_vmstate_file
{
257 my ($class, $snap, $force) = @_;
259 my $storecfg = PVE
::Storage
::config
();
261 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
268 sub __snapshot_delete_vol_snapshot
{
269 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
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";
276 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
278 push @$unused, $volid;
281 sub __snapshot_rollback_vol_possible
{
282 my ($class, $drive, $snapname) = @_;
284 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
286 my $storecfg = PVE
::Storage
::config
();
287 my $volid = $drive->{file
};
289 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
292 sub __snapshot_rollback_vol_rollback
{
293 my ($class, $drive, $snapname) = @_;
295 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
297 my $storecfg = PVE
::Storage
::config
();
298 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
301 sub __snapshot_rollback_vm_stop
{
302 my ($class, $vmid) = @_;
304 my $storecfg = PVE
::Storage
::config
();
305 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
308 sub __snapshot_rollback_vm_start
{
309 my ($class, $vmid, $vmstate, $forcemachine) = @_;
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);
316 sub __snapshot_rollback_get_unused
{
317 my ($class, $conf, $snap) = @_;
321 $class->__snapshot_foreach_volume($conf, sub {
322 my ($vs, $volume) = @_;
324 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
327 my $volid = $volume->{file
};
329 $class->__snapshot_foreach_volume($snap, sub {
330 my ($ds, $drive) = @_;
333 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
336 if ($drive->{file
} && $drive->{file
} eq $volid);
339 push @$unused, $volid if !$found;
345 sub __snapshot_foreach_volume
{
346 my ($class, $conf, $func) = @_;
348 PVE
::QemuServer
::foreach_drive
($conf, $func);
350 # END implemented abstract methods from PVE::AbstractConfig