imported from svn 'pve-storage/pve2'

This commit is contained in:
Dietmar Maurer
2011-08-23 07:43:03 +02:00
commit b6cf0a6659
15 changed files with 4207 additions and 0 deletions

5
PVE/API2/Makefile Normal file
View File

@ -0,0 +1,5 @@
.PHONY: install
install:
make -C Storage install

329
PVE/API2/Storage/Config.pm Executable file
View File

@ -0,0 +1,329 @@
package PVE::API2::Storage::Config;
use strict;
use warnings;
use PVE::SafeSyslog;
use PVE::Cluster qw(cfs_read_file cfs_write_file);
use PVE::Storage;
use HTTP::Status qw(:constants);
use Storable qw(dclone);
use PVE::JSONSchema qw(get_standard_option);
use Data::Dumper; # fixme: remove
use PVE::RESTHandler;
use base qw(PVE::RESTHandler);
my @ctypes = qw(images vztmpl iso backup);
my $storage_type_enum = ['dir', 'nfs', 'lvm', 'iscsi'];
my $api_storage_config = sub {
my ($cfg, $storeid) = @_;
my $scfg = dclone(PVE::Storage::storage_config ($cfg, $storeid));
$scfg->{storage} = $storeid;
delete $scfg->{priority};
$scfg->{digest} = $cfg->{digest};
$scfg->{content} = PVE::Storage::content_hash_to_string($scfg->{content});
if ($scfg->{nodes}) {
$scfg->{nodes} = join(',', keys(%{$scfg->{nodes}}));
}
return $scfg;
};
__PACKAGE__->register_method ({
name => 'index',
path => '',
method => 'GET',
description => "Storage index.",
parameters => {
additionalProperties => 0,
properties => {
type => {
description => "Only list storage of specific type",
type => 'string',
enum => $storage_type_enum,
optional => 1,
},
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => { storage => { type => 'string'} },
},
links => [ { rel => 'child', href => "{storage}" } ],
},
code => sub {
my ($param) = @_;
my $cfg = cfs_read_file("storage.cfg");
my @sids = PVE::Storage::storage_ids($cfg);
my $res = [];
foreach my $storeid (@sids) {
my $scfg = &$api_storage_config($cfg, $storeid);
next if $param->{type} && $param->{type} ne $scfg->{type};
push @$res, $scfg;
}
return $res;
}});
__PACKAGE__->register_method ({
name => 'read',
path => '{storage}',
method => 'GET',
description => "Read storage configuration.",
parameters => {
additionalProperties => 0,
properties => {
storage => get_standard_option('pve-storage-id'),
},
},
returns => {},
code => sub {
my ($param) = @_;
my $cfg = cfs_read_file("storage.cfg");
return &$api_storage_config($cfg, $param->{storage});
}});
__PACKAGE__->register_method ({
name => 'create',
protected => 1,
path => '',
method => 'POST',
description => "Create a new storage.",
parameters => {
additionalProperties => 0,
properties => {
storage => get_standard_option('pve-storage-id'),
nodes => get_standard_option('pve-node-list', { optional => 1 }),
type => {
type => 'string',
enum => $storage_type_enum,
},
path => {
type => 'string', format => 'pve-storage-path',
optional => 1,
},
export => {
type => 'string', format => 'pve-storage-path',
optional => 1,
},
server => {
type => 'string', format => 'pve-storage-server',
optional => 1,
},
options => {
type => 'string', format => 'pve-storage-options',
optional => 1,
},
target => {
type => 'string',
optional => 1,
},
vgname => {
type => 'string', format => 'pve-storage-vgname',
optional => 1,
},
base => {
type => 'string', format => 'pve-volume-id',
optional => 1,
},
portal => {
type => 'string', format => 'pve-storage-portal-dns',
optional => 1,
},
content => {
type => 'string', format => 'pve-storage-content-list',
optional => 1,
},
disable => {
type => 'boolean',
optional => 1,
},
shared => {
type => 'boolean',
optional => 1,
},
'format' => {
type => 'string', format => 'pve-storage-format',
optional => 1,
},
},
},
returns => { type => 'null' },
code => sub {
my ($param) = @_;
my $type = $param->{type};
delete $param->{type};
my $storeid = $param->{storage};
delete $param->{storage};
if ($param->{portal}) {
$param->{portal} = PVE::Storage::resolv_portal($param->{portal});
}
my $opts = PVE::Storage::parse_options($storeid, $type, $param, 1);
PVE::Storage::lock_storage_config(
sub {
my $cfg = cfs_read_file('storage.cfg');
if (my $scfg = PVE::Storage::storage_config ($cfg, $storeid, 1)) {
die "storage ID '$storeid' already defined\n";
}
$cfg->{ids}->{$storeid} = $opts;
if ($type eq 'lvm' && $opts->{base}) {
my ($baseid, $volname) = PVE::Storage::parse_volume_id ($opts->{base});
my $basecfg = PVE::Storage::storage_config ($cfg, $baseid, 1);
die "base storage ID '$baseid' does not exist\n" if !$basecfg;
# we only support iscsi for now
if (!($basecfg->{type} eq 'iscsi')) {
die "unsupported base type '$basecfg->{type}'";
}
my $path = PVE::Storage::path ($cfg, $opts->{base});
PVE::Storage::activate_storage($cfg, $baseid);
PVE::Storage::lvm_create_volume_group ($path, $opts->{vgname}, $opts->{shared});
}
# try to activate if enabled on local node,
# we only do this to detect errors/problems sooner
if (PVE::Storage::storage_check_enabled($cfg, $storeid, undef, 1)) {
PVE::Storage::activate_storage($cfg, $storeid);
}
cfs_write_file('storage.cfg', $cfg);
}, "create storage failed");
}});
__PACKAGE__->register_method ({
name => 'update',
protected => 1,
path => '{storage}',
method => 'PUT',
description => "Update storage configuration.",
parameters => {
additionalProperties => 0,
properties => {
storage => get_standard_option('pve-storage-id'),
nodes => get_standard_option('pve-node-list', { optional => 1 }),
content => {
type => 'string', format => 'pve-storage-content-list',
optional => 1,
},
'format' => {
type => 'string', format => 'pve-storage-format',
optional => 1,
},
disable => {
type => 'boolean',
optional => 1,
},
shared => {
type => 'boolean',
optional => 1,
},
options => {
type => 'string', format => 'pve-storage-options',
optional => 1,
},
digest => {
type => 'string',
optional => 1,
}
},
},
returns => { type => 'null' },
code => sub {
my ($param) = @_;
my $storeid = $param->{storage};
delete($param->{storage});
my $digest = $param->{digest};
delete($param->{digest});
PVE::Storage::lock_storage_config(
sub {
my $cfg = cfs_read_file('storage.cfg');
PVE::Storage::assert_if_modified ($cfg, $digest);
my $scfg = PVE::Storage::storage_config ($cfg, $storeid);
my $opts = PVE::Storage::parse_options($storeid, $scfg->{type}, $param);
foreach my $k (%$opts) {
$scfg->{$k} = $opts->{$k};
}
cfs_write_file('storage.cfg', $cfg);
}, "update storage failed");
return undef;
}});
__PACKAGE__->register_method ({
name => 'delete',
protected => 1,
path => '{storage}', # /storage/config/{storage}
method => 'DELETE',
description => "Delete storage configuration.",
parameters => {
additionalProperties => 0,
properties => {
storage => get_standard_option('pve-storage-id'),
},
},
returns => { type => 'null' },
code => sub {
my ($param) = @_;
my $storeid = $param->{storage};
delete($param->{storage});
PVE::Storage::lock_storage_config(
sub {
my $cfg = cfs_read_file('storage.cfg');
die "can't remove storage - storage is used as base of another storage\n"
if PVE::Storage::storage_is_used ($cfg, $storeid);
delete ($cfg->{ids}->{$storeid});
cfs_write_file('storage.cfg', $cfg);
}, "delete storage failed");
return undef;
}});
1;

257
PVE/API2/Storage/Content.pm Normal file
View File

@ -0,0 +1,257 @@
package PVE::API2::Storage::Content;
use strict;
use warnings;
use PVE::SafeSyslog;
use PVE::Cluster qw(cfs_read_file);
use PVE::Storage;
use PVE::INotify;
use PVE::Exception qw(raise_param_exc);
use PVE::RPCEnvironment;
use PVE::RESTHandler;
use PVE::JSONSchema qw(get_standard_option);
use base qw(PVE::RESTHandler);
my @ctypes = qw(images vztmpl iso backup);
__PACKAGE__->register_method ({
name => 'index',
path => '',
method => 'GET',
description => "List storage content.",
protected => 1,
proxyto => 'node',
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option('pve-storage-id'),
content => {
description => "Only list content of this type.",
type => 'string', format => 'pve-storage-content',
optional => 1,
},
vmid => get_standard_option
('pve-vmid', {
description => "Only list images for this VM",
optional => 1,
}),
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => {
volid => {
type => 'string'
}
},
},
links => [ { rel => 'child', href => "{volid}" } ],
},
code => sub {
my ($param) = @_;
my $cts = $param->{content} ? [ $param->{content} ] : [ @ctypes ];
my $storeid = $param->{storage};
my $cfg = cfs_read_file("storage.cfg");
my $scfg = PVE::Storage::storage_config ($cfg, $storeid);
my $res = [];
foreach my $ct (@$cts) {
my $data;
if ($ct eq 'images') {
$data = PVE::Storage::vdisk_list ($cfg, $storeid, $param->{vmid});
} elsif ($ct eq 'iso') {
$data = PVE::Storage::template_list ($cfg, $storeid, 'iso')
if !$param->{vmid};
} elsif ($ct eq 'vztmpl') {
$data = PVE::Storage::template_list ($cfg, $storeid, 'vztmpl')
if !$param->{vmid};
} elsif ($ct eq 'backup') {
$data = PVE::Storage::template_list ($cfg, $storeid, 'backup')
if !$param->{vmid};
}
next if !$data || !$data->{$storeid};
foreach my $item (@{$data->{$storeid}}) {
push @$res, $item;
}
}
return $res;
}});
__PACKAGE__->register_method ({
name => 'create',
path => '',
method => 'POST',
description => "Allocate disk images.",
protected => 1,
proxyto => 'node',
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option('pve-storage-id'),
filename => {
description => "The name of the file to create/upload.",
type => 'string',
},
vmid => get_standard_option('pve-vmid', { description => "Specify owner VM" } ),
size => {
description => "Size in kilobyte (1024 bytes). Optional suffixes 'M' (megabyte, 1024K) and 'G' (gigabyte, 1024M)",
type => 'string',
pattern => '\d+[MG]?',
},
'format' => {
type => 'string',
enum => ['raw', 'qcow2'],
requires => 'size',
optional => 1,
},
},
},
returns => {
description => "Volume identifier",
type => 'string',
},
code => sub {
my ($param) = @_;
my $storeid = $param->{storage};
my $name = $param->{filename};
my $sizestr = $param->{size};
my $size;
if ($sizestr =~ m/^\d+$/) {
$size = $sizestr;
} elsif ($sizestr =~ m/^(\d+)M$/) {
$size = $1 * 1024;
} elsif ($sizestr =~ m/^(\d+)G$/) {
$size = $1 * 1024 * 1024;
} else {
raise_param_exc({ size => "unable to parse size '$sizestr'" });
}
# extract FORMAT from name
if ($name =~ m/\.(raw|qcow2)$/) {
my $fmt = $1;
raise_param_exc({ format => "different storage formats ($param->{format} != $fmt)" })
if $param->{format} && $param->{format} ne $fmt;
$param->{format} = $fmt;
}
my $cfg = cfs_read_file('storage.cfg');
my $volid = PVE::Storage::vdisk_alloc ($cfg, $storeid, $param->{vmid},
$param->{format},
$name, $size);
return $volid;
}});
# we allow to pass volume names (without storage prefix) if the storage
# is specified as separate parameter.
my $real_volume_id = sub {
my ($storeid, $volume) = @_;
my $volid;
if ($volume =~ m/:/) {
eval {
my ($sid, $volname) = PVE::Storage::parse_volume_id ($volume);
raise_param_exc({ storage => "storage ID missmatch" })
if $storeid && $sid ne $storeid;
$volid = $volume;
};
raise_param_exc({ volume => $@}) if $@;
} else {
raise_param_exc({ volume => "no storage speficied - incomplete volume ID" })
if !$storeid;
$volid = "$storeid:$volume";
}
return $volid;
};
__PACKAGE__->register_method ({
name => 'info',
path => '{volume}',
method => 'GET',
description => "Get volume attributes",
protected => 1,
proxyto => 'node',
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option('pve-storage-id', { optional => 1 }),
volume => {
description => "Volume identifier",
type => 'string',
},
},
},
returns => { type => 'object' },
code => sub {
my ($param) = @_;
my $volid = &$real_volume_id($param->{storage}, $param->{volume});
my $cfg = cfs_read_file('storage.cfg');
my $path = PVE::Storage::path($cfg, $volid);
my ($size, $format, $used) = PVE::Storage::file_size_info ($path);
# fixme: return more attributes?
return {
path => $path,
size => $size,
used => $used,
};
}});
__PACKAGE__->register_method ({
name => 'delete',
path => '{volume}',
method => 'DELETE',
description => "Delete volume",
protected => 1,
proxyto => 'node',
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option('pve-storage-id', { optional => 1}),
volume => {
description => "Volume identifier",
type => 'string',
},
},
},
returns => { type => 'null' },
code => sub {
my ($param) = @_;
my $volid = &$real_volume_id($param->{storage}, $param->{volume});
my $cfg = cfs_read_file('storage.cfg');
PVE::Storage::vdisk_free ($cfg, $volid);
return undef;
}});
1;

View File

@ -0,0 +1,6 @@
SOURCES= Content.pm Status.pm Config.pm Scan.pm
.PHONY: install
install:
for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/API2/Storage/$$i; done

190
PVE/API2/Storage/Scan.pm Normal file
View File

@ -0,0 +1,190 @@
package PVE::API2::Storage::Scan;
use strict;
use warnings;
use PVE::SafeSyslog;
use PVE::Storage;
use HTTP::Status qw(:constants);
use PVE::JSONSchema qw(get_standard_option);
use PVE::RESTHandler;
use base qw(PVE::RESTHandler);
__PACKAGE__->register_method ({
name => 'index',
path => '',
method => 'GET',
description => "Index of available scan methods",
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => { method => { type => 'string'} },
},
links => [ { rel => 'child', href => "{method}" } ],
},
code => sub {
my ($param) = @_;
my $res = [
{ method => 'lvm' },
{ method => 'iscsi' },
{ method => 'nfs' },
{ method => 'usb' },
];
return $res;
}});
__PACKAGE__->register_method ({
name => 'nfsscan',
path => 'nfs',
method => 'GET',
description => "Scan remote NFS server.",
protected => 1,
proxyto => "node",
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
server => { type => 'string', format => 'pve-storage-server' },
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => {
path => { type => 'string'},
options => { type => 'string'},
},
},
},
code => sub {
my ($param) = @_;
my $server = $param->{server};
my $res = PVE::Storage::scan_nfs($server);
my $data = [];
foreach my $k (keys %$res) {
push @$data, { path => $k, options => $res->{$k} };
}
return $data;
}});
__PACKAGE__->register_method ({
name => 'iscsiscan',
path => 'iscsi',
method => 'GET',
description => "Scan remote iSCSI server.",
protected => 1,
proxyto => "node",
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
portal => { type => 'string', format => 'pve-storage-portal-dns' },
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => {
target => { type => 'string'},
portal => { type => 'string'},
},
},
},
code => sub {
my ($param) = @_;
my $res = PVE::Storage::scan_iscsi($param->{portal});
my $data = [];
foreach my $k (keys %$res) {
push @$data, { target => $k, portal => join(',', @{$res->{$k}}) };
}
return $data;
}});
__PACKAGE__->register_method ({
name => 'lvmscan',
path => 'lvm',
method => 'GET',
description => "List local LVM volume groups.",
protected => 1,
proxyto => "node",
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => {
vg => { type => 'string'},
},
},
},
code => sub {
my ($param) = @_;
my $res = PVE::Storage::lvm_vgs();
return PVE::RESTHandler::hash_to_array($res, 'vg');
}});
__PACKAGE__->register_method ({
name => 'usbscan',
path => 'usb',
method => 'GET',
description => "List local USB devices.",
protected => 1,
proxyto => "node",
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => {
busnum => { type => 'integer'},
devnum => { type => 'integer'},
port => { type => 'integer'},
usbpath => { type => 'string', optional => 1},
level => { type => 'integer'},
class => { type => 'integer'},
vendid => { type => 'string'},
prodid => { type => 'string'},
speed => { type => 'string'},
product => { type => 'string', optional => 1 },
serial => { type => 'string', optional => 1 },
manufacturer => { type => 'string', optional => 1 },
},
},
},
code => sub {
my ($param) = @_;
return PVE::Storage::scan_usb();
}});
1;

228
PVE/API2/Storage/Status.pm Normal file
View File

@ -0,0 +1,228 @@
package PVE::API2::Storage::Status;
use strict;
use warnings;
use PVE::Cluster qw(cfs_read_file);
use PVE::Storage;
use PVE::API2::Storage::Content;
use PVE::RESTHandler;
use PVE::RPCEnvironment;
use PVE::JSONSchema qw(get_standard_option);
use PVE::Exception qw(raise_param_exc);
use base qw(PVE::RESTHandler);
__PACKAGE__->register_method ({
subclass => "PVE::API2::Storage::Content",
# set fragment delimiter (no subdirs) - we need that, because volume
# IDs may contain a slash '/'
fragmentDelimiter => '',
path => '{storage}/content',
});
__PACKAGE__->register_method ({
name => 'index',
path => '',
method => 'GET',
description => "Get status for all datastores.",
protected => 1,
proxyto => 'node',
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option
('pve-storage-id', {
description => "Only list status for specified storage",
optional => 1,
}),
content => {
description => "Only list stores which support this content type.",
type => 'string', format => 'pve-storage-content',
optional => 1,
},
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => { storage => { type => 'string' } },
},
links => [ { rel => 'child', href => "{storage}" } ],
},
code => sub {
my ($param) = @_;
my $cfg = cfs_read_file("storage.cfg");
my $info = PVE::Storage::storage_info($cfg, $param->{content});
if ($param->{storage}) {
my $data = $info->{$param->{storage}};
raise_param_exc({ storage => "No such storage." })
if !defined($data);
$data->{storage} = $param->{storage};
return [ $data ];
}
return PVE::RESTHandler::hash_to_array($info, 'storage');
}});
__PACKAGE__->register_method ({
name => 'diridx',
path => '{storage}',
method => 'GET',
description => "",
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option('pve-storage-id'),
},
},
returns => {
type => 'array',
items => {
type => "object",
properties => {
subdir => { type => 'string' },
},
},
links => [ { rel => 'child', href => "{subdir}" } ],
},
code => sub {
my ($param) = @_;
my $res = [
{ subdir => 'status' },
{ subdir => 'content' },
{ subdir => 'rrd' },
{ subdir => 'rrddata' },
];
return $res;
}});
__PACKAGE__->register_method ({
name => 'read_status',
path => '{storage}/status',
method => 'GET',
description => "Read storage status.",
protected => 1,
proxyto => 'node',
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option('pve-storage-id'),
},
},
returns => {
type => "object",
properties => {},
},
code => sub {
my ($param) = @_;
my $cfg = cfs_read_file("storage.cfg");
my $info = PVE::Storage::storage_info($cfg, $param->{content});
my $data = $info->{$param->{storage}};
raise_param_exc({ storage => "No such storage." })
if !defined($data);
return $data;
}});
__PACKAGE__->register_method ({
name => 'rrd',
path => '{storage}/rrd',
method => 'GET',
description => "Read storage RRD statistics (returns PNG).",
protected => 1,
proxyto => 'node',
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option('pve-storage-id'),
timeframe => {
description => "Specify the time frame you are interested in.",
type => 'string',
enum => [ 'hour', 'day', 'week', 'month', 'year' ],
},
ds => {
description => "The list of datasources you want to display.",
type => 'string', format => 'pve-configid-list',
},
cf => {
description => "The RRD consolidation function",
type => 'string',
enum => [ 'AVERAGE', 'MAX' ],
optional => 1,
},
},
},
returns => {
type => "object",
properties => {
filename => { type => 'string' },
},
},
code => sub {
my ($param) = @_;
return PVE::Cluster::create_rrd_graph(
"pve2-storage/$param->{node}/$param->{storage}",
$param->{timeframe}, $param->{ds}, $param->{cf});
}});
__PACKAGE__->register_method ({
name => 'rrddata',
path => '{storage}/rrddata',
method => 'GET',
description => "Read storage RRD statistics.",
protected => 1,
proxyto => 'node',
parameters => {
additionalProperties => 0,
properties => {
node => get_standard_option('pve-node'),
storage => get_standard_option('pve-storage-id'),
timeframe => {
description => "Specify the time frame you are interested in.",
type => 'string',
enum => [ 'hour', 'day', 'week', 'month', 'year' ],
},
cf => {
description => "The RRD consolidation function",
type => 'string',
enum => [ 'AVERAGE', 'MAX' ],
optional => 1,
},
},
},
returns => {
type => "array",
items => {
type => "object",
properties => {},
},
},
code => sub {
my ($param) = @_;
return PVE::Cluster::create_rrd_data(
"pve2-storage/$param->{node}/$param->{storage}",
$param->{timeframe}, $param->{cf});
}});
1;