[PATCH v2 qemu-server 1/1] implement external snapshot
Alexandre Derumier
alexandre.derumier at groupe-cyllene.com
Mon Sep 30 13:31:52 CEST 2024
Signed-off-by: Alexandre Derumier <alexandre.derumier at groupe-cyllene.com>
---
PVE/QemuServer.pm | 108 ++++++++++++++++++++++++++++++++++++++++------
1 file changed, 95 insertions(+), 13 deletions(-)
diff --git a/PVE/QemuServer.pm b/PVE/QemuServer.pm
index b26da505..1523df15 100644
--- a/PVE/QemuServer.pm
+++ b/PVE/QemuServer.pm
@@ -1549,7 +1549,11 @@ sub print_drive_commandline_full {
} else {
if ($storeid) {
$path = PVE::Storage::path($storecfg, $volid);
- $format //= qemu_img_format($scfg, $volname);
+ if ($scfg->{snapext}) {
+ $format //= qemu_img_format($scfg, $path);
+ } else {
+ $format //= qemu_img_format($scfg, $volname);
+ }
} else {
$path = $volid;
$format //= "raw";
@@ -4713,9 +4717,31 @@ sub qemu_volume_snapshot {
my ($vmid, $deviceid, $storecfg, $volid, $snap) = @_;
my $running = check_running($vmid);
+ my $do_snapshots_with_qemu = do_snapshots_with_qemu($storecfg, $volid, $deviceid) if $running;
+ if ($do_snapshots_with_qemu) {
+ if($do_snapshots_with_qemu == 2) {
+ my $snapshot_file = PVE::Storage::path($storecfg, $volid, $snap);
+ #allocate volume is external snapshot is a block device
+ my $snap_volid = undef;
+ if ($snapshot_file =~ m|^/dev/.+|) {
+ my ($storeid, $volname) = PVE::Storage::parse_volume_id($volid, 1);
+ my $size = PVE::Storage::volume_size_info($storecfg, $volid, 5);
+ #add 100M for qcow2 headers
+ $size = int($size/1024) + (100*1024);
+ my $snap_volname = $volname."-snap-$snap";
+ $snap_volid = PVE::Storage::vdisk_alloc($storecfg, $storeid, $vmid, 'raw', $snap_volname, $size);
+ PVE::Storage::activate_volumes($storecfg, [$snap_volid]);
+ }
+
+ eval { mon_cmd($vmid, 'blockdev-snapshot-sync', device => $deviceid, 'snapshot-file' => $snapshot_file, format => 'qcow2') };
- if ($running && do_snapshots_with_qemu($storecfg, $volid, $deviceid)) {
- mon_cmd($vmid, 'blockdev-snapshot-internal-sync', device => $deviceid, name => $snap);
+ if ($@) {
+ PVE::Storage::vdisk_free($storecfg, $snap_volid) if $snapshot_file =~ m|^/dev/.+|;
+ die $@;
+ }
+ } else {
+ mon_cmd($vmid, 'blockdev-snapshot-internal-sync', device => $deviceid, name => $snap);
+ }
} else {
PVE::Storage::volume_snapshot($storecfg, $volid, $snap);
}
@@ -4735,13 +4761,52 @@ sub qemu_volume_snapshot_delete {
});
}
- if ($attached_deviceid && do_snapshots_with_qemu($storecfg, $volid, $attached_deviceid)) {
- mon_cmd(
- $vmid,
- 'blockdev-snapshot-delete-internal-sync',
- device => $attached_deviceid,
- name => $snap,
- );
+ my $do_snapshots_with_qemu = do_snapshots_with_qemu($storecfg, $volid, $attached_deviceid) if $running;
+ if ($attached_deviceid && $do_snapshots_with_qemu) {
+
+ if ($do_snapshots_with_qemu == 2) {
+
+ my $snapshots = PVE::Storage::volume_snapshot_info($storecfg, $volid);
+
+ my $currentpath = $snapshots->{current}->{file};
+ my $snappath = $snapshots->{$snap}->{file};
+ my $snapvolid = $snapshots->{$snap}->{volid};
+ return if !$snappath; #already delete
+
+ my $parentsnap = $snapshots->{$snap}->{parent};
+ die "error: we can't find a parent for this snapshot" if !$parentsnap;
+
+ my $parentpath = $snapshots->{$parentsnap}->{file};
+ my $parentformat = $snapshots->{$parentsnap}->{'format'} if $parentsnap;
+
+ print "block-commit top:$snappath base:$parentpath\n";
+
+ my $job_id = "commit-$attached_deviceid";
+ my $jobs = {};
+ mon_cmd(
+ $vmid,
+ 'block-commit',
+ 'job-id' => $job_id,
+ device => $attached_deviceid,
+ top => $snappath,
+ base => $parentpath,
+ );
+ $jobs->{$job_id} = {};
+
+ #if we delete the current, block-job-complete to finish
+ my $completion = $currentpath eq $snappath ? 'complete' : 'auto';
+ qemu_drive_mirror_monitor($vmid, undef, $jobs, $completion, 0, 'commit');
+ #fixme. delete the disks when all jobs are ok ?
+ #delete the lvm volume
+ PVE::Storage::vdisk_free($storecfg, $snapvolid);
+ } else {
+ mon_cmd(
+ $vmid,
+ 'blockdev-snapshot-delete-internal-sync',
+ device => $attached_deviceid,
+ name => $snap,
+ );
+ }
} else {
PVE::Storage::volume_snapshot_delete(
$storecfg, $volid, $snap, $attached_deviceid ? 1 : undef);
@@ -7776,6 +7841,8 @@ sub do_snapshots_with_qemu {
return 1;
}
+ return 2 if $scfg->{snapext};
+
if ($volid =~ m/\.(qcow2|qed)$/){
return 1;
}
@@ -7849,8 +7916,23 @@ sub qemu_img_convert {
if ($src_storeid) {
PVE::Storage::activate_volumes($storecfg, [$src_volid], $snapname);
my $src_scfg = PVE::Storage::storage_config($storecfg, $src_storeid);
- $src_format = qemu_img_format($src_scfg, $src_volname);
- $src_path = PVE::Storage::path($storecfg, $src_volid, $snapname);
+ if($src_scfg->{snapext}) {
+ my $snapshots = PVE::Storage::volume_snapshot_info($storecfg, $src_volid);
+ $snapname = 'current' if !$snapname;
+ #if we don't clone the current image
+ #need to use the parent if available, as it's the readonly image view
+ #at the time of the snapshot
+ my $parentsnap = $snapshots->{$snapname}->{parent};
+ $snapname = $parentsnap if($parentsnap && $snapname ne 'current');
+ $src_format = $snapshots->{$snapname}->{format};
+ $src_path = $snapshots->{$snapname}->{file};
+ $src_volid = $snapshots->{$snapname}->{volid};
+ $snapname = undef;
+ PVE::Storage::activate_volumes($storecfg, [$src_volid], $snapname);
+ } else {
+ $src_format = qemu_img_format($src_scfg, $src_volname);
+ $src_path = PVE::Storage::path($storecfg, $src_volid, $snapname);
+ }
$src_is_iscsi = ($src_path =~ m|^iscsi://|);
$cachemode = 'none' if $src_scfg->{type} eq 'zfspool';
} elsif (-f $src_volid || -b $src_volid) {
@@ -7920,7 +8002,7 @@ sub qemu_img_format {
# FIXME: this entire function is kind of weird given that `parse_volname`
# also already gives us a format?
- my $is_path_storage = $scfg->{path} || $scfg->{type} eq 'esxi';
+ my $is_path_storage = $scfg->{path} || $scfg->{type} eq 'esxi' || $scfg->{snapext};
if ($is_path_storage && $volname =~ m/\.($PVE::QemuServer::Drive::QEMU_FORMAT_RE)$/) {
return $1;
--
2.39.2
More information about the pve-devel
mailing list