mirror of
git://git.proxmox.com/git/pve-storage.git
synced 2024-12-22 13:34:16 +03:00
new plugin architecture
This commit is contained in:
parent
5c009b733e
commit
1dc01b9f30
@ -4,8 +4,10 @@ use strict;
|
||||
use warnings;
|
||||
|
||||
use PVE::SafeSyslog;
|
||||
use PVE::Tools qw(extract_param);
|
||||
use PVE::Cluster qw(cfs_read_file cfs_write_file);
|
||||
use PVE::Storage;
|
||||
use PVE::Storage::Plugin;
|
||||
use HTTP::Status qw(:constants);
|
||||
use Storable qw(dclone);
|
||||
use PVE::JSONSchema qw(get_standard_option);
|
||||
@ -17,19 +19,18 @@ use base qw(PVE::RESTHandler);
|
||||
|
||||
my @ctypes = qw(images vztmpl iso backup);
|
||||
|
||||
my $storage_type_enum = ['dir', 'nfs', 'lvm', 'iscsi'];
|
||||
my $storage_type_enum = PVE::Storage::Plugin->lookup_types();
|
||||
|
||||
my $api_storage_config = sub {
|
||||
my ($cfg, $storeid) = @_;
|
||||
|
||||
my $scfg = dclone(PVE::Storage::storage_config ($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});
|
||||
$scfg->{content} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'content', $scfg->{content});
|
||||
|
||||
if ($scfg->{nodes}) {
|
||||
$scfg->{nodes} = join(',', keys(%{$scfg->{nodes}}));
|
||||
$scfg->{nodes} = PVE::Storage::Plugin->encode_value($scfg->{type}, 'nodes', $scfg->{nodes});
|
||||
}
|
||||
|
||||
return $scfg;
|
||||
@ -119,92 +120,27 @@ __PACKAGE__->register_method ({
|
||||
permissions => {
|
||||
check => ['perm', '/storage', ['Datastore.Allocate']],
|
||||
},
|
||||
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,
|
||||
},
|
||||
maxfiles => {
|
||||
type => 'integer',
|
||||
optional => 1,
|
||||
minimum => 0,
|
||||
},
|
||||
shared => {
|
||||
type => 'boolean',
|
||||
optional => 1,
|
||||
},
|
||||
'format' => {
|
||||
type => 'string', format => 'pve-storage-format',
|
||||
optional => 1,
|
||||
},
|
||||
},
|
||||
},
|
||||
parameters => PVE::Storage::Plugin->createSchema(),
|
||||
returns => { type => 'null' },
|
||||
code => sub {
|
||||
my ($param) = @_;
|
||||
|
||||
my $type = $param->{type};
|
||||
delete $param->{type};
|
||||
|
||||
my $storeid = $param->{storage};
|
||||
delete $param->{storage};
|
||||
my $type = extract_param($param, 'type');
|
||||
my $storeid = extract_param($param, 'storage');
|
||||
|
||||
if ($param->{portal}) {
|
||||
$param->{portal} = PVE::Storage::resolv_portal($param->{portal});
|
||||
}
|
||||
|
||||
my $opts = PVE::Storage::parse_options($storeid, $type, $param, 1);
|
||||
my $plugin = PVE::Storage::Plugin->lookup($type);
|
||||
my $opts = $plugin->check_config($storeid, $param, 1, 1);
|
||||
|
||||
PVE::Storage::lock_storage_config(
|
||||
sub {
|
||||
|
||||
my $cfg = cfs_read_file('storage.cfg');
|
||||
|
||||
if (my $scfg = PVE::Storage::storage_config ($cfg, $storeid, 1)) {
|
||||
if (my $scfg = PVE::Storage::storage_config($cfg, $storeid, 1)) {
|
||||
die "storage ID '$storeid' already defined\n";
|
||||
}
|
||||
|
||||
@ -212,7 +148,7 @@ __PACKAGE__->register_method ({
|
||||
|
||||
if ($type eq 'lvm' && $opts->{base}) {
|
||||
|
||||
my ($baseid, $volname) = PVE::Storage::parse_volume_id ($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;
|
||||
@ -222,11 +158,11 @@ __PACKAGE__->register_method ({
|
||||
die "unsupported base type '$basecfg->{type}'";
|
||||
}
|
||||
|
||||
my $path = PVE::Storage::path ($cfg, $opts->{base});
|
||||
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});
|
||||
PVE::Storage::LVMPlugin::lvm_create_volume_group($path, $opts->{vgname}, $opts->{shared});
|
||||
}
|
||||
|
||||
# try to activate if enabled on local node,
|
||||
@ -239,6 +175,7 @@ __PACKAGE__->register_method ({
|
||||
|
||||
}, "create storage failed");
|
||||
|
||||
return undef;
|
||||
}});
|
||||
|
||||
__PACKAGE__->register_method ({
|
||||
@ -250,64 +187,25 @@ __PACKAGE__->register_method ({
|
||||
permissions => {
|
||||
check => ['perm', '/storage', ['Datastore.Allocate']],
|
||||
},
|
||||
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,
|
||||
},
|
||||
maxfiles => {
|
||||
type => 'integer',
|
||||
optional => 1,
|
||||
minimum => 0,
|
||||
},
|
||||
digest => {
|
||||
type => 'string',
|
||||
description => 'Prevent changes if current configuration file has different SHA1 digest. This can be used to prevent concurrent modifications.',
|
||||
maxLength => 40,
|
||||
optional => 1,
|
||||
}
|
||||
},
|
||||
},
|
||||
parameters => PVE::Storage::Plugin->updateSchema(),
|
||||
returns => { type => 'null' },
|
||||
code => sub {
|
||||
my ($param) = @_;
|
||||
|
||||
my $storeid = $param->{storage};
|
||||
delete($param->{storage});
|
||||
|
||||
my $digest = $param->{digest};
|
||||
delete($param->{digest});
|
||||
my $storeid = extract_param($param, 'storage');
|
||||
my $digest = extract_param($param, 'digest');
|
||||
|
||||
PVE::Storage::lock_storage_config(
|
||||
sub {
|
||||
|
||||
my $cfg = cfs_read_file('storage.cfg');
|
||||
|
||||
PVE::Storage::assert_if_modified ($cfg, $digest);
|
||||
PVE::SectionConfig::assert_if_modified($cfg, $digest);
|
||||
|
||||
my $scfg = PVE::Storage::storage_config ($cfg, $storeid);
|
||||
my $scfg = PVE::Storage::storage_config($cfg, $storeid);
|
||||
|
||||
my $opts = PVE::Storage::parse_options($storeid, $scfg->{type}, $param);
|
||||
my $plugin = PVE::Storage::Plugin->lookup($scfg->{type});
|
||||
my $opts = $plugin->check_config($storeid, $param, 0, 1);
|
||||
|
||||
foreach my $k (%$opts) {
|
||||
$scfg->{$k} = $opts->{$k};
|
||||
@ -339,18 +237,17 @@ __PACKAGE__->register_method ({
|
||||
code => sub {
|
||||
my ($param) = @_;
|
||||
|
||||
my $storeid = $param->{storage};
|
||||
delete($param->{storage});
|
||||
|
||||
my $storeid = extract_param($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);
|
||||
if PVE::Storage::storage_is_used($cfg, $storeid);
|
||||
|
||||
delete ($cfg->{ids}->{$storeid});
|
||||
delete $cfg->{ids}->{$storeid};
|
||||
|
||||
cfs_write_file('storage.cfg', $cfg);
|
||||
|
||||
|
@ -3,4 +3,5 @@
|
||||
.PHONY: install
|
||||
install:
|
||||
install -D -m 0644 Storage.pm ${DESTDIR}${PERLDIR}/PVE/Storage.pm
|
||||
make -C Storage install
|
||||
make -C API2 install
|
1852
PVE/Storage.pm
1852
PVE/Storage.pm
File diff suppressed because it is too large
Load Diff
58
PVE/Storage/DirPlugin.pm
Normal file
58
PVE/Storage/DirPlugin.pm
Normal file
@ -0,0 +1,58 @@
|
||||
package PVE::Storage::DirPlugin;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use File::Path;
|
||||
use PVE::Storage::Plugin;
|
||||
use PVE::JSONSchema qw(get_standard_option);
|
||||
|
||||
use base qw(PVE::Storage::Plugin);
|
||||
|
||||
# Configuration
|
||||
|
||||
sub type {
|
||||
return 'dir';
|
||||
}
|
||||
|
||||
sub plugindata {
|
||||
return {
|
||||
content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1, none => 1 },
|
||||
{ images => 1, rootdir => 1 }],
|
||||
format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
|
||||
};
|
||||
}
|
||||
|
||||
sub properties {
|
||||
return {
|
||||
path => {
|
||||
description => "File system path.",
|
||||
type => 'string', format => 'pve-storage-path',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
sub options {
|
||||
return {
|
||||
path => { fixed => 1 },
|
||||
nodes => { optional => 1 },
|
||||
shared => { optional => 1 },
|
||||
disable => { optional => 1 },
|
||||
maxfiles => { optional => 1 },
|
||||
content => { optional => 1 },
|
||||
format => { optional => 1 },
|
||||
};
|
||||
}
|
||||
|
||||
# Storage implementation
|
||||
|
||||
sub activate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
my $path = $scfg->{path};
|
||||
mkpath $path;
|
||||
|
||||
$class->SUPER::activate_storage($storeid, $scfg, $cache);
|
||||
}
|
||||
|
||||
|
||||
1;
|
374
PVE/Storage/ISCSIPlugin.pm
Normal file
374
PVE/Storage/ISCSIPlugin.pm
Normal file
@ -0,0 +1,374 @@
|
||||
package PVE::Storage::ISCSIPlugin;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use File::stat;
|
||||
use IO::Dir;
|
||||
use IO::File;
|
||||
use PVE::Tools qw(run_command file_read_firstline trim dir_glob_regex dir_glob_foreach);
|
||||
use PVE::Storage::Plugin;
|
||||
use PVE::JSONSchema qw(get_standard_option);
|
||||
use Net::Ping;
|
||||
|
||||
use base qw(PVE::Storage::Plugin);
|
||||
|
||||
# iscsi helper function
|
||||
|
||||
my $ISCSIADM = '/usr/bin/iscsiadm';
|
||||
$ISCSIADM = undef if ! -X $ISCSIADM;
|
||||
|
||||
sub check_iscsi_support {
|
||||
my $noerr = shift;
|
||||
|
||||
if (!$ISCSIADM) {
|
||||
my $msg = "no iscsi support - please install open-iscsi";
|
||||
if ($noerr) {
|
||||
warn "warning: $msg\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
die "error: $msg\n";
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
sub iscsi_session_list {
|
||||
|
||||
check_iscsi_support ();
|
||||
|
||||
my $cmd = [$ISCSIADM, '--mode', 'session'];
|
||||
|
||||
my $res = {};
|
||||
|
||||
run_command($cmd, outfunc => sub {
|
||||
my $line = shift;
|
||||
|
||||
if ($line =~ m/^tcp:\s+\[(\S+)\]\s+\S+\s+(\S+)\s*$/) {
|
||||
my ($session, $target) = ($1, $2);
|
||||
# there can be several sessions per target (multipath)
|
||||
push @{$res->{$target}}, $session;
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
sub iscsi_test_portal {
|
||||
my ($portal) = @_;
|
||||
|
||||
my ($server, $port) = split(':', $portal);
|
||||
my $p = Net::Ping->new("tcp", 2);
|
||||
$p->port_number($port || 3260);
|
||||
return $p->ping($server);
|
||||
}
|
||||
|
||||
sub iscsi_discovery {
|
||||
my ($portal) = @_;
|
||||
|
||||
check_iscsi_support ();
|
||||
|
||||
my $cmd = [$ISCSIADM, '--mode', 'discovery', '--type', 'sendtargets',
|
||||
'--portal', $portal];
|
||||
|
||||
my $res = {};
|
||||
|
||||
return $res if !iscsi_test_portal($portal); # fixme: raise exception here?
|
||||
|
||||
run_command($cmd, outfunc => sub {
|
||||
my $line = shift;
|
||||
|
||||
if ($line =~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}:\d+)\,\S+\s+(\S+)\s*$/) {
|
||||
my $portal = $1;
|
||||
my $target = $2;
|
||||
# one target can have more than one portal (multipath).
|
||||
push @{$res->{$target}}, $portal;
|
||||
}
|
||||
});
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
sub iscsi_login {
|
||||
my ($target, $portal_in) = @_;
|
||||
|
||||
check_iscsi_support ();
|
||||
|
||||
eval { iscsi_discovery ($portal_in); };
|
||||
warn $@ if $@;
|
||||
|
||||
my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--login'];
|
||||
run_command($cmd);
|
||||
}
|
||||
|
||||
sub iscsi_logout {
|
||||
my ($target, $portal) = @_;
|
||||
|
||||
check_iscsi_support ();
|
||||
|
||||
my $cmd = [$ISCSIADM, '--mode', 'node', '--targetname', $target, '--logout'];
|
||||
run_command($cmd);
|
||||
}
|
||||
|
||||
my $rescan_filename = "/var/run/pve-iscsi-rescan.lock";
|
||||
|
||||
sub iscsi_session_rescan {
|
||||
my $session_list = shift;
|
||||
|
||||
check_iscsi_support();
|
||||
|
||||
my $rstat = stat($rescan_filename);
|
||||
|
||||
if (!$rstat) {
|
||||
if (my $fh = IO::File->new($rescan_filename, "a")) {
|
||||
utime undef, undef, $fh;
|
||||
close($fh);
|
||||
}
|
||||
} else {
|
||||
my $atime = $rstat->atime;
|
||||
my $tdiff = time() - $atime;
|
||||
# avoid frequent rescans
|
||||
return if !($tdiff < 0 || $tdiff > 10);
|
||||
utime undef, undef, $rescan_filename;
|
||||
}
|
||||
|
||||
foreach my $session (@$session_list) {
|
||||
my $cmd = [$ISCSIADM, '--mode', 'session', '-r', $session, '-R'];
|
||||
eval { run_command($cmd, outfunc => sub {}); };
|
||||
warn $@ if $@;
|
||||
}
|
||||
}
|
||||
|
||||
sub load_stable_scsi_paths {
|
||||
|
||||
my $stable_paths = {};
|
||||
|
||||
my $stabledir = "/dev/disk/by-id";
|
||||
|
||||
if (my $dh = IO::Dir->new($stabledir)) {
|
||||
while (defined(my $tmp = $dh->read)) {
|
||||
# exclude filenames with part in name (same disk but partitions)
|
||||
# use only filenames with scsi(with multipath i have the same device
|
||||
# with dm-uuid-mpath , dm-name and scsi in name)
|
||||
if($tmp !~ m/-part\d+$/ && $tmp =~ m/^scsi-/) {
|
||||
my $path = "$stabledir/$tmp";
|
||||
my $bdevdest = readlink($path);
|
||||
if ($bdevdest && $bdevdest =~ m|^../../([^/]+)|) {
|
||||
$stable_paths->{$1}=$tmp;
|
||||
}
|
||||
}
|
||||
}
|
||||
$dh->close;
|
||||
}
|
||||
return $stable_paths;
|
||||
}
|
||||
|
||||
sub iscsi_device_list {
|
||||
|
||||
my $res = {};
|
||||
|
||||
my $dirname = '/sys/class/iscsi_session';
|
||||
|
||||
my $stable_paths = load_stable_scsi_paths();
|
||||
|
||||
dir_glob_foreach($dirname, 'session(\d+)', sub {
|
||||
my ($ent, $session) = @_;
|
||||
|
||||
my $target = file_read_firstline("$dirname/$ent/targetname");
|
||||
return if !$target;
|
||||
|
||||
my (undef, $host) = dir_glob_regex("$dirname/$ent/device", 'target(\d+):.*');
|
||||
return if !defined($host);
|
||||
|
||||
dir_glob_foreach("/sys/bus/scsi/devices", "$host:" . '(\d+):(\d+):(\d+)', sub {
|
||||
my ($tmp, $channel, $id, $lun) = @_;
|
||||
|
||||
my $type = file_read_firstline("/sys/bus/scsi/devices/$tmp/type");
|
||||
return if !defined($type) || $type ne '0'; # list disks only
|
||||
|
||||
my $bdev;
|
||||
if (-d "/sys/bus/scsi/devices/$tmp/block") { # newer kernels
|
||||
(undef, $bdev) = dir_glob_regex("/sys/bus/scsi/devices/$tmp/block/", '([A-Za-z]\S*)');
|
||||
} else {
|
||||
(undef, $bdev) = dir_glob_regex("/sys/bus/scsi/devices/$tmp", 'block:(\S+)');
|
||||
}
|
||||
return if !$bdev;
|
||||
|
||||
#check multipath
|
||||
if (-d "/sys/block/$bdev/holders") {
|
||||
my $multipathdev = dir_glob_regex("/sys/block/$bdev/holders", '[A-Za-z]\S*');
|
||||
$bdev = $multipathdev if $multipathdev;
|
||||
}
|
||||
|
||||
my $blockdev = $stable_paths->{$bdev};
|
||||
return if !$blockdev;
|
||||
|
||||
my $size = file_read_firstline("/sys/block/$bdev/size");
|
||||
return if !$size;
|
||||
|
||||
my $volid = "$channel.$id.$lun.$blockdev";
|
||||
|
||||
$res->{$target}->{$volid} = {
|
||||
'format' => 'raw',
|
||||
'size' => int($size * 512),
|
||||
'vmid' => 0, # not assigned to any vm
|
||||
'channel' => int($channel),
|
||||
'id' => int($id),
|
||||
'lun' => int($lun),
|
||||
};
|
||||
|
||||
#print "TEST: $target $session $host,$bus,$tg,$lun $blockdev\n";
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
# Configuration
|
||||
|
||||
sub type {
|
||||
return 'iscsi';
|
||||
}
|
||||
|
||||
sub plugindata {
|
||||
return {
|
||||
content => [ {images => 1, none => 1}, { images => 1 }],
|
||||
};
|
||||
}
|
||||
|
||||
sub properties {
|
||||
return {
|
||||
target => {
|
||||
description => "iSCSI target.",
|
||||
type => 'string',
|
||||
},
|
||||
portal => {
|
||||
description => "iSCSI portal (IP or DNS name with optional port).",
|
||||
type => 'string', format => 'pve-storage-portal-dns',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
sub options {
|
||||
return {
|
||||
portal => { fixed => 1 },
|
||||
target => { fixed => 1 },
|
||||
nodes => { optional => 1},
|
||||
disable => { optional => 1},
|
||||
content => { optional => 1},
|
||||
};
|
||||
}
|
||||
|
||||
# Storage implementation
|
||||
|
||||
sub parse_volname {
|
||||
my ($class, $volname) = @_;
|
||||
|
||||
if ($volname =~ m!^\d+\.\d+\.\d+\.(\S+)$!) {
|
||||
return ('images', $1, undef);
|
||||
}
|
||||
|
||||
die "unable to parse iscsi volume name '$volname'\n";
|
||||
}
|
||||
|
||||
sub path {
|
||||
my ($class, $scfg, $volname) = @_;
|
||||
|
||||
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
|
||||
|
||||
my $path = "/dev/disk/by-id/$name";
|
||||
|
||||
return ($path, $vmid, $vtype);
|
||||
}
|
||||
|
||||
sub alloc_image {
|
||||
my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
|
||||
|
||||
die "can't allocate space in iscsi storage\n";
|
||||
}
|
||||
|
||||
sub free_image {
|
||||
my ($class, $storeid, $scfg, $volname) = @_;
|
||||
|
||||
die "can't free space in iscsi storage\n";
|
||||
}
|
||||
|
||||
sub list_images {
|
||||
my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
|
||||
|
||||
my $res = [];
|
||||
|
||||
$cache->{iscsi_devices} = iscsi_device_list() if !$cache->{iscsi_devices};
|
||||
|
||||
# we have no owner for iscsi devices
|
||||
|
||||
my $target = $scfg->{target};
|
||||
|
||||
if (my $dat = $cache->{iscsi_devices}->{$target}) {
|
||||
|
||||
foreach my $volname (keys %$dat) {
|
||||
|
||||
my $volid = "$storeid:$volname";
|
||||
|
||||
if ($vollist) {
|
||||
my $found = grep { $_ eq $volid } @$vollist;
|
||||
next if !$found;
|
||||
} else {
|
||||
# we have no owner for iscsi devices
|
||||
next if defined($vmid);
|
||||
}
|
||||
|
||||
my $info = $dat->{$volname};
|
||||
$info->{volid} = $volid;
|
||||
|
||||
push @$res, $info;
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
sub status {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
$cache->{iscsi_sessions} = iscsi_session_list() if !$cache->{iscsi_sessions};
|
||||
|
||||
my $active = defined($cache->{iscsi_sessions}->{$scfg->{target}});
|
||||
|
||||
return (0, 0, 0, $active);
|
||||
}
|
||||
|
||||
sub activate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
return if !check_iscsi_support(1);
|
||||
|
||||
$cache->{iscsi_sessions} = iscsi_session_list() if !$cache->{iscsi_sessions};
|
||||
|
||||
my $iscsi_sess = $cache->{iscsi_sessions}->{$scfg->{target}};
|
||||
if (!defined ($iscsi_sess)) {
|
||||
eval { iscsi_login($scfg->{target}, $scfg->{portal}); };
|
||||
warn $@ if $@;
|
||||
} else {
|
||||
# make sure we get all devices
|
||||
iscsi_session_rescan($iscsi_sess);
|
||||
}
|
||||
}
|
||||
|
||||
sub deactivate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
return if !check_iscsi_support(1);
|
||||
|
||||
$cache->{iscsi_sessions} = iscsi_session_list() if !$cache->{iscsi_sessions};
|
||||
|
||||
my $iscsi_sess = $cache->{iscsi_sessions}->{$scfg->{target}};
|
||||
|
||||
if (defined ($iscsi_sess)) {
|
||||
iscsi_logout($scfg->{target}, $scfg->{portal});
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
411
PVE/Storage/LVMPlugin.pm
Normal file
411
PVE/Storage/LVMPlugin.pm
Normal file
@ -0,0 +1,411 @@
|
||||
package PVE::Storage::LVMPlugin;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use IO::File;
|
||||
use PVE::Tools qw(run_command trim);
|
||||
use PVE::Storage::Plugin;
|
||||
use PVE::JSONSchema qw(get_standard_option);
|
||||
|
||||
use base qw(PVE::Storage::Plugin);
|
||||
|
||||
# lvm helper functions
|
||||
|
||||
sub lvm_pv_info {
|
||||
my ($device) = @_;
|
||||
|
||||
die "no device specified" if !$device;
|
||||
|
||||
my $has_label = 0;
|
||||
|
||||
my $cmd = ['/usr/bin/file', '-L', '-s', $device];
|
||||
run_command($cmd, outfunc => sub {
|
||||
my $line = shift;
|
||||
$has_label = 1 if $line =~ m/LVM2/;
|
||||
});
|
||||
|
||||
return undef if !$has_label;
|
||||
|
||||
$cmd = ['/sbin/pvs', '--separator', ':', '--noheadings', '--units', 'k',
|
||||
'--unbuffered', '--nosuffix', '--options',
|
||||
'pv_name,pv_size,vg_name,pv_uuid', $device];
|
||||
|
||||
my $pvinfo;
|
||||
run_command($cmd, outfunc => sub {
|
||||
my $line = shift;
|
||||
|
||||
$line = trim($line);
|
||||
|
||||
my ($pvname, $size, $vgname, $uuid) = split(':', $line);
|
||||
|
||||
die "found multiple pvs entries for device '$device'\n"
|
||||
if $pvinfo;
|
||||
|
||||
$pvinfo = {
|
||||
pvname => $pvname,
|
||||
size => $size,
|
||||
vgname => $vgname,
|
||||
uuid => $uuid,
|
||||
};
|
||||
});
|
||||
|
||||
return $pvinfo;
|
||||
}
|
||||
|
||||
sub clear_first_sector {
|
||||
my ($dev) = shift;
|
||||
|
||||
if (my $fh = IO::File->new($dev, "w")) {
|
||||
my $buf = 0 x 512;
|
||||
syswrite $fh, $buf;
|
||||
$fh->close();
|
||||
}
|
||||
}
|
||||
|
||||
sub lvm_create_volume_group {
|
||||
my ($device, $vgname, $shared) = @_;
|
||||
|
||||
my $res = lvm_pv_info($device);
|
||||
|
||||
if ($res->{vgname}) {
|
||||
return if $res->{vgname} eq $vgname; # already created
|
||||
die "device '$device' is already used by volume group '$res->{vgname}'\n";
|
||||
}
|
||||
|
||||
clear_first_sector($device); # else pvcreate fails
|
||||
|
||||
# we use --metadatasize 250k, which reseults in "pe_start = 512"
|
||||
# so pe_start is aligned on a 128k boundary (advantage for SSDs)
|
||||
my $cmd = ['/sbin/pvcreate', '--metadatasize', '250k', $device];
|
||||
|
||||
run_command($cmd, errmsg => "pvcreate '$device' error");
|
||||
|
||||
$cmd = ['/sbin/vgcreate', $vgname, $device];
|
||||
# push @$cmd, '-c', 'y' if $shared; # we do not use this yet
|
||||
|
||||
run_command($cmd, errmsg => "vgcreate $vgname $device error");
|
||||
}
|
||||
|
||||
sub lvm_vgs {
|
||||
|
||||
my $cmd = ['/sbin/vgs', '--separator', ':', '--noheadings', '--units', 'b',
|
||||
'--unbuffered', '--nosuffix', '--options',
|
||||
'vg_name,vg_size,vg_free'];
|
||||
|
||||
my $vgs = {};
|
||||
eval {
|
||||
run_command($cmd, outfunc => sub {
|
||||
my $line = shift;
|
||||
|
||||
$line = trim($line);
|
||||
|
||||
my ($name, $size, $free) = split (':', $line);
|
||||
|
||||
$vgs->{$name} = { size => int ($size), free => int ($free) };
|
||||
});
|
||||
};
|
||||
my $err = $@;
|
||||
|
||||
# just warn (vgs return error code 5 if clvmd does not run)
|
||||
# but output is still OK (list without clustered VGs)
|
||||
warn $err if $err;
|
||||
|
||||
return $vgs;
|
||||
}
|
||||
|
||||
sub lvm_lvs {
|
||||
my ($vgname) = @_;
|
||||
|
||||
my $cmd = ['/sbin/lvs', '--separator', ':', '--noheadings', '--units', 'b',
|
||||
'--unbuffered', '--nosuffix', '--options',
|
||||
'vg_name,lv_name,lv_size,uuid,tags'];
|
||||
|
||||
push @$cmd, $vgname if $vgname;
|
||||
|
||||
my $lvs = {};
|
||||
run_command($cmd, outfunc => sub {
|
||||
my $line = shift;
|
||||
|
||||
$line = trim($line);
|
||||
|
||||
my ($vg, $name, $size, $uuid, $tags) = split(':', $line);
|
||||
|
||||
return if $name !~ m/^vm-(\d+)-/;
|
||||
my $nid = $1;
|
||||
|
||||
my $owner;
|
||||
foreach my $tag (split (/,/, $tags)) {
|
||||
if ($tag =~ m/^pve-vm-(\d+)$/) {
|
||||
$owner = $1;
|
||||
last;
|
||||
}
|
||||
}
|
||||
|
||||
if ($owner) {
|
||||
if ($owner ne $nid) {
|
||||
warn "owner mismatch name = $name, owner = $owner\n";
|
||||
}
|
||||
|
||||
$lvs->{$vg}->{$name} = { format => 'raw', size => $size,
|
||||
uuid => $uuid, tags => $tags,
|
||||
vmid => $owner };
|
||||
}
|
||||
});
|
||||
|
||||
return $lvs;
|
||||
}
|
||||
|
||||
# Configuration
|
||||
|
||||
PVE::JSONSchema::register_format('pve-storage-vgname', \&parse_lvm_name);
|
||||
sub parse_lvm_name {
|
||||
my ($name, $noerr) = @_;
|
||||
|
||||
if ($name !~ m/^[a-z][a-z0-9\-\_\.]*[a-z0-9]$/i) {
|
||||
return undef if $noerr;
|
||||
die "lvm name '$name' contains illegal characters\n";
|
||||
}
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
sub type {
|
||||
return 'lvm';
|
||||
}
|
||||
|
||||
sub plugindata {
|
||||
return {
|
||||
content => [ {images => 1}, { images => 1 }],
|
||||
};
|
||||
}
|
||||
|
||||
sub properties {
|
||||
return {
|
||||
vgname => {
|
||||
description => "Volume group name.",
|
||||
type => 'string', format => 'pve-storage-vgname',
|
||||
},
|
||||
base => {
|
||||
description => "Base volume. This volume is automatically activated.",
|
||||
type => 'string', format => 'pve-volume-id',
|
||||
},
|
||||
saferemove => {
|
||||
description => "Zero-out data when removing LVs.",
|
||||
type => 'boolean',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
sub options {
|
||||
return {
|
||||
vgname => { fixed => 1 },
|
||||
nodes => { optional => 1 },
|
||||
shared => { optional => 1 },
|
||||
disable => { optional => 1 },
|
||||
saferemove => { optional => 1 },
|
||||
content => { optional => 1 },
|
||||
base => { fixed => 1, optional => 1 },
|
||||
};
|
||||
}
|
||||
|
||||
# Storage implementation
|
||||
|
||||
sub parse_volname {
|
||||
my ($class, $volname) = @_;
|
||||
|
||||
parse_lvm_name($volname);
|
||||
|
||||
if ($volname =~ m/^(vm-(\d+)-\S+)$/) {
|
||||
return ('images', $1, $2);
|
||||
}
|
||||
|
||||
die "unable to parse lvm volume name '$volname'\n";
|
||||
}
|
||||
|
||||
sub path {
|
||||
my ($class, $scfg, $volname) = @_;
|
||||
|
||||
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
|
||||
|
||||
my $vg = $scfg->{vgname};
|
||||
|
||||
my $path = "/dev/$vg/$name";
|
||||
|
||||
return ($path, $vmid, $vtype);
|
||||
}
|
||||
|
||||
sub alloc_image {
|
||||
my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
|
||||
|
||||
die "unsupported format '$fmt'" if $fmt ne 'raw';
|
||||
|
||||
die "illegal name '$name' - sould be 'vm-$vmid-*'\n"
|
||||
if $name && $name !~ m/^vm-$vmid-/;
|
||||
|
||||
my $vgs = lvm_vgs();
|
||||
|
||||
my $vg = $scfg->{vgname};
|
||||
|
||||
die "no such volume gruoup '$vg'\n" if !defined ($vgs->{$vg});
|
||||
|
||||
my $free = int($vgs->{$vg}->{free});
|
||||
|
||||
die "not enough free space ($free < $size)\n" if $free < $size;
|
||||
|
||||
if (!$name) {
|
||||
my $lvs = lvm_lvs($vg);
|
||||
|
||||
for (my $i = 1; $i < 100; $i++) {
|
||||
my $tn = "vm-$vmid-disk-$i";
|
||||
if (!defined ($lvs->{$vg}->{$tn})) {
|
||||
$name = $tn;
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
|
||||
if !$name;
|
||||
|
||||
my $cmd = ['/sbin/lvcreate', '-aly', '--addtag', "pve-vm-$vmid", '--size', "${size}k", '--name', $name, $vg];
|
||||
|
||||
run_command($cmd, errmsg => "lvcreate '$vg/pve-vm-$vmid' error");
|
||||
|
||||
return $name;
|
||||
}
|
||||
|
||||
sub free_image {
|
||||
my ($class, $storeid, $scfg, $volname) = @_;
|
||||
|
||||
my $vg = $scfg->{vgname};
|
||||
|
||||
# we need to zero out LVM data for security reasons
|
||||
# and to allow thin provisioning
|
||||
|
||||
my $zero_out_worker = sub {
|
||||
print "zero-out data on image $volname\n";
|
||||
my $cmd = ['dd', "if=/dev/zero", "of=/dev/$vg/del-$volname", "bs=1M"];
|
||||
eval { run_command($cmd, errmsg => "zero out failed"); };
|
||||
warn $@ if $@;
|
||||
|
||||
$class->cluster_lock_storage($storeid, $scfg->{shared}, undef, sub {
|
||||
my $cmd = ['/sbin/lvremove', '-f', "$vg/del-$volname"];
|
||||
run_command($cmd, errmsg => "lvremove '$vg/del-$volname' error");
|
||||
});
|
||||
print "successfully removed volume $volname\n";
|
||||
};
|
||||
|
||||
if ($scfg->{saferemove}) {
|
||||
# avoid long running task, so we only rename here
|
||||
my $cmd = ['/sbin/lvrename', $vg, $volname, "del-$volname"];
|
||||
run_command($cmd, errmsg => "lvrename '$vg/$volname' error");
|
||||
return $zero_out_worker;
|
||||
} else {
|
||||
my $tmpvg = $scfg->{vgname};
|
||||
my $cmd = ['/sbin/lvremove', '-f', "$tmpvg/$volname"];
|
||||
run_command($cmd, errmsg => "lvremove '$tmpvg/$volname' error");
|
||||
}
|
||||
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub list_images {
|
||||
my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
|
||||
|
||||
my $vgname = $scfg->{vgname};
|
||||
|
||||
$cache->{lvs} = lvm_lvs() if !$cache->{lvs};
|
||||
|
||||
my $res = [];
|
||||
|
||||
if (my $dat = $cache->{lvs}->{$vgname}) {
|
||||
|
||||
foreach my $volname (keys %$dat) {
|
||||
|
||||
my $owner = $dat->{$volname}->{vmid};
|
||||
|
||||
my $volid = "$storeid:$volname";
|
||||
|
||||
if ($vollist) {
|
||||
my $found = grep { $_ eq $volid } @$vollist;
|
||||
next if !$found;
|
||||
} else {
|
||||
next if defined ($vmid) && ($owner ne $vmid);
|
||||
}
|
||||
|
||||
my $info = $dat->{$volname};
|
||||
$info->{volid} = $volid;
|
||||
|
||||
push @$res, $info;
|
||||
}
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
sub status {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
$cache->{vgs} = lvm_vgs() if !$cache->{vgs};
|
||||
|
||||
my $vgname = $scfg->{vgname};
|
||||
|
||||
my $total = 0;
|
||||
my $free = 0;
|
||||
my $used = 0;
|
||||
|
||||
if (my $info = $cache->{vgs}->{$vgname}) {
|
||||
return ($info->{size}, $info->{free}, $total - $free, 1);
|
||||
}
|
||||
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub activate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
$cache->{vgs} = lvm_vgs() if !$cache->{vgs};
|
||||
|
||||
# In LVM2, vgscans take place automatically;
|
||||
# this is just to be sure
|
||||
if ($cache->{vgs} && !$cache->{vgscaned} &&
|
||||
!$cache->{vgs}->{$scfg->{vgname}}) {
|
||||
$cache->{vgscaned} = 1;
|
||||
my $cmd = ['/sbin/vgscan', '--ignorelockingfailure', '--mknodes'];
|
||||
eval { run_command($cmd, outfunc => sub {}); };
|
||||
warn $@ if $@;
|
||||
}
|
||||
|
||||
# we do not acticate any volumes here ('vgchange -aly')
|
||||
# instead, volumes are activate individually later
|
||||
}
|
||||
|
||||
sub deactivate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
my $cmd = ['/sbin/vgchange', '-aln', $scfg->{vgname}];
|
||||
run_command($cmd, errmsg => "can't deactivate VG '$scfg->{vgname}'");
|
||||
}
|
||||
|
||||
sub activate_volume {
|
||||
my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
|
||||
|
||||
my $path = $class->path($scfg, $volname);
|
||||
|
||||
my $lvm_activate_mode = $exclusive ? 'ey' : 'ly';
|
||||
|
||||
my $cmd = ['/sbin/lvchange', "-a$lvm_activate_mode", $path];
|
||||
run_command($cmd, errmsg => "can't activate LV '$path'");
|
||||
}
|
||||
|
||||
sub deactivate_volume {
|
||||
my ($class, $storeid, $scfg, $volname, $cache) = @_;
|
||||
|
||||
my $path = $class->path($scfg, $volname);
|
||||
return if ! -b $path;
|
||||
|
||||
my $cmd = ['/sbin/lvchange', '-aln', $path];
|
||||
run_command($cmd, errmsg => "can't deactivate LV '$path'");
|
||||
}
|
||||
|
||||
1;
|
5
PVE/Storage/Makefile
Normal file
5
PVE/Storage/Makefile
Normal file
@ -0,0 +1,5 @@
|
||||
SOURCES=Plugin.pm DirPlugin.pm LVMPlugin.pm NFSPlugin.pm ISCSIPlugin.pm
|
||||
|
||||
.PHONY: install
|
||||
install:
|
||||
for i in ${SOURCES}; do install -D -m 0644 $$i ${DESTDIR}${PERLDIR}/PVE/Storage/$$i; done
|
163
PVE/Storage/NFSPlugin.pm
Normal file
163
PVE/Storage/NFSPlugin.pm
Normal file
@ -0,0 +1,163 @@
|
||||
package PVE::Storage::NFSPlugin;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use IO::File;
|
||||
use PVE::Storage::Plugin;
|
||||
use PVE::JSONSchema qw(get_standard_option);
|
||||
|
||||
use base qw(PVE::Storage::Plugin);
|
||||
|
||||
# NFS helper functions
|
||||
|
||||
sub read_proc_mounts {
|
||||
|
||||
local $/; # enable slurp mode
|
||||
|
||||
my $data = "";
|
||||
if (my $fd = IO::File->new("/proc/mounts", "r")) {
|
||||
$data = <$fd>;
|
||||
close ($fd);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
sub nfs_is_mounted {
|
||||
my ($server, $export, $mountpoint, $mountdata) = @_;
|
||||
|
||||
my $source = "$server:$export";
|
||||
|
||||
$mountdata = read_proc_mounts() if !$mountdata;
|
||||
|
||||
if ($mountdata =~ m|^$source/?\s$mountpoint\snfs|m) {
|
||||
return $mountpoint;
|
||||
}
|
||||
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub nfs_mount {
|
||||
my ($server, $export, $mountpoint, $options) = @_;
|
||||
|
||||
my $source = "$server:$export";
|
||||
|
||||
my $cmd = ['/bin/mount', '-t', 'nfs', $source, $mountpoint];
|
||||
if ($options) {
|
||||
push @$cmd, '-o', $options;
|
||||
}
|
||||
|
||||
run_command($cmd, errmsg => "mount error");
|
||||
}
|
||||
|
||||
# Configuration
|
||||
|
||||
sub type {
|
||||
return 'nfs';
|
||||
}
|
||||
|
||||
sub plugindata {
|
||||
return {
|
||||
content => [ { images => 1, rootdir => 1, vztmpl => 1, iso => 1, backup => 1},
|
||||
{ images => 1 }],
|
||||
format => [ { raw => 1, qcow2 => 1, vmdk => 1 } , 'raw' ],
|
||||
};
|
||||
}
|
||||
|
||||
sub properties {
|
||||
return {
|
||||
export => {
|
||||
description => "NFS export path.",
|
||||
type => 'string', format => 'pve-storage-path',
|
||||
},
|
||||
server => {
|
||||
description => "Server IP or DNS name.",
|
||||
type => 'string', format => 'pve-storage-server',
|
||||
},
|
||||
options => {
|
||||
description => "NFS mount options (see 'man nfs')",
|
||||
type => 'string', format => 'pve-storage-options',
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
sub options {
|
||||
return {
|
||||
path => { fixed => 1 },
|
||||
server => { fixed => 1 },
|
||||
export => { fixed => 1 },
|
||||
nodes => { optional => 1 },
|
||||
disable => { optional => 1 },
|
||||
maxfiles => { optional => 1 },
|
||||
options => { optional => 1 },
|
||||
content => { optional => 1 },
|
||||
format => { optional => 1 },
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
sub check_config {
|
||||
my ($class, $sectionId, $config, $create, $skipSchemaCheck) = @_;
|
||||
|
||||
$config->{path} = "/mnt/pve/$sectionId" if $create && !$config->{path};
|
||||
|
||||
return $class->SUPER::check_config($sectionId, $config, $create, $skipSchemaCheck);
|
||||
}
|
||||
|
||||
# Storage implementation
|
||||
|
||||
sub status {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
$cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
|
||||
|
||||
my $path = $scfg->{path};
|
||||
my $server = $scfg->{server};
|
||||
my $export = $scfg->{export};
|
||||
|
||||
return undef if !nfs_is_mounted($server, $export, $path, $cache->{mountdata});
|
||||
|
||||
return $class->SUPER::status($storeid, $scfg, $cache);
|
||||
}
|
||||
|
||||
sub activate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
$cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
|
||||
|
||||
my $path = $scfg->{path};
|
||||
my $server = $scfg->{server};
|
||||
my $export = $scfg->{export};
|
||||
|
||||
if (!nfs_is_mounted($server, $export, $path, $cache->{mountdata})) {
|
||||
|
||||
# NOTE: only call mkpath when not mounted (avoid hang
|
||||
# when NFS server is offline
|
||||
|
||||
mkpath $path;
|
||||
|
||||
die "unable to activate storage '$storeid' - " .
|
||||
"directory '$path' does not exist\n" if ! -d $path;
|
||||
|
||||
nfs_mount($server, $export, $path, $scfg->{options});
|
||||
}
|
||||
|
||||
$class->SUPER::activate_storage($storeid, $scfg, $cache);
|
||||
}
|
||||
|
||||
sub deactivate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
$cache->{mountdata} = read_proc_mounts() if !$cache->{mountdata};
|
||||
|
||||
my $path = $scfg->{path};
|
||||
my $server = $scfg->{server};
|
||||
my $export = $scfg->{export};
|
||||
|
||||
if (nfs_is_mounted($server, $export, $path, $cache->{mountdata})) {
|
||||
my $cmd = ['/bin/umount', $path];
|
||||
run_command($cmd, errmsg => 'umount error');
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
582
PVE/Storage/Plugin.pm
Normal file
582
PVE/Storage/Plugin.pm
Normal file
@ -0,0 +1,582 @@
|
||||
package PVE::Storage::Plugin;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
use File::Path;
|
||||
use PVE::Tools qw(run_command);
|
||||
use PVE::JSONSchema qw(get_standard_option);
|
||||
use PVE::Cluster qw(cfs_register_file);
|
||||
|
||||
use Data::Dumper;
|
||||
|
||||
use base qw(PVE::SectionConfig);
|
||||
|
||||
cfs_register_file ('storage.cfg',
|
||||
sub { __PACKAGE__->parse_config(@_); },
|
||||
sub { __PACKAGE__->write_config(@_); });
|
||||
|
||||
my $defaultData = {
|
||||
propertyList => {
|
||||
type => { description => "Storage type." },
|
||||
storage => get_standard_option('pve-storage-id'),
|
||||
nodes => get_standard_option('pve-node-list', { optional => 1 }),
|
||||
content => {
|
||||
description => "Allowed content types.",
|
||||
type => 'string', format => 'pve-storage-content-list',
|
||||
optional => 1,
|
||||
},
|
||||
disable => {
|
||||
description => "Flag to disable the storage.",
|
||||
type => 'boolean',
|
||||
optional => 1,
|
||||
},
|
||||
maxfiles => {
|
||||
description => "Maximal number of backup files per VM. Use '0' for unlimted.",
|
||||
type => 'integer',
|
||||
minimum => 0,
|
||||
optional => 1,
|
||||
},
|
||||
shared => {
|
||||
description => "Mark storage as shared.",
|
||||
type => 'boolean',
|
||||
optional => 1,
|
||||
},
|
||||
'format' => {
|
||||
description => "Default Image format.",
|
||||
type => 'string', format => 'pve-storage-format',
|
||||
optional => 1,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
sub content_hash_to_string {
|
||||
my $hash = shift;
|
||||
|
||||
my @cta;
|
||||
foreach my $ct (keys %$hash) {
|
||||
push @cta, $ct if $hash->{$ct};
|
||||
}
|
||||
|
||||
return join(',', @cta);
|
||||
}
|
||||
|
||||
sub valid_content_types {
|
||||
my ($type) = @_;
|
||||
|
||||
my $def = $defaultData->{plugindata}->{$type};
|
||||
|
||||
return {} if !$def;
|
||||
|
||||
return $def->{content}->[0];
|
||||
}
|
||||
|
||||
sub default_format {
|
||||
my ($scfg) = @_;
|
||||
|
||||
my $type = $scfg->{type};
|
||||
my $def = $defaultData->{plugindata}->{$type};
|
||||
|
||||
my $def_format = 'raw';
|
||||
my $valid_formats = [ $def_format ];
|
||||
|
||||
if (defined($def->{format})) {
|
||||
$def_format = $scfg->{format} || $def->{format}->[1];
|
||||
$valid_formats = [ sort keys %{$def->{format}->[0]} ];
|
||||
}
|
||||
|
||||
return wantarray ? ($def_format, $valid_formats) : $def_format;
|
||||
}
|
||||
|
||||
PVE::JSONSchema::register_format('pve-storage-path', \&verify_path);
|
||||
sub verify_path {
|
||||
my ($path, $noerr) = @_;
|
||||
|
||||
# fixme: exclude more shell meta characters?
|
||||
# we need absolute paths
|
||||
if ($path !~ m|^/[^;\(\)]+|) {
|
||||
return undef if $noerr;
|
||||
die "value does not look like a valid absolute path\n";
|
||||
}
|
||||
return $path;
|
||||
}
|
||||
|
||||
PVE::JSONSchema::register_format('pve-storage-server', \&verify_server);
|
||||
sub verify_server {
|
||||
my ($server, $noerr) = @_;
|
||||
|
||||
# fixme: use better regex ?
|
||||
# IP or DNS name
|
||||
if ($server !~ m/^[[:alnum:]\-\.]+$/) {
|
||||
return undef if $noerr;
|
||||
die "value does not look like a valid server name or IP address\n";
|
||||
}
|
||||
return $server;
|
||||
}
|
||||
|
||||
# fixme: do we need this
|
||||
#PVE::JSONSchema::register_format('pve-storage-portal', \&verify_portal);
|
||||
#sub verify_portal {
|
||||
# my ($portal, $noerr) = @_;
|
||||
#
|
||||
# # IP with optional port
|
||||
# if ($portal !~ m/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}(:\d+)?$/) {
|
||||
# return undef if $noerr;
|
||||
# die "value does not look like a valid portal address\n";
|
||||
# }
|
||||
# return $portal;
|
||||
#}
|
||||
|
||||
PVE::JSONSchema::register_format('pve-storage-portal-dns', \&verify_portal_dns);
|
||||
sub verify_portal_dns {
|
||||
my ($portal, $noerr) = @_;
|
||||
|
||||
# IP or DNS name with optional port
|
||||
if ($portal !~ m/^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|[[:alnum:]\-\.]+)(:\d+)?$/) {
|
||||
return undef if $noerr;
|
||||
die "value does not look like a valid portal address\n";
|
||||
}
|
||||
return $portal;
|
||||
}
|
||||
|
||||
PVE::JSONSchema::register_format('pve-storage-content', \&verify_content);
|
||||
sub verify_content {
|
||||
my ($ct, $noerr) = @_;
|
||||
|
||||
my $valid_content = valid_content_types('dir'); # dir includes all types
|
||||
|
||||
if (!$valid_content->{$ct}) {
|
||||
return undef if $noerr;
|
||||
die "invalid content type '$ct'\n";
|
||||
}
|
||||
|
||||
return $ct;
|
||||
}
|
||||
|
||||
PVE::JSONSchema::register_format('pve-storage-format', \&verify_format);
|
||||
sub verify_format {
|
||||
my ($fmt, $noerr) = @_;
|
||||
|
||||
if ($fmt !~ m/(raw|qcow2|vmdk)/) {
|
||||
return undef if $noerr;
|
||||
die "invalid format '$fmt'\n";
|
||||
}
|
||||
|
||||
return $fmt;
|
||||
}
|
||||
|
||||
PVE::JSONSchema::register_format('pve-storage-options', \&verify_options);
|
||||
sub verify_options {
|
||||
my ($value, $noerr) = @_;
|
||||
|
||||
# mount options (see man fstab)
|
||||
if ($value !~ m/^\S+$/) {
|
||||
return undef if $noerr;
|
||||
die "invalid options '$value'\n";
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
|
||||
sub private {
|
||||
return $defaultData;
|
||||
}
|
||||
|
||||
sub parse_section_header {
|
||||
my ($class, $line) = @_;
|
||||
|
||||
if ($line =~ m/^(\S+):\s*(\S+)\s*$/) {
|
||||
my ($type, $storeid) = (lc($1), $2);
|
||||
my $errmsg = undef; # set if you want to skip whole section
|
||||
eval { PVE::JSONSchema::parse_storage_id($storeid); };
|
||||
$errmsg = $@ if $@;
|
||||
my $config = {}; # to return additional attributes
|
||||
return ($type, $storeid, $errmsg, $config);
|
||||
}
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub decode_value {
|
||||
my ($class, $type, $key, $value) = @_;
|
||||
|
||||
my $def = $defaultData->{plugindata}->{$type};
|
||||
|
||||
if ($key eq 'content') {
|
||||
my $valid_content = $def->{content}->[0];
|
||||
|
||||
my $res = {};
|
||||
|
||||
foreach my $c (PVE::Tools::split_list($value)) {
|
||||
if (!$valid_content->{$c}) {
|
||||
die "storage does not support content type '$c'\n";
|
||||
}
|
||||
$res->{$c} = 1;
|
||||
}
|
||||
|
||||
if ($res->{none} && scalar (keys %$res) > 1) {
|
||||
die "unable to combine 'none' with other content types\n";
|
||||
}
|
||||
|
||||
return $res;
|
||||
} elsif ($key eq 'format') {
|
||||
my $valid_formats = $def->{format}->[0];
|
||||
|
||||
if (!$valid_formats->{$value}) {
|
||||
die "storage does not support format '$value'\n";
|
||||
}
|
||||
|
||||
return $value;
|
||||
} elsif ($key eq 'nodes') {
|
||||
my $res = {};
|
||||
|
||||
foreach my $node (PVE::Tools::split_list($value)) {
|
||||
if (PVE::JSONSchema::pve_verify_node_name($node)) {
|
||||
$res->{$node} = 1;
|
||||
}
|
||||
}
|
||||
|
||||
# fixme:
|
||||
# no node restrictions for local storage
|
||||
#if ($storeid && $storeid eq 'local' && scalar(keys(%$res))) {
|
||||
# die "storage '$storeid' does not allow node restrictions\n";
|
||||
#}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
sub encode_value {
|
||||
my ($class, $type, $key, $value) = @_;
|
||||
|
||||
if ($key eq 'nodes') {
|
||||
return join(',', keys(%$value));
|
||||
} elsif ($key eq 'content') {
|
||||
my $res = content_hash_to_string($value) || 'none';
|
||||
return $res;
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
sub parse_config {
|
||||
my ($class, $filename, $raw) = @_;
|
||||
|
||||
my $cfg = $class->SUPER::parse_config($filename, $raw);
|
||||
my $ids = $cfg->{ids};
|
||||
|
||||
# make sure we have a reasonable 'local:' storage
|
||||
# openvz expects things to be there
|
||||
if (!$ids->{local} || $ids->{local}->{type} ne 'dir' ||
|
||||
($ids->{local}->{path} && $ids->{local}->{path} ne '/var/lib/vz')) {
|
||||
$ids->{local} = {
|
||||
type => 'dir',
|
||||
priority => 0, # force first entry
|
||||
path => '/var/lib/vz',
|
||||
maxfiles => 0,
|
||||
content => { images => 1, rootdir => 1, vztmpl => 1, iso => 1},
|
||||
};
|
||||
}
|
||||
|
||||
# we always need this for OpenVZ
|
||||
$ids->{local}->{content}->{rootdir} = 1;
|
||||
$ids->{local}->{content}->{vztmpl} = 1;
|
||||
delete ($ids->{local}->{disable});
|
||||
|
||||
# make sure we have a path
|
||||
$ids->{local}->{path} = '/var/lib/vz' if !$ids->{local}->{path};
|
||||
|
||||
# remove node restrictions for local storage
|
||||
delete($ids->{local}->{nodes});
|
||||
|
||||
foreach my $storeid (keys %$ids) {
|
||||
my $d = $ids->{$storeid};
|
||||
my $type = $d->{type};
|
||||
|
||||
my $def = $defaultData->{plugindata}->{$type};
|
||||
|
||||
if ($def->{content}) {
|
||||
$d->{content} = $def->{content}->[1] if !$d->{content};
|
||||
}
|
||||
|
||||
if ($type eq 'iscsi' || $type eq 'nfs') {
|
||||
$d->{shared} = 1;
|
||||
}
|
||||
}
|
||||
|
||||
return $cfg;
|
||||
}
|
||||
|
||||
# Storage implementation
|
||||
|
||||
sub cluster_lock_storage {
|
||||
my ($class, $storeid, $shared, $timeout, $func, @param) = @_;
|
||||
|
||||
my $res;
|
||||
if (!$shared) {
|
||||
my $lockid = "pve-storage-$storeid";
|
||||
my $lockdir = "/var/lock/pve-manager";
|
||||
mkdir $lockdir;
|
||||
$res = PVE::Tools::lock_file("$lockdir/$lockid", $timeout, $func, @param);
|
||||
die $@ if $@;
|
||||
} else {
|
||||
$res = PVE::Cluster::cfs_lock_storage($storeid, $timeout, $func, @param);
|
||||
die $@ if $@;
|
||||
}
|
||||
return $res;
|
||||
}
|
||||
|
||||
sub parse_name_dir {
|
||||
my $name = shift;
|
||||
|
||||
if ($name =~ m!^([^/\s]+\.(raw|qcow2|vmdk))$!) {
|
||||
return ($1, $2);
|
||||
}
|
||||
|
||||
die "unable to parse volume filename '$name'\n";
|
||||
}
|
||||
|
||||
sub parse_volname {
|
||||
my ($class, $volname) = @_;
|
||||
|
||||
if ($volname =~ m!^(\d+)/(\S+)$!) {
|
||||
my ($vmid, $name) = ($1, $2);
|
||||
parse_name_dir($name);
|
||||
return ('images', $name, $vmid);
|
||||
} elsif ($volname =~ m!^iso/([^/]+\.[Ii][Ss][Oo])$!) {
|
||||
return ('iso', $1);
|
||||
} elsif ($volname =~ m!^vztmpl/([^/]+\.tar\.gz)$!) {
|
||||
return ('vztmpl', $1);
|
||||
} elsif ($volname =~ m!^rootdir/(\d+)$!) {
|
||||
return ('rootdir', $1, $1);
|
||||
} elsif ($volname =~ m!^backup/([^/]+(\.(tar|tar\.gz|tar\.lzo|tgz)))$!) {
|
||||
my $fn = $1;
|
||||
if ($fn =~ m/^vzdump-(openvz|qemu)-(\d+)-.+/) {
|
||||
return ('backup', $fn, $2);
|
||||
}
|
||||
return ('backup', $fn);
|
||||
}
|
||||
|
||||
die "unable to parse directory volume name '$volname'\n";
|
||||
}
|
||||
|
||||
my $vtype_subdirs = {
|
||||
images => 'images',
|
||||
rootdir => 'private',
|
||||
iso => 'template/iso',
|
||||
vztmpl => 'template/cache',
|
||||
backup => 'dump',
|
||||
};
|
||||
|
||||
sub get_subdir {
|
||||
my ($class, $scfg, $vtype) = @_;
|
||||
|
||||
my $path = $scfg->{path};
|
||||
|
||||
die "storage definintion has no path\n" if !$path;
|
||||
|
||||
my $subdir = $vtype_subdirs->{$vtype};
|
||||
|
||||
die "unknown vtype '$vtype'\n" if !defined($subdir);
|
||||
|
||||
return "$path/$subdir";
|
||||
}
|
||||
|
||||
sub path {
|
||||
my ($class, $scfg, $volname) = @_;
|
||||
|
||||
my ($vtype, $name, $vmid) = $class->parse_volname($volname);
|
||||
|
||||
my $dir = $class->get_subdir($scfg, $vtype);
|
||||
|
||||
$dir .= "/$vmid" if $vtype eq 'images';
|
||||
|
||||
my $path = "$dir/$name";
|
||||
|
||||
return wantarray ? ($path, $vmid, $vtype) : $path;
|
||||
}
|
||||
|
||||
sub alloc_image {
|
||||
my ($class, $storeid, $scfg, $vmid, $fmt, $name, $size) = @_;
|
||||
|
||||
my $imagedir = $class->get_subdir($scfg, 'images');
|
||||
$imagedir .= "/$vmid";
|
||||
|
||||
mkpath $imagedir;
|
||||
|
||||
if (!$name) {
|
||||
for (my $i = 1; $i < 100; $i++) {
|
||||
my @gr = <$imagedir/vm-$vmid-disk-$i.*>;
|
||||
if (!scalar(@gr)) {
|
||||
$name = "vm-$vmid-disk-$i.$fmt";
|
||||
last;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
die "unable to allocate an image name for VM $vmid in storage '$storeid'\n"
|
||||
if !$name;
|
||||
|
||||
my (undef, $tmpfmt) = parse_name_dir($name);
|
||||
|
||||
die "illegal name '$name' - wrong extension for format ('$tmpfmt != '$fmt')\n"
|
||||
if $tmpfmt ne $fmt;
|
||||
|
||||
my $path = "$imagedir/$name";
|
||||
|
||||
die "disk image '$path' already exists\n" if -e $path;
|
||||
|
||||
run_command("/usr/bin/qemu-img create -f $fmt '$path' ${size}K",
|
||||
errmsg => "unable to create image");
|
||||
|
||||
return "$vmid/$name";
|
||||
}
|
||||
|
||||
sub free_image {
|
||||
my ($class, $storeid, $scfg, $volname) = @_;
|
||||
|
||||
my $path = $class->path($scfg, $volname);
|
||||
|
||||
if (! -f $path) {
|
||||
warn "disk image '$path' does not exists\n";
|
||||
} else {
|
||||
unlink $path;
|
||||
}
|
||||
|
||||
return undef;
|
||||
}
|
||||
|
||||
sub file_size_info {
|
||||
my ($filename, $timeout) = @_;
|
||||
|
||||
my $cmd = ['/usr/bin/qemu-img', 'info', $filename];
|
||||
|
||||
my $format;
|
||||
my $size = 0;
|
||||
my $used = 0;
|
||||
|
||||
eval {
|
||||
run_command($cmd, timeout => $timeout, outfunc => sub {
|
||||
my $line = shift;
|
||||
|
||||
if ($line =~ m/^file format:\s+(\S+)\s*$/) {
|
||||
$format = $1;
|
||||
} elsif ($line =~ m/^virtual size:\s\S+\s+\((\d+)\s+bytes\)$/) {
|
||||
$size = int($1);
|
||||
} elsif ($line =~ m/^disk size:\s+(\d+(.\d+)?)([KMGT])\s*$/) {
|
||||
$used = $1;
|
||||
my $u = $3;
|
||||
|
||||
$used *= 1024 if $u eq 'K';
|
||||
$used *= (1024*1024) if $u eq 'M';
|
||||
$used *= (1024*1024*1024) if $u eq 'G';
|
||||
$used *= (1024*1024*1024*1024) if $u eq 'T';
|
||||
|
||||
$used = int($used);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
return wantarray ? ($size, $format, $used) : $size;
|
||||
}
|
||||
|
||||
sub list_images {
|
||||
my ($class, $storeid, $scfg, $vmid, $vollist, $cache) = @_;
|
||||
|
||||
my $imagedir = $class->get_subdir($scfg, 'images');
|
||||
|
||||
my ($defFmt, $vaidFmts) = default_format($scfg);
|
||||
my $fmts = join ('|', @$vaidFmts);
|
||||
|
||||
my $res = [];
|
||||
|
||||
foreach my $fn (<$imagedir/[0-9][0-9]*/*>) {
|
||||
|
||||
next if $fn !~ m!^(/.+/(\d+)/([^/]+\.($fmts)))$!;
|
||||
$fn = $1; # untaint
|
||||
|
||||
my $owner = $2;
|
||||
my $name = $3;
|
||||
my $volid = "$storeid:$owner/$name";
|
||||
|
||||
if ($vollist) {
|
||||
my $found = grep { $_ eq $volid } @$vollist;
|
||||
next if !$found;
|
||||
} else {
|
||||
next if defined($vmid) && ($owner ne $vmid);
|
||||
}
|
||||
|
||||
my ($size, $format, $used) = file_size_info($fn);
|
||||
|
||||
if ($format && $size) {
|
||||
push @$res, {
|
||||
volid => $volid, format => $format,
|
||||
size => $size, vmid => $owner, used => $used };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return $res;
|
||||
}
|
||||
|
||||
sub status {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
my $path = $scfg->{path};
|
||||
|
||||
die "storage definintion has no path\n" if !$path;
|
||||
|
||||
my $timeout = 2;
|
||||
my $res = PVE::Tools::df($path, $timeout);
|
||||
|
||||
return undef if !$res || !$res->{total};
|
||||
|
||||
return ($res->{total}, $res->{avail}, $res->{used}, 1);
|
||||
}
|
||||
|
||||
sub activate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
my $path = $scfg->{path};
|
||||
|
||||
die "storage definintion has no path\n" if !$path;
|
||||
|
||||
die "unable to activate storage '$storeid' - " .
|
||||
"directory '$path' does not exist\n" if ! -d $path;
|
||||
|
||||
if (defined($scfg->{content})) {
|
||||
foreach my $vtype (keys %$vtype_subdirs) {
|
||||
next if !defined($scfg->{content}->{$vtype});
|
||||
my $subdir = $class->get_subdir($scfg, $vtype);
|
||||
mkpath $subdir if $subdir ne $path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub deactivate_storage {
|
||||
my ($class, $storeid, $scfg, $cache) = @_;
|
||||
|
||||
# do nothing by default
|
||||
}
|
||||
|
||||
sub activate_volume {
|
||||
my ($class, $storeid, $scfg, $volname, $exclusive, $cache) = @_;
|
||||
|
||||
my $path = $class->path($scfg, $volname);
|
||||
|
||||
# check is volume exists
|
||||
if ($scfg->{path}) {
|
||||
die "volume '$storeid:$volname' does not exist\n" if ! -e $path;
|
||||
} else {
|
||||
die "volume '$storeid:$volname' does not exist\n" if ! -b $path;
|
||||
}
|
||||
}
|
||||
|
||||
sub deactivate_volume {
|
||||
my ($class, $storeid, $scfg, $volname, $cache) = @_;
|
||||
|
||||
# do nothing by default
|
||||
}
|
||||
|
||||
1;
|
Loading…
Reference in New Issue
Block a user