public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pve-devel] [PATCH storage v3] fix #4272: btrfs: add rename feature
@ 2024-07-04 12:02 Maximiliano Sandoval
  2024-07-04 14:47 ` Aaron Lauterer
  0 siblings, 1 reply; 5+ messages in thread
From: Maximiliano Sandoval @ 2024-07-04 12:02 UTC (permalink / raw)
  To: pve-devel

Adds the ability to change the owner of a guest image.

Btrfs does not need special commands to rename a subvolume and this can
be achieved the same as in Storage/plugin.pm's rename_volume taking
special care of how the directory structure used by Btrfs.

Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
---
Differences from v2:
 - use indices instead of assigning to undef 5 times

Differences from v1:
 - avoid assigning unused values of returned list to variables

 src/PVE/Storage/BTRFSPlugin.pm | 31 +++++++++++++++++++++++++++++++
 1 file changed, 31 insertions(+)

diff --git a/src/PVE/Storage/BTRFSPlugin.pm b/src/PVE/Storage/BTRFSPlugin.pm
index 42815cb..7376ae4 100644
--- a/src/PVE/Storage/BTRFSPlugin.pm
+++ b/src/PVE/Storage/BTRFSPlugin.pm
@@ -618,6 +618,9 @@ sub volume_has_feature {
 	    base => { qcow2 => 1, raw => 1, vmdk => 1 },
 	    current => { qcow2 => 1, raw => 1, vmdk => 1 },
 	},
+	rename => {
+	    current => { raw => 1 },
+	},
     };
 
     my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname);
@@ -930,4 +933,32 @@ sub volume_import {
     return "$storeid:$volname";
 }
 
+sub rename_volume {
+    my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
+    die "no path found\n" if !$scfg->{path};
+
+    my $format = ($class->parse_volname($source_volname))[6];
+
+    my $ppath = $class->filesystem_path($scfg, $source_volname);
+
+    $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 1)
+	if !$target_volname;
+    $target_volname = "$target_vmid/$target_volname";
+
+    my $basedir = $class->get_subdir($scfg, 'images');
+
+    mkpath "${basedir}/${target_vmid}";
+    my $source_dir = raw_name_to_dir($source_volname);
+    my $target_dir = raw_name_to_dir($target_volname);
+
+    my $old_path = "${basedir}/${source_dir}";
+    my $new_path = "${basedir}/${target_dir}";
+
+    die "target volume '${target_volname}' already exists\n" if -e $new_path;
+    rename $old_path, $new_path ||
+	die "rename '$old_path' to '$new_path' failed - $!\n";
+
+    return "${storeid}:$target_volname";
+}
+
 1
-- 
2.39.2



_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH storage v3] fix #4272: btrfs: add rename feature
  2024-07-04 12:02 [pve-devel] [PATCH storage v3] fix #4272: btrfs: add rename feature Maximiliano Sandoval
@ 2024-07-04 14:47 ` Aaron Lauterer
  2024-07-05 12:41   ` Maximiliano Sandoval
  2024-07-05 13:14   ` Maximiliano Sandoval
  0 siblings, 2 replies; 5+ messages in thread
From: Aaron Lauterer @ 2024-07-04 14:47 UTC (permalink / raw)
  To: Proxmox VE development discussion, Maximiliano Sandoval

gave it a try and it does what it should.
by enabling the rename feature only for `raw` we avoid potential 
pitfalls if we encounter a non regular situation on BTRFS. For example, 
an images/{vmid}/vm-{vmid}-disk-X.qcow2 file directly instead of the 
images/{vmid}/vm-{vmid}-disk-X/disk.raw as is the way the BTRFS plugin 
handles it in subvolumes.

But if we add the following diff, it seems to handle the case of a qcow2 
file in the same directory structure just fine:

diff --git a/src/PVE/Storage/BTRFSPlugin.pm b/src/PVE/Storage/BTRFSPlugin.pm
index 7376ae4..143442c 100644
--- a/src/PVE/Storage/BTRFSPlugin.pm
+++ b/src/PVE/Storage/BTRFSPlugin.pm
@@ -619,7 +619,7 @@ sub volume_has_feature {
             current => { qcow2 => 1, raw => 1, vmdk => 1 },
         },
         rename => {
-           current => { raw => 1 },
+           current => { qcow2 => 1, raw => 1},
         },
      };

@@ -939,6 +939,10 @@ sub rename_volume {

      my $format = ($class->parse_volname($source_volname))[6];

+    if ($format ne 'raw' && $format ne 'subvol') {
+       return $class->SUPER::rename_volume($scfg, $storeid, 
$source_volname, $target_vmid, $target_volname);
+    }
+
      my $ppath = $class->filesystem_path($scfg, $source_volname);

      $target_volname = $class->find_free_diskname($storeid, $scfg, 
$target_vmid, $format, 1)


Since we do have that in the other functions (alloc_image, free_image), 
we might want to add it here as well, just to be safe.

If we aren't concerned about this, then consider this:

Reviewed-By: Aaron Lauterer <a.lauterer@proxmox.com>
Tested-By: Aaron Lauterer <a.lauterer@proxmox.com>

On  2024-07-04  14:02, Maximiliano Sandoval wrote:
> Adds the ability to change the owner of a guest image.
> 
> Btrfs does not need special commands to rename a subvolume and this can
> be achieved the same as in Storage/plugin.pm's rename_volume taking
> special care of how the directory structure used by Btrfs.
> 
> Signed-off-by: Maximiliano Sandoval <m.sandoval@proxmox.com>
> ---
> Differences from v2:
>   - use indices instead of assigning to undef 5 times
> 
> Differences from v1:
>   - avoid assigning unused values of returned list to variables
> 
>   src/PVE/Storage/BTRFSPlugin.pm | 31 +++++++++++++++++++++++++++++++
>   1 file changed, 31 insertions(+)
> 
> diff --git a/src/PVE/Storage/BTRFSPlugin.pm b/src/PVE/Storage/BTRFSPlugin.pm
> index 42815cb..7376ae4 100644
> --- a/src/PVE/Storage/BTRFSPlugin.pm
> +++ b/src/PVE/Storage/BTRFSPlugin.pm
> @@ -618,6 +618,9 @@ sub volume_has_feature {
>   	    base => { qcow2 => 1, raw => 1, vmdk => 1 },
>   	    current => { qcow2 => 1, raw => 1, vmdk => 1 },
>   	},
> +	rename => {
> +	    current => { raw => 1 },
> +	},
>       };
>   
>       my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) = $class->parse_volname($volname);
> @@ -930,4 +933,32 @@ sub volume_import {
>       return "$storeid:$volname";
>   }
>   
> +sub rename_volume {
> +    my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
> +    die "no path found\n" if !$scfg->{path};
> +
> +    my $format = ($class->parse_volname($source_volname))[6];
> +
> +    my $ppath = $class->filesystem_path($scfg, $source_volname);
> +
> +    $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format, 1)
> +	if !$target_volname;
> +    $target_volname = "$target_vmid/$target_volname";
> +
> +    my $basedir = $class->get_subdir($scfg, 'images');
> +
> +    mkpath "${basedir}/${target_vmid}";
> +    my $source_dir = raw_name_to_dir($source_volname);
> +    my $target_dir = raw_name_to_dir($target_volname);
> +
> +    my $old_path = "${basedir}/${source_dir}";
> +    my $new_path = "${basedir}/${target_dir}";
> +
> +    die "target volume '${target_volname}' already exists\n" if -e $new_path;
> +    rename $old_path, $new_path ||
> +	die "rename '$old_path' to '$new_path' failed - $!\n";
> +
> +    return "${storeid}:$target_volname";
> +}
> +
>   1


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH storage v3] fix #4272: btrfs: add rename feature
  2024-07-04 14:47 ` Aaron Lauterer
@ 2024-07-05 12:41   ` Maximiliano Sandoval
  2024-07-05 12:57     ` Aaron Lauterer
  2024-07-05 13:14   ` Maximiliano Sandoval
  1 sibling, 1 reply; 5+ messages in thread
From: Maximiliano Sandoval @ 2024-07-05 12:41 UTC (permalink / raw)
  To: Aaron Lauterer; +Cc: Proxmox VE development discussion

Aaron Lauterer <a.lauterer@proxmox.com> writes:

> gave it a try and it does what it should.
> by enabling the rename feature only for `raw` we avoid potential pitfalls if we
> encounter a non regular situation on BTRFS. For example, an
> images/{vmid}/vm-{vmid}-disk-X.qcow2 file directly instead of the
> images/{vmid}/vm-{vmid}-disk-X/disk.raw as is the way the BTRFS plugin handles
> it in subvolumes.
>
> But if we add the following diff, it seems to handle the case of a qcow2 file in
> the same directory structure just fine:

Did you try it without your patch? I tested it here and it seemed to
work from my limited testing.

-- 
Maximiliano


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH storage v3] fix #4272: btrfs: add rename feature
  2024-07-05 12:41   ` Maximiliano Sandoval
@ 2024-07-05 12:57     ` Aaron Lauterer
  0 siblings, 0 replies; 5+ messages in thread
From: Aaron Lauterer @ 2024-07-05 12:57 UTC (permalink / raw)
  To: Maximiliano Sandoval; +Cc: Proxmox VE development discussion

Without my changes I get the following error when I try to do it with 
the manually places qcow2 file:

Storage does not support moving of this disk to another VM (500)

And if I enable qcow2 for the rename feature without adding the format 
check in the rename_volume function:

diff --git a/src/PVE/Storage/BTRFSPlugin.pm b/src/PVE/Storage/BTRFSPlugin.pm
index 7376ae4..56730d1 100644
--- a/src/PVE/Storage/BTRFSPlugin.pm
+++ b/src/PVE/Storage/BTRFSPlugin.pm
@@ -619,7 +619,7 @@ sub volume_has_feature {
             current => { qcow2 => 1, raw => 1, vmdk => 1 },
         },
         rename => {
-           current => { raw => 1 },
+           current => { qcow2 => 1, raw => 1 },
         },
      };

it fails with the following error:

internal error: bad disk name: 104/vm-104-disk-1.qcow2 at 
/usr/share/perl5/PVE/Storage/BTRFSPlugin.pm: 951


On  2024-07-05  14:41, Maximiliano Sandoval wrote:
> Aaron Lauterer <a.lauterer@proxmox.com> writes:
> 
>> gave it a try and it does what it should.
>> by enabling the rename feature only for `raw` we avoid potential pitfalls if we
>> encounter a non regular situation on BTRFS. For example, an
>> images/{vmid}/vm-{vmid}-disk-X.qcow2 file directly instead of the
>> images/{vmid}/vm-{vmid}-disk-X/disk.raw as is the way the BTRFS plugin handles
>> it in subvolumes.
>>
>> But if we add the following diff, it seems to handle the case of a qcow2 file in
>> the same directory structure just fine:
> 
> Did you try it without your patch? I tested it here and it seemed to
> work from my limited testing.
> 


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [pve-devel] [PATCH storage v3] fix #4272: btrfs: add rename feature
  2024-07-04 14:47 ` Aaron Lauterer
  2024-07-05 12:41   ` Maximiliano Sandoval
@ 2024-07-05 13:14   ` Maximiliano Sandoval
  1 sibling, 0 replies; 5+ messages in thread
From: Maximiliano Sandoval @ 2024-07-05 13:14 UTC (permalink / raw)
  To: Aaron Lauterer; +Cc: Proxmox VE development discussion

Aaron Lauterer <a.lauterer@proxmox.com> writes:

> gave it a try and it does what it should.
> by enabling the rename feature only for `raw` we avoid potential pitfalls if we
> encounter a non regular situation on BTRFS. For example, an
> images/{vmid}/vm-{vmid}-disk-X.qcow2 file directly instead of the
> images/{vmid}/vm-{vmid}-disk-X/disk.raw as is the way the BTRFS plugin handles
> it in subvolumes.
>
> But if we add the following diff, it seems to handle the case of a qcow2 file in
> the same directory structure just fine:
>
> diff --git a/src/PVE/Storage/BTRFSPlugin.pm b/src/PVE/Storage/BTRFSPlugin.pm
> index 7376ae4..143442c 100644
> --- a/src/PVE/Storage/BTRFSPlugin.pm
> +++ b/src/PVE/Storage/BTRFSPlugin.pm
> @@ -619,7 +619,7 @@ sub volume_has_feature {
>             current => { qcow2 => 1, raw => 1, vmdk => 1 },
>         },
>         rename => {
> -           current => { raw => 1 },
> +           current => { qcow2 => 1, raw => 1},
>         },
>      };
>
> @@ -939,6 +939,10 @@ sub rename_volume {
>
>      my $format = ($class->parse_volname($source_volname))[6];
>
> +    if ($format ne 'raw' && $format ne 'subvol') {
> +       return $class->SUPER::rename_volume($scfg, $storeid, $source_volname,
> $target_vmid, $target_volname);
> +    }
> +
>      my $ppath = $class->filesystem_path($scfg, $source_volname);
>
>      $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid,
>     $format, 1)
>
>
> Since we do have that in the other functions (alloc_image, free_image), we might
> want to add it here as well, just to be safe.
>
> If we aren't concerned about this, then consider this:
>
> Reviewed-By: Aaron Lauterer <a.lauterer@proxmox.com>
> Tested-By: Aaron Lauterer <a.lauterer@proxmox.com>

I added your suggestion on v4.

-- 
Maximiliano


_______________________________________________
pve-devel mailing list
pve-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-07-05 13:14 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-07-04 12:02 [pve-devel] [PATCH storage v3] fix #4272: btrfs: add rename feature Maximiliano Sandoval
2024-07-04 14:47 ` Aaron Lauterer
2024-07-05 12:41   ` Maximiliano Sandoval
2024-07-05 12:57     ` Aaron Lauterer
2024-07-05 13:14   ` Maximiliano Sandoval

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal