[pve-devel] [PATCH storage 2/9] add disk rename feature

Fabian Ebner f.ebner at proxmox.com
Thu Aug 12 14:51:04 CEST 2021


Am 06.08.21 um 15:46 schrieb Aaron Lauterer:
> Functionality has been added for the following storage types:
> 
> * directory ones, based on the default implementation:
>      * directory
>      * NFS
>      * CIFS
>      * gluster
> * ZFS
> * (thin) LVM
> * Ceph
> 
> A new feature `rename` has been introduced to mark which storage
> plugin supports the feature.
> 
> Version API and AGE have been bumped.
> 
> The storage gets locked and each plugin checks if the target volume
> already exists prior renaming.
> This is done because there could be a race condition from the time the
> external caller requests a new free disk name to the time the volume is
> actually renamed.
> 
> Signed-off-by: Aaron Lauterer <a.lauterer at proxmox.com>
> ---
> v1 -> v2:
> * many small fixes and improvements
> * rename_volume now accepts $source_volname instead of $source_volid
>      helps us to avoid to parse the volid a second time
> 
> rfc -> v1:
> * reduced number of parameters to minimum needed, plugins infer needed
>    information themselves
> * added storage locking and checking if volume already exists
> * parse target_volname prior to renaming to check if valid
> 
> old dedicated API endpoint -> rfc:
> only do rename now but the rename function handles templates and returns
> the new volid as this can be differently handled on some storages.
> 
> 
>   PVE/Storage.pm               | 20 +++++++++++++++--
>   PVE/Storage/LVMPlugin.pm     | 24 ++++++++++++++++++++
>   PVE/Storage/LvmThinPlugin.pm |  1 +
>   PVE/Storage/Plugin.pm        | 43 ++++++++++++++++++++++++++++++++++++
>   PVE/Storage/RBDPlugin.pm     | 28 +++++++++++++++++++++++
>   PVE/Storage/ZFSPoolPlugin.pm | 24 ++++++++++++++++++++
>   6 files changed, 138 insertions(+), 2 deletions(-)
> 
> diff --git a/PVE/Storage.pm b/PVE/Storage.pm
> index c38fe7b..2430991 100755
> --- a/PVE/Storage.pm
> +++ b/PVE/Storage.pm
> @@ -41,11 +41,11 @@ use PVE::Storage::PBSPlugin;
>   use PVE::Storage::BTRFSPlugin;
>   
>   # Storage API version. Increment it on changes in storage API interface.
> -use constant APIVER => 9;
> +use constant APIVER => 10;
>   # Age is the number of versions we're backward compatible with.
>   # This is like having 'current=APIVER' and age='APIAGE' in libtool,
>   # see https://www.gnu.org/software/libtool/manual/html_node/Libtool-versioning.html
> -use constant APIAGE => 0;
> +use constant APIAGE => 1;
>   
>   # load standard plugins
>   PVE::Storage::DirPlugin->register();
> @@ -358,6 +358,7 @@ sub volume_snapshot_needs_fsfreeze {
>   #            snapshot - taking a snapshot is possible
>   #            sparseinit - volume is sparsely initialized
>   #            template - conversion to base image is possible
> +#            rename - renaming volumes is possible
>   # $snap - check if the feature is supported for a given snapshot
>   # $running - if the guest owning the volume is running
>   # $opts - hash with further options:
> @@ -1866,6 +1867,21 @@ sub complete_volume {
>       return $res;
>   }
>   
> +sub rename_volume {
> +    my ($cfg, $source_volid, $target_volname) = @_;
> +
> +    my ($storeid, $source_volname) = parse_volume_id($source_volid);
> +
> +    activate_storage($cfg, $storeid);
> +
> +    my $scfg = storage_config($cfg, $storeid);
> +    my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
> +
> +    return $plugin->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
> +	return $plugin->rename_volume($scfg, $storeid, $source_volname, $target_volname);
> +    });
> +}
> +
>   # Various io-heavy operations require io/bandwidth limits which can be
>   # configured on multiple levels: The global defaults in datacenter.cfg, and
>   # per-storage overrides. When we want to do a restore from storage A to storage
> diff --git a/PVE/Storage/LVMPlugin.pm b/PVE/Storage/LVMPlugin.pm
> index 139d391..d28a94c 100644
> --- a/PVE/Storage/LVMPlugin.pm
> +++ b/PVE/Storage/LVMPlugin.pm
> @@ -339,6 +339,15 @@ sub lvcreate {
>       run_command($cmd, errmsg => "lvcreate '$vg/$name' error");
>   }
>   
> +sub lvrename {
> +    my ($vg, $oldname, $newname) = @_;
> +
> +    run_command(
> +	['/sbin/lvrename', $vg, $oldname, $newname],
> +	errmsg => "lvrename '${vg}/${oldname}' to '${newname}' error"

style nit: missing trailing comma

> +    );
> +}
> +
>   sub alloc_image {
>       my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
>   
> @@ -584,6 +593,7 @@ sub volume_has_feature {
>   
>       my $features = {
>   	copy => { base => 1, current => 1},
> +	rename => {current => 1},
>       };
>   
>       my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
> @@ -692,4 +702,18 @@ sub volume_import_write {
>   	input => '<&'.fileno($input_fh));
>   }
>   
> +sub rename_volume {
> +    my ($class, $scfg, $storeid, $source_volname, $target_volname) = @_;
> +
> +    $class->parse_volname($target_volname);
> +
> +    my $vg = $scfg->{vgname};
> +    my $lvs = lvm_list_volumes($vg);
> +    die "target volume '${target_volname}' already exists\n"
> +	if ($lvs->{$vg}->{$target_volname});
> +
> +    lvrename($vg, $source_volname, $target_volname);
> +    return "${storeid}:${target_volname}";
> +}
> +
>   1;
> diff --git a/PVE/Storage/LvmThinPlugin.pm b/PVE/Storage/LvmThinPlugin.pm
> index 4ba6f90..c24af22 100644
> --- a/PVE/Storage/LvmThinPlugin.pm
> +++ b/PVE/Storage/LvmThinPlugin.pm
> @@ -355,6 +355,7 @@ sub volume_has_feature {
>   	template => { current => 1},
>   	copy => { base => 1, current => 1, snap => 1},
>   	sparseinit => { base => 1, current => 1},
> +	rename => {current => 1},
>       };
>   
>       my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
> diff --git a/PVE/Storage/Plugin.pm b/PVE/Storage/Plugin.pm
> index e043329..7b75252 100644
> --- a/PVE/Storage/Plugin.pm
> +++ b/PVE/Storage/Plugin.pm
> @@ -971,6 +971,7 @@ sub volume_has_feature {
>   		  snap => {qcow2 => 1} },
>   	sparseinit => { base => {qcow2 => 1, raw => 1, vmdk => 1},
>   			current => {qcow2 => 1, raw => 1, vmdk => 1} },
> +	rename => { current => {qcow2 => 1, raw => 1, vmdk => 1} },
>       };
>   
>       # clone_image creates a qcow2 volume
> @@ -978,6 +979,14 @@ sub volume_has_feature {
>   		defined($opts->{valid_target_formats}) &&
>   		!(grep { $_ eq 'qcow2' } @{$opts->{valid_target_formats}});
>   
> +    if (
> +	$feature eq 'rename'
> +	&& $class->can('api')
> +	&& $class->api() < 10
> +    ) {
> +	return 0;
> +    }
> +
>       my ($vtype, $name, $vmid, $basename, $basevmid, $isBase, $format) =
>   	$class->parse_volname($volname);
>   
> @@ -1495,4 +1504,38 @@ sub volume_import_formats {
>       return ();
>   }
>   
> +sub rename_volume {
> +    my ($class, $scfg, $storeid, $source_volname, $target_volname) = @_;
> +    die "no path found\n" if !exists($scfg->{path});

Same nit as for the previous patch: using exists is not consistent with 
existing checks for $scfg->{path}.

> +    die "not implemented in storage plugin '$class'\n" if $class->can('api') && $class->api() < 10;
> +
> +    my (undef, undef, $target_vmid) = $class->parse_volname($target_volname);
> +
> +    my (
> +	undef,
> +	$source_name,
> +	$source_vmid,
> +	$base_name,
> +	$base_vmid,
> +	undef,
> +	$format
> +    ) = $class->parse_volname($source_volname);
> +
> +    my $basedir = $class->get_subdir($scfg, 'images');
> +
> +    mkpath "${basedir}/${target_vmid}";;

style nit: double semicolon

> +
> +    my $old_path = "${basedir}/${source_volname}";
> +    my $new_path = "${basedir}/${target_volname}";
> +
> +    die "target volume '${target_volname}' already exists\n" if -e $new_path;
> +
> +    my $base = $base_name ? "${base_vmid}/${base_name}/" : '';
> +
> +    rename($old_path, $new_path) ||
> +	die "rename '$old_path' to '$new_path' failed - $!\n";
> +

There is an issue when the source is a linked clone:
     # qm move-disk 123 scsi7 --target-vmid 122
     moving disk 'scsi7' from VM '123' to '122'
     rename 
'/mnt/pve/mycifs/images/116/base-116-disk-0.qcow2/123/vm-123-disk-0.qcow2' 
to '/mnt/pve/mycifs/images/122/vm-122-disk-0.qcow2' failed - Not a directory

> +    return "${storeid}:${base}${target_volname}";
> +}
> +
>   1;
> diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm
> index a8d1243..88350d2 100644
> --- a/PVE/Storage/RBDPlugin.pm
> +++ b/PVE/Storage/RBDPlugin.pm
> @@ -728,6 +728,7 @@ sub volume_has_feature {
>   	template => { current => 1},
>   	copy => { base => 1, current => 1, snap => 1},
>   	sparseinit => { base => 1, current => 1},
> +	rename => {current => 1},
>       };
>   
>       my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname);
> @@ -743,4 +744,31 @@ sub volume_has_feature {
>       return undef;
>   }
>   
> +sub rename_volume {
> +    my ($class, $scfg, $storeid, $source_volname, $target_volname) = @_;
> +
> +    $class->parse_volname($target_volname);
> +
> +    my (undef, undef, $source_vmid, $base_name) = $class->parse_volname($source_volname);
> +
> +    eval {
> +	my $cmd = $rbd_cmd->($scfg, $storeid, 'info', $target_volname);
> +	run_rbd_command($cmd, errmsg => "exist check",  quiet => 1, noerr => 1);
> +    };
> +    my $err = $@;
> +    die "target volume '${target_volname}' already exists\n"
> +	if !$err;
> +
> +    my $cmd = $rbd_cmd->($scfg, $storeid, 'rename', $source_volname, $target_volname);
> +
> +    run_rbd_command(
> +	$cmd,
> +	errmsg => "could not rename image '${source_volname}' to '${target_volname}'",
> +    );

There is an issue when the source is a linked clone:
     # qm move-disk 123 scsi6 --target-vmid 122
     moving disk 'scsi6' from VM '123' to '122'
     could not rename image 'base-116-disk-0/vm-123-disk-0' to 
'vm-122-disk-0': rbd: error opening pool 'base-116-disk-0': (2) No such 
file or directory


> +
> +    $base_name = $base_name ? "${base_name}/" : '';
> +
> +    return "${storeid}:${base_name}${target_volname}";
> +}
> +
>   1;
> diff --git a/PVE/Storage/ZFSPoolPlugin.pm b/PVE/Storage/ZFSPoolPlugin.pm
> index c4be70f..543eef9 100644
> --- a/PVE/Storage/ZFSPoolPlugin.pm
> +++ b/PVE/Storage/ZFSPoolPlugin.pm
> @@ -687,6 +687,7 @@ sub volume_has_feature {
>   	copy => { base => 1, current => 1},
>   	sparseinit => { base => 1, current => 1},
>   	replicate => { base => 1, current => 1},
> +	rename => {current => 1},
>       };
>   
>       my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
> @@ -789,4 +790,27 @@ sub volume_import_formats {
>       return $class->volume_export_formats($scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots);
>   }
>   
> +sub rename_volume {
> +    my ($class, $scfg, $storeid, $source_volname, $target_volname) = @_;
> +
> +    print "parsing volname: $target_volname\n";

debug print?

> +    $class->parse_volname($target_volname);
> +
> +    my (undef, undef, $source_vmid, $base_name) = $class->parse_volname($source_volname);

Also has an issue when the source is a linked clone:
     # qm move-disk 123 scsi5 --target-vmid 122
     moving disk 'scsi5' from VM '123' to '122'
     parsing volname: vm-122-disk-0
     zfs error: cannot open 'myzpool/base-116-disk-0/vm-123-disk-0': 
dataset does not exist


> +
> +    my $pool = $scfg->{pool};
> +    my $source_zfspath = "${pool}/${source_volname}";
> +    my $target_zfspath = "${pool}/${target_volname}";
> +
> +    my $exists = 0 == run_command(['zfs', 'get', '-H', 'name', $target_zfspath],
> +				  noerr => 1, quiet => 1);
> +    die "target volume '${target_volname}' already exists\n" if $exists;
> +
> +    $class->zfs_request($scfg, 5, 'rename', ${source_zfspath}, ${target_zfspath});
> +
> +    $base_name = $base_name ? "${base_name}/" : '';
> +
> +    return "${storeid}:${base_name}${target_volname}";
> +}
> +
>   1;
> 





More information about the pve-devel mailing list