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 && 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 __snapshot_save_vmstate
{
67 my ($class, $vmid, $conf, $snapname, $storecfg) = @_;
69 my $snap = $conf->{snapshots
}->{$snapname};
73 # search shared storage first
74 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
76 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
77 return if !$scfg->{shared
};
79 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage
83 # now search local storage
84 PVE
::QemuServer
::foreach_writable_storage
($conf, sub {
86 my $scfg = PVE
::Storage
::storage_config
($storecfg, $sid);
87 return if $scfg->{shared
};
89 $target = $sid if !$target || $scfg->{path
}; # prefer file based storage;
93 $target = 'local' if !$target;
95 my $driver_state_size = 500; # assume 32MB is enough to safe all driver state;
96 # we abort live save after $conf->{memory}, so we need at max twice that space
97 my $size = $conf->{memory
}*2 + $driver_state_size;
99 my $name = "vm-$vmid-state-$snapname";
100 my $scfg = PVE
::Storage
::storage_config
($storecfg, $target);
101 $name .= ".raw" if $scfg->{path
}; # add filename extension for file base storage
102 $snap->{vmstate
} = PVE
::Storage
::vdisk_alloc
($storecfg, $target, $vmid, 'raw', $name, $size*1024);
103 # always overwrite machine if we save vmstate. This makes sure we
104 # can restore it later using correct machine type
105 $snap->{machine
} = PVE
::QemuServer
::get_current_qemu_machine
($vmid);
108 sub __snapshot_check_running
{
109 my ($class, $vmid) = @_;
110 return PVE
::QemuServer
::check_running
($vmid);
113 sub __snapshot_check_freeze_needed
{
114 my ($class, $vmid, $config, $save_vmstate) = @_;
116 my $running = $class->__snapshot_check_running($vmid);
117 if (!$save_vmstate) {
118 return ($running, $running && $config->{agent
} && PVE
::QemuServer
::qga_check_running
($vmid));
120 return ($running, 0);
124 sub __snapshot_freeze
{
125 my ($class, $vmid, $unfreeze) = @_;
128 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-thaw"); };
129 warn "guest-fsfreeze-thaw problems - $@" if $@;
131 eval { PVE
::QemuServer
::vm_mon_cmd
($vmid, "guest-fsfreeze-freeze"); };
132 warn "guest-fsfreeze-freeze problems - $@" if $@;
136 sub __snapshot_create_vol_snapshots_hook
{
137 my ($class, $vmid, $snap, $running, $hook) = @_;
140 my $storecfg = PVE
::Storage
::config
();
142 if ($hook eq "before") {
143 if ($snap->{vmstate
}) {
144 my $path = PVE
::Storage
::path
($storecfg, $snap->{vmstate
});
145 PVE
::Storage
::activate_volumes
($storecfg, [$snap->{vmstate
}]);
147 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start", statefile
=> $path);
149 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
150 if (!$stat->{status
}) {
151 die "savevm not active\n";
152 } elsif ($stat->{status
} eq 'active') {
155 } elsif ($stat->{status
} eq 'completed') {
158 die "query-savevm returned status '$stat->{status}'\n";
162 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-start");
164 } elsif ($hook eq "after") {
166 PVE
::QemuServer
::vm_mon_cmd
($vmid, "savevm-end");
167 PVE
::Storage
::deactivate_volumes
($storecfg, [$snap->{vmstate
}]) if $snap->{vmstate
};
170 } elsif ($hook eq "after-freeze") {
171 # savevm-end is async, we need to wait
173 my $stat = PVE
::QemuServer
::vm_mon_cmd_nocheck
($vmid, "query-savevm");
174 if (!$stat->{bytes
}) {
177 print "savevm not yet finished\n";
186 sub __snapshot_create_vol_snapshot
{
187 my ($class, $vmid, $ds, $drive, $snapname) = @_;
189 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
191 my $volid = $drive->{file
};
192 my $device = "drive-$ds";
193 my $storecfg = PVE
::Storage
::config
();
195 PVE
::QemuServer
::qemu_volume_snapshot
($vmid, $device, $storecfg, $volid, $snapname);
198 sub __snapshot_delete_remove_drive
{
199 my ($class, $snap, $remove_drive) = @_;
201 if ($remove_drive eq 'vmstate') {
202 delete $snap->{$remove_drive};
204 my $drive = PVE
::QemuServer
::parse_drive
($remove_drive, $snap->{$remove_drive});
205 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
207 my $volid = $drive->{file
};
208 delete $snap->{$remove_drive};
209 $class->add_unused_volume($snap, $volid);
213 sub __snapshot_delete_vmstate_file
{
214 my ($class, $snap, $force) = @_;
216 my $storecfg = PVE
::Storage
::config
();
218 eval { PVE
::Storage
::vdisk_free
($storecfg, $snap->{vmstate
}); };
225 sub __snapshot_delete_vol_snapshot
{
226 my ($class, $vmid, $ds, $drive, $snapname, $unused) = @_;
228 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
229 my $storecfg = PVE
::Storage
::config
();
230 my $volid = $drive->{file
};
231 my $device = "drive-$ds";
233 PVE
::QemuServer
::qemu_volume_snapshot_delete
($vmid, $device, $storecfg, $volid, $snapname);
235 push @$unused, $volid;
238 sub __snapshot_rollback_vol_possible
{
239 my ($class, $drive, $snapname) = @_;
241 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
243 my $storecfg = PVE
::Storage
::config
();
244 my $volid = $drive->{file
};
246 PVE
::Storage
::volume_rollback_is_possible
($storecfg, $volid, $snapname);
249 sub __snapshot_rollback_vol_rollback
{
250 my ($class, $drive, $snapname) = @_;
252 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
254 my $storecfg = PVE
::Storage
::config
();
255 PVE
::Storage
::volume_snapshot_rollback
($storecfg, $drive->{file
}, $snapname);
258 sub __snapshot_rollback_vm_stop
{
259 my ($class, $vmid) = @_;
261 my $storecfg = PVE
::Storage
::config
();
262 PVE
::QemuServer
::vm_stop
($storecfg, $vmid, undef, undef, 5, undef, undef);
265 sub __snapshot_rollback_vm_start
{
266 my ($class, $vmid, $vmstate, $forcemachine) = @_;
268 my $storecfg = PVE
::Storage
::config
();
269 my $statefile = PVE
::Storage
::path
($storecfg, $vmstate);
270 PVE
::QemuServer
::vm_start
($storecfg, $vmid, $statefile, undef, undef, undef, $forcemachine);
273 sub __snapshot_rollback_get_unused
{
274 my ($class, $conf, $snap) = @_;
278 $class->__snapshot_foreach_volume($conf, sub {
279 my ($vs, $volume) = @_;
281 return if PVE
::QemuServer
::drive_is_cdrom
($volume);
284 my $volid = $volume->{file
};
286 $class->__snapshot_foreach_volume($snap, sub {
287 my ($ds, $drive) = @_;
290 return if PVE
::QemuServer
::drive_is_cdrom
($drive);
293 if ($drive->{file
} && $drive->{file
} eq $volid);
296 push @$unused, $volid if !$found;
302 sub __snapshot_foreach_volume
{
303 my ($class, $conf, $func) = @_;
305 PVE
::QemuServer
::foreach_drive
($conf, $func);
307 # END implemented abstract methods from PVE::AbstractConfig