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, $conf, $noerr) = @_;
71 my $test_volid = sub {
72 my ($volid, $drive) = @_;
76 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
78 return if defined($drive->{replicate
}) && !$drive->{replicate
};
80 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
82 die "missing replicate feature on volume '$volid'\n";
85 $volhash->{$volid} = 1;
88 PVE
::QemuServer
::foreach_drive
($conf, sub {
89 my ($ds, $drive) = @_;
90 $test_volid->($drive->{file
}, $drive);
93 foreach my $snapname (keys %{$conf->{snapshots
}}) {
94 my $snap = $conf->{snapshots
}->{$snapname};
95 # fixme: what about $snap->{vmstate}
96 PVE
::QemuServer
::foreach_drive
($snap, sub {
97 my ($ds, $drive) = @_;
98 $test_volid->($drive->{file
}, $drive);
105 sub __snapshot_save_vmstate
{
106 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
108 my $snap = $conf->{snapshots
}->{$snapname};
112 # search shared storage first
113 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
115 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
116 return if !$scfg->{shared
};
118 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage
122 # now search local storage
123 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
125 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
126 return if $scfg->{shared
};
128 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage;
132 $target = 'local' if !$target;
134 my $driver_state_size = 500; # assume 32MB is enough to safe all driver state;
135 # we abort live save after $conf->{memory}, so we need at max twice that space
136 my $size = $conf->{memory
}*2 + $driver_state_size;
138 my $name = "vm-$vmid-state-$snapname";
139 my $scfg = PVE
::Storage
::storage_config
($storecfg, $target);
140 $name .= ".raw" if $scfg->{path
}; # add filename extension for file base storage
141 $snap->{vmstate
} = PVE
::Storage
::vdisk_alloc
($storecfg, $target, $vmid, 'raw', $name, $size*1024);
142 # always overwrite machine if we save vmstate. This makes sure we
143 # can restore it later using correct machine type
144 $snap->{machine
} = PVE
::QemuServer
::get_current_qemu_machine
($vmid);
147 sub __snapshot_check_running
{
148 my ($class, $vmid) = @_;
149 return PVE
::QemuServer
::check_running
($vmid);
152 sub __snapshot_check_freeze_needed
{
153 my ($class, $vmid, $config, $save_vmstate) = @_;
155 my $running = $class->__snapshot_check_running($vmid);
157 return ($running, $running && $config->{agent
} && PVE
::QemuServer
::qga_check_running
($vmid));
159 return ($running, 0);
163 sub __snapshot_freeze
{
164 my ($class, $vmid, $unfreeze) = @_;
167 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
168 warn "guest-fsfreeze-thaw problems - $@" if $@;
170 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
171 warn "guest-fsfreeze-freeze problems - $@" if $@;
175 sub __snapshot_create_vol_snapshots_hook
{
176 my ($class, $vmid, $snap, $running, $hook) = @_;
179 my $storecfg = PVE
::Storage
::config
();
181 if ($hook eq "before") {
182 if ($snap->{vmstate
}) {
183 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
184 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
186 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start", statefile
=> $path);
188 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
189 if (!$stat->{status
}) {
190 die "savevm not active\n";
191 } elsif ($stat->{status
} eq 'active') {
194 } elsif ($stat->{status
} eq 'completed') {
197 die "query-savevm returned status '$stat->{status}'\n";
201 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start");
203 } elsif ($hook eq "after") {
205 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-end");
206 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
209 } elsif ($hook eq "after-freeze") {
210 # savevm-end is async, we need to wait
212 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
213 if (!$stat->{bytes
}) {
216 print "savevm not yet finished\n";
225 sub __snapshot_create_vol_snapshot
{
226 my ($class, $vmid, $ds, $drive, $snapname) = @_;
228 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
230 my $volid = $drive->{file
};
231 my $device = "drive-$ds";
232 my $storecfg = PVE
::Storage
::config
();
234 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
237 sub __snapshot_delete_remove_drive
{
238 my ($class, $snap, $remove_drive) = @_;
240 if ($remove_drive eq 'vmstate') {
241 delete $snap->{$remove_drive};
243 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
244 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
246 my $volid = $drive->{file
};
247 delete $snap->{$remove_drive};
248 $class->add_unused_volume($snap, $volid);
252 sub __snapshot_delete_vmstate_file
{
253 my ($class, $snap, $force) = @_;
255 my $storecfg = PVE
::Storage
::config
();
257 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
264 sub __snapshot_delete_vol_snapshot
{
265 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
267 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
268 my $storecfg = PVE
::Storage
::config
();
269 my $volid = $drive->{file
};
270 my $device = "drive-$ds";
272 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
274 push @$unused, $volid;
277 sub __snapshot_rollback_vol_possible
{
278 my ($class, $drive, $snapname) = @_;
280 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
282 my $storecfg = PVE
::Storage
::config
();
283 my $volid = $drive->{file
};
285 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
288 sub __snapshot_rollback_vol_rollback
{
289 my ($class, $drive, $snapname) = @_;
291 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
293 my $storecfg = PVE
::Storage
::config
();
294 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
297 sub __snapshot_rollback_vm_stop
{
298 my ($class, $vmid) = @_;
300 my $storecfg = PVE
::Storage
::config
();
301 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
304 sub __snapshot_rollback_vm_start
{
305 my ($class, $vmid, $vmstate, $forcemachine) = @_;
307 my $storecfg = PVE
::Storage
::config
();
308 my $statefile = PVE
::Storage
::path
($storecfg, $vmstate);
309 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $statefile, undef, undef, undef, $forcemachine);
312 sub __snapshot_rollback_get_unused
{
313 my ($class, $conf, $snap) = @_;
317 $class->__snapshot_foreach_volume($conf, sub {
318 my ($vs, $volume) = @_;
320 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
323 my $volid = $volume->{file
};
325 $class->__snapshot_foreach_volume($snap, sub {
326 my ($ds, $drive) = @_;
329 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
332 if ($drive->{file
} && $drive->{file
} eq $volid);
335 push @$unused, $volid if !$found;
341 sub __snapshot_foreach_volume
{
342 my ($class, $conf, $func) = @_;
344 PVE
::QemuServer
::foreach_drive
($conf, $func);
346 # END implemented abstract methods from PVE::AbstractConfig