X-Git-Url: https://git.proxmox.com/?a=blobdiff_plain;f=PVE%2FStorage%2FRBDPlugin.pm;h=b3f9d20b39b7e603290ed6a414765b7062b42cfa;hb=8cc610093b871f1f4913944d54a767718c803e8a;hp=277d1e8560489518144f41944d33136adcb59683;hpb=5a2b2e2f1428ad206b8ffbafc6317543fc232415;p=pve-storage.git diff --git a/PVE/Storage/RBDPlugin.pm b/PVE/Storage/RBDPlugin.pm index 277d1e8..b3f9d20 100644 --- a/PVE/Storage/RBDPlugin.pm +++ b/PVE/Storage/RBDPlugin.pm @@ -9,16 +9,70 @@ use PVE::JSONSchema qw(get_standard_option); use base qw(PVE::Storage::Plugin); +sub rbd_unittobytes { + { + "M" => 1024*1024, + "G" => 1024*1024*1024, + "T" => 1024*1024*1024*1024, + } +} + +my $add_pool_to_disk = sub { + my ($scfg, $disk) = @_; + + my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + + return "$pool/$disk"; +}; + my $rbd_cmd = sub { my ($scfg, $storeid, $op, @options) = @_; my $monhost = $scfg->{monhost}; $monhost =~ s/;/,/g; - my $cmd = ['/usr/bin/rbd', '-p', $scfg->{pool}, '-m', $monhost, '-n', - "client.$scfg->{username}", - '--keyring', "/etc/pve/priv/ceph/${storeid}.keyring", - '--auth_supported', $scfg->{authsupported}, $op]; + my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring"; + my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + my $username = $scfg->{username} ? $scfg->{username} : 'admin'; + + my $cmd = ['/usr/bin/rbd', '-p', $pool, '-m', $monhost]; + + if(-e $keyring){ + push @$cmd, '-n', "client.$username"; + push @$cmd, '--keyring', $keyring; + push @$cmd, '--auth_supported', 'cephx'; + }else{ + push @$cmd, '--auth_supported', 'none'; + } + + push @$cmd, $op; + + push @$cmd, @options if scalar(@options); + + return $cmd; +}; + +my $rados_cmd = sub { + my ($scfg, $storeid, $op, @options) = @_; + + my $monhost = $scfg->{monhost}; + $monhost =~ s/;/,/g; + + my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring"; + my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + my $username = $scfg->{username} ? $scfg->{username} : 'admin'; + + my $cmd = ['/usr/bin/rados', '-p', $pool, '-m', $monhost]; + + if(-e $keyring){ + push @$cmd, '-n', "client.$username"; + push @$cmd, '--keyring', $keyring; + push @$cmd, '--auth_supported', 'cephx'; + }else{ + push @$cmd, '--auth_supported', 'none'; + } + + push @$cmd, $op; push @$cmd, @options if scalar(@options); @@ -28,19 +82,21 @@ my $rbd_cmd = sub { sub rbd_ls { my ($scfg, $storeid) = @_; - my $cmd = &$rbd_cmd($scfg, $storeid, 'ls'); + my $cmd = &$rbd_cmd($scfg, $storeid, 'ls', '-l'); + my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; my $list = {}; my $parser = sub { my $line = shift; - if ($line =~ m/^(vm-(\d+)-\S+)$/) { - my ($image, $owner) = ($1, $2); + if ($line =~ m/^((vm|base)-(\d+)-disk-\d+)\s+(\d+)(M|G|T)\s((\S+)\/((vm|base)-\d+-\S+@\S+))?/) { + my ($image, $owner, $size, $unit, $parent) = ($1, $3, $4, $5, $8); - $list->{$scfg->{pool}}->{$image} = { + $list->{$pool}->{$image} = { name => $image, - size => 0, + size => $size*rbd_unittobytes()->{$unit}, + parent => $parent, vmid => $owner }; } @@ -56,6 +112,42 @@ sub rbd_ls { return $list; } +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 $size = undef; + my $parent = undef; + my $format = undef; + my $protected = undef; + + my $parser = sub { + my $line = shift; + + if ($line =~ m/size (\d+) (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_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser); + + return ($size, $parent, $format, $protected); +} + sub addslashes { my $text = shift; $text =~ s/;/\\;/g; @@ -113,9 +205,8 @@ sub options { nodes => { optional => 1 }, disable => { optional => 1 }, monhost => { fixed => 1 }, - pool => { fixed => 1 }, - username => { fixed => 1 }, - authsupported => { fixed => 1 }, + pool => { optional => 1 }, + username => { optional => 1 }, content => { optional => 1 }, }; } @@ -125,28 +216,122 @@ sub options { sub parse_volname { my ($class, $volname) = @_; - if ($volname =~ m/^(vm-(\d+)-\S+)$/) { - return ('images', $1, $2); + if ($volname =~ m/^((base-(\d+)-\S+)\/)?((base)?(vm)?-(\d+)-\S+)$/) { + return ('images', $4, $7, $2, $3, $5); } die "unable to parse rbd volume name '$volname'\n"; } sub path { - my ($class, $scfg, $volname, $storeid) = @_; + my ($class, $scfg, $volname, $storeid, $snapname) = @_; my ($vtype, $name, $vmid) = $class->parse_volname($volname); + $name .= '@'.$snapname if $snapname; my $monhost = addslashes($scfg->{monhost}); - my $pool = $scfg->{pool}; - my $username = $scfg->{username}; - my $authsupported = addslashes($scfg->{authsupported}); - - my $path = "rbd:$pool/$name:id=$username:auth_supported=$authsupported:keyring=/etc/pve/priv/ceph/$storeid.keyring:mon_host=$monhost"; + my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + my $username = $scfg->{username} ? $scfg->{username} : 'admin'; + + my $path = "rbd:$pool/$name:mon_host=$monhost"; + my $keyring = "/etc/pve/priv/ceph/${storeid}.keyring"; + + if(-e $keyring ){ + $path .= ":id=$username:auth_supported=cephx:keyring=$keyring"; + }else{ + $path .= ":auth_supported=none"; + } return ($path, $vmid, $vtype); } +my $find_free_diskname = sub { + my ($storeid, $scfg, $vmid) = @_; + + my $rbd = rbd_ls($scfg, $storeid); + my $pool = $scfg->{pool} ? $scfg->{pool} : 'rbd'; + my $disk_ids = {}; + my $dat = $rbd->{$pool}; + + foreach my $image (keys %$dat) { + my $volname = $dat->{$image}->{name}; + if ($volname =~ m/(vm|base)-$vmid-disk-(\d+)/){ + $disk_ids->{$2} = 1; + } + } + #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"; +}; + +sub create_base { + my ($class, $storeid, $scfg, $volname) = @_; + + my $snap = '__base__'; + + my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = + $class->parse_volname($volname); + + die "create_base not possible with base image\n" if $isBase; + + my ($size, $parent, $format, undef) = rbd_volume_info($scfg, $storeid, $name); + die "rbd volume info on '$name' failed\n" if !($size); + + 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); + + my $newname = $name; + $newname =~ s/^vm-/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)); + run_command($cmd, errmsg => "rbd rename '$name' error"); + + 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_command($cmd, errmsg => "rbd protect $newname snap $snap' error"); + } + + return $newvolname; + +} + +sub clone_image { + my ($class, $scfg, $storeid, $volname, $vmid) = @_; + + my $snap = '__base__'; + + my ($vtype, $basename, $basevmid, undef, undef, $isBase) = + $class->parse_volname($volname); + + die "clone_image onyl works on base images\n" if !$isBase; + + my $name = &$find_free_diskname($storeid, $scfg, $vmid); + + warn "clone $volname: $basename to $name\n"; + + my $newvol = "$basename/$name"; + + my $cmd = &$rbd_cmd($scfg, $storeid, 'clone', &$add_pool_to_disk($scfg, $basename), '--snap', $snap, &$add_pool_to_disk($scfg, $name)); + run_command($cmd, errmsg => "rbd clone $basename' error"); + + return $newvol; +} + sub alloc_image { my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_; @@ -154,32 +339,34 @@ sub alloc_image { die "illegal name '$name' - sould be 'vm-$vmid-*'\n" if $name && $name !~ m/^vm-$vmid-/; - if (!$name) { - my $rdb = rbd_ls($scfg, $storeid); - - for (my $i = 1; $i < 100; $i++) { - my $tn = "vm-$vmid-disk-$i"; - if (!defined ($rdb->{$scfg->{pool}}->{$tn})) { - $name = $tn; - last; - } - } - } + $name = &$find_free_diskname($storeid, $scfg, $vmid); - die "unable to allocate an image name for VM $vmid in storage '$storeid'\n" - if !$name; - - my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--size', ($size/1024), $name); - run_command($cmd, errmsg => "rbd create $name' error", errfunc => sub {}); + my $cmd = &$rbd_cmd($scfg, $storeid, 'create', '--format' , 2, '--size', int(($size+1023)/1024), $name); + run_command($cmd, errmsg => "rbd create $name' error"); return $name; } sub free_image { - my ($class, $storeid, $scfg, $volname) = @_; + my ($class, $storeid, $scfg, $volname, $isBase) = @_; + + my ($vtype, $name, $vmid, undef, undef, undef) = + $class->parse_volname($volname); - my $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $volname); - run_command($cmd, errmsg => "rbd rm $volname' error", outfunc => sub {}, errfunc => sub {}); + 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); + run_command($cmd, errmsg => "rbd unprotect $name snap $snap' error"); + } + } + + my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'purge', $name); + run_command($cmd, errmsg => "rbd snap purge $volname' error"); + + $cmd = &$rbd_cmd($scfg, $storeid, 'rm', $name); + run_command($cmd, errmsg => "rbd rm $volname' error"); return undef; } @@ -188,10 +375,11 @@ 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}->{$scfg->{pool}}) { + if (my $dat = $cache->{rbd}->{$pool}) { foreach my $image (keys %$dat) { my $volname = $dat->{$image}->{name}; @@ -220,9 +408,24 @@ sub list_images { sub status { my ($class, $storeid, $scfg, $cache) = @_; - my $total = 0; - my $free = 0; - my $used = 0; + my $cmd = &$rados_cmd($scfg, $storeid, 'df'); + + my $stats = {}; + + my $parser = sub { + my $line = shift; + if ($line =~ m/^\s+total\s(\S+)\s+(\d+)/) { + $stats->{$1} = $2; + } + }; + + eval { + run_command($cmd, errmsg => "rados error", errfunc => sub {}, outfunc => $parser); + }; + + my $total = $stats->{space} ? $stats->{space}*1024 : 0; + my $free = $stats->{avail} ? $stats->{avail}*1024 : 0; + my $used = $stats->{used} ? $stats->{used}*1024: 0; my $active = 1; return ($total, $free, $used, $active); @@ -251,20 +454,8 @@ sub deactivate_volume { sub volume_size_info { my ($class, $scfg, $storeid, $volname, $timeout) = @_; - my $cmd = &$rbd_cmd($scfg, $storeid, 'info', $volname); - my $size = undef; - my $parser = sub { - my $line = shift; - - if ($line =~ m/size (\d+) MB in (\d+) objects/) { - $size = $1; - } - }; - - run_command($cmd, errmsg => "rbd error", errfunc => sub {}, outfunc => $parser); - - $size = $size*1024*1024 if $size; - + my ($vtype, $name, $vmid) = $class->parse_volname($volname); + my ($size, undef) = rbd_volume_info($scfg, $storeid, $name); return $size; } @@ -273,8 +464,10 @@ sub volume_resize { return 1 if $running; - my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $volname); - run_command($cmd, errmsg => "rbd resize $volname' error", errfunc => sub {}); + my ($vtype, $name, $vmid) = $class->parse_volname($volname); + + my $cmd = &$rbd_cmd($scfg, $storeid, 'resize', '--size', ($size/1024/1024), $name); + run_command($cmd, errmsg => "rbd resize $volname' error"); return undef; } @@ -283,16 +476,56 @@ sub volume_snapshot { return 1 if $running; - my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $volname); - run_command($cmd, errmsg => "rbd snapshot $volname' error", errfunc => sub {}); + my ($vtype, $name, $vmid) = $class->parse_volname($volname); + + my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'create', '--snap', $snap, $name); + run_command($cmd, errmsg => "rbd snapshot $volname' error"); return undef; } sub volume_snapshot_rollback { my ($class, $scfg, $storeid, $volname, $snap) = @_; - my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $volname); - run_command($cmd, errmsg => "rbd snapshot $volname to $snap' error", errfunc => sub {}); + my ($vtype, $name, $vmid) = $class->parse_volname($volname); + + my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rollback', '--snap', $snap, $name); + run_command($cmd, errmsg => "rbd snapshot $volname to $snap' error"); +} + +sub volume_snapshot_delete { + my ($class, $scfg, $storeid, $volname, $snap, $running) = @_; + + return 1 if $running; + + my ($vtype, $name, $vmid) = $class->parse_volname($volname); + + my $cmd = &$rbd_cmd($scfg, $storeid, 'snap', 'rm', '--snap', $snap, $name); + run_command($cmd, errmsg => "rbd snapshot $volname' error"); + return undef; +} + +sub volume_has_feature { + my ($class, $scfg, $feature, $storeid, $volname, $snapname, $running) = @_; + + my $features = { + snapshot => { current => 1, snap => 1}, + clone => { base => 1}, + template => { current => 1}, + copy => { base => 1, current => 1, snap => 1}, + }; + + my ($vtype, $name, $vmid, $basename, $basevmid, $isBase) = + $class->parse_volname($volname); + + my $key = undef; + if($snapname){ + $key = 'snap'; + }else{ + $key = $isBase ? 'base' : 'current'; + } + return 1 if $features->{$feature}->{$key}; + + return undef; } 1;