X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FStorage%2FRBDPlugin.pm;h=35b2372b704ba6dcf53fd7d87b46140c6b30421b;hb=d70d814ccf09968a63755e8443acb12d3132aaad;hp=decfbf586f068bd3f6390d906605190ee292742c;hpb=79127fb5c7133c22e5b42555e670ee9c9a213b9a;p=pve-storage.git diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm index decfbf5..35b2372 100644 --- a/PVE/Storage/RBDPlugin.pm +++ b/PVE/Storage/RBDPlugin.pm @@ -2,74 +2,105 @@ package PVE::Storage::RBDPlugin; use strict; use warnings; + +use Cwd qw(abs_path); use IO::File; +use JSON; use Net::IP; -use PVE::Tools qw(run_command trim); -use PVE::Storage::Plugin; + +use PVE::CephConfig; +use PVE::Cluster qw(cfs_read_file);; use PVE::JSONSchema qw(get_standard_option); +use PVE::ProcFSTools; +use PVE::RADOS; +use PVE::RPCEnvironment; +use PVE::Storage::Plugin; +use PVE::Tools qw(run_command trim file_read_firstline); use base qw(PVE::Storage::Plugin); -my $pveceph_config = '/etc/pve/ceph.conf'; - -my $rbd_unittobytes = { - "k" => 1024, - "M" => 1024*1024, - "G" => 1024*1024*1024, - "T" => 1024*1024*1024*1024, +my $get_parent_image_name = sub { + my ($parent) = @_; + return undef if !$parent; + return $parent->{image} . "@" . $parent->{snapshot}; }; -my $add_pool_to_disk = sub { - my ($scfg, $disk) = @_; +my $librados_connect = sub { + my ($scfg, $storeid, $options) = @_; - my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + $options->{timeout} = 60 + if !defined($options->{timeout}) && PVE::RPCEnvironment->is_worker(); - return "$pool/$disk"; -}; + my $librados_config = PVE::CephConfig::ceph_connect_option($scfg, $storeid, $options->%*); -my $hostlist = sub { - my ($list_text, $separator) = @_; + my $rados = PVE::RADOS->new(%$librados_config); - my @monhostlist = PVE::Tools::split_list($list_text); - return join($separator, map { - my ($host, $port) = PVE::Tools::parse_host_and_port($_); - $port = defined($port) ? ":$port" : ''; - $host = "[$host]" if Net::IP::ip_is_ipv6($host); - "${host}${port}" - } @monhostlist); + return $rados; }; -my $build_cmd = sub { - my ($binary, $scfg, $storeid, $op, @options) = @_; +my sub get_rbd_path { + my ($scfg, $volume) = @_; + my $path = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + $path .= "/$scfg->{namespace}" if defined($scfg->{namespace}); + $path .= "/$volume" if defined($volume); + return $path; +}; - my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring"; - my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; - my $username = $scfg->{username} ? $scfg->{username} : 'admin'; +my sub get_rbd_dev_path { + my ($scfg, $storeid, $volume) = @_; - my $cmd = [$binary, '-p', $pool]; - my $pveceph_managed = !defined($scfg->{monhost}); + 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}; + } elsif ($scfg->{monhost}) { + 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}; + } - if ($pveceph_managed) { - push @$cmd, '-c', $pveceph_config; + 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 } else { - push @$cmd, '-m', $hostlist->($scfg->{monhost}, ','); - push @$cmd, '--auth_supported', -e $keyring ? 'cephx' : 'none'; + die "cluster fsid has invalid format\n"; } - if (-e $keyring) { - push @$cmd, '-n', "client.$username"; - push @$cmd, '--keyring', $keyring; + my $rbd_path = get_rbd_path($scfg, $volume); + my $pve_path = "/dev/rbd-pve/${cluster_id}/${rbd_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; } + return $pve_path; +} + +my $build_cmd = sub { + my ($binary, $scfg, $storeid, $op, @options) = @_; - my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf"; + my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid); + my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; - if (-e $cephconfig) { - if ($pveceph_managed) { - warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n"; - } else { - push @$cmd, '-c', $cephconfig; - } + my $cmd = [$binary, '-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}; } + 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, '-n', "client.$cmd_option->{userid}" if ($cmd_option->{userid}); + push @$cmd, '--keyring', $cmd_option->{keyring} if ($cmd_option->{keyring}); push @$cmd, $op; @@ -91,47 +122,51 @@ my $rados_cmd = sub { }; # needed for volumes created using ceph jewel (or higher) -my $krdb_feature_disable = sub { +my $krbd_feature_update = sub { my ($scfg, $storeid, $name) = @_; - return 1 if !$scfg->{krbd}; - - my ($major, undef, undef, undef) = ceph_version(); - return 1 if $major < 10; - - my $feature_cmd = &$rbd_cmd($scfg, $storeid, 'feature', 'disable', $name, 'deep-flatten,fast-diff,object-map,exclusive-lock'); - run_rbd_command($feature_cmd, errmsg => "could not disable krbd-incompatible image features of rbd volume $name"); -}; + my (@disable, @enable); + my ($kmajor, $kminor) = PVE::ProcFSTools::kernel_version(); -my $ceph_version_parser = sub { - my $line = shift; - if ($line =~ m/^ceph version ((\d+)\.(\d+)\.(\d+))(?: \([a-fA-F0-9]+\))/) { - return ($2, $3, $4, $1); + if ($kmajor > 5 || $kmajor == 5 && $kminor >= 3) { + # 'deep-flatten' can only be disabled, not enabled after image creation + push @enable, 'fast-diff', 'object-map'; } else { - warn "Could not parse Ceph version: '$line'\n"; + push @disable, 'fast-diff', 'object-map', 'deep-flatten'; } -}; -sub ceph_version { - my ($cache) = @_; + if ($kmajor >= 5) { + push @enable, 'exclusive-lock'; + } else { + push @disable, 'exclusive-lock'; + } - my $version_string = $cache; + my $active_features_list = (rbd_volume_info($scfg, $storeid, $name))[4]; + my $active_features = { map { $_ => 1 } @$active_features_list }; - my $major; - my $minor; - my $bugfix; + my $to_disable = join(',', grep { $active_features->{$_} } @disable); + my $to_enable = join(',', grep { !$active_features->{$_} } @enable ); - if (defined($version_string)) { - ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($version_string); - } else { - run_command('ceph --version', outfunc => sub { - my $line = shift; - ($major, $minor, $bugfix, $version_string) = &$ceph_version_parser($line); - }); + 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", + ); } - return undef if !defined($version_string); - return wantarray ? ($major, $minor, $bugfix, $version_string) : $version_string; -} + 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 $@; + } +}; sub run_rbd_command { my ($cmd, %args) = @_; @@ -152,7 +187,7 @@ sub run_rbd_command { *STDERR->flush(); }; } - + eval { run_command($cmd, %args); }; if (my $err = $@) { die $errmsg . $lasterr if length($lasterr); @@ -165,71 +200,151 @@ sub run_rbd_command { sub rbd_ls { my ($scfg, $storeid) = @_; - my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l'); my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + $pool .= "/$scfg->{namespace}" if defined($scfg->{namespace}); - my $list = {}; - - my $parser = sub { - my $line = shift; - - if ($line =~ m/^((vm|base)-(\d+)-\S+)\s+(\d+)(k|M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) { - my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8); - return if $image =~ /@/; #skip snapshots - - $list->{$pool}->{$image} = { - name => $image, - size => $size*$rbd_unittobytes->{$unit}, - parent => $parent, - vmid => $owner - }; - } - }; + my $raw = ''; + my $parser = sub { $raw .= shift }; + my $cmd = $rbd_cmd->($scfg, $storeid, 'ls', '-l', '--format', 'json'); eval { run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser); }; my $err = $@; die $err if $err && $err !~ m/doesn't contain rbd images/ ; - + + my $result; + if ($raw eq '') { + $result = []; + } elsif ($raw =~ m/^(\[.*\])$/s) { # untaint + $result = JSON::decode_json($1); + } else { + die "got unexpected data from rbd ls: '$raw'\n"; + } + + my $list = {}; + + foreach my $el (@$result) { + next if defined($el->{snapshot}); + + my $image = $el->{image}; + + my ($owner) = $image =~ m/^(?:vm|base)-(\d+)-/; + next if !defined($owner); + + $list->{$pool}->{$image} = { + name => $image, + size => $el->{size}, + parent => $get_parent_image_name->($el->{parent}), + vmid => $owner + }; + } + return $list; } +sub rbd_ls_snap { + my ($scfg, $storeid, $name) = @_; + + 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; }); + + my $list; + if ($raw =~ m/^(\[.*\])$/s) { # untaint + $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"; + } + + $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, + }; + } + return $res; +} + sub rbd_volume_info { my ($scfg, $storeid, $volname, $snap) = @_; my $cmd = undef; - if($snap){ - $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname, '--snap', $snap); - }else{ - $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname); + my @options = ('info', $volname, '--format', 'json'); + if ($snap) { + push @options, '--snap', $snap; } - my $size = undef; - my $parent = undef; - my $format = undef; - my $protected = undef; + $cmd = $rbd_cmd->($scfg, $storeid, @options); - my $parser = sub { - my $line = shift; + my $raw = ''; + my $parser = sub { $raw .= shift }; - if ($line =~ m/size (\d+) (k|M|G|T)B in (\d+) objects/) { - $size = $1 * $rbd_unittobytes->{$2} if ($1); - } elsif ($line =~ m/parent:\s(\S+)\/(\S+)/) { - $parent = $2; - } elsif ($line =~ m/format:\s(\d+)/) { - $format = $1; - } elsif ($line =~ m/protected:\s(\S+)/) { - $protected = 1 if $1 eq "True"; - } + run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser); - }; + my $volume; + if ($raw eq '') { + $volume = {}; + } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint + $volume = JSON::decode_json($1); + } else { + 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; + + return $volume->@{qw(size parent format protected features)}; +} + +sub rbd_volume_du { + my ($scfg, $storeid, $volname) = @_; + + my @options = ('du', $volname, '--format', 'json'); + my $cmd = $rbd_cmd->($scfg, $storeid, @options); + + my $raw = ''; + my $parser = sub { $raw .= shift }; run_rbd_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser); - return ($size, $parent, $format, $protected); + my $volume; + if ($raw eq '') { + $volume = {}; + } elsif ($raw =~ m/^(\{.*\})$/s) { # untaint + $volume = JSON::decode_json($1); + } else { + die "got unexpected data from rbd du: '$raw'\n"; + } + + if (!defined($volume->{images})) { + 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}); + + # 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"; } # Configuration @@ -254,6 +369,14 @@ sub properties { 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', @@ -263,9 +386,13 @@ sub properties { type => 'string', }, krbd => { - description => "Access rbd through krbd kernel module.", + description => "Always access rbd through krbd kernel module.", type => 'boolean', }, + keyring => { + description => "Client keyring contents (for external clusters).", + type => 'string', + }, }; } @@ -275,14 +402,47 @@ sub options { 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 }, }; } # Storage implementation +sub on_add_hook { + my ($class, $storeid, $scfg, %param) = @_; + + my $secret = $param{keyring} if defined $param{keyring} // undef; + PVE::CephConfig::ceph_create_keyfile($scfg->{type}, $storeid, $secret); + + return; +} + +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); + } + } + + return; +} + +sub on_delete_hook { + my ($class, $storeid, $scfg) = @_; + PVE::CephConfig::ceph_remove_keyfile($scfg->{type}, $storeid); + return; +} + sub parse_volname { my ($class, $volname) = @_; @@ -296,53 +456,42 @@ sub parse_volname { sub path { my ($class, $scfg, $volname, $storeid, $snapname) = @_; + my $cmd_option = PVE::CephConfig::ceph_connect_option($scfg, $storeid); my ($vtype, $name, $vmid) = $class->parse_volname($volname); $name .= '@'.$snapname if $snapname; - my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; - return ("/dev/rbd/$pool/$name", $vmid, $vtype) if $scfg->{krbd}; - - my $username = $scfg->{username} ? $scfg->{username} : 'admin'; + if ($scfg->{krbd}) { + my $rbd_dev_path = get_rbd_dev_path($scfg, $storeid, $name); + return ($rbd_dev_path, $vmid, $vtype); + } - my $path = "rbd:$pool/$name"; - my $pveceph_managed = !defined($scfg->{monhost}); - my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring"; + my $rbd_path = get_rbd_path($scfg, $name); + my $path = "rbd:${rbd_path}"; - if ($pveceph_managed) { - $path .= ":conf=$pveceph_config"; - } else { - my $monhost = $hostlist->($scfg->{monhost}, ';'); + $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 .= -e $keyring ? ":auth_supported=cephx" : ":auth_supported=none"; + $path .= ":auth_supported=$cmd_option->{auth_supported}"; } - $path .= ":id=$username:keyring=$keyring" if -e $keyring; - - my $cephconfig = "/etc/pve/priv/ceph/${storeid}.conf"; - - if (-e $cephconfig) { - if ($pveceph_managed) { - warn "ignoring custom ceph config for storage '$storeid', 'monhost' is not set (assuming pveceph managed cluster)!\n"; - } else { - $path .= ":conf=$cephconfig"; - } - } + $path .= ":id=$cmd_option->{userid}:keyring=$cmd_option->{keyring}" if ($cmd_option->{keyring}); return ($path, $vmid, $vtype); } -my $find_free_diskname = sub { - my ($storeid, $scfg, $vmid) = @_; +sub find_free_diskname { + my ($class, $storeid, $scfg, $vmid, $fmt, $add_fmt_suffix) = @_; + + my $cmd = $rbd_cmd->($scfg, $storeid, 'ls'); - my $cmd = &$rbd_cmd($scfg, $storeid, 'ls'); - my $disk_ids = {}; + my $disk_list = []; my $parser = sub { my $line = shift; - - if ($line =~ m/^(vm|base)-\Q$vmid\E+-disk-(\d+)$/) { - $disk_ids->{$2} = 1; + if ($line =~ m/^(.*)$/) { # untaint + push @$disk_list, $1; } }; @@ -353,15 +502,8 @@ my $find_free_diskname = sub { die $err if $err && $err !~ m/doesn't contain rbd images/; - #fix: can we search in $rbd hash key with a regex to find (vm|base) ? - for (my $i = 1; $i < 100; $i++) { - if (!$disk_ids->{$i}) { - return "vm-$vmid-disk-$i"; - } - } - - die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"; -}; + return PVE::Storage::Plugin::get_next_vm_diskname($disk_list, $storeid, $vmid, undef, $scfg); +} sub create_base { my ($class, $storeid, $scfg, $volname) = @_; @@ -386,9 +528,18 @@ sub create_base { my $newvolname = $basename ? "$basename/$newname" : "$newname"; - my $cmd = &$rbd_cmd($scfg, $storeid, 'rename', &$add_pool_to_disk($scfg, $name), &$add_pool_to_disk($scfg, $newname)); + my $cmd = $rbd_cmd->( + $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 ? $class->volume_snapshot($scfg, $storeid, $newname, $snap, $running); @@ -396,7 +547,7 @@ sub create_base { 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); + my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $newname, '--snap', $snap); run_rbd_command($cmd, errmsg => "rbd protect $newname snap '$snap' error"); } @@ -416,7 +567,7 @@ sub clone_image { die "$volname is not a base image and snapname is not provided\n" if !$isBase && !length($snapname); - my $name = &$find_free_diskname($storeid, $scfg, $vmid); + my $name = $class->find_free_diskname($storeid, $scfg, $vmid); warn "clone $volname: $basename snapname $snap to $name\n"; @@ -424,7 +575,7 @@ sub clone_image { 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); + my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'protect', $volname, '--snap', $snapname); run_rbd_command($cmd, errmsg => "rbd protect $volname snap $snapname error"); } } @@ -432,13 +583,15 @@ sub clone_image { my $newvol = "$basename/$name"; $newvol = $name if length($snapname); - my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename), - '--snap', $snap, &$add_pool_to_disk($scfg, $name)); + my @options = ( + get_rbd_path($scfg, $basename), + '--snap', $snap, + ); + push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'}; + my $cmd = $rbd_cmd->($scfg, $storeid, 'clone', @options, get_rbd_path($scfg, $name)); run_rbd_command($cmd, errmsg => "rbd clone '$basename' error"); - &$krdb_feature_disable($scfg, $storeid, $name); - return $newvol; } @@ -449,12 +602,16 @@ sub alloc_image { die "illegal name '$name' - should be 'vm-$vmid-*'\n" if $name && $name !~ m/^vm-$vmid-/; - $name = &$find_free_diskname($storeid, $scfg, $vmid) if !$name; + $name = $class->find_free_diskname($storeid, $scfg, $vmid) if !$name; - my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--image-format' , 2, '--size', int(($size+1023)/1024), $name); - run_rbd_command($cmd, errmsg => "rbd create $name' error"); + my @options = ( + '--image-format' , 2, + '--size', int(($size + 1023) / 1024), + ); + push @options, ('--data-pool', $scfg->{'data-pool'}) if $scfg->{'data-pool'}; - &$krdb_feature_disable($scfg, $storeid, $name); + my $cmd = $rbd_cmd->($scfg, $storeid, 'create', @options, $name); + run_rbd_command($cmd, errmsg => "rbd create '$name' error"); return $name; } @@ -465,22 +622,22 @@ sub free_image { my ($vtype, $name, $vmid, undef, undef, undef) = $class->parse_volname($volname); - if ($isBase) { - my $snap = '__base__'; - 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); + + 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"); } } $class->deactivate_volume($storeid, $scfg, $volname); - my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name); - run_rbd_command($cmd, errmsg => "rbd snap purge '$volname' error"); + 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); - run_rbd_command($cmd, errmsg => "rbd rm '$volname' error"); + $cmd = $rbd_cmd->($scfg, $storeid, 'rm', $name); + run_rbd_command($cmd, errmsg => "rbd rm '$name' error"); return undef; } @@ -489,66 +646,56 @@ sub list_images { my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_; $cache->{rbd} = rbd_ls($scfg, $storeid) if !$cache->{rbd}; - my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; - - my $res = []; - - if (my $dat = $cache->{rbd}->{$pool}) { - foreach my $image (keys %$dat) { - my $info = $dat->{$image}; + my $dat = $cache->{rbd}->{get_rbd_path($scfg)}; + return [] if !$dat; # nothing found - my $volname = $info->{name}; - my $parent = $info->{parent}; - my $owner = $info->{vmid}; + my $res = []; + for my $image (sort keys %$dat) { + 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; } sub status { my ($class, $storeid, $scfg, $cache) = @_; - my $cmd = &$rados_cmd($scfg, $storeid, 'df'); + my $rados = $librados_connect->($scfg, $storeid); + my $df = $rados->mon_command({ prefix => 'df', format => 'json' }); - my $stats = {}; + my $pool = $scfg->{'data-pool'} // $scfg->{pool} // 'rbd'; - my $parser = sub { - my $line = shift; - if ($line =~ m/^\s*total(?:\s|_)(\S+)\s+(\d+)(k|M|G|T)?/) { - $stats->{$1} = $2; - # luminous has units here.. - if ($3) { - $stats->{$1} *= $rbd_unittobytes->{$3}/1024; - } - } - }; + my ($d) = grep { $_->{name} eq $pool } @{$df->{pools}}; - eval { - run_rbd_command($cmd, errmsg => "rados error", errfunc => sub {}, outfunc => $parser); - }; + if (!defined($d)) { + warn "could not get usage stats for pool '$pool'\n"; + return; + } - my $total = $stats->{space} ? $stats->{space}*1024 : 0; - my $free = $stats->{avail} ? $stats->{avail}*1024 : 0; - my $used = $stats->{used} ? $stats->{used}*1024: 0; + # max_avail -> max available space for data w/o replication in the pool + # bytes_used -> data w/o replication in the pool + my $free = $d->{stats}->{max_avail}; + my $used = $d->{stats}->{stored} // $d->{stats}->{bytes_used}; + my $total = $used + $free; my $active = 1; return ($total, $free, $used, $active); @@ -564,39 +711,55 @@ sub deactivate_storage { return 1; } -sub activate_volume { - my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; +sub map_volume { + my ($class, $storeid, $scfg, $volname, $snapname) = @_; - return 1 if !$scfg->{krbd}; + my ($vtype, $img_name, $vmid) = $class->parse_volname($volname); - my ($vtype, $name, $vmid) = $class->parse_volname($volname); - my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + my $name = $img_name; + $name .= '@'.$snapname if $snapname; - my $path = "/dev/rbd/$pool/$name"; - $path .= '@'.$snapname if $snapname; - return if -b $path; + my $kerneldev = get_rbd_dev_path($scfg, $storeid, $name); + return $kerneldev if -b $kerneldev; # already mapped + + # features can only be enabled/disabled for image, not for snapshot! + $krbd_feature_update->($scfg, $storeid, $img_name); + + my $cmd = $rbd_cmd->($scfg, $storeid, 'map', $name); + run_rbd_command($cmd, errmsg => "can't map rbd volume $name"); + + return $kerneldev; +} + +sub unmap_volume { + my ($class, $storeid, $scfg, $volname, $snapname) = @_; + + my ($vtype, $name, $vmid) = $class->parse_volname($volname); $name .= '@'.$snapname if $snapname; - my $cmd = &$rbd_cmd($scfg, $storeid, 'map', $name); - run_rbd_command($cmd, errmsg => "can't mount rbd volume $name"); + + 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"); + } return 1; } -sub deactivate_volume { +sub activate_volume { my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; - return 1 if !$scfg->{krbd}; + $class->map_volume($storeid, $scfg, $volname, $snapname) if $scfg->{krbd}; - my ($vtype, $name, $vmid) = $class->parse_volname($volname); - my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + return 1; +} - my $path = "/dev/rbd/$pool/$name"; - $path .= '@'.$snapname if $snapname; - return if ! -b $path; +sub deactivate_volume { + my ($class, $storeid, $scfg, $volname, $snapname, $cache) = @_; - my $cmd = &$rbd_cmd($scfg, $storeid, 'unmap', $path); - run_rbd_command($cmd, errmsg => "can't unmap rbd volume $name"); + $class->unmap_volume($storeid, $scfg, $volname, $snapname); return 1; } @@ -605,18 +768,19 @@ sub volume_size_info { my ($class, $scfg, $storeid, $volname, $timeout) = @_; my ($vtype, $name, $vmid) = $class->parse_volname($volname); - my ($size, undef) = rbd_volume_info($scfg, $storeid, $name); - return $size; + my ($size, $parent) = rbd_volume_info($scfg, $storeid, $name); + my $used = wantarray ? rbd_volume_du($scfg, $storeid, $name) : 0; + return wantarray ? ($size, 'raw', $used, $parent) : $size; } sub volume_resize { my ($class, $scfg, $storeid, $volname, $size, $running) = @_; - return 1 if $running && !$scfg->{krbd}; + return 1 if $running && !$scfg->{krbd}; # FIXME??? my ($vtype, $name, $vmid) = $class->parse_volname($volname); - my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name); + my $cmd = $rbd_cmd->($scfg, $storeid, 'resize', '--allow-shrink', '--size', ($size/1024/1024), $name); run_rbd_command($cmd, errmsg => "rbd resize '$volname' error"); return undef; } @@ -626,7 +790,7 @@ sub volume_snapshot { my ($vtype, $name, $vmid) = $class->parse_volname($volname); - my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name); + my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name); run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error"); return undef; } @@ -636,32 +800,34 @@ sub volume_snapshot_rollback { my ($vtype, $name, $vmid) = $class->parse_volname($volname); - my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name); + my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name); run_rbd_command($cmd, errmsg => "rbd snapshot $volname to '$snap' error"); } sub volume_snapshot_delete { my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; - return 1 if $running && !$scfg->{krbd}; - $class->deactivate_volume($storeid, $scfg, $volname, $snap, {}); 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); + 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); + my $cmd = $rbd_cmd->($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name); run_rbd_command($cmd, errmsg => "rbd snapshot '$volname' error"); return undef; } +sub volume_snapshot_needs_fsfreeze { + return 1; +} + sub volume_has_feature { my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; @@ -671,20 +837,56 @@ 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); + my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = $class->parse_volname($volname); my $key = undef; - if($snapname){ + if ($snapname){ $key = 'snap'; - }else{ - $key = $isBase ? 'base' : 'current'; + } else { + $key = $isBase ? 'base' : 'current'; } return 1 if $features->{$feature}->{$key}; return undef; } +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 + ) = $class->parse_volname($source_volname); + $target_volname = $class->find_free_diskname($storeid, $scfg, $target_vmid, $format) + if !$target_volname; + + eval { + my $cmd = $rbd_cmd->($scfg, $storeid, 'info', $target_volname); + run_rbd_command($cmd, errmsg => "exist check", quiet => 1); + }; + die "target volume '${target_volname}' already exists\n" if !$@; + + 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}'", + ); + + eval { $class->unmap_volume($storeid, $scfg, $source_volname); }; + warn $@ if $@; + + $base_name = $base_name ? "${base_name}/" : ''; + + return "${storeid}:${base_name}${target_volname}"; +} + 1;