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 = storage_config
($storecfg, $storeid);
78 return if $scfg->{shared
};
80 return if $attr->{cdrom
};
82 return if !$cleanup && !$attr->{replicate
};
84 if (!PVE
::Storage
::volume_has_feature
($storecfg, 'replicate', $volid)) {
85 return if $cleanup || $noerr;
86 die "missing replicate feature on volume '$volid'\n";
89 $volhash->{$volid} = 1;
92 PVE
::QemuServer
::foreach_volid
($conf, $test_volid);
97 sub __snapshot_save_vmstate
{
98 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
100 my $snap = $conf->{snapshots
}->{$snapname};
104 # search shared storage first
105 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
107 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
108 return if !$scfg->{shared
};
110 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage
114 # now search local storage
115 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
117 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
118 return if $scfg->{shared
};
120 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage;
124 $target = 'local' if !$target;
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;
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);
139 sub __snapshot_check_running
{
140 my ($class, $vmid) = @_;
141 return PVE
::QemuServer
::check_running
($vmid);
144 sub __snapshot_check_freeze_needed
{
145 my ($class, $vmid, $config, $save_vmstate) = @_;
147 my $running = $class->__snapshot_check_running($vmid);
149 return ($running, $running && $config->{agent
} && PVE
::QemuServer
::qga_check_running
($vmid));
151 return ($running, 0);
155 sub __snapshot_freeze
{
156 my ($class, $vmid, $unfreeze) = @_;
159 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
160 warn "guest-fsfreeze-thaw problems - $@" if $@;
162 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
163 warn "guest-fsfreeze-freeze problems - $@" if $@;
167 sub __snapshot_create_vol_snapshots_hook
{
168 my ($class, $vmid, $snap, $running, $hook) = @_;
171 my $storecfg = PVE
::Storage
::config
();
173 if ($hook eq "before") {
174 if ($snap->{vmstate
}) {
175 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
176 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
178 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start", statefile
=> $path);
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') {
186 } elsif ($stat->{status
} eq 'completed') {
189 die "query-savevm returned status '$stat->{status}'\n";
193 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start");
195 } elsif ($hook eq "after") {
197 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-end");
198 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
201 } elsif ($hook eq "after-freeze") {
202 # savevm-end is async, we need to wait
204 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
205 if (!$stat->{bytes
}) {
208 print "savevm not yet finished\n";
217 sub __snapshot_create_vol_snapshot
{
218 my ($class, $vmid, $ds, $drive, $snapname) = @_;
220 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
222 my $volid = $drive->{file
};
223 my $device = "drive-$ds";
224 my $storecfg = PVE
::Storage
::config
();
226 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
229 sub __snapshot_delete_remove_drive
{
230 my ($class, $snap, $remove_drive) = @_;
232 if ($remove_drive eq 'vmstate') {
233 delete $snap->{$remove_drive};
235 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
236 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
238 my $volid = $drive->{file
};
239 delete $snap->{$remove_drive};
240 $class->add_unused_volume($snap, $volid);
244 sub __snapshot_delete_vmstate_file
{
245 my ($class, $snap, $force) = @_;
247 my $storecfg = PVE
::Storage
::config
();
249 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
256 sub __snapshot_delete_vol_snapshot
{
257 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
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";
264 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
266 push @$unused, $volid;
269 sub __snapshot_rollback_vol_possible
{
270 my ($class, $drive, $snapname) = @_;
272 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
274 my $storecfg = PVE
::Storage
::config
();
275 my $volid = $drive->{file
};
277 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
280 sub __snapshot_rollback_vol_rollback
{
281 my ($class, $drive, $snapname) = @_;
283 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
285 my $storecfg = PVE
::Storage
::config
();
286 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
289 sub __snapshot_rollback_vm_stop
{
290 my ($class, $vmid) = @_;
292 my $storecfg = PVE
::Storage
::config
();
293 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
296 sub __snapshot_rollback_vm_start
{
297 my ($class, $vmid, $vmstate, $forcemachine) = @_;
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);
304 sub __snapshot_rollback_get_unused
{
305 my ($class, $conf, $snap) = @_;
309 $class->__snapshot_foreach_volume($conf, sub {
310 my ($vs, $volume) = @_;
312 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
315 my $volid = $volume->{file
};
317 $class->__snapshot_foreach_volume($snap, sub {
318 my ($ds, $drive) = @_;
321 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
324 if ($drive->{file
} && $drive->{file
} eq $volid);
327 push @$unused, $volid if !$found;
333 sub __snapshot_foreach_volume
{
334 my ($class, $conf, $func) = @_;
336 PVE
::QemuServer
::foreach_drive
($conf, $func);
338 # END implemented abstract methods from PVE::AbstractConfig