auto-format code using perltidy with Proxmox style guide

using the new top-level `make tidy` target, which calls perltidy via
our wrapper to enforce the desired style as closely as possible.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
This commit is contained in:
Thomas Lamprecht
2025-06-11 10:03:21 +02:00
parent 5d23073cb6
commit 5a66c27cc6
54 changed files with 14137 additions and 12461 deletions

View File

@ -10,7 +10,7 @@ use Net::IP;
use POSIX qw(ceil);
use PVE::CephConfig;
use PVE::Cluster qw(cfs_read_file);;
use PVE::Cluster qw(cfs_read_file);
use PVE::JSONSchema qw(get_standard_option);
use PVE::ProcFSTools;
use PVE::RADOS;
@ -32,7 +32,7 @@ my $librados_connect = sub {
my ($scfg, $storeid, $options) = @_;
$options->{timeout} = 60
if !defined($options->{timeout}) && PVE::RPCEnvironment->is_worker();
if !defined($options->{timeout}) && PVE::RPCEnvironment->is_worker();
my $librados_config = PVE::CephConfig::ceph_connect_option($scfg, $storeid, $options->%*);
@ -47,27 +47,27 @@ my sub get_rbd_path {
$path .= "/$scfg->{namespace}" if defined($scfg->{namespace});
$path .= "/$volume" if defined($volume);
return $path;
};
}
my sub get_rbd_dev_path {
my ($scfg, $storeid, $volume) = @_;
my $cluster_id = '';
if ($scfg->{fsid}) {
# NOTE: the config doesn't support this currently (but it could!), hack for qemu-server tests
$cluster_id = $scfg->{fsid};
# NOTE: the config doesn't support this currently (but it could!), hack for qemu-server tests
$cluster_id = $scfg->{fsid};
} elsif ($scfg->{monhost}) {
my $rados = $librados_connect->($scfg, $storeid);
$cluster_id = $rados->mon_command({ prefix => 'fsid', format => 'json' })->{fsid};
my $rados = $librados_connect->($scfg, $storeid);
$cluster_id = $rados->mon_command({ prefix => 'fsid', format => 'json' })->{fsid};
} else {
$cluster_id = cfs_read_file('ceph.conf')->{global}->{fsid};
$cluster_id = cfs_read_file('ceph.conf')->{global}->{fsid};
}
my $uuid_pattern = "([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})";
if ($cluster_id =~ qr/^${uuid_pattern}$/is) {
$cluster_id = $1; # use untained value
$cluster_id = $1; # use untained value
} else {
die "cluster fsid has invalid format\n";
die "cluster fsid has invalid format\n";
}
my $rbd_path = get_rbd_path($scfg, $volume);
@ -75,11 +75,11 @@ my sub get_rbd_dev_path {
my $path = "/dev/rbd/${rbd_path}";
if (!-e $pve_path && -e $path) {
# possibly mapped before rbd-pve rule existed
my $real_dev = abs_path($path);
my ($rbd_id) = ($real_dev =~ m|/dev/rbd([0-9]+)$|);
my $dev_cluster_id = file_read_firstline("/sys/devices/rbd/${rbd_id}/cluster_fsid");
return $path if $cluster_id eq $dev_cluster_id;
# possibly mapped before rbd-pve rule existed
my $real_dev = abs_path($path);
my ($rbd_id) = ($real_dev =~ m|/dev/rbd([0-9]+)$|);
my $dev_cluster_id = file_read_firstline("/sys/devices/rbd/${rbd_id}/cluster_fsid");
return $path if $cluster_id eq $dev_cluster_id;
}
return $pve_path;
}
@ -88,25 +88,26 @@ my $rbd_cmd = sub {
my ($scfg, $storeid, $op, @options) = @_;
my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd';
my $cmd = ['/usr/bin/rbd'];
if ($op eq 'import') {
push $cmd->@*, '--dest-pool', $pool;
push $cmd->@*, '--dest-pool', $pool;
} else {
push $cmd->@*, '-p', $pool;
push $cmd->@*, '-p', $pool;
}
if (defined(my $namespace = $scfg->{namespace})) {
# some subcommands will fail if the --namespace parameter is present
my $no_namespace_parameter = {
unmap => 1,
};
push @$cmd, '--namespace', "$namespace" if !$no_namespace_parameter->{$op};
# some subcommands will fail if the --namespace parameter is present
my $no_namespace_parameter = {
unmap => 1,
};
push @$cmd, '--namespace', "$namespace" if !$no_namespace_parameter->{$op};
}
push @$cmd, '-c', $cmd_option->{ceph_conf} if ($cmd_option->{ceph_conf});
push @$cmd, '-m', $cmd_option->{mon_host} if ($cmd_option->{mon_host});
push @$cmd, '--auth_supported', $cmd_option->{auth_supported} if ($cmd_option->{auth_supported});
push @$cmd, '--auth_supported', $cmd_option->{auth_supported}
if ($cmd_option->{auth_supported});
push @$cmd, '-n', "client.$cmd_option->{userid}" if ($cmd_option->{userid});
push @$cmd, '--keyring', $cmd_option->{keyring} if ($cmd_option->{keyring});
@ -125,42 +126,45 @@ my $krbd_feature_update = sub {
my ($kmajor, $kminor) = PVE::ProcFSTools::kernel_version();
if ($kmajor > 5 || $kmajor == 5 && $kminor >= 3) {
# 'deep-flatten' can only be disabled, not enabled after image creation
push @enable, 'fast-diff', 'object-map';
# 'deep-flatten' can only be disabled, not enabled after image creation
push @enable, 'fast-diff', 'object-map';
} else {
push @disable, 'fast-diff', 'object-map', 'deep-flatten';
push @disable, 'fast-diff', 'object-map', 'deep-flatten';
}
if ($kmajor >= 5) {
push @enable, 'exclusive-lock';
push @enable, 'exclusive-lock';
} else {
push @disable, 'exclusive-lock';
push @disable, 'exclusive-lock';
}
my $active_features_list = (rbd_volume_info($scfg, $storeid, $name))[4];
my $active_features = { map { $_ => 1 } @$active_features_list };
my $to_disable = join(',', grep { $active_features->{$_} } @disable);
my $to_enable = join(',', grep { !$active_features->{$_} } @enable );
my $to_disable = join(',', grep { $active_features->{$_} } @disable);
my $to_enable = join(',', grep { !$active_features->{$_} } @enable);
if ($to_disable) {
print "disable RBD image features this kernel RBD drivers is not compatible with: $to_disable\n";
my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
run_rbd_command(
$cmd,
errmsg => "could not disable krbd-incompatible image features '$to_disable' for rbd image: $name",
);
print
"disable RBD image features this kernel RBD drivers is not compatible with: $to_disable\n";
my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'disable', $name, $to_disable);
run_rbd_command(
$cmd,
errmsg =>
"could not disable krbd-incompatible image features '$to_disable' for rbd image: $name",
);
}
if ($to_enable) {
print "enable RBD image features this kernel RBD drivers supports: $to_enable\n";
eval {
my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
run_rbd_command(
$cmd,
errmsg => "could not enable krbd-compatible image features '$to_enable' for rbd image: $name",
);
};
warn "$@" if $@;
print "enable RBD image features this kernel RBD drivers supports: $to_enable\n";
eval {
my $cmd = $rbd_cmd->($scfg, $storeid, 'feature', 'enable', $name, $to_enable);
run_rbd_command(
$cmd,
errmsg =>
"could not enable krbd-compatible image features '$to_enable' for rbd image: $name",
);
};
warn "$@" if $@;
}
};
@ -170,24 +174,26 @@ sub run_rbd_command {
my $lasterr;
my $errmsg = $args{errmsg} . ": " || "";
if (!exists($args{errfunc})) {
# ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
# at least 1 child(ren) in pool cephstor1
$args{errfunc} = sub {
my $line = shift;
if ($line =~ m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/) {
$lasterr = "$1\n";
} else {
$lasterr = $line;
}
print STDERR $lasterr;
*STDERR->flush();
};
# ' error: 2014-02-06 11:51:59.839135 7f09f94d0760 -1 librbd: snap_unprotect: can't unprotect;
# at least 1 child(ren) in pool cephstor1
$args{errfunc} = sub {
my $line = shift;
if ($line =~
m/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}\.\d+ [0-9a-f]+ [\-\d]+ librbd: (.*)$/
) {
$lasterr = "$1\n";
} else {
$lasterr = $line;
}
print STDERR $lasterr;
*STDERR->flush();
};
}
eval { run_command($cmd, %args); };
if (my $err = $@) {
die $errmsg . $lasterr if length($lasterr);
die $err;
die $errmsg . $lasterr if length($lasterr);
die $err;
}
return undef;
@ -200,33 +206,33 @@ sub rbd_ls {
my $parser = sub { $raw .= shift };
my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '-l', '--format', 'json');
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub { }, outfunc => $parser);
my $result;
if ($raw eq '') {
$result = [];
$result = [];
} elsif ($raw =~ m/^(\[.*\])$/s) { # untaint
$result = JSON::decode_json($1);
$result = JSON::decode_json($1);
} else {
die "got unexpected data from rbd ls: '$raw'\n";
die "got unexpected data from rbd ls: '$raw'\n";
}
my $list = {};
foreach my $el (@$result) {
next if defined($el->{snapshot});
next if defined($el->{snapshot});
my $image = $el->{image};
my $image = $el->{image};
my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
next if !defined($owner);
my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/;
next if !defined($owner);
$list->{$image} = {
name => $image,
size => $el->{size},
parent => $get_parent_image_name->($el->{parent}),
vmid => $owner
};
$list->{$image} = {
name => $image,
size => $el->{size},
parent => $get_parent_image_name->($el->{parent}),
vmid => $owner,
};
}
return $list;
@ -238,28 +244,33 @@ sub rbd_ls_snap {
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'ls', $name, '--format', 'json');
my $raw = '';
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => sub { $raw .= shift; });
run_rbd_command(
$cmd,
errmsg => "rbd error",
errfunc => sub { },
outfunc => sub { $raw .= shift; },
);
my $list;
if ($raw =~ m/^(\[.*\])$/s) { # untaint
$list = eval { JSON::decode_json($1) };
die "invalid JSON output from 'rbd snap ls $name': $@\n" if $@;
$list = eval { JSON::decode_json($1) };
die "invalid JSON output from 'rbd snap ls $name': $@\n" if $@;
} else {
die "got unexpected data from 'rbd snap ls $name': '$raw'\n";
die "got unexpected data from 'rbd snap ls $name': '$raw'\n";
}
$list = [] if !defined($list);
my $res = {};
foreach my $el (@$list) {
my $snap = $el->{name};
my $protected = defined($el->{protected}) && $el->{protected} eq "true" ? 1 : undef;
$res->{$snap} = {
name => $snap,
id => $el->{id} // undef,
size => $el->{size} // 0,
protected => $protected,
};
my $snap = $el->{name};
my $protected = defined($el->{protected}) && $el->{protected} eq "true" ? 1 : undef;
$res->{$snap} = {
name => $snap,
id => $el->{id} // undef,
size => $el->{size} // 0,
protected => $protected,
};
}
return $res;
}
@ -271,7 +282,7 @@ sub rbd_volume_info {
my @options = ('info', $volname, '--format', 'json');
if ($snap) {
push @options, '--snap', $snap;
push @options, '--snap', $snap;
}
$cmd = $rbd_cmd->($scfg, $storeid, @options);
@ -279,19 +290,20 @@ sub rbd_volume_info {
my $raw = '';
my $parser = sub { $raw .= shift };
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub { }, outfunc => $parser);
my $volume;
if ($raw eq '') {
$volume = {};
$volume = {};
} elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
$volume = JSON::decode_json($1);
$volume = JSON::decode_json($1);
} else {
die "got unexpected data from rbd info: '$raw'\n";
die "got unexpected data from rbd info: '$raw'\n";
}
$volume->{parent} = $get_parent_image_name->($volume->{parent});
$volume->{protected} = defined($volume->{protected}) && $volume->{protected} eq "true" ? 1 : undef;
$volume->{protected} =
defined($volume->{protected}) && $volume->{protected} eq "true" ? 1 : undef;
return $volume->@{qw(size parent format protected features)};
}
@ -305,31 +317,31 @@ sub rbd_volume_du {
my $raw = '';
my $parser = sub { $raw .= shift };
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub { }, outfunc => $parser);
my $volume;
if ($raw eq '') {
$volume = {};
$volume = {};
} elsif ($raw =~ m/^(\{.*\})$/s) { # untaint
$volume = JSON::decode_json($1);
$volume = JSON::decode_json($1);
} else {
die "got unexpected data from rbd du: '$raw'\n";
die "got unexpected data from rbd du: '$raw'\n";
}
if (!defined($volume->{images})) {
die "got no images from rbd du\n";
die "got no images from rbd du\n";
}
# `rbd du` returns array of images for name matching `volname`,
# including snapshots.
my $images = $volume->{images};
foreach my $image (@$images) {
next if defined($image->{snapshot});
next if !defined($image->{used_size}) || !defined($image->{name});
next if defined($image->{snapshot});
next if !defined($image->{used_size}) || !defined($image->{name});
# Return `used_size` of first volume with matching name which
# is not a snapshot.
return $image->{used_size} if $image->{name} eq $volname;
# Return `used_size` of first volume with matching name which
# is not a snapshot.
return $image->{used_size} if $image->{name} eq $volname;
}
die "got no matching image from rbd du\n";
@ -341,18 +353,22 @@ my sub rbd_volume_exists {
my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '--format', 'json');
my $raw = '';
run_rbd_command(
$cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => sub { $raw .= shift; });
$cmd,
errmsg => "rbd error",
errfunc => sub { },
outfunc => sub { $raw .= shift; },
);
my $list;
if ($raw =~ m/^(\[.*\])$/s) { # untaint
$list = eval { JSON::decode_json($1); };
die "invalid JSON output from 'rbd ls': $@\n" if $@;
$list = eval { JSON::decode_json($1); };
die "invalid JSON output from 'rbd ls': $@\n" if $@;
} else {
die "got unexpected data from 'rbd ls': '$raw'\n";
die "got unexpected data from 'rbd ls': '$raw'\n";
}
for my $name ($list->@*) {
return 1 if $name eq $volname;
return 1 if $name eq $volname;
}
return 0;
@ -366,62 +382,63 @@ sub type {
sub plugindata {
return {
content => [ {images => 1, rootdir => 1}, { images => 1 }],
'sensitive-properties' => { keyring => 1 },
content => [{ images => 1, rootdir => 1 }, { images => 1 }],
'sensitive-properties' => { keyring => 1 },
};
}
sub properties {
return {
monhost => {
description => "IP addresses of monitors (for external clusters).",
type => 'string', format => 'pve-storage-portal-dns-list',
},
pool => {
description => "Pool.",
type => 'string',
},
'data-pool' => {
description => "Data Pool (for erasure coding only)",
type => 'string',
},
namespace => {
description => "Namespace.",
type => 'string',
},
username => {
description => "RBD Id.",
type => 'string',
},
authsupported => {
description => "Authsupported.",
type => 'string',
},
krbd => {
description => "Always access rbd through krbd kernel module.",
type => 'boolean',
default => 0,
},
keyring => {
description => "Client keyring contents (for external clusters).",
type => 'string',
},
monhost => {
description => "IP addresses of monitors (for external clusters).",
type => 'string',
format => 'pve-storage-portal-dns-list',
},
pool => {
description => "Pool.",
type => 'string',
},
'data-pool' => {
description => "Data Pool (for erasure coding only)",
type => 'string',
},
namespace => {
description => "Namespace.",
type => 'string',
},
username => {
description => "RBD Id.",
type => 'string',
},
authsupported => {
description => "Authsupported.",
type => 'string',
},
krbd => {
description => "Always access rbd through krbd kernel module.",
type => 'boolean',
default => 0,
},
keyring => {
description => "Client keyring contents (for external clusters).",
type => 'string',
},
};
}
sub options {
return {
nodes => { optional => 1 },
disable => { optional => 1 },
monhost => { optional => 1},
pool => { optional => 1 },
'data-pool' => { optional => 1 },
namespace => { optional => 1 },
username => { optional => 1 },
content => { optional => 1 },
krbd => { optional => 1 },
keyring => { optional => 1 },
bwlimit => { optional => 1 },
nodes => { optional => 1 },
disable => { optional => 1 },
monhost => { optional => 1 },
pool => { optional => 1 },
'data-pool' => { optional => 1 },
namespace => { optional => 1 },
username => { optional => 1 },
content => { optional => 1 },
krbd => { optional => 1 },
keyring => { optional => 1 },
bwlimit => { optional => 1 },
};
}
@ -439,11 +456,11 @@ sub on_update_hook {
my ($class, $storeid, $scfg, %param) = @_;
if (exists($param{keyring})) {
if (defined($param{keyring})) {
PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring});
} else {
PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
}
if (defined($param{keyring})) {
PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $param{keyring});
} else {
PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid);
}
}
return;
@ -459,7 +476,7 @@ sub parse_volname {
my ($class, $volname) = @_;
if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) {
return ('images', $4, $7, $2, $3, $5, 'raw');
return ('images', $4, $7, $2, $3, $5, 'raw');
}
die "unable to parse rbd volume name '$volname'\n";
@ -470,11 +487,11 @@ sub path {
my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid);
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
$name .= '@'.$snapname if $snapname;
$name .= '@' . $snapname if $snapname;
if ($scfg->{krbd}) {
my $rbd_dev_path = get_rbd_dev_path($scfg, $storeid, $name);
return ($rbd_dev_path, $vmid, $vtype);
my $rbd_dev_path = get_rbd_dev_path($scfg, $storeid, $name);
return ($rbd_dev_path, $vmid, $vtype);
}
my $rbd_path = get_rbd_path($scfg, $name);
@ -482,10 +499,10 @@ sub path {
$path .= ":conf=$cmd_option->{ceph_conf}" if $cmd_option->{ceph_conf};
if (defined($scfg->{monhost})) {
my $monhost = PVE::CephConfig::hostlist($scfg->{monhost}, ';');
$monhost =~ s/:/\\:/g;
$path .= ":mon_host=$monhost";
$path .= ":auth_supported=$cmd_option->{auth_supported}";
my $monhost = PVE::CephConfig::hostlist($scfg->{monhost}, ';');
$monhost =~ s/:/\\:/g;
$path .= ":mon_host=$monhost";
$path .= ":auth_supported=$cmd_option->{auth_supported}";
}
$path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring});
@ -501,14 +518,14 @@ sub find_free_diskname {
my $disk_list = [];
my $parser = sub {
my $line = shift;
if ($line =~ m/^(.*)$/) { # untaint
push @$disk_list, $1;
}
my $line = shift;
if ($line =~ m/^(.*)$/) { # untaint
push @$disk_list, $1;
}
};
eval {
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser);
run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub { }, outfunc => $parser);
};
my $err = $@;
@ -522,8 +539,7 @@ sub create_base {
my $snap = '__base__';
my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) =
$class->parse_volname($volname);
my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname);
die "create_base not possible with base image\n" if $isBase;
@ -533,7 +549,7 @@ sub create_base {
die "rbd image must be at format V2" if $format ne "2";
die "volname '$volname' contains wrong information about parent $parent $basename\n"
if $basename && (!$parent || $parent ne $basename."@".$snap);
if $basename && (!$parent || $parent ne $basename . "@" . $snap);
my $newname = $name;
$newname =~ s/^vm-/base-/;
@ -541,26 +557,24 @@ sub create_base {
my $newvolname = $basename ? "$basename/$newname" : "$newname";
my $cmd = $rbd_cmd->(
$scfg,
$storeid,
'rename',
get_rbd_path($scfg, $name),
get_rbd_path($scfg, $newname),
$scfg, $storeid, 'rename',
get_rbd_path($scfg, $name),
get_rbd_path($scfg, $newname),
);
run_rbd_command($cmd, errmsg => "rbd rename '$name' error");
eval { $class->unmap_volume($storeid, $scfg, $volname); };
warn $@ if $@;
my $running = undef; #fixme : is create_base always offline ?
my $running = undef; #fixme : is create_base always offline ?
$class->volume_snapshot($scfg, $storeid, $newname, $snap, $running);
my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $newname, $snap);
if (!$protected){
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
if (!$protected) {
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap);
run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error");
}
return $newvolname;
@ -573,31 +587,30 @@ sub clone_image {
my $snap = '__base__';
$snap = $snapname if length $snapname;
my ($vtype, $basename, $basevmid, undef, undef, $isBase) =
$class->parse_volname($volname);
my ($vtype, $basename, $basevmid, undef, undef, $isBase) = $class->parse_volname($volname);
die "$volname is not a base image and snapname is not provided\n"
if !$isBase && !length($snapname);
die "$volname is not a base image and snapname is not provided\n"
if !$isBase && !length($snapname);
my $name = $class->find_free_diskname($storeid, $scfg, $vmid);
warn "clone $volname: $basename snapname $snap to $name\n";
if (length($snapname)) {
my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $volname, $snapname);
my (undef, undef, undef, $protected) =
rbd_volume_info($scfg, $storeid, $volname, $snapname);
if (!$protected) {
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
}
if (!$protected) {
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname);
run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error");
}
}
my $newvol = "$basename/$name";
$newvol = $name if length($snapname);
my @options = (
get_rbd_path($scfg, $basename),
'--snap', $snap,
get_rbd_path($scfg, $basename), '--snap', $snap,
);
push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
@ -610,15 +623,13 @@ sub clone_image {
sub alloc_image {
my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
die "illegal name '$name' - should be 'vm-$vmid-*'\n"
if $name && $name !~ m/^vm-$vmid-/;
if $name && $name !~ m/^vm-$vmid-/;
$name = $class->find_free_diskname($storeid, $scfg, $vmid) if !$name;
my @options = (
'--image-format' , 2,
'--size', int(($size + 1023) / 1024),
'--image-format', 2, '--size', int(($size + 1023) / 1024),
);
push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'};
@ -631,21 +642,19 @@ sub alloc_image {
sub free_image {
my ($class, $storeid, $scfg, $volname, $isBase) = @_;
my ($vtype, $name, $vmid, undef, undef, undef) =
$class->parse_volname($volname);
my ($vtype, $name, $vmid, undef, undef, undef) = $class->parse_volname($volname);
my $snaps = rbd_ls_snap($scfg, $storeid, $name);
foreach my $snap (keys %$snaps) {
if ($snaps->{$snap}->{protected}) {
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
}
if ($snaps->{$snap}->{protected}) {
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
}
}
$class->deactivate_volume($storeid, $scfg, $volname);
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'purge', $name);
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'purge', $name);
run_rbd_command($cmd, errmsg => "rbd snap purge '$name' error");
$cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name);
@ -662,25 +671,25 @@ sub list_images {
my $res = [];
for my $image (sort keys %$dat) {
my $info = $dat->{$image};
my ($volname, $parent, $owner) = $info->@{'name', 'parent', 'vmid'};
my $info = $dat->{$image};
my ($volname, $parent, $owner) = $info->@{ 'name', 'parent', 'vmid' };
if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
$info->{volid} = "$storeid:$1/$volname";
} else {
$info->{volid} = "$storeid:$volname";
}
if ($parent && $parent =~ m/^(base-\d+-\S+)\@__base__$/) {
$info->{volid} = "$storeid:$1/$volname";
} else {
$info->{volid} = "$storeid:$volname";
}
if ($vollist) {
my $found = grep { $_ eq $info->{volid} } @$vollist;
next if !$found;
} else {
next if defined ($vmid) && ($owner ne $vmid);
}
if ($vollist) {
my $found = grep { $_ eq $info->{volid} } @$vollist;
next if !$found;
} else {
next if defined($vmid) && ($owner ne $vmid);
}
$info->{format} = 'raw';
$info->{format} = 'raw';
push @$res, $info;
push @$res, $info;
}
return $res;
@ -694,11 +703,11 @@ sub status {
my $pool = $scfg->{'data-pool'} // $scfg->{pool} // 'rbd';
my ($d) = grep { $_->{name} eq $pool } @{$df->{pools}};
my ($d) = grep { $_->{name} eq $pool } @{ $df->{pools} };
if (!defined($d)) {
warn "could not get usage stats for pool '$pool'\n";
return;
warn "could not get usage stats for pool '$pool'\n";
return;
}
# max_avail -> max available space for data w/o replication in the pool
@ -727,7 +736,7 @@ sub map_volume {
my ($vtype, $img_name, $vmid) = $class->parse_volname($volname);
my $name = $img_name;
$name .= '@'.$snapname if $snapname;
$name .= '@' . $snapname if $snapname;
my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
@ -746,13 +755,13 @@ sub unmap_volume {
my ($class, $storeid, $scfg, $volname, $snapname) = @_;
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
$name .= '@'.$snapname if $snapname;
$name .= '@' . $snapname if $snapname;
my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name);
if (-b $kerneldev) {
my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
run_rbd_command($cmd, errmsg => "can't unmap rbd device $kerneldev");
my $cmd = $rbd_cmd->($scfg, $storeid, 'unmap', $kerneldev);
run_rbd_command($cmd, errmsg => "can't unmap rbd device $kerneldev");
}
return 1;
@ -790,7 +799,8 @@ sub volume_resize {
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
my $cmd = $rbd_cmd->($scfg, $storeid, 'resize', '--size', int(ceil($size/1024/1024)), $name);
my $cmd =
$rbd_cmd->($scfg, $storeid, 'resize', '--size', int(ceil($size / 1024 / 1024)), $name);
run_rbd_command($cmd, errmsg => "rbd resize '$volname' error");
return undef;
}
@ -822,9 +832,9 @@ sub volume_snapshot_delete {
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
my (undef, undef, undef, $protected) = rbd_volume_info($scfg, $storeid, $name, $snap);
if ($protected){
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
if ($protected) {
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'unprotect', $name, '--snap', $snap);
run_rbd_command($cmd, errmsg => "rbd unprotect $name snap '$snap' error");
}
my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name);
@ -841,22 +851,22 @@ sub volume_snapshot_needs_fsfreeze {
sub volume_has_feature {
my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_;
my $features = {
snapshot => { current => 1, snap => 1},
clone => { base => 1, snap => 1},
template => { current => 1},
copy => { base => 1, current => 1, snap => 1},
sparseinit => { base => 1, current => 1},
rename => {current => 1},
my $features = {
snapshot => { current => 1, snap => 1 },
clone => { base => 1, snap => 1 },
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);
my $key = undef;
if ($snapname){
$key = 'snap';
if ($snapname) {
$key = 'snap';
} else {
$key = $isBase ? 'base' : 'current';
$key = $isBase ? 'base' : 'current';
}
return 1 if $features->{$feature}->{$key};
@ -867,20 +877,21 @@ sub volume_export_formats {
my ($class, $scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots) = @_;
return $class->volume_import_formats(
$scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots);
$scfg, $storeid, $volname, $snapshot, $base_snapshot, $with_snapshots,
);
}
sub volume_export {
my (
$class,
$scfg,
$storeid,
$fh,
$volname,
$format,
$snapshot,
$base_snapshot,
$with_snapshots,
$class,
$scfg,
$storeid,
$fh,
$volname,
$format,
$snapshot,
$base_snapshot,
$with_snapshots,
) = @_;
die "volume export format $format not available for $class\n" if $format ne 'raw+size';
@ -891,9 +902,9 @@ sub volume_export {
PVE::Storage::Plugin::write_common_header($fh, $size);
my $cmd = $rbd_cmd->($scfg, $storeid, 'export', '--export-format', '1', $volname, '-');
run_rbd_command(
$cmd,
errmsg => 'could not export image',
output => '>&'.fileno($fh),
$cmd,
errmsg => 'could not export image',
output => '>&' . fileno($fh),
);
return;
@ -908,16 +919,16 @@ sub volume_import_formats {
sub volume_import {
my (
$class,
$scfg,
$storeid,
$fh,
$volname,
$format,
$snapshot,
$base_snapshot,
$with_snapshots,
$allow_rename,
$class,
$scfg,
$storeid,
$fh,
$volname,
$format,
$snapshot,
$base_snapshot,
$with_snapshots,
$allow_rename,
) = @_;
die "volume import format $format not available for $class\n" if $format ne 'raw+size';
@ -926,32 +937,32 @@ sub volume_import {
my (undef, $name, $vmid, undef, undef, undef, $file_format) = $class->parse_volname($volname);
die "cannot import format $format into a volume of format $file_format\n"
if $file_format ne 'raw';
if $file_format ne 'raw';
if (rbd_volume_exists($scfg, $storeid, $name)) {
die "volume $name already exists\n" if !$allow_rename;
warn "volume $name already exists - importing with a different name\n";
$volname = $class->find_free_diskname($storeid, $scfg, $vmid, $file_format);
die "volume $name already exists\n" if !$allow_rename;
warn "volume $name already exists - importing with a different name\n";
$volname = $class->find_free_diskname($storeid, $scfg, $vmid, $file_format);
}
my ($size) = PVE::Storage::Plugin::read_common_header($fh);
$size = PVE::Storage::Common::align_size_up($size, 1024) / 1024;
eval {
my $cmd = $rbd_cmd->($scfg, $storeid, 'import', '--export-format', '1', '-', $volname);
run_rbd_command(
$cmd,
errmsg => 'could not import image',
input => '<&'.fileno($fh),
);
my $cmd = $rbd_cmd->($scfg, $storeid, 'import', '--export-format', '1', '-', $volname);
run_rbd_command(
$cmd,
errmsg => 'could not import image',
input => '<&' . fileno($fh),
);
};
if (my $err = $@) {
# FIXME there is a slight race between finding the free disk name and removal here
# Does not only affect this plugin, see:
# https://lore.proxmox.com/pve-devel/20240403150712.262773-1-h.duerr@proxmox.com/
eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format); };
warn $@ if $@;
die $err;
# FIXME there is a slight race between finding the free disk name and removal here
# Does not only affect this plugin, see:
# https://lore.proxmox.com/pve-devel/20240403150712.262773-1-h.duerr@proxmox.com/
eval { $class->free_image($storeid, $scfg, $volname, 0, $file_format); };
warn $@ if $@;
die $err;
}
return "$storeid:$volname";
@ -961,25 +972,19 @@ sub rename_volume {
my ($class, $scfg, $storeid, $source_volname, $target_vmid, $target_volname) = @_;
my (
undef,
$source_image,
$source_vmid,
$base_name,
$base_vmid,
undef,
$format
undef, $source_image, $source_vmid, $base_name, $base_vmid, undef, $format,
) = $class->parse_volname($source_volname);
$target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format)
if !$target_volname;
if !$target_volname;
die "target volume '${target_volname}' already exists\n"
if rbd_volume_exists($scfg, $storeid, $target_volname);
if rbd_volume_exists($scfg, $storeid, $target_volname);
my $cmd = $rbd_cmd->($scfg, $storeid, 'rename', $source_image, $target_volname);
run_rbd_command(
$cmd,
errmsg => "could not rename image '${source_image}' to '${target_volname}'",
$cmd,
errmsg => "could not rename image '${source_image}' to '${target_volname}'",
);
eval { $class->unmap_volume($storeid, $scfg, $source_volname); };